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 <hintids.hxx>
21 #include <doc.hxx>
22 #include <docufld.hxx>
23 #include <unofldmid.h>
24 #include <com/sun/star/uri/UriReferenceFactory.hpp>
25 #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
27 #include <osl/diagnose.h>
28 
29 using namespace ::com::sun::star;
30 
33  m_pDoc(pDocument)
34 {
35 }
36 
37 std::unique_ptr<SwFieldType> SwMacroFieldType::Copy() const
38 {
39  return std::make_unique<SwMacroFieldType>(m_pDoc);
40 }
41 
43  const OUString& rLibAndName, const OUString& rText) :
44  SwField(pInitType), m_aMacro(rLibAndName), m_aText(rText), m_bIsScriptURL(false)
45 {
47 }
48 
49 OUString SwMacroField::ExpandImpl(SwRootFrame const*const) const
50 {
51  return m_aText ;
52 }
53 
54 std::unique_ptr<SwField> SwMacroField::Copy() const
55 {
56  return std::make_unique<SwMacroField>(static_cast<SwMacroFieldType*>(GetTyp()), m_aMacro, m_aText);
57 }
58 
60 {
61  return GetTyp()->GetName() + " " + m_aMacro;
62 }
63 
64 OUString SwMacroField::GetLibName() const
65 {
66  // if it is a Scripting Framework macro return an empty string
67  if (m_bIsScriptURL)
68  {
69  return OUString();
70  }
71 
72  if (!m_aMacro.isEmpty())
73  {
74  sal_Int32 nPos = m_aMacro.getLength();
75 
76  for (sal_Int32 i = 0; i < 3 && nPos > 0; i++)
77  while (m_aMacro[--nPos] != '.' && nPos > 0) ;
78 
79  return m_aMacro.copy(0, nPos);
80  }
81 
82  OSL_FAIL("No LibName");
83  return OUString();
84 }
85 
87 {
88  if (!m_aMacro.isEmpty())
89  {
90  if (m_bIsScriptURL)
91  {
92  return m_aMacro;
93  }
94  else
95  {
96  sal_Int32 nPos = m_aMacro.getLength();
97 
98  for (sal_Int32 i = 0; i < 3 && nPos > 0; i++)
99  while (m_aMacro[--nPos] != '.' && nPos > 0) ;
100 
101  return m_aMacro.copy( ++nPos );
102  }
103  }
104 
105  OSL_FAIL("No MacroName");
106  return OUString();
107 }
108 
110 {
111  if (m_bIsScriptURL)
112  {
113  return SvxMacro(m_aMacro, OUString(), EXTENDED_STYPE);
114  }
115  else
116  {
118  }
119 }
120 
122 void SwMacroField::SetPar1(const OUString& rStr)
123 {
124  m_aMacro = rStr;
126 }
127 
129 OUString SwMacroField::GetPar1() const
130 {
131  return m_aMacro;
132 }
133 
135 void SwMacroField::SetPar2(const OUString& rStr)
136 {
137  m_aText = rStr;
138 }
139 
141 OUString SwMacroField::GetPar2() const
142 {
143  return m_aText;
144 }
145 
146 bool SwMacroField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
147 {
148  switch( nWhichId )
149  {
150  case FIELD_PROP_PAR1:
151  rAny <<= GetMacroName();
152  break;
153  case FIELD_PROP_PAR2:
154  rAny <<= m_aText;
155  break;
156  case FIELD_PROP_PAR3:
157  rAny <<= GetLibName();
158  break;
159  case FIELD_PROP_PAR4:
160  rAny <<= m_bIsScriptURL ? GetMacroName() : OUString();
161  break;
162  default:
163  assert(false);
164  }
165  return true;
166 }
167 
168 bool SwMacroField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
169 {
170  OUString sTmp;
171  switch( nWhichId )
172  {
173  case FIELD_PROP_PAR1:
174  rAny >>= sTmp;
176  break;
177  case FIELD_PROP_PAR2:
178  rAny >>= m_aText;
179  break;
180  case FIELD_PROP_PAR3:
181  rAny >>= sTmp;
183  break;
184  case FIELD_PROP_PAR4:
185  rAny >>= m_aMacro;
187  break;
188  default:
189  assert(false);
190  }
191 
192  return true;
193 }
194 
197  OUString& rMacro,
198  const OUString& rMacroName,
199  const OUString& rLibraryName )
200 {
201  // concatenate library and name; use dot only if both strings have content
202  rMacro = rLibraryName;
203  if ( !rLibraryName.isEmpty() && !rMacroName.isEmpty() )
204  rMacro += ".";
205  rMacro += rMacroName;
206 }
207 
208 bool SwMacroField::isScriptURL( const OUString& str )
209 {
210  try
211  {
212  uno::Reference<uno::XComponentContext> xContext = ::comphelper::getProcessComponentContext();
213  uno::Reference<uri::XUriReferenceFactory> xFactory = uri::UriReferenceFactory::create(xContext);
214  uno::Reference<uri::XVndSunStarScriptUrl> xUrl(xFactory->parse(str), uno::UNO_QUERY);
215  return xUrl.is();
216  }
217  catch (...)
218  {
219  }
220  return false;
221 }
222 
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Instances of SwFields and those derived from it occur 0 to n times.
Definition: fldbas.hxx:233
EXTENDED_STYPE
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt16 nWhich) const override
Definition: macrofld.cxx:146
OUString m_aMacro
Definition: docufld.hxx:390
virtual std::unique_ptr< SwFieldType > Copy() const override
Definition: macrofld.cxx:37
virtual void SetPar2(const OUString &rStr) override
set macro text
Definition: macrofld.cxx:135
Base class of all fields.
Definition: fldbas.hxx:279
Definition: doc.hxx:185
OUString GetLibName() const
Definition: macrofld.cxx:64
The root element of a Writer document layout.
Definition: rootfrm.hxx:79
static bool isScriptURL(const OUString &str)
Definition: macrofld.cxx:208
virtual std::unique_ptr< SwField > Copy() const override
Definition: macrofld.cxx:54
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt16 nWhich) override
Definition: macrofld.cxx:168
virtual void SetPar1(const OUString &rStr) override
LibName and MacroName.
Definition: macrofld.cxx:122
virtual OUString GetPar1() const override
Library and FileName.
Definition: macrofld.cxx:129
int i
SwFieldIds
Definition: fldbas.hxx:38
virtual OUString ExpandImpl(SwRootFrame const *pLayout) const override
Definition: macrofld.cxx:49
OUString GetMacroName() const
Definition: macrofld.cxx:86
#define FIELD_PROP_PAR3
Definition: unofldmid.h:25
SvxMacro GetSvxMacro() const
Definition: macrofld.cxx:109
virtual OUString GetPar2() const override
Macrotext.
Definition: macrofld.cxx:141
SwFieldType * GetTyp() const
Definition: fldbas.hxx:382
SwMacroFieldType(SwDoc *)
Definition: macrofld.cxx:31
#define FIELD_PROP_PAR1
Definition: unofldmid.h:23
virtual OUString GetName() const
Only in derived classes.
Definition: fldbas.cxx:145
#define FIELD_PROP_PAR2
Definition: unofldmid.h:24
virtual OUString GetFieldName() const override
get name or content
Definition: macrofld.cxx:59
OUString m_aText
Definition: docufld.hxx:391
SwMacroField(SwMacroFieldType *, const OUString &rLibAndName, const OUString &rText)
Direct input, delete old value.
Definition: macrofld.cxx:42
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:196
bool m_bIsScriptURL
Definition: docufld.hxx:392
#define FIELD_PROP_PAR4
Definition: unofldmid.h:36
SwDoc *const m_pDoc
Definition: docufld.hxx:380
sal_Int32 nPos
STARBASIC