1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_unotools.hxx"
30 #ifndef GCC
31 #endif
32 
33 //_________________________________________________________________________________________________________________
34 //	includes
35 //_________________________________________________________________________________________________________________
36 
37 #include <unotools/cacheoptions.hxx>
38 #include <unotools/configmgr.hxx>
39 #include <unotools/configitem.hxx>
40 #include <tools/debug.hxx>
41 #include <com/sun/star/uno/Any.hxx>
42 #include <com/sun/star/uno/Sequence.hxx>
43 
44 //_________________________________________________________________________________________________________________
45 //	namespaces
46 //_________________________________________________________________________________________________________________
47 
48 using namespace ::utl;
49 using namespace ::rtl;
50 using namespace ::osl;
51 using namespace ::com::sun::star::uno;
52 
53 //_________________________________________________________________________________________________________________
54 //	const
55 //_________________________________________________________________________________________________________________
56 
57 #define	ROOTNODE_START			   			OUString(RTL_CONSTASCII_USTRINGPARAM("Office.Common/Cache"	))
58 #define	DEFAULT_WRITEROLE					20
59 #define	DEFAULT_DRAWINGOLE					20
60 #define	DEFAULT_GRFMGR_TOTALSIZE			10000000
61 #define	DEFAULT_GRFMGR_OBJECTSIZE			2400000
62 #define	DEFAULT_GRFMGR_OBJECTRELEASE	    600
63 
64 #define	PROPERTYNAME_WRITEROLE				OUString(RTL_CONSTASCII_USTRINGPARAM("Writer/OLE_Objects"))
65 #define	PROPERTYNAME_DRAWINGOLE				OUString(RTL_CONSTASCII_USTRINGPARAM("DrawingEngine/OLE_Objects"))
66 #define	PROPERTYNAME_GRFMGR_TOTALSIZE		OUString(RTL_CONSTASCII_USTRINGPARAM("GraphicManager/TotalCacheSize"))
67 #define	PROPERTYNAME_GRFMGR_OBJECTSIZE		OUString(RTL_CONSTASCII_USTRINGPARAM("GraphicManager/ObjectCacheSize"))
68 #define PROPERTYNAME_GRFMGR_OBJECTRELEASE   OUString(RTL_CONSTASCII_USTRINGPARAM("GraphicManager/ObjectReleaseTime"))
69 
70 #define	PROPERTYHANDLE_WRITEROLE			0
71 #define	PROPERTYHANDLE_DRAWINGOLE			1
72 #define	PROPERTYHANDLE_GRFMGR_TOTALSIZE		2
73 #define	PROPERTYHANDLE_GRFMGR_OBJECTSIZE	3
74 #define PROPERTYHANDLE_GRFMGR_OBJECTRELEASE 4
75 
76 #define	PROPERTYCOUNT						5
77 
78 class SvtCacheOptions_Impl : public ConfigItem
79 {
80 public:
81 
82 //---------------------------------------------------------------------------------------------------------
83 //	constructor / destructor
84 //---------------------------------------------------------------------------------------------------------
85 
86 	 SvtCacheOptions_Impl();
87 	~SvtCacheOptions_Impl();
88 
89 //---------------------------------------------------------------------------------------------------------
90 //	overloaded methods of baseclass
91 //---------------------------------------------------------------------------------------------------------
92 
93 	virtual void	Commit();
94     virtual void    Notify( const com::sun::star::uno::Sequence< rtl::OUString >& aPropertyNames );
95 
96 //---------------------------------------------------------------------------------------------------------
97 //	public interface
98 //---------------------------------------------------------------------------------------------------------
99 
100 	sal_Int32		GetWriterOLE_Objects() const;
101 	sal_Int32		GetDrawingEngineOLE_Objects() const;
102 	sal_Int32		GetGraphicManagerTotalCacheSize() const;
103 	sal_Int32		GetGraphicManagerObjectCacheSize() const;
104 	sal_Int32		GetGraphicManagerObjectReleaseTime() const;
105 
106 	void			SetWriterOLE_Objects( sal_Int32 nObjects );
107 	void			SetDrawingEngineOLE_Objects( sal_Int32 nObjects );
108 	void			SetGraphicManagerTotalCacheSize( sal_Int32 nTotalCacheSize );
109 	void			SetGraphicManagerObjectCacheSize( sal_Int32 nObjectCacheSize );
110 	void            SetGraphicManagerObjectReleaseTime( sal_Int32 nReleaseTimeSeconds );
111 
112 //-------------------------------------------------------------------------------------------------------------
113 //	private methods
114 //-------------------------------------------------------------------------------------------------------------
115 
116 private:
117 
118 	static Sequence< OUString > impl_GetPropertyNames();
119 
120 //-------------------------------------------------------------------------------------------------------------
121 //	private member
122 //-------------------------------------------------------------------------------------------------------------
123 
124 private:
125 
126 		sal_Int32	mnWriterOLE;
127 		sal_Int32	mnDrawingOLE;
128 		sal_Int32	mnGrfMgrTotalSize;
129 		sal_Int32	mnGrfMgrObjectSize;
130         sal_Int32   mnGrfMgrObjectRelease;
131 };
132 
133 //_________________________________________________________________________________________________________________
134 //	definitions
135 //_________________________________________________________________________________________________________________
136 
137 //*****************************************************************************************************************
138 //	constructor
139 //*****************************************************************************************************************
140 SvtCacheOptions_Impl::SvtCacheOptions_Impl() :
141 	ConfigItem( ROOTNODE_START	),
142 	mnWriterOLE( DEFAULT_WRITEROLE ),
143 	mnDrawingOLE( DEFAULT_DRAWINGOLE ),
144 	mnGrfMgrTotalSize( DEFAULT_GRFMGR_TOTALSIZE ),
145 	mnGrfMgrObjectSize( DEFAULT_GRFMGR_OBJECTSIZE ),
146     mnGrfMgrObjectRelease( DEFAULT_GRFMGR_OBJECTRELEASE )
147 {
148 	Sequence< OUString >	seqNames( impl_GetPropertyNames() );
149 	Sequence< Any >			seqValues	= GetProperties( seqNames )	;
150 
151 	DBG_ASSERT( !(seqNames.getLength()!=seqValues.getLength()), "SvtCacheOptions_Impl::SvtCacheOptions_Impl()\nI miss some values of configuration keys!\n" );
152 
153 	// Copy values from list in right order to ouer internal member.
154 	sal_Int32 nPropertyCount = seqValues.getLength();
155 	sal_Int32 nProperty	= 0;
156 
157 	for( nProperty=0; nProperty<nPropertyCount; ++nProperty )
158 	{
159 		if( seqValues[ nProperty ].hasValue() )
160 		{
161 			switch( nProperty )
162 			{
163 				case PROPERTYHANDLE_WRITEROLE:
164 				{
165 					if( seqValues[ nProperty ].getValueTypeClass() == TypeClass_LONG )
166 						seqValues[nProperty] >>= mnWriterOLE;
167 				}
168 				break;
169 
170 				case PROPERTYHANDLE_DRAWINGOLE:
171 				{
172 					if( seqValues[ nProperty ].getValueTypeClass() == TypeClass_LONG )
173 						seqValues[nProperty] >>= mnDrawingOLE;
174 				}
175 				break;
176 
177 				case PROPERTYHANDLE_GRFMGR_TOTALSIZE:
178 				{
179 					if( seqValues[ nProperty ].getValueTypeClass() == TypeClass_LONG )
180 						seqValues[nProperty] >>= mnGrfMgrTotalSize;
181 				}
182 				break;
183 
184 				case PROPERTYHANDLE_GRFMGR_OBJECTSIZE:
185 				{
186 					if( seqValues[ nProperty ].getValueTypeClass() == TypeClass_LONG )
187 						seqValues[nProperty] >>= mnGrfMgrObjectSize;
188 				}
189 				break;
190 
191 				case PROPERTYHANDLE_GRFMGR_OBJECTRELEASE:
192 				{
193 					if( seqValues[ nProperty ].getValueTypeClass() == TypeClass_LONG )
194 						seqValues[nProperty] >>= mnGrfMgrObjectRelease;
195 				}
196 				break;
197 			}
198 		}
199 	}
200 }
201 
202 //*****************************************************************************************************************
203 //	destructor
204 //*****************************************************************************************************************
205 SvtCacheOptions_Impl::~SvtCacheOptions_Impl()
206 {
207 	if( IsModified() )
208 		Commit();
209 }
210 
211 //*****************************************************************************************************************
212 //	Commit
213 //*****************************************************************************************************************
214 void SvtCacheOptions_Impl::Commit()
215 {
216 	Sequence< OUString >	aSeqNames( impl_GetPropertyNames() );
217 	Sequence< Any >			aSeqValues( aSeqNames.getLength() );
218 
219 	for( sal_Int32 nProperty = 0, nCount = aSeqNames.getLength(); nProperty < nCount; ++nProperty )
220 	{
221         switch( nProperty )
222         {
223             case PROPERTYHANDLE_WRITEROLE:
224 				aSeqValues[nProperty] <<= mnWriterOLE;
225 			break;
226 
227             case PROPERTYHANDLE_DRAWINGOLE:
228 				aSeqValues[nProperty] <<= mnDrawingOLE;
229 			break;
230 
231             case PROPERTYHANDLE_GRFMGR_TOTALSIZE:
232 				aSeqValues[nProperty] <<= mnGrfMgrTotalSize;
233 			break;
234 
235             case PROPERTYHANDLE_GRFMGR_OBJECTSIZE:
236 				aSeqValues[nProperty] <<= mnGrfMgrObjectSize;
237 			break;
238 
239             case PROPERTYHANDLE_GRFMGR_OBJECTRELEASE:
240 				aSeqValues[nProperty] <<= mnGrfMgrObjectRelease;
241 			break;
242         }
243 	}
244 
245 	PutProperties( aSeqNames, aSeqValues );
246 }
247 
248 void SvtCacheOptions_Impl::Notify( const Sequence< rtl::OUString >&  )
249 {
250 }
251 
252 //*****************************************************************************************************************
253 //	public method
254 //*****************************************************************************************************************
255 sal_Int32 SvtCacheOptions_Impl::GetWriterOLE_Objects() const
256 {
257 	return mnWriterOLE;
258 }
259 
260 //*****************************************************************************************************************
261 //	public method
262 //*****************************************************************************************************************
263 sal_Int32 SvtCacheOptions_Impl::GetDrawingEngineOLE_Objects() const
264 {
265 	return mnDrawingOLE;
266 }
267 
268 //*****************************************************************************************************************
269 //	public method
270 //*****************************************************************************************************************
271 sal_Int32 SvtCacheOptions_Impl::GetGraphicManagerTotalCacheSize() const
272 {
273 	return mnGrfMgrTotalSize;
274 }
275 
276 //*****************************************************************************************************************
277 //	public method
278 //*****************************************************************************************************************
279 sal_Int32 SvtCacheOptions_Impl::GetGraphicManagerObjectCacheSize() const
280 {
281 	return mnGrfMgrObjectSize;
282 }
283 
284 //*****************************************************************************************************************
285 //	public method
286 //*****************************************************************************************************************
287 sal_Int32 SvtCacheOptions_Impl::GetGraphicManagerObjectReleaseTime() const
288 {
289 	return mnGrfMgrObjectRelease;
290 }
291 
292 //*****************************************************************************************************************
293 //	public method
294 //*****************************************************************************************************************
295 void SvtCacheOptions_Impl::SetWriterOLE_Objects( sal_Int32 nWriterOLE )
296 {
297 	mnWriterOLE = nWriterOLE;
298 	SetModified();
299 }
300 
301 //*****************************************************************************************************************
302 //	public method
303 //*****************************************************************************************************************
304 void SvtCacheOptions_Impl::SetDrawingEngineOLE_Objects( sal_Int32 nDrawingOLE )
305 {
306 	mnDrawingOLE = nDrawingOLE;
307 	SetModified();
308 }
309 
310 //*****************************************************************************************************************
311 //	public method
312 //*****************************************************************************************************************
313 void SvtCacheOptions_Impl::SetGraphicManagerTotalCacheSize( sal_Int32 nGrfMgrTotalSize )
314 {
315 	mnGrfMgrTotalSize = nGrfMgrTotalSize;
316 	SetModified();
317 }
318 
319 //*****************************************************************************************************************
320 //	public method
321 //*****************************************************************************************************************
322 void SvtCacheOptions_Impl::SetGraphicManagerObjectCacheSize( sal_Int32 nGrfMgrObjectSize )
323 {
324 	mnGrfMgrObjectSize = nGrfMgrObjectSize;
325 	SetModified();
326 }
327 
328 //*****************************************************************************************************************
329 //	public method
330 //*****************************************************************************************************************
331 void SvtCacheOptions_Impl::SetGraphicManagerObjectReleaseTime( sal_Int32 nGrfMgrObjectReleaseTime )
332 {
333 	mnGrfMgrObjectRelease = nGrfMgrObjectReleaseTime;
334 	SetModified();
335 }
336 
337 //*****************************************************************************************************************
338 //	private method
339 //*****************************************************************************************************************
340 Sequence< OUString > SvtCacheOptions_Impl::impl_GetPropertyNames()
341 {
342 	// Build static list of configuration key names.
343 	static const OUString pProperties[] =
344 	{
345 		PROPERTYNAME_WRITEROLE,
346 		PROPERTYNAME_DRAWINGOLE,
347 		PROPERTYNAME_GRFMGR_TOTALSIZE,
348 		PROPERTYNAME_GRFMGR_OBJECTSIZE,
349 		PROPERTYNAME_GRFMGR_OBJECTRELEASE
350 	};
351 	// Initialize return sequence with these list ...
352 	static const Sequence< OUString > seqPropertyNames( pProperties, PROPERTYCOUNT );
353 	// ... and return it.
354 	return seqPropertyNames;
355 }
356 
357 //*****************************************************************************************************************
358 //	initialize static member
359 //	DON'T DO IT IN YOUR HEADER!
360 //	see definition for further informations
361 //*****************************************************************************************************************
362 SvtCacheOptions_Impl*	SvtCacheOptions::m_pDataContainer = NULL;
363 sal_Int32				SvtCacheOptions::m_nRefCount = 0;
364 
365 //*****************************************************************************************************************
366 //	constructor
367 //*****************************************************************************************************************
368 SvtCacheOptions::SvtCacheOptions()
369 {
370     // Global access, must be guarded (multithreading!).
371     MutexGuard aGuard( GetOwnStaticMutex() );
372 	// Increase ouer refcount ...
373 	++m_nRefCount;
374 	// ... and initialize ouer data container only if it not already!
375     if( m_pDataContainer == NULL )
376 	{
377         m_pDataContainer = new SvtCacheOptions_Impl();
378 	}
379 }
380 
381 //*****************************************************************************************************************
382 //	destructor
383 //*****************************************************************************************************************
384 SvtCacheOptions::~SvtCacheOptions()
385 {
386     // Global access, must be guarded (multithreading!)
387     MutexGuard aGuard( GetOwnStaticMutex() );
388 	// Decrease ouer refcount.
389 	--m_nRefCount;
390 	// If last instance was deleted ...
391 	// we must destroy ouer static data container!
392     if( m_nRefCount <= 0 )
393 	{
394 		delete m_pDataContainer;
395 		m_pDataContainer = NULL;
396 	}
397 }
398 
399 //*****************************************************************************************************************
400 //	public method
401 //*****************************************************************************************************************
402 sal_Int32 SvtCacheOptions::GetWriterOLE_Objects() const
403 {
404     MutexGuard aGuard( GetOwnStaticMutex() );
405 	return m_pDataContainer->GetWriterOLE_Objects();
406 }
407 
408 //*****************************************************************************************************************
409 //	public method
410 //*****************************************************************************************************************
411 sal_Int32 SvtCacheOptions::GetDrawingEngineOLE_Objects() const
412 {
413     MutexGuard aGuard( GetOwnStaticMutex() );
414 	return m_pDataContainer->GetDrawingEngineOLE_Objects();
415 }
416 
417 //*****************************************************************************************************************
418 //	public method
419 //*****************************************************************************************************************
420 sal_Int32 SvtCacheOptions::GetGraphicManagerTotalCacheSize() const
421 {
422     MutexGuard aGuard( GetOwnStaticMutex() );
423 	return m_pDataContainer->GetGraphicManagerTotalCacheSize();
424 }
425 
426 //*****************************************************************************************************************
427 //	public method
428 //*****************************************************************************************************************
429 sal_Int32 SvtCacheOptions::GetGraphicManagerObjectCacheSize() const
430 {
431     MutexGuard aGuard( GetOwnStaticMutex() );
432 	return m_pDataContainer->GetGraphicManagerObjectCacheSize();
433 }
434 
435 //*****************************************************************************************************************
436 //	public method
437 //*****************************************************************************************************************
438 sal_Int32 SvtCacheOptions::GetGraphicManagerObjectReleaseTime() const
439 {
440     MutexGuard aGuard( GetOwnStaticMutex() );
441 	return m_pDataContainer->GetGraphicManagerObjectReleaseTime();
442 }
443 
444 //*****************************************************************************************************************
445 //	public method
446 //*****************************************************************************************************************
447 void SvtCacheOptions::SetWriterOLE_Objects( sal_Int32 nWriterOLE )
448 {
449     MutexGuard aGuard( GetOwnStaticMutex() );
450 	m_pDataContainer->SetWriterOLE_Objects( nWriterOLE );
451 }
452 
453 //*****************************************************************************************************************
454 //	public method
455 //*****************************************************************************************************************
456 void SvtCacheOptions::SetDrawingEngineOLE_Objects( sal_Int32 nDrawingOLE )
457 {
458     MutexGuard aGuard( GetOwnStaticMutex() );
459 	m_pDataContainer->SetDrawingEngineOLE_Objects( nDrawingOLE );
460 }
461 
462 //*****************************************************************************************************************
463 //	public method
464 //*****************************************************************************************************************
465 void SvtCacheOptions::SetGraphicManagerTotalCacheSize( sal_Int32 nGrfMgrTotalSize )
466 {
467     MutexGuard aGuard( GetOwnStaticMutex() );
468 	m_pDataContainer->SetGraphicManagerTotalCacheSize( nGrfMgrTotalSize );
469 }
470 
471 //*****************************************************************************************************************
472 //	public method
473 //*****************************************************************************************************************
474 void SvtCacheOptions::SetGraphicManagerObjectCacheSize( sal_Int32 nGrfMgrObjectSize )
475 {
476     MutexGuard aGuard( GetOwnStaticMutex() );
477 	m_pDataContainer->SetGraphicManagerObjectCacheSize( nGrfMgrObjectSize );
478 }
479 
480 //*****************************************************************************************************************
481 //	public method
482 //*****************************************************************************************************************
483 void SvtCacheOptions::SetGraphicManagerObjectReleaseTime( sal_Int32 nGrfMgrObjectReleaseTime )
484 {
485     MutexGuard aGuard( GetOwnStaticMutex() );
486 	m_pDataContainer->SetGraphicManagerObjectReleaseTime( nGrfMgrObjectReleaseTime );
487 }
488 
489 //*****************************************************************************************************************
490 //	private method
491 //*****************************************************************************************************************
492 Mutex& SvtCacheOptions::GetOwnStaticMutex()
493 {
494 	// Initialize static mutex only for one time!
495     static Mutex* pMutex = NULL;
496 	// If these method first called (Mutex not already exist!) ...
497     if( pMutex == NULL )
498     {
499 		// ... we must create a new one. Protect follow code with the global mutex -
500 		// It must be - we create a static variable!
501         MutexGuard aGuard( Mutex::getGlobalMutex() );
502 		// We must check our pointer again - because it can be that another instance of ouer class will be fastr then these!
503         if( pMutex == NULL )
504         {
505 			// Create the new mutex and set it for return on static variable.
506             static Mutex aMutex;
507             pMutex = &aMutex;
508         }
509     }
510 	// Return new created or already existing mutex object.
511     return *pMutex;
512 }
513