LCOV - code coverage report
Current view: top level - libreoffice/unotools/source/config - cmdoptions.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 63 107 58.9 %
Date: 2012-12-27 Functions: 18 25 72.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/cmdoptions.hxx>
      21             : #include <unotools/configmgr.hxx>
      22             : #include <unotools/configitem.hxx>
      23             : #include <tools/debug.hxx>
      24             : #include <com/sun/star/uno/Any.hxx>
      25             : #include <com/sun/star/uno/Sequence.hxx>
      26             : #include <cppuhelper/weakref.hxx>
      27             : #include <rtl/ustrbuf.hxx>
      28             : #include <rtl/instance.hxx>
      29             : 
      30             : #include <itemholder1.hxx>
      31             : 
      32             : #include <algorithm>
      33             : #include <boost/unordered_map.hpp>
      34             : 
      35             : using namespace ::std                   ;
      36             : using namespace ::utl                   ;
      37             : using namespace ::rtl                   ;
      38             : using namespace ::osl                   ;
      39             : using namespace ::com::sun::star::uno   ;
      40             : using namespace ::com::sun::star::beans ;
      41             : 
      42             : #define ROOTNODE_CMDOPTIONS                             OUString(RTL_CONSTASCII_USTRINGPARAM("Office.Commands/Execute"  ))
      43             : #define PATHDELIMITER                                   OUString(RTL_CONSTASCII_USTRINGPARAM("/"                        ))
      44             : 
      45             : #define SETNODE_DISABLED                                OUString(RTL_CONSTASCII_USTRINGPARAM("Disabled"                 ))
      46             : 
      47             : #define PROPERTYNAME_CMD                                OUString(RTL_CONSTASCII_USTRINGPARAM("Command"                  ))
      48             : 
      49             : // Method to retrieve a hash code from a string. May be we have to change it to decrease collisions in the hash map
      50             : struct OUStringHashCode
      51             : {
      52        2367 :     size_t operator()( const ::rtl::OUString& sString ) const
      53             :     {
      54        2367 :         return sString.hashCode();
      55             :     }
      56             : };
      57             : 
      58             : /*-****************************************************************************************************************
      59             :     @descr  support simple command option structures and operations on it
      60             : ****************************************************************************************************************-*/
      61          26 : class SvtCmdOptions
      62             : {
      63             :     public:
      64             :         //---------------------------------------------------------------------------------------------------------
      65             :         // the only way to free memory!
      66           0 :         void Clear()
      67             :         {
      68           0 :             m_aCommandHashMap.clear();
      69           0 :         }
      70             : 
      71           0 :         sal_Bool HasEntries() const
      72             :         {
      73           0 :             return ( m_aCommandHashMap.size() > 0 );
      74             :         }
      75             : 
      76        5221 :         sal_Bool Lookup( const OUString& aCmd ) const
      77             :         {
      78        5221 :             CommandHashMap::const_iterator pEntry = m_aCommandHashMap.find( aCmd );
      79        5221 :             return ( pEntry != m_aCommandHashMap.end() );
      80             :         }
      81             : 
      82           0 :         void AddCommand( const OUString& aCmd )
      83             :         {
      84           0 :             m_aCommandHashMap.insert( CommandHashMap::value_type( aCmd, 0 ) );
      85           0 :         }
      86             : 
      87             :         //---------------------------------------------------------------------------------------------------------
      88             :         // convert internal list to external format
      89             :         // for using it on right menus realy
      90             :         // Notice:   We build a property list with 4 entries and set it on result list then.
      91             :         //           The while-loop starts with pointer on internal member list lSetupEntries, change to
      92             :         //           lUserEntries then and stop after that with NULL!
      93             :         //           Separator entries will be packed in another way then normal entries! We define
      94             :         //           special strings "sEmpty" and "sSeperator" to perform too ...
      95             :         Sequence< OUString > GetList() const
      96             :         {
      97             :             sal_Int32               nCount = (sal_Int32)m_aCommandHashMap.size();
      98             :             sal_Int32               nIndex = 0;
      99             :             Sequence< OUString >    aList( nCount );
     100             : 
     101             :             CommandHashMap::const_iterator pEntry = m_aCommandHashMap.begin();
     102             :             while ( pEntry != m_aCommandHashMap.end() )
     103             :                 aList[nIndex++] = pEntry->first;
     104             : 
     105             :             return aList;
     106             :         }
     107             : 
     108             :     private:
     109          26 :         class CommandHashMap : public ::boost::unordered_map< ::rtl::OUString       ,
     110             :                                                         sal_Int32           ,
     111             :                                                         OUStringHashCode    ,
     112             :                                                         ::std::equal_to< ::rtl::OUString >  >
     113             :         {
     114             :             public:
     115             :                 inline void free()
     116             :                 {
     117             :                     CommandHashMap().swap( *this );
     118             :                 }
     119             :         };
     120             : 
     121             :         CommandHashMap m_aCommandHashMap;
     122             : };
     123             : 
     124             : typedef ::std::vector< ::com::sun::star::uno::WeakReference< ::com::sun::star::frame::XFrame > > SvtFrameVector;
     125             : 
     126             : class SvtCommandOptions_Impl : public ConfigItem
     127             : {
     128             :     public:
     129             : 
     130             :          SvtCommandOptions_Impl();
     131             :         ~SvtCommandOptions_Impl();
     132             : 
     133             :         /*-****************************************************************************************************//**
     134             :             @short      called for notify of configmanager
     135             :             @descr      These method is called from the ConfigManager before application ends or from the
     136             :                          PropertyChangeListener if the sub tree broadcasts changes. You must update your
     137             :                         internal values.
     138             : 
     139             :             @seealso    baseclass ConfigItem
     140             : 
     141             :             @param      "lPropertyNames" is the list of properties which should be updated.
     142             :             @return     -
     143             : 
     144             :             @onerror    -
     145             :         *//*-*****************************************************************************************************/
     146             : 
     147             :         virtual void Notify( const Sequence< OUString >& lPropertyNames );
     148             : 
     149             :         /*-****************************************************************************************************//**
     150             :             @short      write changes to configuration
     151             :             @descr      These method writes the changed values into the sub tree
     152             :                         and should always called in our destructor to guarantee consistency of config data.
     153             : 
     154             :             @seealso    baseclass ConfigItem
     155             : 
     156             :             @param      -
     157             :             @return     -
     158             : 
     159             :             @onerror    -
     160             :         *//*-*****************************************************************************************************/
     161             : 
     162             :         virtual void Commit();
     163             : 
     164             :         /*-****************************************************************************************************//**
     165             :             @short      base implementation of public interface for "SvtDynamicMenuOptions"!
     166             :             @descr      These class is used as static member of "SvtDynamicMenuOptions" ...
     167             :                         => The code exist only for one time and isn't duplicated for every instance!
     168             : 
     169             :             @seealso    -
     170             : 
     171             :             @param      -
     172             :             @return     -
     173             : 
     174             :             @onerror    -
     175             :         *//*-*****************************************************************************************************/
     176             : 
     177             :         sal_Bool                HasEntries  (   SvtCommandOptions::CmdOption    eOption     ) const;
     178             :         sal_Bool                Lookup      (   SvtCommandOptions::CmdOption    eCmdOption, const OUString& ) const;
     179             :         void EstablisFrameCallback(const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& xFrame);
     180             : 
     181             :     private:
     182             : 
     183             :         /*-****************************************************************************************************//**
     184             :             @short      return list of key names of our configuration management which represent oue module tree
     185             :             @descr      These methods return the current list of key names! We need it to get needed values from our
     186             :                         configuration management and support dynamical menu item lists!
     187             : 
     188             :             @seealso    -
     189             : 
     190             :             @param      "nDisabledCount"    ,   returns count of menu entries for "new"
     191             :             @return     A list of configuration key names is returned.
     192             : 
     193             :             @onerror    -
     194             :         *//*-*****************************************************************************************************/
     195             : 
     196             :         Sequence< OUString > impl_GetPropertyNames();
     197             : 
     198             :     private:
     199             :         SvtCmdOptions  m_aDisabledCommands;
     200             :         SvtFrameVector m_lFrames;
     201             : };
     202             : 
     203             : //*****************************************************************************************************************
     204             : //  constructor
     205             : //*****************************************************************************************************************
     206          19 : SvtCommandOptions_Impl::SvtCommandOptions_Impl()
     207             :     // Init baseclasses first
     208          19 :     :   ConfigItem( ROOTNODE_CMDOPTIONS )
     209             :     // Init member then...
     210             : {
     211             :     // Get names and values of all accessable menu entries and fill internal structures.
     212             :     // See impl_GetPropertyNames() for further informations.
     213          19 :     Sequence< OUString >    lNames              = impl_GetPropertyNames ();
     214          19 :     Sequence< Any >         lValues             = GetProperties         ( lNames         );
     215             : 
     216             :     // Safe impossible cases.
     217             :     // We need values from ALL configuration keys.
     218             :     // Follow assignment use order of values in relation to our list of key names!
     219             :     DBG_ASSERT( !(lNames.getLength()!=lValues.getLength()), "SvtCommandOptions_Impl::SvtCommandOptions_Impl()\nI miss some values of configuration keys!\n" );
     220             : 
     221             :     // Copy values from list in right order to ouer internal member.
     222             :     // Attention: List for names and values have an internal construction pattern!
     223          19 :     sal_Int32   nItem     = 0 ;
     224          19 :     OUString    sCmd          ;
     225             : 
     226             :     // Get names/values for disabled commands.
     227          19 :     for( nItem=0; nItem < lNames.getLength(); ++nItem )
     228             :     {
     229             :         // Currently only one value
     230           0 :         lValues[nItem] >>= sCmd;
     231           0 :         m_aDisabledCommands.AddCommand( sCmd );
     232             :     }
     233             : 
     234             : /*TODO: Not used in the moment! see Notify() ...
     235             :     // Enable notification mechanism of ouer baseclass.
     236             :     // We need it to get information about changes outside these class on ouer used configuration keys! */
     237          19 :     Sequence< OUString > aNotifySeq( 1 );
     238          19 :     aNotifySeq[0] = OUString( RTL_CONSTASCII_USTRINGPARAM( "Disabled" ));
     239          19 :     EnableNotification( aNotifySeq, sal_True );
     240          19 : }
     241             : 
     242             : //*****************************************************************************************************************
     243             : //  destructor
     244             : //*****************************************************************************************************************
     245          21 : SvtCommandOptions_Impl::~SvtCommandOptions_Impl()
     246             : {
     247             :     // We must save our current values .. if user forget it!
     248           7 :     if( IsModified() == sal_True )
     249             :     {
     250           0 :         Commit();
     251             :     }
     252          14 : }
     253             : 
     254             : //*****************************************************************************************************************
     255             : //  public method
     256             : //*****************************************************************************************************************
     257           0 : void SvtCommandOptions_Impl::Notify( const Sequence< OUString >& )
     258             : {
     259           0 :     MutexGuard aGuard( SvtCommandOptions::GetOwnStaticMutex() );
     260             : 
     261           0 :     Sequence< OUString >    lNames   = impl_GetPropertyNames ();
     262           0 :     Sequence< Any >         lValues  = GetProperties         ( lNames         );
     263             : 
     264             :     // Safe impossible cases.
     265             :     // We need values from ALL configuration keys.
     266             :     // Follow assignment use order of values in relation to our list of key names!
     267             :     DBG_ASSERT( !(lNames.getLength()!=lValues.getLength()), "SvtCommandOptions_Impl::SvtCommandOptions_Impl()\nI miss some values of configuration keys!\n" );
     268             : 
     269             :     // Copy values from list in right order to ouer internal member.
     270             :     // Attention: List for names and values have an internal construction pattern!
     271           0 :     sal_Int32   nItem     = 0 ;
     272           0 :     OUString    sCmd          ;
     273             : 
     274           0 :     m_aDisabledCommands.Clear();
     275             : 
     276             :     // Get names/values for disabled commands.
     277           0 :     for( nItem=0; nItem < lNames.getLength(); ++nItem )
     278             :     {
     279             :         // Currently only one value
     280           0 :         lValues[nItem] >>= sCmd;
     281           0 :         m_aDisabledCommands.AddCommand( sCmd );
     282             :     }
     283             : 
     284             :     // dont forget to update all existing frames and her might cached dispatch objects!
     285             :     // But look for already killed frames. We hold weak references instead of hard ones ...
     286           0 :     for (SvtFrameVector::const_iterator pIt  = m_lFrames.begin();
     287           0 :                                         pIt != m_lFrames.end()  ;
     288             :                                       ++pIt                     )
     289             :     {
     290           0 :         ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame > xFrame(pIt->get(), ::com::sun::star::uno::UNO_QUERY);
     291           0 :         if (xFrame.is())
     292           0 :             xFrame->contextChanged();
     293           0 :     }
     294           0 : }
     295             : 
     296             : //*****************************************************************************************************************
     297             : //  public method
     298             : //*****************************************************************************************************************
     299           0 : void SvtCommandOptions_Impl::Commit()
     300             : {
     301             :     OSL_FAIL( "SvtCommandOptions_Impl::Commit()\nNot implemented yet!\n" );
     302           0 : }
     303             : 
     304             : //*****************************************************************************************************************
     305             : //  public method
     306             : //*****************************************************************************************************************
     307           0 : sal_Bool SvtCommandOptions_Impl::HasEntries( SvtCommandOptions::CmdOption eOption ) const
     308             : {
     309           0 :     if ( eOption == SvtCommandOptions::CMDOPTION_DISABLED )
     310           0 :         return ( m_aDisabledCommands.HasEntries() > 0 );
     311             :     else
     312           0 :         return sal_False;
     313             : }
     314             : 
     315             : //*****************************************************************************************************************
     316             : //  public method
     317             : //*****************************************************************************************************************
     318        5221 : sal_Bool SvtCommandOptions_Impl::Lookup( SvtCommandOptions::CmdOption eCmdOption, const OUString& aCommand ) const
     319             : {
     320        5221 :     switch( eCmdOption )
     321             :     {
     322             :         case SvtCommandOptions::CMDOPTION_DISABLED:
     323             :         {
     324        5221 :             return m_aDisabledCommands.Lookup( aCommand );
     325             :         }
     326             :         default:
     327             :             DBG_ASSERT( sal_False, "SvtCommandOptions_Impl::GetList()\nUnknown option type given!\n" );
     328             :     }
     329             : 
     330           0 :     return sal_False;
     331             : }
     332             : 
     333             : //*****************************************************************************************************************
     334             : //  public method
     335             : //*****************************************************************************************************************
     336         240 : void SvtCommandOptions_Impl::EstablisFrameCallback(const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& xFrame)
     337             : {
     338             :     // check if frame already exists inside list
     339             :     // ignore double registrations
     340             :     // every frame must be notified one times only!
     341         240 :     ::com::sun::star::uno::WeakReference< ::com::sun::star::frame::XFrame > xWeak(xFrame);
     342         240 :     SvtFrameVector::const_iterator pIt = ::std::find(m_lFrames.begin(), m_lFrames.end(), xWeak);
     343         240 :     if (pIt == m_lFrames.end())
     344         240 :         m_lFrames.push_back(xWeak);
     345         240 : }
     346             : 
     347             : //*****************************************************************************************************************
     348             : //  private method
     349             : //*****************************************************************************************************************
     350          19 : Sequence< OUString > SvtCommandOptions_Impl::impl_GetPropertyNames()
     351             : {
     352             :     // First get ALL names of current existing list items in configuration!
     353          19 :     Sequence< OUString > lDisabledItems      = GetNodeNames( SETNODE_DISABLED, utl::CONFIG_NAME_LOCAL_PATH );
     354             : 
     355          19 :     OUString aSetNode( SETNODE_DISABLED );
     356          19 :     aSetNode += PATHDELIMITER;
     357             : 
     358          19 :     OUString aCommandKey( PATHDELIMITER );
     359          19 :     aCommandKey += PROPERTYNAME_CMD;
     360             : 
     361             :     // Expand all keys
     362          19 :     for (sal_Int32 i=0; i<lDisabledItems.getLength(); ++i )
     363             :     {
     364           0 :         OUStringBuffer aBuffer( 32 );
     365           0 :         aBuffer.append( aSetNode );
     366           0 :         aBuffer.append( lDisabledItems[i] );
     367           0 :         aBuffer.append( aCommandKey );
     368           0 :         lDisabledItems[i] = aBuffer.makeStringAndClear();
     369           0 :     }
     370             : 
     371             :     // Return result.
     372          19 :     return lDisabledItems;
     373             : }
     374             : 
     375             : //*****************************************************************************************************************
     376             : //  initialize static member
     377             : //  DON'T DO IT IN YOUR HEADER!
     378             : //  see definition for further informations
     379             : //*****************************************************************************************************************
     380             : SvtCommandOptions_Impl*     SvtCommandOptions::m_pDataContainer = NULL  ;
     381             : sal_Int32                   SvtCommandOptions::m_nRefCount      = 0     ;
     382             : 
     383             : //*****************************************************************************************************************
     384             : //  constructor
     385             : //*****************************************************************************************************************
     386        1505 : SvtCommandOptions::SvtCommandOptions()
     387             : {
     388             :     // Global access, must be guarded (multithreading!).
     389        1505 :     MutexGuard aGuard( GetOwnStaticMutex() );
     390             :     // Increase ouer refcount ...
     391        1505 :     ++m_nRefCount;
     392             :     // ... and initialize ouer data container only if it not already exist!
     393        1505 :     if( m_pDataContainer == NULL )
     394             :     {
     395          19 :         m_pDataContainer = new SvtCommandOptions_Impl;
     396          19 :         ItemHolder1::holdConfigItem(E_CMDOPTIONS);
     397        1505 :     }
     398        1505 : }
     399             : 
     400             : //*****************************************************************************************************************
     401             : //  destructor
     402             : //*****************************************************************************************************************
     403        2651 : SvtCommandOptions::~SvtCommandOptions()
     404             : {
     405             :     // Global access, must be guarded (multithreading!)
     406        1316 :     MutexGuard aGuard( GetOwnStaticMutex() );
     407             :     // Decrease ouer refcount.
     408        1316 :     --m_nRefCount;
     409             :     // If last instance was deleted ...
     410             :     // we must destroy ouer static data container!
     411        1316 :     if( m_nRefCount <= 0 )
     412             :     {
     413           7 :         delete m_pDataContainer;
     414           7 :         m_pDataContainer = NULL;
     415        1316 :     }
     416        1335 : }
     417             : 
     418             : //*****************************************************************************************************************
     419             : //  public method
     420             : //*****************************************************************************************************************
     421           0 : sal_Bool SvtCommandOptions::HasEntries( CmdOption eOption ) const
     422             : {
     423           0 :     MutexGuard aGuard( GetOwnStaticMutex() );
     424           0 :     return m_pDataContainer->HasEntries( eOption );
     425             : }
     426             : 
     427             : //*****************************************************************************************************************
     428             : //  public method
     429             : //*****************************************************************************************************************
     430        5221 : sal_Bool SvtCommandOptions::Lookup( CmdOption eCmdOption, const OUString& aCommandURL ) const
     431             : {
     432        5221 :     MutexGuard aGuard( GetOwnStaticMutex() );
     433        5221 :     return m_pDataContainer->Lookup( eCmdOption, aCommandURL );
     434             : }
     435             : 
     436             : //*****************************************************************************************************************
     437             : //  public method
     438             : //*****************************************************************************************************************
     439         240 : void SvtCommandOptions::EstablisFrameCallback(const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& xFrame)
     440             : {
     441         240 :     MutexGuard aGuard( GetOwnStaticMutex() );
     442         240 :     m_pDataContainer->EstablisFrameCallback(xFrame);
     443         240 : }
     444             : 
     445             : namespace
     446             : {
     447             :     class theCommandOptionsMutex : public rtl::Static<osl::Mutex, theCommandOptionsMutex>{};
     448             : }
     449             : 
     450             : //*****************************************************************************************************************
     451             : //  private method
     452             : //*****************************************************************************************************************
     453        8282 : Mutex& SvtCommandOptions::GetOwnStaticMutex()
     454             : {
     455        8282 :     return theCommandOptionsMutex::get();
     456             : }
     457             : 
     458             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10