1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_framework.hxx"
30*cdf0e10cSrcweir #include <accelerators/documentacceleratorconfiguration.hxx>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #ifndef __FRAMEWORK_XML_ACCELERATORCONFIGURATIONREADER_HXX_
33*cdf0e10cSrcweir #include <xml/acceleratorconfigurationreader.hxx>
34*cdf0e10cSrcweir #endif
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #ifndef __FRAMEWORK_XML_ACCELERATORCONFIGURATIONWRITER_HXX_
37*cdf0e10cSrcweir #include <xml/acceleratorconfigurationwriter.hxx>
38*cdf0e10cSrcweir #endif
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #ifndef __FRAMEWORK_XML_SAXNAMESPACEFILTER_HXX_
41*cdf0e10cSrcweir #include <xml/saxnamespacefilter.hxx>
42*cdf0e10cSrcweir #endif
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir //_______________________________________________
45*cdf0e10cSrcweir // own includes
46*cdf0e10cSrcweir #include <threadhelp/readguard.hxx>
47*cdf0e10cSrcweir #include <threadhelp/writeguard.hxx>
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #ifndef __FRAMEWORK_ACCELERATORCONST_H_
50*cdf0e10cSrcweir #include <acceleratorconst.h>
51*cdf0e10cSrcweir #endif
52*cdf0e10cSrcweir #include <services.h>
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir //_______________________________________________
55*cdf0e10cSrcweir // interface includes
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_IO_XACTIVEDATASOURCE_HPP_
58*cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
59*cdf0e10cSrcweir #endif
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_IO_XSEEKABLE_HPP_
62*cdf0e10cSrcweir #include <com/sun/star/io/XSeekable.hpp>
63*cdf0e10cSrcweir #endif
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_IO_XTRUNCATE_HPP_
66*cdf0e10cSrcweir #include <com/sun/star/io/XTruncate.hpp>
67*cdf0e10cSrcweir #endif
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_EMBED_ELEMENTMODES_HPP_
70*cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp>
71*cdf0e10cSrcweir #endif
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_XML_SAX_INPUTSOURCE_HPP_
74*cdf0e10cSrcweir #include <com/sun/star/xml/sax/InputSource.hpp>
75*cdf0e10cSrcweir #endif
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_XML_SAX_XPARSER_HPP_
78*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XParser.hpp>
79*cdf0e10cSrcweir #endif
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir //_______________________________________________
82*cdf0e10cSrcweir // other includes
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir #ifndef _COMPHELPER_SEQUENCEASHASHMAP_HXX
85*cdf0e10cSrcweir #include <comphelper/sequenceashashmap.hxx>
86*cdf0e10cSrcweir #endif
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir //_______________________________________________
89*cdf0e10cSrcweir // const
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir namespace framework
92*cdf0e10cSrcweir {
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir //-----------------------------------------------
95*cdf0e10cSrcweir // XInterface, XTypeProvider, XServiceInfo
96*cdf0e10cSrcweir DEFINE_XINTERFACE_2(DocumentAcceleratorConfiguration                   ,
97*cdf0e10cSrcweir                     XMLBasedAcceleratorConfiguration                           ,
98*cdf0e10cSrcweir                     DIRECT_INTERFACE(css::lang::XServiceInfo)          ,
99*cdf0e10cSrcweir                     DIRECT_INTERFACE(css::lang::XInitialization))
100*cdf0e10cSrcweir //                    DIRECT_INTERFACE(css::ui::XUIConfigurationStorage))
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir DEFINE_XTYPEPROVIDER_2_WITH_BASECLASS(DocumentAcceleratorConfiguration ,
103*cdf0e10cSrcweir                                       XMLBasedAcceleratorConfiguration         ,
104*cdf0e10cSrcweir                                       css::lang::XServiceInfo          ,
105*cdf0e10cSrcweir                                       css::lang::XInitialization)
106*cdf0e10cSrcweir //                                      css::ui::XUIConfigurationStorage)
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir DEFINE_XSERVICEINFO_MULTISERVICE(DocumentAcceleratorConfiguration                   ,
109*cdf0e10cSrcweir                                  ::cppu::OWeakObject                                ,
110*cdf0e10cSrcweir                                  SERVICENAME_DOCUMENTACCELERATORCONFIGURATION       ,
111*cdf0e10cSrcweir                                  IMPLEMENTATIONNAME_DOCUMENTACCELERATORCONFIGURATION)
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir DEFINE_INIT_SERVICE(DocumentAcceleratorConfiguration,
114*cdf0e10cSrcweir                     {
115*cdf0e10cSrcweir                         /*Attention
116*cdf0e10cSrcweir                         I think we don't need any mutex or lock here ... because we are called by our own static method impl_createInstance()
117*cdf0e10cSrcweir                         to create a new instance of this class by our own supported service factory.
118*cdf0e10cSrcweir                         see macro DEFINE_XSERVICEINFO_MULTISERVICE and "impl_initService()" for further informations!
119*cdf0e10cSrcweir                         */
120*cdf0e10cSrcweir                     }
121*cdf0e10cSrcweir                    )
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir //-----------------------------------------------
124*cdf0e10cSrcweir DocumentAcceleratorConfiguration::DocumentAcceleratorConfiguration(const css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR)
125*cdf0e10cSrcweir     : XMLBasedAcceleratorConfiguration(xSMGR)
126*cdf0e10cSrcweir {
127*cdf0e10cSrcweir }
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir //-----------------------------------------------
130*cdf0e10cSrcweir DocumentAcceleratorConfiguration::~DocumentAcceleratorConfiguration()
131*cdf0e10cSrcweir {
132*cdf0e10cSrcweir     m_aPresetHandler.removeStorageListener(this);
133*cdf0e10cSrcweir }
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir //-----------------------------------------------
136*cdf0e10cSrcweir void SAL_CALL DocumentAcceleratorConfiguration::initialize(const css::uno::Sequence< css::uno::Any >& lArguments)
137*cdf0e10cSrcweir     throw(css::uno::Exception       ,
138*cdf0e10cSrcweir           css::uno::RuntimeException)
139*cdf0e10cSrcweir {
140*cdf0e10cSrcweir     // SAFE -> ----------------------------------
141*cdf0e10cSrcweir     WriteGuard aWriteLock(m_aLock);
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir     ::comphelper::SequenceAsHashMap lArgs(lArguments);
144*cdf0e10cSrcweir     m_xDocumentRoot = lArgs.getUnpackedValueOrDefault(
145*cdf0e10cSrcweir                         ::rtl::OUString::createFromAscii("DocumentRoot"),
146*cdf0e10cSrcweir                         css::uno::Reference< css::embed::XStorage >());
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir     aWriteLock.unlock();
149*cdf0e10cSrcweir     // <- SAFE ----------------------------------
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir     impl_ts_fillCache();
152*cdf0e10cSrcweir }
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir //-----------------------------------------------
155*cdf0e10cSrcweir void SAL_CALL DocumentAcceleratorConfiguration::setStorage(const css::uno::Reference< css::embed::XStorage >& xStorage)
156*cdf0e10cSrcweir 	throw(css::uno::RuntimeException)
157*cdf0e10cSrcweir {
158*cdf0e10cSrcweir 	// Attention! xStorage must be accepted too, if it's NULL !
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 	// SAFE -> ----------------------------------
161*cdf0e10cSrcweir 	WriteGuard aWriteLock(m_aLock);
162*cdf0e10cSrcweir 	sal_Bool bForgetOldStorages = m_xDocumentRoot.is();
163*cdf0e10cSrcweir 	m_xDocumentRoot = xStorage;
164*cdf0e10cSrcweir 	aWriteLock.unlock();
165*cdf0e10cSrcweir 	// <- SAFE ----------------------------------
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 	if (bForgetOldStorages)
168*cdf0e10cSrcweir 		impl_ts_clearCache();
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 	if (xStorage.is())
171*cdf0e10cSrcweir 		impl_ts_fillCache();
172*cdf0e10cSrcweir }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir //-----------------------------------------------
175*cdf0e10cSrcweir sal_Bool SAL_CALL DocumentAcceleratorConfiguration::hasStorage()
176*cdf0e10cSrcweir 	throw(css::uno::RuntimeException)
177*cdf0e10cSrcweir {
178*cdf0e10cSrcweir 	// SAFE -> ----------------------------------
179*cdf0e10cSrcweir 	ReadGuard aReadLock(m_aLock);
180*cdf0e10cSrcweir 	return m_xDocumentRoot.is();
181*cdf0e10cSrcweir 	// <- SAFE ----------------------------------
182*cdf0e10cSrcweir }
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir //-----------------------------------------------
185*cdf0e10cSrcweir void DocumentAcceleratorConfiguration::impl_ts_fillCache()
186*cdf0e10cSrcweir {
187*cdf0e10cSrcweir 	// SAFE -> ----------------------------------
188*cdf0e10cSrcweir 	ReadGuard aReadLock(m_aLock);
189*cdf0e10cSrcweir 	css::uno::Reference< css::embed::XStorage > xDocumentRoot = m_xDocumentRoot;
190*cdf0e10cSrcweir 	aReadLock.unlock();
191*cdf0e10cSrcweir 	// <- SAFE ----------------------------------
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 	// Sometimes we must live without a document root.
194*cdf0e10cSrcweir 	// E.g. if the document is readonly ...
195*cdf0e10cSrcweir 	if (!xDocumentRoot.is())
196*cdf0e10cSrcweir 		return;
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir 	// get current office locale ... but dont cache it.
199*cdf0e10cSrcweir 	// Otherwise we must be listener on the configuration layer
200*cdf0e10cSrcweir 	// which seems to superflous for this small implementation .-)
201*cdf0e10cSrcweir 	::comphelper::Locale aLocale = impl_ts_getLocale();
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir 	// May be the current document does not contain any
204*cdf0e10cSrcweir 	// accelerator config? Handle it gracefully :-)
205*cdf0e10cSrcweir 	try
206*cdf0e10cSrcweir 	{
207*cdf0e10cSrcweir 		// Note: The used preset class is threadsafe by itself ... and live if we live!
208*cdf0e10cSrcweir 		// We do not need any mutex here.
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 		// open the folder, where the configuration exists
211*cdf0e10cSrcweir 		m_aPresetHandler.connectToResource(
212*cdf0e10cSrcweir 			PresetHandler::E_DOCUMENT,
213*cdf0e10cSrcweir 			PresetHandler::RESOURCETYPE_ACCELERATOR(),
214*cdf0e10cSrcweir 			::rtl::OUString(),
215*cdf0e10cSrcweir 			xDocumentRoot,
216*cdf0e10cSrcweir 			aLocale);
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 		DocumentAcceleratorConfiguration::reload();
219*cdf0e10cSrcweir 		m_aPresetHandler.addStorageListener(this);
220*cdf0e10cSrcweir 	}
221*cdf0e10cSrcweir 	/*
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir 	Sometimes the configuration seams to be corrupted ..
224*cdf0e10cSrcweir 	So it would be nice if we dont crash the office then .-)
225*cdf0e10cSrcweir 	#121559#
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir 	catch(const css::uno::RuntimeException& exRun)
228*cdf0e10cSrcweir 	{ throw exRun; }
229*cdf0e10cSrcweir 	*/
230*cdf0e10cSrcweir 	catch(const css::uno::Exception&)
231*cdf0e10cSrcweir 	{}
232*cdf0e10cSrcweir }
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir //-----------------------------------------------
235*cdf0e10cSrcweir void DocumentAcceleratorConfiguration::impl_ts_clearCache()
236*cdf0e10cSrcweir {
237*cdf0e10cSrcweir     m_aPresetHandler.forgetCachedStorages();
238*cdf0e10cSrcweir }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir } // namespace framework
241