1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_unotools.hxx"
26 #ifndef GCC
27 #endif
28 
29 //_________________________________________________________________________________________________________________
30 //	includes
31 //_________________________________________________________________________________________________________________
32 
33 #include <unotools/fontoptions.hxx>
34 #include <unotools/configmgr.hxx>
35 #include <unotools/configitem.hxx>
36 #include <tools/debug.hxx>
37 #include <com/sun/star/uno/Any.hxx>
38 #include <com/sun/star/uno/Sequence.hxx>
39 
40 #include <rtl/logfile.hxx>
41 #include "itemholder1.hxx"
42 
43 //_________________________________________________________________________________________________________________
44 //	namespaces
45 //_________________________________________________________________________________________________________________
46 
47 using namespace ::utl					;
48 using namespace ::rtl					;
49 using namespace ::osl					;
50 using namespace ::com::sun::star::uno	;
51 
52 //_________________________________________________________________________________________________________________
53 //	const
54 //_________________________________________________________________________________________________________________
55 
56 #define	ROOTNODE_FONT						OUString(RTL_CONSTASCII_USTRINGPARAM("Office.Common/Font"			))
57 
58 #define	PROPERTYNAME_REPLACEMENTTABLE		OUString(RTL_CONSTASCII_USTRINGPARAM("Substitution/Replacement"		))
59 #define	PROPERTYNAME_FONTHISTORY			OUString(RTL_CONSTASCII_USTRINGPARAM("View/History"					))
60 #define	PROPERTYNAME_FONTWYSIWYG			OUString(RTL_CONSTASCII_USTRINGPARAM("View/ShowFontBoxWYSIWYG"		))
61 
62 #define	PROPERTYHANDLE_REPLACEMENTTABLE		0
63 #define	PROPERTYHANDLE_FONTHISTORY			1
64 #define	PROPERTYHANDLE_FONTWYSIWYG			2
65 
66 #define	PROPERTYCOUNT						3
67 
68 //_________________________________________________________________________________________________________________
69 //	private declarations!
70 //_________________________________________________________________________________________________________________
71 
72 class SvtFontOptions_Impl : public ConfigItem
73 {
74 	//-------------------------------------------------------------------------------------------------------------
75 	//	public methods
76 	//-------------------------------------------------------------------------------------------------------------
77 
78 	public:
79 
80 		//---------------------------------------------------------------------------------------------------------
81 		//	constructor / destructor
82 		//---------------------------------------------------------------------------------------------------------
83 
84 		 SvtFontOptions_Impl();
85 		~SvtFontOptions_Impl();
86 
87 		//---------------------------------------------------------------------------------------------------------
88 		//	overloaded methods of baseclass
89 		//---------------------------------------------------------------------------------------------------------
90 
91 		/*-****************************************************************************************************//**
92 			@short		called for notify of configmanager
93 			@descr		These method is called from the ConfigManager before application ends or from the
94 			 			PropertyChangeListener if the sub tree broadcasts changes. You must update your
95 						internal values.
96 
97 			@seealso	baseclass ConfigItem
98 
99 			@param		"seqPropertyNames" is the list of properties which should be updated.
100 			@return		-
101 
102 			@onerror	-
103 		*//*-*****************************************************************************************************/
104 
105     	virtual void Notify( const Sequence< OUString >& seqPropertyNames );
106 
107 		/*-****************************************************************************************************//**
108 			@short		write changes to configuration
109 			@descr		These method writes the changed values into the sub tree
110 						and should always called in our destructor to guarantee consistency of config data.
111 
112 			@seealso	baseclass ConfigItem
113 
114 			@param		-
115 			@return		-
116 
117 			@onerror	-
118 		*//*-*****************************************************************************************************/
119 
120     	virtual void Commit();
121 
122 		//---------------------------------------------------------------------------------------------------------
123 		//	public interface
124 		//---------------------------------------------------------------------------------------------------------
125 
126 		/*-****************************************************************************************************//**
127 			@short		access method to get internal values
128 			@descr		These method gives us a chance to regulate access to our internal values.
129 						It's not used in the moment - but it's possible for the future!
130 
131 			@seealso	-
132 
133 			@param		-
134 			@return		-
135 
136 			@onerror	-
137 		*//*-*****************************************************************************************************/
138 
139 		sal_Bool	IsReplacementTableEnabled	(					) const	;
140 		void		EnableReplacementTable		( sal_Bool bState	)		;
141 
142 		sal_Bool	IsFontHistoryEnabled		(					) const	;
143 		void		EnableFontHistory			( sal_Bool bState	)		;
144 
145 		sal_Bool	IsFontWYSIWYGEnabled		(					) const	;
146 		void		EnableFontWYSIWYG			( sal_Bool bState	)		;
147 
148 	//-------------------------------------------------------------------------------------------------------------
149 	//	private methods
150 	//-------------------------------------------------------------------------------------------------------------
151 
152 	private:
153 
154 		/*-****************************************************************************************************//**
155 			@short		return list of key names of our configuration management which represent our module tree
156 			@descr		These methods return a static const list of key names. We need it to get needed values from our
157 						configuration management.
158 
159 			@seealso	-
160 
161 			@param		-
162 			@return		A list of needed configuration keys is returned.
163 
164 			@onerror	-
165 		*//*-*****************************************************************************************************/
166 
167 		static Sequence< OUString > impl_GetPropertyNames();
168 
169 	//-------------------------------------------------------------------------------------------------------------
170 	//	private member
171 	//-------------------------------------------------------------------------------------------------------------
172 
173 	private:
174 
175 		sal_Bool		m_bReplacementTable		;
176 		sal_Bool		m_bFontHistory			;
177 		sal_Bool		m_bFontWYSIWYG			;
178 };
179 
180 //_________________________________________________________________________________________________________________
181 //	definitions
182 //_________________________________________________________________________________________________________________
183 
184 //*****************************************************************************************************************
185 //	constructor
186 //*****************************************************************************************************************
SvtFontOptions_Impl()187 SvtFontOptions_Impl::SvtFontOptions_Impl()
188 	// Init baseclasses first
189     :	ConfigItem			( ROOTNODE_FONT	)
190 	// Init member then.
191 	,	m_bReplacementTable	( sal_False		)
192 	,	m_bFontHistory		( sal_False		)
193 	,	m_bFontWYSIWYG		( sal_False		)
194 {
195 	// Use our static list of configuration keys to get his values.
196 	Sequence< OUString >	seqNames	= impl_GetPropertyNames	(			);
197 	Sequence< Any >			seqValues	= GetProperties			( seqNames	);
198 
199 	// Safe impossible cases.
200 	// We need values from ALL configuration keys.
201 	// Follow assignment use order of values in relation to our list of key names!
202 	DBG_ASSERT( !(seqNames.getLength()!=seqValues.getLength()), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nI miss some values of configuration keys!\n" );
203 
204 	// Copy values from list in right order to our internal member.
205 	sal_Int32 nPropertyCount = seqValues.getLength();
206 	for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty )
207 	{
208 		// Safe impossible cases.
209 		// Check any for valid value.
210 		DBG_ASSERT( !(seqValues[nProperty].hasValue()==sal_False), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nInvalid property value detected!\n" );
211         switch( nProperty )
212         {
213             case PROPERTYHANDLE_REPLACEMENTTABLE	:	{
214 															DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nWho has changed the value type of \"Office.Common\\Font\\Substitution\\Replacement\"?" );
215 															seqValues[nProperty] >>= m_bReplacementTable;
216 														}
217 														break;
218             case PROPERTYHANDLE_FONTHISTORY			:	{
219 															DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nWho has changed the value type of \"Office.Common\\Font\\View\\History\"?" );
220 															seqValues[nProperty] >>= m_bFontHistory;
221 														}
222 														break;
223             case PROPERTYHANDLE_FONTWYSIWYG			:	{
224 															DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nWho has changed the value type of \"Office.Common\\Font\\View\\ShowFontBoxWYSIWYG\"?" );
225 															seqValues[nProperty] >>= m_bFontWYSIWYG;
226 														}
227 														break;
228         }
229 	}
230 
231 	// Enable notification mechanism of our baseclass.
232 	// We need it to get information about changes outside these class on our used configuration keys!
233 	EnableNotification( seqNames );
234 }
235 
236 //*****************************************************************************************************************
237 //	destructor
238 //*****************************************************************************************************************
~SvtFontOptions_Impl()239 SvtFontOptions_Impl::~SvtFontOptions_Impl()
240 {
241 	// We must save our current values .. if user forget it!
242 	if( IsModified() == sal_True )
243 	{
244 		Commit();
245 	}
246 }
247 
248 //*****************************************************************************************************************
249 //	public method
250 //*****************************************************************************************************************
Notify(const Sequence<OUString> & seqPropertyNames)251 void SvtFontOptions_Impl::Notify( const Sequence< OUString >& seqPropertyNames )
252 {
253 	// Use given list of updated properties to get his values from configuration directly!
254 	Sequence< Any > seqValues = GetProperties( seqPropertyNames );
255 	// Safe impossible cases.
256 	// We need values from ALL notified configuration keys.
257 	DBG_ASSERT( !(seqPropertyNames.getLength()!=seqValues.getLength()), "SvtFontOptions_Impl::Notify()\nI miss some values of configuration keys!\n" );
258 	// Step over list of property names and get right value from coreesponding value list to set it on internal members!
259 	sal_Int32 nCount = seqPropertyNames.getLength();
260 	for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
261 	{
262 		if( seqPropertyNames[nProperty] == PROPERTYNAME_REPLACEMENTTABLE )
263 		{
264 			DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\Font\\Substitution\\Replacement\"?" );
265 			seqValues[nProperty] >>= m_bReplacementTable;
266 		}
267 		else
268 		if( seqPropertyNames[nProperty] == PROPERTYNAME_FONTHISTORY )
269 		{
270 			DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\Font\\View\\History\"?" );
271 			seqValues[nProperty] >>= m_bFontHistory;
272 		}
273 		else
274 		if( seqPropertyNames[nProperty] == PROPERTYNAME_FONTWYSIWYG )
275 		{
276 			DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\Font\\View\\ShowFontBoxWYSIWYG\"?" );
277 			seqValues[nProperty] >>= m_bFontWYSIWYG;
278 		}
279         #if OSL_DEBUG_LEVEL > 1
280 		else DBG_ASSERT( sal_False, "SvtFontOptions_Impl::Notify()\nUnkown property detected ... I can't handle these!\n" );
281 		#endif
282 	}
283 }
284 
285 //*****************************************************************************************************************
286 //	public method
287 //*****************************************************************************************************************
Commit()288 void SvtFontOptions_Impl::Commit()
289 {
290 	// Get names of supported properties, create a list for values and copy current values to it.
291 	Sequence< OUString >	seqNames	= impl_GetPropertyNames();
292 	sal_Int32				nCount		= seqNames.getLength();
293 	Sequence< Any >			seqValues	( nCount );
294 	for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
295 	{
296         switch( nProperty )
297         {
298             case PROPERTYHANDLE_REPLACEMENTTABLE	:	{
299                 											seqValues[nProperty] <<= m_bReplacementTable;
300 														}
301                 										break;
302             case PROPERTYHANDLE_FONTHISTORY			:	{
303                 											seqValues[nProperty] <<= m_bFontHistory;
304 														}
305                 										break;
306             case PROPERTYHANDLE_FONTWYSIWYG			:	{
307                 											seqValues[nProperty] <<= m_bFontWYSIWYG;
308 														}
309                 										break;
310         }
311 	}
312 	// Set properties in configuration.
313 	PutProperties( seqNames, seqValues );
314 }
315 
316 //*****************************************************************************************************************
317 //	public method
318 //*****************************************************************************************************************
IsReplacementTableEnabled() const319 sal_Bool SvtFontOptions_Impl::IsReplacementTableEnabled() const
320 {
321 	return m_bReplacementTable;
322 }
323 
324 //*****************************************************************************************************************
325 //	public method
326 //*****************************************************************************************************************
EnableReplacementTable(sal_Bool bState)327 void SvtFontOptions_Impl::EnableReplacementTable( sal_Bool bState )
328 {
329 	m_bReplacementTable = bState;
330 	SetModified();
331 }
332 
333 //*****************************************************************************************************************
334 //	public method
335 //*****************************************************************************************************************
IsFontHistoryEnabled() const336 sal_Bool SvtFontOptions_Impl::IsFontHistoryEnabled() const
337 {
338 	return m_bFontHistory;
339 }
340 
341 //*****************************************************************************************************************
342 //	public method
343 //*****************************************************************************************************************
EnableFontHistory(sal_Bool bState)344 void SvtFontOptions_Impl::EnableFontHistory( sal_Bool bState )
345 {
346 	m_bFontHistory = bState;
347 	SetModified();
348 }
349 
350 //*****************************************************************************************************************
351 //	public method
352 //*****************************************************************************************************************
IsFontWYSIWYGEnabled() const353 sal_Bool SvtFontOptions_Impl::IsFontWYSIWYGEnabled() const
354 {
355 	return m_bFontWYSIWYG;
356 }
357 
358 //*****************************************************************************************************************
359 //	public method
360 //*****************************************************************************************************************
EnableFontWYSIWYG(sal_Bool bState)361 void SvtFontOptions_Impl::EnableFontWYSIWYG( sal_Bool bState )
362 {
363 	m_bFontWYSIWYG = bState;
364 	SetModified();
365 }
366 
367 //*****************************************************************************************************************
368 //	private method
369 //*****************************************************************************************************************
impl_GetPropertyNames()370 Sequence< OUString > SvtFontOptions_Impl::impl_GetPropertyNames()
371 {
372 	// Build static list of configuration key names.
373 	static const OUString pProperties[] =
374 	{
375 		PROPERTYNAME_REPLACEMENTTABLE	,
376 		PROPERTYNAME_FONTHISTORY		,
377 		PROPERTYNAME_FONTWYSIWYG		,
378 	};
379 	// Initialize return sequence with these list ...
380 	static const Sequence< OUString > seqPropertyNames( pProperties, PROPERTYCOUNT );
381 	// ... and return it.
382 	return seqPropertyNames;
383 }
384 
385 //*****************************************************************************************************************
386 //	initialize static member
387 //	DON'T DO IT IN YOUR HEADER!
388 //	see definition for further informations
389 //*****************************************************************************************************************
390 SvtFontOptions_Impl*	SvtFontOptions::m_pDataContainer	= NULL	;
391 sal_Int32				SvtFontOptions::m_nRefCount			= 0		;
392 
393 //*****************************************************************************************************************
394 //	constructor
395 //*****************************************************************************************************************
SvtFontOptions()396 SvtFontOptions::SvtFontOptions()
397 {
398     // Global access, must be guarded (multithreading!).
399     MutexGuard aGuard( impl_GetOwnStaticMutex() );
400 	// Increase our refcount ...
401 	++m_nRefCount;
402 	// ... and initialize our data container only if it not already exist!
403     if( m_pDataContainer == NULL )
404 	{
405         RTL_LOGFILE_CONTEXT(aLog, "unotools ( ??? ) ::SvtFontOptions_Impl::ctor()");
406         m_pDataContainer = new SvtFontOptions_Impl;
407 
408         ItemHolder1::holdConfigItem(E_FONTOPTIONS);
409 	}
410 }
411 
412 //*****************************************************************************************************************
413 //	destructor
414 //*****************************************************************************************************************
~SvtFontOptions()415 SvtFontOptions::~SvtFontOptions()
416 {
417     // Global access, must be guarded (multithreading!)
418     MutexGuard aGuard( impl_GetOwnStaticMutex() );
419 	// Decrease our refcount.
420 	--m_nRefCount;
421 	// If last instance was deleted ...
422 	// we must destroy our static data container!
423     if( m_nRefCount <= 0 )
424 	{
425 		delete m_pDataContainer;
426 		m_pDataContainer = NULL;
427 	}
428 }
429 
430 //*****************************************************************************************************************
431 //	public method
432 //*****************************************************************************************************************
IsReplacementTableEnabled() const433 sal_Bool SvtFontOptions::IsReplacementTableEnabled() const
434 {
435     MutexGuard aGuard( impl_GetOwnStaticMutex() );
436 	return m_pDataContainer->IsReplacementTableEnabled();
437 }
438 
439 //*****************************************************************************************************************
440 //	public method
441 //*****************************************************************************************************************
EnableReplacementTable(sal_Bool bState)442 void SvtFontOptions::EnableReplacementTable( sal_Bool bState )
443 {
444     MutexGuard aGuard( impl_GetOwnStaticMutex() );
445 	m_pDataContainer->EnableReplacementTable( bState );
446 }
447 
448 //*****************************************************************************************************************
449 //	public method
450 //*****************************************************************************************************************
IsFontHistoryEnabled() const451 sal_Bool SvtFontOptions::IsFontHistoryEnabled() const
452 {
453     MutexGuard aGuard( impl_GetOwnStaticMutex() );
454 	return m_pDataContainer->IsFontHistoryEnabled();
455 }
456 
457 //*****************************************************************************************************************
458 //	public method
459 //*****************************************************************************************************************
EnableFontHistory(sal_Bool bState)460 void SvtFontOptions::EnableFontHistory( sal_Bool bState )
461 {
462     MutexGuard aGuard( impl_GetOwnStaticMutex() );
463 	m_pDataContainer->EnableFontHistory( bState );
464 }
465 
466 //*****************************************************************************************************************
467 //	public method
468 //*****************************************************************************************************************
IsFontWYSIWYGEnabled() const469 sal_Bool SvtFontOptions::IsFontWYSIWYGEnabled() const
470 {
471     MutexGuard aGuard( impl_GetOwnStaticMutex() );
472 	return m_pDataContainer->IsFontWYSIWYGEnabled();
473 }
474 
475 //*****************************************************************************************************************
476 //	public method
477 //*****************************************************************************************************************
EnableFontWYSIWYG(sal_Bool bState)478 void SvtFontOptions::EnableFontWYSIWYG( sal_Bool bState )
479 {
480     MutexGuard aGuard( impl_GetOwnStaticMutex() );
481 	m_pDataContainer->EnableFontWYSIWYG( bState );
482 }
483 
484 //*****************************************************************************************************************
485 //	private method
486 //*****************************************************************************************************************
impl_GetOwnStaticMutex()487 Mutex& SvtFontOptions::impl_GetOwnStaticMutex()
488 {
489 	// Initialize static mutex only for one time!
490     static Mutex* pMutex = NULL;
491 	// If these method first called (Mutex not already exist!) ...
492     if( pMutex == NULL )
493     {
494 		// ... we must create a new one. Protect follow code with the global mutex -
495 		// It must be - we create a static variable!
496         MutexGuard aGuard( Mutex::getGlobalMutex() );
497 		// We must check our pointer again - because it can be that another instance of our class will be faster than these!
498         if( pMutex == NULL )
499         {
500 			// Create the new mutex and set it for return on static variable.
501             static Mutex aMutex;
502             pMutex = &aMutex;
503         }
504     }
505 	// Return new created or already existing mutex object.
506     return *pMutex;
507 }
508