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