Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / unotools / source / config / eventcfg.cxx
blob88d54a9f118f77144b928847b12bdc2a899cae03
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <unotools/eventcfg.hxx>
22 #include <unotools/configmgr.hxx>
23 #include <unotools/configitem.hxx>
24 #include <com/sun/star/uno/Any.hxx>
25 #include <com/sun/star/uno/Sequence.hxx>
26 #include <com/sun/star/beans/PropertyValue.hpp>
27 #include <cppuhelper/weakref.hxx>
29 #include <boost/unordered_map.hpp>
30 #include <rtl/ustrbuf.hxx>
32 #include <itemholder1.hxx>
34 #include <algorithm>
36 using namespace ::std ;
37 using namespace ::utl ;
38 using namespace ::rtl ;
39 using namespace ::osl ;
40 using namespace ::com::sun::star::uno ;
41 using namespace ::com::sun::star;
43 #define ROOTNODE_EVENTS OUString(RTL_CONSTASCII_USTRINGPARAM("Office.Events/ApplicationEvents" ))
44 #define PATHDELIMITER OUString(RTL_CONSTASCII_USTRINGPARAM("/"))
45 #define SETNODE_BINDINGS OUString(RTL_CONSTASCII_USTRINGPARAM("Bindings" ))
46 #define PROPERTYNAME_BINDINGURL OUString(RTL_CONSTASCII_USTRINGPARAM("BindingURL"))
48 const char* pEventAsciiNames[] =
50 "OnStartApp",
51 "OnCloseApp",
52 "OnCreate",
53 "OnNew",
54 "OnLoadFinished",
55 "OnLoad",
56 "OnPrepareUnload",
57 "OnUnload",
58 "OnSave",
59 "OnSaveDone",
60 "OnSaveFailed",
61 "OnSaveAs",
62 "OnSaveAsDone",
63 "OnSaveAsFailed",
64 "OnCopyTo",
65 "OnCopyToDone",
66 "OnCopyToFailed",
67 "OnFocus",
68 "OnUnfocus",
69 "OnPrint",
70 "OnViewCreated",
71 "OnPrepareViewClosing",
72 "OnViewClosed",
73 "OnModifyChanged",
74 "OnTitleChanged",
75 "OnVisAreaChanged",
76 "OnModeChanged",
77 "OnStorageChanged"
80 GlobalEventConfig_Impl::GlobalEventConfig_Impl()
81 : ConfigItem( ROOTNODE_EVENTS, CONFIG_MODE_IMMEDIATE_UPDATE )
83 // the supported event names
84 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_STARTAPP] ) );
85 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_CLOSEAPP] ) );
86 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_DOCCREATED] ) );
87 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_CREATEDOC] ) );
88 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_LOADFINISHED] ) );
89 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_OPENDOC] ) );
90 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_PREPARECLOSEDOC] ) );
91 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_CLOSEDOC] ) );
92 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEDOC] ) );
93 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEDOCDONE] ) );
94 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEDOCFAILED] ) );
95 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEASDOC] ) );
96 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEASDOCDONE] ) );
97 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEASDOCFAILED] ) );
98 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVETODOC] ) );
99 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVETODOCDONE] ) );
100 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVETODOCFAILED] ) );
101 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_ACTIVATEDOC] ) );
102 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_DEACTIVATEDOC] ) );
103 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_PRINTDOC] ) );
104 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_VIEWCREATED] ) );
105 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_PREPARECLOSEVIEW] ) );
106 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_CLOSEVIEW] ) );
107 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_MODIFYCHANGED] ) );
108 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_TITLECHANGED] ) );
109 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_VISAREACHANGED] ) );
110 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_MODECHANGED] ) );
111 m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_STORAGECHANGED] ) );
113 initBindingInfo();
115 /*TODO: Not used in the moment! see Notify() ...
116 // Enable notification mechanism of our baseclass.
117 // We need it to get information about changes outside these class on our used configuration keys! */
118 Sequence< OUString > aNotifySeq( 1 );
119 aNotifySeq[0] = OUString( RTL_CONSTASCII_USTRINGPARAM( "Events" ));
120 EnableNotification( aNotifySeq, sal_True );
123 //*****************************************************************************************************************
124 // destructor
125 //*****************************************************************************************************************
126 GlobalEventConfig_Impl::~GlobalEventConfig_Impl()
128 // We must save our current values .. if user forget it!
129 if( IsModified() == sal_True )
131 Commit();
135 ::rtl::OUString GlobalEventConfig_Impl::GetEventName( sal_Int32 nIndex )
137 if ( nIndex < (sal_Int32) m_supportedEvents.size() )
138 return m_supportedEvents[nIndex];
139 else
140 return rtl::OUString();
143 //*****************************************************************************************************************
144 // public method
145 //*****************************************************************************************************************
146 void GlobalEventConfig_Impl::Notify( const Sequence< OUString >& )
148 MutexGuard aGuard( GlobalEventConfig::GetOwnStaticMutex() );
150 initBindingInfo();
152 // dont forget to update all existing frames and her might cached dispatch objects!
153 // But look for already killed frames. We hold weak references instead of hard ones ...
154 for (FrameVector::const_iterator pIt = m_lFrames.begin();
155 pIt != m_lFrames.end() ;
156 ++pIt )
158 ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame > xFrame(pIt->get(), ::com::sun::star::uno::UNO_QUERY);
159 if (xFrame.is())
160 xFrame->contextChanged();
164 //*****************************************************************************************************************
165 // public method
166 //*****************************************************************************************************************
167 void GlobalEventConfig_Impl::Commit()
169 //DF need to check it this is correct??
170 OSL_TRACE("In GlobalEventConfig_Impl::Commit");
171 EventBindingHash::const_iterator it = m_eventBindingHash.begin();
172 EventBindingHash::const_iterator it_end = m_eventBindingHash.end();
173 // clear the existing nodes
174 ClearNodeSet( SETNODE_BINDINGS );
175 Sequence< beans::PropertyValue > seqValues( 1 );
176 OUString sNode;
177 static const OUString sPrefix(SETNODE_BINDINGS + PATHDELIMITER + OUString(RTL_CONSTASCII_USTRINGPARAM("BindingType['")));
178 static const OUString sPostfix(OUString(RTL_CONSTASCII_USTRINGPARAM("']")) + PATHDELIMITER + PROPERTYNAME_BINDINGURL);
179 //step through the list of events
180 for(int i=0;it!=it_end;++it,++i)
182 //no point in writing out empty bindings!
183 if(it->second.isEmpty() )
184 continue;
185 sNode = sPrefix + it->first + sPostfix;
186 OSL_TRACE("writing binding for: %s",::rtl::OUStringToOString(sNode , RTL_TEXTENCODING_ASCII_US ).pData->buffer);
187 seqValues[ 0 ].Name = sNode;
188 seqValues[ 0 ].Value <<= it->second;
189 //write the data to the registry
190 SetSetProperties(SETNODE_BINDINGS,seqValues);
194 //*****************************************************************************************************************
195 // private method
196 //*****************************************************************************************************************
197 void GlobalEventConfig_Impl::initBindingInfo()
199 // Get ALL names of current existing list items in configuration!
200 Sequence< OUString > lEventNames = GetNodeNames( SETNODE_BINDINGS, utl::CONFIG_NAME_LOCAL_PATH );
202 OUString aSetNode( SETNODE_BINDINGS );
203 aSetNode += PATHDELIMITER;
205 OUString aCommandKey( PATHDELIMITER );
206 aCommandKey += PROPERTYNAME_BINDINGURL;
208 // Expand all keys
209 Sequence< OUString > lMacros(1);
210 for (sal_Int32 i=0; i<lEventNames.getLength(); ++i )
212 OUStringBuffer aBuffer( 32 );
213 aBuffer.append( aSetNode );
214 aBuffer.append( lEventNames[i] );
215 aBuffer.append( aCommandKey );
216 lMacros[0] = aBuffer.makeStringAndClear();
217 OSL_TRACE("reading binding for: %s",::rtl::OUStringToOString(lMacros[0] , RTL_TEXTENCODING_ASCII_US ).pData->buffer);
218 Sequence< Any > lValues = GetProperties( lMacros );
219 OUString sMacroURL;
220 if( lValues.getLength() > 0 )
222 lValues[0] >>= sMacroURL;
223 sal_Int32 startIndex = lEventNames[i].indexOf('\'');
224 sal_Int32 endIndex = lEventNames[i].lastIndexOf('\'');
225 if( startIndex >=0 && endIndex > 0 )
227 startIndex++;
228 OUString eventName = lEventNames[i].copy(startIndex,endIndex-startIndex);
229 m_eventBindingHash[ eventName ] = sMacroURL;
235 void SAL_CALL GlobalEventConfig_Impl::replaceByName( const OUString& aName, const Any& aElement ) throw (lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, RuntimeException)
237 Sequence< beans::PropertyValue > props;
238 //DF should we prepopulate the hash with a list of valid event Names?
239 if( sal_False == ( aElement >>= props ) )
241 throw lang::IllegalArgumentException( OUString(),
242 Reference< XInterface > (), 2);
244 OUString macroURL;
245 sal_Int32 nPropCount = props.getLength();
246 for( sal_Int32 index = 0 ; index < nPropCount ; ++index )
248 if ( props[ index ].Name.compareToAscii( "Script" ) == 0 )
249 props[ index ].Value >>= macroURL;
251 m_eventBindingHash[ aName ] = macroURL;
252 SetModified();
255 Any SAL_CALL GlobalEventConfig_Impl::getByName( const OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, RuntimeException)
257 Any aRet;
258 Sequence< beans::PropertyValue > props(2);
259 props[0].Name = OUString(RTL_CONSTASCII_USTRINGPARAM("EventType"));
260 props[0].Value <<= OUString(RTL_CONSTASCII_USTRINGPARAM("Script"));
261 props[1].Name = OUString(RTL_CONSTASCII_USTRINGPARAM("Script"));
262 EventBindingHash::const_iterator it = m_eventBindingHash.find( aName );
263 if( it != m_eventBindingHash.end() )
265 props[1].Value <<= it->second;
267 else
269 // not yet accessed - is it a supported name?
270 SupportedEventsVector::const_iterator pos = ::std::find(
271 m_supportedEvents.begin(), m_supportedEvents.end(), aName );
272 if ( pos == m_supportedEvents.end() )
273 throw container::NoSuchElementException( aName, NULL );
275 props[1].Value <<= OUString();
277 aRet <<= props;
278 return aRet;
281 Sequence< OUString > SAL_CALL GlobalEventConfig_Impl::getElementNames( ) throw (RuntimeException)
283 const ::rtl::OUString* pRet = m_supportedEvents.empty() ? NULL : &m_supportedEvents[0];
284 return uno::Sequence< ::rtl::OUString >(pRet, m_supportedEvents.size());
287 sal_Bool SAL_CALL GlobalEventConfig_Impl::hasByName( const OUString& aName ) throw (RuntimeException)
289 if ( m_eventBindingHash.find( aName ) != m_eventBindingHash.end() )
290 return sal_True;
292 // never accessed before - is it supported in general?
293 SupportedEventsVector::const_iterator pos = ::std::find(
294 m_supportedEvents.begin(), m_supportedEvents.end(), aName );
295 if ( pos != m_supportedEvents.end() )
296 return sal_True;
298 return sal_False;
301 Type SAL_CALL GlobalEventConfig_Impl::getElementType( ) throw (RuntimeException)
303 //DF definitly not sure about this??
304 return ::getCppuType((const Sequence<beans::PropertyValue>*)0);
307 sal_Bool SAL_CALL GlobalEventConfig_Impl::hasElements( ) throw (RuntimeException)
309 return ( m_eventBindingHash.empty() );
312 // and now the wrapper
315 //initialize static member
316 GlobalEventConfig_Impl* GlobalEventConfig::m_pImpl = NULL ;
317 sal_Int32 GlobalEventConfig::m_nRefCount = 0 ;
319 GlobalEventConfig::GlobalEventConfig()
321 // Global access, must be guarded (multithreading!).
322 MutexGuard aGuard( GetOwnStaticMutex() );
323 // Increase our refcount ...
324 ++m_nRefCount;
325 // ... and initialize our data container only if it not already exist!
326 if( m_pImpl == NULL )
328 m_pImpl = new GlobalEventConfig_Impl;
329 ItemHolder1::holdConfigItem(E_EVENTCFG);
333 GlobalEventConfig::~GlobalEventConfig()
335 // Global access, must be guarded (multithreading!)
336 MutexGuard aGuard( GetOwnStaticMutex() );
337 // Decrease our refcount.
338 --m_nRefCount;
339 // If last instance was deleted ...
340 // we must destroy our static data container!
341 if( m_nRefCount <= 0 )
343 delete m_pImpl;
344 m_pImpl = NULL;
348 Reference< container::XNameReplace > SAL_CALL GlobalEventConfig::getEvents() throw (::com::sun::star::uno::RuntimeException)
350 MutexGuard aGuard( GetOwnStaticMutex() );
351 Reference< container::XNameReplace > ret(this);
352 return ret;
355 void SAL_CALL GlobalEventConfig::replaceByName( const OUString& aName, const Any& aElement ) throw (lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, RuntimeException)
357 MutexGuard aGuard( GetOwnStaticMutex() );
358 m_pImpl->replaceByName( aName, aElement );
360 Any SAL_CALL GlobalEventConfig::getByName( const OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, RuntimeException)
362 MutexGuard aGuard( GetOwnStaticMutex() );
363 return m_pImpl->getByName( aName );
365 Sequence< OUString > SAL_CALL GlobalEventConfig::getElementNames( ) throw (RuntimeException)
367 MutexGuard aGuard( GetOwnStaticMutex() );
368 return m_pImpl->getElementNames( );
370 sal_Bool SAL_CALL GlobalEventConfig::hasByName( const OUString& aName ) throw (RuntimeException)
372 MutexGuard aGuard( GetOwnStaticMutex() );
373 return m_pImpl->hasByName( aName );
375 Type SAL_CALL GlobalEventConfig::getElementType( ) throw (RuntimeException)
377 MutexGuard aGuard( GetOwnStaticMutex() );
378 return m_pImpl->getElementType( );
380 sal_Bool SAL_CALL GlobalEventConfig::hasElements( ) throw (RuntimeException)
382 MutexGuard aGuard( GetOwnStaticMutex() );
383 return m_pImpl->hasElements( );
386 namespace
388 class theGlobalEventConfigMutex : public rtl::Static<osl::Mutex, theGlobalEventConfigMutex>{};
391 Mutex& GlobalEventConfig::GetOwnStaticMutex()
393 return theGlobalEventConfigMutex::get();
396 ::rtl::OUString GlobalEventConfig::GetEventName( sal_Int32 nIndex )
398 return GlobalEventConfig().m_pImpl->GetEventName( nIndex );
401 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */