Update ooo320-m1
[ooovba.git] / svtools / source / config / localisationoptions.cxx
blobdfcd47e13cf0489768c448ac1351880d76dabefc
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: localisationoptions.cxx,v $
10 * $Revision: 1.12 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
33 #ifndef GCC
34 #endif
36 //_________________________________________________________________________________________________________________
37 // includes
38 //_________________________________________________________________________________________________________________
40 #include <svtools/localisationoptions.hxx>
41 #include <unotools/configmgr.hxx>
42 #include <unotools/configitem.hxx>
43 #include <tools/debug.hxx>
44 #include <com/sun/star/uno/Any.hxx>
45 #include <com/sun/star/uno/Sequence.hxx>
47 #include <rtl/logfile.hxx>
48 #include "itemholder1.hxx"
50 //_________________________________________________________________________________________________________________
51 // namespaces
52 //_________________________________________________________________________________________________________________
54 using namespace ::utl ;
55 using namespace ::rtl ;
56 using namespace ::osl ;
57 using namespace ::com::sun::star::uno ;
59 //_________________________________________________________________________________________________________________
60 // const
61 //_________________________________________________________________________________________________________________
63 #define ROOTNODE_LOCALISATION OUString(RTL_CONSTASCII_USTRINGPARAM("Office.Common/View/Localisation"))
64 #define DEFAULT_AUTOMNEMONIC sal_False
65 #define DEFAULT_DIALOGSCALE 0
67 #define PROPERTYNAME_AUTOMNEMONIC OUString(RTL_CONSTASCII_USTRINGPARAM("AutoMnemonic" ))
68 #define PROPERTYNAME_DIALOGSCALE OUString(RTL_CONSTASCII_USTRINGPARAM("DialogScale" ))
70 #define PROPERTYHANDLE_AUTOMNEMONIC 0
71 #define PROPERTYHANDLE_DIALOGSCALE 1
73 #define PROPERTYCOUNT 2
75 //_________________________________________________________________________________________________________________
76 // private declarations!
77 //_________________________________________________________________________________________________________________
79 class SvtLocalisationOptions_Impl : public ConfigItem
81 //-------------------------------------------------------------------------------------------------------------
82 // public methods
83 //-------------------------------------------------------------------------------------------------------------
85 public:
87 //---------------------------------------------------------------------------------------------------------
88 // constructor / destructor
89 //---------------------------------------------------------------------------------------------------------
91 SvtLocalisationOptions_Impl();
92 ~SvtLocalisationOptions_Impl();
94 //---------------------------------------------------------------------------------------------------------
95 // overloaded methods of baseclass
96 //---------------------------------------------------------------------------------------------------------
98 /*-****************************************************************************************************//**
99 @short called for notify of configmanager
100 @descr These method is called from the ConfigManager before application ends or from the
101 PropertyChangeListener if the sub tree broadcasts changes. You must update your
102 internal values.
104 @seealso baseclass ConfigItem
106 @param "seqPropertyNames" is the list of properties which should be updated.
107 @return -
109 @onerror -
110 *//*-*****************************************************************************************************/
112 virtual void Notify( const Sequence< OUString >& seqPropertyNames );
114 /*-****************************************************************************************************//**
115 @short write changes to configuration
116 @descr These method writes the changed values into the sub tree
117 and should always called in our destructor to guarantee consistency of config data.
119 @seealso baseclass ConfigItem
121 @param -
122 @return -
124 @onerror -
125 *//*-*****************************************************************************************************/
127 virtual void Commit();
129 //---------------------------------------------------------------------------------------------------------
130 // public interface
131 //---------------------------------------------------------------------------------------------------------
133 /*-****************************************************************************************************//**
134 @short access method to get internal values
135 @descr These method give us a chance to regulate acces to ouer internal values.
136 It's not used in the moment - but it's possible for the feature!
138 @seealso -
140 @param -
141 @return -
143 @onerror -
144 *//*-*****************************************************************************************************/
146 sal_Bool IsAutoMnemonic ( ) const ;
147 void SetAutoMnemonic ( sal_Bool bState ) ;
148 sal_Int32 GetDialogScale ( ) const ;
149 void SetDialogScale ( sal_Int32 nScale ) ;
151 //-------------------------------------------------------------------------------------------------------------
152 // private methods
153 //-------------------------------------------------------------------------------------------------------------
155 private:
157 /*-****************************************************************************************************//**
158 @short return list of key names of ouer configuration management which represent oue module tree
159 @descr These methods return a static const list of key names. We need it to get needed values from our
160 configuration management.
162 @seealso -
164 @param -
165 @return A list of needed configuration keys is returned.
167 @onerror -
168 *//*-*****************************************************************************************************/
170 static Sequence< OUString > GetPropertyNames();
172 //-------------------------------------------------------------------------------------------------------------
173 // private member
174 //-------------------------------------------------------------------------------------------------------------
176 private:
178 sal_Bool m_bAutoMnemonic ;
179 sal_Int32 m_nDialogScale ;
182 //_________________________________________________________________________________________________________________
183 // definitions
184 //_________________________________________________________________________________________________________________
186 //*****************************************************************************************************************
187 // constructor
188 //*****************************************************************************************************************
189 SvtLocalisationOptions_Impl::SvtLocalisationOptions_Impl()
190 // Init baseclasses first
191 : ConfigItem ( ROOTNODE_LOCALISATION )
192 // Init member then.
193 , m_bAutoMnemonic ( DEFAULT_AUTOMNEMONIC )
194 , m_nDialogScale ( DEFAULT_DIALOGSCALE )
196 // Use our static list of configuration keys to get his values.
197 Sequence< OUString > seqNames = GetPropertyNames ( );
198 Sequence< Any > seqValues = GetProperties ( seqNames );
200 // Safe impossible cases.
201 // We need values from ALL configuration keys.
202 // Follow assignment use order of values in relation to our list of key names!
203 DBG_ASSERT( !(seqNames.getLength()!=seqValues.getLength()), "SvtLocalisationOptions_Impl::SvtLocalisationOptions_Impl()\nI miss some values of configuration keys!\n" );
205 // Copy values from list in right order to ouer internal member.
206 sal_Int32 nPropertyCount = seqValues.getLength();
207 for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty )
209 // Safe impossible cases.
210 // Check any for valid value.
211 DBG_ASSERT( !(seqValues[nProperty].hasValue()==sal_False), "SvtLocalisationOptions_Impl::SvtLocalisationOptions_Impl()\nInvalid property value detected!\n" );
212 switch( nProperty )
214 case PROPERTYHANDLE_AUTOMNEMONIC : {
215 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtLocalisationOptions_Impl::SvtLocalisationOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Localisation\\AutoMnemonic\"?" );
216 seqValues[nProperty] >>= m_bAutoMnemonic;
218 break;
220 case PROPERTYHANDLE_DIALOGSCALE : {
221 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_LONG), "SvtLocalisationOptions_Impl::SvtLocalisationOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Localisation\\DialogScale\"?" );
222 seqValues[nProperty] >>= m_nDialogScale;
224 break;
228 // Enable notification mechanism of ouer baseclass.
229 // We need it to get information about changes outside these class on ouer used configuration keys!
230 EnableNotification( seqNames );
233 //*****************************************************************************************************************
234 // destructor
235 //*****************************************************************************************************************
236 SvtLocalisationOptions_Impl::~SvtLocalisationOptions_Impl()
238 // We must save our current values .. if user forget it!
239 if( IsModified() == sal_True )
241 Commit();
245 //*****************************************************************************************************************
246 // public method
247 //*****************************************************************************************************************
248 void SvtLocalisationOptions_Impl::Notify( const Sequence< OUString >& seqPropertyNames )
250 // Use given list of updated properties to get his values from configuration directly!
251 Sequence< Any > seqValues = GetProperties( seqPropertyNames );
252 // Safe impossible cases.
253 // We need values from ALL notified configuration keys.
254 DBG_ASSERT( !(seqPropertyNames.getLength()!=seqValues.getLength()), "SvtLocalisationOptions_Impl::Notify()\nI miss some values of configuration keys!\n" );
255 // Step over list of property names and get right value from coreesponding value list to set it on internal members!
256 sal_Int32 nCount = seqPropertyNames.getLength();
257 for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
259 if( seqPropertyNames[nProperty] == PROPERTYNAME_AUTOMNEMONIC )
261 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtLocalisationOptions_Impl::SvtLocalisationOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Localisation\\AutoMnemonic\"?" );
262 seqValues[nProperty] >>= m_bAutoMnemonic;
264 else
265 if( seqPropertyNames[nProperty] == PROPERTYNAME_DIALOGSCALE )
267 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_LONG), "SvtLocalisationOptions_Impl::SvtLocalisationOptions_Impl()\nWho has changed the value type of \"Office.Common\\View\\Localisation\\DialogScale\"?" );
268 seqValues[nProperty] >>= m_nDialogScale;
270 #if OSL_DEBUG_LEVEL > 1
271 else DBG_ASSERT( sal_False, "SvtLocalisationOptions_Impl::Notify()\nUnkown property detected ... I can't handle these!\n" );
272 #endif
276 //*****************************************************************************************************************
277 // public method
278 //*****************************************************************************************************************
279 void SvtLocalisationOptions_Impl::Commit()
281 // Get names of supported properties, create a list for values and copy current values to it.
282 Sequence< OUString > seqNames = GetPropertyNames ();
283 sal_Int32 nCount = seqNames.getLength();
284 Sequence< Any > seqValues ( nCount );
285 for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
287 switch( nProperty )
289 case PROPERTYHANDLE_AUTOMNEMONIC : {
290 seqValues[nProperty] <<= m_bAutoMnemonic;
292 break;
294 case PROPERTYHANDLE_DIALOGSCALE : {
295 seqValues[nProperty] <<= m_nDialogScale;
297 break;
300 // Set properties in configuration.
301 PutProperties( seqNames, seqValues );
304 //*****************************************************************************************************************
305 // public method
306 //*****************************************************************************************************************
307 sal_Bool SvtLocalisationOptions_Impl::IsAutoMnemonic() const
309 return m_bAutoMnemonic;
312 //*****************************************************************************************************************
313 // public method
314 //*****************************************************************************************************************
315 void SvtLocalisationOptions_Impl::SetAutoMnemonic( sal_Bool bState )
317 m_bAutoMnemonic = bState;
318 SetModified();
321 //*****************************************************************************************************************
322 // public method
323 //*****************************************************************************************************************
324 sal_Int32 SvtLocalisationOptions_Impl::GetDialogScale() const
326 return m_nDialogScale;
329 //*****************************************************************************************************************
330 // public method
331 //*****************************************************************************************************************
332 void SvtLocalisationOptions_Impl::SetDialogScale( sal_Int32 nScale )
334 m_nDialogScale = nScale;
335 SetModified();
338 //*****************************************************************************************************************
339 // private method
340 //*****************************************************************************************************************
341 Sequence< OUString > SvtLocalisationOptions_Impl::GetPropertyNames()
343 // Build static list of configuration key names.
344 static const OUString pProperties[] =
346 PROPERTYNAME_AUTOMNEMONIC ,
347 PROPERTYNAME_DIALOGSCALE ,
349 // Initialize return sequence with these list ...
350 static const Sequence< OUString > seqPropertyNames( pProperties, PROPERTYCOUNT );
351 // ... and return it.
352 return seqPropertyNames;
355 //*****************************************************************************************************************
356 // initialize static member
357 // DON'T DO IT IN YOUR HEADER!
358 // see definition for further informations
359 //*****************************************************************************************************************
360 SvtLocalisationOptions_Impl* SvtLocalisationOptions::m_pDataContainer = NULL ;
361 sal_Int32 SvtLocalisationOptions::m_nRefCount = 0 ;
363 //*****************************************************************************************************************
364 // constructor
365 //*****************************************************************************************************************
366 SvtLocalisationOptions::SvtLocalisationOptions()
368 // Global access, must be guarded (multithreading!).
369 MutexGuard aGuard( GetOwnStaticMutex() );
370 // Increase ouer refcount ...
371 ++m_nRefCount;
372 // ... and initialize ouer data container only if it not already exist!
373 if( m_pDataContainer == NULL )
375 RTL_LOGFILE_CONTEXT(aLog, "svtools ( ??? ) ::SvtLocalisationOptions_Impl::ctor()");
376 m_pDataContainer = new SvtLocalisationOptions_Impl;
378 ItemHolder1::holdConfigItem(E_LOCALISATIONOPTIONS);
382 //*****************************************************************************************************************
383 // destructor
384 //*****************************************************************************************************************
385 SvtLocalisationOptions::~SvtLocalisationOptions()
387 // Global access, must be guarded (multithreading!)
388 MutexGuard aGuard( GetOwnStaticMutex() );
389 // Decrease ouer refcount.
390 --m_nRefCount;
391 // If last instance was deleted ...
392 // we must destroy ouer static data container!
393 if( m_nRefCount <= 0 )
395 delete m_pDataContainer;
396 m_pDataContainer = NULL;
400 //*****************************************************************************************************************
401 // public method
402 //*****************************************************************************************************************
403 sal_Bool SvtLocalisationOptions::IsAutoMnemonic() const
405 MutexGuard aGuard( GetOwnStaticMutex() );
406 return m_pDataContainer->IsAutoMnemonic();
409 //*****************************************************************************************************************
410 // public method
411 //*****************************************************************************************************************
412 void SvtLocalisationOptions::SetAutoMnemonic( sal_Bool bState )
414 MutexGuard aGuard( GetOwnStaticMutex() );
415 m_pDataContainer->SetAutoMnemonic( bState );
418 //*****************************************************************************************************************
419 // public method
420 //*****************************************************************************************************************
421 sal_Int32 SvtLocalisationOptions::GetDialogScale() const
423 MutexGuard aGuard( GetOwnStaticMutex() );
424 return m_pDataContainer->GetDialogScale();
427 //*****************************************************************************************************************
428 // public method
429 //*****************************************************************************************************************
430 void SvtLocalisationOptions::SetDialogScale( sal_Int32 nScale )
432 MutexGuard aGuard( GetOwnStaticMutex() );
433 m_pDataContainer->SetDialogScale( nScale );
436 //*****************************************************************************************************************
437 // private method
438 //*****************************************************************************************************************
439 Mutex& SvtLocalisationOptions::GetOwnStaticMutex()
441 // Initialize static mutex only for one time!
442 static Mutex* pMutex = NULL;
443 // If these method first called (Mutex not already exist!) ...
444 if( pMutex == NULL )
446 // ... we must create a new one. Protect follow code with the global mutex -
447 // It must be - we create a static variable!
448 MutexGuard aGuard( Mutex::getGlobalMutex() );
449 // We must check our pointer again - because it can be that another instance of ouer class will be fastr then these!
450 if( pMutex == NULL )
452 // Create the new mutex and set it for return on static variable.
453 static Mutex aMutex;
454 pMutex = &aMutex;
457 // Return new created or already existing mutex object.
458 return *pMutex;