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