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 <accelerators/acceleratorconfiguration.hxx>
21 : #include <accelerators/istoragelistener.hxx>
22 : #include <accelerators/presethandler.hxx>
23 :
24 : #include <xml/acceleratorconfigurationreader.hxx>
25 : #include <xml/acceleratorconfigurationwriter.hxx>
26 : #include <xml/saxnamespacefilter.hxx>
27 :
28 : #include <acceleratorconst.h>
29 :
30 : #include <com/sun/star/lang/XServiceInfo.hpp>
31 : #include <com/sun/star/ui/XUIConfigurationStorage.hpp>
32 :
33 : #include <cppuhelper/implbase1.hxx>
34 : #include <cppuhelper/supportsservice.hxx>
35 : #include <comphelper/sequenceashashmap.hxx>
36 : #include <i18nlangtag/languagetag.hxx>
37 : #include <rtl/ref.hxx>
38 : #include <vcl/svapp.hxx>
39 :
40 : using namespace framework;
41 :
42 : namespace {
43 :
44 : /**
45 : implements a read/write access to a document
46 : based accelerator configuration.
47 : */
48 :
49 : typedef ::cppu::ImplInheritanceHelper1<
50 : XMLBasedAcceleratorConfiguration,
51 : css::lang::XServiceInfo> DocumentAcceleratorConfiguration_BASE;
52 :
53 : class DocumentAcceleratorConfiguration : public DocumentAcceleratorConfiguration_BASE
54 : {
55 : private:
56 :
57 : /** points to the root storage of the outside document,
58 : where we can read/save our configuration data. */
59 : css::uno::Reference< css::embed::XStorage > m_xDocumentRoot;
60 :
61 : public:
62 :
63 : /** initialize this instance and fill the internal cache.
64 :
65 : @param xSMGR
66 : reference to an uno service manager, which is used internally.
67 : */
68 : DocumentAcceleratorConfiguration(
69 : const css::uno::Reference< css::uno::XComponentContext >& xContext,
70 : const css::uno::Sequence< css::uno::Any >& lArguments);
71 :
72 : virtual ~DocumentAcceleratorConfiguration();
73 :
74 0 : virtual OUString SAL_CALL getImplementationName()
75 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
76 : {
77 0 : return OUString("com.sun.star.comp.framework.DocumentAcceleratorConfiguration");
78 : }
79 :
80 0 : virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName)
81 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
82 : {
83 0 : return cppu::supportsService(this, ServiceName);
84 : }
85 :
86 0 : virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames()
87 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
88 : {
89 0 : css::uno::Sequence< OUString > aSeq(1);
90 0 : aSeq[0] = OUString("com.sun.star.ui.DocumentAcceleratorConfiguration");
91 0 : return aSeq;
92 : }
93 :
94 : // XUIConfigurationStorage
95 : virtual void SAL_CALL setStorage(const css::uno::Reference< css::embed::XStorage >& xStorage)
96 : throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
97 :
98 : virtual sal_Bool SAL_CALL hasStorage()
99 : throw(css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
100 :
101 : private:
102 :
103 : /** read all data into the cache. */
104 : void impl_ts_fillCache();
105 :
106 : /** forget all currently cached data AND(!)
107 : forget all currently used storages. */
108 : void impl_ts_clearCache();
109 : };
110 :
111 0 : DocumentAcceleratorConfiguration::DocumentAcceleratorConfiguration(
112 : const css::uno::Reference< css::uno::XComponentContext >& xContext,
113 : const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& lArguments)
114 0 : : DocumentAcceleratorConfiguration_BASE(xContext)
115 : {
116 : {
117 0 : SolarMutexGuard g;
118 0 : css::uno::Reference<css::embed::XStorage> xRoot;
119 0 : if (lArguments.getLength() == 1 && (lArguments[0] >>= xRoot))
120 : {
121 0 : m_xDocumentRoot = xRoot;
122 : }
123 : else
124 : {
125 0 : ::comphelper::SequenceAsHashMap lArgs(lArguments);
126 0 : m_xDocumentRoot = lArgs.getUnpackedValueOrDefault(
127 : OUString("DocumentRoot"),
128 0 : css::uno::Reference< css::embed::XStorage >());
129 0 : }
130 : }
131 :
132 0 : impl_ts_fillCache();
133 0 : }
134 :
135 0 : DocumentAcceleratorConfiguration::~DocumentAcceleratorConfiguration()
136 : {
137 0 : m_aPresetHandler.removeStorageListener(this);
138 0 : }
139 :
140 0 : void SAL_CALL DocumentAcceleratorConfiguration::setStorage(const css::uno::Reference< css::embed::XStorage >& xStorage)
141 : throw(css::uno::RuntimeException, std::exception)
142 : {
143 : // Attention! xStorage must be accepted too, if it's NULL !
144 :
145 : bool bForgetOldStorages;
146 : {
147 0 : SolarMutexGuard g;
148 0 : bForgetOldStorages = m_xDocumentRoot.is();
149 0 : m_xDocumentRoot = xStorage;
150 : }
151 :
152 0 : if (bForgetOldStorages)
153 0 : impl_ts_clearCache();
154 :
155 0 : if (xStorage.is())
156 0 : impl_ts_fillCache();
157 0 : }
158 :
159 0 : sal_Bool SAL_CALL DocumentAcceleratorConfiguration::hasStorage()
160 : throw(css::uno::RuntimeException, std::exception)
161 : {
162 0 : SolarMutexGuard g;
163 0 : return m_xDocumentRoot.is();
164 : }
165 :
166 0 : void DocumentAcceleratorConfiguration::impl_ts_fillCache()
167 : {
168 0 : css::uno::Reference< css::embed::XStorage > xDocumentRoot;
169 : {
170 0 : SolarMutexGuard g;
171 0 : xDocumentRoot = m_xDocumentRoot;
172 : }
173 :
174 : // Sometimes we must live without a document root.
175 : // E.g. if the document is readonly ...
176 0 : if (!xDocumentRoot.is())
177 0 : return;
178 :
179 : // get current office locale ... but dont cache it.
180 : // Otherwise we must be listener on the configuration layer
181 : // which seems to superflous for this small implementation .-)
182 0 : LanguageTag aLanguageTag( impl_ts_getLocale());
183 :
184 : // May be the current document does not contain any
185 : // accelerator config? Handle it gracefully :-)
186 : try
187 : {
188 : // Note: The used preset class is threadsafe by itself ... and live if we live!
189 : // We do not need any mutex here.
190 :
191 : // open the folder, where the configuration exists
192 : m_aPresetHandler.connectToResource(
193 : PresetHandler::E_DOCUMENT,
194 : PresetHandler::RESOURCETYPE_ACCELERATOR(),
195 : OUString(),
196 : xDocumentRoot,
197 0 : aLanguageTag);
198 :
199 0 : DocumentAcceleratorConfiguration::reload();
200 0 : m_aPresetHandler.addStorageListener(this);
201 : }
202 0 : catch(const css::uno::Exception&)
203 0 : {}
204 : }
205 :
206 0 : void DocumentAcceleratorConfiguration::impl_ts_clearCache()
207 : {
208 0 : m_aPresetHandler.forgetCachedStorages();
209 0 : }
210 :
211 : } // namespace framework
212 :
213 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
214 0 : com_sun_star_comp_framework_DocumentAcceleratorConfiguration_get_implementation(
215 : css::uno::XComponentContext *context,
216 : css::uno::Sequence<css::uno::Any> const &arguments)
217 : {
218 0 : return cppu::acquire(new DocumentAcceleratorConfiguration(context, arguments));
219 : }
220 :
221 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|