1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sd.hxx"
26
27 #include "SlsCacheConfiguration.hxx"
28 #include <vos/mutex.hxx>
29 #include <vcl/svapp.hxx>
30
31 #include <comphelper/processfactory.hxx>
32 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
34 #ifndef _COM_SUN_STAR_CONTAINER_PROPERTYVALUE_HPP_
35 #include <com/sun/star/beans/PropertyValue.hpp>
36 #endif
37
38 using namespace ::com::sun::star;
39 using namespace ::com::sun::star::uno;
40
41 namespace sd { namespace slidesorter { namespace cache {
42
43 ::boost::shared_ptr<CacheConfiguration> CacheConfiguration::mpInstance;
44 ::boost::weak_ptr<CacheConfiguration> CacheConfiguration::mpWeakInstance;
45 Timer CacheConfiguration::maReleaseTimer;
46
47
48
Instance(void)49 ::boost::shared_ptr<CacheConfiguration> CacheConfiguration::Instance (void)
50 {
51 ::vos::OGuard aSolarGuard (Application::GetSolarMutex());
52 if (mpInstance.get() == NULL)
53 {
54 // Maybe somebody else kept a previously created instance alive.
55 if ( ! mpWeakInstance.expired())
56 mpInstance = ::boost::shared_ptr<CacheConfiguration>(mpWeakInstance);
57 if (mpInstance.get() == NULL)
58 {
59 // We have to create a new instance.
60 mpInstance.reset(new CacheConfiguration());
61 mpWeakInstance = mpInstance;
62 // Prepare to release this instance in the near future.
63 maReleaseTimer.SetTimeoutHdl(
64 LINK(mpInstance.get(),CacheConfiguration,TimerCallback));
65 maReleaseTimer.SetTimeout(5000 /* 5s */);
66 maReleaseTimer.Start();
67 }
68 }
69 return mpInstance;
70 }
71
72
73
74
CacheConfiguration(void)75 CacheConfiguration::CacheConfiguration (void)
76 {
77 // Get the cache size from configuration.
78 const ::rtl::OUString sConfigurationProviderServiceName(
79 RTL_CONSTASCII_USTRINGPARAM(
80 "com.sun.star.configuration.ConfigurationProvider"));
81 const ::rtl::OUString sPathToImpressConfigurationRoot(
82 RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Office.Impress/"));
83 const ::rtl::OUString sPathToNode(
84 RTL_CONSTASCII_USTRINGPARAM(
85 "MultiPaneGUI/SlideSorter/PreviewCache"));
86
87 try
88 {
89 do
90 {
91 // Obtain access to the configuration.
92 Reference<lang::XMultiServiceFactory> xProvider (
93 ::comphelper::getProcessServiceFactory()->createInstance(
94 sConfigurationProviderServiceName),
95 UNO_QUERY);
96 if ( ! xProvider.is())
97 break;
98
99 // Obtain access to Impress configuration.
100 Sequence<Any> aCreationArguments(3);
101 aCreationArguments[0] = makeAny(beans::PropertyValue(
102 ::rtl::OUString(
103 RTL_CONSTASCII_USTRINGPARAM("nodepath")),
104 0,
105 makeAny(sPathToImpressConfigurationRoot),
106 beans::PropertyState_DIRECT_VALUE));
107 aCreationArguments[1] = makeAny(beans::PropertyValue(
108 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("depth")),
109 0,
110 makeAny((sal_Int32)-1),
111 beans::PropertyState_DIRECT_VALUE));
112 aCreationArguments[2] = makeAny(beans::PropertyValue(
113 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("lazywrite")),
114 0,
115 makeAny(true),
116 beans::PropertyState_DIRECT_VALUE));
117 ::rtl::OUString sAccessService (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
118 "com.sun.star.configuration.ConfigurationAccess")));
119 Reference<XInterface> xRoot (xProvider->createInstanceWithArguments(
120 sAccessService, aCreationArguments));
121 if ( ! xRoot.is())
122 break;
123 Reference<container::XHierarchicalNameAccess> xHierarchy (xRoot, UNO_QUERY);
124 if ( ! xHierarchy.is())
125 break;
126
127 // Get the node for the slide sorter preview cache.
128 mxCacheNode = Reference<container::XNameAccess>(
129 xHierarchy->getByHierarchicalName(sPathToNode),
130 UNO_QUERY);
131 }
132 while (false);
133 }
134 catch (RuntimeException aException)
135 {
136 (void)aException;
137 }
138 catch (Exception aException)
139 {
140 (void)aException;
141 }
142 }
143
144
145
146
GetValue(const::rtl::OUString & rName)147 Any CacheConfiguration::GetValue (const ::rtl::OUString& rName)
148 {
149 Any aResult;
150
151 if (mxCacheNode != NULL)
152 {
153 try
154 {
155 aResult = mxCacheNode->getByName(rName);
156 }
157 catch (Exception aException)
158 {
159 (void)aException;
160 }
161 }
162
163 return aResult;
164 }
165
166
167
168
IMPL_LINK(CacheConfiguration,TimerCallback,Timer *,EMPTYARG)169 IMPL_LINK(CacheConfiguration,TimerCallback, Timer*,EMPTYARG)
170 {
171 // Release out reference to the instance.
172 mpInstance.reset();
173 return 0;
174 }
175
176
177 } } } // end of namespace ::sd::slidesorter::cache
178