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