1*b5088357SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*b5088357SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*b5088357SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*b5088357SAndrew Rist * distributed with this work for additional information
6*b5088357SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*b5088357SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*b5088357SAndrew Rist * "License"); you may not use this file except in compliance
9*b5088357SAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*b5088357SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*b5088357SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*b5088357SAndrew Rist * software distributed under the License is distributed on an
15*b5088357SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b5088357SAndrew Rist * KIND, either express or implied. See the License for the
17*b5088357SAndrew Rist * specific language governing permissions and limitations
18*b5088357SAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*b5088357SAndrew Rist *************************************************************/
21*b5088357SAndrew Rist
22*b5088357SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_unotools.hxx"
26cdf0e10cSrcweir #ifndef GCC
27cdf0e10cSrcweir #endif
28cdf0e10cSrcweir #include "rtl/instance.hxx"
29cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
30cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
31cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
32cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
33cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
34cdf0e10cSrcweir #include <com/sun/star/xml/sax/XParser.hpp>
35cdf0e10cSrcweir #include <unotools/configmgr.hxx>
36cdf0e10cSrcweir #include <unotools/configitem.hxx>
37cdf0e10cSrcweir #include <tools/debug.hxx>
38cdf0e10cSrcweir
39cdf0e10cSrcweir #include <osl/mutex.hxx>
40cdf0e10cSrcweir #include <tools/string.hxx>
41cdf0e10cSrcweir #include <tools/urlobj.hxx>
42cdf0e10cSrcweir #include <unotools/streamwrap.hxx>
43cdf0e10cSrcweir #include <unotools/ucbstreamhelper.hxx>
44cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
45cdf0e10cSrcweir
46cdf0e10cSrcweir #include <unotools/accelcfg.hxx>
47cdf0e10cSrcweir #include <unotools/xmlaccelcfg.hxx>
48cdf0e10cSrcweir #include <unotools/pathoptions.hxx>
49cdf0e10cSrcweir #include "itemholder1.hxx"
50cdf0e10cSrcweir
51cdf0e10cSrcweir
52cdf0e10cSrcweir using namespace utl;
53cdf0e10cSrcweir using namespace rtl;
54cdf0e10cSrcweir using namespace com::sun::star::uno;
55cdf0e10cSrcweir using namespace com::sun::star::io;
56cdf0e10cSrcweir using namespace com::sun::star::xml::sax;
57cdf0e10cSrcweir
58cdf0e10cSrcweir
59cdf0e10cSrcweir static SvtAcceleratorConfig_Impl* pOptions = NULL;
60cdf0e10cSrcweir static sal_Int32 nRefCount = 0;
61cdf0e10cSrcweir
62cdf0e10cSrcweir class SvtAcceleratorConfig_Impl
63cdf0e10cSrcweir {
64cdf0e10cSrcweir public:
65cdf0e10cSrcweir
66cdf0e10cSrcweir SvtAcceleratorItemList aList;
67cdf0e10cSrcweir bool bModified;
68cdf0e10cSrcweir
SvtAcceleratorConfig_Impl()69cdf0e10cSrcweir SvtAcceleratorConfig_Impl()
70cdf0e10cSrcweir : bModified( sal_False )
71cdf0e10cSrcweir {}
72cdf0e10cSrcweir
73cdf0e10cSrcweir SvtAcceleratorConfig_Impl( Reference< XInputStream >& xInputStream );
74cdf0e10cSrcweir bool Commit( Reference< XOutputStream >& xOutputStream );
75cdf0e10cSrcweir };
76cdf0e10cSrcweir
77cdf0e10cSrcweir // -----------------------------------------------------------------------
78cdf0e10cSrcweir
SvtAcceleratorConfig_Impl(Reference<XInputStream> & rInputStream)79cdf0e10cSrcweir SvtAcceleratorConfig_Impl::SvtAcceleratorConfig_Impl( Reference< XInputStream >& rInputStream )
80cdf0e10cSrcweir : bModified( false )
81cdf0e10cSrcweir {
82cdf0e10cSrcweir Reference< XParser > xParser( ::comphelper::getProcessServiceFactory()->createInstance(
83cdf0e10cSrcweir ::rtl::OUString::createFromAscii( "com.sun.star.xml.sax.Parser" )),
84cdf0e10cSrcweir UNO_QUERY);
85cdf0e10cSrcweir
86cdf0e10cSrcweir // connect stream to input stream to the parser
87cdf0e10cSrcweir InputSource aInputSource;
88cdf0e10cSrcweir aInputSource.aInputStream = rInputStream;
89cdf0e10cSrcweir
90cdf0e10cSrcweir // get filter
91cdf0e10cSrcweir Reference< XDocumentHandler > xFilter( new OReadAccelatorDocumentHandler( aList ));
92cdf0e10cSrcweir
93cdf0e10cSrcweir // connect parser and filter
94cdf0e10cSrcweir xParser->setDocumentHandler( xFilter );
95cdf0e10cSrcweir xParser->parseStream( aInputSource );
96cdf0e10cSrcweir }
97cdf0e10cSrcweir
Commit(Reference<XOutputStream> & rOutputStream)98cdf0e10cSrcweir bool SvtAcceleratorConfig_Impl::Commit( Reference< XOutputStream >& rOutputStream )
99cdf0e10cSrcweir {
100cdf0e10cSrcweir Reference< XDocumentHandler > xWriter;
101cdf0e10cSrcweir
102cdf0e10cSrcweir xWriter = Reference< XDocumentHandler >( ::comphelper::getProcessServiceFactory()->createInstance(
103cdf0e10cSrcweir ::rtl::OUString::createFromAscii( "com.sun.star.xml.sax.Writer" )), UNO_QUERY) ;
104cdf0e10cSrcweir
105cdf0e10cSrcweir Reference< ::com::sun::star::io::XActiveDataSource> xDataSource( xWriter , UNO_QUERY );
106cdf0e10cSrcweir xDataSource->setOutputStream( rOutputStream );
107cdf0e10cSrcweir try
108cdf0e10cSrcweir {
109cdf0e10cSrcweir OWriteAccelatorDocumentHandler aWriteHandler( aList, xWriter );
110cdf0e10cSrcweir aWriteHandler.WriteAcceleratorDocument();
111cdf0e10cSrcweir rOutputStream->flush();
112cdf0e10cSrcweir return true;
113cdf0e10cSrcweir }
114cdf0e10cSrcweir catch ( RuntimeException& )
115cdf0e10cSrcweir {
116cdf0e10cSrcweir }
117cdf0e10cSrcweir catch ( SAXException& )
118cdf0e10cSrcweir {
119cdf0e10cSrcweir }
120cdf0e10cSrcweir catch ( ::com::sun::star::io::IOException& )
121cdf0e10cSrcweir {
122cdf0e10cSrcweir }
123cdf0e10cSrcweir
124cdf0e10cSrcweir return false;
125cdf0e10cSrcweir }
126cdf0e10cSrcweir
127cdf0e10cSrcweir namespace
128cdf0e10cSrcweir {
129cdf0e10cSrcweir class LocalSingleton : public rtl::Static< osl::Mutex, LocalSingleton >
130cdf0e10cSrcweir {
131cdf0e10cSrcweir };
132cdf0e10cSrcweir }
133cdf0e10cSrcweir
SvtAcceleratorConfiguration()134cdf0e10cSrcweir SvtAcceleratorConfiguration::SvtAcceleratorConfiguration()
135cdf0e10cSrcweir {
136cdf0e10cSrcweir // Global access, must be guarded (multithreading)
137cdf0e10cSrcweir ::osl::MutexGuard aGuard( LocalSingleton::get() );
138cdf0e10cSrcweir if ( !pOptions )
139cdf0e10cSrcweir {
140cdf0e10cSrcweir SvStream* pStream = GetDefaultStream( STREAM_STD_READ );
141cdf0e10cSrcweir ::utl::OInputStreamWrapper aHelper( *pStream );
142cdf0e10cSrcweir com::sun::star::uno::Reference < ::com::sun::star::io::XInputStream > xOut( &aHelper );
143cdf0e10cSrcweir
144cdf0e10cSrcweir try
145cdf0e10cSrcweir {
146cdf0e10cSrcweir pOptions = new SvtAcceleratorConfig_Impl( xOut );
147cdf0e10cSrcweir }
148cdf0e10cSrcweir catch ( RuntimeException& )
149cdf0e10cSrcweir {
150cdf0e10cSrcweir pOptions = new SvtAcceleratorConfig_Impl();
151cdf0e10cSrcweir }
152cdf0e10cSrcweir catch( SAXException& )
153cdf0e10cSrcweir {
154cdf0e10cSrcweir pOptions = new SvtAcceleratorConfig_Impl();
155cdf0e10cSrcweir }
156cdf0e10cSrcweir catch( ::com::sun::star::io::IOException& )
157cdf0e10cSrcweir {
158cdf0e10cSrcweir pOptions = new SvtAcceleratorConfig_Impl();
159cdf0e10cSrcweir }
160cdf0e10cSrcweir
161cdf0e10cSrcweir if (pOptions)
162cdf0e10cSrcweir ItemHolder1::holdConfigItem(E_ACCELCFG);
163cdf0e10cSrcweir
164cdf0e10cSrcweir delete pStream;
165cdf0e10cSrcweir }
166cdf0e10cSrcweir
167cdf0e10cSrcweir ++nRefCount;
168cdf0e10cSrcweir pImp = pOptions;
169cdf0e10cSrcweir }
170cdf0e10cSrcweir
CreateFromStream(SvStream & rStream)171cdf0e10cSrcweir SvtAcceleratorConfiguration* SvtAcceleratorConfiguration::CreateFromStream( SvStream& rStream )
172cdf0e10cSrcweir {
173cdf0e10cSrcweir SvtAcceleratorConfiguration* pRet = new SvtAcceleratorConfiguration;
174cdf0e10cSrcweir ::utl::OInputStreamWrapper aHelper( rStream );
175cdf0e10cSrcweir com::sun::star::uno::Reference < ::com::sun::star::io::XInputStream > xOut( &aHelper );
176cdf0e10cSrcweir try
177cdf0e10cSrcweir {
178cdf0e10cSrcweir pRet->pImp = new SvtAcceleratorConfig_Impl( xOut );
179cdf0e10cSrcweir }
180cdf0e10cSrcweir catch ( RuntimeException& )
181cdf0e10cSrcweir {
182cdf0e10cSrcweir DELETEZ( pRet );
183cdf0e10cSrcweir }
184cdf0e10cSrcweir catch( SAXException& )
185cdf0e10cSrcweir {
186cdf0e10cSrcweir DELETEZ( pRet );
187cdf0e10cSrcweir }
188cdf0e10cSrcweir catch( ::com::sun::star::io::IOException& )
189cdf0e10cSrcweir {
190cdf0e10cSrcweir DELETEZ( pRet );
191cdf0e10cSrcweir }
192cdf0e10cSrcweir
193cdf0e10cSrcweir return pRet;
194cdf0e10cSrcweir }
195cdf0e10cSrcweir
196cdf0e10cSrcweir // -----------------------------------------------------------------------
197cdf0e10cSrcweir
~SvtAcceleratorConfiguration()198cdf0e10cSrcweir SvtAcceleratorConfiguration::~SvtAcceleratorConfiguration()
199cdf0e10cSrcweir {
200cdf0e10cSrcweir if ( pImp == pOptions )
201cdf0e10cSrcweir {
202cdf0e10cSrcweir // Global access, must be guarded (multithreading)
203cdf0e10cSrcweir ::osl::MutexGuard aGuard( LocalSingleton::get() );
204cdf0e10cSrcweir if ( !--nRefCount )
205cdf0e10cSrcweir {
206cdf0e10cSrcweir if ( pImp->bModified )
207cdf0e10cSrcweir {
208cdf0e10cSrcweir String aUserConfig = SvtPathOptions().GetUserConfigPath();
209cdf0e10cSrcweir INetURLObject aObj( aUserConfig );
210cdf0e10cSrcweir aObj.insertName( String::CreateFromAscii("GlobalKeyBindings.xml") );
211cdf0e10cSrcweir SvStream* pStream = ::utl::UcbStreamHelper::CreateStream( aObj.GetMainURL( INetURLObject::NO_DECODE ), STREAM_STD_READWRITE|STREAM_TRUNC );
212cdf0e10cSrcweir ::utl::OOutputStreamWrapper aHelper( *pStream );
213cdf0e10cSrcweir com::sun::star::uno::Reference < ::com::sun::star::io::XOutputStream > xOut( &aHelper );
214cdf0e10cSrcweir pImp->Commit( xOut );
215cdf0e10cSrcweir delete pStream;
216cdf0e10cSrcweir }
217cdf0e10cSrcweir
218cdf0e10cSrcweir DELETEZ( pOptions );
219cdf0e10cSrcweir }
220cdf0e10cSrcweir }
221cdf0e10cSrcweir else
222cdf0e10cSrcweir {
223cdf0e10cSrcweir delete pImp;
224cdf0e10cSrcweir }
225cdf0e10cSrcweir }
226cdf0e10cSrcweir
GetCommand(const::com::sun::star::awt::KeyEvent & rKeyEvent)227cdf0e10cSrcweir ::rtl::OUString SvtAcceleratorConfiguration::GetCommand( const ::com::sun::star::awt::KeyEvent& rKeyEvent )
228cdf0e10cSrcweir {
229cdf0e10cSrcweir sal_Int16 nCode=rKeyEvent.KeyCode, nModifier=rKeyEvent.Modifiers;
230cdf0e10cSrcweir if ( !nCode )
231cdf0e10cSrcweir nCode = rKeyEvent.KeyFunc;
232cdf0e10cSrcweir
233cdf0e10cSrcweir std::list< SvtAcceleratorConfigItem>::const_iterator p;
234cdf0e10cSrcweir for ( p = pImp->aList.begin(); p != pImp->aList.end(); p++ )
235cdf0e10cSrcweir if ( p->nCode == nCode && p->nModifier == nModifier )
236cdf0e10cSrcweir return p->aCommand;
237cdf0e10cSrcweir
238cdf0e10cSrcweir return ::rtl::OUString();
239cdf0e10cSrcweir }
240cdf0e10cSrcweir
GetItems()241cdf0e10cSrcweir const SvtAcceleratorItemList& SvtAcceleratorConfiguration::GetItems()
242cdf0e10cSrcweir {
243cdf0e10cSrcweir return pImp->aList;
244cdf0e10cSrcweir }
245cdf0e10cSrcweir
SetCommand(const SvtAcceleratorConfigItem & rItem)246cdf0e10cSrcweir void SvtAcceleratorConfiguration::SetCommand( const SvtAcceleratorConfigItem& rItem )
247cdf0e10cSrcweir {
248cdf0e10cSrcweir std::list< SvtAcceleratorConfigItem>::iterator p;
249cdf0e10cSrcweir for ( p = pImp->aList.begin(); p != pImp->aList.end(); p++ )
250cdf0e10cSrcweir if ( p->nCode == rItem.nCode && p->nModifier == rItem.nModifier )
251cdf0e10cSrcweir {
252cdf0e10cSrcweir p->aCommand = rItem.aCommand;
253cdf0e10cSrcweir return;
254cdf0e10cSrcweir }
255cdf0e10cSrcweir
256cdf0e10cSrcweir pImp->aList.push_back( rItem );
257cdf0e10cSrcweir
258cdf0e10cSrcweir }
259cdf0e10cSrcweir
SetItems(const SvtAcceleratorItemList & rItems,bool bClear)260cdf0e10cSrcweir void SvtAcceleratorConfiguration::SetItems( const SvtAcceleratorItemList& rItems, bool bClear )
261cdf0e10cSrcweir {
262cdf0e10cSrcweir if ( bClear )
263cdf0e10cSrcweir {
264cdf0e10cSrcweir pImp->aList = rItems;
265cdf0e10cSrcweir }
266cdf0e10cSrcweir else
267cdf0e10cSrcweir {
268cdf0e10cSrcweir std::list< SvtAcceleratorConfigItem>::const_iterator p;
269cdf0e10cSrcweir for ( p = rItems.begin(); p != rItems.end(); p++ )
270cdf0e10cSrcweir SetCommand( *p );
271cdf0e10cSrcweir }
272cdf0e10cSrcweir }
273cdf0e10cSrcweir
GetStreamName()274cdf0e10cSrcweir String SvtAcceleratorConfiguration::GetStreamName()
275cdf0e10cSrcweir {
276cdf0e10cSrcweir return String::CreateFromAscii("KeyBindings.xml");
277cdf0e10cSrcweir }
278cdf0e10cSrcweir
GetDefaultStream(StreamMode nMode)279cdf0e10cSrcweir SvStream* SvtAcceleratorConfiguration::GetDefaultStream( StreamMode nMode )
280cdf0e10cSrcweir {
281cdf0e10cSrcweir String aUserConfig = SvtPathOptions().GetUserConfigPath();
282cdf0e10cSrcweir INetURLObject aObj( aUserConfig );
283cdf0e10cSrcweir aObj.insertName( GetStreamName() );
284cdf0e10cSrcweir return ::utl::UcbStreamHelper::CreateStream( aObj.GetMainURL( INetURLObject::NO_DECODE ), nMode );
285cdf0e10cSrcweir }
286