LCOV - code coverage report
Current view: top level - libreoffice/unotools/source/config - eventcfg.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 102 179 57.0 %
Date: 2012-12-27 Functions: 16 25 64.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : 
      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>
      28             : 
      29             : #include <boost/unordered_map.hpp>
      30             : #include <rtl/ustrbuf.hxx>
      31             : 
      32             : #include <itemholder1.hxx>
      33             : 
      34             : #include <algorithm>
      35             : 
      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;
      42             : 
      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"))
      47             : 
      48             : const char* pEventAsciiNames[] =
      49             : {
      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"
      78             : };
      79             : 
      80          39 : GlobalEventConfig_Impl::GlobalEventConfig_Impl()
      81          39 :     :   ConfigItem( ROOTNODE_EVENTS, CONFIG_MODE_IMMEDIATE_UPDATE )
      82             : {
      83             :     // the supported event names
      84          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_STARTAPP] ) );
      85          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_CLOSEAPP] ) );
      86          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_DOCCREATED] ) );
      87          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_CREATEDOC] ) );
      88          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_LOADFINISHED] ) );
      89          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_OPENDOC] ) );
      90          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_PREPARECLOSEDOC] ) );
      91          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_CLOSEDOC] ) );
      92          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEDOC] ) );
      93          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEDOCDONE] ) );
      94          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEDOCFAILED] ) );
      95          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEASDOC] ) );
      96          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEASDOCDONE] ) );
      97          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVEASDOCFAILED] ) );
      98          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVETODOC] ) );
      99          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVETODOCDONE] ) );
     100          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_SAVETODOCFAILED] ) );
     101          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_ACTIVATEDOC] ) );
     102          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_DEACTIVATEDOC] ) );
     103          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_PRINTDOC] ) );
     104          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_VIEWCREATED] ) );
     105          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_PREPARECLOSEVIEW] ) );
     106          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_CLOSEVIEW] ) );
     107          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_MODIFYCHANGED] ) );
     108          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_TITLECHANGED] ) );
     109          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_VISAREACHANGED] ) );
     110          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_MODECHANGED] ) );
     111          39 :     m_supportedEvents.push_back(::rtl::OUString::createFromAscii( pEventAsciiNames[STR_EVENT_STORAGECHANGED] ) );
     112             : 
     113          39 :     initBindingInfo();
     114             : 
     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          39 :     Sequence< OUString > aNotifySeq( 1 );
     119          39 :     aNotifySeq[0] = OUString( RTL_CONSTASCII_USTRINGPARAM( "Events" ));
     120          39 :     EnableNotification( aNotifySeq, sal_True );
     121          39 : }
     122             : 
     123             : //*****************************************************************************************************************
     124             : //  destructor
     125             : //*****************************************************************************************************************
     126          69 : GlobalEventConfig_Impl::~GlobalEventConfig_Impl()
     127             : {
     128             :     // We must save our current values .. if user forget it!
     129          23 :     if( IsModified() == sal_True )
     130             :     {
     131           0 :         Commit();
     132             :     }
     133          46 : }
     134             : 
     135        5475 : ::rtl::OUString GlobalEventConfig_Impl::GetEventName( sal_Int32 nIndex )
     136             : {
     137        5475 :     if ( nIndex < (sal_Int32) m_supportedEvents.size() )
     138        5475 :         return m_supportedEvents[nIndex];
     139             :     else
     140           0 :         return rtl::OUString();
     141             : }
     142             : 
     143             : //*****************************************************************************************************************
     144             : //  public method
     145             : //*****************************************************************************************************************
     146           0 : void GlobalEventConfig_Impl::Notify( const Sequence< OUString >& )
     147             : {
     148           0 :     MutexGuard aGuard( GlobalEventConfig::GetOwnStaticMutex() );
     149             : 
     150           0 :     initBindingInfo();
     151             : 
     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           0 :     for (FrameVector::const_iterator pIt  = m_lFrames.begin();
     155           0 :                                         pIt != m_lFrames.end()  ;
     156             :                                       ++pIt                     )
     157             :     {
     158           0 :         ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame > xFrame(pIt->get(), ::com::sun::star::uno::UNO_QUERY);
     159           0 :         if (xFrame.is())
     160           0 :             xFrame->contextChanged();
     161           0 :     }
     162           0 : }
     163             : 
     164             : //*****************************************************************************************************************
     165             : //  public method
     166             : //*****************************************************************************************************************
     167           0 : void GlobalEventConfig_Impl::Commit()
     168             : {
     169             :     //DF need to check it this is correct??
     170             :     OSL_TRACE("In GlobalEventConfig_Impl::Commit");
     171           0 :     EventBindingHash::const_iterator it = m_eventBindingHash.begin();
     172           0 :     EventBindingHash::const_iterator it_end = m_eventBindingHash.end();
     173             :     // clear the existing nodes
     174           0 :     ClearNodeSet( SETNODE_BINDINGS );
     175           0 :     Sequence< beans::PropertyValue > seqValues( 1 );
     176           0 :     OUString sNode;
     177           0 :     static const OUString sPrefix(SETNODE_BINDINGS + PATHDELIMITER + OUString(RTL_CONSTASCII_USTRINGPARAM("BindingType['")));
     178           0 :     static const OUString sPostfix(OUString(RTL_CONSTASCII_USTRINGPARAM("']")) + PATHDELIMITER + PROPERTYNAME_BINDINGURL);
     179             :     //step through the list of events
     180           0 :     for(int i=0;it!=it_end;++it,++i)
     181             :     {
     182             :         //no point in writing out empty bindings!
     183           0 :         if(it->second.isEmpty() )
     184           0 :             continue;
     185           0 :         sNode = sPrefix + it->first + sPostfix;
     186             :         OSL_TRACE("writing binding for: %s",::rtl::OUStringToOString(sNode , RTL_TEXTENCODING_ASCII_US ).pData->buffer);
     187           0 :         seqValues[ 0 ].Name = sNode;
     188           0 :         seqValues[ 0 ].Value <<= it->second;
     189             :         //write the data to the registry
     190           0 :         SetSetProperties(SETNODE_BINDINGS,seqValues);
     191           0 :     }
     192           0 : }
     193             : 
     194             : //*****************************************************************************************************************
     195             : //  private method
     196             : //*****************************************************************************************************************
     197          39 : void GlobalEventConfig_Impl::initBindingInfo()
     198             : {
     199             :     // Get ALL names of current existing list items in configuration!
     200          39 :     Sequence< OUString > lEventNames      = GetNodeNames( SETNODE_BINDINGS, utl::CONFIG_NAME_LOCAL_PATH );
     201             : 
     202          39 :     OUString aSetNode( SETNODE_BINDINGS );
     203          39 :     aSetNode += PATHDELIMITER;
     204             : 
     205          39 :     OUString aCommandKey( PATHDELIMITER );
     206          39 :     aCommandKey += PROPERTYNAME_BINDINGURL;
     207             : 
     208             :     // Expand all keys
     209          39 :     Sequence< OUString > lMacros(1);
     210          39 :     for (sal_Int32 i=0; i<lEventNames.getLength(); ++i )
     211             :     {
     212           0 :         OUStringBuffer aBuffer( 32 );
     213           0 :         aBuffer.append( aSetNode );
     214           0 :         aBuffer.append( lEventNames[i] );
     215           0 :         aBuffer.append( aCommandKey );
     216           0 :         lMacros[0] = aBuffer.makeStringAndClear();
     217             :         OSL_TRACE("reading binding for: %s",::rtl::OUStringToOString(lMacros[0] , RTL_TEXTENCODING_ASCII_US ).pData->buffer);
     218           0 :         Sequence< Any > lValues = GetProperties( lMacros );
     219           0 :         OUString sMacroURL;
     220           0 :         if( lValues.getLength() > 0 )
     221             :         {
     222           0 :             lValues[0] >>= sMacroURL;
     223           0 :             sal_Int32 startIndex = lEventNames[i].indexOf('\'');
     224           0 :             sal_Int32 endIndex =  lEventNames[i].lastIndexOf('\'');
     225           0 :             if( startIndex >=0 && endIndex > 0 )
     226             :             {
     227           0 :                 startIndex++;
     228           0 :                 OUString eventName = lEventNames[i].copy(startIndex,endIndex-startIndex);
     229           0 :                 m_eventBindingHash[ eventName ] = sMacroURL;
     230             :             }
     231             :         }
     232          39 :     }
     233          39 : }
     234             : 
     235           0 : void SAL_CALL GlobalEventConfig_Impl::replaceByName( const OUString& aName, const Any& aElement ) throw (lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, RuntimeException)
     236             : {
     237           0 :     Sequence< beans::PropertyValue > props;
     238             :     //DF should we prepopulate the hash with a list of valid event Names?
     239           0 :     if( sal_False == ( aElement >>= props ) )
     240             :     {
     241             :         throw lang::IllegalArgumentException( OUString(),
     242           0 :                 Reference< XInterface > (), 2);
     243             :     }
     244           0 :     OUString macroURL;
     245           0 :     sal_Int32 nPropCount = props.getLength();
     246           0 :     for( sal_Int32 index = 0 ; index < nPropCount ; ++index )
     247             :     {
     248           0 :         if ( props[ index ].Name.compareToAscii( "Script" ) == 0 )
     249           0 :             props[ index ].Value >>= macroURL;
     250             :     }
     251           0 :     m_eventBindingHash[ aName ] = macroURL;
     252           0 :     SetModified();
     253           0 : }
     254             : 
     255        4718 : Any SAL_CALL GlobalEventConfig_Impl::getByName( const OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, RuntimeException)
     256             : {
     257        4718 :     Any aRet;
     258        4718 :     Sequence< beans::PropertyValue > props(2);
     259        4718 :     props[0].Name = OUString(RTL_CONSTASCII_USTRINGPARAM("EventType"));
     260        4718 :     props[0].Value <<= OUString(RTL_CONSTASCII_USTRINGPARAM("Script"));
     261        4718 :     props[1].Name = OUString(RTL_CONSTASCII_USTRINGPARAM("Script"));
     262        4718 :     EventBindingHash::const_iterator it = m_eventBindingHash.find( aName );
     263        4718 :     if( it != m_eventBindingHash.end() )
     264             :     {
     265           0 :         props[1].Value <<= it->second;
     266             :     }
     267             :     else
     268             :     {
     269             :         // not yet accessed - is it a supported name?
     270             :         SupportedEventsVector::const_iterator pos = ::std::find(
     271        4718 :             m_supportedEvents.begin(), m_supportedEvents.end(), aName );
     272        4718 :         if ( pos == m_supportedEvents.end() )
     273           0 :             throw container::NoSuchElementException( aName, NULL );
     274             : 
     275        4718 :         props[1].Value <<= OUString();
     276             :     }
     277        4718 :     aRet <<= props;
     278        4718 :     return aRet;
     279             : }
     280             : 
     281           2 : Sequence< OUString > SAL_CALL GlobalEventConfig_Impl::getElementNames(  ) throw (RuntimeException)
     282             : {
     283           2 :     const ::rtl::OUString* pRet = m_supportedEvents.empty() ? NULL : &m_supportedEvents[0];
     284           2 :     return uno::Sequence< ::rtl::OUString >(pRet, m_supportedEvents.size());
     285             : }
     286             : 
     287        4809 : sal_Bool SAL_CALL GlobalEventConfig_Impl::hasByName( const OUString& aName ) throw (RuntimeException)
     288             : {
     289        4809 :     if ( m_eventBindingHash.find( aName ) != m_eventBindingHash.end() )
     290           0 :         return sal_True;
     291             : 
     292             :     // never accessed before - is it supported in general?
     293             :     SupportedEventsVector::const_iterator pos = ::std::find(
     294        4809 :         m_supportedEvents.begin(), m_supportedEvents.end(), aName );
     295        4809 :     if ( pos != m_supportedEvents.end() )
     296        4718 :         return sal_True;
     297             : 
     298          91 :     return sal_False;
     299             : }
     300             : 
     301           0 : Type SAL_CALL GlobalEventConfig_Impl::getElementType(  ) throw (RuntimeException)
     302             : {
     303             :     //DF definitly not sure about this??
     304           0 :     return ::getCppuType((const Sequence<beans::PropertyValue>*)0);
     305             : }
     306             : 
     307           0 : sal_Bool SAL_CALL GlobalEventConfig_Impl::hasElements(  ) throw (RuntimeException)
     308             : {
     309           0 :     return ( m_eventBindingHash.empty() );
     310             : }
     311             : 
     312             : // and now the wrapper
     313             : 
     314             : 
     315             : //initialize static member
     316             : GlobalEventConfig_Impl*     GlobalEventConfig::m_pImpl = NULL  ;
     317             : sal_Int32                   GlobalEventConfig::m_nRefCount      = 0     ;
     318             : 
     319        5495 : GlobalEventConfig::GlobalEventConfig()
     320             : {
     321             :     // Global access, must be guarded (multithreading!).
     322        5495 :     MutexGuard aGuard( GetOwnStaticMutex() );
     323             :     // Increase our refcount ...
     324        5495 :     ++m_nRefCount;
     325             :     // ... and initialize our data container only if it not already exist!
     326        5495 :     if( m_pImpl == NULL )
     327             :     {
     328          39 :         m_pImpl = new GlobalEventConfig_Impl;
     329          39 :         ItemHolder1::holdConfigItem(E_EVENTCFG);
     330        5495 :     }
     331        5495 : }
     332             : 
     333       10960 : GlobalEventConfig::~GlobalEventConfig()
     334             : {
     335             :     // Global access, must be guarded (multithreading!)
     336        5479 :     MutexGuard aGuard( GetOwnStaticMutex() );
     337             :     // Decrease our refcount.
     338        5479 :     --m_nRefCount;
     339             :     // If last instance was deleted ...
     340             :     // we must destroy our static data container!
     341        5479 :     if( m_nRefCount <= 0 )
     342             :     {
     343          23 :         delete m_pImpl;
     344          23 :         m_pImpl = NULL;
     345        5479 :     }
     346        5481 : }
     347             : 
     348           0 : Reference< container::XNameReplace > SAL_CALL GlobalEventConfig::getEvents() throw (::com::sun::star::uno::RuntimeException)
     349             : {
     350           0 :     MutexGuard aGuard( GetOwnStaticMutex() );
     351           0 :     Reference< container::XNameReplace > ret(this);
     352           0 :     return ret;
     353             : }
     354             : 
     355           0 : void SAL_CALL GlobalEventConfig::replaceByName( const OUString& aName, const Any& aElement ) throw (lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, RuntimeException)
     356             : {
     357           0 :     MutexGuard aGuard( GetOwnStaticMutex() );
     358           0 :     m_pImpl->replaceByName( aName, aElement );
     359           0 : }
     360        4718 : Any SAL_CALL GlobalEventConfig::getByName( const OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, RuntimeException)
     361             : {
     362        4718 :     MutexGuard aGuard( GetOwnStaticMutex() );
     363        4718 :     return m_pImpl->getByName( aName );
     364             : }
     365           2 : Sequence< OUString > SAL_CALL GlobalEventConfig::getElementNames(  ) throw (RuntimeException)
     366             : {
     367           2 :     MutexGuard aGuard( GetOwnStaticMutex() );
     368           2 :     return m_pImpl->getElementNames( );
     369             : }
     370        4809 : sal_Bool SAL_CALL GlobalEventConfig::hasByName( const OUString& aName ) throw (RuntimeException)
     371             : {
     372        4809 :     MutexGuard aGuard( GetOwnStaticMutex() );
     373        4809 :     return m_pImpl->hasByName( aName );
     374             : }
     375           0 : Type SAL_CALL GlobalEventConfig::getElementType(  ) throw (RuntimeException)
     376             : {
     377           0 :     MutexGuard aGuard( GetOwnStaticMutex() );
     378           0 :     return m_pImpl->getElementType( );
     379             : }
     380           0 : sal_Bool SAL_CALL GlobalEventConfig::hasElements(  ) throw (RuntimeException)
     381             : {
     382           0 :     MutexGuard aGuard( GetOwnStaticMutex() );
     383           0 :     return m_pImpl->hasElements( );
     384             : }
     385             : 
     386             : namespace
     387             : {
     388             :     class theGlobalEventConfigMutex : public rtl::Static<osl::Mutex, theGlobalEventConfigMutex>{};
     389             : }
     390             : 
     391       20503 : Mutex& GlobalEventConfig::GetOwnStaticMutex()
     392             : {
     393       20503 :     return theGlobalEventConfigMutex::get();
     394             : }
     395             : 
     396        5475 : ::rtl::OUString GlobalEventConfig::GetEventName( sal_Int32 nIndex )
     397             : {
     398        5475 :     return GlobalEventConfig().m_pImpl->GetEventName( nIndex );
     399             : }
     400             : 
     401             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10