xref: /aoo42x/main/sw/source/core/fields/macrofld.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sw.hxx"
30 
31 #include <hintids.hxx>
32 #include <doc.hxx>
33 #include <docufld.hxx>
34 #include <unofldmid.h>
35 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 #include <com/sun/star/uri/XUriReferenceFactory.hpp>
37 #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
38 #include <comphelper/processfactory.hxx>
39 
40 
41 using namespace ::com::sun::star;
42 using ::rtl::OUString;
43 
44 /*--------------------------------------------------------------------
45 	Beschreibung: MacroFeldtypen
46  --------------------------------------------------------------------*/
47 
48 SwMacroFieldType::SwMacroFieldType(SwDoc* pDocument)
49 	: SwFieldType( RES_MACROFLD ),
50 	  pDoc(pDocument)
51 {
52 }
53 
54 SwFieldType* SwMacroFieldType::Copy() const
55 {
56 	SwMacroFieldType* pType = new SwMacroFieldType(pDoc);
57 	return pType;
58 }
59 
60 /*--------------------------------------------------------------------
61 	Beschreibung: Das Macrofeld selbst
62  --------------------------------------------------------------------*/
63 
64 SwMacroField::SwMacroField(SwMacroFieldType* pInitType,
65 						   const String& rLibAndName, const String& rTxt) :
66 	SwField(pInitType), aMacro(rLibAndName), aText(rTxt), bIsScriptURL(sal_False)
67 {
68     bIsScriptURL = isScriptURL(aMacro);
69 }
70 
71 String SwMacroField::Expand() const
72 { 	// Button malen anstatt von
73 	return aText ;
74 }
75 
76 SwField* SwMacroField::Copy() const
77 {
78 	return new SwMacroField((SwMacroFieldType*)GetTyp(), aMacro, aText);
79 }
80 
81 String SwMacroField::GetFieldName() const
82 {
83     String aStr(GetTyp()->GetName());
84     aStr += ' ';
85     aStr += aMacro;
86     return aStr;
87 }
88 
89 String SwMacroField::GetLibName() const
90 {
91 	// if it is a Scripting Framework macro return an empty string
92 	if (bIsScriptURL)
93 	{
94 		return String();
95 	}
96 
97 	if (aMacro.Len())
98 	{
99 		sal_uInt16 nPos = aMacro.Len();
100 
101 		for (sal_uInt16 i = 0; i < 3 && nPos > 0; i++)
102 			while (aMacro.GetChar(--nPos) != '.' && nPos > 0) ;
103 
104 		return aMacro.Copy(0, nPos );
105 	}
106 
107 	DBG_ASSERT(0, "Kein Macroname vorhanden");
108 	return aEmptyStr;
109 }
110 
111 String SwMacroField::GetMacroName() const
112 {
113 	if (aMacro.Len())
114 	{
115 		if (bIsScriptURL)
116 		{
117 			return aMacro.Copy( 0 );
118 		}
119 		else
120 		{
121 			sal_uInt16 nPos = aMacro.Len();
122 
123 			for (sal_uInt16 i = 0; i < 3 && nPos > 0; i++)
124 				while (aMacro.GetChar(--nPos) != '.' && nPos > 0) ;
125 
126 			return aMacro.Copy( ++nPos );
127 		}
128 	}
129 
130 	DBG_ASSERT(0, "Kein Macroname vorhanden");
131 	return aEmptyStr;
132 }
133 
134 SvxMacro SwMacroField::GetSvxMacro() const
135 {
136   if (bIsScriptURL)
137 	{
138 		return SvxMacro(aMacro, String(), EXTENDED_STYPE);
139 	}
140 	else
141 	{
142 		return SvxMacro(GetMacroName(), GetLibName(), STARBASIC);
143 	}
144 }
145 
146 /*--------------------------------------------------------------------
147 	Beschreibung: LibName und MacroName
148  --------------------------------------------------------------------*/
149 
150 void SwMacroField::SetPar1(const String& rStr)
151 {
152 	aMacro = rStr;
153 	bIsScriptURL = isScriptURL(aMacro);
154 }
155 
156 const String& SwMacroField::GetPar1() const
157 {
158 	return aMacro;
159 }
160 
161 /*--------------------------------------------------------------------
162 	Beschreibung: Macrotext
163  --------------------------------------------------------------------*/
164 
165 void SwMacroField::SetPar2(const String& rStr)
166 {
167 	aText = rStr;
168 }
169 
170 String SwMacroField::GetPar2() const
171 {
172 	return aText;
173 }
174 
175 /*-----------------05.03.98 13:38-------------------
176 
177 --------------------------------------------------*/
178 sal_Bool SwMacroField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
179 {
180     switch( nWhichId )
181 	{
182 	case FIELD_PROP_PAR1:
183 		rAny <<= OUString(GetMacroName());
184 		break;
185 	case FIELD_PROP_PAR2:
186 		rAny <<= OUString(aText);
187 		break;
188 	case FIELD_PROP_PAR3:
189 		rAny <<= OUString(GetLibName());
190 		break;
191 	case FIELD_PROP_PAR4:
192 		rAny <<= bIsScriptURL ? OUString(GetMacroName()): OUString();
193 		break;
194 	default:
195 		DBG_ERROR("illegal property");
196 	}
197 	return sal_True;
198 }
199 /*-----------------05.03.98 13:38-------------------
200 
201 --------------------------------------------------*/
202 sal_Bool SwMacroField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
203 {
204 	String sTmp;
205     switch( nWhichId )
206 	{
207 	case FIELD_PROP_PAR1:
208 		CreateMacroString( aMacro, ::GetString(rAny, sTmp), GetLibName());
209 		break;
210 	case FIELD_PROP_PAR2:
211 		::GetString( rAny, aText );
212 		break;
213 	case FIELD_PROP_PAR3:
214 		CreateMacroString(aMacro, GetMacroName(), ::GetString(rAny, sTmp) );
215 		break;
216 	case FIELD_PROP_PAR4:
217 		::GetString(rAny, aMacro);
218 		bIsScriptURL = isScriptURL(aMacro);
219 		break;
220 	default:
221 		DBG_ERROR("illegal property");
222 	}
223 
224 	return sal_True;
225 }
226 
227 // create an internally used macro name from the library and macro name parts
228 void SwMacroField::CreateMacroString(
229     String& rMacro,
230     const String& rMacroName,
231     const String& rLibraryName )
232 {
233     // concatenate library and name; use dot only if both strings have content
234     rMacro = rLibraryName;
235     if ( rLibraryName.Len() > 0 && rMacroName.Len() > 0 )
236         rMacro += '.';
237     rMacro += rMacroName;
238 }
239 
240 sal_Bool SwMacroField::isScriptURL( const String& str )
241 {
242 	uno::Reference< lang::XMultiServiceFactory > xSMgr =
243 		::comphelper::getProcessServiceFactory();
244 
245 	uno::Reference< uri::XUriReferenceFactory >
246 		xFactory( xSMgr->createInstance(
247 			OUString::createFromAscii(
248 				"com.sun.star.uri.UriReferenceFactory" ) ), uno::UNO_QUERY );
249 
250 	if ( xFactory.is() )
251 	{
252 		uno::Reference< uri::XVndSunStarScriptUrl >
253 			xUrl( xFactory->parse( str ), uno::UNO_QUERY );
254 
255 		if ( xUrl.is() )
256 		{
257 			return sal_True;
258 		}
259 	}
260 	return sal_False;
261 }
262