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 : #include <unotools/eventcfg.hxx>
21 : #include <unotools/configmgr.hxx>
22 : #include <unotools/configitem.hxx>
23 : #include <com/sun/star/uno/Any.hxx>
24 : #include <com/sun/star/uno/Sequence.hxx>
25 : #include <com/sun/star/beans/PropertyValue.hpp>
26 : #include <cppuhelper/weakref.hxx>
27 :
28 : #include <boost/unordered_map.hpp>
29 : #include <rtl/ustrbuf.hxx>
30 :
31 : #include "itemholder1.hxx"
32 :
33 : #include <algorithm>
34 :
35 : using namespace ::std;
36 : using namespace ::utl;
37 : using namespace ::osl;
38 : using namespace ::com::sun::star::uno;
39 : using namespace ::com::sun::star;
40 :
41 : #define ROOTNODE_EVENTS OUString("Office.Events/ApplicationEvents")
42 : #define PATHDELIMITER OUString("/")
43 : #define SETNODE_BINDINGS OUString("Bindings")
44 : #define PROPERTYNAME_BINDINGURL OUString("BindingURL")
45 :
46 : const char* pEventAsciiNames[] =
47 : {
48 : "OnStartApp",
49 : "OnCloseApp",
50 : "OnCreate",
51 : "OnNew",
52 : "OnLoadFinished",
53 : "OnLoad",
54 : "OnPrepareUnload",
55 : "OnUnload",
56 : "OnSave",
57 : "OnSaveDone",
58 : "OnSaveFailed",
59 : "OnSaveAs",
60 : "OnSaveAsDone",
61 : "OnSaveAsFailed",
62 : "OnCopyTo",
63 : "OnCopyToDone",
64 : "OnCopyToFailed",
65 : "OnFocus",
66 : "OnUnfocus",
67 : "OnPrint",
68 : "OnViewCreated",
69 : "OnPrepareViewClosing",
70 : "OnViewClosed",
71 : "OnModifyChanged",
72 : "OnTitleChanged",
73 : "OnVisAreaChanged",
74 : "OnModeChanged",
75 : "OnStorageChanged"
76 : };
77 :
78 418 : GlobalEventConfig_Impl::GlobalEventConfig_Impl()
79 418 : : ConfigItem( ROOTNODE_EVENTS, CONFIG_MODE_IMMEDIATE_UPDATE )
80 : {
81 : // the supported event names
82 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_STARTAPP] ) );
83 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_CLOSEAPP] ) );
84 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_DOCCREATED] ) );
85 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_CREATEDOC] ) );
86 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_LOADFINISHED] ) );
87 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_OPENDOC] ) );
88 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_PREPARECLOSEDOC] ) );
89 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_CLOSEDOC] ) );
90 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEDOC] ) );
91 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEDOCDONE] ) );
92 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEDOCFAILED] ) );
93 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEASDOC] ) );
94 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEASDOCDONE] ) );
95 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEASDOCFAILED] ) );
96 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVETODOC] ) );
97 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVETODOCDONE] ) );
98 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVETODOCFAILED] ) );
99 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_ACTIVATEDOC] ) );
100 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_DEACTIVATEDOC] ) );
101 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_PRINTDOC] ) );
102 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_VIEWCREATED] ) );
103 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_PREPARECLOSEVIEW] ) );
104 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_CLOSEVIEW] ) );
105 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_MODIFYCHANGED] ) );
106 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_TITLECHANGED] ) );
107 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_VISAREACHANGED] ) );
108 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_MODECHANGED] ) );
109 418 : m_supportedEvents.push_back(OUString::createFromAscii( pEventAsciiNames[STR_EVENT_STORAGECHANGED] ) );
110 :
111 418 : initBindingInfo();
112 :
113 : /*TODO: Not used in the moment! see Notify() ...
114 : // Enable notification mechanism of our baseclass.
115 : // We need it to get information about changes outside these class on our used configuration keys! */
116 418 : Sequence< OUString > aNotifySeq( 1 );
117 418 : aNotifySeq[0] = "Events";
118 418 : EnableNotification( aNotifySeq, true );
119 418 : }
120 :
121 : // destructor
122 :
123 1239 : GlobalEventConfig_Impl::~GlobalEventConfig_Impl()
124 : {
125 : // We must save our current values .. if user forget it!
126 413 : if( IsModified() )
127 : {
128 0 : Commit();
129 : }
130 826 : }
131 :
132 92088 : OUString GlobalEventConfig_Impl::GetEventName( sal_Int32 nIndex )
133 : {
134 92088 : if ( nIndex < (sal_Int32) m_supportedEvents.size() )
135 92088 : return m_supportedEvents[nIndex];
136 : else
137 0 : return OUString();
138 : }
139 :
140 : // public method
141 :
142 0 : void GlobalEventConfig_Impl::Notify( const Sequence< OUString >& )
143 : {
144 0 : MutexGuard aGuard( GlobalEventConfig::GetOwnStaticMutex() );
145 :
146 0 : initBindingInfo();
147 :
148 : // dont forget to update all existing frames and her might cached dispatch objects!
149 : // But look for already killed frames. We hold weak references instead of hard ones ...
150 0 : for (FrameVector::const_iterator pIt = m_lFrames.begin();
151 0 : pIt != m_lFrames.end();
152 : ++pIt )
153 : {
154 0 : ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame > xFrame(pIt->get(), ::com::sun::star::uno::UNO_QUERY);
155 0 : if (xFrame.is())
156 0 : xFrame->contextChanged();
157 0 : }
158 0 : }
159 :
160 : // public method
161 :
162 0 : void GlobalEventConfig_Impl::Commit()
163 : {
164 : //DF need to check it this is correct??
165 : OSL_TRACE("In GlobalEventConfig_Impl::Commit");
166 0 : EventBindingHash::const_iterator it = m_eventBindingHash.begin();
167 0 : EventBindingHash::const_iterator it_end = m_eventBindingHash.end();
168 : // clear the existing nodes
169 0 : ClearNodeSet( SETNODE_BINDINGS );
170 0 : Sequence< beans::PropertyValue > seqValues( 1 );
171 0 : OUString sNode;
172 0 : static const OUString sPrefix(SETNODE_BINDINGS + PATHDELIMITER + "BindingType['");
173 0 : static const OUString sPostfix("']" + PATHDELIMITER + PROPERTYNAME_BINDINGURL);
174 : //step through the list of events
175 0 : for(int i=0;it!=it_end;++it,++i)
176 : {
177 : //no point in writing out empty bindings!
178 0 : if(it->second.isEmpty() )
179 0 : continue;
180 0 : sNode = sPrefix + it->first + sPostfix;
181 : OSL_TRACE("writing binding for: %s",OUStringToOString(sNode , RTL_TEXTENCODING_ASCII_US ).pData->buffer);
182 0 : seqValues[ 0 ].Name = sNode;
183 0 : seqValues[ 0 ].Value <<= it->second;
184 : //write the data to the registry
185 0 : SetSetProperties(SETNODE_BINDINGS,seqValues);
186 0 : }
187 0 : }
188 :
189 : // private method
190 :
191 418 : void GlobalEventConfig_Impl::initBindingInfo()
192 : {
193 : // Get ALL names of current existing list items in configuration!
194 418 : Sequence< OUString > lEventNames = GetNodeNames( SETNODE_BINDINGS, utl::CONFIG_NAME_LOCAL_PATH );
195 :
196 836 : OUString aSetNode( SETNODE_BINDINGS );
197 418 : aSetNode += PATHDELIMITER;
198 :
199 836 : OUString aCommandKey( PATHDELIMITER );
200 418 : aCommandKey += PROPERTYNAME_BINDINGURL;
201 :
202 : // Expand all keys
203 836 : Sequence< OUString > lMacros(1);
204 418 : for (sal_Int32 i=0; i<lEventNames.getLength(); ++i )
205 : {
206 0 : OUStringBuffer aBuffer( 32 );
207 0 : aBuffer.append( aSetNode );
208 0 : aBuffer.append( lEventNames[i] );
209 0 : aBuffer.append( aCommandKey );
210 0 : lMacros[0] = aBuffer.makeStringAndClear();
211 : OSL_TRACE("reading binding for: %s",OUStringToOString(lMacros[0] , RTL_TEXTENCODING_ASCII_US ).pData->buffer);
212 0 : Sequence< Any > lValues = GetProperties( lMacros );
213 0 : OUString sMacroURL;
214 0 : if( lValues.getLength() > 0 )
215 : {
216 0 : lValues[0] >>= sMacroURL;
217 0 : sal_Int32 startIndex = lEventNames[i].indexOf('\'');
218 0 : sal_Int32 endIndex = lEventNames[i].lastIndexOf('\'');
219 0 : if( startIndex >=0 && endIndex > 0 )
220 : {
221 0 : startIndex++;
222 0 : OUString eventName = lEventNames[i].copy(startIndex,endIndex-startIndex);
223 0 : m_eventBindingHash[ eventName ] = sMacroURL;
224 : }
225 : }
226 418 : }
227 418 : }
228 :
229 0 : void SAL_CALL GlobalEventConfig_Impl::replaceByName( const OUString& aName, const Any& aElement ) throw (lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, RuntimeException)
230 : {
231 0 : Sequence< beans::PropertyValue > props;
232 : //DF should we prepopulate the hash with a list of valid event Names?
233 0 : if( !( aElement >>= props ) )
234 : {
235 : throw lang::IllegalArgumentException( OUString(),
236 0 : Reference< XInterface > (), 2);
237 : }
238 0 : OUString macroURL;
239 0 : sal_Int32 nPropCount = props.getLength();
240 0 : for( sal_Int32 index = 0; index < nPropCount; ++index )
241 : {
242 0 : if ( props[ index ].Name.equalsAscii( "Script" ) )
243 0 : props[ index ].Value >>= macroURL;
244 : }
245 0 : m_eventBindingHash[ aName ] = macroURL;
246 0 : SetModified();
247 0 : }
248 :
249 69588 : Any SAL_CALL GlobalEventConfig_Impl::getByName( const OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, RuntimeException)
250 : {
251 69588 : Any aRet;
252 139176 : Sequence< beans::PropertyValue > props(2);
253 69588 : props[0].Name = "EventType";
254 69588 : props[0].Value <<= OUString("Script");
255 69588 : props[1].Name = "Script";
256 69588 : EventBindingHash::const_iterator it = m_eventBindingHash.find( aName );
257 69588 : if( it != m_eventBindingHash.end() )
258 : {
259 0 : props[1].Value <<= it->second;
260 : }
261 : else
262 : {
263 : // not yet accessed - is it a supported name?
264 : SupportedEventsVector::const_iterator pos = ::std::find(
265 69588 : m_supportedEvents.begin(), m_supportedEvents.end(), aName );
266 69588 : if ( pos == m_supportedEvents.end() )
267 0 : throw container::NoSuchElementException( aName );
268 :
269 69588 : props[1].Value <<= OUString();
270 : }
271 69588 : aRet <<= props;
272 139176 : return aRet;
273 : }
274 :
275 40 : Sequence< OUString > SAL_CALL GlobalEventConfig_Impl::getElementNames( ) throw (RuntimeException)
276 : {
277 40 : const OUString* pRet = m_supportedEvents.empty() ? NULL : &m_supportedEvents[0];
278 40 : return uno::Sequence< OUString >(pRet, m_supportedEvents.size());
279 : }
280 :
281 70572 : bool SAL_CALL GlobalEventConfig_Impl::hasByName( const OUString& aName ) throw (RuntimeException)
282 : {
283 70572 : if ( m_eventBindingHash.find( aName ) != m_eventBindingHash.end() )
284 0 : return true;
285 :
286 : // never accessed before - is it supported in general?
287 : SupportedEventsVector::const_iterator pos = ::std::find(
288 70572 : m_supportedEvents.begin(), m_supportedEvents.end(), aName );
289 70572 : if ( pos != m_supportedEvents.end() )
290 69588 : return true;
291 :
292 984 : return false;
293 : }
294 :
295 0 : Type SAL_CALL GlobalEventConfig_Impl::getElementType( ) throw (RuntimeException)
296 : {
297 : //DF definitely not sure about this??
298 0 : return ::getCppuType((const Sequence<beans::PropertyValue>*)0);
299 : }
300 :
301 0 : bool SAL_CALL GlobalEventConfig_Impl::hasElements( ) throw (RuntimeException)
302 : {
303 0 : return ( m_eventBindingHash.empty() );
304 : }
305 :
306 : // and now the wrapper
307 :
308 : //initialize static member
309 : GlobalEventConfig_Impl* GlobalEventConfig::m_pImpl = NULL;
310 : sal_Int32 GlobalEventConfig::m_nRefCount = 0;
311 :
312 92418 : GlobalEventConfig::GlobalEventConfig()
313 : {
314 : // Global access, must be guarded (multithreading!).
315 92418 : MutexGuard aGuard( GetOwnStaticMutex() );
316 : // Increase our refcount ...
317 92418 : ++m_nRefCount;
318 : // ... and initialize our data container only if it not already exist!
319 92418 : if( m_pImpl == NULL )
320 : {
321 418 : m_pImpl = new GlobalEventConfig_Impl;
322 418 : ItemHolder1::holdConfigItem(E_EVENTCFG);
323 92418 : }
324 92418 : }
325 :
326 185111 : GlobalEventConfig::~GlobalEventConfig()
327 : {
328 : // Global access, must be guarded (multithreading!)
329 92413 : MutexGuard aGuard( GetOwnStaticMutex() );
330 : // Decrease our refcount.
331 92413 : --m_nRefCount;
332 : // If last instance was deleted ...
333 : // we must destroy our static data container!
334 92413 : if( m_nRefCount <= 0 )
335 : {
336 413 : delete m_pImpl;
337 413 : m_pImpl = NULL;
338 92413 : }
339 92698 : }
340 :
341 0 : Reference< container::XNameReplace > SAL_CALL GlobalEventConfig::getEvents() throw (::com::sun::star::uno::RuntimeException, std::exception)
342 : {
343 0 : MutexGuard aGuard( GetOwnStaticMutex() );
344 0 : Reference< container::XNameReplace > ret(this);
345 0 : return ret;
346 : }
347 :
348 0 : void SAL_CALL GlobalEventConfig::replaceByName( const OUString& aName, const Any& aElement ) throw (lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, RuntimeException, std::exception)
349 : {
350 0 : MutexGuard aGuard( GetOwnStaticMutex() );
351 0 : m_pImpl->replaceByName( aName, aElement );
352 0 : }
353 69588 : Any SAL_CALL GlobalEventConfig::getByName( const OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, RuntimeException, std::exception)
354 : {
355 69588 : MutexGuard aGuard( GetOwnStaticMutex() );
356 69588 : return m_pImpl->getByName( aName );
357 : }
358 40 : Sequence< OUString > SAL_CALL GlobalEventConfig::getElementNames( ) throw (RuntimeException, std::exception)
359 : {
360 40 : MutexGuard aGuard( GetOwnStaticMutex() );
361 40 : return m_pImpl->getElementNames( );
362 : }
363 70572 : sal_Bool SAL_CALL GlobalEventConfig::hasByName( const OUString& aName ) throw (RuntimeException, std::exception)
364 : {
365 70572 : MutexGuard aGuard( GetOwnStaticMutex() );
366 70572 : return m_pImpl->hasByName( aName );
367 : }
368 0 : Type SAL_CALL GlobalEventConfig::getElementType( ) throw (RuntimeException, std::exception)
369 : {
370 0 : MutexGuard aGuard( GetOwnStaticMutex() );
371 0 : return m_pImpl->getElementType( );
372 : }
373 0 : sal_Bool SAL_CALL GlobalEventConfig::hasElements( ) throw (RuntimeException, std::exception)
374 : {
375 0 : MutexGuard aGuard( GetOwnStaticMutex() );
376 0 : return m_pImpl->hasElements( );
377 : }
378 :
379 : namespace
380 : {
381 : class theGlobalEventConfigMutex : public rtl::Static<osl::Mutex, theGlobalEventConfigMutex>{};
382 : }
383 :
384 325031 : Mutex& GlobalEventConfig::GetOwnStaticMutex()
385 : {
386 325031 : return theGlobalEventConfigMutex::get();
387 : }
388 :
389 92088 : OUString GlobalEventConfig::GetEventName( sal_Int32 nIndex )
390 : {
391 92088 : return GlobalEventConfig().m_pImpl->GetEventName( nIndex );
392 : }
393 :
394 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|