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