1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_unotools.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <unotools/eventcfg.hxx>
32*cdf0e10cSrcweir #include <unotools/configmgr.hxx>
33*cdf0e10cSrcweir #include <unotools/configitem.hxx>
34*cdf0e10cSrcweir #include <tools/debug.hxx>
35*cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
36*cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
37*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
38*cdf0e10cSrcweir #include <cppuhelper/weakref.hxx>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #ifndef __SGI_STL_HASH_MAP
41*cdf0e10cSrcweir #include <hash_map>
42*cdf0e10cSrcweir #endif
43*cdf0e10cSrcweir #include <tools/urlobj.hxx>
44*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #include <itemholder1.hxx>
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir #include <algorithm>
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir using namespace ::std					;
51*cdf0e10cSrcweir using namespace ::utl					;
52*cdf0e10cSrcweir using namespace ::rtl					;
53*cdf0e10cSrcweir using namespace ::osl					;
54*cdf0e10cSrcweir using namespace ::com::sun::star::uno	;
55*cdf0e10cSrcweir using namespace ::com::sun::star;
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir #define ROOTNODE_EVENTS OUString(RTL_CONSTASCII_USTRINGPARAM("Office.Events/ApplicationEvents" ))
58*cdf0e10cSrcweir #define PATHDELIMITER OUString(RTL_CONSTASCII_USTRINGPARAM("/"))
59*cdf0e10cSrcweir #define SETNODE_BINDINGS OUString(RTL_CONSTASCII_USTRINGPARAM("Bindings" ))
60*cdf0e10cSrcweir #define PROPERTYNAME_BINDINGURL OUString(RTL_CONSTASCII_USTRINGPARAM("BindingURL"))
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir const char* pEventAsciiNames[] =
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir "OnStartApp",
65*cdf0e10cSrcweir "OnCloseApp",
66*cdf0e10cSrcweir "OnCreate",
67*cdf0e10cSrcweir "OnNew",
68*cdf0e10cSrcweir "OnLoadFinished",
69*cdf0e10cSrcweir "OnLoad",
70*cdf0e10cSrcweir "OnPrepareUnload",
71*cdf0e10cSrcweir "OnUnload",
72*cdf0e10cSrcweir "OnSave",
73*cdf0e10cSrcweir "OnSaveDone",
74*cdf0e10cSrcweir "OnSaveFailed",
75*cdf0e10cSrcweir "OnSaveAs",
76*cdf0e10cSrcweir "OnSaveAsDone",
77*cdf0e10cSrcweir "OnSaveAsFailed",
78*cdf0e10cSrcweir "OnCopyTo",
79*cdf0e10cSrcweir "OnCopyToDone",
80*cdf0e10cSrcweir "OnCopyToFailed",
81*cdf0e10cSrcweir "OnFocus",
82*cdf0e10cSrcweir "OnUnfocus",
83*cdf0e10cSrcweir "OnPrint",
84*cdf0e10cSrcweir "OnViewCreated",
85*cdf0e10cSrcweir "OnPrepareViewClosing",
86*cdf0e10cSrcweir "OnViewClosed",
87*cdf0e10cSrcweir "OnModifyChanged",
88*cdf0e10cSrcweir "OnTitleChanged",
89*cdf0e10cSrcweir "OnVisAreaChanged",
90*cdf0e10cSrcweir "OnModeChanged",
91*cdf0e10cSrcweir "OnStorageChanged"
92*cdf0e10cSrcweir };
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir GlobalEventConfig_Impl::GlobalEventConfig_Impl()
95*cdf0e10cSrcweir     :   ConfigItem( ROOTNODE_EVENTS, CONFIG_MODE_IMMEDIATE_UPDATE )
96*cdf0e10cSrcweir {
97*cdf0e10cSrcweir     // the supported event names
98*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_STARTAPP] ) );
99*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_CLOSEAPP] ) );
100*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_DOCCREATED] ) );
101*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_CREATEDOC] ) );
102*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_LOADFINISHED] ) );
103*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_OPENDOC] ) );
104*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_PREPARECLOSEDOC] ) );
105*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_CLOSEDOC] ) );
106*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEDOC] ) );
107*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEDOCDONE] ) );
108*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEDOCFAILED] ) );
109*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEASDOC] ) );
110*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEASDOCDONE] ) );
111*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEASDOCFAILED] ) );
112*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVETODOC] ) );
113*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVETODOCDONE] ) );
114*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVETODOCFAILED] ) );
115*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_ACTIVATEDOC] ) );
116*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_DEACTIVATEDOC] ) );
117*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_PRINTDOC] ) );
118*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_VIEWCREATED] ) );
119*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_PREPARECLOSEVIEW] ) );
120*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_CLOSEVIEW] ) );
121*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_MODIFYCHANGED] ) );
122*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_TITLECHANGED] ) );
123*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_VISAREACHANGED] ) );
124*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_MODECHANGED] ) );
125*cdf0e10cSrcweir     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_STORAGECHANGED] ) );
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir     initBindingInfo();
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir /*TODO: Not used in the moment! see Notify() ...
130*cdf0e10cSrcweir 	// Enable notification mechanism of our baseclass.
131*cdf0e10cSrcweir 	// We need it to get information about changes outside these class on our used configuration keys! */
132*cdf0e10cSrcweir     Sequence< OUString > aNotifySeq( 1 );
133*cdf0e10cSrcweir     aNotifySeq[0] = OUString( RTL_CONSTASCII_USTRINGPARAM( "Events" ));
134*cdf0e10cSrcweir     EnableNotification( aNotifySeq, sal_True );
135*cdf0e10cSrcweir }
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir //*****************************************************************************************************************
138*cdf0e10cSrcweir //	destructor
139*cdf0e10cSrcweir //*****************************************************************************************************************
140*cdf0e10cSrcweir GlobalEventConfig_Impl::~GlobalEventConfig_Impl()
141*cdf0e10cSrcweir {
142*cdf0e10cSrcweir 	// We must save our current values .. if user forget it!
143*cdf0e10cSrcweir 	if( IsModified() == sal_True )
144*cdf0e10cSrcweir 	{
145*cdf0e10cSrcweir 		Commit();
146*cdf0e10cSrcweir 	}
147*cdf0e10cSrcweir }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir ::rtl::OUString GlobalEventConfig_Impl::GetEventName( sal_Int32 nIndex )
150*cdf0e10cSrcweir {
151*cdf0e10cSrcweir 	if ( nIndex < (sal_Int32) m_supportedEvents.size() )
152*cdf0e10cSrcweir 		return m_supportedEvents[nIndex];
153*cdf0e10cSrcweir 	else
154*cdf0e10cSrcweir 		return rtl::OUString();
155*cdf0e10cSrcweir }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir //*****************************************************************************************************************
158*cdf0e10cSrcweir //	public method
159*cdf0e10cSrcweir //*****************************************************************************************************************
160*cdf0e10cSrcweir void GlobalEventConfig_Impl::Notify( const Sequence< OUString >& )
161*cdf0e10cSrcweir {
162*cdf0e10cSrcweir     MutexGuard aGuard( GlobalEventConfig::GetOwnStaticMutex() );
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir     initBindingInfo();
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir     // dont forget to update all existing frames and her might cached dispatch objects!
167*cdf0e10cSrcweir     // But look for already killed frames. We hold weak references instead of hard ones ...
168*cdf0e10cSrcweir     for (FrameVector::const_iterator pIt  = m_lFrames.begin();
169*cdf0e10cSrcweir                                         pIt != m_lFrames.end()  ;
170*cdf0e10cSrcweir                                       ++pIt                     )
171*cdf0e10cSrcweir     {
172*cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame > xFrame(pIt->get(), ::com::sun::star::uno::UNO_QUERY);
173*cdf0e10cSrcweir         if (xFrame.is())
174*cdf0e10cSrcweir             xFrame->contextChanged();
175*cdf0e10cSrcweir     }
176*cdf0e10cSrcweir }
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir //*****************************************************************************************************************
179*cdf0e10cSrcweir //	public method
180*cdf0e10cSrcweir //*****************************************************************************************************************
181*cdf0e10cSrcweir void GlobalEventConfig_Impl::Commit()
182*cdf0e10cSrcweir {
183*cdf0e10cSrcweir     //DF need to check it this is correct??
184*cdf0e10cSrcweir     OSL_TRACE("In GlobalEventConfig_Impl::Commit");
185*cdf0e10cSrcweir     EventBindingHash::const_iterator it = m_eventBindingHash.begin();
186*cdf0e10cSrcweir     EventBindingHash::const_iterator it_end = m_eventBindingHash.end();
187*cdf0e10cSrcweir     // clear the existing nodes
188*cdf0e10cSrcweir     ClearNodeSet( SETNODE_BINDINGS );
189*cdf0e10cSrcweir     Sequence< beans::PropertyValue > seqValues( 1 );
190*cdf0e10cSrcweir     OUString sNode;
191*cdf0e10cSrcweir     static const OUString sPrefix(SETNODE_BINDINGS + PATHDELIMITER + OUString::createFromAscii("BindingType['"));
192*cdf0e10cSrcweir     static const OUString sPostfix(OUString::createFromAscii("']") + PATHDELIMITER + PROPERTYNAME_BINDINGURL);
193*cdf0e10cSrcweir     //step through the list of events
194*cdf0e10cSrcweir     for(int i=0;it!=it_end;++it,++i)
195*cdf0e10cSrcweir     {
196*cdf0e10cSrcweir         //no point in writing out empty bindings!
197*cdf0e10cSrcweir         if(it->second.getLength() == 0 )
198*cdf0e10cSrcweir             continue;
199*cdf0e10cSrcweir         sNode = sPrefix + it->first + sPostfix;
200*cdf0e10cSrcweir         OSL_TRACE("writing binding for: %s",::rtl::OUStringToOString(sNode , RTL_TEXTENCODING_ASCII_US ).pData->buffer);
201*cdf0e10cSrcweir         seqValues[ 0 ].Name = sNode;
202*cdf0e10cSrcweir         seqValues[ 0 ].Value <<= it->second;
203*cdf0e10cSrcweir         //write the data to the registry
204*cdf0e10cSrcweir         SetSetProperties(SETNODE_BINDINGS,seqValues);
205*cdf0e10cSrcweir     }
206*cdf0e10cSrcweir }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir //*****************************************************************************************************************
209*cdf0e10cSrcweir //  public method
210*cdf0e10cSrcweir //*****************************************************************************************************************
211*cdf0e10cSrcweir void GlobalEventConfig_Impl::EstablishFrameCallback(const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& xFrame)
212*cdf0e10cSrcweir {
213*cdf0e10cSrcweir     // check if frame already exists inside list
214*cdf0e10cSrcweir     // ignore double registrations
215*cdf0e10cSrcweir     // every frame must be notified one times only!
216*cdf0e10cSrcweir     ::com::sun::star::uno::WeakReference< ::com::sun::star::frame::XFrame > xWeak(xFrame);
217*cdf0e10cSrcweir     FrameVector::const_iterator pIt = ::std::find(m_lFrames.begin(), m_lFrames.end(), xWeak);
218*cdf0e10cSrcweir     if (pIt == m_lFrames.end())
219*cdf0e10cSrcweir         m_lFrames.push_back(xWeak);
220*cdf0e10cSrcweir }
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir //*****************************************************************************************************************
223*cdf0e10cSrcweir //	private method
224*cdf0e10cSrcweir //*****************************************************************************************************************
225*cdf0e10cSrcweir void GlobalEventConfig_Impl::initBindingInfo()
226*cdf0e10cSrcweir {
227*cdf0e10cSrcweir 	// Get ALL names of current existing list items in configuration!
228*cdf0e10cSrcweir     Sequence< OUString > lEventNames      = GetNodeNames( SETNODE_BINDINGS, utl::CONFIG_NAME_LOCAL_PATH );
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir 	OUString aSetNode( SETNODE_BINDINGS );
231*cdf0e10cSrcweir 	aSetNode += PATHDELIMITER;
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir 	OUString aCommandKey( PATHDELIMITER );
234*cdf0e10cSrcweir 	aCommandKey += PROPERTYNAME_BINDINGURL;
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir 	// Expand all keys
237*cdf0e10cSrcweir     Sequence< OUString > lMacros(1);
238*cdf0e10cSrcweir 	for (sal_Int32 i=0; i<lEventNames.getLength(); ++i )
239*cdf0e10cSrcweir 	{
240*cdf0e10cSrcweir 		OUStringBuffer aBuffer( 32 );
241*cdf0e10cSrcweir 		aBuffer.append( aSetNode );
242*cdf0e10cSrcweir 		aBuffer.append( lEventNames[i] );
243*cdf0e10cSrcweir 		aBuffer.append( aCommandKey );
244*cdf0e10cSrcweir 		lMacros[0] = aBuffer.makeStringAndClear();
245*cdf0e10cSrcweir         OSL_TRACE("reading binding for: %s",::rtl::OUStringToOString(lMacros[0] , RTL_TEXTENCODING_ASCII_US ).pData->buffer);
246*cdf0e10cSrcweir         Sequence< Any > lValues = GetProperties( lMacros );
247*cdf0e10cSrcweir         OUString sMacroURL;
248*cdf0e10cSrcweir         if( lValues.getLength() > 0 )
249*cdf0e10cSrcweir         {
250*cdf0e10cSrcweir             lValues[0] >>= sMacroURL;
251*cdf0e10cSrcweir             sal_Int32 startIndex = lEventNames[i].indexOf('\'');
252*cdf0e10cSrcweir             sal_Int32 endIndex =  lEventNames[i].lastIndexOf('\'');
253*cdf0e10cSrcweir             if( startIndex >=0 && endIndex > 0 )
254*cdf0e10cSrcweir             {
255*cdf0e10cSrcweir                 startIndex++;
256*cdf0e10cSrcweir                 OUString eventName = lEventNames[i].copy(startIndex,endIndex-startIndex);
257*cdf0e10cSrcweir                 m_eventBindingHash[ eventName ] = sMacroURL;
258*cdf0e10cSrcweir             }
259*cdf0e10cSrcweir         }
260*cdf0e10cSrcweir 	}
261*cdf0e10cSrcweir }
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir Reference< container::XNameReplace > SAL_CALL GlobalEventConfig_Impl::getEvents() throw (::com::sun::star::uno::RuntimeException)
264*cdf0e10cSrcweir {
265*cdf0e10cSrcweir     //how to return this object as an XNameReplace?
266*cdf0e10cSrcweir     Reference< container::XNameReplace > ret;
267*cdf0e10cSrcweir     return ret;
268*cdf0e10cSrcweir }
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir void SAL_CALL GlobalEventConfig_Impl::replaceByName( const OUString& aName, const Any& aElement ) throw (lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, RuntimeException)
271*cdf0e10cSrcweir {
272*cdf0e10cSrcweir     Sequence< beans::PropertyValue > props;
273*cdf0e10cSrcweir     //DF should we prepopulate the hash with a list of valid event Names?
274*cdf0e10cSrcweir     if( sal_False == ( aElement >>= props ) )
275*cdf0e10cSrcweir     {
276*cdf0e10cSrcweir         throw lang::IllegalArgumentException( OUString(),
277*cdf0e10cSrcweir                 Reference< XInterface > (), 2);
278*cdf0e10cSrcweir     }
279*cdf0e10cSrcweir     OUString macroURL;
280*cdf0e10cSrcweir     sal_Int32 nPropCount = props.getLength();
281*cdf0e10cSrcweir     for( sal_Int32 index = 0 ; index < nPropCount ; ++index )
282*cdf0e10cSrcweir     {
283*cdf0e10cSrcweir         if ( props[ index ].Name.compareToAscii( "Script" ) == 0 )
284*cdf0e10cSrcweir             props[ index ].Value >>= macroURL;
285*cdf0e10cSrcweir     }
286*cdf0e10cSrcweir     m_eventBindingHash[ aName ] = macroURL;
287*cdf0e10cSrcweir     SetModified();
288*cdf0e10cSrcweir }
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir Any SAL_CALL GlobalEventConfig_Impl::getByName( const OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, RuntimeException)
291*cdf0e10cSrcweir {
292*cdf0e10cSrcweir     Any aRet;
293*cdf0e10cSrcweir     Sequence< beans::PropertyValue > props(2);
294*cdf0e10cSrcweir     props[0].Name = OUString::createFromAscii("EventType");
295*cdf0e10cSrcweir     props[0].Value <<= OUString::createFromAscii("Script");
296*cdf0e10cSrcweir     props[1].Name = OUString::createFromAscii("Script");
297*cdf0e10cSrcweir     EventBindingHash::const_iterator it = m_eventBindingHash.find( aName );
298*cdf0e10cSrcweir     if( it != m_eventBindingHash.end() )
299*cdf0e10cSrcweir     {
300*cdf0e10cSrcweir         props[1].Value <<= it->second;
301*cdf0e10cSrcweir     }
302*cdf0e10cSrcweir     else
303*cdf0e10cSrcweir     {
304*cdf0e10cSrcweir         // not yet accessed - is it a supported name?
305*cdf0e10cSrcweir         SupportedEventsVector::const_iterator pos = ::std::find(
306*cdf0e10cSrcweir             m_supportedEvents.begin(), m_supportedEvents.end(), aName );
307*cdf0e10cSrcweir         if ( pos == m_supportedEvents.end() )
308*cdf0e10cSrcweir             throw container::NoSuchElementException( aName, NULL );
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir         props[1].Value <<= OUString();
311*cdf0e10cSrcweir     }
312*cdf0e10cSrcweir     aRet <<= props;
313*cdf0e10cSrcweir     return aRet;
314*cdf0e10cSrcweir }
315*cdf0e10cSrcweir 
316*cdf0e10cSrcweir Sequence< OUString > SAL_CALL GlobalEventConfig_Impl::getElementNames(  ) throw (RuntimeException)
317*cdf0e10cSrcweir {
318*cdf0e10cSrcweir     const ::rtl::OUString* pRet = m_supportedEvents.empty() ? NULL : &m_supportedEvents[0];
319*cdf0e10cSrcweir 	return uno::Sequence< ::rtl::OUString >(pRet, m_supportedEvents.size());
320*cdf0e10cSrcweir }
321*cdf0e10cSrcweir 
322*cdf0e10cSrcweir sal_Bool SAL_CALL GlobalEventConfig_Impl::hasByName( const OUString& aName ) throw (RuntimeException)
323*cdf0e10cSrcweir {
324*cdf0e10cSrcweir     if ( m_eventBindingHash.find( aName ) != m_eventBindingHash.end() )
325*cdf0e10cSrcweir         return sal_True;
326*cdf0e10cSrcweir 
327*cdf0e10cSrcweir     // never accessed before - is it supported in general?
328*cdf0e10cSrcweir     SupportedEventsVector::const_iterator pos = ::std::find(
329*cdf0e10cSrcweir         m_supportedEvents.begin(), m_supportedEvents.end(), aName );
330*cdf0e10cSrcweir     if ( pos != m_supportedEvents.end() )
331*cdf0e10cSrcweir         return sal_True;
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir     return sal_False;
334*cdf0e10cSrcweir }
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir Type SAL_CALL GlobalEventConfig_Impl::getElementType(  ) throw (RuntimeException)
337*cdf0e10cSrcweir {
338*cdf0e10cSrcweir     //DF definitly not sure about this??
339*cdf0e10cSrcweir     return ::getCppuType((const Sequence<beans::PropertyValue>*)0);
340*cdf0e10cSrcweir }
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir sal_Bool SAL_CALL GlobalEventConfig_Impl::hasElements(  ) throw (RuntimeException)
343*cdf0e10cSrcweir {
344*cdf0e10cSrcweir     return ( m_eventBindingHash.empty() );
345*cdf0e10cSrcweir }
346*cdf0e10cSrcweir 
347*cdf0e10cSrcweir // and now the wrapper
348*cdf0e10cSrcweir 
349*cdf0e10cSrcweir 
350*cdf0e10cSrcweir //initialize static member
351*cdf0e10cSrcweir GlobalEventConfig_Impl*     GlobalEventConfig::m_pImpl = NULL  ;
352*cdf0e10cSrcweir sal_Int32                   GlobalEventConfig::m_nRefCount      = 0     ;
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir GlobalEventConfig::GlobalEventConfig()
355*cdf0e10cSrcweir {
356*cdf0e10cSrcweir     // Global access, must be guarded (multithreading!).
357*cdf0e10cSrcweir     MutexGuard aGuard( GetOwnStaticMutex() );
358*cdf0e10cSrcweir 	// Increase our refcount ...
359*cdf0e10cSrcweir 	++m_nRefCount;
360*cdf0e10cSrcweir 	// ... and initialize our data container only if it not already exist!
361*cdf0e10cSrcweir     if( m_pImpl == NULL )
362*cdf0e10cSrcweir 	{
363*cdf0e10cSrcweir         m_pImpl = new GlobalEventConfig_Impl;
364*cdf0e10cSrcweir         ItemHolder1::holdConfigItem(E_EVENTCFG);
365*cdf0e10cSrcweir 	}
366*cdf0e10cSrcweir }
367*cdf0e10cSrcweir 
368*cdf0e10cSrcweir GlobalEventConfig::~GlobalEventConfig()
369*cdf0e10cSrcweir {
370*cdf0e10cSrcweir     // Global access, must be guarded (multithreading!)
371*cdf0e10cSrcweir     MutexGuard aGuard( GetOwnStaticMutex() );
372*cdf0e10cSrcweir 	// Decrease our refcount.
373*cdf0e10cSrcweir 	--m_nRefCount;
374*cdf0e10cSrcweir 	// If last instance was deleted ...
375*cdf0e10cSrcweir 	// we must destroy our static data container!
376*cdf0e10cSrcweir     if( m_nRefCount <= 0 )
377*cdf0e10cSrcweir 	{
378*cdf0e10cSrcweir 		delete m_pImpl;
379*cdf0e10cSrcweir 		m_pImpl = NULL;
380*cdf0e10cSrcweir 	}
381*cdf0e10cSrcweir }
382*cdf0e10cSrcweir 
383*cdf0e10cSrcweir void GlobalEventConfig::EstablishFrameCallback(const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& xFrame)
384*cdf0e10cSrcweir {
385*cdf0e10cSrcweir     MutexGuard aGuard( GetOwnStaticMutex() );
386*cdf0e10cSrcweir     m_pImpl->EstablishFrameCallback( xFrame );
387*cdf0e10cSrcweir }
388*cdf0e10cSrcweir 
389*cdf0e10cSrcweir Reference< container::XNameReplace > SAL_CALL GlobalEventConfig::getEvents() throw (::com::sun::star::uno::RuntimeException)
390*cdf0e10cSrcweir {
391*cdf0e10cSrcweir     MutexGuard aGuard( GetOwnStaticMutex() );
392*cdf0e10cSrcweir     Reference< container::XNameReplace > ret(this);
393*cdf0e10cSrcweir     return ret;
394*cdf0e10cSrcweir }
395*cdf0e10cSrcweir 
396*cdf0e10cSrcweir void SAL_CALL GlobalEventConfig::replaceByName( const OUString& aName, const Any& aElement ) throw (lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, RuntimeException)
397*cdf0e10cSrcweir {
398*cdf0e10cSrcweir     MutexGuard aGuard( GetOwnStaticMutex() );
399*cdf0e10cSrcweir     m_pImpl->replaceByName( aName, aElement );
400*cdf0e10cSrcweir }
401*cdf0e10cSrcweir Any SAL_CALL GlobalEventConfig::getByName( const OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, RuntimeException)
402*cdf0e10cSrcweir {
403*cdf0e10cSrcweir     MutexGuard aGuard( GetOwnStaticMutex() );
404*cdf0e10cSrcweir     return m_pImpl->getByName( aName );
405*cdf0e10cSrcweir }
406*cdf0e10cSrcweir Sequence< OUString > SAL_CALL GlobalEventConfig::getElementNames(  ) throw (RuntimeException)
407*cdf0e10cSrcweir {
408*cdf0e10cSrcweir     MutexGuard aGuard( GetOwnStaticMutex() );
409*cdf0e10cSrcweir     return m_pImpl->getElementNames( );
410*cdf0e10cSrcweir }
411*cdf0e10cSrcweir sal_Bool SAL_CALL GlobalEventConfig::hasByName( const OUString& aName ) throw (RuntimeException)
412*cdf0e10cSrcweir {
413*cdf0e10cSrcweir     MutexGuard aGuard( GetOwnStaticMutex() );
414*cdf0e10cSrcweir     return m_pImpl->hasByName( aName );
415*cdf0e10cSrcweir }
416*cdf0e10cSrcweir Type SAL_CALL GlobalEventConfig::getElementType(  ) throw (RuntimeException)
417*cdf0e10cSrcweir {
418*cdf0e10cSrcweir     MutexGuard aGuard( GetOwnStaticMutex() );
419*cdf0e10cSrcweir     return m_pImpl->getElementType( );
420*cdf0e10cSrcweir }
421*cdf0e10cSrcweir sal_Bool SAL_CALL GlobalEventConfig::hasElements(  ) throw (RuntimeException)
422*cdf0e10cSrcweir {
423*cdf0e10cSrcweir     MutexGuard aGuard( GetOwnStaticMutex() );
424*cdf0e10cSrcweir     return m_pImpl->hasElements( );
425*cdf0e10cSrcweir }
426*cdf0e10cSrcweir 
427*cdf0e10cSrcweir Mutex& GlobalEventConfig::GetOwnStaticMutex()
428*cdf0e10cSrcweir {
429*cdf0e10cSrcweir 	// Initialize static mutex only for one time!
430*cdf0e10cSrcweir     static Mutex* pMutex = NULL;
431*cdf0e10cSrcweir 	// If these method first called (Mutex not already exist!) ...
432*cdf0e10cSrcweir     if( pMutex == NULL )
433*cdf0e10cSrcweir     {
434*cdf0e10cSrcweir 		// ... we must create a new one. Protect following code with
435*cdf0e10cSrcweir         // the global mutex -
436*cdf0e10cSrcweir 		// It must be - we create a static variable!
437*cdf0e10cSrcweir         MutexGuard aGuard( Mutex::getGlobalMutex() );
438*cdf0e10cSrcweir 		// We must check our pointer again - because it can be that
439*cdf0e10cSrcweir         // another instance of our class will be faster then these!
440*cdf0e10cSrcweir         if( pMutex == NULL )
441*cdf0e10cSrcweir         {
442*cdf0e10cSrcweir 			// Create the new mutex and set it for return on static variable.
443*cdf0e10cSrcweir             static Mutex aMutex;
444*cdf0e10cSrcweir             pMutex = &aMutex;
445*cdf0e10cSrcweir         }
446*cdf0e10cSrcweir     }
447*cdf0e10cSrcweir 	// Return new created or already existing mutex object.
448*cdf0e10cSrcweir     return *pMutex;
449*cdf0e10cSrcweir }
450*cdf0e10cSrcweir 
451*cdf0e10cSrcweir ::rtl::OUString GlobalEventConfig::GetEventName( sal_Int32 nIndex )
452*cdf0e10cSrcweir {
453*cdf0e10cSrcweir 	return GlobalEventConfig().m_pImpl->GetEventName( nIndex );
454*cdf0e10cSrcweir }
455*cdf0e10cSrcweir 
456