1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: eventcfg.cxx,v $
10 * $Revision: 1.8.24.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
34 #include "eventcfg.hxx"
35 #include <unotools/configmgr.hxx>
36 #include <unotools/configitem.hxx>
37 #include <tools/debug.hxx>
38 #include <com/sun/star/uno/Any.hxx>
39 #include <com/sun/star/uno/Sequence.hxx>
40 #include <com/sun/star/beans/PropertyValue.hpp>
41 #include <cppuhelper/weakref.hxx>
43 #ifndef __SGI_STL_HASH_MAP
46 #include <tools/urlobj.hxx>
47 #include <rtl/ustrbuf.hxx>
49 #include <itemholder1.hxx>
53 using namespace ::std
;
54 using namespace ::utl
;
55 using namespace ::rtl
;
56 using namespace ::osl
;
57 using namespace ::com::sun::star::uno
;
58 using namespace ::com::sun::star
;
60 #define ROOTNODE_EVENTS OUString(RTL_CONSTASCII_USTRINGPARAM("Office.Events/ApplicationEvents" ))
61 #define PATHDELIMITER OUString(RTL_CONSTASCII_USTRINGPARAM("/"))
62 #define SETNODE_BINDINGS OUString(RTL_CONSTASCII_USTRINGPARAM("Bindings" ))
63 #define PROPERTYNAME_BINDINGURL OUString(RTL_CONSTASCII_USTRINGPARAM("BindingURL"))
65 GlobalEventConfig_Impl::GlobalEventConfig_Impl()
66 : ConfigItem( ROOTNODE_EVENTS
, CONFIG_MODE_IMMEDIATE_UPDATE
)
68 // the supported event names
69 m_supportedEvents
.push_back(::rtl::OUString::createFromAscii("OnStartApp"));
70 m_supportedEvents
.push_back(::rtl::OUString::createFromAscii("OnCloseApp"));
71 m_supportedEvents
.push_back(::rtl::OUString::createFromAscii("OnNew"));
72 m_supportedEvents
.push_back(::rtl::OUString::createFromAscii("OnUnload"));
73 m_supportedEvents
.push_back(::rtl::OUString::createFromAscii("OnPrepareUnload"));
74 m_supportedEvents
.push_back(::rtl::OUString::createFromAscii("OnLoad"));
75 m_supportedEvents
.push_back(::rtl::OUString::createFromAscii("OnSave"));
76 m_supportedEvents
.push_back(::rtl::OUString::createFromAscii("OnSaveAs"));
77 m_supportedEvents
.push_back(::rtl::OUString::createFromAscii("OnSaveDone"));
78 m_supportedEvents
.push_back(::rtl::OUString::createFromAscii("OnSaveAsDone"));
79 m_supportedEvents
.push_back(::rtl::OUString::createFromAscii("OnFocus"));
80 m_supportedEvents
.push_back(::rtl::OUString::createFromAscii("OnUnfocus"));
81 m_supportedEvents
.push_back(::rtl::OUString::createFromAscii("OnPrint"));
82 m_supportedEvents
.push_back(::rtl::OUString::createFromAscii("OnModifyChanged"));
86 /*TODO: Not used in the moment! see Notify() ...
87 // Enable notification mechanism of our baseclass.
88 // We need it to get information about changes outside these class on our used configuration keys! */
89 Sequence
< OUString
> aNotifySeq( 1 );
90 aNotifySeq
[0] = OUString( RTL_CONSTASCII_USTRINGPARAM( "Events" ));
91 EnableNotification( aNotifySeq
, sal_True
);
94 //*****************************************************************************************************************
96 //*****************************************************************************************************************
97 GlobalEventConfig_Impl::~GlobalEventConfig_Impl()
99 // We must save our current values .. if user forget it!
100 if( IsModified() == sal_True
)
106 //*****************************************************************************************************************
108 //*****************************************************************************************************************
109 void GlobalEventConfig_Impl::Notify( const Sequence
< OUString
>& )
111 MutexGuard
aGuard( GlobalEventConfig::GetOwnStaticMutex() );
115 // dont forget to update all existing frames and her might cached dispatch objects!
116 // But look for already killed frames. We hold weak references instead of hard ones ...
117 for (FrameVector::const_iterator pIt
= m_lFrames
.begin();
118 pIt
!= m_lFrames
.end() ;
121 ::com::sun::star::uno::Reference
< ::com::sun::star::frame::XFrame
> xFrame(pIt
->get(), ::com::sun::star::uno::UNO_QUERY
);
123 xFrame
->contextChanged();
127 //*****************************************************************************************************************
129 //*****************************************************************************************************************
130 void GlobalEventConfig_Impl::Commit()
132 //DF need to check it this is correct??
133 OSL_TRACE("In GlobalEventConfig_Impl::Commit");
134 EventBindingHash::const_iterator it
= m_eventBindingHash
.begin();
135 EventBindingHash::const_iterator it_end
= m_eventBindingHash
.end();
136 // clear the existing nodes
137 ClearNodeSet( SETNODE_BINDINGS
);
138 Sequence
< beans::PropertyValue
> seqValues( 1 );
140 static const OUString
sPrefix(SETNODE_BINDINGS
+ PATHDELIMITER
+ OUString::createFromAscii("BindingType['"));
141 static const OUString
sPostfix(OUString::createFromAscii("']") + PATHDELIMITER
+ PROPERTYNAME_BINDINGURL
);
142 //step through the list of events
143 for(int i
=0;it
!=it_end
;++it
,++i
)
145 //no point in writing out empty bindings!
146 if(it
->second
.getLength() == 0 )
148 sNode
= sPrefix
+ it
->first
+ sPostfix
;
149 OSL_TRACE("writing binding for: %s",::rtl::OUStringToOString(sNode
, RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
150 seqValues
[ 0 ].Name
= sNode
;
151 seqValues
[ 0 ].Value
<<= it
->second
;
152 //write the data to the registry
153 SetSetProperties(SETNODE_BINDINGS
,seqValues
);
157 //*****************************************************************************************************************
159 //*****************************************************************************************************************
160 void GlobalEventConfig_Impl::EstablishFrameCallback(const ::com::sun::star::uno::Reference
< ::com::sun::star::frame::XFrame
>& xFrame
)
162 // check if frame already exists inside list
163 // ignore double registrations
164 // every frame must be notified one times only!
165 ::com::sun::star::uno::WeakReference
< ::com::sun::star::frame::XFrame
> xWeak(xFrame
);
166 FrameVector::const_iterator pIt
= ::std::find(m_lFrames
.begin(), m_lFrames
.end(), xWeak
);
167 if (pIt
== m_lFrames
.end())
168 m_lFrames
.push_back(xWeak
);
171 //*****************************************************************************************************************
173 //*****************************************************************************************************************
174 void GlobalEventConfig_Impl::initBindingInfo()
176 // Get ALL names of current existing list items in configuration!
177 Sequence
< OUString
> lEventNames
= GetNodeNames( SETNODE_BINDINGS
, utl::CONFIG_NAME_LOCAL_PATH
);
179 OUString
aSetNode( SETNODE_BINDINGS
);
180 aSetNode
+= PATHDELIMITER
;
182 OUString
aCommandKey( PATHDELIMITER
);
183 aCommandKey
+= PROPERTYNAME_BINDINGURL
;
186 Sequence
< OUString
> lMacros(1);
187 for (sal_Int32 i
=0; i
<lEventNames
.getLength(); ++i
)
189 OUStringBuffer
aBuffer( 32 );
190 aBuffer
.append( aSetNode
);
191 aBuffer
.append( lEventNames
[i
] );
192 aBuffer
.append( aCommandKey
);
193 lMacros
[0] = aBuffer
.makeStringAndClear();
194 OSL_TRACE("reading binding for: %s",::rtl::OUStringToOString(lMacros
[0] , RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
195 Sequence
< Any
> lValues
= GetProperties( lMacros
);
197 if( lValues
.getLength() > 0 )
199 lValues
[0] >>= sMacroURL
;
200 sal_Int32 startIndex
= lEventNames
[i
].indexOf('\'');
201 sal_Int32 endIndex
= lEventNames
[i
].lastIndexOf('\'');
202 if( startIndex
>=0 && endIndex
> 0 )
205 OUString eventName
= lEventNames
[i
].copy(startIndex
,endIndex
-startIndex
);
206 m_eventBindingHash
[ eventName
] = sMacroURL
;
212 Reference
< container::XNameReplace
> SAL_CALL
GlobalEventConfig_Impl::getEvents() throw (::com::sun::star::uno::RuntimeException
)
214 //how to return this object as an XNameReplace?
215 Reference
< container::XNameReplace
> ret
;
219 void SAL_CALL
GlobalEventConfig_Impl::replaceByName( const OUString
& aName
, const Any
& aElement
) throw (lang::IllegalArgumentException
, container::NoSuchElementException
, lang::WrappedTargetException
, RuntimeException
)
221 Sequence
< beans::PropertyValue
> props
;
222 //DF should we prepopulate the hash with a list of valid event Names?
223 if( sal_False
== ( aElement
>>= props
) )
225 throw lang::IllegalArgumentException( OUString(),
226 Reference
< XInterface
> (), 2);
229 sal_Int32 nPropCount
= props
.getLength();
230 for( sal_Int32 index
= 0 ; index
< nPropCount
; ++index
)
232 if ( props
[ index
].Name
.compareToAscii( "Script" ) == 0 )
233 props
[ index
].Value
>>= macroURL
;
235 m_eventBindingHash
[ aName
] = macroURL
;
239 Any SAL_CALL
GlobalEventConfig_Impl::getByName( const OUString
& aName
) throw (container::NoSuchElementException
, lang::WrappedTargetException
, RuntimeException
)
242 Sequence
< beans::PropertyValue
> props(2);
243 props
[0].Name
= OUString::createFromAscii("EventType");
244 props
[0].Value
<<= OUString::createFromAscii("Script");
245 props
[1].Name
= OUString::createFromAscii("Script");
246 EventBindingHash::const_iterator it
= m_eventBindingHash
.find( aName
);
247 if( it
!= m_eventBindingHash
.end() )
249 props
[1].Value
<<= it
->second
;
253 // not yet accessed - is it a supported name?
254 SupportedEventsVector::const_iterator pos
= ::std::find(
255 m_supportedEvents
.begin(), m_supportedEvents
.end(), aName
);
256 if ( pos
== m_supportedEvents
.end() )
257 throw container::NoSuchElementException( aName
, NULL
);
259 props
[1].Value
<<= OUString();
265 Sequence
< OUString
> SAL_CALL
GlobalEventConfig_Impl::getElementNames( ) throw (RuntimeException
)
267 const ::rtl::OUString
* pRet
= m_supportedEvents
.empty() ? NULL
: &m_supportedEvents
[0];
268 return uno::Sequence
< ::rtl::OUString
>(pRet
, m_supportedEvents
.size());
271 sal_Bool SAL_CALL
GlobalEventConfig_Impl::hasByName( const OUString
& aName
) throw (RuntimeException
)
273 if ( m_eventBindingHash
.find( aName
) != m_eventBindingHash
.end() )
276 // never accessed before - is it supported in general?
277 SupportedEventsVector::const_iterator pos
= ::std::find(
278 m_supportedEvents
.begin(), m_supportedEvents
.end(), aName
);
279 if ( pos
!= m_supportedEvents
.end() )
285 Type SAL_CALL
GlobalEventConfig_Impl::getElementType( ) throw (RuntimeException
)
287 //DF definitly not sure about this??
288 return ::getCppuType((const Sequence
<beans::PropertyValue
>*)0);
291 sal_Bool SAL_CALL
GlobalEventConfig_Impl::hasElements( ) throw (RuntimeException
)
293 return ( m_eventBindingHash
.empty() );
296 // and now the wrapper
299 //initialize static member
300 GlobalEventConfig_Impl
* GlobalEventConfig::m_pImpl
= NULL
;
301 sal_Int32
GlobalEventConfig::m_nRefCount
= 0 ;
303 GlobalEventConfig::GlobalEventConfig()
305 // Global access, must be guarded (multithreading!).
306 MutexGuard
aGuard( GetOwnStaticMutex() );
307 // Increase our refcount ...
309 // ... and initialize our data container only if it not already exist!
310 if( m_pImpl
== NULL
)
312 m_pImpl
= new GlobalEventConfig_Impl
;
313 ItemHolder1::holdConfigItem(E_EVENTCFG
);
317 GlobalEventConfig::~GlobalEventConfig()
319 // Global access, must be guarded (multithreading!)
320 MutexGuard
aGuard( GetOwnStaticMutex() );
321 // Decrease our refcount.
323 // If last instance was deleted ...
324 // we must destroy our static data container!
325 if( m_nRefCount
<= 0 )
332 void GlobalEventConfig::EstablishFrameCallback(const ::com::sun::star::uno::Reference
< ::com::sun::star::frame::XFrame
>& xFrame
)
334 MutexGuard
aGuard( GetOwnStaticMutex() );
335 m_pImpl
->EstablishFrameCallback( xFrame
);
338 Reference
< container::XNameReplace
> SAL_CALL
GlobalEventConfig::getEvents() throw (::com::sun::star::uno::RuntimeException
)
340 MutexGuard
aGuard( GetOwnStaticMutex() );
341 Reference
< container::XNameReplace
> ret(this);
345 void SAL_CALL
GlobalEventConfig::replaceByName( const OUString
& aName
, const Any
& aElement
) throw (lang::IllegalArgumentException
, container::NoSuchElementException
, lang::WrappedTargetException
, RuntimeException
)
347 MutexGuard
aGuard( GetOwnStaticMutex() );
348 m_pImpl
->replaceByName( aName
, aElement
);
350 Any SAL_CALL
GlobalEventConfig::getByName( const OUString
& aName
) throw (container::NoSuchElementException
, lang::WrappedTargetException
, RuntimeException
)
352 MutexGuard
aGuard( GetOwnStaticMutex() );
353 return m_pImpl
->getByName( aName
);
355 Sequence
< OUString
> SAL_CALL
GlobalEventConfig::getElementNames( ) throw (RuntimeException
)
357 MutexGuard
aGuard( GetOwnStaticMutex() );
358 return m_pImpl
->getElementNames( );
360 sal_Bool SAL_CALL
GlobalEventConfig::hasByName( const OUString
& aName
) throw (RuntimeException
)
362 MutexGuard
aGuard( GetOwnStaticMutex() );
363 return m_pImpl
->hasByName( aName
);
365 Type SAL_CALL
GlobalEventConfig::getElementType( ) throw (RuntimeException
)
367 MutexGuard
aGuard( GetOwnStaticMutex() );
368 return m_pImpl
->getElementType( );
370 sal_Bool SAL_CALL
GlobalEventConfig::hasElements( ) throw (RuntimeException
)
372 MutexGuard
aGuard( GetOwnStaticMutex() );
373 return m_pImpl
->hasElements( );
376 Mutex
& GlobalEventConfig::GetOwnStaticMutex()
378 // Initialize static mutex only for one time!
379 static Mutex
* pMutex
= NULL
;
380 // If these method first called (Mutex not already exist!) ...
383 // ... we must create a new one. Protect following code with
384 // the global mutex -
385 // It must be - we create a static variable!
386 MutexGuard
aGuard( Mutex::getGlobalMutex() );
387 // We must check our pointer again - because it can be that
388 // another instance of our class will be faster then these!
391 // Create the new mutex and set it for return on static variable.
396 // Return new created or already existing mutex object.