xref: /AOO41X/main/sd/source/ui/slidesorter/cache/SlsCacheConfiguration.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sd.hxx"
30 
31 #include "SlsCacheConfiguration.hxx"
32 #include <vos/mutex.hxx>
33 #include <vcl/svapp.hxx>
34 
35 #include <comphelper/processfactory.hxx>
36 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
37 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
38 #ifndef _COM_SUN_STAR_CONTAINER_PROPERTYVALUE_HPP_
39 #include <com/sun/star/beans/PropertyValue.hpp>
40 #endif
41 
42 using namespace ::com::sun::star;
43 using namespace ::com::sun::star::uno;
44 
45 namespace sd { namespace slidesorter { namespace cache {
46 
47 ::boost::shared_ptr<CacheConfiguration> CacheConfiguration::mpInstance;
48 ::boost::weak_ptr<CacheConfiguration> CacheConfiguration::mpWeakInstance;
49 Timer CacheConfiguration::maReleaseTimer;
50 
51 
52 
53 ::boost::shared_ptr<CacheConfiguration> CacheConfiguration::Instance (void)
54 {
55     ::vos::OGuard aSolarGuard (Application::GetSolarMutex());
56     if (mpInstance.get() == NULL)
57     {
58         // Maybe somebody else kept a previously created instance alive.
59         if ( ! mpWeakInstance.expired())
60             mpInstance = ::boost::shared_ptr<CacheConfiguration>(mpWeakInstance);
61         if (mpInstance.get() == NULL)
62         {
63             // We have to create a new instance.
64             mpInstance.reset(new CacheConfiguration());
65             mpWeakInstance = mpInstance;
66             // Prepare to release this instance in the near future.
67             maReleaseTimer.SetTimeoutHdl(
68                 LINK(mpInstance.get(),CacheConfiguration,TimerCallback));
69             maReleaseTimer.SetTimeout(5000 /* 5s */);
70             maReleaseTimer.Start();
71         }
72     }
73     return mpInstance;
74 }
75 
76 
77 
78 
79 CacheConfiguration::CacheConfiguration (void)
80 {
81     // Get the cache size from configuration.
82     const ::rtl::OUString sConfigurationProviderServiceName(
83         RTL_CONSTASCII_USTRINGPARAM(
84             "com.sun.star.configuration.ConfigurationProvider"));
85     const ::rtl::OUString sPathToImpressConfigurationRoot(
86         RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Office.Impress/"));
87     const ::rtl::OUString sPathToNode(
88         RTL_CONSTASCII_USTRINGPARAM(
89             "MultiPaneGUI/SlideSorter/PreviewCache"));
90 
91     try
92     {
93         do
94         {
95             // Obtain access to the configuration.
96             Reference<lang::XMultiServiceFactory> xProvider (
97                 ::comphelper::getProcessServiceFactory()->createInstance(
98                     sConfigurationProviderServiceName),
99                 UNO_QUERY);
100             if ( ! xProvider.is())
101                 break;
102 
103             // Obtain access to Impress configuration.
104             Sequence<Any> aCreationArguments(3);
105             aCreationArguments[0] = makeAny(beans::PropertyValue(
106                 ::rtl::OUString(
107                     RTL_CONSTASCII_USTRINGPARAM("nodepath")),
108                 0,
109                 makeAny(sPathToImpressConfigurationRoot),
110                 beans::PropertyState_DIRECT_VALUE));
111             aCreationArguments[1] = makeAny(beans::PropertyValue(
112                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("depth")),
113                 0,
114                 makeAny((sal_Int32)-1),
115                 beans::PropertyState_DIRECT_VALUE));
116             aCreationArguments[2] = makeAny(beans::PropertyValue(
117                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("lazywrite")),
118                 0,
119                 makeAny(true),
120                 beans::PropertyState_DIRECT_VALUE));
121             ::rtl::OUString sAccessService (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
122                 "com.sun.star.configuration.ConfigurationAccess")));
123             Reference<XInterface> xRoot (xProvider->createInstanceWithArguments(
124                 sAccessService, aCreationArguments));
125             if ( ! xRoot.is())
126                 break;
127             Reference<container::XHierarchicalNameAccess> xHierarchy (xRoot, UNO_QUERY);
128             if ( ! xHierarchy.is())
129                 break;
130 
131             // Get the node for the slide sorter preview cache.
132             mxCacheNode = Reference<container::XNameAccess>(
133                 xHierarchy->getByHierarchicalName(sPathToNode),
134                 UNO_QUERY);
135         }
136         while (false);
137     }
138     catch (RuntimeException aException)
139     {
140         (void)aException;
141     }
142     catch (Exception aException)
143     {
144         (void)aException;
145     }
146 }
147 
148 
149 
150 
151 Any CacheConfiguration::GetValue (const ::rtl::OUString& rName)
152 {
153     Any aResult;
154 
155     if (mxCacheNode != NULL)
156     {
157         try
158         {
159             aResult = mxCacheNode->getByName(rName);
160         }
161         catch (Exception aException)
162         {
163             (void)aException;
164         }
165     }
166 
167     return aResult;
168 }
169 
170 
171 
172 
173 IMPL_LINK(CacheConfiguration,TimerCallback, Timer*,EMPTYARG)
174 {
175     // Release out reference to the instance.
176     mpInstance.reset();
177     return 0;
178 }
179 
180 
181 } } } // end of namespace ::sd::slidesorter::cache
182