1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "SlsCacheConfiguration.hxx"
21 #include <rtl/instance.hxx>
22 #include <vcl/svapp.hxx>
24 #include <comphelper/processfactory.hxx>
25 #include <comphelper/propertysequence.hxx>
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
28 #include <com/sun/star/container/XNameAccess.hpp>
29 #include <com/sun/star/configuration/theDefaultProvider.hpp>
30 #include <com/sun/star/beans/PropertyValue.hpp>
32 using namespace ::com::sun::star
;
33 using namespace ::com::sun::star::uno
;
35 namespace sd
{ namespace slidesorter
{ namespace cache
{
39 typedef std::shared_ptr
<CacheConfiguration
> CacheConfigSharedPtr
;
41 public rtl::Static
<CacheConfigSharedPtr
, theInstance
> {};
44 std::weak_ptr
<CacheConfiguration
> CacheConfiguration::mpWeakInstance
;
46 std::shared_ptr
<CacheConfiguration
> CacheConfiguration::Instance()
48 SolarMutexGuard aSolarGuard
;
49 CacheConfigSharedPtr
&rInstancePtr
= theInstance::get();
50 if (rInstancePtr
.get() == nullptr)
52 // Maybe somebody else kept a previously created instance alive.
53 if ( ! mpWeakInstance
.expired())
54 rInstancePtr
= std::shared_ptr
<CacheConfiguration
>(mpWeakInstance
);
55 if (rInstancePtr
.get() == nullptr)
57 // We have to create a new instance.
58 rInstancePtr
.reset(new CacheConfiguration());
59 mpWeakInstance
= rInstancePtr
;
60 // Prepare to release this instance in the near future.
61 rInstancePtr
->m_ReleaseTimer
.SetInvokeHandler(
62 LINK(rInstancePtr
.get(),CacheConfiguration
,TimerCallback
));
63 rInstancePtr
->m_ReleaseTimer
.SetTimeout(5000 /* 5s */);
64 rInstancePtr
->m_ReleaseTimer
.SetDebugName("sd::CacheConfiguration maReleaseTimer");
65 rInstancePtr
->m_ReleaseTimer
.Start();
71 CacheConfiguration::CacheConfiguration()
73 // Get the cache size from configuration.
74 const OUString
sPathToImpressConfigurationRoot("/org.openoffice.Office.Impress/");
75 const OUString
sPathToNode("MultiPaneGUI/SlideSorter/PreviewCache");
79 // Obtain access to the configuration.
80 Reference
<lang::XMultiServiceFactory
> xProvider
=
81 configuration::theDefaultProvider::get( ::comphelper::getProcessComponentContext() );
83 // Obtain access to Impress configuration.
84 Sequence
<Any
> aCreationArguments(comphelper::InitAnyPropertySequence(
86 {"nodepath", makeAny(sPathToImpressConfigurationRoot
)},
87 {"depth", makeAny(sal_Int32(-1))}
90 Reference
<XInterface
> xRoot (xProvider
->createInstanceWithArguments(
91 "com.sun.star.configuration.ConfigurationAccess",
95 Reference
<container::XHierarchicalNameAccess
> xHierarchy (xRoot
, UNO_QUERY
);
96 if ( ! xHierarchy
.is())
99 // Get the node for the slide sorter preview cache.
100 mxCacheNode
.set( xHierarchy
->getByHierarchicalName(sPathToNode
), UNO_QUERY
);
102 catch (RuntimeException
&)
110 Any
CacheConfiguration::GetValue (const OUString
& rName
)
114 if (mxCacheNode
!= nullptr)
118 aResult
= mxCacheNode
->getByName(rName
);
128 IMPL_STATIC_LINK_NOARG(CacheConfiguration
, TimerCallback
, Timer
*, void)
130 CacheConfigSharedPtr
&rInstancePtr
= theInstance::get();
131 // Release our reference to the instance.
132 rInstancePtr
.reset();
133 // note: if there are no other references to the instance, m_ReleaseTimer
134 // will be deleted now
137 void CacheConfiguration::Shutdown()
139 CacheConfigSharedPtr
&rInstancePtr
= theInstance::get();
140 rInstancePtr
.reset();
141 assert(mpWeakInstance
.expired()); // ensure m_ReleaseTimer is destroyed
144 } } } // end of namespace ::sd::slidesorter::cache
146 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */