LCOV - code coverage report
Current view: top level - unotools/source/config - localisationoptions.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 47 71 66.2 %
Date: 2014-11-03 Functions: 12 14 85.7 %
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/localisationoptions.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             : 
      27             : #include "itemholder1.hxx"
      28             : 
      29             : using namespace ::utl;
      30             : using namespace ::osl;
      31             : using namespace ::com::sun::star::uno;
      32             : 
      33             : #define ROOTNODE_LOCALISATION           OUString("Office.Common/View/Localisation")
      34             : #define DEFAULT_AUTOMNEMONIC            false
      35             : #define DEFAULT_DIALOGSCALE             0
      36             : 
      37             : #define PROPERTYNAME_AUTOMNEMONIC       OUString("AutoMnemonic")
      38             : #define PROPERTYNAME_DIALOGSCALE        OUString("DialogScale")
      39             : 
      40             : #define PROPERTYHANDLE_AUTOMNEMONIC     0
      41             : #define PROPERTYHANDLE_DIALOGSCALE      1
      42             : 
      43             : #define PROPERTYCOUNT                   2
      44             : 
      45             : class SvtLocalisationOptions_Impl : public ConfigItem
      46             : {
      47             :     public:
      48             : 
      49             :          SvtLocalisationOptions_Impl();
      50             :         virtual ~SvtLocalisationOptions_Impl();
      51             : 
      52             :         /*-****************************************************************************************************
      53             :             @short      called for notify of configmanager
      54             :             @descr      These method is called from the ConfigManager before application ends or from the
      55             :                          PropertyChangeListener if the sub tree broadcasts changes. You must update your
      56             :                         internal values.
      57             : 
      58             :             @seealso    baseclass ConfigItem
      59             : 
      60             :             @param      "seqPropertyNames" is the list of properties which should be updated.
      61             :         *//*-*****************************************************************************************************/
      62             : 
      63             :         virtual void Notify( const Sequence< OUString >& seqPropertyNames ) SAL_OVERRIDE;
      64             : 
      65             :         /*-****************************************************************************************************
      66             :             @short      write changes to configuration
      67             :             @descr      These method writes the changed values into the sub tree
      68             :                         and should always called in our destructor to guarantee consistency of config data.
      69             : 
      70             :             @seealso    baseclass ConfigItem
      71             :         *//*-*****************************************************************************************************/
      72             : 
      73             :         virtual void Commit() SAL_OVERRIDE;
      74             : 
      75             :         /*-****************************************************************************************************
      76             :             @short      access method to get internal values
      77             :             @descr      These method give us a chance to regulate acces to our internal values.
      78             :                         It's not used in the moment - but it's possible for the feature!
      79             :         *//*-*****************************************************************************************************/
      80             : 
      81         303 :         bool    IsAutoMnemonic  (                   ) const { return m_bAutoMnemonic;}
      82         303 :         sal_Int32   GetDialogScale  (                   ) const { return m_nDialogScale;}
      83             : 
      84             :     private:
      85             : 
      86             :         /*-****************************************************************************************************
      87             :             @short      return list of key names of our configuration management which represent oue module tree
      88             :             @descr      These methods return a static const list of key names. We need it to get needed values from our
      89             :                         configuration management.
      90             :             @return     A list of needed configuration keys is returned.
      91             :         *//*-*****************************************************************************************************/
      92             : 
      93             :         static Sequence< OUString > GetPropertyNames();
      94             : 
      95             :     private:
      96             : 
      97             :         bool    m_bAutoMnemonic;
      98             :         sal_Int32   m_nDialogScale;
      99             : };
     100             : 
     101             : //  constructor
     102             : 
     103         303 : SvtLocalisationOptions_Impl::SvtLocalisationOptions_Impl()
     104             :     // Init baseclasses first
     105             :     :   ConfigItem          ( ROOTNODE_LOCALISATION )
     106             :     // Init member then.
     107             :     ,   m_bAutoMnemonic     ( DEFAULT_AUTOMNEMONIC  )
     108         303 :     ,   m_nDialogScale      ( DEFAULT_DIALOGSCALE   )
     109             : {
     110             :     // Use our static list of configuration keys to get his values.
     111         303 :     Sequence< OUString >    seqNames    = GetPropertyNames  (           );
     112         606 :     Sequence< Any >         seqValues   = GetProperties     ( seqNames  );
     113             : 
     114             :     // Safe impossible cases.
     115             :     // We need values from ALL configuration keys.
     116             :     // Follow assignment use order of values in relation to our list of key names!
     117             :     DBG_ASSERT( !(seqNames.getLength()!=seqValues.getLength()), "SvtLocalisationOptions_Impl::SvtLocalisationOptions_Impl()\nI miss some values of configuration keys!\n" );
     118             : 
     119             :     // Copy values from list in right order to our internal member.
     120         303 :     sal_Int32 nPropertyCount = seqValues.getLength();
     121         909 :     for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty )
     122             :     {
     123         606 :         if (!seqValues[nProperty].hasValue())
     124           0 :             continue;
     125         606 :         switch( nProperty )
     126             :         {
     127             :             case PROPERTYHANDLE_AUTOMNEMONIC    :   {
     128             :                                                         DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtLocalisationOptions_Impl::SvtLocalisationOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Localisation\\AutoMnemonic\"?" );
     129         303 :                                                         seqValues[nProperty] >>= m_bAutoMnemonic;
     130             :                                                     }
     131         303 :                                                     break;
     132             : 
     133             :             case PROPERTYHANDLE_DIALOGSCALE     :   {
     134             :                                                         DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_LONG), "SvtLocalisationOptions_Impl::SvtLocalisationOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Localisation\\DialogScale\"?" );
     135         303 :                                                         seqValues[nProperty] >>= m_nDialogScale;
     136             :                                                     }
     137         303 :                                                     break;
     138             :         }
     139             :     }
     140             : 
     141             :     // Enable notification mechanism of our baseclass.
     142             :     // We need it to get information about changes outside these class on our used configuration keys!
     143         606 :     EnableNotification( seqNames );
     144         303 : }
     145             : 
     146             : //  destructor
     147             : 
     148         897 : SvtLocalisationOptions_Impl::~SvtLocalisationOptions_Impl()
     149             : {
     150             :     // We must save our current values .. if user forget it!
     151         299 :     if( IsModified() )
     152             :     {
     153           0 :         Commit();
     154             :     }
     155         598 : }
     156             : 
     157             : //  public method
     158             : 
     159           0 : void SvtLocalisationOptions_Impl::Notify( const Sequence< OUString >& seqPropertyNames )
     160             : {
     161             :     // Use given list of updated properties to get his values from configuration directly!
     162           0 :     Sequence< Any > seqValues = GetProperties( seqPropertyNames );
     163             :     // Safe impossible cases.
     164             :     // We need values from ALL notified configuration keys.
     165             :     DBG_ASSERT( !(seqPropertyNames.getLength()!=seqValues.getLength()), "SvtLocalisationOptions_Impl::Notify()\nI miss some values of configuration keys!\n" );
     166             :     // Step over list of property names and get right value from coreesponding value list to set it on internal members!
     167           0 :     sal_Int32 nCount = seqPropertyNames.getLength();
     168           0 :     for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
     169             :     {
     170           0 :         if( seqPropertyNames[nProperty] == PROPERTYNAME_AUTOMNEMONIC )
     171             :         {
     172             :             DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtLocalisationOptions_Impl::SvtLocalisationOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Localisation\\AutoMnemonic\"?" );
     173           0 :             seqValues[nProperty] >>= m_bAutoMnemonic;
     174             :         }
     175             :         else
     176           0 :         if( seqPropertyNames[nProperty] == PROPERTYNAME_DIALOGSCALE )
     177             :         {
     178             :             DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_LONG), "SvtLocalisationOptions_Impl::SvtLocalisationOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Localisation\\DialogScale\"?" );
     179           0 :             seqValues[nProperty] >>= m_nDialogScale;
     180             :         }
     181             :         #if OSL_DEBUG_LEVEL > 1
     182             :         else DBG_ASSERT( sal_False, "SvtLocalisationOptions_Impl::Notify()\nUnknown property detected ... I can't handle these!\n" );
     183             :         #endif
     184             :     }
     185             : 
     186           0 :     NotifyListeners(0);
     187           0 : }
     188             : 
     189             : //  public method
     190             : 
     191           0 : void SvtLocalisationOptions_Impl::Commit()
     192             : {
     193             :     // Get names of supported properties, create a list for values and copy current values to it.
     194           0 :     Sequence< OUString >    seqNames    = GetPropertyNames  ();
     195           0 :     sal_Int32               nCount      = seqNames.getLength();
     196           0 :     Sequence< Any >         seqValues   ( nCount );
     197           0 :     for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
     198             :     {
     199           0 :         switch( nProperty )
     200             :         {
     201             :             case PROPERTYHANDLE_AUTOMNEMONIC    :   {
     202           0 :                                                         seqValues[nProperty] <<= m_bAutoMnemonic;
     203             :                                                     }
     204           0 :                                                     break;
     205             : 
     206             :             case PROPERTYHANDLE_DIALOGSCALE     :   {
     207           0 :                                                         seqValues[nProperty] <<= m_nDialogScale;
     208             :                                                     }
     209           0 :                                                     break;
     210             :         }
     211             :     }
     212             :     // Set properties in configuration.
     213           0 :     PutProperties( seqNames, seqValues );
     214           0 : }
     215             : 
     216             : //  public method
     217             : 
     218             : 
     219         303 : Sequence< OUString > SvtLocalisationOptions_Impl::GetPropertyNames()
     220             : {
     221             :     // Build static list of configuration key names.
     222             :     const OUString aProperties[] =
     223             :     {
     224             :         PROPERTYNAME_AUTOMNEMONIC   ,
     225             :         PROPERTYNAME_DIALOGSCALE    ,
     226         909 :     };
     227             :     // Initialize return sequence with these list ...
     228         303 :     Sequence< OUString > seqPropertyNames(aProperties, PROPERTYCOUNT);
     229             :     // ... and return it.
     230         909 :     return seqPropertyNames;
     231             : }
     232             : 
     233             : //  initialize static member
     234             : //  DON'T DO IT IN YOUR HEADER!
     235             : //  see definition for further information
     236             : 
     237             : SvtLocalisationOptions_Impl*    SvtLocalisationOptions::m_pDataContainer    = NULL;
     238             : sal_Int32                       SvtLocalisationOptions::m_nRefCount         = 0;
     239             : 
     240             : //  constructor
     241             : 
     242         606 : SvtLocalisationOptions::SvtLocalisationOptions()
     243             : {
     244             :     // Global access, must be guarded (multithreading!).
     245         606 :     MutexGuard aGuard( GetOwnStaticMutex() );
     246             :     // Increase our refcount ...
     247         606 :     ++m_nRefCount;
     248             :     // ... and initialize our data container only if it not already exist!
     249         606 :     if( m_pDataContainer == NULL )
     250             :     {
     251         303 :         m_pDataContainer = new SvtLocalisationOptions_Impl;
     252             : 
     253         303 :         ItemHolder1::holdConfigItem(E_LOCALISATIONOPTIONS);
     254         606 :     }
     255         606 : }
     256             : 
     257             : //  destructor
     258             : 
     259        1503 : SvtLocalisationOptions::~SvtLocalisationOptions()
     260             : {
     261             :     // Global access, must be guarded (multithreading!)
     262         602 :     MutexGuard aGuard( GetOwnStaticMutex() );
     263             :     // Decrease our refcount.
     264         602 :     --m_nRefCount;
     265             :     // If last instance was deleted ...
     266             :     // we must destroy our static data container!
     267         602 :     if( m_nRefCount <= 0 )
     268             :     {
     269         299 :         delete m_pDataContainer;
     270         299 :         m_pDataContainer = NULL;
     271         602 :     }
     272         901 : }
     273             : 
     274             : //  public method
     275             : 
     276         303 : bool SvtLocalisationOptions::IsAutoMnemonic() const
     277             : {
     278         303 :     MutexGuard aGuard( GetOwnStaticMutex() );
     279         303 :     return m_pDataContainer->IsAutoMnemonic();
     280             : }
     281             : 
     282             : //  public method
     283             : 
     284         303 : sal_Int32 SvtLocalisationOptions::GetDialogScale() const
     285             : {
     286         303 :     MutexGuard aGuard( GetOwnStaticMutex() );
     287         303 :     return m_pDataContainer->GetDialogScale();
     288             : }
     289             : 
     290             : namespace
     291             : {
     292             :     class theLocalisationOptionsMutex : public rtl::Static<osl::Mutex, theLocalisationOptionsMutex>{};
     293             : }
     294             : 
     295             : //  private method
     296             : 
     297        1814 : Mutex& SvtLocalisationOptions::GetOwnStaticMutex()
     298             : {
     299        1814 :     return theLocalisationOptionsMutex::get();
     300             : }
     301             : 
     302             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10