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_framework.hxx"
26 #include <accelerators/documentacceleratorconfiguration.hxx>
27
28 #ifndef __FRAMEWORK_XML_ACCELERATORCONFIGURATIONREADER_HXX_
29 #include <xml/acceleratorconfigurationreader.hxx>
30 #endif
31
32 #ifndef __FRAMEWORK_XML_ACCELERATORCONFIGURATIONWRITER_HXX_
33 #include <xml/acceleratorconfigurationwriter.hxx>
34 #endif
35
36 #ifndef __FRAMEWORK_XML_SAXNAMESPACEFILTER_HXX_
37 #include <xml/saxnamespacefilter.hxx>
38 #endif
39
40 //_______________________________________________
41 // own includes
42 #include <threadhelp/readguard.hxx>
43 #include <threadhelp/writeguard.hxx>
44
45 #ifndef __FRAMEWORK_ACCELERATORCONST_H_
46 #include <acceleratorconst.h>
47 #endif
48 #include <services.h>
49
50 //_______________________________________________
51 // interface includes
52
53 #ifndef _COM_SUN_STAR_IO_XACTIVEDATASOURCE_HPP_
54 #include <com/sun/star/io/XActiveDataSource.hpp>
55 #endif
56
57 #ifndef _COM_SUN_STAR_IO_XSEEKABLE_HPP_
58 #include <com/sun/star/io/XSeekable.hpp>
59 #endif
60
61 #ifndef _COM_SUN_STAR_IO_XTRUNCATE_HPP_
62 #include <com/sun/star/io/XTruncate.hpp>
63 #endif
64
65 #ifndef _COM_SUN_STAR_EMBED_ELEMENTMODES_HPP_
66 #include <com/sun/star/embed/ElementModes.hpp>
67 #endif
68
69 #ifndef _COM_SUN_STAR_XML_SAX_INPUTSOURCE_HPP_
70 #include <com/sun/star/xml/sax/InputSource.hpp>
71 #endif
72
73 #ifndef _COM_SUN_STAR_XML_SAX_XPARSER_HPP_
74 #include <com/sun/star/xml/sax/XParser.hpp>
75 #endif
76
77 //_______________________________________________
78 // other includes
79
80 #ifndef _COMPHELPER_SEQUENCEASHASHMAP_HXX
81 #include <comphelper/sequenceashashmap.hxx>
82 #endif
83
84 //_______________________________________________
85 // const
86
87 namespace framework
88 {
89
90 //-----------------------------------------------
91 // XInterface, XTypeProvider, XServiceInfo
DEFINE_XINTERFACE_2(DocumentAcceleratorConfiguration,XMLBasedAcceleratorConfiguration,DIRECT_INTERFACE (css::lang::XServiceInfo),DIRECT_INTERFACE (css::lang::XInitialization))92 DEFINE_XINTERFACE_2(DocumentAcceleratorConfiguration ,
93 XMLBasedAcceleratorConfiguration ,
94 DIRECT_INTERFACE(css::lang::XServiceInfo) ,
95 DIRECT_INTERFACE(css::lang::XInitialization))
96 // DIRECT_INTERFACE(css::ui::XUIConfigurationStorage))
97
98 DEFINE_XTYPEPROVIDER_2_WITH_BASECLASS(DocumentAcceleratorConfiguration ,
99 XMLBasedAcceleratorConfiguration ,
100 css::lang::XServiceInfo ,
101 css::lang::XInitialization)
102 // css::ui::XUIConfigurationStorage)
103
104 DEFINE_XSERVICEINFO_MULTISERVICE(DocumentAcceleratorConfiguration ,
105 ::cppu::OWeakObject ,
106 SERVICENAME_DOCUMENTACCELERATORCONFIGURATION ,
107 IMPLEMENTATIONNAME_DOCUMENTACCELERATORCONFIGURATION)
108
109 DEFINE_INIT_SERVICE(DocumentAcceleratorConfiguration,
110 {
111 /*Attention
112 I think we don't need any mutex or lock here ... because we are called by our own static method impl_createInstance()
113 to create a new instance of this class by our own supported service factory.
114 see macro DEFINE_XSERVICEINFO_MULTISERVICE and "impl_initService()" for further informations!
115 */
116 }
117 )
118
119 //-----------------------------------------------
120 DocumentAcceleratorConfiguration::DocumentAcceleratorConfiguration(const css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR)
121 : XMLBasedAcceleratorConfiguration(xSMGR)
122 {
123 }
124
125 //-----------------------------------------------
~DocumentAcceleratorConfiguration()126 DocumentAcceleratorConfiguration::~DocumentAcceleratorConfiguration()
127 {
128 m_aPresetHandler.removeStorageListener(this);
129 }
130
131 //-----------------------------------------------
initialize(const css::uno::Sequence<css::uno::Any> & lArguments)132 void SAL_CALL DocumentAcceleratorConfiguration::initialize(const css::uno::Sequence< css::uno::Any >& lArguments)
133 {
134 // SAFE -> ----------------------------------
135 WriteGuard aWriteLock(m_aLock);
136
137 ::comphelper::SequenceAsHashMap lArgs(lArguments);
138 m_xDocumentRoot = lArgs.getUnpackedValueOrDefault(
139 ::rtl::OUString::createFromAscii("DocumentRoot"),
140 css::uno::Reference< css::embed::XStorage >());
141
142 aWriteLock.unlock();
143 // <- SAFE ----------------------------------
144
145 impl_ts_fillCache();
146 }
147
148 //-----------------------------------------------
setStorage(const css::uno::Reference<css::embed::XStorage> & xStorage)149 void SAL_CALL DocumentAcceleratorConfiguration::setStorage(const css::uno::Reference< css::embed::XStorage >& xStorage)
150 {
151 // Attention! xStorage must be accepted too, if it's NULL !
152
153 // SAFE -> ----------------------------------
154 WriteGuard aWriteLock(m_aLock);
155 sal_Bool bForgetOldStorages = m_xDocumentRoot.is();
156 m_xDocumentRoot = xStorage;
157 aWriteLock.unlock();
158 // <- SAFE ----------------------------------
159
160 if (bForgetOldStorages)
161 impl_ts_clearCache();
162
163 if (xStorage.is())
164 impl_ts_fillCache();
165 }
166
167 //-----------------------------------------------
hasStorage()168 sal_Bool SAL_CALL DocumentAcceleratorConfiguration::hasStorage()
169 {
170 // SAFE -> ----------------------------------
171 ReadGuard aReadLock(m_aLock);
172 return m_xDocumentRoot.is();
173 // <- SAFE ----------------------------------
174 }
175
176 //-----------------------------------------------
impl_ts_fillCache()177 void DocumentAcceleratorConfiguration::impl_ts_fillCache()
178 {
179 // SAFE -> ----------------------------------
180 ReadGuard aReadLock(m_aLock);
181 css::uno::Reference< css::embed::XStorage > xDocumentRoot = m_xDocumentRoot;
182 aReadLock.unlock();
183 // <- SAFE ----------------------------------
184
185 // Sometimes we must live without a document root.
186 // E.g. if the document is readonly ...
187 if (!xDocumentRoot.is())
188 return;
189
190 // get current office locale ... but dont cache it.
191 // Otherwise we must be listener on the configuration layer
192 // which seems to superflous for this small implementation .-)
193 ::comphelper::Locale aLocale = impl_ts_getLocale();
194
195 // May be the current document does not contain any
196 // accelerator config? Handle it gracefully :-)
197 try
198 {
199 // Note: The used preset class is threadsafe by itself ... and live if we live!
200 // We do not need any mutex here.
201
202 // open the folder, where the configuration exists
203 m_aPresetHandler.connectToResource(
204 PresetHandler::E_DOCUMENT,
205 PresetHandler::RESOURCETYPE_ACCELERATOR(),
206 ::rtl::OUString(),
207 xDocumentRoot,
208 aLocale);
209
210 DocumentAcceleratorConfiguration::reload();
211 m_aPresetHandler.addStorageListener(this);
212 }
213 /*
214
215 Sometimes the configuration seems to be corrupted...
216 So it would be nice if we don't crash the office then .-)
217 #121559#
218
219 catch(const css::uno::RuntimeException& exRun)
220 { throw exRun; }
221 */
222 catch(const css::uno::Exception&)
223 {}
224 }
225
226 //-----------------------------------------------
impl_ts_clearCache()227 void DocumentAcceleratorConfiguration::impl_ts_clearCache()
228 {
229 m_aPresetHandler.forgetCachedStorages();
230 }
231
232 } // namespace framework
233