LCOV - code coverage report
Current view: top level - svtools/source/config - menuoptions.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 64 135 47.4 %
Date: 2015-06-13 12:38:46 Functions: 13 21 61.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : 
      21             : #include <svtools/menuoptions.hxx>
      22             : #include <unotools/configmgr.hxx>
      23             : #include <unotools/configitem.hxx>
      24             : #include <tools/debug.hxx>
      25             : #include <com/sun/star/uno/Any.hxx>
      26             : #include <com/sun/star/uno/Sequence.hxx>
      27             : #include <vcl/svapp.hxx>
      28             : #include <vcl/settings.hxx>
      29             : 
      30             : #include "itemholder2.hxx"
      31             : 
      32             : #include <list>
      33             : 
      34             : 
      35             : //  namespaces
      36             : 
      37             : 
      38             : using namespace ::utl                   ;
      39             : using namespace ::osl                   ;
      40             : using namespace ::com::sun::star::uno   ;
      41             : 
      42             : #define ROOTNODE_MENU                           OUString("Office.Common/View/Menu"  )
      43             : #define DEFAULT_DONTHIDEDISABLEDENTRIES         false
      44             : #define DEFAULT_FOLLOWMOUSE                     true
      45             : #define DEFAULT_MENUICONS                       TRISTATE_INDET
      46             : 
      47             : #define PROPERTYNAME_DONTHIDEDISABLEDENTRIES    "DontHideDisabledEntry"
      48             : #define PROPERTYNAME_FOLLOWMOUSE                "FollowMouse"
      49             : #define PROPERTYNAME_SHOWICONSINMENUES          "ShowIconsInMenues"
      50             : #define PROPERTYNAME_SYSTEMICONSINMENUES        "IsSystemIconsInMenus"
      51             : 
      52             : #define PROPERTYHANDLE_DONTHIDEDISABLEDENTRIES  0
      53             : #define PROPERTYHANDLE_FOLLOWMOUSE              1
      54             : #define PROPERTYHANDLE_SHOWICONSINMENUES        2
      55             : #define PROPERTYHANDLE_SYSTEMICONSINMENUES      3
      56             : 
      57             : #define PROPERTYCOUNT                           4
      58             : 
      59             : #include <tools/link.hxx>
      60             : 
      61             : 
      62             : //  private declarations!
      63             : 
      64             : 
      65             : class SvtMenuOptions_Impl : public ConfigItem
      66             : {
      67             : 
      68             :     //  private member
      69             : 
      70             : 
      71             :     private:
      72             :         ::std::list<Link<>> aList;
      73             :         bool        m_bDontHideDisabledEntries          ;   /// cache "DontHideDisabledEntries" of Menu section
      74             :         bool        m_bFollowMouse                      ;   /// cache "FollowMouse" of Menu section
      75             :         TriState    m_eMenuIcons                        ;   /// cache "MenuIcons" of Menu section
      76             : 
      77             : 
      78             :     //  public methods
      79             : 
      80             : 
      81             :     public:
      82             : 
      83             : 
      84             :         //  constructor / destructor
      85             : 
      86             : 
      87             :          SvtMenuOptions_Impl();
      88             :         virtual ~SvtMenuOptions_Impl();
      89             : 
      90             :         void AddListenerLink( const Link<>& rLink );
      91             :         void RemoveListenerLink( const Link<>& rLink );
      92             : 
      93             : 
      94             :         //  override methods of baseclass
      95             : 
      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      "seqPropertyNames" is the list of properties which should be updated.
     106             :         *//*-*****************************************************************************************************/
     107             : 
     108             :         virtual void Notify( const Sequence< OUString >& seqPropertyNames ) SAL_OVERRIDE;
     109             : 
     110             :         //  public interface
     111             : 
     112             : 
     113             :         /*-****************************************************************************************************
     114             :             @short      access method to get internal values
     115             :             @descr      These methods give us a chance to regulate access to our internal values.
     116             :                         It's not used in the moment - but it's possible for the future!
     117             :         *//*-*****************************************************************************************************/
     118             : 
     119          18 :         bool        IsEntryHidingEnabled() const
     120          18 :                     { return m_bDontHideDisabledEntries; }
     121             : 
     122         116 :         TriState    GetMenuIconsState() const
     123         116 :                     { return m_eMenuIcons; }
     124             : 
     125           0 :         void        SetMenuIconsState(TriState eState)
     126             :                     {
     127           0 :                         m_eMenuIcons = eState;
     128           0 :                         SetModified();
     129           0 :                         for ( ::std::list<Link<>>::const_iterator iter = aList.begin(); iter != aList.end(); ++iter )
     130           0 :                             iter->Call( this );
     131           0 :                         Commit();
     132           0 :                     }
     133             : 
     134             : 
     135             :     //  private methods
     136             : 
     137             : 
     138             :     private:
     139             : 
     140             :         virtual void ImplCommit() SAL_OVERRIDE;
     141             : 
     142             :         /*-****************************************************************************************************
     143             :             @short      return list of fix key names of our configuration management which represent our module tree
     144             :             @descr      These methods return a static const list of key names. We need it to get needed values from our
     145             :                         configuration management.
     146             :             @return     A list of needed configuration keys is returned.
     147             :         *//*-*****************************************************************************************************/
     148             : 
     149             :         static Sequence< OUString > impl_GetPropertyNames();
     150             : };
     151             : 
     152             : 
     153             : //  constructor
     154             : 
     155         116 : SvtMenuOptions_Impl::SvtMenuOptions_Impl()
     156             :     // Init baseclasses first
     157             :     :   ConfigItem                  ( ROOTNODE_MENU                     )
     158             :     // Init member then.
     159             :     ,   m_bDontHideDisabledEntries  ( DEFAULT_DONTHIDEDISABLEDENTRIES   )
     160             :     ,   m_bFollowMouse              ( DEFAULT_FOLLOWMOUSE               )
     161         116 :     ,   m_eMenuIcons                ( DEFAULT_MENUICONS                 )
     162             : {
     163             :     // Use our static list of configuration keys to get his values.
     164         116 :     Sequence< OUString >    seqNames    = impl_GetPropertyNames();
     165         232 :     Sequence< Any >         seqValues   = GetProperties( seqNames ) ;
     166             : 
     167             :     // Safe impossible cases.
     168             :     // We need values from ALL configuration keys.
     169             :     // Follow assignment use order of values in relation to our list of key names!
     170             :     DBG_ASSERT( !(seqNames.getLength()!=seqValues.getLength()), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nI miss some values of configuration keys!\n" );
     171             : 
     172         116 :     bool bMenuIcons = true;
     173         116 :     bool bSystemMenuIcons = true;
     174         116 :     if (m_eMenuIcons == TRISTATE_INDET)
     175         116 :         bMenuIcons = Application::GetSettings().GetStyleSettings().GetPreferredUseImagesInMenus();
     176             :     else
     177             :     {
     178           0 :         bSystemMenuIcons = false;
     179           0 :         bMenuIcons = m_eMenuIcons != TRISTATE_FALSE;
     180             :     }
     181             : 
     182             :     // Copy values from list in right order to our internal member.
     183         116 :     sal_Int32 nPropertyCount    =   seqValues.getLength()   ;
     184         116 :     sal_Int32 nProperty         =   0                       ;
     185         580 :     for( nProperty=0; nProperty<nPropertyCount; ++nProperty )
     186             :     {
     187             :         // Safe impossible cases.
     188             :         // Check any for valid value.
     189             :         DBG_ASSERT( seqValues[nProperty].hasValue(), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nInvalid property value for property detected!\n" );
     190             : 
     191         464 :         if (!seqValues[nProperty].hasValue())
     192           0 :             continue;
     193             : 
     194         464 :         switch( nProperty )
     195             :         {
     196             :             case PROPERTYHANDLE_DONTHIDEDISABLEDENTRIES :   {
     197             :                                                                 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\DontHideDisabledEntry\"?" );
     198         116 :                                                                 seqValues[nProperty] >>= m_bDontHideDisabledEntries;
     199             :                                                             }
     200         116 :                                                             break;
     201             : 
     202             :             case PROPERTYHANDLE_FOLLOWMOUSE             :   {
     203             :                                                                 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\FollowMouse\"?" );
     204         116 :                                                                 seqValues[nProperty] >>= m_bFollowMouse;
     205             :                                                             }
     206         116 :                                                             break;
     207             :             case PROPERTYHANDLE_SHOWICONSINMENUES       :   {
     208             :                                                                 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\ShowIconsInMenues\"?" );
     209         116 :                                                                 seqValues[nProperty] >>= bMenuIcons;
     210             :                                                             }
     211         116 :                                                             break;
     212             :             case PROPERTYHANDLE_SYSTEMICONSINMENUES     :   {
     213             :                                                                 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\IsSystemIconsInMenus\"?" );
     214         116 :                                                                 seqValues[nProperty] >>= bSystemMenuIcons;
     215             :                                                             }
     216         116 :                                                             break;
     217             :         }
     218             :     }
     219             : 
     220         116 :     m_eMenuIcons = bSystemMenuIcons ? TRISTATE_INDET : static_cast<TriState>(bMenuIcons);
     221             : 
     222         232 :     EnableNotification( seqNames );
     223         116 : }
     224             : 
     225             : 
     226             : //  destructor
     227             : 
     228         232 : SvtMenuOptions_Impl::~SvtMenuOptions_Impl()
     229             : {
     230             :     assert(!IsModified()); // should have been committed
     231         232 : }
     232             : 
     233             : 
     234             : //  public method
     235             : 
     236           0 : void SvtMenuOptions_Impl::Notify( const Sequence< OUString >& seqPropertyNames )
     237             : {
     238             :     // Use given list of updated properties to get his values from configuration directly!
     239           0 :     Sequence< Any > seqValues = GetProperties( seqPropertyNames );
     240             :     // Safe impossible cases.
     241             :     // We need values from ALL notified configuration keys.
     242             :     DBG_ASSERT( !(seqPropertyNames.getLength()!=seqValues.getLength()), "SvtMenuOptions_Impl::Notify()\nI miss some values of configuration keys!\n" );
     243             : 
     244           0 :     bool bMenuSettingsChanged = false;
     245           0 :     bool bMenuIcons = true;
     246           0 :     bool bSystemMenuIcons = true;
     247           0 :     if (m_eMenuIcons == TRISTATE_INDET)
     248           0 :         bMenuIcons = Application::GetSettings().GetStyleSettings().GetUseImagesInMenus();
     249             :     else
     250             :     {
     251           0 :         bSystemMenuIcons = false;
     252           0 :         bMenuIcons = m_eMenuIcons != TRISTATE_FALSE;
     253             :     }
     254             : 
     255             :     // Step over list of property names and get right value from coreesponding value list to set it on internal members!
     256           0 :     sal_Int32 nCount = seqPropertyNames.getLength();
     257           0 :     for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
     258             :     {
     259           0 :         if( seqPropertyNames[nProperty] == PROPERTYNAME_DONTHIDEDISABLEDENTRIES )
     260             :         {
     261             :             DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\View\\Menu\\DontHideDisabledEntry\"?" );
     262           0 :             seqValues[nProperty] >>= m_bDontHideDisabledEntries;
     263             :         }
     264           0 :         else if( seqPropertyNames[nProperty] == PROPERTYNAME_FOLLOWMOUSE )
     265             :         {
     266             :             DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\View\\Menu\\FollowMouse\"?" );
     267           0 :             seqValues[nProperty] >>= m_bFollowMouse;
     268             :         }
     269           0 :         else if( seqPropertyNames[nProperty] == PROPERTYNAME_SHOWICONSINMENUES )
     270             :         {
     271             :             DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\ShowIconsInMenues\"?" );
     272           0 :             bMenuSettingsChanged |= seqValues[nProperty] >>= bMenuIcons;
     273             :         }
     274           0 :         else if( seqPropertyNames[nProperty] == PROPERTYNAME_SYSTEMICONSINMENUES )
     275             :         {
     276             :             DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtMenuOptions_Impl::SvtMenuOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Menu\\IsSystemIconsInMenus\"?" );
     277           0 :             bMenuSettingsChanged |= seqValues[nProperty] >>= bSystemMenuIcons;
     278             :         }
     279             : 
     280             :         #if OSL_DEBUG_LEVEL > 1
     281             :         else DBG_ASSERT( sal_False, "SvtMenuOptions_Impl::Notify()\nUnknown property detected ... I can't handle these!\n" );
     282             :         #endif
     283             :     }
     284             : 
     285           0 :     if ( bMenuSettingsChanged )
     286           0 :         m_eMenuIcons = bSystemMenuIcons ? TRISTATE_INDET : static_cast<TriState>(bMenuIcons);
     287             : 
     288           0 :     for ( ::std::list<Link<>>::const_iterator iter = aList.begin(); iter != aList.end(); ++iter )
     289           0 :         iter->Call( this );
     290           0 : }
     291             : 
     292             : 
     293             : //  public method
     294             : 
     295           0 : void SvtMenuOptions_Impl::ImplCommit()
     296             : {
     297             :     // Get names of supported properties, create a list for values and copy current values to it.
     298           0 :     Sequence< OUString >    seqNames    = impl_GetPropertyNames();
     299           0 :     sal_Int32               nCount      = seqNames.getLength();
     300           0 :     Sequence< Any >         seqValues   ( nCount );
     301           0 :     for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
     302             :     {
     303           0 :         switch( nProperty )
     304             :         {
     305             :             case PROPERTYHANDLE_DONTHIDEDISABLEDENTRIES :   {
     306           0 :                                                                 seqValues[nProperty] <<= m_bDontHideDisabledEntries;
     307             :                                                             }
     308           0 :                                                             break;
     309             : 
     310             :             case PROPERTYHANDLE_FOLLOWMOUSE             :   {
     311           0 :                                                                 seqValues[nProperty] <<= m_bFollowMouse;
     312             :                                                             }
     313           0 :                                                             break;
     314             :             //Output cache of current setting as possibly modified by System Theme for older version
     315             :             case PROPERTYHANDLE_SHOWICONSINMENUES       :   {
     316           0 :                                                                 bool bValue = Application::GetSettings().GetStyleSettings().GetUseImagesInMenus();
     317           0 :                                                                 seqValues[nProperty] <<= bValue;
     318             :                                                             }
     319           0 :                                                             break;
     320             :             case PROPERTYHANDLE_SYSTEMICONSINMENUES     :   {
     321           0 :                                                                 bool bValue = m_eMenuIcons == TRISTATE_INDET;
     322           0 :                                                                 seqValues[nProperty] <<= bValue;
     323             :                                                             }
     324           0 :                                                             break;
     325             :         }
     326             :     }
     327             :     // Set properties in configuration.
     328           0 :     PutProperties( seqNames, seqValues );
     329           0 : }
     330             : 
     331             : 
     332             : //  private method
     333             : 
     334         116 : Sequence< OUString > SvtMenuOptions_Impl::impl_GetPropertyNames()
     335             : {
     336             :     // Build static list of configuration key names.
     337             :     static const OUString pProperties[] =
     338             :     {
     339             :         OUString(PROPERTYNAME_DONTHIDEDISABLEDENTRIES)    ,
     340             :         OUString(PROPERTYNAME_FOLLOWMOUSE)                ,
     341             :         OUString(PROPERTYNAME_SHOWICONSINMENUES)          ,
     342             :         OUString(PROPERTYNAME_SYSTEMICONSINMENUES)
     343         232 :     };
     344             :     // Initialize return sequence with these list ...
     345         116 :     static const Sequence< OUString > seqPropertyNames( pProperties, PROPERTYCOUNT );
     346             :     // ... and return it.
     347         116 :     return seqPropertyNames;
     348             : }
     349             : 
     350           0 : void SvtMenuOptions_Impl::AddListenerLink( const Link<>& rLink )
     351             : {
     352           0 :     aList.push_back( rLink );
     353           0 : }
     354             : 
     355           0 : void SvtMenuOptions_Impl::RemoveListenerLink( const Link<>& rLink )
     356             : {
     357           0 :     for ( ::std::list<Link<>>::iterator iter = aList.begin(); iter != aList.end(); ++iter )
     358             :     {
     359           0 :         if ( *iter == rLink )
     360             :         {
     361           0 :             aList.erase(iter);
     362           0 :             break;
     363             :         }
     364             :     }
     365           0 : }
     366             : 
     367             : 
     368             : //  initialize static member
     369             : //  DON'T DO IT IN YOUR HEADER!
     370             : //  see definition for further information
     371             : 
     372             : SvtMenuOptions_Impl*    SvtMenuOptions::m_pDataContainer    = NULL  ;
     373             : sal_Int32               SvtMenuOptions::m_nRefCount         = 0     ;
     374             : 
     375             : 
     376             : //  constructor
     377             : 
     378         250 : SvtMenuOptions::SvtMenuOptions()
     379             : {
     380             :     // Global access, must be guarded (multithreading!).
     381         250 :     MutexGuard aGuard( GetOwnStaticMutex() );
     382             :     // Increase our refcount ...
     383         250 :     ++m_nRefCount;
     384             :     // ... and initialize our data container only if it not already!
     385         250 :     if( m_pDataContainer == NULL )
     386             :     {
     387         116 :         m_pDataContainer = new SvtMenuOptions_Impl();
     388             : 
     389         116 :         svtools::ItemHolder2::holdConfigItem(E_MENUOPTIONS);
     390         250 :     }
     391         250 : }
     392             : 
     393             : 
     394             : //  destructor
     395             : 
     396         616 : SvtMenuOptions::~SvtMenuOptions()
     397             : {
     398             :     // Global access, must be guarded (multithreading!)
     399         250 :     MutexGuard aGuard( GetOwnStaticMutex() );
     400             :     // Decrease our refcount.
     401         250 :     --m_nRefCount;
     402             :     // If last instance was deleted ...
     403             :     // we must destroy our static data container!
     404         250 :     if( m_nRefCount <= 0 )
     405             :     {
     406         116 :         delete m_pDataContainer;
     407         116 :         m_pDataContainer = NULL;
     408         250 :     }
     409         366 : }
     410             : 
     411             : 
     412             : //  public method
     413             : 
     414          18 : bool SvtMenuOptions::IsEntryHidingEnabled() const
     415             : {
     416          18 :     MutexGuard aGuard( GetOwnStaticMutex() );
     417          18 :     return m_pDataContainer->IsEntryHidingEnabled();
     418             : }
     419             : 
     420             : 
     421             : //  public method
     422             : 
     423         116 : TriState SvtMenuOptions::GetMenuIconsState() const
     424             : {
     425         116 :     MutexGuard aGuard( GetOwnStaticMutex() );
     426         116 :     return m_pDataContainer->GetMenuIconsState();
     427             : }
     428             : 
     429             : 
     430             : //  public method
     431             : 
     432           0 : void SvtMenuOptions::SetMenuIconsState(TriState eState)
     433             : {
     434           0 :     MutexGuard aGuard( GetOwnStaticMutex() );
     435           0 :     m_pDataContainer->SetMenuIconsState(eState);
     436           0 : }
     437             : 
     438             : 
     439             : //  private method
     440             : 
     441         634 : Mutex& SvtMenuOptions::GetOwnStaticMutex()
     442             : {
     443             :     // Initialize static mutex only for one time!
     444             :     static Mutex* pMutex = NULL;
     445             :     // If these method first called (Mutex not already exist!) ...
     446         634 :     if( pMutex == NULL )
     447             :     {
     448             :         // ... we must create a new one. Protect follow code with the global mutex -
     449             :         // It must be - we create a static variable!
     450         116 :         MutexGuard aGuard( Mutex::getGlobalMutex() );
     451             :         // We must check our pointer again - because it can be that another instance of our class will be faster than these!
     452         116 :         if( pMutex == NULL )
     453             :         {
     454             :             // Create the new mutex and set it for return on static variable.
     455         116 :             static Mutex aMutex;
     456         116 :             pMutex = &aMutex;
     457         116 :         }
     458             :     }
     459             :     // Return new created or already existing mutex object.
     460         634 :     return *pMutex;
     461             : }
     462             : 
     463           0 : void SvtMenuOptions::AddListenerLink( const Link<>& rLink )
     464             : {
     465           0 :     m_pDataContainer->AddListenerLink( rLink );
     466           0 : }
     467             : 
     468           0 : void SvtMenuOptions::RemoveListenerLink( const Link<>& rLink )
     469             : {
     470           0 :     m_pDataContainer->RemoveListenerLink( rLink );
     471           0 : }
     472             : 
     473             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11