1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: historyoptions.cxx,v $
10 * $Revision: 1.21.234.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
36 //_________________________________________________________________________________________________________________
38 //_________________________________________________________________________________________________________________
40 #include <svtools/historyoptions.hxx>
41 #include "configitems/historyoptions_const.hxx"
42 #include <unotools/configmgr.hxx>
43 #include <unotools/configitem.hxx>
44 #include <tools/debug.hxx>
45 #include <com/sun/star/uno/Any.hxx>
46 #include <com/sun/star/uno/Sequence.hxx>
48 #ifndef __SGI_STL_DEQUE
52 #ifndef __SGI_STL_ALGORITHM
56 #include <rtl/logfile.hxx>
57 #include "itemholder1.hxx"
59 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
60 #include <com/sun/star/beans/XPropertySet.hpp>
63 #ifndef _COM_SUN_STAR_CONTAINER_XNAMEACCESS_HPP_
64 #include <com/sun/star/container/XNameAccess.hpp>
67 #ifndef _COM_SUN_STAR_CONTAINER_XNAMECONTAINER_HPP_
68 #include <com/sun/star/container/XNameContainer.hpp>
71 #ifndef _COM_SUN_STAR_LANG_XSINGLESERVICEFACTORY_HPP_
72 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
75 #ifndef _COMPHELPER_CONFIGURATIONHELPER_HXX_
76 #include <comphelper/configurationhelper.hxx>
79 #ifndef _UNOTOOLS_PROCESSFACTORY_HXX_
80 #include <unotools/processfactory.hxx>
83 #ifndef _SVT_LOGHELPER_HXX
84 #include "loghelper.hxx"
87 //_________________________________________________________________________________________________________________
89 //_________________________________________________________________________________________________________________
91 using namespace ::std
;
92 using namespace ::utl
;
93 using namespace ::rtl
;
94 using namespace ::osl
;
95 using namespace ::com::sun::star::uno
;
96 using namespace ::com::sun::star::beans
;
98 namespace css
= ::com::sun::star
;
100 //_________________________________________________________________________________________________________________
102 //_________________________________________________________________________________________________________________
105 static const ::sal_Int32 s_nOffsetURL
= 0;
106 static const ::sal_Int32 s_nOffsetFilter
= 1;
107 static const ::sal_Int32 s_nOffsetTitle
= 2;
108 static const ::sal_Int32 s_nOffsetPassword
= 3;
111 //_________________________________________________________________________________________________________________
112 // private declarations!
113 //_________________________________________________________________________________________________________________
115 struct IMPL_THistoryItem
121 IMPL_THistoryItem( const OUString
& sNewURL
,
122 const OUString
& sNewFilter
,
123 const OUString
& sNewTitle
,
124 const OUString
& sNewPassword
)
127 sFilter
= sNewFilter
;
129 sPassword
= sNewPassword
;
132 sal_Bool
operator==( const OUString
& sSearchedURL
)
134 return( sURL
== sSearchedURL
);
143 //*****************************************************************************************************************
144 // class SvtHistoryOptions_Impl
146 //*****************************************************************************************************************
147 class SvtHistoryOptions_Impl
150 SvtHistoryOptions_Impl();
151 ~SvtHistoryOptions_Impl();
153 sal_uInt32
GetSize( EHistoryType eHistory
);
154 void SetSize( EHistoryType eHistory
, sal_uInt32 nSize
);
155 void Clear( EHistoryType eHistory
);
156 Sequence
< Sequence
< PropertyValue
> > GetList( EHistoryType eHistory
);
157 void AppendItem( EHistoryType eHistory
,
158 const OUString
& sURL
,
159 const OUString
& sFilter
,
160 const OUString
& sTitle
,
161 const OUString
& sPassword
);
164 void impl_truncateList (EHistoryType eHistory
, sal_uInt32 nSize
);
167 css::uno::Reference
< css::container::XNameAccess
> m_xCfg
;
168 css::uno::Reference
< css::container::XNameAccess
> m_xCommonXCU
;
171 //*****************************************************************************************************************
173 //*****************************************************************************************************************
174 SvtHistoryOptions_Impl::SvtHistoryOptions_Impl()
178 m_xCfg
= Reference
< css::container::XNameAccess
> (
179 ::comphelper::ConfigurationHelper::openConfig(
180 utl::getProcessServiceFactory(),
182 ::comphelper::ConfigurationHelper::E_STANDARD
),
183 css::uno::UNO_QUERY
);
185 m_xCommonXCU
= Reference
< css::container::XNameAccess
> (
186 ::comphelper::ConfigurationHelper::openConfig(
187 utl::getProcessServiceFactory(),
189 ::comphelper::ConfigurationHelper::E_STANDARD
),
190 css::uno::UNO_QUERY
);
192 catch(const css::uno::Exception
& ex
)
195 m_xCommonXCU
.clear();
197 LogHelper::logIt(ex
);
201 //*****************************************************************************************************************
203 //*****************************************************************************************************************
204 SvtHistoryOptions_Impl::~SvtHistoryOptions_Impl()
208 //*****************************************************************************************************************
210 // Attention: We return the max. size of our internal lists - That is the capacity not the size!
211 //*****************************************************************************************************************
212 sal_uInt32
SvtHistoryOptions_Impl::GetSize( EHistoryType eHistory
)
214 sal_uInt32 nSize
= 0 ;
215 css::uno::Reference
< css::beans::XPropertySet
> xListAccess(m_xCommonXCU
, css::uno::UNO_QUERY
);
222 xListAccess
->getPropertyValue(s_sPickListSize
) >>= nSize
;
226 xListAccess
->getPropertyValue(s_sURLHistorySize
) >>= nSize
;
230 xListAccess
->getPropertyValue(s_sHelpBookmarksSize
) >>= nSize
;
237 catch(const css::uno::Exception
& ex
)
239 LogHelper::logIt(ex
);
245 //*****************************************************************************************************************
247 // Attention: We return the max. size of our internal lists - That is the capacity not the size!
248 //*****************************************************************************************************************
249 void SvtHistoryOptions_Impl::SetSize( EHistoryType eHistory
, sal_uInt32 nSize
)
251 css::uno::Reference
< css::beans::XPropertySet
> xListAccess(m_xCommonXCU
, css::uno::UNO_QUERY
);
252 if (! xListAccess
.is ())
260 if(nSize
!=GetSize(ePICKLIST
))
262 xListAccess
->setPropertyValue(s_sPickListSize
, css::uno::makeAny(nSize
));
263 ::comphelper::ConfigurationHelper::flush(m_xCommonXCU
);
268 if(nSize
!=GetSize(eHISTORY
))
270 xListAccess
->setPropertyValue(s_sURLHistorySize
, css::uno::makeAny(nSize
));
271 ::comphelper::ConfigurationHelper::flush(m_xCommonXCU
);
276 if(nSize
!=GetSize(eHELPBOOKMARKS
))
278 xListAccess
->setPropertyValue(s_sHelpBookmarksSize
, css::uno::makeAny(nSize
));
279 ::comphelper::ConfigurationHelper::flush(m_xCommonXCU
);
287 impl_truncateList (eHistory
, nSize
);
289 catch(const css::uno::Exception
& ex
)
291 LogHelper::logIt(ex
);
295 //*****************************************************************************************************************
296 void SvtHistoryOptions_Impl::impl_truncateList ( EHistoryType eHistory
, sal_uInt32 nSize
)
298 css::uno::Reference
< css::container::XNameAccess
> xList
;
299 css::uno::Reference
< css::container::XNameContainer
> xItemList
;
300 css::uno::Reference
< css::container::XNameContainer
> xOrderList
;
301 css::uno::Reference
< css::beans::XPropertySet
> xSet
;
308 m_xCfg
->getByName(s_sPickList
) >>= xList
;
312 m_xCfg
->getByName(s_sURLHistory
) >>= xList
;
316 m_xCfg
->getByName(s_sHelpBookmarks
) >>= xList
;
323 // If too much items in current list ...
324 // truncate the oldest items BEFORE you set the new one.
328 xList
->getByName(s_sOrderList
) >>= xOrderList
;
329 xList
->getByName(s_sItemList
) >>= xItemList
;
331 const sal_uInt32 nLength
= xOrderList
->getElementNames().getLength();
334 for (sal_uInt32 i
=nLength
-1; i
>=nSize
; --i
)
336 ::rtl::OUString sTmp
;
337 const ::rtl::OUString sRemove
= ::rtl::OUString::valueOf((sal_Int32
)i
);
338 xOrderList
->getByName(sRemove
) >>= xSet
;
339 xSet
->getPropertyValue(s_sHistoryItemRef
) >>= sTmp
;
340 xItemList
->removeByName(sTmp
);
341 xOrderList
->removeByName(sRemove
);
344 ::comphelper::ConfigurationHelper::flush(m_xCfg
);
347 catch(const css::uno::Exception
& ex
)
349 LogHelper::logIt(ex
);
353 //*****************************************************************************************************************
355 // Clear specified history list
356 //*****************************************************************************************************************
357 void SvtHistoryOptions_Impl::Clear( EHistoryType eHistory
)
359 css::uno::Reference
< css::container::XNameAccess
> xListAccess
;
360 css::uno::Reference
< css::container::XNameContainer
> xNode
;
361 Sequence
< ::rtl::OUString
> lOrders
;
369 m_xCfg
->getByName(s_sPickList
) >>= xListAccess
;
375 m_xCfg
->getByName(s_sURLHistory
) >>= xListAccess
;
381 m_xCfg
->getByName(s_sHelpBookmarks
) >>= xListAccess
;
389 if (xListAccess
.is())
392 xListAccess
->getByName(s_sItemList
) >>= xNode
;
393 lOrders
= xNode
->getElementNames();
394 const sal_Int32 nLength
= lOrders
.getLength();
395 for(sal_Int32 i
=0; i
<nLength
; ++i
)
396 xNode
->removeByName(lOrders
[i
]);
399 xListAccess
->getByName(s_sOrderList
) >>= xNode
;
400 lOrders
= xNode
->getElementNames();
401 for(sal_Int32 j
=0; j
<nLength
; ++j
)
402 xNode
->removeByName(lOrders
[j
]);
404 ::comphelper::ConfigurationHelper::flush(m_xCfg
);
407 catch(const css::uno::Exception
& ex
)
409 LogHelper::logIt(ex
);
413 //*****************************************************************************************************************
415 // get a sequence list from the items
416 //*****************************************************************************************************************
417 Sequence
< Sequence
< PropertyValue
> > SvtHistoryOptions_Impl::GetList( EHistoryType eHistory
)
419 impl_truncateList (eHistory
, GetSize (eHistory
));
421 Sequence
< Sequence
< PropertyValue
> > seqReturn
; // Set default return value.
422 Sequence
< PropertyValue
> seqProperties( 4 );
423 Sequence
< ::rtl::OUString
> lOrders
;
425 css::uno::Reference
< css::container::XNameAccess
> xListAccess
;
426 css::uno::Reference
< css::container::XNameAccess
> xItemList
;
427 css::uno::Reference
< css::container::XNameAccess
> xOrderList
;
428 css::uno::Reference
< css::beans::XPropertySet
> xSet
;
430 seqProperties
[s_nOffsetURL
].Name
= HISTORY_PROPERTYNAME_URL
;
431 seqProperties
[s_nOffsetFilter
].Name
= HISTORY_PROPERTYNAME_FILTER
;
432 seqProperties
[s_nOffsetTitle
].Name
= HISTORY_PROPERTYNAME_TITLE
;
433 seqProperties
[s_nOffsetPassword
].Name
= HISTORY_PROPERTYNAME_PASSWORD
;
441 m_xCfg
->getByName(s_sPickList
) >>= xListAccess
;
447 m_xCfg
->getByName(s_sURLHistory
) >>= xListAccess
;
453 m_xCfg
->getByName(s_sHelpBookmarks
) >>= xListAccess
;
461 if (xListAccess
.is())
463 xListAccess
->getByName(s_sItemList
) >>= xItemList
;
464 xListAccess
->getByName(s_sOrderList
) >>= xOrderList
;
466 const sal_Int32 nLength
= xOrderList
->getElementNames().getLength();
467 Sequence
< Sequence
< PropertyValue
> > aRet(nLength
);
469 for(sal_Int32 nItem
=0; nItem
<nLength
; ++nItem
)
471 ::rtl::OUString sUrl
;
472 xOrderList
->getByName(::rtl::OUString::valueOf(nItem
)) >>= xSet
;
473 xSet
->getPropertyValue(s_sHistoryItemRef
) >>= sUrl
;
475 xItemList
->getByName(sUrl
) >>= xSet
;
476 seqProperties
[s_nOffsetURL
].Value
<<= sUrl
;
477 xSet
->getPropertyValue(s_sFilter
) >>= seqProperties
[s_nOffsetFilter
].Value
;
478 xSet
->getPropertyValue(s_sTitle
) >>= seqProperties
[s_nOffsetTitle
].Value
;
479 xSet
->getPropertyValue(s_sPassword
) >>= seqProperties
[s_nOffsetPassword
].Value
;
480 aRet
[nItem
] = seqProperties
;
485 catch(const css::uno::Exception
& ex
)
487 LogHelper::logIt(ex
);
493 //*****************************************************************************************************************
495 // implements a deque in XML
496 //*****************************************************************************************************************
497 void SvtHistoryOptions_Impl::AppendItem( EHistoryType eHistory
,
498 const OUString
& sURL
,
499 const OUString
& sFilter
,
500 const OUString
& sTitle
,
501 const OUString
& sPassword
)
503 impl_truncateList (eHistory
, GetSize (eHistory
));
505 css::uno::Reference
< css::container::XNameAccess
> xListAccess
;
506 sal_Int32 nMaxSize
= 0;
512 m_xCfg
->getByName(s_sPickList
) >>= xListAccess
;
513 nMaxSize
= GetSize(ePICKLIST
);
518 m_xCfg
->getByName(s_sURLHistory
) >>= xListAccess
;
519 nMaxSize
= GetSize(eHISTORY
);
524 m_xCfg
->getByName(s_sHelpBookmarks
) >>= xListAccess
;
525 nMaxSize
= GetSize(eHELPBOOKMARKS
);
535 css::uno::Reference
< css::container::XNameContainer
> xItemList
;
536 css::uno::Reference
< css::container::XNameContainer
> xOrderList
;
537 css::uno::Reference
< css::beans::XPropertySet
> xSet
;
541 xListAccess
->getByName(s_sItemList
) >>= xItemList
;
542 xListAccess
->getByName(s_sOrderList
) >>= xOrderList
;
543 sal_Int32 nLength
= xOrderList
->getElementNames().getLength();
545 // The item to be appended is already existing!
546 if (xItemList
->hasByName(sURL
))
548 for (sal_Int32 i
=0; i
<nLength
; ++i
)
550 ::rtl::OUString sTmp
;
551 xOrderList
->getByName(::rtl::OUString::valueOf(i
)) >>= xSet
;
552 xSet
->getPropertyValue(s_sHistoryItemRef
) >>= sTmp
;
556 ::rtl::OUString sFind
;
557 xOrderList
->getByName( ::rtl::OUString::valueOf(i
) ) >>= xSet
;
558 xSet
->getPropertyValue(s_sHistoryItemRef
) >>= sFind
;
559 for (sal_Int32 j
=i
-1; j
>=0; --j
)
561 css::uno::Reference
< css::beans::XPropertySet
> xPrevSet
;
562 css::uno::Reference
< css::beans::XPropertySet
> xNextSet
;
563 xOrderList
->getByName( ::rtl::OUString::valueOf(j
+1) ) >>= xPrevSet
;
564 xOrderList
->getByName( ::rtl::OUString::valueOf(j
) ) >>= xNextSet
;
566 ::rtl::OUString sTemp
;
567 xNextSet
->getPropertyValue(s_sHistoryItemRef
) >>= sTemp
;
568 xPrevSet
->setPropertyValue(s_sHistoryItemRef
, css::uno::makeAny(sTemp
));
570 xOrderList
->getByName( ::rtl::OUString::valueOf((sal_Int32
)0) ) >>= xSet
;
571 xSet
->setPropertyValue(s_sHistoryItemRef
, css::uno::makeAny(sFind
));
573 ::comphelper::ConfigurationHelper::flush(m_xCfg
);
579 // The item to be appended is not existing!
582 css::uno::Reference
< css::lang::XSingleServiceFactory
> xFac
;
583 css::uno::Reference
< css::uno::XInterface
> xInst
;
584 css::uno::Reference
< css::beans::XPropertySet
> xPrevSet
;
585 css::uno::Reference
< css::beans::XPropertySet
> xNextSet
;
587 // Append new item to OrderList.
588 if ( nLength
== nMaxSize
)
590 ::rtl::OUString sRemove
;
591 xOrderList
->getByName(::rtl::OUString::valueOf(nLength
-1)) >>= xSet
;
592 xSet
->getPropertyValue(s_sHistoryItemRef
) >>= sRemove
;
593 xItemList
->removeByName(sRemove
);
595 if ( nLength
!= nMaxSize
)
597 xFac
= css::uno::Reference
< css::lang::XSingleServiceFactory
>(xOrderList
, css::uno::UNO_QUERY
);
598 xInst
= xFac
->createInstance();
599 ::rtl::OUString sPush
= ::rtl::OUString::valueOf(nLength
++);
600 xOrderList
->insertByName(sPush
, css::uno::makeAny(xInst
));
602 for (sal_Int32 j
=nLength
-1; j
>0; --j
)
604 xOrderList
->getByName( ::rtl::OUString::valueOf(j
) ) >>= xPrevSet
;
605 xOrderList
->getByName( ::rtl::OUString::valueOf(j
-1) ) >>= xNextSet
;
606 ::rtl::OUString sTemp
;
607 xNextSet
->getPropertyValue(s_sHistoryItemRef
) >>= sTemp
;
608 xPrevSet
->setPropertyValue(s_sHistoryItemRef
, css::uno::makeAny(sTemp
));
610 xOrderList
->getByName( ::rtl::OUString::valueOf((sal_Int32
)0) ) >>= xSet
;
611 xSet
->setPropertyValue(s_sHistoryItemRef
, css::uno::makeAny(sURL
));
613 // Append the item to ItemList.
614 xFac
= css::uno::Reference
< css::lang::XSingleServiceFactory
>(xItemList
, css::uno::UNO_QUERY
);
615 xInst
= xFac
->createInstance();
616 xItemList
->insertByName(sURL
, css::uno::makeAny(xInst
));
617 xSet
= css::uno::Reference
< css::beans::XPropertySet
>(xInst
, css::uno::UNO_QUERY
);
618 xSet
->setPropertyValue(s_sFilter
, css::uno::makeAny(sFilter
));
619 xSet
->setPropertyValue(s_sTitle
, css::uno::makeAny(sTitle
));
620 xSet
->setPropertyValue(s_sPassword
, css::uno::makeAny(sPassword
));
622 ::comphelper::ConfigurationHelper::flush(m_xCfg
);
625 catch(const css::uno::Exception
& ex
)
627 LogHelper::logIt(ex
);
631 //*****************************************************************************************************************
632 // initialize static member
633 // DON'T DO IT IN YOUR HEADER!
634 // see definition for further informations
635 //*****************************************************************************************************************
636 SvtHistoryOptions_Impl
* SvtHistoryOptions::m_pDataContainer
= NULL
;
637 sal_Int32
SvtHistoryOptions::m_nRefCount
= 0 ;
639 //*****************************************************************************************************************
641 //*****************************************************************************************************************
642 SvtHistoryOptions::SvtHistoryOptions()
644 // Global access, must be guarded (multithreading!).
645 MutexGuard
aGuard( GetOwnStaticMutex() );
646 // Increase ouer refcount ...
648 // ... and initialize ouer data container only if it not already exist!
649 if( m_pDataContainer
== NULL
)
651 RTL_LOGFILE_CONTEXT(aLog
, "svtools ( ??? ) ::SvtHistoryOptions_Impl::ctor()");
652 m_pDataContainer
= new SvtHistoryOptions_Impl
;
654 ItemHolder1::holdConfigItem(E_HISTORYOPTIONS
);
658 //*****************************************************************************************************************
660 //*****************************************************************************************************************
661 SvtHistoryOptions::~SvtHistoryOptions()
663 // Global access, must be guarded (multithreading!)
664 MutexGuard
aGuard( GetOwnStaticMutex() );
665 // Decrease ouer refcount.
667 // If last instance was deleted ...
668 // we must destroy ouer static data container!
669 if( m_nRefCount
<= 0 )
671 delete m_pDataContainer
;
672 m_pDataContainer
= NULL
;
676 //*****************************************************************************************************************
678 //*****************************************************************************************************************
679 sal_uInt32
SvtHistoryOptions::GetSize( EHistoryType eHistory
) const
681 MutexGuard
aGuard( GetOwnStaticMutex() );
682 return m_pDataContainer
->GetSize( eHistory
);
685 //*****************************************************************************************************************
687 //*****************************************************************************************************************
688 void SvtHistoryOptions::SetSize( EHistoryType eHistory
, sal_uInt32 nSize
)
690 MutexGuard
aGuard( GetOwnStaticMutex() );
691 m_pDataContainer
->SetSize( eHistory
, nSize
);
694 //*****************************************************************************************************************
696 //*****************************************************************************************************************
697 void SvtHistoryOptions::Clear( EHistoryType eHistory
)
699 MutexGuard
aGuard( GetOwnStaticMutex() );
700 m_pDataContainer
->Clear( eHistory
);
703 //*****************************************************************************************************************
705 //*****************************************************************************************************************
706 Sequence
< Sequence
< PropertyValue
> > SvtHistoryOptions::GetList( EHistoryType eHistory
) const
708 MutexGuard
aGuard( GetOwnStaticMutex() );
709 return m_pDataContainer
->GetList( eHistory
);
712 //*****************************************************************************************************************
714 //*****************************************************************************************************************
715 void SvtHistoryOptions::AppendItem( EHistoryType eHistory
,
716 const OUString
& sURL
,
717 const OUString
& sFilter
,
718 const OUString
& sTitle
,
719 const OUString
& sPassword
)
721 MutexGuard
aGuard( GetOwnStaticMutex() );
722 m_pDataContainer
->AppendItem( eHistory
, sURL
, sFilter
, sTitle
, sPassword
);
725 //*****************************************************************************************************************
727 //*****************************************************************************************************************
728 Mutex
& SvtHistoryOptions::GetOwnStaticMutex()
730 // Initialize static mutex only for one time!
731 static Mutex
* pMutex
= NULL
;
732 // If these method first called (Mutex not already exist!) ...
735 // ... we must create a new one. Protect follow code with the global mutex -
736 // It must be - we create a static variable!
737 MutexGuard
aGuard( Mutex::getGlobalMutex() );
738 // We must check our pointer again - because it can be that another instance of ouer class will be fastr then these!
741 // Create the new mutex and set it for return on static variable.
746 // Return new created or already existing mutex object.