diff options
author | Noel Grandin <noelgrandin@gmail.com> | 2021-07-26 17:05:26 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2021-07-27 08:43:31 +0200 |
commit | ae34c3fe49267be34f9fb055ea1cb92240796a26 (patch) | |
tree | c9c5250268b5d574c45f6c7fa3fda1c2e4481063 /unotools | |
parent | df2b617b612f24b1dd394faf6fd605bdc69790cb (diff) |
use officecfg for SvtHistoryOptions
Change-Id: I5cfd3a51bc4026ccb66ee23eb6f49a5dff5fe042
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/119525
Tested-by: Noel Grandin <noel.grandin@collabora.co.uk>
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'unotools')
-rw-r--r-- | unotools/source/config/historyoptions.cxx | 436 | ||||
-rw-r--r-- | unotools/source/config/itemholder1.cxx | 4 |
2 files changed, 153 insertions, 287 deletions
diff --git a/unotools/source/config/historyoptions.cxx b/unotools/source/config/historyoptions.cxx index 2d95036d54c8..62de1e70d1c4 100644 --- a/unotools/source/config/historyoptions.cxx +++ b/unotools/source/config/historyoptions.cxx @@ -36,20 +36,12 @@ #include <tools/diagnose_ex.h> #include <optional> -using namespace ::std; using namespace ::utl; -using namespace ::osl; using namespace ::com::sun::star; using namespace ::com::sun::star::uno; using namespace ::com::sun::star::beans; namespace { - const ::sal_Int32 s_nOffsetURL = 0; - const ::sal_Int32 s_nOffsetFilter = 1; - const ::sal_Int32 s_nOffsetTitle = 2; - const ::sal_Int32 s_nOffsetPassword = 3; - const ::sal_Int32 s_nOffsetThumbnail = 4; - constexpr OUStringLiteral s_sItemList = u"ItemList"; constexpr OUStringLiteral s_sOrderList = u"OrderList"; constexpr OUStringLiteral s_sHistoryItemRef = u"HistoryItemRef"; @@ -57,177 +49,31 @@ namespace { constexpr OUStringLiteral s_sTitle = u"Title"; constexpr OUStringLiteral s_sPassword = u"Password"; constexpr OUStringLiteral s_sThumbnail = u"Thumbnail"; - - class theHistoryOptionsMutex : public rtl::Static<osl::Mutex, theHistoryOptionsMutex>{}; } -/// Internal implementation of the SvtHistoryOptions. -class SvtHistoryOptions_Impl -{ -public: - SvtHistoryOptions_Impl(); - - /// Returns the maximum size of the internal lists, ie. the capacity not the size. - sal_uInt32 GetCapacity(EHistoryType eHistory) const; - - /// Clear the specified history list. - void Clear(EHistoryType eHistory); - - /// Get a sequence list from the items. - Sequence< Sequence<PropertyValue> > GetList(EHistoryType eHistory); - - void AppendItem(EHistoryType eHistory, - const OUString& sURL, const OUString& sFilter, const OUString& sTitle, - const std::optional<OUString>& sThumbnail); - - void DeleteItem(EHistoryType eHistory, const OUString& sURL); - -private: - /// Return the appropriate list of recent documents (based on eHistory). - uno::Reference<container::XNameAccess> GetListAccess(EHistoryType eHistory) const; - - void impl_truncateList(EHistoryType eHistory, sal_uInt32 nSize); - -private: - uno::Reference<container::XNameAccess> m_xCfg; - uno::Reference<container::XNameAccess> m_xCommonXCU; -}; - -SvtHistoryOptions_Impl::SvtHistoryOptions_Impl() +static uno::Reference<container::XNameAccess> GetConfig(); +static uno::Reference<container::XNameAccess> GetCommonXCU(); +static uno::Reference<container::XNameAccess> GetListAccess( + uno::Reference<container::XNameAccess> const & xCfg, + EHistoryType eHistory); +static void TruncateList( + const uno::Reference<container::XNameAccess>& xCfg, + const uno::Reference<container::XNameAccess>& xList, + sal_uInt32 nSize); +static sal_uInt32 GetCapacity(const uno::Reference<container::XNameAccess>& xCommonXCU, EHistoryType eHistory); + +namespace SvtHistoryOptions { - try - { - m_xCfg.set( - ::comphelper::ConfigurationHelper::openConfig( - ::comphelper::getProcessComponentContext(), - "org.openoffice.Office.Histories/Histories", - ::comphelper::EConfigurationModes::Standard), - uno::UNO_QUERY); - - m_xCommonXCU.set( - ::comphelper::ConfigurationHelper::openConfig( - ::comphelper::getProcessComponentContext(), - "org.openoffice.Office.Common/History", - ::comphelper::EConfigurationModes::Standard), - uno::UNO_QUERY); - } - catch(const uno::Exception&) - { - DBG_UNHANDLED_EXCEPTION("unotools.config"); - m_xCfg.clear(); - m_xCommonXCU.clear(); - } -} -sal_uInt32 SvtHistoryOptions_Impl::GetCapacity(EHistoryType eHistory) const +void Clear( EHistoryType eHistory ) { - uno::Reference<beans::XPropertySet> xListAccess(m_xCommonXCU, uno::UNO_QUERY); - - if (!xListAccess.is()) - return 0; - - sal_uInt32 nSize = 0; - try { - switch (eHistory) - { - case EHistoryType::PickList: - xListAccess->getPropertyValue("PickListSize") >>= nSize; - break; - - case EHistoryType::HelpBookmarks: - xListAccess->getPropertyValue("HelpBookmarkSize") >>= nSize; - break; - - default: - break; - } - } - catch (const uno::Exception&) - { - DBG_UNHANDLED_EXCEPTION("unotools.config"); - } - - return nSize; -} - -uno::Reference<container::XNameAccess> SvtHistoryOptions_Impl::GetListAccess(EHistoryType eHistory) const -{ - uno::Reference<container::XNameAccess> xListAccess; - - try - { - switch (eHistory) - { - case EHistoryType::PickList: - m_xCfg->getByName("PickList") >>= xListAccess; - break; - - case EHistoryType::HelpBookmarks: - m_xCfg->getByName("HelpBookmarks") >>= xListAccess; - break; - - default: - break; - } - } - catch (const uno::Exception&) - { - DBG_UNHANDLED_EXCEPTION("unotools.config"); - } - - return xListAccess; -} - -void SvtHistoryOptions_Impl::impl_truncateList(EHistoryType eHistory, sal_uInt32 nSize) -{ - uno::Reference<container::XNameAccess> xList(GetListAccess(eHistory)); - if (!xList.is()) - return; - - uno::Reference<container::XNameContainer> xItemList; - uno::Reference<container::XNameContainer> xOrderList; - uno::Reference<beans::XPropertySet> xSet; - - try - { - xList->getByName(s_sOrderList) >>= xOrderList; - xList->getByName(s_sItemList) >>= xItemList; - - const sal_uInt32 nLength = xOrderList->getElementNames().getLength(); - if (nSize < nLength) - { - for (sal_uInt32 i=nLength-1; i>=nSize; --i) - { - OUString sTmp; - const OUString sRemove = OUString::number(i); - xOrderList->getByName(sRemove) >>= xSet; - xSet->getPropertyValue(s_sHistoryItemRef) >>= sTmp; - xItemList->removeByName(sTmp); - xOrderList->removeByName(sRemove); - } - - ::comphelper::ConfigurationHelper::flush(m_xCfg); - } - } - catch(const uno::Exception&) - { - DBG_UNHANDLED_EXCEPTION("unotools.config"); - } -} + uno::Reference<container::XNameAccess> xCfg = GetConfig(); + uno::Reference<container::XNameAccess> xListAccess(GetListAccess(xCfg, eHistory)); -void SvtHistoryOptions_Impl::Clear( EHistoryType eHistory ) -{ - uno::Reference<container::XNameAccess> xListAccess(GetListAccess(eHistory)); - if (!xListAccess.is()) - return; - - uno::Reference<container::XNameContainer> xNode; - - try - { // clear ItemList + uno::Reference<container::XNameContainer> xNode; xListAccess->getByName(s_sItemList) >>= xNode; Sequence<OUString> aStrings(xNode->getElementNames()); @@ -241,7 +87,7 @@ void SvtHistoryOptions_Impl::Clear( EHistoryType eHistory ) for (const auto& rString : std::as_const(aStrings)) xNode->removeByName(rString); - ::comphelper::ConfigurationHelper::flush(m_xCfg); + ::comphelper::ConfigurationHelper::flush(xCfg); } catch(const uno::Exception&) { @@ -249,92 +95,81 @@ void SvtHistoryOptions_Impl::Clear( EHistoryType eHistory ) } } -Sequence< Sequence<PropertyValue> > SvtHistoryOptions_Impl::GetList(EHistoryType eHistory) +std::vector< HistoryItem > GetList( EHistoryType eHistory ) { - uno::Reference<container::XNameAccess> xListAccess(GetListAccess(eHistory)); - if (!xListAccess.is()) - return Sequence< Sequence<PropertyValue> >(); - - impl_truncateList(eHistory, GetCapacity(eHistory)); - - Sequence<PropertyValue> seqProperties(5); - seqProperties[s_nOffsetURL ].Name = HISTORY_PROPERTYNAME_URL; - seqProperties[s_nOffsetFilter ].Name = HISTORY_PROPERTYNAME_FILTER; - seqProperties[s_nOffsetTitle ].Name = HISTORY_PROPERTYNAME_TITLE; - seqProperties[s_nOffsetPassword ].Name = HISTORY_PROPERTYNAME_PASSWORD; - seqProperties[s_nOffsetThumbnail ].Name = HISTORY_PROPERTYNAME_THUMBNAIL; - - uno::Reference<container::XNameAccess> xItemList; - uno::Reference<container::XNameAccess> xOrderList; + std::vector< HistoryItem > aRet; try { + uno::Reference<container::XNameAccess> xCfg = GetConfig(); + uno::Reference<container::XNameAccess> xCommonXCU = GetCommonXCU(); + uno::Reference<container::XNameAccess> xListAccess(GetListAccess(xCfg, eHistory)); + + TruncateList(xCfg, xListAccess, GetCapacity(xCommonXCU, eHistory)); + + uno::Reference<container::XNameAccess> xItemList; + uno::Reference<container::XNameAccess> xOrderList; xListAccess->getByName(s_sItemList) >>= xItemList; xListAccess->getByName(s_sOrderList) >>= xOrderList; - } - catch(const uno::Exception&) - { - DBG_UNHANDLED_EXCEPTION("unotools.config"); - } - const sal_Int32 nLength = xOrderList->getElementNames().getLength(); - Sequence< Sequence<PropertyValue> > aRet(nLength); - sal_Int32 nCount = 0; + const sal_Int32 nLength = xOrderList->getElementNames().getLength(); + aRet.reserve(nLength); - for (sal_Int32 nItem = 0; nItem < nLength; ++nItem) - { - try - { - OUString sUrl; - uno::Reference<beans::XPropertySet> xSet; - xOrderList->getByName(OUString::number(nItem)) >>= xSet; - xSet->getPropertyValue(s_sHistoryItemRef) >>= sUrl; - - xItemList->getByName(sUrl) >>= xSet; - seqProperties[s_nOffsetURL ].Value <<= sUrl; - - seqProperties[s_nOffsetFilter ].Value = xSet->getPropertyValue(s_sFilter); - seqProperties[s_nOffsetTitle ].Value = xSet->getPropertyValue(s_sTitle); - seqProperties[s_nOffsetPassword ].Value = xSet->getPropertyValue(s_sPassword); - seqProperties[s_nOffsetThumbnail].Value = xSet->getPropertyValue(s_sThumbnail); - aRet[nCount++] = seqProperties; - } - catch(const uno::Exception&) + for (sal_Int32 nItem = 0; nItem < nLength; ++nItem) { - // <https://bugs.libreoffice.org/show_bug.cgi?id=46074> - // "FILEOPEN: No Recent Documents..." discusses a problem - // with corrupted /org.openoffice.Office/Histories/Histories - // configuration items; to work around that problem, simply - // ignore such corrupted individual items here, so that at - // least newly added items are successfully reported back - // from this function: - DBG_UNHANDLED_EXCEPTION("unotools.config"); + try + { + OUString sUrl; + uno::Reference<beans::XPropertySet> xSet; + xOrderList->getByName(OUString::number(nItem)) >>= xSet; + xSet->getPropertyValue(s_sHistoryItemRef) >>= sUrl; + + xItemList->getByName(sUrl) >>= xSet; + HistoryItem aItem; + aItem.sURL = sUrl; + xSet->getPropertyValue(s_sFilter) >>= aItem.sFilter; + xSet->getPropertyValue(s_sTitle) >>= aItem.sTitle; + xSet->getPropertyValue(s_sPassword) >>= aItem.sPassword; + xSet->getPropertyValue(s_sThumbnail) >>= aItem.sThumbnail; + aRet.push_back(aItem); + } + catch(const uno::Exception&) + { + // <https://bugs.libreoffice.org/show_bug.cgi?id=46074> + // "FILEOPEN: No Recent Documents..." discusses a problem + // with corrupted /org.openoffice.Office/Histories/Histories + // configuration items; to work around that problem, simply + // ignore such corrupted individual items here, so that at + // least newly added items are successfully reported back + // from this function: + DBG_UNHANDLED_EXCEPTION("unotools.config"); + } } } - assert(nCount <= nLength); - aRet.realloc(nCount); + catch(const uno::Exception&) + { + DBG_UNHANDLED_EXCEPTION("unotools.config"); + } return aRet; } -void SvtHistoryOptions_Impl::AppendItem(EHistoryType eHistory, +void AppendItem(EHistoryType eHistory, const OUString& sURL, const OUString& sFilter, const OUString& sTitle, const std::optional<OUString>& sThumbnail) { - uno::Reference<container::XNameAccess> xListAccess(GetListAccess(eHistory)); - if (!xListAccess.is()) - return; - - impl_truncateList(eHistory, GetCapacity(eHistory)); + try + { + uno::Reference<container::XNameAccess> xCfg = GetConfig(); + uno::Reference<container::XNameAccess> xCommonXCU = GetCommonXCU(); + uno::Reference<container::XNameAccess> xListAccess(GetListAccess(xCfg, eHistory)); - sal_Int32 nMaxSize = GetCapacity(eHistory); - if (nMaxSize == 0) - return; + TruncateList(xCfg, xListAccess, GetCapacity(xCommonXCU, eHistory)); - uno::Reference<container::XNameContainer> xItemList; - uno::Reference<container::XNameContainer> xOrderList; - uno::Reference<beans::XPropertySet> xSet; + sal_Int32 nMaxSize = GetCapacity(xCommonXCU, eHistory); + if (nMaxSize == 0) + return; - try - { + uno::Reference<container::XNameContainer> xItemList; + uno::Reference<container::XNameContainer> xOrderList; xListAccess->getByName(s_sItemList) >>= xItemList; xListAccess->getByName(s_sOrderList) >>= xOrderList; sal_Int32 nLength = xOrderList->getElementNames().getLength(); @@ -342,6 +177,7 @@ void SvtHistoryOptions_Impl::AppendItem(EHistoryType eHistory, // The item to be appended already exists if (xItemList->hasByName(sURL)) { + uno::Reference<beans::XPropertySet> xSet; if (sThumbnail) { // update the thumbnail @@ -374,10 +210,11 @@ void SvtHistoryOptions_Impl::AppendItem(EHistoryType eHistory, } } - ::comphelper::ConfigurationHelper::flush(m_xCfg); + ::comphelper::ConfigurationHelper::flush(xCfg); } else // The item to be appended does not exist yet { + uno::Reference<beans::XPropertySet> xSet; uno::Reference<lang::XSingleServiceFactory> xFac; uno::Reference<uno::XInterface> xInst; uno::Reference<beans::XPropertySet> xPrevSet; @@ -436,7 +273,7 @@ void SvtHistoryOptions_Impl::AppendItem(EHistoryType eHistory, xSet->setPropertyValue(s_sPassword, uno::makeAny(OUString())); xSet->setPropertyValue(s_sThumbnail, uno::makeAny(sThumbnail.value_or(OUString()))); - ::comphelper::ConfigurationHelper::flush(m_xCfg); + ::comphelper::ConfigurationHelper::flush(xCfg); } } catch(const uno::Exception&) @@ -445,18 +282,15 @@ void SvtHistoryOptions_Impl::AppendItem(EHistoryType eHistory, } } -void SvtHistoryOptions_Impl::DeleteItem(EHistoryType eHistory, const OUString& sURL) +void DeleteItem(EHistoryType eHistory, const OUString& sURL) { - uno::Reference<container::XNameAccess> xListAccess(GetListAccess(eHistory)); - if (!xListAccess.is()) - return; - - uno::Reference<container::XNameContainer> xItemList; - uno::Reference<container::XNameContainer> xOrderList; - uno::Reference<beans::XPropertySet> xSet; - try { + uno::Reference<container::XNameAccess> xCfg = GetConfig(); + uno::Reference<container::XNameAccess> xListAccess(GetListAccess(xCfg, eHistory)); + + uno::Reference<container::XNameContainer> xItemList; + uno::Reference<container::XNameContainer> xOrderList; xListAccess->getByName(s_sItemList) >>= xItemList; xListAccess->getByName(s_sOrderList) >>= xOrderList; sal_Int32 nLength = xOrderList->getElementNames().getLength(); @@ -476,6 +310,7 @@ void SvtHistoryOptions_Impl::DeleteItem(EHistoryType eHistory, const OUString& s sal_Int32 nFromWhere = 0; for (; nFromWhere < nLength - 1; ++nFromWhere) { + uno::Reference<beans::XPropertySet> xSet; OUString aItem; xOrderList->getByName(OUString::number(nFromWhere)) >>= xSet; xSet->getPropertyValue(s_sHistoryItemRef) >>= aItem; @@ -501,7 +336,7 @@ void SvtHistoryOptions_Impl::DeleteItem(EHistoryType eHistory, const OUString& s // and finally remove it from the ItemList xItemList->removeByName(sURL); - ::comphelper::ConfigurationHelper::flush(m_xCfg); + ::comphelper::ConfigurationHelper::flush(xCfg); } catch (const uno::Exception&) { @@ -509,60 +344,95 @@ void SvtHistoryOptions_Impl::DeleteItem(EHistoryType eHistory, const OUString& s } } -namespace { +} // namespace -std::weak_ptr<SvtHistoryOptions_Impl> g_pHistoryOptions; +static uno::Reference<container::XNameAccess> GetConfig() +{ + return uno::Reference<container::XNameAccess>( + ::comphelper::ConfigurationHelper::openConfig( + ::comphelper::getProcessComponentContext(), + "org.openoffice.Office.Histories/Histories", + ::comphelper::EConfigurationModes::Standard), + uno::UNO_QUERY_THROW); } -SvtHistoryOptions::SvtHistoryOptions() +static uno::Reference<container::XNameAccess> GetCommonXCU() { - MutexGuard aGuard(theHistoryOptionsMutex::get()); - - m_pImpl = g_pHistoryOptions.lock(); - if( !m_pImpl ) - { - m_pImpl = std::make_shared<SvtHistoryOptions_Impl>(); - g_pHistoryOptions = m_pImpl; - ItemHolder1::holdConfigItem(EItem::HistoryOptions); - } + return uno::Reference<container::XNameAccess>( + ::comphelper::ConfigurationHelper::openConfig( + ::comphelper::getProcessComponentContext(), + "org.openoffice.Office.Common/History", + ::comphelper::EConfigurationModes::Standard), + uno::UNO_QUERY_THROW); } -SvtHistoryOptions::~SvtHistoryOptions() +static uno::Reference<container::XNameAccess> GetListAccess( + const uno::Reference<container::XNameAccess>& xCfg, + EHistoryType eHistory) { - MutexGuard aGuard(theHistoryOptionsMutex::get()); + uno::Reference<container::XNameAccess> xListAccess; + switch (eHistory) + { + case EHistoryType::PickList: + xCfg->getByName("PickList") >>= xListAccess; + break; - m_pImpl.reset(); + case EHistoryType::HelpBookmarks: + xCfg->getByName("HelpBookmarks") >>= xListAccess; + break; + } + return xListAccess; } -void SvtHistoryOptions::Clear( EHistoryType eHistory ) +static void TruncateList( + const uno::Reference<container::XNameAccess>& xCfg, + const uno::Reference<container::XNameAccess>& xList, + sal_uInt32 nSize) { - MutexGuard aGuard(theHistoryOptionsMutex::get()); + uno::Reference<container::XNameContainer> xItemList; + uno::Reference<container::XNameContainer> xOrderList; + xList->getByName(s_sOrderList) >>= xOrderList; + xList->getByName(s_sItemList) >>= xItemList; + + const sal_uInt32 nLength = xOrderList->getElementNames().getLength(); + if (nSize < nLength) + { + for (sal_uInt32 i=nLength-1; i>=nSize; --i) + { + uno::Reference<beans::XPropertySet> xSet; + OUString sTmp; + const OUString sRemove = OUString::number(i); + xOrderList->getByName(sRemove) >>= xSet; + xSet->getPropertyValue(s_sHistoryItemRef) >>= sTmp; + xItemList->removeByName(sTmp); + xOrderList->removeByName(sRemove); + } - m_pImpl->Clear( eHistory ); + ::comphelper::ConfigurationHelper::flush(xCfg); + } } -Sequence< Sequence< PropertyValue > > SvtHistoryOptions::GetList( EHistoryType eHistory ) const -{ - MutexGuard aGuard(theHistoryOptionsMutex::get()); - return m_pImpl->GetList( eHistory ); -} -void SvtHistoryOptions::AppendItem(EHistoryType eHistory, - const OUString& sURL, const OUString& sFilter, const OUString& sTitle, - const std::optional<OUString>& sThumbnail) +static sal_uInt32 GetCapacity(const uno::Reference<container::XNameAccess>& xCommonXCU, EHistoryType eHistory) { - MutexGuard aGuard(theHistoryOptionsMutex::get()); + uno::Reference<beans::XPropertySet> xListAccess(xCommonXCU, uno::UNO_QUERY_THROW); - m_pImpl->AppendItem(eHistory, sURL, sFilter, sTitle, sThumbnail); -} + sal_uInt32 nSize = 0; -void SvtHistoryOptions::DeleteItem(EHistoryType eHistory, const OUString& sURL) -{ - MutexGuard aGuard(theHistoryOptionsMutex::get()); + switch (eHistory) + { + case EHistoryType::PickList: + xListAccess->getPropertyValue("PickListSize") >>= nSize; + break; + + case EHistoryType::HelpBookmarks: + xListAccess->getPropertyValue("HelpBookmarkSize") >>= nSize; + break; + } - m_pImpl->DeleteItem(eHistory, sURL); + return nSize; } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/itemholder1.cxx b/unotools/source/config/itemholder1.cxx index 7183ea2e12f9..a5a78edbd299 100644 --- a/unotools/source/config/itemholder1.cxx +++ b/unotools/source/config/itemholder1.cxx @@ -129,10 +129,6 @@ void ItemHolder1::impl_newItem(TItemInfo& rItem) //rItem.pItem.reset( new GlobalEventConfig() ); break; - case EItem::HistoryOptions : - rItem.pItem.reset( new SvtHistoryOptions() ); - break; - case EItem::LinguConfig : rItem.pItem.reset( new SvtLinguConfig() ); break; |