Bump version to 4.3-4
[LibreOffice.git] / unotools / source / config / fontoptions.cxx
blob10321798746dbb1a1b0a50e6a59c25d9f7d505a5
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 .
20 #include <unotools/fontoptions.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>
27 #include <rtl/instance.hxx>
28 #include "itemholder1.hxx"
30 using namespace ::utl;
31 using namespace ::rtl;
32 using namespace ::osl;
33 using namespace ::com::sun::star::uno;
35 #define ROOTNODE_FONT OUString("Office.Common/Font")
37 #define PROPERTYNAME_REPLACEMENTTABLE OUString("Substitution/Replacement")
38 #define PROPERTYNAME_FONTHISTORY OUString("View/History")
39 #define PROPERTYNAME_FONTWYSIWYG OUString("View/ShowFontBoxWYSIWYG")
41 #define PROPERTYHANDLE_REPLACEMENTTABLE 0
42 #define PROPERTYHANDLE_FONTHISTORY 1
43 #define PROPERTYHANDLE_FONTWYSIWYG 2
45 #define PROPERTYCOUNT 3
47 class SvtFontOptions_Impl : public ConfigItem
49 public:
51 SvtFontOptions_Impl();
52 virtual ~SvtFontOptions_Impl();
54 /*-****************************************************************************************************
55 @short called for notify of configmanager
56 @descr These method is called from the ConfigManager before application ends or from the
57 PropertyChangeListener if the sub tree broadcasts changes. You must update your
58 internal values.
60 @seealso baseclass ConfigItem
62 @param "seqPropertyNames" is the list of properties which should be updated.
63 *//*-*****************************************************************************************************/
65 virtual void Notify( const Sequence< OUString >& seqPropertyNames ) SAL_OVERRIDE;
67 /*-****************************************************************************************************
68 @short write changes to configuration
69 @descr These method writes the changed values into the sub tree
70 and should always called in our destructor to guarantee consistency of config data.
72 @seealso baseclass ConfigItem
73 *//*-*****************************************************************************************************/
75 virtual void Commit() SAL_OVERRIDE;
77 /*-****************************************************************************************************
78 @short access method to get internal values
79 @descr These method give us a chance to regulate acces to our internal values.
80 It's not used in the moment - but it's possible for the feature!
81 *//*-*****************************************************************************************************/
83 bool IsFontHistoryEnabled ( ) const;
84 void EnableFontHistory ( bool bState );
86 bool IsFontWYSIWYGEnabled ( ) const;
87 void EnableFontWYSIWYG ( bool bState );
89 private:
91 /*-****************************************************************************************************
92 @short return list of key names of our configuration management which represent oue module tree
93 @descr These methods return a static const list of key names. We need it to get needed values from our
94 configuration management.
95 @return A list of needed configuration keys is returned.
96 *//*-*****************************************************************************************************/
98 static Sequence< OUString > impl_GetPropertyNames();
100 private:
102 bool m_bReplacementTable;
103 bool m_bFontHistory;
104 bool m_bFontWYSIWYG;
107 // constructor
109 SvtFontOptions_Impl::SvtFontOptions_Impl()
110 // Init baseclasses first
111 : ConfigItem ( ROOTNODE_FONT )
112 // Init member then.
113 , m_bReplacementTable ( false )
114 , m_bFontHistory ( false )
115 , m_bFontWYSIWYG ( false )
117 // Use our static list of configuration keys to get his values.
118 Sequence< OUString > seqNames = impl_GetPropertyNames ( );
119 Sequence< Any > seqValues = GetProperties ( seqNames );
121 // Safe impossible cases.
122 // We need values from ALL configuration keys.
123 // Follow assignment use order of values in relation to our list of key names!
124 DBG_ASSERT( !(seqNames.getLength()!=seqValues.getLength()), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nI miss some values of configuration keys!\n" );
126 // Copy values from list in right order to our internal member.
127 sal_Int32 nPropertyCount = seqValues.getLength();
128 for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty )
130 // Safe impossible cases.
131 // Check any for valid value.
132 DBG_ASSERT( seqValues[nProperty].hasValue(), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nInvalid property value detected!\n" );
133 switch( nProperty )
135 case PROPERTYHANDLE_REPLACEMENTTABLE : {
136 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nWho has changed the value type of \"Office.Common\\Font\\Substitution\\Replacement\"?" );
137 seqValues[nProperty] >>= m_bReplacementTable;
139 break;
140 case PROPERTYHANDLE_FONTHISTORY : {
141 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nWho has changed the value type of \"Office.Common\\Font\\View\\History\"?" );
142 seqValues[nProperty] >>= m_bFontHistory;
144 break;
145 case PROPERTYHANDLE_FONTWYSIWYG : {
146 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nWho has changed the value type of \"Office.Common\\Font\\View\\ShowFontBoxWYSIWYG\"?" );
147 seqValues[nProperty] >>= m_bFontWYSIWYG;
149 break;
153 // Enable notification mechanism of our baseclass.
154 // We need it to get information about changes outside these class on our used configuration keys!
155 EnableNotification( seqNames );
158 // destructor
160 SvtFontOptions_Impl::~SvtFontOptions_Impl()
162 // We must save our current values .. if user forget it!
163 if( IsModified() )
165 Commit();
169 // public method
171 void SvtFontOptions_Impl::Notify( const Sequence< OUString >& seqPropertyNames )
173 // Use given list of updated properties to get his values from configuration directly!
174 Sequence< Any > seqValues = GetProperties( seqPropertyNames );
175 // Safe impossible cases.
176 // We need values from ALL notified configuration keys.
177 DBG_ASSERT( !(seqPropertyNames.getLength()!=seqValues.getLength()), "SvtFontOptions_Impl::Notify()\nI miss some values of configuration keys!\n" );
178 // Step over list of property names and get right value from coreesponding value list to set it on internal members!
179 sal_Int32 nCount = seqPropertyNames.getLength();
180 for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
182 if( seqPropertyNames[nProperty] == PROPERTYNAME_REPLACEMENTTABLE )
184 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\Font\\Substitution\\Replacement\"?" );
185 seqValues[nProperty] >>= m_bReplacementTable;
187 else
188 if( seqPropertyNames[nProperty] == PROPERTYNAME_FONTHISTORY )
190 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\Font\\View\\History\"?" );
191 seqValues[nProperty] >>= m_bFontHistory;
193 else
194 if( seqPropertyNames[nProperty] == PROPERTYNAME_FONTWYSIWYG )
196 DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\Font\\View\\ShowFontBoxWYSIWYG\"?" );
197 seqValues[nProperty] >>= m_bFontWYSIWYG;
199 #if OSL_DEBUG_LEVEL > 1
200 else DBG_ASSERT( sal_False, "SvtFontOptions_Impl::Notify()\nUnknown property detected ... I can't handle these!\n" );
201 #endif
205 // public method
207 void SvtFontOptions_Impl::Commit()
209 // Get names of supported properties, create a list for values and copy current values to it.
210 Sequence< OUString > seqNames = impl_GetPropertyNames();
211 sal_Int32 nCount = seqNames.getLength();
212 Sequence< Any > seqValues ( nCount );
213 for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
215 switch( nProperty )
217 case PROPERTYHANDLE_REPLACEMENTTABLE : {
218 seqValues[nProperty] <<= m_bReplacementTable;
220 break;
221 case PROPERTYHANDLE_FONTHISTORY : {
222 seqValues[nProperty] <<= m_bFontHistory;
224 break;
225 case PROPERTYHANDLE_FONTWYSIWYG : {
226 seqValues[nProperty] <<= m_bFontWYSIWYG;
228 break;
231 // Set properties in configuration.
232 PutProperties( seqNames, seqValues );
235 // public method
237 bool SvtFontOptions_Impl::IsFontHistoryEnabled() const
239 return m_bFontHistory;
242 // public method
244 void SvtFontOptions_Impl::EnableFontHistory( bool bState )
246 m_bFontHistory = bState;
247 SetModified();
250 // public method
252 bool SvtFontOptions_Impl::IsFontWYSIWYGEnabled() const
254 return m_bFontWYSIWYG;
257 // public method
259 void SvtFontOptions_Impl::EnableFontWYSIWYG( bool bState )
261 m_bFontWYSIWYG = bState;
262 SetModified();
265 // private method
267 Sequence< OUString > SvtFontOptions_Impl::impl_GetPropertyNames()
269 // Build list of configuration key names.
270 const OUString pProperties[] =
272 PROPERTYNAME_REPLACEMENTTABLE ,
273 PROPERTYNAME_FONTHISTORY ,
274 PROPERTYNAME_FONTWYSIWYG ,
276 // Initialize return sequence with these list ...
277 const Sequence< OUString > seqPropertyNames( pProperties, PROPERTYCOUNT );
278 // ... and return it.
279 return seqPropertyNames;
282 // initialize static member
283 // DON'T DO IT IN YOUR HEADER!
284 // see definition for further information
286 SvtFontOptions_Impl* SvtFontOptions::m_pDataContainer = NULL;
287 sal_Int32 SvtFontOptions::m_nRefCount = 0;
289 // constructor
291 SvtFontOptions::SvtFontOptions()
293 // Global access, must be guarded (multithreading!).
294 MutexGuard aGuard( impl_GetOwnStaticMutex() );
295 // Increase our refcount ...
296 ++m_nRefCount;
297 // ... and initialize our data container only if it not already exist!
298 if( m_pDataContainer == NULL )
300 m_pDataContainer = new SvtFontOptions_Impl;
302 ItemHolder1::holdConfigItem(E_FONTOPTIONS);
306 // destructor
308 SvtFontOptions::~SvtFontOptions()
310 // Global access, must be guarded (multithreading!)
311 MutexGuard aGuard( impl_GetOwnStaticMutex() );
312 // Decrease our refcount.
313 --m_nRefCount;
314 // If last instance was deleted ...
315 // we must destroy our static data container!
316 if( m_nRefCount <= 0 )
318 delete m_pDataContainer;
319 m_pDataContainer = NULL;
323 // public method
325 bool SvtFontOptions::IsFontHistoryEnabled() const
327 MutexGuard aGuard( impl_GetOwnStaticMutex() );
328 return m_pDataContainer->IsFontHistoryEnabled();
331 // public method
333 void SvtFontOptions::EnableFontHistory( bool bState )
335 MutexGuard aGuard( impl_GetOwnStaticMutex() );
336 m_pDataContainer->EnableFontHistory( bState );
339 // public method
341 bool SvtFontOptions::IsFontWYSIWYGEnabled() const
343 MutexGuard aGuard( impl_GetOwnStaticMutex() );
344 return m_pDataContainer->IsFontWYSIWYGEnabled();
347 // public method
349 void SvtFontOptions::EnableFontWYSIWYG( bool bState )
351 MutexGuard aGuard( impl_GetOwnStaticMutex() );
352 m_pDataContainer->EnableFontWYSIWYG( bState );
355 namespace
357 class theFontOptionsMutex : public rtl::Static<osl::Mutex, theFontOptionsMutex> {};
360 // private method
362 Mutex& SvtFontOptions::impl_GetOwnStaticMutex()
364 return theFontOptionsMutex::get();
367 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */