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