Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / unotools / source / config / fontoptions.cxx
blob69452159a853ce168baee5a10f1cb44e5e95e5ea
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <unotools/fontoptions.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>
28 #include <rtl/logfile.hxx>
29 #include <rtl/instance.hxx>
30 #include "itemholder1.hxx"
32 using namespace ::utl ;
33 using namespace ::rtl ;
34 using namespace ::osl ;
35 using namespace ::com::sun::star::uno ;
37 #define ROOTNODE_FONT OUString(RTL_CONSTASCII_USTRINGPARAM("Office.Common/Font" ))
39 #define PROPERTYNAME_REPLACEMENTTABLE OUString(RTL_CONSTASCII_USTRINGPARAM("Substitution/Replacement" ))
40 #define PROPERTYNAME_FONTHISTORY OUString(RTL_CONSTASCII_USTRINGPARAM("View/History" ))
41 #define PROPERTYNAME_FONTWYSIWYG OUString(RTL_CONSTASCII_USTRINGPARAM("View/ShowFontBoxWYSIWYG" ))
43 #define PROPERTYHANDLE_REPLACEMENTTABLE 0
44 #define PROPERTYHANDLE_FONTHISTORY 1
45 #define PROPERTYHANDLE_FONTWYSIWYG 2
47 #define PROPERTYCOUNT 3
49 class SvtFontOptions_Impl : public ConfigItem
51 public:
53 SvtFontOptions_Impl();
54 ~SvtFontOptions_Impl();
56 /*-****************************************************************************************************//**
57 @short called for notify of configmanager
58 @descr These method is called from the ConfigManager before application ends or from the
59 PropertyChangeListener if the sub tree broadcasts changes. You must update your
60 internal values.
62 @seealso baseclass ConfigItem
64 @param "seqPropertyNames" is the list of properties which should be updated.
65 @return -
67 @onerror -
68 *//*-*****************************************************************************************************/
70 virtual void Notify( const Sequence< OUString >& seqPropertyNames );
72 /*-****************************************************************************************************//**
73 @short write changes to configuration
74 @descr These method writes the changed values into the sub tree
75 and should always called in our destructor to guarantee consistency of config data.
77 @seealso baseclass ConfigItem
79 @param -
80 @return -
82 @onerror -
83 *//*-*****************************************************************************************************/
85 virtual void Commit();
87 /*-****************************************************************************************************//**
88 @short access method to get internal values
89 @descr These method give us a chance to regulate acces to ouer internal values.
90 It's not used in the moment - but it's possible for the feature!
92 @seealso -
94 @param -
95 @return -
97 @onerror -
98 *//*-*****************************************************************************************************/
100 sal_Bool IsFontHistoryEnabled ( ) const ;
101 void EnableFontHistory ( sal_Bool bState ) ;
103 sal_Bool IsFontWYSIWYGEnabled ( ) const ;
104 void EnableFontWYSIWYG ( sal_Bool bState ) ;
106 private:
108 /*-****************************************************************************************************//**
109 @short return list of key names of ouer configuration management which represent oue module tree
110 @descr These methods return a static const list of key names. We need it to get needed values from our
111 configuration management.
113 @seealso -
115 @param -
116 @return A list of needed configuration keys is returned.
118 @onerror -
119 *//*-*****************************************************************************************************/
121 static Sequence< OUString > impl_GetPropertyNames();
123 private:
125 sal_Bool m_bReplacementTable ;
126 sal_Bool m_bFontHistory ;
127 sal_Bool m_bFontWYSIWYG ;
130 //*****************************************************************************************************************
131 // constructor
132 //*****************************************************************************************************************
133 SvtFontOptions_Impl::SvtFontOptions_Impl()
134 // Init baseclasses first
135 : ConfigItem ( ROOTNODE_FONT )
136 // Init member then.
137 , m_bReplacementTable ( sal_False )
138 , m_bFontHistory ( sal_False )
139 , m_bFontWYSIWYG ( sal_False )
141 // Use our static list of configuration keys to get his values.
142 Sequence< OUString > seqNames = impl_GetPropertyNames ( );
143 Sequence< Any > seqValues = GetProperties ( seqNames );
145 // Safe impossible cases.
146 // We need values from ALL configuration keys.
147 // Follow assignment use order of values in relation to our list of key names!
148 DBG_ASSERT( !(seqNames.getLength()!=seqValues.getLength()), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nI miss some values of configuration keys!\n" );
150 // Copy values from list in right order to ouer internal member.
151 sal_Int32 nPropertyCount = seqValues.getLength();
152 for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty )
154 // Safe impossible cases.
155 // Check any for valid value.
156 DBG_ASSERT( !(seqValues[nProperty].hasValue()==sal_False), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nInvalid property value detected!\n" );
157 switch( nProperty )
159 case PROPERTYHANDLE_REPLACEMENTTABLE : {
160 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nWho has changed the value type of \"Office.Common\\Font\\Substitution\\Replacement\"?" );
161 seqValues[nProperty] >>= m_bReplacementTable;
163 break;
164 case PROPERTYHANDLE_FONTHISTORY : {
165 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nWho has changed the value type of \"Office.Common\\Font\\View\\History\"?" );
166 seqValues[nProperty] >>= m_bFontHistory;
168 break;
169 case PROPERTYHANDLE_FONTWYSIWYG : {
170 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nWho has changed the value type of \"Office.Common\\Font\\View\\ShowFontBoxWYSIWYG\"?" );
171 seqValues[nProperty] >>= m_bFontWYSIWYG;
173 break;
177 // Enable notification mechanism of ouer baseclass.
178 // We need it to get information about changes outside these class on ouer used configuration keys!
179 EnableNotification( seqNames );
182 //*****************************************************************************************************************
183 // destructor
184 //*****************************************************************************************************************
185 SvtFontOptions_Impl::~SvtFontOptions_Impl()
187 // We must save our current values .. if user forget it!
188 if( IsModified() == sal_True )
190 Commit();
194 //*****************************************************************************************************************
195 // public method
196 //*****************************************************************************************************************
197 void SvtFontOptions_Impl::Notify( const Sequence< OUString >& seqPropertyNames )
199 // Use given list of updated properties to get his values from configuration directly!
200 Sequence< Any > seqValues = GetProperties( seqPropertyNames );
201 // Safe impossible cases.
202 // We need values from ALL notified configuration keys.
203 DBG_ASSERT( !(seqPropertyNames.getLength()!=seqValues.getLength()), "SvtFontOptions_Impl::Notify()\nI miss some values of configuration keys!\n" );
204 // Step over list of property names and get right value from coreesponding value list to set it on internal members!
205 sal_Int32 nCount = seqPropertyNames.getLength();
206 for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
208 if( seqPropertyNames[nProperty] == PROPERTYNAME_REPLACEMENTTABLE )
210 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\Font\\Substitution\\Replacement\"?" );
211 seqValues[nProperty] >>= m_bReplacementTable;
213 else
214 if( seqPropertyNames[nProperty] == PROPERTYNAME_FONTHISTORY )
216 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\Font\\View\\History\"?" );
217 seqValues[nProperty] >>= m_bFontHistory;
219 else
220 if( seqPropertyNames[nProperty] == PROPERTYNAME_FONTWYSIWYG )
222 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\Font\\View\\ShowFontBoxWYSIWYG\"?" );
223 seqValues[nProperty] >>= m_bFontWYSIWYG;
225 #if OSL_DEBUG_LEVEL > 1
226 else DBG_ASSERT( sal_False, "SvtFontOptions_Impl::Notify()\nUnkown property detected ... I can't handle these!\n" );
227 #endif
231 //*****************************************************************************************************************
232 // public method
233 //*****************************************************************************************************************
234 void SvtFontOptions_Impl::Commit()
236 // Get names of supported properties, create a list for values and copy current values to it.
237 Sequence< OUString > seqNames = impl_GetPropertyNames();
238 sal_Int32 nCount = seqNames.getLength();
239 Sequence< Any > seqValues ( nCount );
240 for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
242 switch( nProperty )
244 case PROPERTYHANDLE_REPLACEMENTTABLE : {
245 seqValues[nProperty] <<= m_bReplacementTable;
247 break;
248 case PROPERTYHANDLE_FONTHISTORY : {
249 seqValues[nProperty] <<= m_bFontHistory;
251 break;
252 case PROPERTYHANDLE_FONTWYSIWYG : {
253 seqValues[nProperty] <<= m_bFontWYSIWYG;
255 break;
258 // Set properties in configuration.
259 PutProperties( seqNames, seqValues );
262 //*****************************************************************************************************************
263 // public method
264 //*****************************************************************************************************************
265 sal_Bool SvtFontOptions_Impl::IsFontHistoryEnabled() const
267 return m_bFontHistory;
270 //*****************************************************************************************************************
271 // public method
272 //*****************************************************************************************************************
273 void SvtFontOptions_Impl::EnableFontHistory( sal_Bool bState )
275 m_bFontHistory = bState;
276 SetModified();
279 //*****************************************************************************************************************
280 // public method
281 //*****************************************************************************************************************
282 sal_Bool SvtFontOptions_Impl::IsFontWYSIWYGEnabled() const
284 return m_bFontWYSIWYG;
287 //*****************************************************************************************************************
288 // public method
289 //*****************************************************************************************************************
290 void SvtFontOptions_Impl::EnableFontWYSIWYG( sal_Bool bState )
292 m_bFontWYSIWYG = bState;
293 SetModified();
296 //*****************************************************************************************************************
297 // private method
298 //*****************************************************************************************************************
299 Sequence< OUString > SvtFontOptions_Impl::impl_GetPropertyNames()
301 // Build list of configuration key names.
302 const OUString pProperties[] =
304 PROPERTYNAME_REPLACEMENTTABLE ,
305 PROPERTYNAME_FONTHISTORY ,
306 PROPERTYNAME_FONTWYSIWYG ,
308 // Initialize return sequence with these list ...
309 const Sequence< OUString > seqPropertyNames( pProperties, PROPERTYCOUNT );
310 // ... and return it.
311 return seqPropertyNames;
314 //*****************************************************************************************************************
315 // initialize static member
316 // DON'T DO IT IN YOUR HEADER!
317 // see definition for further informations
318 //*****************************************************************************************************************
319 SvtFontOptions_Impl* SvtFontOptions::m_pDataContainer = NULL ;
320 sal_Int32 SvtFontOptions::m_nRefCount = 0 ;
322 //*****************************************************************************************************************
323 // constructor
324 //*****************************************************************************************************************
325 SvtFontOptions::SvtFontOptions()
327 // Global access, must be guarded (multithreading!).
328 MutexGuard aGuard( impl_GetOwnStaticMutex() );
329 // Increase ouer refcount ...
330 ++m_nRefCount;
331 // ... and initialize ouer data container only if it not already exist!
332 if( m_pDataContainer == NULL )
334 RTL_LOGFILE_CONTEXT(aLog, "unotools ( ??? ) ::SvtFontOptions_Impl::ctor()");
335 m_pDataContainer = new SvtFontOptions_Impl;
337 ItemHolder1::holdConfigItem(E_FONTOPTIONS);
341 //*****************************************************************************************************************
342 // destructor
343 //*****************************************************************************************************************
344 SvtFontOptions::~SvtFontOptions()
346 // Global access, must be guarded (multithreading!)
347 MutexGuard aGuard( impl_GetOwnStaticMutex() );
348 // Decrease ouer refcount.
349 --m_nRefCount;
350 // If last instance was deleted ...
351 // we must destroy ouer static data container!
352 if( m_nRefCount <= 0 )
354 delete m_pDataContainer;
355 m_pDataContainer = NULL;
359 //*****************************************************************************************************************
360 // public method
361 //*****************************************************************************************************************
362 sal_Bool SvtFontOptions::IsFontHistoryEnabled() const
364 MutexGuard aGuard( impl_GetOwnStaticMutex() );
365 return m_pDataContainer->IsFontHistoryEnabled();
368 //*****************************************************************************************************************
369 // public method
370 //*****************************************************************************************************************
371 void SvtFontOptions::EnableFontHistory( sal_Bool bState )
373 MutexGuard aGuard( impl_GetOwnStaticMutex() );
374 m_pDataContainer->EnableFontHistory( bState );
377 //*****************************************************************************************************************
378 // public method
379 //*****************************************************************************************************************
380 sal_Bool SvtFontOptions::IsFontWYSIWYGEnabled() const
382 MutexGuard aGuard( impl_GetOwnStaticMutex() );
383 return m_pDataContainer->IsFontWYSIWYGEnabled();
386 //*****************************************************************************************************************
387 // public method
388 //*****************************************************************************************************************
389 void SvtFontOptions::EnableFontWYSIWYG( sal_Bool bState )
391 MutexGuard aGuard( impl_GetOwnStaticMutex() );
392 m_pDataContainer->EnableFontWYSIWYG( bState );
395 namespace
397 class theFontOptionsMutex : public rtl::Static<osl::Mutex, theFontOptionsMutex> {};
400 //*****************************************************************************************************************
401 // private method
402 //*****************************************************************************************************************
403 Mutex& SvtFontOptions::impl_GetOwnStaticMutex()
405 return theFontOptionsMutex::get();
408 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */