1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: accelcfg.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
35 #include "rtl/instance.hxx"
36 #include <com/sun/star/uno/Any.hxx>
37 #include <com/sun/star/uno/Sequence.hxx>
38 #include <com/sun/star/io/XActiveDataSource.hpp>
39 #include <com/sun/star/io/XInputStream.hpp>
40 #include <com/sun/star/io/XOutputStream.hpp>
41 #include <com/sun/star/xml/sax/XParser.hpp>
42 #include <unotools/configmgr.hxx>
43 #include <unotools/configitem.hxx>
44 #include <tools/debug.hxx>
46 #include <osl/mutex.hxx>
47 #include <tools/string.hxx>
48 #include <tools/urlobj.hxx>
49 #include <unotools/streamwrap.hxx>
50 #include <unotools/ucbstreamhelper.hxx>
51 #include <comphelper/processfactory.hxx>
53 #include <svtools/accelcfg.hxx>
54 #include "xmlaccelcfg.hxx"
55 #include <svtools/pathoptions.hxx>
56 #include "itemholder1.hxx"
61 using namespace com::sun::star::uno
;
62 using namespace com::sun::star::io
;
63 using namespace com::sun::star::xml::sax
;
66 static SvtAcceleratorConfig_Impl
* pOptions
= NULL
;
67 static sal_Int32 nRefCount
= 0;
69 class SvtAcceleratorConfig_Impl
73 SvtAcceleratorItemList aList
;
76 SvtAcceleratorConfig_Impl()
80 SvtAcceleratorConfig_Impl( Reference
< XInputStream
>& xInputStream
);
81 bool Commit( Reference
< XOutputStream
>& xOutputStream
);
84 // -----------------------------------------------------------------------
86 SvtAcceleratorConfig_Impl::SvtAcceleratorConfig_Impl( Reference
< XInputStream
>& rInputStream
)
89 Reference
< XParser
> xParser( ::comphelper::getProcessServiceFactory()->createInstance(
90 ::rtl::OUString::createFromAscii( "com.sun.star.xml.sax.Parser" )),
93 // connect stream to input stream to the parser
94 InputSource aInputSource
;
95 aInputSource
.aInputStream
= rInputStream
;
98 Reference
< XDocumentHandler
> xFilter( new OReadAccelatorDocumentHandler( aList
));
100 // connect parser and filter
101 xParser
->setDocumentHandler( xFilter
);
102 xParser
->parseStream( aInputSource
);
105 bool SvtAcceleratorConfig_Impl::Commit( Reference
< XOutputStream
>& rOutputStream
)
107 Reference
< XDocumentHandler
> xWriter
;
109 xWriter
= Reference
< XDocumentHandler
>( ::comphelper::getProcessServiceFactory()->createInstance(
110 ::rtl::OUString::createFromAscii( "com.sun.star.xml.sax.Writer" )), UNO_QUERY
) ;
112 Reference
< ::com::sun::star::io::XActiveDataSource
> xDataSource( xWriter
, UNO_QUERY
);
113 xDataSource
->setOutputStream( rOutputStream
);
116 OWriteAccelatorDocumentHandler
aWriteHandler( aList
, xWriter
);
117 aWriteHandler
.WriteAcceleratorDocument();
118 rOutputStream
->flush();
121 catch ( RuntimeException
& )
124 catch ( SAXException
& )
127 catch ( ::com::sun::star::io::IOException
& )
136 class LocalSingleton
: public rtl::Static
< osl::Mutex
, LocalSingleton
>
141 SvtAcceleratorConfiguration::SvtAcceleratorConfiguration()
143 // Global access, must be guarded (multithreading)
144 ::osl::MutexGuard
aGuard( LocalSingleton::get() );
147 SvStream
* pStream
= GetDefaultStream( STREAM_STD_READ
);
148 ::utl::OInputStreamWrapper
aHelper( *pStream
);
149 com::sun::star::uno::Reference
< ::com::sun::star::io::XInputStream
> xOut( &aHelper
);
153 pOptions
= new SvtAcceleratorConfig_Impl( xOut
);
155 catch ( RuntimeException
& )
157 pOptions
= new SvtAcceleratorConfig_Impl();
159 catch( SAXException
& )
161 pOptions
= new SvtAcceleratorConfig_Impl();
163 catch( ::com::sun::star::io::IOException
& )
165 pOptions
= new SvtAcceleratorConfig_Impl();
169 ItemHolder1::holdConfigItem(E_ACCELCFG
);
178 SvtAcceleratorConfiguration
* SvtAcceleratorConfiguration::CreateFromStream( SvStream
& rStream
)
180 SvtAcceleratorConfiguration
* pRet
= new SvtAcceleratorConfiguration
;
181 ::utl::OInputStreamWrapper
aHelper( rStream
);
182 com::sun::star::uno::Reference
< ::com::sun::star::io::XInputStream
> xOut( &aHelper
);
185 pRet
->pImp
= new SvtAcceleratorConfig_Impl( xOut
);
187 catch ( RuntimeException
& )
191 catch( SAXException
& )
195 catch( ::com::sun::star::io::IOException
& )
203 // -----------------------------------------------------------------------
205 SvtAcceleratorConfiguration::~SvtAcceleratorConfiguration()
207 if ( pImp
== pOptions
)
209 // Global access, must be guarded (multithreading)
210 ::osl::MutexGuard
aGuard( LocalSingleton::get() );
213 if ( pImp
->bModified
)
215 String aUserConfig
= SvtPathOptions().GetUserConfigPath();
216 INetURLObject
aObj( aUserConfig
);
217 aObj
.insertName( String::CreateFromAscii("GlobalKeyBindings.xml") );
218 SvStream
* pStream
= ::utl::UcbStreamHelper::CreateStream( aObj
.GetMainURL( INetURLObject::NO_DECODE
), STREAM_STD_READWRITE
|STREAM_TRUNC
);
219 ::utl::OOutputStreamWrapper
aHelper( *pStream
);
220 com::sun::star::uno::Reference
< ::com::sun::star::io::XOutputStream
> xOut( &aHelper
);
221 pImp
->Commit( xOut
);
234 ::rtl::OUString
SvtAcceleratorConfiguration::GetCommand( const ::com::sun::star::awt::KeyEvent
& rKeyEvent
)
236 sal_Int16 nCode
=rKeyEvent
.KeyCode
, nModifier
=rKeyEvent
.Modifiers
;
238 nCode
= rKeyEvent
.KeyFunc
;
240 std::list
< SvtAcceleratorConfigItem
>::const_iterator p
;
241 for ( p
= pImp
->aList
.begin(); p
!= pImp
->aList
.end(); p
++ )
242 if ( p
->nCode
== nCode
&& p
->nModifier
== nModifier
)
245 return ::rtl::OUString();
248 const SvtAcceleratorItemList
& SvtAcceleratorConfiguration::GetItems()
253 void SvtAcceleratorConfiguration::SetCommand( const SvtAcceleratorConfigItem
& rItem
)
255 std::list
< SvtAcceleratorConfigItem
>::iterator p
;
256 for ( p
= pImp
->aList
.begin(); p
!= pImp
->aList
.end(); p
++ )
257 if ( p
->nCode
== rItem
.nCode
&& p
->nModifier
== rItem
.nModifier
)
259 p
->aCommand
= rItem
.aCommand
;
263 pImp
->aList
.push_back( rItem
);
267 void SvtAcceleratorConfiguration::SetItems( const SvtAcceleratorItemList
& rItems
, bool bClear
)
271 pImp
->aList
= rItems
;
275 std::list
< SvtAcceleratorConfigItem
>::const_iterator p
;
276 for ( p
= rItems
.begin(); p
!= rItems
.end(); p
++ )
281 String
SvtAcceleratorConfiguration::GetStreamName()
283 return String::CreateFromAscii("KeyBindings.xml");
286 SvStream
* SvtAcceleratorConfiguration::GetDefaultStream( StreamMode nMode
)
288 String aUserConfig
= SvtPathOptions().GetUserConfigPath();
289 INetURLObject
aObj( aUserConfig
);
290 aObj
.insertName( GetStreamName() );
291 return ::utl::UcbStreamHelper::CreateStream( aObj
.GetMainURL( INetURLObject::NO_DECODE
), nMode
);