Line data Source code
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 :
21 : #include <hintids.hxx>
22 : #include <doc.hxx>
23 : #include <docufld.hxx>
24 : #include <unofldmid.h>
25 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 : #include <com/sun/star/uri/UriReferenceFactory.hpp>
27 : #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
28 : #include <comphelper/processfactory.hxx>
29 :
30 :
31 : using namespace ::com::sun::star;
32 : using ::rtl::OUString;
33 :
34 : /*--------------------------------------------------------------------
35 : Beschreibung: MacroFeldtypen
36 : --------------------------------------------------------------------*/
37 :
38 276 : SwMacroFieldType::SwMacroFieldType(SwDoc* pDocument)
39 : : SwFieldType( RES_MACROFLD ),
40 276 : pDoc(pDocument)
41 : {
42 276 : }
43 :
44 0 : SwFieldType* SwMacroFieldType::Copy() const
45 : {
46 0 : SwMacroFieldType* pType = new SwMacroFieldType(pDoc);
47 0 : return pType;
48 : }
49 :
50 : /*--------------------------------------------------------------------
51 : Beschreibung: Das Macrofeld selbst
52 : --------------------------------------------------------------------*/
53 :
54 0 : SwMacroField::SwMacroField(SwMacroFieldType* pInitType,
55 : const String& rLibAndName, const String& rTxt) :
56 0 : SwField(pInitType), aMacro(rLibAndName), aText(rTxt), bIsScriptURL(sal_False)
57 : {
58 0 : bIsScriptURL = isScriptURL(aMacro);
59 0 : }
60 :
61 0 : String SwMacroField::Expand() const
62 : { // Button malen anstatt von
63 0 : return aText ;
64 : }
65 :
66 0 : SwField* SwMacroField::Copy() const
67 : {
68 0 : return new SwMacroField((SwMacroFieldType*)GetTyp(), aMacro, aText);
69 : }
70 :
71 0 : String SwMacroField::GetFieldName() const
72 : {
73 0 : String aStr(GetTyp()->GetName());
74 0 : aStr += ' ';
75 0 : aStr += aMacro;
76 0 : return aStr;
77 : }
78 :
79 0 : String SwMacroField::GetLibName() const
80 : {
81 : // if it is a Scripting Framework macro return an empty string
82 0 : if (bIsScriptURL)
83 : {
84 0 : return String();
85 : }
86 :
87 0 : if (!aMacro.isEmpty())
88 : {
89 0 : sal_Int32 nPos = aMacro.getLength();
90 :
91 0 : for (sal_Int32 i = 0; i < 3 && nPos > 0; i++)
92 0 : while (aMacro[--nPos] != '.' && nPos > 0) ;
93 :
94 0 : return aMacro.copy(0, nPos);
95 : }
96 :
97 : OSL_FAIL("Kein Macroname vorhanden");
98 0 : return aEmptyStr;
99 : }
100 :
101 0 : String SwMacroField::GetMacroName() const
102 : {
103 0 : if (!aMacro.isEmpty())
104 : {
105 0 : if (bIsScriptURL)
106 : {
107 0 : return aMacro;
108 : }
109 : else
110 : {
111 0 : sal_Int32 nPos = aMacro.getLength();
112 :
113 0 : for (sal_Int32 i = 0; i < 3 && nPos > 0; i++)
114 0 : while (aMacro[--nPos] != '.' && nPos > 0) ;
115 :
116 0 : return aMacro.copy( ++nPos );
117 : }
118 : }
119 :
120 : OSL_FAIL("Kein Macroname vorhanden");
121 0 : return aEmptyStr;
122 : }
123 :
124 0 : SvxMacro SwMacroField::GetSvxMacro() const
125 : {
126 0 : if (bIsScriptURL)
127 : {
128 0 : return SvxMacro(aMacro, String(), EXTENDED_STYPE);
129 : }
130 : else
131 : {
132 0 : return SvxMacro(GetMacroName(), GetLibName(), STARBASIC);
133 : }
134 : }
135 :
136 : /*--------------------------------------------------------------------
137 : Beschreibung: LibName und MacroName
138 : --------------------------------------------------------------------*/
139 :
140 0 : void SwMacroField::SetPar1(const rtl::OUString& rStr)
141 : {
142 0 : aMacro = rStr;
143 0 : bIsScriptURL = isScriptURL(aMacro);
144 0 : }
145 :
146 0 : const rtl::OUString& SwMacroField::GetPar1() const
147 : {
148 0 : return aMacro;
149 : }
150 :
151 : /*--------------------------------------------------------------------
152 : Beschreibung: Macrotext
153 : --------------------------------------------------------------------*/
154 :
155 0 : void SwMacroField::SetPar2(const rtl::OUString& rStr)
156 : {
157 0 : aText = rStr;
158 0 : }
159 :
160 0 : rtl::OUString SwMacroField::GetPar2() const
161 : {
162 0 : return aText;
163 : }
164 :
165 0 : bool SwMacroField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
166 : {
167 0 : switch( nWhichId )
168 : {
169 : case FIELD_PROP_PAR1:
170 0 : rAny <<= OUString(GetMacroName());
171 0 : break;
172 : case FIELD_PROP_PAR2:
173 0 : rAny <<= OUString(aText);
174 0 : break;
175 : case FIELD_PROP_PAR3:
176 0 : rAny <<= OUString(GetLibName());
177 0 : break;
178 : case FIELD_PROP_PAR4:
179 0 : rAny <<= bIsScriptURL ? OUString(GetMacroName()): OUString();
180 0 : break;
181 : default:
182 : OSL_FAIL("illegal property");
183 : }
184 0 : return true;
185 : }
186 :
187 0 : bool SwMacroField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
188 : {
189 0 : rtl::OUString sTmp;
190 0 : switch( nWhichId )
191 : {
192 : case FIELD_PROP_PAR1:
193 0 : rAny >>= sTmp;
194 0 : CreateMacroString( aMacro, sTmp, GetLibName());
195 0 : break;
196 : case FIELD_PROP_PAR2:
197 0 : rAny >>= aText;
198 0 : break;
199 : case FIELD_PROP_PAR3:
200 0 : rAny >>= sTmp;
201 0 : CreateMacroString(aMacro, GetMacroName(), sTmp );
202 0 : break;
203 : case FIELD_PROP_PAR4:
204 0 : rAny >>= aMacro;
205 0 : bIsScriptURL = isScriptURL(aMacro);
206 0 : break;
207 : default:
208 : OSL_FAIL("illegal property");
209 : }
210 :
211 0 : return true;
212 : }
213 :
214 : // create an internally used macro name from the library and macro name parts
215 0 : void SwMacroField::CreateMacroString(
216 : rtl::OUString& rMacro,
217 : const String& rMacroName,
218 : const String& rLibraryName )
219 : {
220 : // concatenate library and name; use dot only if both strings have content
221 0 : rMacro = rLibraryName;
222 0 : if ( rLibraryName.Len() > 0 && rMacroName.Len() > 0 )
223 0 : rMacro += rtl::OUString('.');
224 0 : rMacro += rMacroName;
225 0 : }
226 :
227 0 : sal_Bool SwMacroField::isScriptURL( const String& str )
228 : {
229 : uno::Reference< uno::XComponentContext > xContext =
230 0 : ::comphelper::getProcessComponentContext();
231 :
232 : uno::Reference< uri::XUriReferenceFactory >
233 0 : xFactory = uri::UriReferenceFactory::create( xContext );
234 :
235 : uno::Reference< uri::XVndSunStarScriptUrl >
236 0 : xUrl( xFactory->parse( str ), uno::UNO_QUERY );
237 :
238 0 : if ( xUrl.is() )
239 : {
240 0 : return sal_True;
241 : }
242 0 : return sal_False;
243 : }
244 :
245 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|