Line data Source code
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 :
20 : #include "SlsCacheConfiguration.hxx"
21 : #include <osl/mutex.hxx>
22 : #include <rtl/instance.hxx>
23 : #include <vcl/svapp.hxx>
24 :
25 : #include <comphelper/processfactory.hxx>
26 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27 : #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
28 : #include <com/sun/star/configuration/theDefaultProvider.hpp>
29 : #include <com/sun/star/beans/PropertyValue.hpp>
30 :
31 : using namespace ::com::sun::star;
32 : using namespace ::com::sun::star::uno;
33 :
34 : namespace sd { namespace slidesorter { namespace cache {
35 :
36 : namespace
37 : {
38 : typedef ::boost::shared_ptr<CacheConfiguration> CacheConfigSharedPtr;
39 : class theInstance :
40 : public rtl::Static<CacheConfigSharedPtr, theInstance> {};
41 : }
42 :
43 22 : ::boost::weak_ptr<CacheConfiguration> CacheConfiguration::mpWeakInstance;
44 22 : Timer CacheConfiguration::maReleaseTimer;
45 :
46 384 : ::boost::shared_ptr<CacheConfiguration> CacheConfiguration::Instance()
47 : {
48 384 : SolarMutexGuard aSolarGuard;
49 384 : CacheConfigSharedPtr &rInstancePtr = theInstance::get();
50 384 : if (rInstancePtr.get() == NULL)
51 : {
52 : // Maybe somebody else kept a previously created instance alive.
53 34 : if ( ! mpWeakInstance.expired())
54 0 : rInstancePtr = ::boost::shared_ptr<CacheConfiguration>(mpWeakInstance);
55 34 : if (rInstancePtr.get() == NULL)
56 : {
57 : // We have to create a new instance.
58 34 : rInstancePtr.reset(new CacheConfiguration());
59 34 : mpWeakInstance = rInstancePtr;
60 : // Prepare to release this instance in the near future.
61 : maReleaseTimer.SetTimeoutHdl(
62 34 : LINK(rInstancePtr.get(),CacheConfiguration,TimerCallback));
63 34 : maReleaseTimer.SetTimeout(5000 /* 5s */);
64 34 : maReleaseTimer.Start();
65 : }
66 : }
67 384 : return rInstancePtr;
68 : }
69 :
70 34 : CacheConfiguration::CacheConfiguration()
71 : {
72 : // Get the cache size from configuration.
73 34 : const OUString sPathToImpressConfigurationRoot("/org.openoffice.Office.Impress/");
74 68 : const OUString sPathToNode("MultiPaneGUI/SlideSorter/PreviewCache");
75 :
76 : try
77 : {
78 : // Obtain access to the configuration.
79 : Reference<lang::XMultiServiceFactory> xProvider =
80 34 : configuration::theDefaultProvider::get( ::comphelper::getProcessComponentContext() );
81 :
82 : // Obtain access to Impress configuration.
83 68 : Sequence<Any> aCreationArguments(3);
84 68 : aCreationArguments[0] = makeAny(beans::PropertyValue(
85 : "nodepath",
86 : 0,
87 : makeAny(sPathToImpressConfigurationRoot),
88 34 : beans::PropertyState_DIRECT_VALUE));
89 68 : aCreationArguments[1] = makeAny(beans::PropertyValue(
90 : "depth",
91 : 0,
92 : makeAny((sal_Int32)-1),
93 34 : beans::PropertyState_DIRECT_VALUE));
94 68 : aCreationArguments[2] = makeAny(beans::PropertyValue(
95 : "lazywrite",
96 : 0,
97 : makeAny(true),
98 34 : beans::PropertyState_DIRECT_VALUE));
99 :
100 34 : Reference<XInterface> xRoot (xProvider->createInstanceWithArguments(
101 : "com.sun.star.configuration.ConfigurationAccess",
102 68 : aCreationArguments));
103 34 : if ( ! xRoot.is())
104 0 : return;
105 68 : Reference<container::XHierarchicalNameAccess> xHierarchy (xRoot, UNO_QUERY);
106 34 : if ( ! xHierarchy.is())
107 0 : return;
108 :
109 : // Get the node for the slide sorter preview cache.
110 34 : mxCacheNode = Reference<container::XNameAccess>(
111 34 : xHierarchy->getByHierarchicalName(sPathToNode),
112 34 : UNO_QUERY);
113 : }
114 0 : catch (RuntimeException &)
115 : {
116 : }
117 34 : catch (Exception &)
118 : {
119 34 : }
120 : }
121 :
122 384 : Any CacheConfiguration::GetValue (const OUString& rName)
123 : {
124 384 : Any aResult;
125 :
126 384 : if (mxCacheNode != NULL)
127 : {
128 : try
129 : {
130 0 : aResult = mxCacheNode->getByName(rName);
131 : }
132 0 : catch (Exception &)
133 : {
134 : }
135 : }
136 :
137 384 : return aResult;
138 : }
139 :
140 56 : IMPL_STATIC_LINK_NOARG_TYPED(CacheConfiguration, TimerCallback, Timer *, void)
141 : {
142 28 : CacheConfigSharedPtr &rInstancePtr = theInstance::get();
143 : // Release our reference to the instance.
144 28 : rInstancePtr.reset();
145 28 : }
146 :
147 66 : } } } // end of namespace ::sd::slidesorter::cache
148 :
149 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|