xref: /aoo41x/main/sw/source/core/fields/macrofld.cxx (revision efeef26f)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26 
27 #include <hintids.hxx>
28 #include <doc.hxx>
29 #include <docufld.hxx>
30 #include <unofldmid.h>
31 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 #include <com/sun/star/uri/XUriReferenceFactory.hpp>
33 #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
34 #include <comphelper/processfactory.hxx>
35 
36 
37 using namespace ::com::sun::star;
38 using ::rtl::OUString;
39 
40 /*--------------------------------------------------------------------
41 	Beschreibung: MacroFeldtypen
42  --------------------------------------------------------------------*/
43 
SwMacroFieldType(SwDoc * pDocument)44 SwMacroFieldType::SwMacroFieldType(SwDoc* pDocument)
45 	: SwFieldType( RES_MACROFLD ),
46 	  pDoc(pDocument)
47 {
48 }
49 
Copy() const50 SwFieldType* SwMacroFieldType::Copy() const
51 {
52 	SwMacroFieldType* pType = new SwMacroFieldType(pDoc);
53 	return pType;
54 }
55 
56 /*--------------------------------------------------------------------
57 	Beschreibung: Das Macrofeld selbst
58  --------------------------------------------------------------------*/
59 
SwMacroField(SwMacroFieldType * pInitType,const String & rLibAndName,const String & rTxt)60 SwMacroField::SwMacroField(SwMacroFieldType* pInitType,
61 						   const String& rLibAndName, const String& rTxt) :
62 	SwField(pInitType), aMacro(rLibAndName), aText(rTxt), bIsScriptURL(sal_False)
63 {
64     bIsScriptURL = isScriptURL(aMacro);
65 }
66 
Expand() const67 String SwMacroField::Expand() const
68 { 	// Button malen anstatt von
69 	return aText ;
70 }
71 
Copy() const72 SwField* SwMacroField::Copy() const
73 {
74 	return new SwMacroField((SwMacroFieldType*)GetTyp(), aMacro, aText);
75 }
76 
GetFieldName() const77 String SwMacroField::GetFieldName() const
78 {
79     String aStr(GetTyp()->GetName());
80     aStr += ' ';
81     aStr += aMacro;
82     return aStr;
83 }
84 
GetLibName() const85 String SwMacroField::GetLibName() const
86 {
87 	// if it is a Scripting Framework macro return an empty string
88 	if (bIsScriptURL)
89 	{
90 		return String();
91 	}
92 
93 	if (aMacro.Len())
94 	{
95 		sal_uInt16 nPos = aMacro.Len();
96 
97 		for (sal_uInt16 i = 0; i < 3 && nPos > 0; i++)
98 			while (aMacro.GetChar(--nPos) != '.' && nPos > 0) ;
99 
100 		return aMacro.Copy(0, nPos );
101 	}
102 
103 	DBG_ASSERT(0, "Kein Macroname vorhanden");
104 	return aEmptyStr;
105 }
106 
GetMacroName() const107 String SwMacroField::GetMacroName() const
108 {
109 	if (aMacro.Len())
110 	{
111 		if (bIsScriptURL)
112 		{
113 			return aMacro.Copy( 0 );
114 		}
115 		else
116 		{
117 			sal_uInt16 nPos = aMacro.Len();
118 
119 			for (sal_uInt16 i = 0; i < 3 && nPos > 0; i++)
120 				while (aMacro.GetChar(--nPos) != '.' && nPos > 0) ;
121 
122 			return aMacro.Copy( ++nPos );
123 		}
124 	}
125 
126 	DBG_ASSERT(0, "Kein Macroname vorhanden");
127 	return aEmptyStr;
128 }
129 
GetSvxMacro() const130 SvxMacro SwMacroField::GetSvxMacro() const
131 {
132   if (bIsScriptURL)
133 	{
134 		return SvxMacro(aMacro, String(), EXTENDED_STYPE);
135 	}
136 	else
137 	{
138 		return SvxMacro(GetMacroName(), GetLibName(), STARBASIC);
139 	}
140 }
141 
142 /*--------------------------------------------------------------------
143 	Beschreibung: LibName und MacroName
144  --------------------------------------------------------------------*/
145 
SetPar1(const String & rStr)146 void SwMacroField::SetPar1(const String& rStr)
147 {
148 	aMacro = rStr;
149 	bIsScriptURL = isScriptURL(aMacro);
150 }
151 
GetPar1() const152 const String& SwMacroField::GetPar1() const
153 {
154 	return aMacro;
155 }
156 
157 /*--------------------------------------------------------------------
158 	Beschreibung: Macrotext
159  --------------------------------------------------------------------*/
160 
SetPar2(const String & rStr)161 void SwMacroField::SetPar2(const String& rStr)
162 {
163 	aText = rStr;
164 }
165 
GetPar2() const166 String SwMacroField::GetPar2() const
167 {
168 	return aText;
169 }
170 
171 /*-----------------05.03.98 13:38-------------------
172 
173 --------------------------------------------------*/
QueryValue(uno::Any & rAny,sal_uInt16 nWhichId) const174 sal_Bool SwMacroField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
175 {
176     switch( nWhichId )
177 	{
178 	case FIELD_PROP_PAR1:
179 		rAny <<= OUString(GetMacroName());
180 		break;
181 	case FIELD_PROP_PAR2:
182 		rAny <<= OUString(aText);
183 		break;
184 	case FIELD_PROP_PAR3:
185 		rAny <<= OUString(GetLibName());
186 		break;
187 	case FIELD_PROP_PAR4:
188 		rAny <<= bIsScriptURL ? OUString(GetMacroName()): OUString();
189 		break;
190 	default:
191 		DBG_ERROR("illegal property");
192 	}
193 	return sal_True;
194 }
195 /*-----------------05.03.98 13:38-------------------
196 
197 --------------------------------------------------*/
PutValue(const uno::Any & rAny,sal_uInt16 nWhichId)198 sal_Bool SwMacroField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
199 {
200 	String sTmp;
201     switch( nWhichId )
202 	{
203 	case FIELD_PROP_PAR1:
204 		CreateMacroString( aMacro, ::GetString(rAny, sTmp), GetLibName());
205 		break;
206 	case FIELD_PROP_PAR2:
207 		::GetString( rAny, aText );
208 		break;
209 	case FIELD_PROP_PAR3:
210 		CreateMacroString(aMacro, GetMacroName(), ::GetString(rAny, sTmp) );
211 		break;
212 	case FIELD_PROP_PAR4:
213 		::GetString(rAny, aMacro);
214 		bIsScriptURL = isScriptURL(aMacro);
215 		break;
216 	default:
217 		DBG_ERROR("illegal property");
218 	}
219 
220 	return sal_True;
221 }
222 
223 // create an internally used macro name from the library and macro name parts
CreateMacroString(String & rMacro,const String & rMacroName,const String & rLibraryName)224 void SwMacroField::CreateMacroString(
225     String& rMacro,
226     const String& rMacroName,
227     const String& rLibraryName )
228 {
229     // concatenate library and name; use dot only if both strings have content
230     rMacro = rLibraryName;
231     if ( rLibraryName.Len() > 0 && rMacroName.Len() > 0 )
232         rMacro += '.';
233     rMacro += rMacroName;
234 }
235 
isScriptURL(const String & str)236 sal_Bool SwMacroField::isScriptURL( const String& str )
237 {
238 	uno::Reference< lang::XMultiServiceFactory > xSMgr =
239 		::comphelper::getProcessServiceFactory();
240 
241 	uno::Reference< uri::XUriReferenceFactory >
242 		xFactory( xSMgr->createInstance(
243 			OUString::createFromAscii(
244 				"com.sun.star.uri.UriReferenceFactory" ) ), uno::UNO_QUERY );
245 
246 	if ( xFactory.is() )
247 	{
248 		uno::Reference< uri::XVndSunStarScriptUrl >
249 			xUrl( xFactory->parse( str ), uno::UNO_QUERY );
250 
251 		if ( xUrl.is() )
252 		{
253 			return sal_True;
254 		}
255 	}
256 	return sal_False;
257 }
258