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