Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <boost/scoped_ptr.hpp>
22 :
23 : #include <vcl/msgbox.hxx>
24 : #include <svl/lstner.hxx>
25 : #include <basic/basmgr.hxx>
26 : #include <basic/sbmod.hxx>
27 : #include <basic/sbx.hxx>
28 : #include <sot/storage.hxx>
29 : #include <unotools/securityoptions.hxx>
30 :
31 : #include <rtl/ustring.h>
32 : #include <com/sun/star/uno/Any.hxx>
33 : #include <framework/eventsconfiguration.hxx>
34 : #include <comphelper/processfactory.hxx>
35 : #include <sfx2/evntconf.hxx>
36 :
37 : #include <sfx2/docfile.hxx>
38 : #include <sfx2/app.hxx>
39 : #include <sfx2/objsh.hxx>
40 : #include <sfx2/dispatch.hxx>
41 : #include <sfx2/sfxresid.hxx>
42 : #include "eventsupplier.hxx"
43 :
44 : #include <com/sun/star/beans/PropertyValue.hpp>
45 : #include <com/sun/star/container/XNameReplace.hpp>
46 : #include <com/sun/star/document/XEventsSupplier.hpp>
47 : #include <com/sun/star/frame/theGlobalEventBroadcaster.hpp>
48 : #include <com/sun/star/uno/Sequence.hxx>
49 : #include <com/sun/star/uno/Reference.hxx>
50 :
51 :
52 0 : TYPEINIT1(SfxEventNamesItem, SfxPoolItem);
53 :
54 : using namespace com::sun::star;
55 :
56 0 : SfxEventNamesList& SfxEventNamesList::operator=( const SfxEventNamesList& rTbl )
57 : {
58 0 : DelDtor();
59 0 : for ( size_t i = 0, n = rTbl.size(); i < n; ++i )
60 : {
61 0 : SfxEventName* pTmp = rTbl.at( i );
62 0 : SfxEventName* pNew = new SfxEventName( *pTmp );
63 0 : aEventNamesList.push_back( pNew );
64 : }
65 0 : return *this;
66 : }
67 :
68 0 : void SfxEventNamesList::DelDtor()
69 : {
70 0 : for ( size_t i = 0, n = aEventNamesList.size(); i < n; ++i )
71 0 : delete aEventNamesList[ i ];
72 0 : aEventNamesList.clear();
73 0 : }
74 :
75 0 : bool SfxEventNamesItem::operator==( const SfxPoolItem& rAttr ) const
76 : {
77 : DBG_ASSERT( SfxPoolItem::operator==(rAttr), "unequal types" );
78 :
79 0 : const SfxEventNamesList& rOwn = aEventsList;
80 0 : const SfxEventNamesList& rOther = static_cast<const SfxEventNamesItem&>( rAttr ).aEventsList;
81 :
82 0 : if ( rOwn.size() != rOther.size() )
83 0 : return false;
84 :
85 0 : for ( size_t nNo = 0, nCnt = rOwn.size(); nNo < nCnt; ++nNo )
86 : {
87 0 : const SfxEventName *pOwn = rOwn.at( nNo );
88 0 : const SfxEventName *pOther = rOther.at( nNo );
89 0 : if ( pOwn->mnId != pOther->mnId ||
90 0 : pOwn->maEventName != pOther->maEventName ||
91 0 : pOwn->maUIName != pOther->maUIName )
92 0 : return false;
93 : }
94 :
95 0 : return true;
96 :
97 : }
98 :
99 0 : bool SfxEventNamesItem::GetPresentation( SfxItemPresentation,
100 : SfxMapUnit,
101 : SfxMapUnit,
102 : OUString &rText,
103 : const IntlWrapper* ) const
104 : {
105 0 : rText.clear();
106 0 : return false;
107 : }
108 :
109 0 : SfxPoolItem* SfxEventNamesItem::Clone( SfxItemPool *) const
110 : {
111 0 : return new SfxEventNamesItem(*this);
112 : }
113 :
114 0 : SfxPoolItem* SfxEventNamesItem::Create(SvStream &, sal_uInt16) const
115 : {
116 : OSL_FAIL("not streamable!");
117 0 : return new SfxEventNamesItem(Which());
118 : }
119 :
120 0 : SvStream& SfxEventNamesItem::Store(SvStream &rStream, sal_uInt16 ) const
121 : {
122 : OSL_FAIL("not streamable!");
123 0 : return rStream;
124 : }
125 :
126 0 : sal_uInt16 SfxEventNamesItem::GetVersion( sal_uInt16 ) const
127 : {
128 : OSL_FAIL("not streamable!");
129 0 : return 0;
130 : }
131 :
132 0 : void SfxEventNamesItem::AddEvent( const OUString& rName, const OUString& rUIName, sal_uInt16 nID )
133 : {
134 0 : aEventsList.push_back( new SfxEventName( nID, rName, !rUIName.isEmpty() ? rUIName : rName ) );
135 0 : }
136 :
137 :
138 :
139 :
140 :
141 0 : uno::Any CreateEventData_Impl( const SvxMacro *pMacro )
142 : {
143 : /*
144 : This function converts a SvxMacro into an Any containing three
145 : properties. These properties are EventType and Script. Possible
146 : values for EventType ar StarBasic, JavaScript, ...
147 : The Script property should contain the URL to the macro and looks
148 : like "macro://./standard.module1.main()"
149 :
150 : If pMacro is NULL, we return an empty property sequence, so PropagateEvent_Impl
151 : can delete an event binding.
152 : */
153 0 : uno::Any aEventData;
154 :
155 0 : if ( pMacro )
156 : {
157 0 : if ( pMacro->GetScriptType() == STARBASIC )
158 : {
159 0 : uno::Sequence < beans::PropertyValue > aProperties(3);
160 0 : beans::PropertyValue *pValues = aProperties.getArray();
161 :
162 0 : OUString aType(STAR_BASIC );
163 0 : OUString aLib = pMacro->GetLibName();
164 0 : OUString aMacro = pMacro->GetMacName();
165 :
166 0 : pValues[ 0 ].Name = PROP_EVENT_TYPE;
167 0 : pValues[ 0 ].Value <<= aType;
168 :
169 0 : pValues[ 1 ].Name = PROP_LIBRARY;
170 0 : pValues[ 1 ].Value <<= aLib;
171 :
172 0 : pValues[ 2 ].Name = PROP_MACRO_NAME;
173 0 : pValues[ 2 ].Value <<= aMacro;
174 :
175 0 : aEventData <<= aProperties;
176 : }
177 0 : else if ( pMacro->GetScriptType() == EXTENDED_STYPE )
178 : {
179 0 : uno::Sequence < beans::PropertyValue > aProperties(2);
180 0 : beans::PropertyValue *pValues = aProperties.getArray();
181 :
182 0 : OUString aLib = pMacro->GetLibName();
183 0 : OUString aMacro = pMacro->GetMacName();
184 :
185 0 : pValues[ 0 ].Name = PROP_EVENT_TYPE;
186 0 : pValues[ 0 ].Value <<= aLib;
187 :
188 0 : pValues[ 1 ].Name = PROP_SCRIPT;
189 0 : pValues[ 1 ].Value <<= aMacro;
190 :
191 0 : aEventData <<= aProperties;
192 : }
193 0 : else if ( pMacro->GetScriptType() == JAVASCRIPT )
194 : {
195 0 : uno::Sequence < beans::PropertyValue > aProperties(2);
196 0 : beans::PropertyValue *pValues = aProperties.getArray();
197 :
198 0 : OUString aMacro = pMacro->GetMacName();
199 :
200 0 : pValues[ 0 ].Name = PROP_EVENT_TYPE;
201 0 : pValues[ 0 ].Value <<= OUString(SVX_MACRO_LANGUAGE_JAVASCRIPT);
202 :
203 0 : pValues[ 1 ].Name = PROP_MACRO_NAME;
204 0 : pValues[ 1 ].Value <<= aMacro;
205 :
206 0 : aEventData <<= aProperties;
207 : }
208 : else
209 : {
210 : SAL_WARN( "sfx.config", "CreateEventData_Impl(): ScriptType not supported!");
211 : }
212 : }
213 : else
214 : {
215 0 : uno::Sequence < beans::PropertyValue > aProperties;
216 0 : aEventData <<= aProperties;
217 : }
218 :
219 0 : return aEventData;
220 : }
221 :
222 :
223 0 : void PropagateEvent_Impl( SfxObjectShell *pDoc, const OUString& aEventName, const SvxMacro* pMacro )
224 : {
225 0 : uno::Reference < document::XEventsSupplier > xSupplier;
226 0 : if ( pDoc )
227 : {
228 0 : xSupplier = uno::Reference < document::XEventsSupplier >( pDoc->GetModel(), uno::UNO_QUERY );
229 : }
230 : else
231 : {
232 0 : xSupplier = uno::Reference < document::XEventsSupplier >
233 : ( frame::theGlobalEventBroadcaster::get(::comphelper::getProcessComponentContext()),
234 0 : uno::UNO_QUERY );
235 : }
236 :
237 0 : if ( xSupplier.is() )
238 : {
239 0 : uno::Reference < container::XNameReplace > xEvents = xSupplier->getEvents();
240 0 : if ( !aEventName.isEmpty() )
241 : {
242 0 : uno::Any aEventData = CreateEventData_Impl( pMacro );
243 :
244 : try
245 : {
246 0 : xEvents->replaceByName( aEventName, aEventData );
247 : }
248 0 : catch( const ::com::sun::star::lang::IllegalArgumentException& )
249 : {
250 : SAL_WARN( "sfx.config", "PropagateEvents_Impl: caught IllegalArgumentException" );
251 : }
252 0 : catch( const ::com::sun::star::container::NoSuchElementException& )
253 : {
254 : SAL_WARN( "sfx.config", "PropagateEvents_Impl: caught NoSuchElementException" );
255 0 : }
256 : }
257 : else {
258 : DBG_WARNING( "PropagateEvents_Impl: Got unknown event" );
259 0 : }
260 0 : }
261 0 : }
262 :
263 :
264 0 : void SfxEventConfiguration::ConfigureEvent( const OUString& aName, const SvxMacro& rMacro, SfxObjectShell *pDoc )
265 : {
266 0 : boost::scoped_ptr<SvxMacro> pMacro;
267 0 : if ( rMacro.HasMacro() )
268 0 : pMacro.reset( new SvxMacro( rMacro.GetMacName(), rMacro.GetLibName(), rMacro.GetScriptType() ) );
269 0 : PropagateEvent_Impl( pDoc ? pDoc : 0, aName, pMacro.get() );
270 0 : }
271 :
272 :
273 56 : SvxMacro* SfxEventConfiguration::ConvertToMacro( const com::sun::star::uno::Any& rElement, SfxObjectShell* pDoc, bool bBlowUp )
274 : {
275 56 : return SfxEvents_Impl::ConvertToMacro( rElement, pDoc, bBlowUp );
276 648 : }
277 :
278 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|