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

Generated by: LCOV version 1.11