LCOV - code coverage report
Current view: top level - unotools/source/config - eventcfg.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 73 146 50.0 %
Date: 2015-06-13 12:38:46 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             : #include <unotools/eventcfg.hxx>
      21             : #include <unotools/configmgr.hxx>
      22             : #include <unotools/configitem.hxx>
      23             : #include <com/sun/star/uno/Any.hxx>
      24             : #include <com/sun/star/uno/Sequence.hxx>
      25             : #include <com/sun/star/beans/PropertyValue.hpp>
      26             : #include <cppuhelper/weakref.hxx>
      27             : #include <o3tl/enumarray.hxx>
      28             : #include <o3tl/enumrange.hxx>
      29             : 
      30             : #include <rtl/ustrbuf.hxx>
      31             : #include <osl/diagnose.h>
      32             : 
      33             : #include "itemholder1.hxx"
      34             : 
      35             : #include <algorithm>
      36             : 
      37             : using namespace ::std;
      38             : using namespace ::utl;
      39             : using namespace ::osl;
      40             : using namespace ::com::sun::star::uno;
      41             : using namespace ::com::sun::star;
      42             : 
      43             : static const char ROOTNODE_EVENTS[] = "Office.Events/ApplicationEvents";
      44             : #define PATHDELIMITER "/"
      45             : #define SETNODE_BINDINGS "Bindings"
      46             : #define PROPERTYNAME_BINDINGURL "BindingURL"
      47             : 
      48             : static o3tl::enumarray<GlobalEventId, 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             : typedef std::unordered_map< OUString, OUString, OUStringHash, std::equal_to< OUString > > EventBindingHash;
      81             : typedef std::vector< css::uno::WeakReference< css::frame::XFrame > > FrameVector;
      82             : typedef o3tl::enumarray< GlobalEventId, OUString > SupportedEventsVector;
      83             : 
      84             : class GlobalEventConfig_Impl : public utl::ConfigItem
      85             : {
      86             : private:
      87             :     EventBindingHash m_eventBindingHash;
      88             :     FrameVector m_lFrames;
      89             :     SupportedEventsVector m_supportedEvents;
      90             : 
      91             :     void initBindingInfo();
      92             : 
      93             :     virtual void ImplCommit() SAL_OVERRIDE;
      94             : 
      95             : public:
      96             :     GlobalEventConfig_Impl( );
      97             :     virtual ~GlobalEventConfig_Impl( );
      98             : 
      99             :     void            Notify( const com::sun::star::uno::Sequence<OUString>& aPropertyNames) SAL_OVERRIDE;
     100             : 
     101             :     void SAL_CALL replaceByName( const OUString& aName, const ::com::sun::star::uno::Any& aElement ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
     102             :     ::com::sun::star::uno::Any SAL_CALL getByName( const OUString& aName ) throw (::com::sun::star::container::NoSuchElementException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
     103             :     ::com::sun::star::uno::Sequence< OUString > SAL_CALL getElementNames(  ) throw (::com::sun::star::uno::RuntimeException);
     104             :     bool SAL_CALL hasByName( const OUString& aName ) throw (::com::sun::star::uno::RuntimeException);
     105             :     static ::com::sun::star::uno::Type SAL_CALL getElementType(  ) throw (::com::sun::star::uno::RuntimeException);
     106             :     bool SAL_CALL hasElements(  ) throw (::com::sun::star::uno::RuntimeException);
     107             :     OUString GetEventName( GlobalEventId nID );
     108             : };
     109             : 
     110             : 
     111         264 : GlobalEventConfig_Impl::GlobalEventConfig_Impl()
     112         264 :     :   ConfigItem( ROOTNODE_EVENTS, ConfigItemMode::ImmediateUpdate )
     113             : {
     114             :     // the supported event names
     115        7656 :     for (GlobalEventId id : o3tl::enumrange<GlobalEventId>())
     116        7392 :         m_supportedEvents[id] = OUString::createFromAscii( pEventAsciiNames[id] );
     117             : 
     118         264 :     initBindingInfo();
     119             : 
     120             : /*TODO: Not used in the moment! see Notify() ...
     121             :     // Enable notification mechanism of our baseclass.
     122             :     // We need it to get information about changes outside these class on our used configuration keys! */
     123         264 :     Sequence< OUString > aNotifySeq( 1 );
     124         264 :     aNotifySeq[0] = "Events";
     125         264 :     EnableNotification( aNotifySeq, true );
     126         264 : }
     127             : 
     128             : //  destructor
     129             : 
     130         506 : GlobalEventConfig_Impl::~GlobalEventConfig_Impl()
     131             : {
     132             :     assert(!IsModified()); // should have been committed
     133         506 : }
     134             : 
     135       52422 : OUString GlobalEventConfig_Impl::GetEventName( GlobalEventId nIndex )
     136             : {
     137       52422 :     return m_supportedEvents[nIndex];
     138             : }
     139             : 
     140             : //  public method
     141             : 
     142           0 : void GlobalEventConfig_Impl::Notify( const Sequence< OUString >& )
     143             : {
     144           0 :     MutexGuard aGuard( GlobalEventConfig::GetOwnStaticMutex() );
     145             : 
     146           0 :     initBindingInfo();
     147             : 
     148             :     // dont forget to update all existing frames and her might cached dispatch objects!
     149             :     // But look for already killed frames. We hold weak references instead of hard ones ...
     150           0 :     for (FrameVector::const_iterator pIt  = m_lFrames.begin();
     151           0 :                                         pIt != m_lFrames.end();
     152             :                                       ++pIt                     )
     153             :     {
     154           0 :         ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame > xFrame(pIt->get(), ::com::sun::star::uno::UNO_QUERY);
     155           0 :         if (xFrame.is())
     156           0 :             xFrame->contextChanged();
     157           0 :     }
     158           0 : }
     159             : 
     160             : //  public method
     161             : 
     162           0 : void GlobalEventConfig_Impl::ImplCommit()
     163             : {
     164             :     //DF need to check it this is correct??
     165             :     OSL_TRACE("In GlobalEventConfig_Impl::ImplCommit");
     166           0 :     EventBindingHash::const_iterator it = m_eventBindingHash.begin();
     167           0 :     EventBindingHash::const_iterator it_end = m_eventBindingHash.end();
     168             :     // clear the existing nodes
     169           0 :     ClearNodeSet( SETNODE_BINDINGS );
     170           0 :     Sequence< beans::PropertyValue > seqValues( 1 );
     171           0 :     OUString sNode;
     172             :     static const char sPrefix[] = SETNODE_BINDINGS PATHDELIMITER "BindingType['";
     173             :     static const char sPostfix[] = "']" PATHDELIMITER PROPERTYNAME_BINDINGURL;
     174             :     //step through the list of events
     175           0 :     for(int i=0;it!=it_end;++it,++i)
     176             :     {
     177             :         //no point in writing out empty bindings!
     178           0 :         if(it->second.isEmpty() )
     179           0 :             continue;
     180           0 :         sNode = sPrefix + it->first + sPostfix;
     181             :         OSL_TRACE("writing binding for: %s",OUStringToOString(sNode , RTL_TEXTENCODING_ASCII_US ).pData->buffer);
     182           0 :         seqValues[ 0 ].Name = sNode;
     183           0 :         seqValues[ 0 ].Value <<= it->second;
     184             :         //write the data to the registry
     185           0 :         SetSetProperties(SETNODE_BINDINGS,seqValues);
     186           0 :     }
     187           0 : }
     188             : 
     189             : //  private method
     190             : 
     191         264 : void GlobalEventConfig_Impl::initBindingInfo()
     192             : {
     193             :     // Get ALL names of current existing list items in configuration!
     194         264 :     Sequence< OUString > lEventNames      = GetNodeNames( SETNODE_BINDINGS, utl::CONFIG_NAME_LOCAL_PATH );
     195             : 
     196         528 :     OUString aSetNode( SETNODE_BINDINGS );
     197         264 :     aSetNode += PATHDELIMITER;
     198             : 
     199         528 :     OUString aCommandKey( PATHDELIMITER );
     200         264 :     aCommandKey += PROPERTYNAME_BINDINGURL;
     201             : 
     202             :     // Expand all keys
     203         528 :     Sequence< OUString > lMacros(1);
     204         264 :     for (sal_Int32 i=0; i<lEventNames.getLength(); ++i )
     205             :     {
     206           0 :         OUStringBuffer aBuffer( 32 );
     207           0 :         aBuffer.append( aSetNode );
     208           0 :         aBuffer.append( lEventNames[i] );
     209           0 :         aBuffer.append( aCommandKey );
     210           0 :         lMacros[0] = aBuffer.makeStringAndClear();
     211             :         OSL_TRACE("reading binding for: %s",OUStringToOString(lMacros[0] , RTL_TEXTENCODING_ASCII_US ).pData->buffer);
     212           0 :         Sequence< Any > lValues = GetProperties( lMacros );
     213           0 :         OUString sMacroURL;
     214           0 :         if( lValues.getLength() > 0 )
     215             :         {
     216           0 :             lValues[0] >>= sMacroURL;
     217           0 :             sal_Int32 startIndex = lEventNames[i].indexOf('\'');
     218           0 :             sal_Int32 endIndex =  lEventNames[i].lastIndexOf('\'');
     219           0 :             if( startIndex >=0 && endIndex > 0 )
     220             :             {
     221           0 :                 startIndex++;
     222           0 :                 OUString eventName = lEventNames[i].copy(startIndex,endIndex-startIndex);
     223           0 :                 m_eventBindingHash[ eventName ] = sMacroURL;
     224             :             }
     225             :         }
     226         264 :     }
     227         264 : }
     228             : 
     229           0 : void SAL_CALL GlobalEventConfig_Impl::replaceByName( const OUString& aName, const Any& aElement ) throw (lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, RuntimeException)
     230             : {
     231           0 :     Sequence< beans::PropertyValue > props;
     232             :     //DF should we prepopulate the hash with a list of valid event Names?
     233           0 :     if( !( aElement >>= props ) )
     234             :     {
     235             :         throw lang::IllegalArgumentException( OUString(),
     236           0 :                 Reference< XInterface > (), 2);
     237             :     }
     238           0 :     OUString macroURL;
     239           0 :     sal_Int32 nPropCount = props.getLength();
     240           0 :     for( sal_Int32 index = 0; index < nPropCount; ++index )
     241             :     {
     242           0 :         if ( props[ index ].Name == "Script" )
     243           0 :             props[ index ].Value >>= macroURL;
     244             :     }
     245           0 :     m_eventBindingHash[ aName ] = macroURL;
     246           0 :     SetModified();
     247           0 : }
     248             : 
     249       39780 : Any SAL_CALL GlobalEventConfig_Impl::getByName( const OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, RuntimeException)
     250             : {
     251       39780 :     Any aRet;
     252       79560 :     Sequence< beans::PropertyValue > props(2);
     253       39780 :     props[0].Name = "EventType";
     254       39780 :     props[0].Value <<= OUString("Script");
     255       39780 :     props[1].Name = "Script";
     256       39780 :     EventBindingHash::const_iterator it = m_eventBindingHash.find( aName );
     257       39780 :     if( it != m_eventBindingHash.end() )
     258             :     {
     259           0 :         props[1].Value <<= it->second;
     260             :     }
     261             :     else
     262             :     {
     263             :         // not yet accessed - is it a supported name?
     264             :         SupportedEventsVector::iterator pos = ::std::find(
     265       39780 :             m_supportedEvents.begin(), m_supportedEvents.end(), aName );
     266       39780 :         if ( pos == m_supportedEvents.end() )
     267           0 :             throw container::NoSuchElementException( aName );
     268             : 
     269       39780 :         props[1].Value <<= OUString();
     270             :     }
     271       39780 :     aRet <<= props;
     272       79560 :     return aRet;
     273             : }
     274             : 
     275          24 : Sequence< OUString > SAL_CALL GlobalEventConfig_Impl::getElementNames(  ) throw (RuntimeException)
     276             : {
     277          24 :     return uno::Sequence< OUString >(m_supportedEvents.data(), SupportedEventsVector::size());
     278             : }
     279             : 
     280       45667 : bool SAL_CALL GlobalEventConfig_Impl::hasByName( const OUString& aName ) throw (RuntimeException)
     281             : {
     282       45667 :     if ( m_eventBindingHash.find( aName ) != m_eventBindingHash.end() )
     283           0 :         return true;
     284             : 
     285             :     // never accessed before - is it supported in general?
     286             :     SupportedEventsVector::iterator pos = ::std::find(
     287       45667 :         m_supportedEvents.begin(), m_supportedEvents.end(), aName );
     288       45667 :     if ( pos != m_supportedEvents.end() )
     289       39780 :         return true;
     290             : 
     291        5887 :     return false;
     292             : }
     293             : 
     294           0 : Type SAL_CALL GlobalEventConfig_Impl::getElementType(  ) throw (RuntimeException)
     295             : {
     296             :     //DF definitely not sure about this??
     297           0 :     return cppu::UnoType<Sequence<beans::PropertyValue>>::get();
     298             : }
     299             : 
     300           0 : bool SAL_CALL GlobalEventConfig_Impl::hasElements(  ) throw (RuntimeException)
     301             : {
     302           0 :     return ( m_eventBindingHash.empty() );
     303             : }
     304             : 
     305             : // and now the wrapper
     306             : 
     307             : //initialize static member
     308             : GlobalEventConfig_Impl*     GlobalEventConfig::m_pImpl = NULL;
     309             : sal_Int32                   GlobalEventConfig::m_nRefCount      = 0;
     310             : 
     311       52642 : GlobalEventConfig::GlobalEventConfig()
     312             : {
     313             :     // Global access, must be guarded (multithreading!).
     314       52642 :     MutexGuard aGuard( GetOwnStaticMutex() );
     315             :     // Increase our refcount ...
     316       52642 :     ++m_nRefCount;
     317             :     // ... and initialize our data container only if it not already exist!
     318       52642 :     if( m_pImpl == NULL )
     319             :     {
     320         264 :         m_pImpl = new GlobalEventConfig_Impl;
     321         264 :         ItemHolder1::holdConfigItem(E_EVENTCFG);
     322       52642 :     }
     323       52642 : }
     324             : 
     325      105447 : GlobalEventConfig::~GlobalEventConfig()
     326             : {
     327             :     // Global access, must be guarded (multithreading!)
     328       52631 :     MutexGuard aGuard( GetOwnStaticMutex() );
     329             :     // Decrease our refcount.
     330       52631 :     --m_nRefCount;
     331             :     // If last instance was deleted ...
     332             :     // we must destroy our static data container!
     333       52631 :     if( m_nRefCount <= 0 )
     334             :     {
     335         253 :         delete m_pImpl;
     336         253 :         m_pImpl = NULL;
     337       52631 :     }
     338       52816 : }
     339             : 
     340           0 : Reference< container::XNameReplace > SAL_CALL GlobalEventConfig::getEvents() throw (::com::sun::star::uno::RuntimeException, std::exception)
     341             : {
     342           0 :     MutexGuard aGuard( GetOwnStaticMutex() );
     343           0 :     Reference< container::XNameReplace > ret(this);
     344           0 :     return ret;
     345             : }
     346             : 
     347           0 : void SAL_CALL GlobalEventConfig::replaceByName( const OUString& aName, const Any& aElement ) throw (lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, RuntimeException, std::exception)
     348             : {
     349           0 :     MutexGuard aGuard( GetOwnStaticMutex() );
     350           0 :     m_pImpl->replaceByName( aName, aElement );
     351           0 : }
     352       39780 : Any SAL_CALL GlobalEventConfig::getByName( const OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, RuntimeException, std::exception)
     353             : {
     354       39780 :     MutexGuard aGuard( GetOwnStaticMutex() );
     355       39780 :     return m_pImpl->getByName( aName );
     356             : }
     357          24 : Sequence< OUString > SAL_CALL GlobalEventConfig::getElementNames(  ) throw (RuntimeException, std::exception)
     358             : {
     359          24 :     MutexGuard aGuard( GetOwnStaticMutex() );
     360          24 :     return m_pImpl->getElementNames( );
     361             : }
     362       45667 : sal_Bool SAL_CALL GlobalEventConfig::hasByName( const OUString& aName ) throw (RuntimeException, std::exception)
     363             : {
     364       45667 :     MutexGuard aGuard( GetOwnStaticMutex() );
     365       45667 :     return m_pImpl->hasByName( aName );
     366             : }
     367           0 : Type SAL_CALL GlobalEventConfig::getElementType(  ) throw (RuntimeException, std::exception)
     368             : {
     369           0 :     MutexGuard aGuard( GetOwnStaticMutex() );
     370           0 :     return GlobalEventConfig_Impl::getElementType( );
     371             : }
     372           0 : sal_Bool SAL_CALL GlobalEventConfig::hasElements(  ) throw (RuntimeException, std::exception)
     373             : {
     374           0 :     MutexGuard aGuard( GetOwnStaticMutex() );
     375           0 :     return m_pImpl->hasElements( );
     376             : }
     377             : 
     378             : namespace
     379             : {
     380             :     class theGlobalEventConfigMutex : public rtl::Static<osl::Mutex, theGlobalEventConfigMutex>{};
     381             : }
     382             : 
     383      190744 : Mutex& GlobalEventConfig::GetOwnStaticMutex()
     384             : {
     385      190744 :     return theGlobalEventConfigMutex::get();
     386             : }
     387             : 
     388       52422 : OUString GlobalEventConfig::GetEventName( GlobalEventId nIndex )
     389             : {
     390       52422 :     return GlobalEventConfig().m_pImpl->GetEventName( nIndex );
     391             : }
     392             : 
     393             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11