1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <accelerators/acceleratorconfiguration.hxx>
21 #include <accelerators/istoragelistener.hxx>
22 #include <accelerators/presethandler.hxx>
24 #include <xml/acceleratorconfigurationreader.hxx>
25 #include <xml/acceleratorconfigurationwriter.hxx>
26 #include <xml/saxnamespacefilter.hxx>
28 #include <acceleratorconst.h>
30 #include <com/sun/star/lang/XServiceInfo.hpp>
31 #include <com/sun/star/ui/XUIConfigurationStorage.hpp>
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>
40 using namespace framework
;
42 #define RESOURCETYPE_ACCELERATOR "accelerator"
47 implements a read/write access to a document
48 based accelerator configuration.
51 typedef ::cppu::ImplInheritanceHelper1
<
52 XMLBasedAcceleratorConfiguration
,
53 css::lang::XServiceInfo
> DocumentAcceleratorConfiguration_BASE
;
55 class DocumentAcceleratorConfiguration
: public DocumentAcceleratorConfiguration_BASE
59 /** points to the root storage of the outside document,
60 where we can read/save our configuration data. */
61 css::uno::Reference
< css::embed::XStorage
> m_xDocumentRoot
;
65 /** initialize this instance and fill the internal cache.
68 reference to an uno service manager, which is used internally.
70 DocumentAcceleratorConfiguration(
71 const css::uno::Reference
< css::uno::XComponentContext
>& xContext
,
72 const css::uno::Sequence
< css::uno::Any
>& lArguments
);
74 virtual ~DocumentAcceleratorConfiguration();
76 virtual OUString SAL_CALL
getImplementationName()
77 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
79 return OUString("com.sun.star.comp.framework.DocumentAcceleratorConfiguration");
82 virtual sal_Bool SAL_CALL
supportsService(OUString
const & ServiceName
)
83 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
85 return cppu::supportsService(this, ServiceName
);
88 virtual css::uno::Sequence
<OUString
> SAL_CALL
getSupportedServiceNames()
89 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
91 css::uno::Sequence
< OUString
> aSeq(1);
92 aSeq
[0] = "com.sun.star.ui.DocumentAcceleratorConfiguration";
96 // XUIConfigurationStorage
97 virtual void SAL_CALL
setStorage(const css::uno::Reference
< css::embed::XStorage
>& xStorage
)
98 throw(css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
100 virtual sal_Bool SAL_CALL
hasStorage()
101 throw(css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
103 /** read all data into the cache. */
108 /** forget all currently cached data AND(!)
109 forget all currently used storages. */
113 DocumentAcceleratorConfiguration::DocumentAcceleratorConfiguration(
114 const css::uno::Reference
< css::uno::XComponentContext
>& xContext
,
115 const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
>& lArguments
)
116 : DocumentAcceleratorConfiguration_BASE(xContext
)
120 css::uno::Reference
<css::embed::XStorage
> xRoot
;
121 if (lArguments
.getLength() == 1 && (lArguments
[0] >>= xRoot
))
123 m_xDocumentRoot
= xRoot
;
127 ::comphelper::SequenceAsHashMap
lArgs(lArguments
);
128 m_xDocumentRoot
= lArgs
.getUnpackedValueOrDefault(
129 OUString("DocumentRoot"),
130 css::uno::Reference
< css::embed::XStorage
>());
135 DocumentAcceleratorConfiguration::~DocumentAcceleratorConfiguration()
137 m_aPresetHandler
.removeStorageListener(this);
140 void SAL_CALL
DocumentAcceleratorConfiguration::setStorage(const css::uno::Reference
< css::embed::XStorage
>& xStorage
)
141 throw(css::uno::RuntimeException
, std::exception
)
143 // Attention! xStorage must be accepted too, if it's NULL !
145 bool bForgetOldStorages
;
148 bForgetOldStorages
= m_xDocumentRoot
.is();
149 m_xDocumentRoot
= xStorage
;
152 if (bForgetOldStorages
)
159 sal_Bool SAL_CALL
DocumentAcceleratorConfiguration::hasStorage()
160 throw(css::uno::RuntimeException
, std::exception
)
163 return m_xDocumentRoot
.is();
166 void DocumentAcceleratorConfiguration::fillCache()
168 css::uno::Reference
< css::embed::XStorage
> xDocumentRoot
;
171 xDocumentRoot
= m_xDocumentRoot
;
174 // Sometimes we must live without a document root.
175 // E.g. if the document is readonly ...
176 if (!xDocumentRoot
.is())
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 LanguageTag
aLanguageTag( impl_ts_getLocale());
184 // May be the current document does not contain any
185 // accelerator config? Handle it gracefully :-)
188 // Note: The used preset class is threadsafe by itself ... and live if we live!
189 // We do not need any mutex here.
191 // open the folder, where the configuration exists
192 m_aPresetHandler
.connectToResource(
193 PresetHandler::E_DOCUMENT
,
194 RESOURCETYPE_ACCELERATOR
,
199 DocumentAcceleratorConfiguration::reload();
200 m_aPresetHandler
.addStorageListener(this);
202 catch(const css::uno::Exception
&)
206 void DocumentAcceleratorConfiguration::clearCache()
208 m_aPresetHandler
.forgetCachedStorages();
211 } // namespace framework
213 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
214 com_sun_star_comp_framework_DocumentAcceleratorConfiguration_get_implementation(
215 css::uno::XComponentContext
*context
,
216 css::uno::Sequence
<css::uno::Any
> const &arguments
)
218 DocumentAcceleratorConfiguration
*inst
= new DocumentAcceleratorConfiguration(context
, arguments
);
219 css::uno::XInterface
*acquired_inst
= cppu::acquire(inst
);
223 return acquired_inst
;
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */