1d119d52dSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3d119d52dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4d119d52dSAndrew Rist * or more contributor license agreements. See the NOTICE file 5d119d52dSAndrew Rist * distributed with this work for additional information 6d119d52dSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7d119d52dSAndrew Rist * to you under the Apache License, Version 2.0 (the 8d119d52dSAndrew Rist * "License"); you may not use this file except in compliance 9d119d52dSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11d119d52dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13d119d52dSAndrew Rist * Unless required by applicable law or agreed to in writing, 14d119d52dSAndrew Rist * software distributed under the License is distributed on an 15d119d52dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16d119d52dSAndrew Rist * KIND, either express or implied. See the License for the 17d119d52dSAndrew Rist * specific language governing permissions and limitations 18d119d52dSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20d119d52dSAndrew Rist *************************************************************/ 21d119d52dSAndrew Rist 22d119d52dSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_sfx2.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir //-------------------------------------------------------------------------------------------------------- 28cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp> 29cdf0e10cSrcweir 30cdf0e10cSrcweir #ifndef _COM_SUN_STAR_UTL_URL_HPP_ 31cdf0e10cSrcweir #include <com/sun/star/util/URL.hpp> 32cdf0e10cSrcweir #endif 33cdf0e10cSrcweir 34cdf0e10cSrcweir #ifndef _COM_SUN_STAR_UTL_XURLTRANSFORMER_HPP_ 35cdf0e10cSrcweir #include <com/sun/star/util/XURLTransformer.hpp> 36cdf0e10cSrcweir #endif 37cdf0e10cSrcweir #include <tools/urlobj.hxx> 38cdf0e10cSrcweir #include <tools/diagnose_ex.h> 39cdf0e10cSrcweir #include <svl/macitem.hxx> 40cdf0e10cSrcweir #include <sfx2/appuno.hxx> 41cdf0e10cSrcweir #include <sfx2/objsh.hxx> 42cdf0e10cSrcweir #include <sfx2/sfxbasemodel.hxx> 43cdf0e10cSrcweir #include <sfx2/evntconf.hxx> 44cdf0e10cSrcweir #include <unotools/eventcfg.hxx> 45cdf0e10cSrcweir 46cdf0e10cSrcweir #include <unotools/securityoptions.hxx> 47cdf0e10cSrcweir #include <comphelper/processfactory.hxx> 48cdf0e10cSrcweir #include <comphelper/namedvaluecollection.hxx> 49cdf0e10cSrcweir #include "eventsupplier.hxx" 50cdf0e10cSrcweir 51cdf0e10cSrcweir #include <sfx2/app.hxx> 52cdf0e10cSrcweir #include "sfx2/sfxresid.hxx" 53cdf0e10cSrcweir 54cdf0e10cSrcweir #include <sfx2/sfxsids.hrc> 55cdf0e10cSrcweir #include "sfxlocal.hrc" 56cdf0e10cSrcweir #include <sfx2/docfile.hxx> 57cdf0e10cSrcweir #include <sfx2/viewfrm.hxx> 58cdf0e10cSrcweir #include <sfx2/frame.hxx> 59cdf0e10cSrcweir 60cdf0e10cSrcweir //-------------------------------------------------------------------------------------------------------- 61cdf0e10cSrcweir 62cdf0e10cSrcweir #define MACRO_PRFIX "macro://" 63cdf0e10cSrcweir #define MACRO_POSTFIX "()" 64cdf0e10cSrcweir 65cdf0e10cSrcweir //-------------------------------------------------------------------------------------------------------- 66cdf0e10cSrcweir 67cdf0e10cSrcweir #define PROPERTYVALUE ::com::sun::star::beans::PropertyValue 68cdf0e10cSrcweir #define UNO_QUERY ::com::sun::star::uno::UNO_QUERY 69cdf0e10cSrcweir 70cdf0e10cSrcweir namespace css = ::com::sun::star; 71cdf0e10cSrcweir using ::com::sun::star::uno::Sequence; 72cdf0e10cSrcweir using ::com::sun::star::beans::PropertyValue; 73cdf0e10cSrcweir 74cdf0e10cSrcweir //-------------------------------------------------------------------------------------------------------- 75cdf0e10cSrcweir // --- XNameReplace --- 76cdf0e10cSrcweir //-------------------------------------------------------------------------------------------------------- 77cdf0e10cSrcweir void SAL_CALL SfxEvents_Impl::replaceByName( const OUSTRING & aName, const ANY & rElement ) 78cdf0e10cSrcweir throw( ILLEGALARGUMENTEXCEPTION, NOSUCHELEMENTEXCEPTION, 79cdf0e10cSrcweir WRAPPEDTARGETEXCEPTION, RUNTIMEEXCEPTION ) 80cdf0e10cSrcweir { 81cdf0e10cSrcweir ::osl::MutexGuard aGuard( maMutex ); 82cdf0e10cSrcweir 83cdf0e10cSrcweir // find the event in the list and replace the data 84cdf0e10cSrcweir long nCount = maEventNames.getLength(); 85cdf0e10cSrcweir for ( long i=0; i<nCount; i++ ) 86cdf0e10cSrcweir { 87cdf0e10cSrcweir if ( maEventNames[i] == aName ) 88cdf0e10cSrcweir { 89cdf0e10cSrcweir // check for correct type of the element 901b257601SMichael Stahl if ( !::comphelper::NamedValueCollection::canExtractFrom( rElement ) ) 91cdf0e10cSrcweir throw ILLEGALARGUMENTEXCEPTION(); 921b257601SMichael Stahl ::comphelper::NamedValueCollection const aEventDescriptor( rElement ); 93cdf0e10cSrcweir 94cdf0e10cSrcweir // create Configuration at first, creation might call this method also and that would overwrite everything 95cdf0e10cSrcweir // we might have stored before! 96cdf0e10cSrcweir if ( mpObjShell && !mpObjShell->IsLoading() ) 97cdf0e10cSrcweir mpObjShell->SetModified( sal_True ); 98cdf0e10cSrcweir 99cdf0e10cSrcweir ::comphelper::NamedValueCollection aNormalizedDescriptor; 100cdf0e10cSrcweir NormalizeMacro( aEventDescriptor, aNormalizedDescriptor, mpObjShell ); 101cdf0e10cSrcweir 102cdf0e10cSrcweir ::rtl::OUString sType; 103cdf0e10cSrcweir if ( ( aNormalizedDescriptor.size() == 1 ) 104cdf0e10cSrcweir && ( aNormalizedDescriptor.has( PROP_EVENT_TYPE ) == 0 ) 105cdf0e10cSrcweir && ( aNormalizedDescriptor.get( PROP_EVENT_TYPE ) >>= sType ) 106cdf0e10cSrcweir && ( sType.getLength() == 0 ) 107cdf0e10cSrcweir ) 108cdf0e10cSrcweir { 109cdf0e10cSrcweir // An empty event type means no binding. Therefore reset data 110cdf0e10cSrcweir // to reflect that state. 111cdf0e10cSrcweir // (that's for compatibility only. Nowadays, the Tools/Customize dialog should 112cdf0e10cSrcweir // set an empty sequence to indicate the request for resetting the assignment.) 113cdf0e10cSrcweir OSL_ENSURE( false, "legacy event assignment format detected" ); 114cdf0e10cSrcweir aNormalizedDescriptor.clear(); 115cdf0e10cSrcweir } 116cdf0e10cSrcweir 117cdf0e10cSrcweir if ( !aNormalizedDescriptor.empty() ) 118cdf0e10cSrcweir { 119cdf0e10cSrcweir maEventData[i] <<= aNormalizedDescriptor.getPropertyValues(); 120cdf0e10cSrcweir } 121cdf0e10cSrcweir else 122cdf0e10cSrcweir { 123cdf0e10cSrcweir maEventData[i].clear(); 124cdf0e10cSrcweir } 125cdf0e10cSrcweir return; 126cdf0e10cSrcweir } 127cdf0e10cSrcweir } 128cdf0e10cSrcweir 129cdf0e10cSrcweir throw NOSUCHELEMENTEXCEPTION(); 130cdf0e10cSrcweir } 131cdf0e10cSrcweir 132cdf0e10cSrcweir //-------------------------------------------------------------------------------------------------------- 133cdf0e10cSrcweir // --- XNameAccess --- 134cdf0e10cSrcweir //-------------------------------------------------------------------------------------------------------- 135cdf0e10cSrcweir ANY SAL_CALL SfxEvents_Impl::getByName( const OUSTRING& aName ) 136cdf0e10cSrcweir throw( NOSUCHELEMENTEXCEPTION, WRAPPEDTARGETEXCEPTION, 137cdf0e10cSrcweir RUNTIMEEXCEPTION ) 138cdf0e10cSrcweir { 139cdf0e10cSrcweir ::osl::MutexGuard aGuard( maMutex ); 140cdf0e10cSrcweir 141cdf0e10cSrcweir // find the event in the list and return the data 142cdf0e10cSrcweir 143cdf0e10cSrcweir long nCount = maEventNames.getLength(); 144cdf0e10cSrcweir 145cdf0e10cSrcweir for ( long i=0; i<nCount; i++ ) 146cdf0e10cSrcweir { 147cdf0e10cSrcweir if ( maEventNames[i] == aName ) 148cdf0e10cSrcweir return maEventData[i]; 149cdf0e10cSrcweir } 150cdf0e10cSrcweir 151cdf0e10cSrcweir throw NOSUCHELEMENTEXCEPTION(); 152cdf0e10cSrcweir } 153cdf0e10cSrcweir 154cdf0e10cSrcweir //-------------------------------------------------------------------------------------------------------- 155cdf0e10cSrcweir SEQUENCE< OUSTRING > SAL_CALL SfxEvents_Impl::getElementNames() throw ( RUNTIMEEXCEPTION ) 156cdf0e10cSrcweir { 157cdf0e10cSrcweir return maEventNames; 158cdf0e10cSrcweir } 159cdf0e10cSrcweir 160cdf0e10cSrcweir //-------------------------------------------------------------------------------------------------------- 161cdf0e10cSrcweir sal_Bool SAL_CALL SfxEvents_Impl::hasByName( const OUSTRING& aName ) throw ( RUNTIMEEXCEPTION ) 162cdf0e10cSrcweir { 163cdf0e10cSrcweir ::osl::MutexGuard aGuard( maMutex ); 164cdf0e10cSrcweir 165cdf0e10cSrcweir // find the event in the list and return the data 166cdf0e10cSrcweir 167cdf0e10cSrcweir long nCount = maEventNames.getLength(); 168cdf0e10cSrcweir 169cdf0e10cSrcweir for ( long i=0; i<nCount; i++ ) 170cdf0e10cSrcweir { 171cdf0e10cSrcweir if ( maEventNames[i] == aName ) 172cdf0e10cSrcweir return sal_True; 173cdf0e10cSrcweir } 174cdf0e10cSrcweir 175cdf0e10cSrcweir return sal_False; 176cdf0e10cSrcweir } 177cdf0e10cSrcweir 178cdf0e10cSrcweir //-------------------------------------------------------------------------------------------------------- 179cdf0e10cSrcweir // --- XElementAccess ( parent of XNameAccess ) --- 180cdf0e10cSrcweir //-------------------------------------------------------------------------------------------------------- 181cdf0e10cSrcweir UNOTYPE SAL_CALL SfxEvents_Impl::getElementType() throw ( RUNTIMEEXCEPTION ) 182cdf0e10cSrcweir { 183cdf0e10cSrcweir UNOTYPE aElementType = ::getCppuType( (const SEQUENCE < PROPERTYVALUE > *)0 ); 184cdf0e10cSrcweir return aElementType; 185cdf0e10cSrcweir } 186cdf0e10cSrcweir 187cdf0e10cSrcweir //-------------------------------------------------------------------------------------------------------- 188cdf0e10cSrcweir sal_Bool SAL_CALL SfxEvents_Impl::hasElements() throw ( RUNTIMEEXCEPTION ) 189cdf0e10cSrcweir { 190cdf0e10cSrcweir ::osl::MutexGuard aGuard( maMutex ); 191cdf0e10cSrcweir 192cdf0e10cSrcweir if ( maEventNames.getLength() ) 193cdf0e10cSrcweir return sal_True; 194cdf0e10cSrcweir else 195cdf0e10cSrcweir return sal_False; 196cdf0e10cSrcweir } 197cdf0e10cSrcweir 198cdf0e10cSrcweir static void Execute( ANY& aEventData, const css::document::DocumentEvent& aTrigger, SfxObjectShell* pDoc ) 199cdf0e10cSrcweir { 200cdf0e10cSrcweir SEQUENCE < PROPERTYVALUE > aProperties; 201cdf0e10cSrcweir if ( aEventData >>= aProperties ) 202cdf0e10cSrcweir { 203cdf0e10cSrcweir OUSTRING aPrefix = OUSTRING( RTL_CONSTASCII_USTRINGPARAM( MACRO_PRFIX ) ); 204cdf0e10cSrcweir OUSTRING aType; 205cdf0e10cSrcweir OUSTRING aScript; 206cdf0e10cSrcweir OUSTRING aLibrary; 207cdf0e10cSrcweir OUSTRING aMacroName; 208cdf0e10cSrcweir 209cdf0e10cSrcweir sal_Int32 nCount = aProperties.getLength(); 210cdf0e10cSrcweir 211cdf0e10cSrcweir if ( !nCount ) 212cdf0e10cSrcweir return; 213cdf0e10cSrcweir 214cdf0e10cSrcweir sal_Int32 nIndex = 0; 215cdf0e10cSrcweir while ( nIndex < nCount ) 216cdf0e10cSrcweir { 217cdf0e10cSrcweir if ( aProperties[ nIndex ].Name.compareToAscii( PROP_EVENT_TYPE ) == 0 ) 218cdf0e10cSrcweir aProperties[ nIndex ].Value >>= aType; 219cdf0e10cSrcweir else if ( aProperties[ nIndex ].Name.compareToAscii( PROP_SCRIPT ) == 0 ) 220cdf0e10cSrcweir aProperties[ nIndex ].Value >>= aScript; 221cdf0e10cSrcweir else if ( aProperties[ nIndex ].Name.compareToAscii( PROP_LIBRARY ) == 0 ) 222cdf0e10cSrcweir aProperties[ nIndex ].Value >>= aLibrary; 223cdf0e10cSrcweir else if ( aProperties[ nIndex ].Name.compareToAscii( PROP_MACRO_NAME ) == 0 ) 224cdf0e10cSrcweir aProperties[ nIndex ].Value >>= aMacroName; 225cdf0e10cSrcweir else { 226cdf0e10cSrcweir DBG_ERROR("Unknown property value!"); 227cdf0e10cSrcweir } 228cdf0e10cSrcweir nIndex += 1; 229cdf0e10cSrcweir } 230cdf0e10cSrcweir 231cdf0e10cSrcweir if ( aType.compareToAscii( STAR_BASIC ) == 0 && aScript.getLength() ) 232cdf0e10cSrcweir { 233cdf0e10cSrcweir com::sun::star::uno::Any aAny; 234cdf0e10cSrcweir SfxMacroLoader::loadMacro( aScript, aAny, pDoc ); 235cdf0e10cSrcweir } 236cdf0e10cSrcweir else if ( aType.compareToAscii( "Service" ) == 0 || 237cdf0e10cSrcweir aType.compareToAscii( "Script" ) == 0 ) 238cdf0e10cSrcweir { 239cdf0e10cSrcweir if ( aScript.getLength() ) 240cdf0e10cSrcweir { 241cdf0e10cSrcweir SfxViewFrame* pView = pDoc ? 242cdf0e10cSrcweir SfxViewFrame::GetFirst( pDoc ) : 243cdf0e10cSrcweir SfxViewFrame::Current(); 244cdf0e10cSrcweir 245cdf0e10cSrcweir ::com::sun::star::uno::Reference 246cdf0e10cSrcweir < ::com::sun::star::util::XURLTransformer > xTrans( 247cdf0e10cSrcweir ::comphelper::getProcessServiceFactory()->createInstance( 248cdf0e10cSrcweir rtl::OUString::createFromAscii( 249cdf0e10cSrcweir "com.sun.star.util.URLTransformer" ) ), 250cdf0e10cSrcweir UNO_QUERY ); 251cdf0e10cSrcweir 252cdf0e10cSrcweir ::com::sun::star::util::URL aURL; 253cdf0e10cSrcweir aURL.Complete = aScript; 254cdf0e10cSrcweir xTrans->parseStrict( aURL ); 255cdf0e10cSrcweir 256cdf0e10cSrcweir ::com::sun::star::uno::Reference 257cdf0e10cSrcweir < ::com::sun::star::frame::XDispatchProvider > xProv; 258cdf0e10cSrcweir 259cdf0e10cSrcweir if ( pView != NULL ) 260cdf0e10cSrcweir { 261cdf0e10cSrcweir xProv = ::com::sun::star::uno::Reference 262cdf0e10cSrcweir < ::com::sun::star::frame::XDispatchProvider > ( 263cdf0e10cSrcweir pView->GetFrame().GetFrameInterface(), UNO_QUERY ); 264cdf0e10cSrcweir } 265cdf0e10cSrcweir else 266cdf0e10cSrcweir { 267cdf0e10cSrcweir xProv = ::com::sun::star::uno::Reference 268cdf0e10cSrcweir < ::com::sun::star::frame::XDispatchProvider > ( 269cdf0e10cSrcweir ::comphelper::getProcessServiceFactory()->createInstance( 270cdf0e10cSrcweir rtl::OUString::createFromAscii( 271cdf0e10cSrcweir "com.sun.star.frame.Desktop" ) ), 272cdf0e10cSrcweir UNO_QUERY ); 273cdf0e10cSrcweir } 274cdf0e10cSrcweir 275cdf0e10cSrcweir ::com::sun::star::uno::Reference < ::com::sun::star::frame::XDispatch > xDisp; 276cdf0e10cSrcweir if ( xProv.is() ) 277cdf0e10cSrcweir xDisp = xProv->queryDispatch( aURL, ::rtl::OUString(), 0 ); 278cdf0e10cSrcweir 279cdf0e10cSrcweir if ( xDisp.is() ) 280cdf0e10cSrcweir { 281cdf0e10cSrcweir //::com::sun::star::uno::Sequence < ::com::sun::star::beans::PropertyValue > aArgs(1); 282cdf0e10cSrcweir //aArgs[0].Name = rtl::OUString::createFromAscii("Referer"); 283cdf0e10cSrcweir //aArs[0].Value <<= ::rtl::OUString( pDoc->GetMedium()->GetName() ); 284cdf0e10cSrcweir //xDisp->dispatch( aURL, aArgs ); 285cdf0e10cSrcweir 286cdf0e10cSrcweir css::beans::PropertyValue aEventParam; 287cdf0e10cSrcweir aEventParam.Value <<= aTrigger; 288cdf0e10cSrcweir css::uno::Sequence< css::beans::PropertyValue > aDispatchArgs( &aEventParam, 1 ); 289cdf0e10cSrcweir xDisp->dispatch( aURL, aDispatchArgs ); 290cdf0e10cSrcweir } 291cdf0e10cSrcweir } 292cdf0e10cSrcweir } 293cdf0e10cSrcweir else if ( aType.getLength() == 0 ) 294cdf0e10cSrcweir { 295cdf0e10cSrcweir // Empty type means no active binding for the event. Just ignore do nothing. 296cdf0e10cSrcweir } 297cdf0e10cSrcweir else 298cdf0e10cSrcweir { 299cdf0e10cSrcweir DBG_ERRORFILE( "notifyEvent(): Unsupported event type" ); 300cdf0e10cSrcweir } 301cdf0e10cSrcweir } 302cdf0e10cSrcweir } 303cdf0e10cSrcweir 304cdf0e10cSrcweir //-------------------------------------------------------------------------------------------------------- 305cdf0e10cSrcweir // --- ::document::XEventListener --- 306cdf0e10cSrcweir //-------------------------------------------------------------------------------------------------------- 307cdf0e10cSrcweir void SAL_CALL SfxEvents_Impl::notifyEvent( const DOCEVENTOBJECT& aEvent ) throw( RUNTIMEEXCEPTION ) 308cdf0e10cSrcweir { 309cdf0e10cSrcweir ::osl::ClearableMutexGuard aGuard( maMutex ); 310cdf0e10cSrcweir 311cdf0e10cSrcweir // get the event name, find the coresponding data, execute the data 312cdf0e10cSrcweir 313cdf0e10cSrcweir OUSTRING aName = aEvent.EventName; 314cdf0e10cSrcweir long nCount = maEventNames.getLength(); 315cdf0e10cSrcweir long nIndex = 0; 316cdf0e10cSrcweir sal_Bool bFound = sal_False; 317cdf0e10cSrcweir 318cdf0e10cSrcweir while ( !bFound && ( nIndex < nCount ) ) 319cdf0e10cSrcweir { 320cdf0e10cSrcweir if ( maEventNames[nIndex] == aName ) 321cdf0e10cSrcweir bFound = sal_True; 322cdf0e10cSrcweir else 323cdf0e10cSrcweir nIndex += 1; 324cdf0e10cSrcweir } 325cdf0e10cSrcweir 326cdf0e10cSrcweir if ( !bFound ) 327cdf0e10cSrcweir return; 328cdf0e10cSrcweir 329cdf0e10cSrcweir ANY aEventData = maEventData[ nIndex ]; 330cdf0e10cSrcweir aGuard.clear(); 331cdf0e10cSrcweir Execute( aEventData, css::document::DocumentEvent(aEvent.Source, aEvent.EventName, NULL, css::uno::Any()), mpObjShell ); 332cdf0e10cSrcweir } 333cdf0e10cSrcweir 334cdf0e10cSrcweir //-------------------------------------------------------------------------------------------------------- 335cdf0e10cSrcweir // --- ::lang::XEventListener --- 336cdf0e10cSrcweir //-------------------------------------------------------------------------------------------------------- 337cdf0e10cSrcweir void SAL_CALL SfxEvents_Impl::disposing( const EVENTOBJECT& /*Source*/ ) throw( RUNTIMEEXCEPTION ) 338cdf0e10cSrcweir { 339cdf0e10cSrcweir ::osl::MutexGuard aGuard( maMutex ); 340cdf0e10cSrcweir 341cdf0e10cSrcweir if ( mxBroadcaster.is() ) 342cdf0e10cSrcweir { 343cdf0e10cSrcweir mxBroadcaster->removeEventListener( this ); 344cdf0e10cSrcweir mxBroadcaster = NULL; 345cdf0e10cSrcweir } 346cdf0e10cSrcweir } 347cdf0e10cSrcweir 348cdf0e10cSrcweir //-------------------------------------------------------------------------------------------------------- 349cdf0e10cSrcweir // 350cdf0e10cSrcweir //-------------------------------------------------------------------------------------------------------- 351cdf0e10cSrcweir SfxEvents_Impl::SfxEvents_Impl( SfxObjectShell* pShell, 352cdf0e10cSrcweir REFERENCE< XEVENTBROADCASTER > xBroadcaster ) 353cdf0e10cSrcweir { 354cdf0e10cSrcweir // get the list of supported events and store it 355cdf0e10cSrcweir if ( pShell ) 356cdf0e10cSrcweir maEventNames = pShell->GetEventNames(); 357cdf0e10cSrcweir else 358cdf0e10cSrcweir maEventNames = GlobalEventConfig().getElementNames(); 359cdf0e10cSrcweir 360cdf0e10cSrcweir maEventData = SEQUENCE < ANY > ( maEventNames.getLength() ); 361cdf0e10cSrcweir 362cdf0e10cSrcweir mpObjShell = pShell; 363cdf0e10cSrcweir mxBroadcaster = xBroadcaster; 364cdf0e10cSrcweir 365cdf0e10cSrcweir if ( mxBroadcaster.is() ) 366cdf0e10cSrcweir mxBroadcaster->addEventListener( this ); 367cdf0e10cSrcweir } 368cdf0e10cSrcweir 369cdf0e10cSrcweir //-------------------------------------------------------------------------------------------------------- 370cdf0e10cSrcweir SfxEvents_Impl::~SfxEvents_Impl() 371cdf0e10cSrcweir { 372cdf0e10cSrcweir } 373cdf0e10cSrcweir 374cdf0e10cSrcweir //-------------------------------------------------------------------------------------------------------- 375cdf0e10cSrcweir SvxMacro* SfxEvents_Impl::ConvertToMacro( const ANY& rElement, SfxObjectShell* pObjShell, sal_Bool bNormalizeMacro ) 376cdf0e10cSrcweir { 377cdf0e10cSrcweir SvxMacro* pMacro = NULL; 378cdf0e10cSrcweir SEQUENCE < PROPERTYVALUE > aProperties; 379cdf0e10cSrcweir ANY aAny; 380cdf0e10cSrcweir if ( bNormalizeMacro ) 381cdf0e10cSrcweir NormalizeMacro( rElement, aAny, pObjShell ); 382cdf0e10cSrcweir else 383cdf0e10cSrcweir aAny = rElement; 384cdf0e10cSrcweir 385cdf0e10cSrcweir if ( aAny >>= aProperties ) 386cdf0e10cSrcweir { 387cdf0e10cSrcweir OUSTRING aType; 388cdf0e10cSrcweir OUSTRING aScriptURL; 389cdf0e10cSrcweir OUSTRING aLibrary; 390cdf0e10cSrcweir OUSTRING aMacroName; 391cdf0e10cSrcweir 392cdf0e10cSrcweir long nCount = aProperties.getLength(); 393cdf0e10cSrcweir long nIndex = 0; 394cdf0e10cSrcweir 395cdf0e10cSrcweir if ( !nCount ) 396cdf0e10cSrcweir return pMacro; 397cdf0e10cSrcweir 398cdf0e10cSrcweir while ( nIndex < nCount ) 399cdf0e10cSrcweir { 400cdf0e10cSrcweir if ( aProperties[ nIndex ].Name.compareToAscii( PROP_EVENT_TYPE ) == 0 ) 401cdf0e10cSrcweir aProperties[ nIndex ].Value >>= aType; 402cdf0e10cSrcweir else if ( aProperties[ nIndex ].Name.compareToAscii( PROP_SCRIPT ) == 0 ) 403cdf0e10cSrcweir aProperties[ nIndex ].Value >>= aScriptURL; 404cdf0e10cSrcweir else if ( aProperties[ nIndex ].Name.compareToAscii( PROP_LIBRARY ) == 0 ) 405cdf0e10cSrcweir aProperties[ nIndex ].Value >>= aLibrary; 406cdf0e10cSrcweir else if ( aProperties[ nIndex ].Name.compareToAscii( PROP_MACRO_NAME ) == 0 ) 407cdf0e10cSrcweir aProperties[ nIndex ].Value >>= aMacroName; 408cdf0e10cSrcweir else { 409cdf0e10cSrcweir DBG_ERROR("Unknown propery value!"); 410cdf0e10cSrcweir } 411cdf0e10cSrcweir nIndex += 1; 412cdf0e10cSrcweir } 413cdf0e10cSrcweir 414cdf0e10cSrcweir // Get the type 415cdf0e10cSrcweir ScriptType eType( STARBASIC ); 416cdf0e10cSrcweir if ( aType.compareToAscii( STAR_BASIC ) == COMPARE_EQUAL ) 417cdf0e10cSrcweir eType = STARBASIC; 418cdf0e10cSrcweir else if ( aType.compareToAscii( "Script" ) == COMPARE_EQUAL && aScriptURL.getLength() ) 419cdf0e10cSrcweir eType = EXTENDED_STYPE; 420cdf0e10cSrcweir else if ( aType.compareToAscii( SVX_MACRO_LANGUAGE_JAVASCRIPT ) == COMPARE_EQUAL ) 421cdf0e10cSrcweir eType = JAVASCRIPT; 422cdf0e10cSrcweir else { 423cdf0e10cSrcweir DBG_ERRORFILE( "ConvertToMacro: Unknown macro type" ); 424cdf0e10cSrcweir } 425cdf0e10cSrcweir 426cdf0e10cSrcweir if ( aMacroName.getLength() ) 427cdf0e10cSrcweir { 428cdf0e10cSrcweir if ( aLibrary.compareToAscii("application") == 0 ) 429cdf0e10cSrcweir aLibrary = SFX_APP()->GetName(); 430cdf0e10cSrcweir else 431cdf0e10cSrcweir aLibrary = ::rtl::OUString(); 432cdf0e10cSrcweir pMacro = new SvxMacro( aMacroName, aLibrary, eType ); 433cdf0e10cSrcweir } 434cdf0e10cSrcweir else if ( eType == EXTENDED_STYPE ) 435cdf0e10cSrcweir pMacro = new SvxMacro( aScriptURL, aType ); 436cdf0e10cSrcweir } 437cdf0e10cSrcweir 438cdf0e10cSrcweir return pMacro; 439cdf0e10cSrcweir } 440cdf0e10cSrcweir 441cdf0e10cSrcweir void SfxEvents_Impl::NormalizeMacro( const ANY& rEvent, ANY& rRet, SfxObjectShell* pDoc ) 442cdf0e10cSrcweir { 443cdf0e10cSrcweir const ::comphelper::NamedValueCollection aEventDescriptor( rEvent ); 444cdf0e10cSrcweir ::comphelper::NamedValueCollection aEventDescriptorOut; 445cdf0e10cSrcweir 446cdf0e10cSrcweir NormalizeMacro( aEventDescriptor, aEventDescriptorOut, pDoc ); 447cdf0e10cSrcweir 448cdf0e10cSrcweir rRet <<= aEventDescriptorOut.getPropertyValues(); 449cdf0e10cSrcweir } 450cdf0e10cSrcweir 451cdf0e10cSrcweir void SfxEvents_Impl::NormalizeMacro( const ::comphelper::NamedValueCollection& i_eventDescriptor, 452cdf0e10cSrcweir ::comphelper::NamedValueCollection& o_normalizedDescriptor, SfxObjectShell* i_document ) 453cdf0e10cSrcweir { 454cdf0e10cSrcweir SfxObjectShell* pDoc = i_document; 455cdf0e10cSrcweir if ( !pDoc ) 456cdf0e10cSrcweir pDoc = SfxObjectShell::Current(); 457cdf0e10cSrcweir 458cdf0e10cSrcweir ::rtl::OUString aType = i_eventDescriptor.getOrDefault( PROP_EVENT_TYPE, ::rtl::OUString() ); 459cdf0e10cSrcweir ::rtl::OUString aScript = i_eventDescriptor.getOrDefault( PROP_SCRIPT, ::rtl::OUString() ); 460cdf0e10cSrcweir ::rtl::OUString aLibrary = i_eventDescriptor.getOrDefault( PROP_LIBRARY, ::rtl::OUString() ); 461cdf0e10cSrcweir ::rtl::OUString aMacroName = i_eventDescriptor.getOrDefault( PROP_MACRO_NAME, ::rtl::OUString() ); 462cdf0e10cSrcweir 463cdf0e10cSrcweir if ( aType.getLength() ) 464cdf0e10cSrcweir o_normalizedDescriptor.put( PROP_EVENT_TYPE, aType ); 465cdf0e10cSrcweir if ( aScript.getLength() ) 466cdf0e10cSrcweir o_normalizedDescriptor.put( PROP_SCRIPT, aScript ); 467cdf0e10cSrcweir 468cdf0e10cSrcweir if ( aType.compareToAscii( STAR_BASIC ) == 0 ) 469cdf0e10cSrcweir { 470cdf0e10cSrcweir if ( aScript.getLength() ) 471cdf0e10cSrcweir { 472cdf0e10cSrcweir if ( !aMacroName.getLength() || !aLibrary.getLength() ) 473cdf0e10cSrcweir { 474cdf0e10cSrcweir sal_Int32 nHashPos = aScript.indexOf( '/', 8 ); 475cdf0e10cSrcweir sal_Int32 nArgsPos = aScript.indexOf( '(' ); 476cdf0e10cSrcweir if ( ( nHashPos != STRING_NOTFOUND ) && ( nHashPos < nArgsPos ) ) 477cdf0e10cSrcweir { 478cdf0e10cSrcweir OUSTRING aBasMgrName( INetURLObject::decode( aScript.copy( 8, nHashPos-8 ), INET_HEX_ESCAPE, INetURLObject::DECODE_WITH_CHARSET ) ); 479cdf0e10cSrcweir if ( aBasMgrName.compareToAscii(".") == 0 ) 480cdf0e10cSrcweir aLibrary = pDoc->GetTitle(); 481cdf0e10cSrcweir /* 482cdf0e10cSrcweir else if ( aBasMgrName.getLength() ) 483cdf0e10cSrcweir aLibrary = aBasMgrName; 484cdf0e10cSrcweir */ 485cdf0e10cSrcweir else 486cdf0e10cSrcweir aLibrary = SFX_APP()->GetName(); 487cdf0e10cSrcweir 488cdf0e10cSrcweir // Get the macro name 489cdf0e10cSrcweir aMacroName = aScript.copy( nHashPos+1, nArgsPos - nHashPos - 1 ); 490cdf0e10cSrcweir } 491cdf0e10cSrcweir else 492cdf0e10cSrcweir { 493cdf0e10cSrcweir DBG_ERRORFILE( "ConvertToMacro: Unknown macro url format" ); 494cdf0e10cSrcweir } 495cdf0e10cSrcweir } 496cdf0e10cSrcweir } 497cdf0e10cSrcweir else if ( aMacroName.getLength() ) 498cdf0e10cSrcweir { 499cdf0e10cSrcweir aScript = OUSTRING( RTL_CONSTASCII_USTRINGPARAM( MACRO_PRFIX ) ); 500cdf0e10cSrcweir if ( aLibrary.compareTo( SFX_APP()->GetName() ) != 0 && aLibrary.compareToAscii("StarDesktop") != 0 && aLibrary.compareToAscii("application") != 0 ) 501cdf0e10cSrcweir aScript += String('.'); 502cdf0e10cSrcweir 503cdf0e10cSrcweir aScript += String('/'); 504cdf0e10cSrcweir aScript += aMacroName; 505cdf0e10cSrcweir aScript += OUSTRING( RTL_CONSTASCII_USTRINGPARAM( MACRO_POSTFIX ) ); 506cdf0e10cSrcweir } 507cdf0e10cSrcweir else 508cdf0e10cSrcweir // wrong properties 509cdf0e10cSrcweir return; 510cdf0e10cSrcweir 511cdf0e10cSrcweir if ( aLibrary.compareToAscii("document") != 0 ) 512cdf0e10cSrcweir { 513cdf0e10cSrcweir if ( !aLibrary.getLength() || (pDoc && ( String(aLibrary) == pDoc->GetTitle( SFX_TITLE_APINAME ) || String(aLibrary) == pDoc->GetTitle() )) ) 514cdf0e10cSrcweir aLibrary = String::CreateFromAscii("document"); 515cdf0e10cSrcweir else 516cdf0e10cSrcweir aLibrary = String::CreateFromAscii("application"); 517cdf0e10cSrcweir } 518cdf0e10cSrcweir 519cdf0e10cSrcweir o_normalizedDescriptor.put( PROP_SCRIPT, aScript ); 520cdf0e10cSrcweir o_normalizedDescriptor.put( PROP_LIBRARY, aLibrary ); 521cdf0e10cSrcweir o_normalizedDescriptor.put( PROP_MACRO_NAME, aMacroName ); 522cdf0e10cSrcweir } 523cdf0e10cSrcweir } 524cdf0e10cSrcweir 525cdf0e10cSrcweir ModelCollectionEnumeration::ModelCollectionEnumeration(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR) 526cdf0e10cSrcweir : ModelCollectionMutexBase( ) 527cdf0e10cSrcweir , m_xSMGR (xSMGR ) 528cdf0e10cSrcweir , m_pEnumerationIt (m_lModels.begin()) 529cdf0e10cSrcweir { 530cdf0e10cSrcweir } 531cdf0e10cSrcweir 532cdf0e10cSrcweir ModelCollectionEnumeration::~ModelCollectionEnumeration() 533cdf0e10cSrcweir { 534cdf0e10cSrcweir } 535cdf0e10cSrcweir 536cdf0e10cSrcweir void ModelCollectionEnumeration::setModelList(const TModelList& rList) 537cdf0e10cSrcweir { 538cdf0e10cSrcweir // SAFE -> 539cdf0e10cSrcweir ::osl::ResettableMutexGuard aLock(m_aLock); 540cdf0e10cSrcweir m_lModels = rList; 541cdf0e10cSrcweir m_pEnumerationIt = m_lModels.begin(); 542cdf0e10cSrcweir aLock.clear(); 543cdf0e10cSrcweir // <- SAFE 544cdf0e10cSrcweir } 545cdf0e10cSrcweir 546cdf0e10cSrcweir sal_Bool SAL_CALL ModelCollectionEnumeration::hasMoreElements() 547cdf0e10cSrcweir throw(css::uno::RuntimeException) 548cdf0e10cSrcweir { 549cdf0e10cSrcweir // SAFE -> 550cdf0e10cSrcweir ::osl::ResettableMutexGuard aLock(m_aLock); 551cdf0e10cSrcweir return (m_pEnumerationIt != m_lModels.end()); 552cdf0e10cSrcweir // <- SAFE 553cdf0e10cSrcweir } 554cdf0e10cSrcweir 555cdf0e10cSrcweir css::uno::Any SAL_CALL ModelCollectionEnumeration::nextElement() 556cdf0e10cSrcweir throw(css::container::NoSuchElementException, 557cdf0e10cSrcweir css::lang::WrappedTargetException , 558cdf0e10cSrcweir css::uno::RuntimeException ) 559cdf0e10cSrcweir { 560cdf0e10cSrcweir // SAFE -> 561cdf0e10cSrcweir ::osl::ResettableMutexGuard aLock(m_aLock); 562cdf0e10cSrcweir if (m_pEnumerationIt == m_lModels.end()) 563cdf0e10cSrcweir throw css::container::NoSuchElementException( 564cdf0e10cSrcweir ::rtl::OUString::createFromAscii("End of model enumeration reached."), 565cdf0e10cSrcweir static_cast< css::container::XEnumeration* >(this)); 566cdf0e10cSrcweir css::uno::Reference< css::frame::XModel > xModel(*m_pEnumerationIt, UNO_QUERY); 567cdf0e10cSrcweir ++m_pEnumerationIt; 568cdf0e10cSrcweir aLock.clear(); 569cdf0e10cSrcweir // <- SAFE 570cdf0e10cSrcweir 571cdf0e10cSrcweir return css::uno::makeAny(xModel); 572cdf0e10cSrcweir } 573cdf0e10cSrcweir 574cdf0e10cSrcweir SFX_IMPL_XSERVICEINFO( SfxGlobalEvents_Impl, "com.sun.star.frame.GlobalEventBroadcaster", "com.sun.star.comp.sfx2.GlobalEventBroadcaster" ) 575cdf0e10cSrcweir SFX_IMPL_ONEINSTANCEFACTORY( SfxGlobalEvents_Impl ); 576cdf0e10cSrcweir 577cdf0e10cSrcweir //----------------------------------------------------------------------------- 578cdf0e10cSrcweir SfxGlobalEvents_Impl::SfxGlobalEvents_Impl( const com::sun::star::uno::Reference < ::com::sun::star::lang::XMultiServiceFactory >& xSMGR) 579cdf0e10cSrcweir : ModelCollectionMutexBase( ) 580cdf0e10cSrcweir , m_xSMGR (xSMGR ) 581cdf0e10cSrcweir , m_aLegacyListeners (m_aLock) 582cdf0e10cSrcweir , m_aDocumentListeners (m_aLock) 583cdf0e10cSrcweir , pImp (0 ) 584cdf0e10cSrcweir { 585cdf0e10cSrcweir m_refCount++; 586cdf0e10cSrcweir SFX_APP(); 587cdf0e10cSrcweir pImp = new GlobalEventConfig(); 588cdf0e10cSrcweir m_xEvents = pImp; 589cdf0e10cSrcweir m_xJobExecutorListener = css::uno::Reference< css::document::XEventListener >( 590cdf0e10cSrcweir xSMGR->createInstance(::rtl::OUString::createFromAscii("com.sun.star.task.JobExecutor")), 591cdf0e10cSrcweir UNO_QUERY); 592cdf0e10cSrcweir m_refCount--; 593cdf0e10cSrcweir } 594cdf0e10cSrcweir 595cdf0e10cSrcweir //----------------------------------------------------------------------------- 596cdf0e10cSrcweir SfxGlobalEvents_Impl::~SfxGlobalEvents_Impl() 597cdf0e10cSrcweir { 598cdf0e10cSrcweir } 599cdf0e10cSrcweir 600cdf0e10cSrcweir //----------------------------------------------------------------------------- 601cdf0e10cSrcweir css::uno::Reference< css::container::XNameReplace > SAL_CALL SfxGlobalEvents_Impl::getEvents() 602cdf0e10cSrcweir throw(css::uno::RuntimeException) 603cdf0e10cSrcweir { 604cdf0e10cSrcweir // SAFE -> 605cdf0e10cSrcweir ::osl::ResettableMutexGuard aLock(m_aLock); 606cdf0e10cSrcweir return m_xEvents; 607cdf0e10cSrcweir // <- SAFE 608cdf0e10cSrcweir } 609cdf0e10cSrcweir 610cdf0e10cSrcweir //----------------------------------------------------------------------------- 611cdf0e10cSrcweir void SAL_CALL SfxGlobalEvents_Impl::addEventListener(const css::uno::Reference< css::document::XEventListener >& xListener) 612cdf0e10cSrcweir throw(css::uno::RuntimeException) 613cdf0e10cSrcweir { 614cdf0e10cSrcweir // container is threadsafe 615cdf0e10cSrcweir m_aLegacyListeners.addInterface(xListener); 616cdf0e10cSrcweir } 617cdf0e10cSrcweir 618cdf0e10cSrcweir //----------------------------------------------------------------------------- 619cdf0e10cSrcweir void SAL_CALL SfxGlobalEvents_Impl::removeEventListener(const css::uno::Reference< css::document::XEventListener >& xListener) 620cdf0e10cSrcweir throw(css::uno::RuntimeException) 621cdf0e10cSrcweir { 622cdf0e10cSrcweir // container is threadsafe 623cdf0e10cSrcweir m_aLegacyListeners.removeInterface(xListener); 624cdf0e10cSrcweir } 625cdf0e10cSrcweir 626cdf0e10cSrcweir //----------------------------------------------------------------------------- 627cdf0e10cSrcweir void SAL_CALL SfxGlobalEvents_Impl::addDocumentEventListener( const css::uno::Reference< css::document::XDocumentEventListener >& _Listener ) 628cdf0e10cSrcweir throw(css::uno::RuntimeException) 629cdf0e10cSrcweir { 630cdf0e10cSrcweir m_aDocumentListeners.addInterface( _Listener ); 631cdf0e10cSrcweir } 632cdf0e10cSrcweir 633cdf0e10cSrcweir //----------------------------------------------------------------------------- 634cdf0e10cSrcweir void SAL_CALL SfxGlobalEvents_Impl::removeDocumentEventListener( const css::uno::Reference< css::document::XDocumentEventListener >& _Listener ) 635cdf0e10cSrcweir throw(css::uno::RuntimeException) 636cdf0e10cSrcweir { 637cdf0e10cSrcweir m_aDocumentListeners.removeInterface( _Listener ); 638cdf0e10cSrcweir } 639cdf0e10cSrcweir 640cdf0e10cSrcweir //----------------------------------------------------------------------------- 641cdf0e10cSrcweir void SAL_CALL SfxGlobalEvents_Impl::notifyDocumentEvent( const ::rtl::OUString& /*_EventName*/, 642cdf0e10cSrcweir const css::uno::Reference< css::frame::XController2 >& /*_ViewController*/, const css::uno::Any& /*_Supplement*/ ) 643cdf0e10cSrcweir throw (css::lang::IllegalArgumentException, css::lang::NoSupportException, css::uno::RuntimeException) 644cdf0e10cSrcweir { 645*86e1cf34SPedro Giffuni // we're a multiplexer only, no chance to generate artificial events here 646cdf0e10cSrcweir throw css::lang::NoSupportException(::rtl::OUString(), *this); 647cdf0e10cSrcweir } 648cdf0e10cSrcweir 649cdf0e10cSrcweir //----------------------------------------------------------------------------- 650cdf0e10cSrcweir void SAL_CALL SfxGlobalEvents_Impl::notifyEvent(const css::document::EventObject& aEvent) 651cdf0e10cSrcweir throw(css::uno::RuntimeException) 652cdf0e10cSrcweir { 653cdf0e10cSrcweir css::document::DocumentEvent aDocEvent(aEvent.Source, aEvent.EventName, NULL, css::uno::Any()); 654cdf0e10cSrcweir implts_notifyJobExecution(aEvent); 655cdf0e10cSrcweir implts_checkAndExecuteEventBindings(aDocEvent); 656cdf0e10cSrcweir implts_notifyListener(aDocEvent); 657cdf0e10cSrcweir } 658cdf0e10cSrcweir 659cdf0e10cSrcweir //----------------------------------------------------------------------------- 660cdf0e10cSrcweir void SAL_CALL SfxGlobalEvents_Impl::documentEventOccured( const ::css::document::DocumentEvent& _Event ) 661cdf0e10cSrcweir throw (::css::uno::RuntimeException) 662cdf0e10cSrcweir { 663cdf0e10cSrcweir implts_notifyJobExecution(css::document::EventObject(_Event.Source, _Event.EventName)); 664cdf0e10cSrcweir implts_checkAndExecuteEventBindings(_Event); 665cdf0e10cSrcweir implts_notifyListener(_Event); 666cdf0e10cSrcweir } 667cdf0e10cSrcweir 668cdf0e10cSrcweir //----------------------------------------------------------------------------- 669cdf0e10cSrcweir void SAL_CALL SfxGlobalEvents_Impl::disposing(const css::lang::EventObject& aEvent) 670cdf0e10cSrcweir throw(css::uno::RuntimeException) 671cdf0e10cSrcweir { 672cdf0e10cSrcweir css::uno::Reference< css::frame::XModel > xDoc(aEvent.Source, UNO_QUERY); 673cdf0e10cSrcweir 674cdf0e10cSrcweir // SAFE -> 675cdf0e10cSrcweir ::osl::ResettableMutexGuard aLock(m_aLock); 676cdf0e10cSrcweir TModelList::iterator pIt = impl_searchDoc(xDoc); 677cdf0e10cSrcweir if (pIt != m_lModels.end()) 678cdf0e10cSrcweir m_lModels.erase(pIt); 679cdf0e10cSrcweir aLock.clear(); 680cdf0e10cSrcweir // <- SAFE 681cdf0e10cSrcweir } 682cdf0e10cSrcweir 683cdf0e10cSrcweir //----------------------------------------------------------------------------- 684cdf0e10cSrcweir sal_Bool SAL_CALL SfxGlobalEvents_Impl::has(const css::uno::Any& aElement) 685cdf0e10cSrcweir throw (css::uno::RuntimeException) 686cdf0e10cSrcweir { 687cdf0e10cSrcweir css::uno::Reference< css::frame::XModel > xDoc; 688cdf0e10cSrcweir aElement >>= xDoc; 689cdf0e10cSrcweir 690cdf0e10cSrcweir sal_Bool bHas = sal_False; 691cdf0e10cSrcweir 692cdf0e10cSrcweir // SAFE -> 693cdf0e10cSrcweir ::osl::ResettableMutexGuard aLock(m_aLock); 694cdf0e10cSrcweir TModelList::iterator pIt = impl_searchDoc(xDoc); 695cdf0e10cSrcweir if (pIt != m_lModels.end()) 696cdf0e10cSrcweir bHas = sal_True; 697cdf0e10cSrcweir aLock.clear(); 698cdf0e10cSrcweir // <- SAFE 699cdf0e10cSrcweir 700cdf0e10cSrcweir return bHas; 701cdf0e10cSrcweir } 702cdf0e10cSrcweir 703cdf0e10cSrcweir //----------------------------------------------------------------------------- 704cdf0e10cSrcweir void SAL_CALL SfxGlobalEvents_Impl::insert( const css::uno::Any& aElement ) 705cdf0e10cSrcweir throw (css::lang::IllegalArgumentException , 706cdf0e10cSrcweir css::container::ElementExistException, 707cdf0e10cSrcweir css::uno::RuntimeException ) 708cdf0e10cSrcweir { 709cdf0e10cSrcweir css::uno::Reference< css::frame::XModel > xDoc; 710cdf0e10cSrcweir aElement >>= xDoc; 711cdf0e10cSrcweir if (!xDoc.is()) 712cdf0e10cSrcweir throw css::lang::IllegalArgumentException( 713*86e1cf34SPedro Giffuni ::rtl::OUString::createFromAscii("Can't locate at least the model parameter."), 714cdf0e10cSrcweir static_cast< css::container::XSet* >(this), 715cdf0e10cSrcweir 0); 716cdf0e10cSrcweir 717cdf0e10cSrcweir // SAFE -> 718cdf0e10cSrcweir ::osl::ResettableMutexGuard aLock(m_aLock); 719cdf0e10cSrcweir TModelList::iterator pIt = impl_searchDoc(xDoc); 720cdf0e10cSrcweir if (pIt != m_lModels.end()) 721cdf0e10cSrcweir throw css::container::ElementExistException( 722cdf0e10cSrcweir ::rtl::OUString(), 723cdf0e10cSrcweir static_cast< css::container::XSet* >(this)); 724cdf0e10cSrcweir m_lModels.push_back(xDoc); 725cdf0e10cSrcweir aLock.clear(); 726cdf0e10cSrcweir // <- SAFE 727cdf0e10cSrcweir 728cdf0e10cSrcweir css::uno::Reference< css::document::XDocumentEventBroadcaster > xDocBroadcaster(xDoc, UNO_QUERY ); 729cdf0e10cSrcweir if (xDocBroadcaster.is()) 730cdf0e10cSrcweir xDocBroadcaster->addDocumentEventListener(this); 731cdf0e10cSrcweir else 732cdf0e10cSrcweir { 733cdf0e10cSrcweir // try the "legacy version" of XDocumentEventBroadcaster, which is XEventBroadcaster 734cdf0e10cSrcweir css::uno::Reference< css::document::XEventBroadcaster > xBroadcaster(xDoc, UNO_QUERY); 735cdf0e10cSrcweir if (xBroadcaster.is()) 736cdf0e10cSrcweir xBroadcaster->addEventListener(static_cast< css::document::XEventListener* >(this)); 737cdf0e10cSrcweir } 738cdf0e10cSrcweir } 739cdf0e10cSrcweir 740cdf0e10cSrcweir //----------------------------------------------------------------------------- 741cdf0e10cSrcweir void SAL_CALL SfxGlobalEvents_Impl::remove( const css::uno::Any& aElement ) 742cdf0e10cSrcweir throw (css::lang::IllegalArgumentException , 743cdf0e10cSrcweir css::container::NoSuchElementException, 744cdf0e10cSrcweir css::uno::RuntimeException ) 745cdf0e10cSrcweir { 746cdf0e10cSrcweir css::uno::Reference< css::frame::XModel > xDoc; 747cdf0e10cSrcweir aElement >>= xDoc; 748cdf0e10cSrcweir if (!xDoc.is()) 749cdf0e10cSrcweir throw css::lang::IllegalArgumentException( 750*86e1cf34SPedro Giffuni ::rtl::OUString::createFromAscii("Can't locate at least the model parameter."), 751cdf0e10cSrcweir static_cast< css::container::XSet* >(this), 752cdf0e10cSrcweir 0); 753cdf0e10cSrcweir 754cdf0e10cSrcweir // SAFE -> 755cdf0e10cSrcweir ::osl::ResettableMutexGuard aLock(m_aLock); 756cdf0e10cSrcweir TModelList::iterator pIt = impl_searchDoc(xDoc); 757cdf0e10cSrcweir if (pIt == m_lModels.end()) 758cdf0e10cSrcweir throw css::container::NoSuchElementException( 759cdf0e10cSrcweir ::rtl::OUString(), 760cdf0e10cSrcweir static_cast< css::container::XSet* >(this)); 761cdf0e10cSrcweir m_lModels.erase(pIt); 762cdf0e10cSrcweir aLock.clear(); 763cdf0e10cSrcweir // <- SAFE 764cdf0e10cSrcweir 765cdf0e10cSrcweir css::uno::Reference< css::document::XDocumentEventBroadcaster > xDocBroadcaster(xDoc, UNO_QUERY ); 766cdf0e10cSrcweir if (xDocBroadcaster.is()) 767cdf0e10cSrcweir xDocBroadcaster->removeDocumentEventListener(this); 768cdf0e10cSrcweir else 769cdf0e10cSrcweir { 770cdf0e10cSrcweir // try the "legacy version" of XDocumentEventBroadcaster, which is XEventBroadcaster 771cdf0e10cSrcweir css::uno::Reference< css::document::XEventBroadcaster > xBroadcaster(xDoc, UNO_QUERY); 772cdf0e10cSrcweir if (xBroadcaster.is()) 773cdf0e10cSrcweir xBroadcaster->removeEventListener(static_cast< css::document::XEventListener* >(this)); 774cdf0e10cSrcweir } 775cdf0e10cSrcweir } 776cdf0e10cSrcweir 777cdf0e10cSrcweir //----------------------------------------------------------------------------- 778cdf0e10cSrcweir css::uno::Reference< css::container::XEnumeration > SAL_CALL SfxGlobalEvents_Impl::createEnumeration() 779cdf0e10cSrcweir throw (css::uno::RuntimeException) 780cdf0e10cSrcweir { 781cdf0e10cSrcweir // SAFE -> 782cdf0e10cSrcweir ::osl::ResettableMutexGuard aLock(m_aLock); 783cdf0e10cSrcweir ModelCollectionEnumeration* pEnum = new ModelCollectionEnumeration(m_xSMGR); 784cdf0e10cSrcweir pEnum->setModelList(m_lModels); 785cdf0e10cSrcweir css::uno::Reference< css::container::XEnumeration > xEnum( 786cdf0e10cSrcweir static_cast< css::container::XEnumeration* >(pEnum), 787cdf0e10cSrcweir UNO_QUERY); 788cdf0e10cSrcweir aLock.clear(); 789cdf0e10cSrcweir // <- SAFE 790cdf0e10cSrcweir 791cdf0e10cSrcweir return xEnum; 792cdf0e10cSrcweir } 793cdf0e10cSrcweir 794cdf0e10cSrcweir //----------------------------------------------------------------------------- 795cdf0e10cSrcweir css::uno::Type SAL_CALL SfxGlobalEvents_Impl::getElementType() 796cdf0e10cSrcweir throw (css::uno::RuntimeException) 797cdf0e10cSrcweir { 798cdf0e10cSrcweir return ::getCppuType(static_cast< css::uno::Reference< css::frame::XModel >* >(NULL)); 799cdf0e10cSrcweir } 800cdf0e10cSrcweir 801cdf0e10cSrcweir //----------------------------------------------------------------------------- 802cdf0e10cSrcweir sal_Bool SAL_CALL SfxGlobalEvents_Impl::hasElements() 803cdf0e10cSrcweir throw (css::uno::RuntimeException) 804cdf0e10cSrcweir { 805cdf0e10cSrcweir // SAFE -> 806cdf0e10cSrcweir ::osl::ResettableMutexGuard aLock(m_aLock); 807cdf0e10cSrcweir return (m_lModels.size()>0); 808cdf0e10cSrcweir // <- SAFE 809cdf0e10cSrcweir } 810cdf0e10cSrcweir 811cdf0e10cSrcweir //----------------------------------------------------------------------------- 812cdf0e10cSrcweir void SfxGlobalEvents_Impl::implts_notifyJobExecution(const css::document::EventObject& aEvent) 813cdf0e10cSrcweir { 814cdf0e10cSrcweir try 815cdf0e10cSrcweir { 816cdf0e10cSrcweir // SAFE -> 817cdf0e10cSrcweir ::osl::ResettableMutexGuard aLock(m_aLock); 818cdf0e10cSrcweir css::uno::Reference< css::document::XEventListener > xJobExecutor(m_xJobExecutorListener); 819cdf0e10cSrcweir aLock.clear(); 820cdf0e10cSrcweir // <- SAFE 821cdf0e10cSrcweir if (xJobExecutor.is()) 822cdf0e10cSrcweir xJobExecutor->notifyEvent(aEvent); 823cdf0e10cSrcweir } 824cdf0e10cSrcweir catch(const css::uno::RuntimeException& exRun) 825cdf0e10cSrcweir { throw exRun; } 826cdf0e10cSrcweir catch(const css::uno::Exception&) 827cdf0e10cSrcweir {} 828cdf0e10cSrcweir } 829cdf0e10cSrcweir 830cdf0e10cSrcweir //----------------------------------------------------------------------------- 831cdf0e10cSrcweir void SfxGlobalEvents_Impl::implts_checkAndExecuteEventBindings(const css::document::DocumentEvent& aEvent) 832cdf0e10cSrcweir { 833cdf0e10cSrcweir try 834cdf0e10cSrcweir { 835cdf0e10cSrcweir // SAFE -> 836cdf0e10cSrcweir ::osl::ResettableMutexGuard aLock(m_aLock); 837cdf0e10cSrcweir css::uno::Reference< css::container::XNameReplace > xEvents = m_xEvents; 838cdf0e10cSrcweir aLock.clear(); 839cdf0e10cSrcweir // <- SAFE 840cdf0e10cSrcweir 841cdf0e10cSrcweir css::uno::Any aAny; 842cdf0e10cSrcweir if ( xEvents.is() && xEvents->hasByName( aEvent.EventName ) ) 843cdf0e10cSrcweir aAny = xEvents->getByName(aEvent.EventName); 844cdf0e10cSrcweir Execute(aAny, aEvent, 0); 845cdf0e10cSrcweir } 846cdf0e10cSrcweir catch ( css::uno::RuntimeException const & ) 847cdf0e10cSrcweir { 848cdf0e10cSrcweir throw; 849cdf0e10cSrcweir } 850cdf0e10cSrcweir catch ( css::uno::Exception const & ) 851cdf0e10cSrcweir { 852cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 853cdf0e10cSrcweir } 854cdf0e10cSrcweir } 855cdf0e10cSrcweir 856cdf0e10cSrcweir //----------------------------------------------------------------------------- 857cdf0e10cSrcweir void SfxGlobalEvents_Impl::implts_notifyListener(const css::document::DocumentEvent& aEvent) 858cdf0e10cSrcweir { 859cdf0e10cSrcweir // containers are threadsafe 860cdf0e10cSrcweir css::document::EventObject aLegacyEvent(aEvent.Source, aEvent.EventName); 861cdf0e10cSrcweir m_aLegacyListeners.notifyEach( &css::document::XEventListener::notifyEvent, aLegacyEvent ); 862cdf0e10cSrcweir 863cdf0e10cSrcweir m_aDocumentListeners.notifyEach( &css::document::XDocumentEventListener::documentEventOccured, aEvent ); 864cdf0e10cSrcweir } 865cdf0e10cSrcweir 866cdf0e10cSrcweir //----------------------------------------------------------------------------- 867cdf0e10cSrcweir // not threadsafe ... must be locked from outside! 868cdf0e10cSrcweir TModelList::iterator SfxGlobalEvents_Impl::impl_searchDoc(const css::uno::Reference< css::frame::XModel >& xModel) 869cdf0e10cSrcweir { 870cdf0e10cSrcweir if (!xModel.is()) 871cdf0e10cSrcweir return m_lModels.end(); 872cdf0e10cSrcweir 873cdf0e10cSrcweir TModelList::iterator pIt; 874cdf0e10cSrcweir for ( pIt = m_lModels.begin(); 875cdf0e10cSrcweir pIt != m_lModels.end() ; 876cdf0e10cSrcweir ++pIt ) 877cdf0e10cSrcweir { 878cdf0e10cSrcweir css::uno::Reference< css::frame::XModel > xContainerDoc(*pIt, UNO_QUERY); 879cdf0e10cSrcweir if (xContainerDoc == xModel) 880cdf0e10cSrcweir break; 881cdf0e10cSrcweir } 882cdf0e10cSrcweir 883cdf0e10cSrcweir return pIt; 884cdf0e10cSrcweir } 885cdf0e10cSrcweir 886