xref: /trunk/main/sfx2/source/config/evntconf.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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_sfx2.hxx"
30 #include <vcl/msgbox.hxx>
31 #include <tools/resary.hxx>
32 #include <svl/lstner.hxx>
33 #include <basic/basmgr.hxx>
34 #include <basic/sbmod.hxx>
35 #include <tools/urlobj.hxx>
36 #include <basic/sbx.hxx>
37 #include <sot/storage.hxx>
38 #include <unotools/securityoptions.hxx>
39 
40 #include <rtl/ustring.h>
41 #include <com/sun/star/uno/Any.hxx>
42 #include <framework/eventsconfiguration.hxx>
43 #include <comphelper/processfactory.hxx>
44 #include <sfx2/evntconf.hxx>
45 
46 #include <sfx2/docfile.hxx>
47 #include <sfx2/app.hxx>
48 #include <sfx2/objsh.hxx>
49 #include <sfx2/dispatch.hxx>
50 #include "sfx2/sfxresid.hxx"
51 #include "eventsupplier.hxx"
52 
53 #include <com/sun/star/beans/PropertyValue.hpp>
54 #include <com/sun/star/container/XNameReplace.hpp>
55 #include <com/sun/star/document/XEventsSupplier.hpp>
56 #include <com/sun/star/uno/Sequence.hxx>
57 #include <com/sun/star/uno/Reference.hxx>
58 
59 // -----------------------------------------------------------------------
60 TYPEINIT1(SfxEventHint, SfxHint);
61 TYPEINIT1(SfxEventNamesItem, SfxPoolItem);
62 TYPEINIT1(SfxViewEventHint, SfxEventHint);
63 
64 using namespace com::sun::star;
65 
66 SfxEventNamesList& SfxEventNamesList::operator=( const SfxEventNamesList& rTbl )
67 {
68     DelDtor();
69     for (sal_uInt16 n=0; n<rTbl.Count(); n++ )
70     {
71         SfxEventName* pTmp = ((SfxEventNamesList&)rTbl).GetObject(n);
72         SfxEventName *pNew = new SfxEventName( *pTmp );
73         Insert( pNew, n );
74     }
75     return *this;
76 }
77 
78 void SfxEventNamesList::DelDtor()
79 {
80     SfxEventName* pTmp = First();
81     while( pTmp )
82     {
83         delete pTmp;
84         pTmp = Next();
85     }
86     Clear();
87 }
88 
89 int SfxEventNamesItem::operator==( const SfxPoolItem& rAttr ) const
90 {
91     DBG_ASSERT( SfxPoolItem::operator==(rAttr), "unequal types" );
92 
93     const SfxEventNamesList& rOwn = aEventsList;
94     const SfxEventNamesList& rOther = ( (SfxEventNamesItem&) rAttr ).aEventsList;
95 
96     if ( rOwn.Count() != rOther.Count() )
97         return sal_False;
98 
99     for ( sal_uInt16 nNo = 0; nNo < rOwn.Count(); ++nNo )
100     {
101         const SfxEventName *pOwn = rOwn.GetObject(nNo);
102         const SfxEventName *pOther = rOther.GetObject(nNo);
103         if (    pOwn->mnId != pOther->mnId ||
104                 pOwn->maEventName != pOther->maEventName ||
105                 pOwn->maUIName != pOther->maUIName )
106             return sal_False;
107     }
108 
109     return sal_True;
110 
111 }
112 
113 SfxItemPresentation SfxEventNamesItem::GetPresentation( SfxItemPresentation,
114                                     SfxMapUnit,
115                                     SfxMapUnit,
116                                     XubString &rText,
117                                     const IntlWrapper* ) const
118 {
119     rText.Erase();
120     return SFX_ITEM_PRESENTATION_NONE;
121 }
122 
123 SfxPoolItem* SfxEventNamesItem::Clone( SfxItemPool *) const
124 {
125     return new SfxEventNamesItem(*this);
126 }
127 
128 SfxPoolItem* SfxEventNamesItem::Create(SvStream &, sal_uInt16) const
129 {
130     DBG_ERROR("not streamable!");
131     return new SfxEventNamesItem(Which());
132 }
133 
134 SvStream& SfxEventNamesItem::Store(SvStream &rStream, sal_uInt16 ) const
135 {
136     DBG_ERROR("not streamable!");
137     return rStream;
138 }
139 
140 sal_uInt16 SfxEventNamesItem::GetVersion( sal_uInt16 ) const
141 {
142     DBG_ERROR("not streamable!");
143     return 0;
144 }
145 
146 void SfxEventNamesItem::AddEvent( const String& rName, const String& rUIName, sal_uInt16 nID )
147 {
148     aEventsList.Insert( new SfxEventName( nID, rName, rUIName.Len() ? rUIName : rName ) );
149 }
150 
151 
152 //==========================================================================
153 
154 //--------------------------------------------------------------------------
155 uno::Any CreateEventData_Impl( const SvxMacro *pMacro )
156 {
157 /*
158     This function converts a SvxMacro into an Any containing three
159     properties. These properties are EventType and Script. Possible
160     values for EventType ar StarBasic, JavaScript, ...
161     The Script property should contain the URL to the macro and looks
162     like "macro://./standard.module1.main()"
163 
164     If pMacro is NULL, we return an empty property sequence, so PropagateEvent_Impl
165     can delete an event binding.
166 */
167     uno::Any aEventData;
168 
169     if ( pMacro )
170     {
171         if ( pMacro->GetScriptType() == STARBASIC )
172         {
173             uno::Sequence < beans::PropertyValue > aProperties(3);
174             beans::PropertyValue *pValues = aProperties.getArray();
175 
176             ::rtl::OUString aType = ::rtl::OUString::createFromAscii( STAR_BASIC );;
177             ::rtl::OUString aLib  = pMacro->GetLibName();
178             ::rtl::OUString aMacro = pMacro->GetMacName();
179 
180             pValues[ 0 ].Name = ::rtl::OUString::createFromAscii( PROP_EVENT_TYPE );
181             pValues[ 0 ].Value <<= aType;
182 
183             pValues[ 1 ].Name = ::rtl::OUString::createFromAscii( PROP_LIBRARY );
184             pValues[ 1 ].Value <<= aLib;
185 
186             pValues[ 2 ].Name = ::rtl::OUString::createFromAscii( PROP_MACRO_NAME );
187             pValues[ 2 ].Value <<= aMacro;
188 
189             aEventData <<= aProperties;
190         }
191         else if ( pMacro->GetScriptType() == EXTENDED_STYPE )
192         {
193             uno::Sequence < beans::PropertyValue > aProperties(2);
194             beans::PropertyValue *pValues = aProperties.getArray();
195 
196             ::rtl::OUString aLib   = pMacro->GetLibName();
197             ::rtl::OUString aMacro = pMacro->GetMacName();
198 
199             pValues[ 0 ].Name = ::rtl::OUString::createFromAscii( PROP_EVENT_TYPE );
200             pValues[ 0 ].Value <<= aLib;
201 
202             pValues[ 1 ].Name = ::rtl::OUString::createFromAscii( PROP_SCRIPT );
203             pValues[ 1 ].Value <<= aMacro;
204 
205             aEventData <<= aProperties;
206         }
207         else if ( pMacro->GetScriptType() == JAVASCRIPT )
208         {
209             uno::Sequence < beans::PropertyValue > aProperties(2);
210             beans::PropertyValue *pValues = aProperties.getArray();
211 
212             ::rtl::OUString aMacro  = pMacro->GetMacName();
213 
214             pValues[ 0 ].Name = ::rtl::OUString::createFromAscii( PROP_EVENT_TYPE );
215             pValues[ 0 ].Value <<= ::rtl::OUString::createFromAscii(SVX_MACRO_LANGUAGE_JAVASCRIPT);
216 
217             pValues[ 1 ].Name = ::rtl::OUString::createFromAscii( PROP_MACRO_NAME );
218             pValues[ 1 ].Value <<= aMacro;
219 
220             aEventData <<= aProperties;
221         }
222         else
223         {
224             DBG_ERRORFILE( "CreateEventData_Impl(): ScriptType not supported!");
225         }
226     }
227     else
228     {
229         uno::Sequence < beans::PropertyValue > aProperties;
230         aEventData <<= aProperties;
231     }
232 
233     return aEventData;
234 }
235 
236 //--------------------------------------------------------------------------
237 void PropagateEvent_Impl( SfxObjectShell *pDoc, rtl::OUString aEventName, const SvxMacro* pMacro )
238 {
239     uno::Reference < document::XEventsSupplier > xSupplier;
240     if ( pDoc )
241     {
242         xSupplier = uno::Reference < document::XEventsSupplier >( pDoc->GetModel(), uno::UNO_QUERY );
243     }
244     else
245     {
246         xSupplier = uno::Reference < document::XEventsSupplier >
247                 ( ::comphelper::getProcessServiceFactory()->createInstance(
248                 rtl::OUString::createFromAscii("com.sun.star.frame.GlobalEventBroadcaster" )), uno::UNO_QUERY );
249     }
250 
251     if ( xSupplier.is() )
252     {
253         uno::Reference < container::XNameReplace > xEvents = xSupplier->getEvents();
254         if ( aEventName.getLength() )
255         {
256             uno::Any aEventData = CreateEventData_Impl( pMacro );
257 
258             try
259             {
260                 xEvents->replaceByName( aEventName, aEventData );
261             }
262             catch( ::com::sun::star::lang::IllegalArgumentException )
263             { DBG_ERRORFILE( "PropagateEvents_Impl: caught IllegalArgumentException" ); }
264             catch( ::com::sun::star::container::NoSuchElementException )
265             { DBG_ERRORFILE( "PropagateEvents_Impl: caught NoSuchElementException" ); }
266         }
267         else {
268             DBG_WARNING( "PropagateEvents_Impl: Got unkown event" );
269         }
270     }
271 }
272 
273 //--------------------------------------------------------------------------------------------------------
274 void SfxEventConfiguration::ConfigureEvent( rtl::OUString aName, const SvxMacro& rMacro, SfxObjectShell *pDoc )
275 {
276     SvxMacro *pMacro = NULL;
277     if ( rMacro.GetMacName().Len() )
278         pMacro = new SvxMacro( rMacro.GetMacName(), rMacro.GetLibName(), rMacro.GetScriptType() );
279     if ( pDoc )
280     {
281         PropagateEvent_Impl( pDoc, aName, pMacro );
282     }
283     else
284     {
285         PropagateEvent_Impl( NULL, aName, pMacro );
286     }
287 }
288 
289 // -------------------------------------------------------------------------------------------------------
290 SvxMacro* SfxEventConfiguration::ConvertToMacro( const com::sun::star::uno::Any& rElement, SfxObjectShell* pDoc, sal_Bool bBlowUp )
291 {
292     return SfxEvents_Impl::ConvertToMacro( rElement, pDoc, bBlowUp );
293 }
294