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/historyoptions.hxx>
34 #include <unotools/historyoptions_const.hxx>
35 #include <unotools/configmgr.hxx>
36 #include <unotools/configitem.hxx>
37 #include <tools/debug.hxx>
38 #include <com/sun/star/uno/Any.hxx>
39 #include <com/sun/star/uno/Sequence.hxx>
40 
41 #ifndef __SGI_STL_DEQUE
42 #include <deque>
43 #endif
44 
45 #ifndef __SGI_STL_ALGORITHM
46 #include <algorithm>
47 #endif
48 
49 #include <rtl/logfile.hxx>
50 #include "itemholder1.hxx"
51 
52 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
53 #include <com/sun/star/beans/XPropertySet.hpp>
54 #endif
55 
56 #ifndef _COM_SUN_STAR_CONTAINER_XNAMEACCESS_HPP_
57 #include <com/sun/star/container/XNameAccess.hpp>
58 #endif
59 
60 #ifndef _COM_SUN_STAR_CONTAINER_XNAMECONTAINER_HPP_
61 #include <com/sun/star/container/XNameContainer.hpp>
62 #endif
63 
64 #ifndef _COM_SUN_STAR_LANG_XSINGLESERVICEFACTORY_HPP_
65 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
66 #endif
67 
68 #ifndef _COMPHELPER_CONFIGURATIONHELPER_HXX_
69 #include <comphelper/configurationhelper.hxx>
70 #endif
71 
72 #ifndef _UNOTOOLS_PROCESSFACTORY_HXX_
73 #include <unotools/processfactory.hxx>
74 #endif
75 
76 #ifndef _SVT_LOGHELPER_HXX
77 #include <unotools/loghelper.hxx>
78 #endif
79 
80 //_________________________________________________________________________________________________________________
81 // namespaces
82 //_________________________________________________________________________________________________________________
83 
84 using namespace ::std     ;
85 using namespace ::utl     ;
86 using namespace ::rtl     ;
87 using namespace ::osl     ;
88 using namespace ::com::sun::star::uno ;
89 using namespace ::com::sun::star::beans ;
90 
91 namespace css = ::com::sun::star;
92 
93 //_________________________________________________________________________________________________________________
94 // const
95 //_________________________________________________________________________________________________________________
96 
97 namespace {
98 	static const ::sal_Int32 s_nOffsetURL               = 0;
99 	static const ::sal_Int32 s_nOffsetFilter            = 1;
100 	static const ::sal_Int32 s_nOffsetTitle             = 2;
101 	static const ::sal_Int32 s_nOffsetPassword          = 3;
102 }
103 
104 //_________________________________________________________________________________________________________________
105 // private declarations!
106 //_________________________________________________________________________________________________________________
107 
108 struct IMPL_THistoryItem
109 {
IMPL_THistoryItemIMPL_THistoryItem110 	IMPL_THistoryItem()
111 	{
112 	}
113 
IMPL_THistoryItemIMPL_THistoryItem114 	IMPL_THistoryItem( const OUString& sNewURL   ,
115 		const OUString& sNewFilter  ,
116 		const OUString& sNewTitle  ,
117 		const OUString& sNewPassword )
118 	{
119 		sURL  = sNewURL  ;
120 		sFilter  = sNewFilter ;
121 		sTitle  = sNewTitle  ;
122 		sPassword = sNewPassword ;
123 	}
124 
operator ==IMPL_THistoryItem125 	sal_Bool operator==( const OUString& sSearchedURL )
126 	{
127 		return( sURL == sSearchedURL );
128 	}
129 
130 	OUString sURL  ;
131 	OUString sFilter  ;
132 	OUString sTitle  ;
133 	OUString sPassword ;
134 };
135 
136 //*****************************************************************************************************************
137 //  class SvtHistoryOptions_Impl
138 //  redesigned
139 //*****************************************************************************************************************
140 class SvtHistoryOptions_Impl
141 {
142 public:
143 	SvtHistoryOptions_Impl();
144 	~SvtHistoryOptions_Impl();
145 
146 	sal_uInt32 GetSize( EHistoryType eHistory );
147 	void SetSize( EHistoryType eHistory, sal_uInt32 nSize );
148 	void Clear( EHistoryType eHistory );
149 	Sequence< Sequence< PropertyValue > > GetList( EHistoryType eHistory );
150 	void                                  AppendItem(       EHistoryType eHistory ,
151 		const OUString&    sURL     ,
152 		const OUString&    sFilter  ,
153 		const OUString&    sTitle   ,
154 		const OUString&    sPassword );
155 
156 private:
157     void impl_truncateList (EHistoryType eHistory, sal_uInt32 nSize);
158 
159 private:
160 	css::uno::Reference< css::container::XNameAccess > m_xCfg;
161 	css::uno::Reference< css::container::XNameAccess > m_xCommonXCU;
162 };
163 
164 //*****************************************************************************************************************
165 //  constructor
166 //*****************************************************************************************************************
SvtHistoryOptions_Impl()167 SvtHistoryOptions_Impl::SvtHistoryOptions_Impl()
168 {
169 	try
170 	{
171 		m_xCfg = Reference< css::container::XNameAccess > (
172 			::comphelper::ConfigurationHelper::openConfig(
173 			utl::getProcessServiceFactory(),
174 			s_sHistories,
175 			::comphelper::ConfigurationHelper::E_STANDARD),
176 			css::uno::UNO_QUERY );
177 
178 		m_xCommonXCU = Reference< css::container::XNameAccess > (
179 			::comphelper::ConfigurationHelper::openConfig(
180 			utl::getProcessServiceFactory(),
181 			s_sCommonHistory,
182 			::comphelper::ConfigurationHelper::E_STANDARD),
183 			css::uno::UNO_QUERY );
184 	}
185 	catch(const css::uno::Exception& ex)
186 	{
187 		m_xCfg.clear();
188 		m_xCommonXCU.clear();
189 
190 		LogHelper::logIt(ex);
191 	}
192 }
193 
194 //*****************************************************************************************************************
195 //  destructor
196 //*****************************************************************************************************************
~SvtHistoryOptions_Impl()197 SvtHistoryOptions_Impl::~SvtHistoryOptions_Impl()
198 {
199 }
200 
201 //*****************************************************************************************************************
202 //  public method
203 //  Attention: We return the max. size of our internal lists - That is the capacity not the size!
204 //*****************************************************************************************************************
GetSize(EHistoryType eHistory)205 sal_uInt32 SvtHistoryOptions_Impl::GetSize( EHistoryType eHistory )
206 {
207 	sal_uInt32                                       nSize = 0  ;
208 	css::uno::Reference< css::beans::XPropertySet >  xListAccess(m_xCommonXCU, css::uno::UNO_QUERY);
209 
210 	try
211 	{
212 		switch( eHistory )
213 		{
214 		case ePICKLIST:
215 			xListAccess->getPropertyValue(s_sPickListSize) >>= nSize;
216 			break;
217 
218 		case eHISTORY:
219 			xListAccess->getPropertyValue(s_sURLHistorySize) >>= nSize;
220 			break;
221 
222 		case eHELPBOOKMARKS:
223 			xListAccess->getPropertyValue(s_sHelpBookmarksSize) >>= nSize;
224 			break;
225 
226 		default:
227 			break;
228 		}
229 	}
230 	catch(const css::uno::Exception& ex)
231 	{
232 		LogHelper::logIt(ex);
233 	}
234 
235 	return nSize;
236 }
237 
238 //*****************************************************************************************************************
239 //  public method
240 //  Attention: We return the max. size of our internal lists - That is the capacity not the size!
241 //*****************************************************************************************************************
SetSize(EHistoryType eHistory,sal_uInt32 nSize)242 void SvtHistoryOptions_Impl::SetSize( EHistoryType eHistory, sal_uInt32 nSize )
243 {
244 	css::uno::Reference< css::beans::XPropertySet > xListAccess(m_xCommonXCU, css::uno::UNO_QUERY);
245     if (! xListAccess.is ())
246         return;
247 
248 	try
249 	{
250 		switch( eHistory )
251 		{
252 		case ePICKLIST:
253 			if(nSize!=GetSize(ePICKLIST))
254 			{
255 				xListAccess->setPropertyValue(s_sPickListSize, css::uno::makeAny(nSize));
256 				::comphelper::ConfigurationHelper::flush(m_xCommonXCU);
257 			}
258 			break;
259 
260 		case eHISTORY:
261 			if(nSize!=GetSize(eHISTORY))
262 			{
263 				xListAccess->setPropertyValue(s_sURLHistorySize, css::uno::makeAny(nSize));
264 				::comphelper::ConfigurationHelper::flush(m_xCommonXCU);
265 			}
266 			break;
267 
268 		case eHELPBOOKMARKS:
269 			if(nSize!=GetSize(eHELPBOOKMARKS))
270 			{
271 				xListAccess->setPropertyValue(s_sHelpBookmarksSize, css::uno::makeAny(nSize));
272 				::comphelper::ConfigurationHelper::flush(m_xCommonXCU);
273 			}
274 			break;
275 
276 		default:
277 			break;
278 		}
279 
280         impl_truncateList (eHistory, nSize);
281 	}
282 	catch(const css::uno::Exception& ex)
283 	{
284 		LogHelper::logIt(ex);
285 	}
286 }
287 
288 //*****************************************************************************************************************
impl_truncateList(EHistoryType eHistory,sal_uInt32 nSize)289 void SvtHistoryOptions_Impl::impl_truncateList ( EHistoryType eHistory, sal_uInt32 nSize )
290 {
291 	css::uno::Reference< css::container::XNameAccess >    xList;
292 	css::uno::Reference< css::container::XNameContainer > xItemList;
293 	css::uno::Reference< css::container::XNameContainer > xOrderList;
294 	css::uno::Reference< css::beans::XPropertySet >       xSet;
295 
296 	try
297 	{
298 		switch( eHistory )
299 		{
300 		case ePICKLIST:
301             m_xCfg->getByName(s_sPickList) >>= xList;
302 			break;
303 
304 		case eHISTORY:
305             m_xCfg->getByName(s_sURLHistory) >>= xList;
306 			break;
307 
308 		case eHELPBOOKMARKS:
309             m_xCfg->getByName(s_sHelpBookmarks) >>= xList;
310 			break;
311 
312 		default:
313 			break;
314 		}
315 
316 		// If too much items in current list ...
317 		// truncate the oldest items BEFORE you set the new one.
318 		if ( ! xList.is())
319             return;
320 
321         xList->getByName(s_sOrderList) >>= xOrderList;
322         xList->getByName(s_sItemList)  >>= xItemList;
323 
324         const sal_uInt32 nLength = xOrderList->getElementNames().getLength();
325         if (nSize < nLength)
326         {
327             for (sal_uInt32 i=nLength-1; i>=nSize; --i)
328             {
329                 ::rtl::OUString sTmp;
330                 const ::rtl::OUString sRemove = ::rtl::OUString::valueOf((sal_Int32)i);
331                 xOrderList->getByName(sRemove) >>= xSet;
332                 xSet->getPropertyValue(s_sHistoryItemRef) >>= sTmp;
333                 xItemList->removeByName(sTmp);
334                 xOrderList->removeByName(sRemove);
335             }
336 
337             ::comphelper::ConfigurationHelper::flush(m_xCfg);
338         }
339 	}
340 	catch(const css::uno::Exception& ex)
341 	{
342 		LogHelper::logIt(ex);
343 	}
344 }
345 
346 //*****************************************************************************************************************
347 //  public method
348 //  Clear specified history list
349 //*****************************************************************************************************************
Clear(EHistoryType eHistory)350 void SvtHistoryOptions_Impl::Clear( EHistoryType eHistory )
351 {
352 	css::uno::Reference< css::container::XNameAccess >    xListAccess;
353 	css::uno::Reference< css::container::XNameContainer > xNode;
354 	Sequence< ::rtl::OUString >                           lOrders;
355 
356 	try
357 	{
358 		switch( eHistory )
359 		{
360 		case ePICKLIST:
361 			{
362 				m_xCfg->getByName(s_sPickList) >>= xListAccess;
363 				break;
364 			}
365 
366 		case eHISTORY:
367 			{
368 				m_xCfg->getByName(s_sURLHistory) >>= xListAccess;
369 				break;
370 			}
371 
372 		case eHELPBOOKMARKS:
373 			{
374 				m_xCfg->getByName(s_sHelpBookmarks) >>= xListAccess;
375 				break;
376 			}
377 
378 		default:
379 			break;
380 		}
381 
382 		if (xListAccess.is())
383 		{
384 			// clear ItemList
385 			xListAccess->getByName(s_sItemList)  >>= xNode  ;
386 			lOrders = xNode->getElementNames();
387 			const sal_Int32 nLength = lOrders.getLength();
388 			for(sal_Int32 i=0; i<nLength; ++i)
389 				xNode->removeByName(lOrders[i]);
390 
391 			// clear OrderList
392 			xListAccess->getByName(s_sOrderList) >>= xNode ;
393 			lOrders = xNode->getElementNames();
394 			for(sal_Int32 j=0; j<nLength; ++j)
395 				xNode->removeByName(lOrders[j]);
396 
397 			::comphelper::ConfigurationHelper::flush(m_xCfg);
398 		}
399 	}
400 	catch(const css::uno::Exception& ex)
401 	{
402 		LogHelper::logIt(ex);
403 	}
404 }
405 
406 //*****************************************************************************************************************
407 //  public method
408 //  get a sequence list from the items
409 //*****************************************************************************************************************
GetList(EHistoryType eHistory)410 Sequence< Sequence< PropertyValue > > SvtHistoryOptions_Impl::GetList( EHistoryType eHistory )
411 {
412     impl_truncateList (eHistory, GetSize (eHistory));
413 
414 	Sequence< Sequence< PropertyValue > > seqReturn; // Set default return value.
415 	Sequence< PropertyValue >             seqProperties( 4 );
416 	Sequence< ::rtl::OUString >           lOrders;
417 
418 	css::uno::Reference< css::container::XNameAccess > xListAccess;
419 	css::uno::Reference< css::container::XNameAccess > xItemList;
420 	css::uno::Reference< css::container::XNameAccess > xOrderList;
421 	css::uno::Reference< css::beans::XPropertySet >    xSet;
422 
423 	seqProperties[s_nOffsetURL       ].Name = HISTORY_PROPERTYNAME_URL;
424 	seqProperties[s_nOffsetFilter    ].Name = HISTORY_PROPERTYNAME_FILTER;
425 	seqProperties[s_nOffsetTitle     ].Name = HISTORY_PROPERTYNAME_TITLE;
426 	seqProperties[s_nOffsetPassword  ].Name = HISTORY_PROPERTYNAME_PASSWORD;
427 
428 	try
429 	{
430 		switch( eHistory )
431 		{
432 		case ePICKLIST:
433 			{
434 				m_xCfg->getByName(s_sPickList) >>= xListAccess;
435 				break;
436 			}
437 
438 		case eHISTORY:
439 			{
440 				m_xCfg->getByName(s_sURLHistory) >>= xListAccess;
441 				break;
442 			}
443 
444 		case eHELPBOOKMARKS:
445 			{
446 				m_xCfg->getByName(s_sHelpBookmarks) >>= xListAccess;
447 				break;
448 			}
449 
450 		default:
451 			break;
452 		}
453 
454 		if (xListAccess.is())
455 		{
456 			xListAccess->getByName(s_sItemList)  >>= xItemList;
457 			xListAccess->getByName(s_sOrderList) >>= xOrderList;
458 
459 			const sal_Int32 nLength = xOrderList->getElementNames().getLength();
460 			Sequence< Sequence< PropertyValue > > aRet(nLength);
461 
462 			for(sal_Int32 nItem=0; nItem<nLength; ++nItem)
463 			{
464 				::rtl::OUString sUrl;
465 				xOrderList->getByName(::rtl::OUString::valueOf(nItem)) >>= xSet;
466 				xSet->getPropertyValue(s_sHistoryItemRef) >>= sUrl;
467 
468 				xItemList->getByName(sUrl) >>= xSet;
469 				seqProperties[s_nOffsetURL  ].Value <<= sUrl;
470 				xSet->getPropertyValue(s_sFilter)   >>= seqProperties[s_nOffsetFilter   ].Value;
471 				xSet->getPropertyValue(s_sTitle)    >>= seqProperties[s_nOffsetTitle    ].Value;
472 				xSet->getPropertyValue(s_sPassword) >>= seqProperties[s_nOffsetPassword ].Value;
473 				aRet[nItem] = seqProperties;
474 			}
475 			seqReturn = aRet;
476 		}
477 	}
478 	catch(const css::uno::Exception& ex)
479 	{
480 		LogHelper::logIt(ex);
481 	}
482 
483 	return seqReturn;
484 }
485 
486 //*****************************************************************************************************************
487 //  public method
488 //  implements a deque in XML
489 //*****************************************************************************************************************
AppendItem(EHistoryType eHistory,const OUString & sURL,const OUString & sFilter,const OUString & sTitle,const OUString & sPassword)490 void SvtHistoryOptions_Impl::AppendItem(       EHistoryType eHistory ,
491 										const OUString& sURL        ,
492 										const OUString& sFilter     ,
493 										const OUString& sTitle      ,
494 										const OUString& sPassword   )
495 {
496     impl_truncateList (eHistory, GetSize (eHistory));
497 
498 	css::uno::Reference< css::container::XNameAccess > xListAccess;
499 	sal_Int32             nMaxSize = 0;
500 
501 	switch(eHistory)
502 	{
503 	case ePICKLIST:
504 		{
505 			m_xCfg->getByName(s_sPickList) >>= xListAccess;
506 			nMaxSize = GetSize(ePICKLIST);
507 		}
508 		break;
509 	case eHISTORY:
510 		{
511 			m_xCfg->getByName(s_sURLHistory) >>= xListAccess;
512 			nMaxSize = GetSize(eHISTORY);
513 		}
514 		break;
515 	case eHELPBOOKMARKS:
516 		{
517 			m_xCfg->getByName(s_sHelpBookmarks) >>= xListAccess;
518 			nMaxSize = GetSize(eHELPBOOKMARKS);
519 		}
520 		break;
521 	default:
522 		break;
523 	}
524 
525 	if (nMaxSize==0)
526 		return;
527 
528 	css::uno::Reference< css::container::XNameContainer > xItemList;
529 	css::uno::Reference< css::container::XNameContainer > xOrderList;
530 	css::uno::Reference< css::beans::XPropertySet >       xSet;
531 
532 	try
533 	{
534 		xListAccess->getByName(s_sItemList)  >>= xItemList;
535 		xListAccess->getByName(s_sOrderList) >>= xOrderList;
536 		sal_Int32 nLength = xOrderList->getElementNames().getLength();
537 
538 		// The item to be appended is already existing!
539 		if (xItemList->hasByName(sURL))
540 		{
541 			for (sal_Int32 i=0; i<nLength; ++i)
542 			{
543 				::rtl::OUString sTmp;
544 				xOrderList->getByName(::rtl::OUString::valueOf(i)) >>= xSet;
545 				xSet->getPropertyValue(s_sHistoryItemRef) >>= sTmp;
546 
547 				if(sURL == sTmp)
548 				{
549 					::rtl::OUString sFind;
550 					xOrderList->getByName( ::rtl::OUString::valueOf(i) ) >>= xSet;
551 					xSet->getPropertyValue(s_sHistoryItemRef) >>= sFind;
552 					for (sal_Int32 j=i-1; j>=0; --j)
553 					{
554 						css::uno::Reference< css::beans::XPropertySet > xPrevSet;
555 						css::uno::Reference< css::beans::XPropertySet > xNextSet;
556 						xOrderList->getByName( ::rtl::OUString::valueOf(j+1) )   >>= xPrevSet;
557 						xOrderList->getByName( ::rtl::OUString::valueOf(j) )     >>= xNextSet;
558 
559 						::rtl::OUString sTemp;
560 						xNextSet->getPropertyValue(s_sHistoryItemRef) >>= sTemp;
561 						xPrevSet->setPropertyValue(s_sHistoryItemRef, css::uno::makeAny(sTemp));
562 					}
563 					xOrderList->getByName( ::rtl::OUString::valueOf((sal_Int32)0) ) >>= xSet;
564 					xSet->setPropertyValue(s_sHistoryItemRef, css::uno::makeAny(sFind));
565 
566 					::comphelper::ConfigurationHelper::flush(m_xCfg);
567 					break;
568 				}
569 			}
570 		}
571 
572 		// The item to be appended is not existing!
573 		else
574 		{
575 			css::uno::Reference< css::lang::XSingleServiceFactory > xFac;
576 			css::uno::Reference< css::uno::XInterface >             xInst;
577 			css::uno::Reference< css::beans::XPropertySet > xPrevSet;
578 			css::uno::Reference< css::beans::XPropertySet > xNextSet;
579 
580 			// Append new item to OrderList.
581 			if ( nLength == nMaxSize )
582 			{
583 				::rtl::OUString sRemove;
584 				xOrderList->getByName(::rtl::OUString::valueOf(nLength-1)) >>= xSet;
585 				xSet->getPropertyValue(s_sHistoryItemRef) >>= sRemove;
586 				xItemList->removeByName(sRemove);
587 			}
588 			if ( nLength != nMaxSize )
589 			{
590 				xFac = css::uno::Reference< css::lang::XSingleServiceFactory >(xOrderList, css::uno::UNO_QUERY);
591 				xInst = xFac->createInstance();
592 				::rtl::OUString sPush = ::rtl::OUString::valueOf(nLength++);
593 				xOrderList->insertByName(sPush, css::uno::makeAny(xInst));
594 			}
595 			for (sal_Int32 j=nLength-1; j>0; --j)
596 			{
597 				xOrderList->getByName( ::rtl::OUString::valueOf(j) )   >>= xPrevSet;
598 				xOrderList->getByName( ::rtl::OUString::valueOf(j-1) ) >>= xNextSet;
599 				::rtl::OUString sTemp;
600 				xNextSet->getPropertyValue(s_sHistoryItemRef) >>= sTemp;
601 				xPrevSet->setPropertyValue(s_sHistoryItemRef, css::uno::makeAny(sTemp));
602 			}
603 			xOrderList->getByName( ::rtl::OUString::valueOf((sal_Int32)0) ) >>= xSet;
604 			xSet->setPropertyValue(s_sHistoryItemRef, css::uno::makeAny(sURL));
605 
606 			// Append the item to ItemList.
607 			xFac = css::uno::Reference< css::lang::XSingleServiceFactory >(xItemList, css::uno::UNO_QUERY);
608 			xInst = xFac->createInstance();
609 			xItemList->insertByName(sURL, css::uno::makeAny(xInst));
610 			xSet = css::uno::Reference< css::beans::XPropertySet >(xInst, css::uno::UNO_QUERY);
611 			xSet->setPropertyValue(s_sFilter, css::uno::makeAny(sFilter));
612 			xSet->setPropertyValue(s_sTitle, css::uno::makeAny(sTitle));
613 			xSet->setPropertyValue(s_sPassword, css::uno::makeAny(sPassword));
614 
615 			::comphelper::ConfigurationHelper::flush(m_xCfg);
616 		}
617 	}
618 	catch(const css::uno::Exception& ex)
619 	{
620 		LogHelper::logIt(ex);
621 	}
622 }
623 
624 //*****************************************************************************************************************
625 // initialize static member
626 // DON'T DO IT IN YOUR HEADER!
627 // see definition for further informations
628 //*****************************************************************************************************************
629 SvtHistoryOptions_Impl*  SvtHistoryOptions::m_pDataContainer = NULL ;
630 sal_Int32     SvtHistoryOptions::m_nRefCount  = 0  ;
631 
632 //*****************************************************************************************************************
633 // constructor
634 //*****************************************************************************************************************
SvtHistoryOptions()635 SvtHistoryOptions::SvtHistoryOptions()
636 {
637 	// Global access, must be guarded (multithreading!).
638 	MutexGuard aGuard( GetOwnStaticMutex() );
639 	// Increase our refcount ...
640 	++m_nRefCount;
641 	// ... and initialize our data container only if it not already exist!
642 	if( m_pDataContainer == NULL )
643 	{
644 		RTL_LOGFILE_CONTEXT(aLog, "unotools ( ??? ) ::SvtHistoryOptions_Impl::ctor()");
645 		m_pDataContainer = new SvtHistoryOptions_Impl;
646 
647 		ItemHolder1::holdConfigItem(E_HISTORYOPTIONS);
648 	}
649 }
650 
651 //*****************************************************************************************************************
652 // destructor
653 //*****************************************************************************************************************
~SvtHistoryOptions()654 SvtHistoryOptions::~SvtHistoryOptions()
655 {
656 	// Global access, must be guarded (multithreading!)
657 	MutexGuard aGuard( GetOwnStaticMutex() );
658 	// Decrease our refcount.
659 	--m_nRefCount;
660 	// If last instance was deleted ...
661 	// we must destroy our static data container!
662 	if( m_nRefCount <= 0 )
663 	{
664 		delete m_pDataContainer;
665 		m_pDataContainer = NULL;
666 	}
667 }
668 
669 //*****************************************************************************************************************
670 // public method
671 //*****************************************************************************************************************
GetSize(EHistoryType eHistory) const672 sal_uInt32 SvtHistoryOptions::GetSize( EHistoryType eHistory ) const
673 {
674 	MutexGuard aGuard( GetOwnStaticMutex() );
675 	return m_pDataContainer->GetSize( eHistory );
676 }
677 
678 //*****************************************************************************************************************
679 // public method
680 //*****************************************************************************************************************
SetSize(EHistoryType eHistory,sal_uInt32 nSize)681 void SvtHistoryOptions::SetSize( EHistoryType eHistory, sal_uInt32 nSize )
682 {
683 	MutexGuard aGuard( GetOwnStaticMutex() );
684 	m_pDataContainer->SetSize( eHistory, nSize );
685 }
686 
687 //*****************************************************************************************************************
688 // public method
689 //*****************************************************************************************************************
Clear(EHistoryType eHistory)690 void SvtHistoryOptions::Clear( EHistoryType eHistory )
691 {
692 	MutexGuard aGuard( GetOwnStaticMutex() );
693 	m_pDataContainer->Clear( eHistory );
694 }
695 
696 //*****************************************************************************************************************
697 // public method
698 //*****************************************************************************************************************
GetList(EHistoryType eHistory) const699 Sequence< Sequence< PropertyValue > > SvtHistoryOptions::GetList( EHistoryType eHistory ) const
700 {
701 	MutexGuard aGuard( GetOwnStaticMutex() );
702 	return m_pDataContainer->GetList( eHistory );
703 }
704 
705 //*****************************************************************************************************************
706 // public method
707 //*****************************************************************************************************************
AppendItem(EHistoryType eHistory,const OUString & sURL,const OUString & sFilter,const OUString & sTitle,const OUString & sPassword)708 void SvtHistoryOptions::AppendItem(   EHistoryType eHistory ,
709 								   const OUString&  sURL  ,
710 								   const OUString&  sFilter  ,
711 								   const OUString&  sTitle  ,
712 								   const OUString&  sPassword )
713 {
714 	MutexGuard aGuard( GetOwnStaticMutex() );
715 	m_pDataContainer->AppendItem( eHistory, sURL, sFilter, sTitle, sPassword );
716 }
717 
718 //*****************************************************************************************************************
719 // private method
720 //*****************************************************************************************************************
GetOwnStaticMutex()721 Mutex& SvtHistoryOptions::GetOwnStaticMutex()
722 {
723 	// Initialize static mutex only for one time!
724 	static Mutex* pMutex = NULL;
725 	// If these method first called (Mutex not already exist!) ...
726 	if( pMutex == NULL )
727 	{
728 		// ... we must create a new one. Protect follow code with the global mutex -
729 		// It must be - we create a static variable!
730 		MutexGuard aGuard( Mutex::getGlobalMutex() );
731 		// We must check our pointer again - because it can be that another instance of our class will be faster than these!
732 		if( pMutex == NULL )
733 		{
734 			// Create the new mutex and set it for return on static variable.
735 			static Mutex aMutex;
736 			pMutex = &aMutex;
737 		}
738 	}
739 	// Return new created or already existing mutex object.
740 	return *pMutex;
741 }
742