xref: /aoo41x/main/sfx2/source/config/evntconf.cxx (revision d119d52d)
1*d119d52dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*d119d52dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*d119d52dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*d119d52dSAndrew Rist  * distributed with this work for additional information
6*d119d52dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*d119d52dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*d119d52dSAndrew Rist  * "License"); you may not use this file except in compliance
9*d119d52dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*d119d52dSAndrew Rist  *
11*d119d52dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*d119d52dSAndrew Rist  *
13*d119d52dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*d119d52dSAndrew Rist  * software distributed under the License is distributed on an
15*d119d52dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d119d52dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*d119d52dSAndrew Rist  * specific language governing permissions and limitations
18*d119d52dSAndrew Rist  * under the License.
19*d119d52dSAndrew Rist  *
20*d119d52dSAndrew Rist  *************************************************************/
21*d119d52dSAndrew Rist 
22*d119d52dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sfx2.hxx"
26cdf0e10cSrcweir #include <vcl/msgbox.hxx>
27cdf0e10cSrcweir #include <tools/resary.hxx>
28cdf0e10cSrcweir #include <svl/lstner.hxx>
29cdf0e10cSrcweir #include <basic/basmgr.hxx>
30cdf0e10cSrcweir #include <basic/sbmod.hxx>
31cdf0e10cSrcweir #include <tools/urlobj.hxx>
32cdf0e10cSrcweir #include <basic/sbx.hxx>
33cdf0e10cSrcweir #include <sot/storage.hxx>
34cdf0e10cSrcweir #include <unotools/securityoptions.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <rtl/ustring.h>
37cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
38cdf0e10cSrcweir #include <framework/eventsconfiguration.hxx>
39cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
40cdf0e10cSrcweir #include <sfx2/evntconf.hxx>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #include <sfx2/docfile.hxx>
43cdf0e10cSrcweir #include <sfx2/app.hxx>
44cdf0e10cSrcweir #include <sfx2/objsh.hxx>
45cdf0e10cSrcweir #include <sfx2/dispatch.hxx>
46cdf0e10cSrcweir #include "sfx2/sfxresid.hxx"
47cdf0e10cSrcweir #include "eventsupplier.hxx"
48cdf0e10cSrcweir 
49cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
50cdf0e10cSrcweir #include <com/sun/star/container/XNameReplace.hpp>
51cdf0e10cSrcweir #include <com/sun/star/document/XEventsSupplier.hpp>
52cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
53cdf0e10cSrcweir #include <com/sun/star/uno/Reference.hxx>
54cdf0e10cSrcweir 
55cdf0e10cSrcweir // -----------------------------------------------------------------------
56cdf0e10cSrcweir TYPEINIT1(SfxEventHint, SfxHint);
57cdf0e10cSrcweir TYPEINIT1(SfxEventNamesItem, SfxPoolItem);
58cdf0e10cSrcweir TYPEINIT1(SfxViewEventHint, SfxEventHint);
59cdf0e10cSrcweir 
60cdf0e10cSrcweir using namespace com::sun::star;
61cdf0e10cSrcweir 
operator =(const SfxEventNamesList & rTbl)62cdf0e10cSrcweir SfxEventNamesList& SfxEventNamesList::operator=( const SfxEventNamesList& rTbl )
63cdf0e10cSrcweir {
64cdf0e10cSrcweir 	DelDtor();
65cdf0e10cSrcweir 	for (sal_uInt16 n=0; n<rTbl.Count(); n++ )
66cdf0e10cSrcweir 	{
67cdf0e10cSrcweir 		SfxEventName* pTmp = ((SfxEventNamesList&)rTbl).GetObject(n);
68cdf0e10cSrcweir 		SfxEventName *pNew = new SfxEventName( *pTmp );
69cdf0e10cSrcweir 		Insert( pNew, n );
70cdf0e10cSrcweir 	}
71cdf0e10cSrcweir 	return *this;
72cdf0e10cSrcweir }
73cdf0e10cSrcweir 
DelDtor()74cdf0e10cSrcweir void SfxEventNamesList::DelDtor()
75cdf0e10cSrcweir {
76cdf0e10cSrcweir 	SfxEventName* pTmp = First();
77cdf0e10cSrcweir 	while( pTmp )
78cdf0e10cSrcweir 	{
79cdf0e10cSrcweir 		delete pTmp;
80cdf0e10cSrcweir 		pTmp = Next();
81cdf0e10cSrcweir 	}
82cdf0e10cSrcweir 	Clear();
83cdf0e10cSrcweir }
84cdf0e10cSrcweir 
operator ==(const SfxPoolItem & rAttr) const85cdf0e10cSrcweir int SfxEventNamesItem::operator==( const SfxPoolItem& rAttr ) const
86cdf0e10cSrcweir {
87cdf0e10cSrcweir 	DBG_ASSERT( SfxPoolItem::operator==(rAttr), "unequal types" );
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 	const SfxEventNamesList& rOwn = aEventsList;
90cdf0e10cSrcweir 	const SfxEventNamesList& rOther = ( (SfxEventNamesItem&) rAttr ).aEventsList;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 	if ( rOwn.Count() != rOther.Count() )
93cdf0e10cSrcweir 		return sal_False;
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 	for ( sal_uInt16 nNo = 0; nNo < rOwn.Count(); ++nNo )
96cdf0e10cSrcweir 	{
97cdf0e10cSrcweir 		const SfxEventName *pOwn = rOwn.GetObject(nNo);
98cdf0e10cSrcweir 		const SfxEventName *pOther = rOther.GetObject(nNo);
99cdf0e10cSrcweir 		if ( 	pOwn->mnId != pOther->mnId ||
100cdf0e10cSrcweir 				pOwn->maEventName != pOther->maEventName ||
101cdf0e10cSrcweir 				pOwn->maUIName != pOther->maUIName )
102cdf0e10cSrcweir 			return sal_False;
103cdf0e10cSrcweir 	}
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 	return sal_True;
106cdf0e10cSrcweir 
107cdf0e10cSrcweir }
108cdf0e10cSrcweir 
GetPresentation(SfxItemPresentation,SfxMapUnit,SfxMapUnit,XubString & rText,const IntlWrapper *) const109cdf0e10cSrcweir SfxItemPresentation SfxEventNamesItem::GetPresentation( SfxItemPresentation,
110cdf0e10cSrcweir 									SfxMapUnit,
111cdf0e10cSrcweir 									SfxMapUnit,
112cdf0e10cSrcweir 									XubString &rText,
113cdf0e10cSrcweir                                     const IntlWrapper* ) const
114cdf0e10cSrcweir {
115cdf0e10cSrcweir 	rText.Erase();
116cdf0e10cSrcweir 	return SFX_ITEM_PRESENTATION_NONE;
117cdf0e10cSrcweir }
118cdf0e10cSrcweir 
Clone(SfxItemPool *) const119cdf0e10cSrcweir SfxPoolItem* SfxEventNamesItem::Clone( SfxItemPool *) const
120cdf0e10cSrcweir {
121cdf0e10cSrcweir 	return new SfxEventNamesItem(*this);
122cdf0e10cSrcweir }
123cdf0e10cSrcweir 
Create(SvStream &,sal_uInt16) const124cdf0e10cSrcweir SfxPoolItem* SfxEventNamesItem::Create(SvStream &, sal_uInt16) const
125cdf0e10cSrcweir {
126cdf0e10cSrcweir 	DBG_ERROR("not streamable!");
127cdf0e10cSrcweir 	return new SfxEventNamesItem(Which());
128cdf0e10cSrcweir }
129cdf0e10cSrcweir 
Store(SvStream & rStream,sal_uInt16) const130cdf0e10cSrcweir SvStream& SfxEventNamesItem::Store(SvStream &rStream, sal_uInt16 ) const
131cdf0e10cSrcweir {
132cdf0e10cSrcweir 	DBG_ERROR("not streamable!");
133cdf0e10cSrcweir 	return rStream;
134cdf0e10cSrcweir }
135cdf0e10cSrcweir 
GetVersion(sal_uInt16) const136cdf0e10cSrcweir sal_uInt16 SfxEventNamesItem::GetVersion( sal_uInt16 ) const
137cdf0e10cSrcweir {
138cdf0e10cSrcweir 	DBG_ERROR("not streamable!");
139cdf0e10cSrcweir 	return 0;
140cdf0e10cSrcweir }
141cdf0e10cSrcweir 
AddEvent(const String & rName,const String & rUIName,sal_uInt16 nID)142cdf0e10cSrcweir void SfxEventNamesItem::AddEvent( const String& rName, const String& rUIName, sal_uInt16 nID )
143cdf0e10cSrcweir {
144cdf0e10cSrcweir 	aEventsList.Insert( new SfxEventName( nID, rName, rUIName.Len() ? rUIName : rName ) );
145cdf0e10cSrcweir }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 
148cdf0e10cSrcweir //==========================================================================
149cdf0e10cSrcweir 
150cdf0e10cSrcweir //--------------------------------------------------------------------------
CreateEventData_Impl(const SvxMacro * pMacro)151cdf0e10cSrcweir uno::Any CreateEventData_Impl( const SvxMacro *pMacro )
152cdf0e10cSrcweir {
153cdf0e10cSrcweir /*
154cdf0e10cSrcweir     This function converts a SvxMacro into an Any containing three
155cdf0e10cSrcweir     properties. These properties are EventType and Script. Possible
156cdf0e10cSrcweir     values for EventType ar StarBasic, JavaScript, ...
157cdf0e10cSrcweir     The Script property should contain the URL to the macro and looks
158cdf0e10cSrcweir     like "macro://./standard.module1.main()"
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     If pMacro is NULL, we return an empty property sequence, so PropagateEvent_Impl
161cdf0e10cSrcweir     can delete an event binding.
162cdf0e10cSrcweir */
163cdf0e10cSrcweir 	uno::Any aEventData;
164cdf0e10cSrcweir 
165cdf0e10cSrcweir     if ( pMacro )
166cdf0e10cSrcweir     {
167cdf0e10cSrcweir         if ( pMacro->GetScriptType() == STARBASIC )
168cdf0e10cSrcweir         {
169cdf0e10cSrcweir 			uno::Sequence < beans::PropertyValue > aProperties(3);
170cdf0e10cSrcweir             beans::PropertyValue *pValues = aProperties.getArray();
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 			::rtl::OUString aType = ::rtl::OUString::createFromAscii( STAR_BASIC );;
173cdf0e10cSrcweir             ::rtl::OUString aLib  = pMacro->GetLibName();
174cdf0e10cSrcweir             ::rtl::OUString aMacro = pMacro->GetMacName();
175cdf0e10cSrcweir 
176cdf0e10cSrcweir             pValues[ 0 ].Name = ::rtl::OUString::createFromAscii( PROP_EVENT_TYPE );
177cdf0e10cSrcweir             pValues[ 0 ].Value <<= aType;
178cdf0e10cSrcweir 
179cdf0e10cSrcweir             pValues[ 1 ].Name = ::rtl::OUString::createFromAscii( PROP_LIBRARY );
180cdf0e10cSrcweir             pValues[ 1 ].Value <<= aLib;
181cdf0e10cSrcweir 
182cdf0e10cSrcweir             pValues[ 2 ].Name = ::rtl::OUString::createFromAscii( PROP_MACRO_NAME );
183cdf0e10cSrcweir             pValues[ 2 ].Value <<= aMacro;
184cdf0e10cSrcweir 
185cdf0e10cSrcweir             aEventData <<= aProperties;
186cdf0e10cSrcweir         }
187cdf0e10cSrcweir         else if ( pMacro->GetScriptType() == EXTENDED_STYPE )
188cdf0e10cSrcweir         {
189cdf0e10cSrcweir             uno::Sequence < beans::PropertyValue > aProperties(2);
190cdf0e10cSrcweir             beans::PropertyValue *pValues = aProperties.getArray();
191cdf0e10cSrcweir 
192cdf0e10cSrcweir             ::rtl::OUString aLib   = pMacro->GetLibName();
193cdf0e10cSrcweir             ::rtl::OUString aMacro = pMacro->GetMacName();
194cdf0e10cSrcweir 
195cdf0e10cSrcweir             pValues[ 0 ].Name = ::rtl::OUString::createFromAscii( PROP_EVENT_TYPE );
196cdf0e10cSrcweir             pValues[ 0 ].Value <<= aLib;
197cdf0e10cSrcweir 
198cdf0e10cSrcweir             pValues[ 1 ].Name = ::rtl::OUString::createFromAscii( PROP_SCRIPT );
199cdf0e10cSrcweir             pValues[ 1 ].Value <<= aMacro;
200cdf0e10cSrcweir 
201cdf0e10cSrcweir             aEventData <<= aProperties;
202cdf0e10cSrcweir 		}
203cdf0e10cSrcweir         else if ( pMacro->GetScriptType() == JAVASCRIPT )
204cdf0e10cSrcweir         {
205cdf0e10cSrcweir             uno::Sequence < beans::PropertyValue > aProperties(2);
206cdf0e10cSrcweir             beans::PropertyValue *pValues = aProperties.getArray();
207cdf0e10cSrcweir 
208cdf0e10cSrcweir             ::rtl::OUString aMacro  = pMacro->GetMacName();
209cdf0e10cSrcweir 
210cdf0e10cSrcweir             pValues[ 0 ].Name = ::rtl::OUString::createFromAscii( PROP_EVENT_TYPE );
211cdf0e10cSrcweir             pValues[ 0 ].Value <<= ::rtl::OUString::createFromAscii(SVX_MACRO_LANGUAGE_JAVASCRIPT);
212cdf0e10cSrcweir 
213cdf0e10cSrcweir             pValues[ 1 ].Name = ::rtl::OUString::createFromAscii( PROP_MACRO_NAME );
214cdf0e10cSrcweir             pValues[ 1 ].Value <<= aMacro;
215cdf0e10cSrcweir 
216cdf0e10cSrcweir             aEventData <<= aProperties;
217cdf0e10cSrcweir 		}
218cdf0e10cSrcweir         else
219cdf0e10cSrcweir         {
220cdf0e10cSrcweir             DBG_ERRORFILE( "CreateEventData_Impl(): ScriptType not supported!");
221cdf0e10cSrcweir         }
222cdf0e10cSrcweir     }
223cdf0e10cSrcweir     else
224cdf0e10cSrcweir     {
225cdf0e10cSrcweir         uno::Sequence < beans::PropertyValue > aProperties;
226cdf0e10cSrcweir         aEventData <<= aProperties;
227cdf0e10cSrcweir     }
228cdf0e10cSrcweir 
229cdf0e10cSrcweir     return aEventData;
230cdf0e10cSrcweir }
231cdf0e10cSrcweir 
232cdf0e10cSrcweir //--------------------------------------------------------------------------
PropagateEvent_Impl(SfxObjectShell * pDoc,rtl::OUString aEventName,const SvxMacro * pMacro)233cdf0e10cSrcweir void PropagateEvent_Impl( SfxObjectShell *pDoc, rtl::OUString aEventName, const SvxMacro* pMacro )
234cdf0e10cSrcweir {
235cdf0e10cSrcweir 	uno::Reference < document::XEventsSupplier > xSupplier;
236cdf0e10cSrcweir 	if ( pDoc )
237cdf0e10cSrcweir 	{
238cdf0e10cSrcweir 		xSupplier = uno::Reference < document::XEventsSupplier >( pDoc->GetModel(), uno::UNO_QUERY );
239cdf0e10cSrcweir 	}
240cdf0e10cSrcweir 	else
241cdf0e10cSrcweir 	{
242cdf0e10cSrcweir 		xSupplier = uno::Reference < document::XEventsSupplier >
243cdf0e10cSrcweir                 ( ::comphelper::getProcessServiceFactory()->createInstance(
244cdf0e10cSrcweir 				rtl::OUString::createFromAscii("com.sun.star.frame.GlobalEventBroadcaster" )), uno::UNO_QUERY );
245cdf0e10cSrcweir 	}
246cdf0e10cSrcweir 
247cdf0e10cSrcweir     if ( xSupplier.is() )
248cdf0e10cSrcweir     {
249cdf0e10cSrcweir 		uno::Reference < container::XNameReplace > xEvents = xSupplier->getEvents();
250cdf0e10cSrcweir         if ( aEventName.getLength() )
251cdf0e10cSrcweir         {
252cdf0e10cSrcweir 			uno::Any aEventData = CreateEventData_Impl( pMacro );
253cdf0e10cSrcweir 
254cdf0e10cSrcweir             try
255cdf0e10cSrcweir             {
256cdf0e10cSrcweir                 xEvents->replaceByName( aEventName, aEventData );
257cdf0e10cSrcweir             }
258cdf0e10cSrcweir             catch( ::com::sun::star::lang::IllegalArgumentException )
259cdf0e10cSrcweir             { DBG_ERRORFILE( "PropagateEvents_Impl: caught IllegalArgumentException" ); }
260cdf0e10cSrcweir             catch( ::com::sun::star::container::NoSuchElementException )
261cdf0e10cSrcweir             { DBG_ERRORFILE( "PropagateEvents_Impl: caught NoSuchElementException" ); }
262cdf0e10cSrcweir         }
263cdf0e10cSrcweir         else {
264cdf0e10cSrcweir             DBG_WARNING( "PropagateEvents_Impl: Got unkown event" );
265cdf0e10cSrcweir         }
266cdf0e10cSrcweir     }
267cdf0e10cSrcweir }
268cdf0e10cSrcweir 
269cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------------
ConfigureEvent(rtl::OUString aName,const SvxMacro & rMacro,SfxObjectShell * pDoc)270cdf0e10cSrcweir void SfxEventConfiguration::ConfigureEvent( rtl::OUString aName, const SvxMacro& rMacro, SfxObjectShell *pDoc )
271cdf0e10cSrcweir {
272cdf0e10cSrcweir     SvxMacro *pMacro = NULL;
273cdf0e10cSrcweir     if ( rMacro.GetMacName().Len() )
274cdf0e10cSrcweir         pMacro = new SvxMacro( rMacro.GetMacName(), rMacro.GetLibName(), rMacro.GetScriptType() );
275cdf0e10cSrcweir     if ( pDoc )
276cdf0e10cSrcweir     {
277cdf0e10cSrcweir         PropagateEvent_Impl( pDoc, aName, pMacro );
278cdf0e10cSrcweir     }
279cdf0e10cSrcweir     else
280cdf0e10cSrcweir     {
281cdf0e10cSrcweir         PropagateEvent_Impl( NULL, aName, pMacro );
282cdf0e10cSrcweir     }
283cdf0e10cSrcweir }
284cdf0e10cSrcweir 
285cdf0e10cSrcweir // -------------------------------------------------------------------------------------------------------
ConvertToMacro(const com::sun::star::uno::Any & rElement,SfxObjectShell * pDoc,sal_Bool bBlowUp)286cdf0e10cSrcweir SvxMacro* SfxEventConfiguration::ConvertToMacro( const com::sun::star::uno::Any& rElement, SfxObjectShell* pDoc, sal_Bool bBlowUp )
287cdf0e10cSrcweir {
288cdf0e10cSrcweir 	return SfxEvents_Impl::ConvertToMacro( rElement, pDoc, bBlowUp );
289cdf0e10cSrcweir }
290