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/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>
26 :
27 : #include <rtl/instance.hxx>
28 : #include "itemholder1.hxx"
29 :
30 : using namespace ::utl;
31 : using namespace ::osl;
32 : using namespace ::com::sun::star::uno;
33 :
34 : #define ROOTNODE_FONT OUString("Office.Common/Font")
35 :
36 : #define PROPERTYNAME_REPLACEMENTTABLE OUString("Substitution/Replacement")
37 : #define PROPERTYNAME_FONTHISTORY OUString("View/History")
38 : #define PROPERTYNAME_FONTWYSIWYG OUString("View/ShowFontBoxWYSIWYG")
39 :
40 : #define PROPERTYHANDLE_REPLACEMENTTABLE 0
41 : #define PROPERTYHANDLE_FONTHISTORY 1
42 : #define PROPERTYHANDLE_FONTWYSIWYG 2
43 :
44 : #define PROPERTYCOUNT 3
45 :
46 : class SvtFontOptions_Impl : public ConfigItem
47 : {
48 : public:
49 :
50 : SvtFontOptions_Impl();
51 : virtual ~SvtFontOptions_Impl();
52 :
53 : /*-****************************************************************************************************
54 : @short called for notify of configmanager
55 : @descr These method is called from the ConfigManager before application ends or from the
56 : PropertyChangeListener if the sub tree broadcasts changes. You must update your
57 : internal values.
58 :
59 : @seealso baseclass ConfigItem
60 :
61 : @param "seqPropertyNames" is the list of properties which should be updated.
62 : *//*-*****************************************************************************************************/
63 :
64 : virtual void Notify( const Sequence< OUString >& seqPropertyNames ) SAL_OVERRIDE;
65 :
66 : /*-****************************************************************************************************
67 : @short write changes to configuration
68 : @descr These method writes the changed values into the sub tree
69 : and should always called in our destructor to guarantee consistency of config data.
70 :
71 : @seealso baseclass ConfigItem
72 : *//*-*****************************************************************************************************/
73 :
74 : virtual void Commit() SAL_OVERRIDE;
75 :
76 : /*-****************************************************************************************************
77 : @short access method to get internal values
78 : @descr These method give us a chance to regulate acces to our internal values.
79 : It's not used in the moment - but it's possible for the feature!
80 : *//*-*****************************************************************************************************/
81 :
82 1956 : bool IsFontHistoryEnabled ( ) const { return m_bFontHistory;}
83 : void EnableFontHistory ( bool bState );
84 :
85 1956 : bool IsFontWYSIWYGEnabled ( ) const { return m_bFontWYSIWYG;}
86 : void EnableFontWYSIWYG ( bool bState );
87 :
88 : private:
89 :
90 : /*-****************************************************************************************************
91 : @short return list of key names of our configuration management which represent oue module tree
92 : @descr These methods return a static const list of key names. We need it to get needed values from our
93 : configuration management.
94 : @return A list of needed configuration keys is returned.
95 : *//*-*****************************************************************************************************/
96 :
97 : static Sequence< OUString > impl_GetPropertyNames();
98 :
99 : private:
100 :
101 : bool m_bReplacementTable;
102 : bool m_bFontHistory;
103 : bool m_bFontWYSIWYG;
104 : };
105 :
106 : // constructor
107 :
108 52 : SvtFontOptions_Impl::SvtFontOptions_Impl()
109 : // Init baseclasses first
110 : : ConfigItem ( ROOTNODE_FONT )
111 : // Init member then.
112 : , m_bReplacementTable ( false )
113 : , m_bFontHistory ( false )
114 52 : , m_bFontWYSIWYG ( false )
115 : {
116 : // Use our static list of configuration keys to get his values.
117 52 : Sequence< OUString > seqNames = impl_GetPropertyNames ( );
118 104 : Sequence< Any > seqValues = GetProperties ( seqNames );
119 :
120 : // Safe impossible cases.
121 : // We need values from ALL configuration keys.
122 : // Follow assignment use order of values in relation to our list of key names!
123 : DBG_ASSERT( !(seqNames.getLength()!=seqValues.getLength()), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nI miss some values of configuration keys!\n" );
124 :
125 : // Copy values from list in right order to our internal member.
126 52 : sal_Int32 nPropertyCount = seqValues.getLength();
127 208 : for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty )
128 : {
129 : // Safe impossible cases.
130 : // Check any for valid value.
131 : DBG_ASSERT( seqValues[nProperty].hasValue(), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nInvalid property value detected!\n" );
132 156 : switch( nProperty )
133 : {
134 : case PROPERTYHANDLE_REPLACEMENTTABLE : {
135 : DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nWho has changed the value type of \"Office.Common\\Font\\Substitution\\Replacement\"?" );
136 52 : seqValues[nProperty] >>= m_bReplacementTable;
137 : }
138 52 : break;
139 : case PROPERTYHANDLE_FONTHISTORY : {
140 : DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nWho has changed the value type of \"Office.Common\\Font\\View\\History\"?" );
141 52 : seqValues[nProperty] >>= m_bFontHistory;
142 : }
143 52 : break;
144 : case PROPERTYHANDLE_FONTWYSIWYG : {
145 : DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nWho has changed the value type of \"Office.Common\\Font\\View\\ShowFontBoxWYSIWYG\"?" );
146 52 : seqValues[nProperty] >>= m_bFontWYSIWYG;
147 : }
148 52 : break;
149 : }
150 : }
151 :
152 : // Enable notification mechanism of our baseclass.
153 : // We need it to get information about changes outside these class on our used configuration keys!
154 104 : EnableNotification( seqNames );
155 52 : }
156 :
157 : // destructor
158 :
159 156 : SvtFontOptions_Impl::~SvtFontOptions_Impl()
160 : {
161 : // We must save our current values .. if user forget it!
162 52 : if( IsModified() )
163 : {
164 0 : Commit();
165 : }
166 104 : }
167 :
168 : // public method
169 :
170 0 : void SvtFontOptions_Impl::Notify( const Sequence< OUString >& seqPropertyNames )
171 : {
172 : // Use given list of updated properties to get his values from configuration directly!
173 0 : Sequence< Any > seqValues = GetProperties( seqPropertyNames );
174 : // Safe impossible cases.
175 : // We need values from ALL notified configuration keys.
176 : DBG_ASSERT( !(seqPropertyNames.getLength()!=seqValues.getLength()), "SvtFontOptions_Impl::Notify()\nI miss some values of configuration keys!\n" );
177 : // Step over list of property names and get right value from coreesponding value list to set it on internal members!
178 0 : sal_Int32 nCount = seqPropertyNames.getLength();
179 0 : for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
180 : {
181 0 : if( seqPropertyNames[nProperty] == PROPERTYNAME_REPLACEMENTTABLE )
182 : {
183 : DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\Font\\Substitution\\Replacement\"?" );
184 0 : seqValues[nProperty] >>= m_bReplacementTable;
185 : }
186 : else
187 0 : if( seqPropertyNames[nProperty] == PROPERTYNAME_FONTHISTORY )
188 : {
189 : DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\Font\\View\\History\"?" );
190 0 : seqValues[nProperty] >>= m_bFontHistory;
191 : }
192 : else
193 0 : if( seqPropertyNames[nProperty] == PROPERTYNAME_FONTWYSIWYG )
194 : {
195 : DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\Font\\View\\ShowFontBoxWYSIWYG\"?" );
196 0 : seqValues[nProperty] >>= m_bFontWYSIWYG;
197 : }
198 : #if OSL_DEBUG_LEVEL > 1
199 : else DBG_ASSERT( sal_False, "SvtFontOptions_Impl::Notify()\nUnknown property detected ... I can't handle these!\n" );
200 : #endif
201 0 : }
202 0 : }
203 :
204 : // public method
205 :
206 0 : void SvtFontOptions_Impl::Commit()
207 : {
208 : // Get names of supported properties, create a list for values and copy current values to it.
209 0 : Sequence< OUString > seqNames = impl_GetPropertyNames();
210 0 : sal_Int32 nCount = seqNames.getLength();
211 0 : Sequence< Any > seqValues ( nCount );
212 0 : for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
213 : {
214 0 : switch( nProperty )
215 : {
216 : case PROPERTYHANDLE_REPLACEMENTTABLE : {
217 0 : seqValues[nProperty] <<= m_bReplacementTable;
218 : }
219 0 : break;
220 : case PROPERTYHANDLE_FONTHISTORY : {
221 0 : seqValues[nProperty] <<= m_bFontHistory;
222 : }
223 0 : break;
224 : case PROPERTYHANDLE_FONTWYSIWYG : {
225 0 : seqValues[nProperty] <<= m_bFontWYSIWYG;
226 : }
227 0 : break;
228 : }
229 : }
230 : // Set properties in configuration.
231 0 : PutProperties( seqNames, seqValues );
232 0 : }
233 :
234 : // public method
235 :
236 0 : void SvtFontOptions_Impl::EnableFontHistory( bool bState )
237 : {
238 0 : m_bFontHistory = bState;
239 0 : SetModified();
240 0 : }
241 :
242 : // public method
243 :
244 0 : void SvtFontOptions_Impl::EnableFontWYSIWYG( bool bState )
245 : {
246 0 : m_bFontWYSIWYG = bState;
247 0 : SetModified();
248 0 : }
249 :
250 : // private method
251 :
252 52 : Sequence< OUString > SvtFontOptions_Impl::impl_GetPropertyNames()
253 : {
254 : // Build list of configuration key names.
255 : const OUString pProperties[] =
256 : {
257 : PROPERTYNAME_REPLACEMENTTABLE ,
258 : PROPERTYNAME_FONTHISTORY ,
259 : PROPERTYNAME_FONTWYSIWYG ,
260 208 : };
261 : // Initialize return sequence with these list ...
262 52 : const Sequence< OUString > seqPropertyNames( pProperties, PROPERTYCOUNT );
263 : // ... and return it.
264 208 : return seqPropertyNames;
265 : }
266 :
267 : // initialize static member
268 : // DON'T DO IT IN YOUR HEADER!
269 : // see definition for further information
270 :
271 : SvtFontOptions_Impl* SvtFontOptions::m_pDataContainer = NULL;
272 : sal_Int32 SvtFontOptions::m_nRefCount = 0;
273 :
274 : // constructor
275 :
276 2008 : SvtFontOptions::SvtFontOptions()
277 : {
278 : // Global access, must be guarded (multithreading!).
279 2008 : MutexGuard aGuard( impl_GetOwnStaticMutex() );
280 : // Increase our refcount ...
281 2008 : ++m_nRefCount;
282 : // ... and initialize our data container only if it not already exist!
283 2008 : if( m_pDataContainer == NULL )
284 : {
285 52 : m_pDataContainer = new SvtFontOptions_Impl;
286 :
287 52 : ItemHolder1::holdConfigItem(E_FONTOPTIONS);
288 2008 : }
289 2008 : }
290 :
291 : // destructor
292 :
293 4068 : SvtFontOptions::~SvtFontOptions()
294 : {
295 : // Global access, must be guarded (multithreading!)
296 2008 : MutexGuard aGuard( impl_GetOwnStaticMutex() );
297 : // Decrease our refcount.
298 2008 : --m_nRefCount;
299 : // If last instance was deleted ...
300 : // we must destroy our static data container!
301 2008 : if( m_nRefCount <= 0 )
302 : {
303 52 : delete m_pDataContainer;
304 52 : m_pDataContainer = NULL;
305 2008 : }
306 2060 : }
307 :
308 : // public method
309 :
310 1956 : bool SvtFontOptions::IsFontHistoryEnabled() const
311 : {
312 1956 : MutexGuard aGuard( impl_GetOwnStaticMutex() );
313 1956 : return m_pDataContainer->IsFontHistoryEnabled();
314 : }
315 :
316 : // public method
317 :
318 0 : void SvtFontOptions::EnableFontHistory( bool bState )
319 : {
320 0 : MutexGuard aGuard( impl_GetOwnStaticMutex() );
321 0 : m_pDataContainer->EnableFontHistory( bState );
322 0 : }
323 :
324 : // public method
325 :
326 1956 : bool SvtFontOptions::IsFontWYSIWYGEnabled() const
327 : {
328 1956 : MutexGuard aGuard( impl_GetOwnStaticMutex() );
329 1956 : return m_pDataContainer->IsFontWYSIWYGEnabled();
330 : }
331 :
332 : // public method
333 :
334 0 : void SvtFontOptions::EnableFontWYSIWYG( bool bState )
335 : {
336 0 : MutexGuard aGuard( impl_GetOwnStaticMutex() );
337 0 : m_pDataContainer->EnableFontWYSIWYG( bState );
338 0 : }
339 :
340 : namespace
341 : {
342 : class theFontOptionsMutex : public rtl::Static<osl::Mutex, theFontOptionsMutex> {};
343 : }
344 :
345 : // private method
346 :
347 7928 : Mutex& SvtFontOptions::impl_GetOwnStaticMutex()
348 : {
349 7928 : return theFontOptionsMutex::get();
350 : }
351 :
352 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|