LibreOffice Module sd (master) 1
SlsCacheConfiguration.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21#include <vcl/svapp.hxx>
22
25#include <com/sun/star/lang/XMultiServiceFactory.hpp>
26#include <com/sun/star/container/XHierarchicalNameAccess.hpp>
27#include <com/sun/star/container/XNameAccess.hpp>
28#include <com/sun/star/configuration/theDefaultProvider.hpp>
29
30using namespace ::com::sun::star;
31using namespace ::com::sun::star::uno;
32
33namespace sd::slidesorter::cache {
34
35namespace
36{
37 typedef std::shared_ptr<CacheConfiguration> CacheConfigSharedPtr;
38 CacheConfigSharedPtr& theInstance()
39 {
40 static CacheConfigSharedPtr SINGLETON;
41 return SINGLETON;
42 }
43}
44
45std::weak_ptr<CacheConfiguration> CacheConfiguration::mpWeakInstance;
46
47std::shared_ptr<CacheConfiguration> CacheConfiguration::Instance()
48{
49 SolarMutexGuard aSolarGuard;
50 CacheConfigSharedPtr &rInstancePtr = theInstance();
51 if (!rInstancePtr)
52 {
53 // Maybe somebody else kept a previously created instance alive.
54 if ( ! mpWeakInstance.expired())
55 rInstancePtr = std::shared_ptr<CacheConfiguration>(mpWeakInstance);
56 if (!rInstancePtr)
57 {
58 // We have to create a new instance.
59 rInstancePtr.reset(new CacheConfiguration());
60 mpWeakInstance = rInstancePtr;
61 // Prepare to release this instance in the near future.
62 rInstancePtr->m_ReleaseTimer.SetInvokeHandler(
63 LINK(rInstancePtr.get(),CacheConfiguration,TimerCallback));
64 rInstancePtr->m_ReleaseTimer.SetTimeout(5000 /* 5s */);
65 rInstancePtr->m_ReleaseTimer.Start();
66 }
67 }
68 return rInstancePtr;
69}
70
72 : m_ReleaseTimer("sd::CacheConfiguration maReleaseTimer")
73{
74 // Get the cache size from configuration.
75 try
76 {
77 // Obtain access to the configuration.
78 Reference<lang::XMultiServiceFactory> xProvider =
79 configuration::theDefaultProvider::get( ::comphelper::getProcessComponentContext() );
80
81 // Obtain access to Impress configuration.
82 Sequence<Any> aCreationArguments(comphelper::InitAnyPropertySequence(
83 {
84 {"nodepath", Any(OUString("/org.openoffice.Office.Impress/"))},
85 {"depth", Any(sal_Int32(-1))}
86 }));
87
88 Reference<XInterface> xRoot (xProvider->createInstanceWithArguments(
89 "com.sun.star.configuration.ConfigurationAccess",
90 aCreationArguments));
91 if ( ! xRoot.is())
92 return;
93 Reference<container::XHierarchicalNameAccess> xHierarchy (xRoot, UNO_QUERY);
94 if ( ! xHierarchy.is())
95 return;
96
97 // Get the node for the slide sorter preview cache.
98 mxCacheNode.set( xHierarchy->getByHierarchicalName("MultiPaneGUI/SlideSorter/PreviewCache"), UNO_QUERY);
99 }
100 catch (RuntimeException &)
101 {
102 }
103 catch (Exception &)
104 {
105 }
106}
107
108Any CacheConfiguration::GetValue (const OUString& rName)
109{
110 Any aResult;
111
112 if (mxCacheNode != nullptr)
113 {
114 try
115 {
116 aResult = mxCacheNode->getByName(rName);
117 }
118 catch (Exception &)
119 {
120 }
121 }
122
123 return aResult;
124}
125
127{
128 CacheConfigSharedPtr &rInstancePtr = theInstance();
129 // Release our reference to the instance.
130 rInstancePtr.reset();
131 // note: if there are no other references to the instance, m_ReleaseTimer
132 // will be deleted now
133}
134
136{
137 CacheConfigSharedPtr &rInstancePtr = theInstance();
138 rInstancePtr.reset();
139 assert(mpWeakInstance.expired()); // ensure m_ReleaseTimer is destroyed
140}
141
142} // end of namespace ::sd::slidesorter::cache
143
144/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
A very simple and easy-to-use access to configuration entries regarding the slide sorter cache.
static std::shared_ptr< CacheConfiguration > Instance()
Return an instance to this class.
css::uno::Any GetValue(const OUString &rName)
Look up the specified value in MultiPaneGUI/SlideSorter/PreviewCache.
static std::weak_ptr< CacheConfiguration > mpWeakInstance
When a caller holds a reference after we have released ours we use this weak pointer to avoid creatin...
css::uno::Reference< css::container::XNameAccess > mxCacheNode
@ Exception
css::uno::Sequence< css::uno::Any > InitAnyPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)
IMPL_STATIC_LINK_NOARG(CacheConfiguration, TimerCallback, Timer *, void)