LCOV - code coverage report
Current view: top level - unotools/source/config - cmdoptions.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 68 104 65.4 %
Date: 2014-04-11 Functions: 18 22 81.8 %
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("Office.Commands/Execute")
      43             : #define PATHDELIMITER                                   OUString("/")
      44             : 
      45             : #define SETNODE_DISABLED                                OUString("Disabled")
      46             : 
      47             : #define PROPERTYNAME_CMD                                OUString("Command")
      48             : 
      49             : /*-****************************************************************************************************************
      50             :     @descr  support simple command option structures and operations on it
      51             : ****************************************************************************************************************-*/
      52         249 : class SvtCmdOptions
      53             : {
      54             :     public:
      55             : 
      56             :         // the only way to free memory!
      57           0 :         void Clear()
      58             :         {
      59           0 :             m_aCommandHashMap.clear();
      60           0 :         }
      61             : 
      62        2123 :         bool HasEntries() const
      63             :         {
      64        2123 :             return ( m_aCommandHashMap.size() > 0 );
      65             :         }
      66             : 
      67      145857 :         bool Lookup( const OUString& aCmd ) const
      68             :         {
      69      145857 :             CommandHashMap::const_iterator pEntry = m_aCommandHashMap.find( aCmd );
      70      145857 :             return ( pEntry != m_aCommandHashMap.end() );
      71             :         }
      72             : 
      73           0 :         void AddCommand( const OUString& aCmd )
      74             :         {
      75           0 :             m_aCommandHashMap.insert( CommandHashMap::value_type( aCmd, 0 ) );
      76           0 :         }
      77             : 
      78             :     private:
      79             :         typedef boost::unordered_map<OUString, sal_Int32, OUStringHash>
      80             :             CommandHashMap;
      81             : 
      82             :         CommandHashMap m_aCommandHashMap;
      83             : };
      84             : 
      85             : typedef ::std::vector< ::com::sun::star::uno::WeakReference< ::com::sun::star::frame::XFrame > > SvtFrameVector;
      86             : 
      87             : class SvtCommandOptions_Impl : public ConfigItem
      88             : {
      89             :     public:
      90             : 
      91             :          SvtCommandOptions_Impl();
      92             :         virtual ~SvtCommandOptions_Impl();
      93             : 
      94             :         /*-****************************************************************************************************
      95             :             @short      called for notify of configmanager
      96             :             @descr      These method is called from the ConfigManager before application ends or from the
      97             :                          PropertyChangeListener if the sub tree broadcasts changes. You must update your
      98             :                         internal values.
      99             : 
     100             :             @seealso    baseclass ConfigItem
     101             : 
     102             :             @param      "lPropertyNames" is the list of properties which should be updated.
     103             :         *//*-*****************************************************************************************************/
     104             : 
     105             :         virtual void Notify( const Sequence< OUString >& lPropertyNames ) SAL_OVERRIDE;
     106             : 
     107             :         /*-****************************************************************************************************
     108             :             @short      write changes to configuration
     109             :             @descr      These method writes the changed values into the sub tree
     110             :                         and should always called in our destructor to guarantee consistency of config data.
     111             : 
     112             :             @seealso    baseclass ConfigItem
     113             :         *//*-*****************************************************************************************************/
     114             : 
     115             :         virtual void Commit() SAL_OVERRIDE;
     116             : 
     117             :         /*-****************************************************************************************************
     118             :             @short      base implementation of public interface for "SvtDynamicMenuOptions"!
     119             :             @descr      These class is used as static member of "SvtDynamicMenuOptions" ...
     120             :                         => The code exist only for one time and isn't duplicated for every instance!
     121             :         *//*-*****************************************************************************************************/
     122             : 
     123             :         bool                HasEntries  (   SvtCommandOptions::CmdOption    eOption     ) const;
     124             :         bool                Lookup      (   SvtCommandOptions::CmdOption    eCmdOption, const OUString& ) const;
     125             :         void EstablisFrameCallback(const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& xFrame);
     126             : 
     127             :     private:
     128             : 
     129             :         /*-****************************************************************************************************
     130             :             @short      return list of key names of our configuration management which represent oue module tree
     131             :             @descr      These methods return the current list of key names! We need it to get needed values from our
     132             :                         configuration management and support dynamical menu item lists!
     133             :             @param      "nDisabledCount"    ,   returns count of menu entries for "new"
     134             :             @return     A list of configuration key names is returned.
     135             :         *//*-*****************************************************************************************************/
     136             : 
     137             :         Sequence< OUString > impl_GetPropertyNames();
     138             : 
     139             :     private:
     140             :         SvtCmdOptions  m_aDisabledCommands;
     141             :         SvtFrameVector m_lFrames;
     142             : };
     143             : 
     144             : //  constructor
     145             : 
     146         146 : SvtCommandOptions_Impl::SvtCommandOptions_Impl()
     147             :     // Init baseclasses first
     148         146 :     :   ConfigItem( ROOTNODE_CMDOPTIONS )
     149             :     // Init member then...
     150             : {
     151             :     // Get names and values of all accessible menu entries and fill internal structures.
     152             :     // See impl_GetPropertyNames() for further information.
     153         146 :     Sequence< OUString >    lNames              = impl_GetPropertyNames ();
     154         292 :     Sequence< Any >         lValues             = GetProperties         ( lNames         );
     155             : 
     156             :     // Safe impossible cases.
     157             :     // We need values from ALL configuration keys.
     158             :     // Follow assignment use order of values in relation to our list of key names!
     159             :     DBG_ASSERT( !(lNames.getLength()!=lValues.getLength()), "SvtCommandOptions_Impl::SvtCommandOptions_Impl()\nI miss some values of configuration keys!\n" );
     160             : 
     161             :     // Copy values from list in right order to our internal member.
     162             :     // Attention: List for names and values have an internal construction pattern!
     163         146 :     sal_Int32   nItem     = 0;
     164         292 :     OUString    sCmd;
     165             : 
     166             :     // Get names/values for disabled commands.
     167         146 :     for( nItem=0; nItem < lNames.getLength(); ++nItem )
     168             :     {
     169             :         // Currently only one value
     170           0 :         lValues[nItem] >>= sCmd;
     171           0 :         m_aDisabledCommands.AddCommand( sCmd );
     172             :     }
     173             : 
     174             : /*TODO: Not used in the moment! see Notify() ...
     175             :     // Enable notification mechanism of our baseclass.
     176             :     // We need it to get information about changes outside these class on our used configuration keys! */
     177         292 :     Sequence< OUString > aNotifySeq( 1 );
     178         146 :     aNotifySeq[0] = "Disabled";
     179         292 :     EnableNotification( aNotifySeq, true );
     180         146 : }
     181             : 
     182             : //  destructor
     183             : 
     184         309 : SvtCommandOptions_Impl::~SvtCommandOptions_Impl()
     185             : {
     186             :     // We must save our current values .. if user forget it!
     187         103 :     if( IsModified() )
     188             :     {
     189           0 :         Commit();
     190             :     }
     191         206 : }
     192             : 
     193             : //  public method
     194             : 
     195           0 : void SvtCommandOptions_Impl::Notify( const Sequence< OUString >& )
     196             : {
     197           0 :     MutexGuard aGuard( SvtCommandOptions::GetOwnStaticMutex() );
     198             : 
     199           0 :     Sequence< OUString >    lNames   = impl_GetPropertyNames ();
     200           0 :     Sequence< Any >         lValues  = GetProperties         ( lNames         );
     201             : 
     202             :     // Safe impossible cases.
     203             :     // We need values from ALL configuration keys.
     204             :     // Follow assignment use order of values in relation to our list of key names!
     205             :     DBG_ASSERT( !(lNames.getLength()!=lValues.getLength()), "SvtCommandOptions_Impl::Notify()\nI miss some values of configuration keys!\n" );
     206             : 
     207             :     // Copy values from list in right order to our internal member.
     208             :     // Attention: List for names and values have an internal construction pattern!
     209           0 :     sal_Int32   nItem     = 0;
     210           0 :     OUString    sCmd;
     211             : 
     212           0 :     m_aDisabledCommands.Clear();
     213             : 
     214             :     // Get names/values for disabled commands.
     215           0 :     for( nItem=0; nItem < lNames.getLength(); ++nItem )
     216             :     {
     217             :         // Currently only one value
     218           0 :         lValues[nItem] >>= sCmd;
     219           0 :         m_aDisabledCommands.AddCommand( sCmd );
     220             :     }
     221             : 
     222             :     // dont forget to update all existing frames and her might cached dispatch objects!
     223             :     // But look for already killed frames. We hold weak references instead of hard ones ...
     224           0 :     for (SvtFrameVector::const_iterator pIt  = m_lFrames.begin();
     225           0 :                                         pIt != m_lFrames.end();
     226             :                                       ++pIt                     )
     227             :     {
     228           0 :         ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame > xFrame(pIt->get(), ::com::sun::star::uno::UNO_QUERY);
     229           0 :         if (xFrame.is())
     230           0 :             xFrame->contextChanged();
     231           0 :     }
     232           0 : }
     233             : 
     234             : //  public method
     235             : 
     236           0 : void SvtCommandOptions_Impl::Commit()
     237             : {
     238             :     OSL_FAIL( "SvtCommandOptions_Impl::Commit()\nNot implemented yet!\n" );
     239           0 : }
     240             : 
     241             : //  public method
     242             : 
     243        2123 : bool SvtCommandOptions_Impl::HasEntries( SvtCommandOptions::CmdOption eOption ) const
     244             : {
     245        2123 :     if ( eOption == SvtCommandOptions::CMDOPTION_DISABLED )
     246        2123 :         return m_aDisabledCommands.HasEntries();
     247             :     else
     248           0 :         return false;
     249             : }
     250             : 
     251             : //  public method
     252             : 
     253      145857 : bool SvtCommandOptions_Impl::Lookup( SvtCommandOptions::CmdOption eCmdOption, const OUString& aCommand ) const
     254             : {
     255      145857 :     switch( eCmdOption )
     256             :     {
     257             :         case SvtCommandOptions::CMDOPTION_DISABLED:
     258             :         {
     259      145857 :             return m_aDisabledCommands.Lookup( aCommand );
     260             :         }
     261             :         default:
     262             :             DBG_ASSERT( false, "SvtCommandOptions_Impl::Lookup()\nUnknown option type given!\n" );
     263             :     }
     264             : 
     265           0 :     return false;
     266             : }
     267             : 
     268             : //  public method
     269             : 
     270        2078 : void SvtCommandOptions_Impl::EstablisFrameCallback(const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& xFrame)
     271             : {
     272             :     // check if frame already exists inside list
     273             :     // ignore double registrations
     274             :     // every frame must be notified one times only!
     275        2078 :     ::com::sun::star::uno::WeakReference< ::com::sun::star::frame::XFrame > xWeak(xFrame);
     276        2078 :     SvtFrameVector::const_iterator pIt = ::std::find(m_lFrames.begin(), m_lFrames.end(), xWeak);
     277        2078 :     if (pIt == m_lFrames.end())
     278        2078 :         m_lFrames.push_back(xWeak);
     279        2078 : }
     280             : 
     281             : //  private method
     282             : 
     283         146 : Sequence< OUString > SvtCommandOptions_Impl::impl_GetPropertyNames()
     284             : {
     285             :     // First get ALL names of current existing list items in configuration!
     286         146 :     Sequence< OUString > lDisabledItems      = GetNodeNames( SETNODE_DISABLED, utl::CONFIG_NAME_LOCAL_PATH );
     287             : 
     288         292 :     OUString aSetNode( SETNODE_DISABLED );
     289         146 :     aSetNode += PATHDELIMITER;
     290             : 
     291         292 :     OUString aCommandKey( PATHDELIMITER );
     292         146 :     aCommandKey += PROPERTYNAME_CMD;
     293             : 
     294             :     // Expand all keys
     295         146 :     for (sal_Int32 i=0; i<lDisabledItems.getLength(); ++i )
     296             :     {
     297           0 :         OUStringBuffer aBuffer( 32 );
     298           0 :         aBuffer.append( aSetNode );
     299           0 :         aBuffer.append( lDisabledItems[i] );
     300           0 :         aBuffer.append( aCommandKey );
     301           0 :         lDisabledItems[i] = aBuffer.makeStringAndClear();
     302           0 :     }
     303             : 
     304             :     // Return result.
     305         292 :     return lDisabledItems;
     306             : }
     307             : 
     308             : //  initialize static member
     309             : //  DON'T DO IT IN YOUR HEADER!
     310             : //  see definition for further information
     311             : 
     312             : SvtCommandOptions_Impl*     SvtCommandOptions::m_pDataContainer = NULL;
     313             : sal_Int32                   SvtCommandOptions::m_nRefCount      = 0;
     314             : 
     315             : //  constructor
     316             : 
     317       16424 : SvtCommandOptions::SvtCommandOptions()
     318             : {
     319             :     // Global access, must be guarded (multithreading!).
     320       16424 :     MutexGuard aGuard( GetOwnStaticMutex() );
     321             :     // Increase our refcount ...
     322       16424 :     ++m_nRefCount;
     323             :     // ... and initialize our data container only if it not already exist!
     324       16424 :     if( m_pDataContainer == NULL )
     325             :     {
     326         146 :         m_pDataContainer = new SvtCommandOptions_Impl;
     327         146 :         ItemHolder1::holdConfigItem(E_CMDOPTIONS);
     328       16424 :     }
     329       16424 : }
     330             : 
     331             : //  destructor
     332             : 
     333       32879 : SvtCommandOptions::~SvtCommandOptions()
     334             : {
     335             :     // Global access, must be guarded (multithreading!)
     336       16367 :     MutexGuard aGuard( GetOwnStaticMutex() );
     337             :     // Decrease our refcount.
     338       16367 :     --m_nRefCount;
     339             :     // If last instance was deleted ...
     340             :     // we must destroy our static data container!
     341       16367 :     if( m_nRefCount <= 0 )
     342             :     {
     343         103 :         delete m_pDataContainer;
     344         103 :         m_pDataContainer = NULL;
     345       16367 :     }
     346       16512 : }
     347             : 
     348             : //  public method
     349             : 
     350        2123 : bool SvtCommandOptions::HasEntries( CmdOption eOption ) const
     351             : {
     352        2123 :     MutexGuard aGuard( GetOwnStaticMutex() );
     353        2123 :     return m_pDataContainer->HasEntries( eOption );
     354             : }
     355             : 
     356             : //  public method
     357             : 
     358      145857 : bool SvtCommandOptions::Lookup( CmdOption eCmdOption, const OUString& aCommandURL ) const
     359             : {
     360      145857 :     MutexGuard aGuard( GetOwnStaticMutex() );
     361      145857 :     return m_pDataContainer->Lookup( eCmdOption, aCommandURL );
     362             : }
     363             : 
     364             : //  public method
     365             : 
     366        2078 : void SvtCommandOptions::EstablisFrameCallback(const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& xFrame)
     367             : {
     368        2078 :     MutexGuard aGuard( GetOwnStaticMutex() );
     369        2078 :     m_pDataContainer->EstablisFrameCallback(xFrame);
     370        2078 : }
     371             : 
     372             : namespace
     373             : {
     374             :     class theCommandOptionsMutex : public rtl::Static<osl::Mutex, theCommandOptionsMutex>{};
     375             : }
     376             : 
     377             : //  private method
     378             : 
     379      182849 : Mutex& SvtCommandOptions::GetOwnStaticMutex()
     380             : {
     381      182849 :     return theCommandOptionsMutex::get();
     382             : }
     383             : 
     384             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10