LibreOffice Module sw (master)  1
macrofld.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <doc.hxx>
21 #include <docufld.hxx>
22 #include <unofldmid.h>
23 #include <com/sun/star/uri/UriReferenceFactory.hpp>
24 #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
26 #include <osl/diagnose.h>
27 
28 using namespace ::com::sun::star;
29 
32  m_pDoc(pDocument)
33 {
34 }
35 
36 std::unique_ptr<SwFieldType> SwMacroFieldType::Copy() const
37 {
38  return std::make_unique<SwMacroFieldType>(m_pDoc);
39 }
40 
42  const OUString& rLibAndName, const OUString& rText) :
43  SwField(pInitType), m_aMacro(rLibAndName), m_aText(rText), m_bIsScriptURL(false)
44 {
46 }
47 
48 OUString SwMacroField::ExpandImpl(SwRootFrame const*const) const
49 {
50  return m_aText ;
51 }
52 
53 std::unique_ptr<SwField> SwMacroField::Copy() const
54 {
55  return std::make_unique<SwMacroField>(static_cast<SwMacroFieldType*>(GetTyp()), m_aMacro, m_aText);
56 }
57 
59 {
60  return GetTyp()->GetName() + " " + m_aMacro;
61 }
62 
63 OUString SwMacroField::GetLibName() const
64 {
65  // if it is a Scripting Framework macro return an empty string
66  if (m_bIsScriptURL)
67  {
68  return OUString();
69  }
70 
71  if (!m_aMacro.isEmpty())
72  {
73  sal_Int32 nPos = m_aMacro.getLength();
74 
75  for (sal_Int32 i = 0; i < 3 && nPos > 0; i++)
76  while (m_aMacro[--nPos] != '.' && nPos > 0) ;
77 
78  return m_aMacro.copy(0, nPos);
79  }
80 
81  OSL_FAIL("No LibName");
82  return OUString();
83 }
84 
86 {
87  if (!m_aMacro.isEmpty())
88  {
89  if (m_bIsScriptURL)
90  {
91  return m_aMacro;
92  }
93  else
94  {
95  sal_Int32 nPos = m_aMacro.getLength();
96 
97  for (sal_Int32 i = 0; i < 3 && nPos > 0; i++)
98  while (m_aMacro[--nPos] != '.' && nPos > 0) ;
99 
100  return m_aMacro.copy( ++nPos );
101  }
102  }
103 
104  OSL_FAIL("No MacroName");
105  return OUString();
106 }
107 
109 {
110  if (m_bIsScriptURL)
111  {
112  return SvxMacro(m_aMacro, OUString(), EXTENDED_STYPE);
113  }
114  else
115  {
117  }
118 }
119 
121 void SwMacroField::SetPar1(const OUString& rStr)
122 {
123  m_aMacro = rStr;
125 }
126 
128 OUString SwMacroField::GetPar1() const
129 {
130  return m_aMacro;
131 }
132 
134 void SwMacroField::SetPar2(const OUString& rStr)
135 {
136  m_aText = rStr;
137 }
138 
140 OUString SwMacroField::GetPar2() const
141 {
142  return m_aText;
143 }
144 
145 bool SwMacroField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
146 {
147  switch( nWhichId )
148  {
149  case FIELD_PROP_PAR1:
150  rAny <<= GetMacroName();
151  break;
152  case FIELD_PROP_PAR2:
153  rAny <<= m_aText;
154  break;
155  case FIELD_PROP_PAR3:
156  rAny <<= GetLibName();
157  break;
158  case FIELD_PROP_PAR4:
159  rAny <<= m_bIsScriptURL ? GetMacroName() : OUString();
160  break;
161  default:
162  assert(false);
163  }
164  return true;
165 }
166 
167 bool SwMacroField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
168 {
169  OUString sTmp;
170  switch( nWhichId )
171  {
172  case FIELD_PROP_PAR1:
173  rAny >>= sTmp;
175  break;
176  case FIELD_PROP_PAR2:
177  rAny >>= m_aText;
178  break;
179  case FIELD_PROP_PAR3:
180  rAny >>= sTmp;
182  break;
183  case FIELD_PROP_PAR4:
184  rAny >>= m_aMacro;
186  break;
187  default:
188  assert(false);
189  }
190 
191  return true;
192 }
193 
196  OUString& rMacro,
197  const OUString& rMacroName,
198  const OUString& rLibraryName )
199 {
200  // concatenate library and name; use dot only if both strings have content
201  rMacro = rLibraryName;
202  if ( !rLibraryName.isEmpty() && !rMacroName.isEmpty() )
203  rMacro += ".";
204  rMacro += rMacroName;
205 }
206 
207 bool SwMacroField::isScriptURL( const OUString& str )
208 {
209  try
210  {
211  uno::Reference<uno::XComponentContext> xContext = ::comphelper::getProcessComponentContext();
212  uno::Reference<uri::XUriReferenceFactory> xFactory = uri::UriReferenceFactory::create(xContext);
213  uno::Reference<uri::XVndSunStarScriptUrl> xUrl(xFactory->parse(str), uno::UNO_QUERY);
214  return xUrl.is();
215  }
216  catch (...)
217  {
218  }
219  return false;
220 }
221 
222 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Instances of SwFields and those derived from it occur 0 to n times.
Definition: fldbas.hxx:240
EXTENDED_STYPE
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt16 nWhich) const override
Definition: macrofld.cxx:145
OUString m_aMacro
Definition: docufld.hxx:390
virtual std::unique_ptr< SwFieldType > Copy() const override
Definition: macrofld.cxx:36
virtual void SetPar2(const OUString &rStr) override
set macro text
Definition: macrofld.cxx:134
Base class of all fields.
Definition: fldbas.hxx:293
Definition: doc.hxx:184
OUString GetLibName() const
Definition: macrofld.cxx:63
The root element of a Writer document layout.
Definition: rootfrm.hxx:79
SwDoc * m_pDoc
Definition: docbm.cxx:1190
static bool isScriptURL(const OUString &str)
Definition: macrofld.cxx:207
virtual std::unique_ptr< SwField > Copy() const override
Definition: macrofld.cxx:53
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt16 nWhich) override
Definition: macrofld.cxx:167
virtual void SetPar1(const OUString &rStr) override
LibName and MacroName.
Definition: macrofld.cxx:121
int i
virtual OUString GetPar1() const override
Library and FileName.
Definition: macrofld.cxx:128
SwFieldIds
Definition: fldbas.hxx:44
virtual OUString ExpandImpl(SwRootFrame const *pLayout) const override
Definition: macrofld.cxx:48
OUString GetMacroName() const
Definition: macrofld.cxx:85
SwDoc * m_pDoc
Definition: docufld.hxx:380
#define FIELD_PROP_PAR3
Definition: unofldmid.h:25
SvxMacro GetSvxMacro() const
Definition: macrofld.cxx:108
virtual OUString GetPar2() const override
Macrotext.
Definition: macrofld.cxx:140
SwFieldType * GetTyp() const
Definition: fldbas.hxx:396
SwMacroFieldType(SwDoc *)
Definition: macrofld.cxx:30
#define FIELD_PROP_PAR1
Definition: unofldmid.h:23
virtual OUString GetName() const
Only in derived classes.
Definition: fldbas.cxx:135
#define FIELD_PROP_PAR2
Definition: unofldmid.h:24
virtual OUString GetFieldName() const override
get name or content
Definition: macrofld.cxx:58
OUString m_aText
Definition: docufld.hxx:391
SwMacroField(SwMacroFieldType *, const OUString &rLibAndName, const OUString &rText)
Direct input, delete old value.
Definition: macrofld.cxx:41
static void CreateMacroString(OUString &rMacro, const OUString &rMacroName, const OUString &rLibraryName)
create an internally used macro name from the library and macro name parts
Definition: macrofld.cxx:195
bool m_bIsScriptURL
Definition: docufld.hxx:392
Reference< XSingleServiceFactory > xFactory
#define FIELD_PROP_PAR4
Definition: unofldmid.h:36
STARBASIC
sal_uInt16 nPos