merge the formfield patch from ooo-build
[ooovba.git] / sfx2 / source / config / evntconf.cxx
blobec52cacbeb7ae259b6e65f8f306219971f8ace2b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: evntconf.cxx,v $
10 * $Revision: 1.32 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sfx2.hxx"
33 #include <vcl/msgbox.hxx>
34 #ifndef _SV_RESARY_HXX
35 #include <tools/resary.hxx>
36 #endif
37 #include <svtools/lstner.hxx>
38 #include <basic/basmgr.hxx>
39 #include <basic/sbmod.hxx>
40 #include <tools/urlobj.hxx>
41 #include <basic/sbx.hxx>
43 #include <sot/storage.hxx>
44 #include <svtools/securityoptions.hxx>
46 #ifndef _RTL_USTRING_
47 #include <rtl/ustring.h>
48 #endif
50 #include <com/sun/star/uno/Any.hxx>
51 #include <framework/eventsconfiguration.hxx>
52 #include <comphelper/processfactory.hxx>
54 #include <sfx2/evntconf.hxx>
56 #include <sfx2/macrconf.hxx>
57 #include <sfx2/docfile.hxx>
58 #include <sfx2/app.hxx>
59 #include <sfx2/objsh.hxx>
60 #include <sfx2/dispatch.hxx>
61 #include "config.hrc"
62 #include "sfxresid.hxx"
63 #include "eventsupplier.hxx"
65 #include <com/sun/star/beans/PropertyValue.hpp>
66 #include <com/sun/star/container/XNameReplace.hpp>
67 #include <com/sun/star/document/XEventsSupplier.hpp>
68 #include <com/sun/star/uno/Sequence.hxx>
69 #include <com/sun/star/uno/Reference.hxx>
71 // -----------------------------------------------------------------------
72 TYPEINIT1(SfxEventHint, SfxHint);
73 TYPEINIT1(SfxEventNamesItem, SfxPoolItem);
75 using namespace com::sun::star;
77 SfxEventNamesList& SfxEventNamesList::operator=( const SfxEventNamesList& rTbl )
79 DelDtor();
80 for (USHORT n=0; n<rTbl.Count(); n++ )
82 SfxEventName* pTmp = ((SfxEventNamesList&)rTbl).GetObject(n);
83 SfxEventName *pNew = new SfxEventName( *pTmp );
84 Insert( pNew, n );
86 return *this;
89 void SfxEventNamesList::DelDtor()
91 SfxEventName* pTmp = First();
92 while( pTmp )
94 delete pTmp;
95 pTmp = Next();
97 Clear();
100 int SfxEventNamesItem::operator==( const SfxPoolItem& rAttr ) const
102 DBG_ASSERT( SfxPoolItem::operator==(rAttr), "unequal types" );
104 const SfxEventNamesList& rOwn = aEventsList;
105 const SfxEventNamesList& rOther = ( (SfxEventNamesItem&) rAttr ).aEventsList;
107 if ( rOwn.Count() != rOther.Count() )
108 return FALSE;
110 for ( USHORT nNo = 0; nNo < rOwn.Count(); ++nNo )
112 const SfxEventName *pOwn = rOwn.GetObject(nNo);
113 const SfxEventName *pOther = rOther.GetObject(nNo);
114 if ( pOwn->mnId != pOther->mnId ||
115 pOwn->maEventName != pOther->maEventName ||
116 pOwn->maUIName != pOther->maUIName )
117 return FALSE;
120 return TRUE;
124 SfxItemPresentation SfxEventNamesItem::GetPresentation( SfxItemPresentation,
125 SfxMapUnit,
126 SfxMapUnit,
127 XubString &rText,
128 const IntlWrapper* ) const
130 rText.Erase();
131 return SFX_ITEM_PRESENTATION_NONE;
134 SfxPoolItem* SfxEventNamesItem::Clone( SfxItemPool *) const
136 return new SfxEventNamesItem(*this);
139 SfxPoolItem* SfxEventNamesItem::Create(SvStream &, USHORT) const
141 DBG_ERROR("not streamable!");
142 return new SfxEventNamesItem(Which());
145 SvStream& SfxEventNamesItem::Store(SvStream &rStream, USHORT ) const
147 DBG_ERROR("not streamable!");
148 return rStream;
151 USHORT SfxEventNamesItem::GetVersion( USHORT ) const
153 DBG_ERROR("not streamable!");
154 return 0;
157 void SfxEventNamesItem::AddEvent( const String& rName, const String& rUIName, USHORT nID )
159 aEventsList.Insert( new SfxEventName( nID, rName, rUIName.Len() ? rUIName : rName ) );
163 //==========================================================================
165 //--------------------------------------------------------------------------
166 uno::Any CreateEventData_Impl( const SvxMacro *pMacro )
169 This function converts a SvxMacro into an Any containing three
170 properties. These properties are EventType and Script. Possible
171 values for EventType ar StarBasic, JavaScript, ...
172 The Script property should contain the URL to the macro and looks
173 like "macro://./standard.module1.main()"
175 If pMacro is NULL, we return an empty property sequence, so PropagateEvent_Impl
176 can delete an event binding.
178 uno::Any aEventData;
180 if ( pMacro )
182 if ( pMacro->GetScriptType() == STARBASIC )
184 uno::Sequence < beans::PropertyValue > aProperties(3);
185 beans::PropertyValue *pValues = aProperties.getArray();
187 ::rtl::OUString aType = ::rtl::OUString::createFromAscii( STAR_BASIC );;
188 ::rtl::OUString aLib = pMacro->GetLibName();
189 ::rtl::OUString aMacro = pMacro->GetMacName();
191 pValues[ 0 ].Name = ::rtl::OUString::createFromAscii( PROP_EVENT_TYPE );
192 pValues[ 0 ].Value <<= aType;
194 pValues[ 1 ].Name = ::rtl::OUString::createFromAscii( PROP_LIBRARY );
195 pValues[ 1 ].Value <<= aLib;
197 pValues[ 2 ].Name = ::rtl::OUString::createFromAscii( PROP_MACRO_NAME );
198 pValues[ 2 ].Value <<= aMacro;
200 aEventData <<= aProperties;
202 else if ( pMacro->GetScriptType() == EXTENDED_STYPE )
204 uno::Sequence < beans::PropertyValue > aProperties(2);
205 beans::PropertyValue *pValues = aProperties.getArray();
207 ::rtl::OUString aLib = pMacro->GetLibName();
208 ::rtl::OUString aMacro = pMacro->GetMacName();
210 pValues[ 0 ].Name = ::rtl::OUString::createFromAscii( PROP_EVENT_TYPE );
211 pValues[ 0 ].Value <<= aLib;
213 pValues[ 1 ].Name = ::rtl::OUString::createFromAscii( PROP_SCRIPT );
214 pValues[ 1 ].Value <<= aMacro;
216 aEventData <<= aProperties;
218 else if ( pMacro->GetScriptType() == JAVASCRIPT )
220 uno::Sequence < beans::PropertyValue > aProperties(2);
221 beans::PropertyValue *pValues = aProperties.getArray();
223 ::rtl::OUString aMacro = pMacro->GetMacName();
225 pValues[ 0 ].Name = ::rtl::OUString::createFromAscii( PROP_EVENT_TYPE );
226 pValues[ 0 ].Value <<= ::rtl::OUString::createFromAscii(SVX_MACRO_LANGUAGE_JAVASCRIPT);
228 pValues[ 1 ].Name = ::rtl::OUString::createFromAscii( PROP_MACRO_NAME );
229 pValues[ 1 ].Value <<= aMacro;
231 aEventData <<= aProperties;
233 else
235 DBG_ERRORFILE( "CreateEventData_Impl(): ScriptType not supported!");
238 else
240 uno::Sequence < beans::PropertyValue > aProperties;
241 aEventData <<= aProperties;
244 return aEventData;
247 //--------------------------------------------------------------------------
248 void PropagateEvent_Impl( SfxObjectShell *pDoc, rtl::OUString aEventName, const SvxMacro* pMacro )
250 uno::Reference < document::XEventsSupplier > xSupplier;
251 if ( pDoc )
253 xSupplier = uno::Reference < document::XEventsSupplier >( pDoc->GetModel(), uno::UNO_QUERY );
255 else
257 xSupplier = uno::Reference < document::XEventsSupplier >
258 ( ::comphelper::getProcessServiceFactory()->createInstance(
259 rtl::OUString::createFromAscii("com.sun.star.frame.GlobalEventBroadcaster" )), uno::UNO_QUERY );
262 if ( xSupplier.is() )
264 uno::Reference < container::XNameReplace > xEvents = xSupplier->getEvents();
265 if ( aEventName.getLength() )
267 uno::Any aEventData = CreateEventData_Impl( pMacro );
271 xEvents->replaceByName( aEventName, aEventData );
273 catch( ::com::sun::star::lang::IllegalArgumentException )
274 { DBG_ERRORFILE( "PropagateEvents_Impl: caught IllegalArgumentException" ); }
275 catch( ::com::sun::star::container::NoSuchElementException )
276 { DBG_ERRORFILE( "PropagateEvents_Impl: caught NoSuchElementException" ); }
278 else {
279 DBG_WARNING( "PropagateEvents_Impl: Got unkown event" );
284 //--------------------------------------------------------------------------------------------------------
285 void SfxEventConfiguration::ConfigureEvent( rtl::OUString aName, const SvxMacro& rMacro, SfxObjectShell *pDoc )
287 SvxMacro *pMacro = NULL;
288 if ( rMacro.GetMacName().Len() )
289 pMacro = new SvxMacro( rMacro.GetMacName(), rMacro.GetLibName(), rMacro.GetScriptType() );
290 if ( pDoc )
292 PropagateEvent_Impl( pDoc, aName, pMacro );
294 else
296 PropagateEvent_Impl( NULL, aName, pMacro );
300 // -------------------------------------------------------------------------------------------------------
301 SvxMacro* SfxEventConfiguration::ConvertToMacro( const com::sun::star::uno::Any& rElement, SfxObjectShell* pDoc, BOOL bBlowUp )
303 return SfxEvents_Impl::ConvertToMacro( rElement, pDoc, bBlowUp );