Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "rtl/instance.hxx"
22 : #include <com/sun/star/uno/Any.hxx>
23 : #include <com/sun/star/uno/Sequence.hxx>
24 : #include <com/sun/star/io/XActiveDataSource.hpp>
25 : #include <com/sun/star/io/XInputStream.hpp>
26 : #include <com/sun/star/io/XOutputStream.hpp>
27 : #include <com/sun/star/xml/sax/Parser.hpp>
28 : #include <com/sun/star/xml/sax/Writer.hpp>
29 : #include <unotools/configmgr.hxx>
30 : #include <unotools/configitem.hxx>
31 :
32 : #include <osl/mutex.hxx>
33 : #include <tools/string.hxx>
34 : #include <tools/urlobj.hxx>
35 : #include <unotools/streamwrap.hxx>
36 : #include <unotools/ucbstreamhelper.hxx>
37 : #include <comphelper/processfactory.hxx>
38 :
39 : #include <unotools/accelcfg.hxx>
40 : #include <unotools/xmlaccelcfg.hxx>
41 : #include <unotools/pathoptions.hxx>
42 : #include "itemholder1.hxx"
43 :
44 :
45 : using namespace utl;
46 : using namespace com::sun::star::uno;
47 : using namespace com::sun::star::io;
48 : using namespace com::sun::star::xml::sax;
49 :
50 : using ::rtl::OUString;
51 :
52 :
53 : static SvtAcceleratorConfig_Impl* pOptions = NULL;
54 : static sal_Int32 nRefCount = 0;
55 :
56 0 : class SvtAcceleratorConfig_Impl
57 : {
58 : public:
59 :
60 : SvtAcceleratorItemList aList;
61 : bool bModified;
62 :
63 0 : SvtAcceleratorConfig_Impl()
64 0 : : bModified( sal_False )
65 0 : {}
66 :
67 : SvtAcceleratorConfig_Impl( Reference< XInputStream >& xInputStream );
68 : bool Commit( Reference< XOutputStream >& xOutputStream );
69 : };
70 :
71 : // -----------------------------------------------------------------------
72 :
73 0 : SvtAcceleratorConfig_Impl::SvtAcceleratorConfig_Impl( Reference< XInputStream >& rInputStream )
74 0 : : bModified( false )
75 : {
76 0 : Reference< XParser > xParser = Parser::create( ::comphelper::getProcessComponentContext() );
77 :
78 : // connect stream to input stream to the parser
79 0 : InputSource aInputSource;
80 0 : aInputSource.aInputStream = rInputStream;
81 :
82 : // get filter
83 0 : Reference< XDocumentHandler > xFilter( new OReadAccelatorDocumentHandler( aList ));
84 :
85 : // connect parser and filter
86 0 : xParser->setDocumentHandler( xFilter );
87 0 : xParser->parseStream( aInputSource );
88 0 : }
89 :
90 0 : bool SvtAcceleratorConfig_Impl::Commit( Reference< XOutputStream >& rOutputStream )
91 : {
92 0 : Reference< XWriter > xWriter = Writer::create( ::comphelper::getProcessComponentContext() );
93 :
94 0 : xWriter->setOutputStream( rOutputStream );
95 : try
96 : {
97 0 : OWriteAccelatorDocumentHandler aWriteHandler( aList, Reference<XDocumentHandler>(xWriter, UNO_QUERY_THROW) );
98 0 : aWriteHandler.WriteAcceleratorDocument();
99 0 : rOutputStream->flush();
100 0 : return true;
101 : }
102 0 : catch ( RuntimeException& )
103 : {
104 : }
105 0 : catch ( SAXException& )
106 : {
107 : }
108 0 : catch ( ::com::sun::star::io::IOException& )
109 : {
110 : }
111 :
112 0 : return false;
113 : }
114 :
115 : namespace
116 : {
117 : class LocalSingleton : public rtl::Static< osl::Mutex, LocalSingleton >
118 : {
119 : };
120 : }
121 :
122 0 : SvtAcceleratorConfiguration::SvtAcceleratorConfiguration()
123 : {
124 : // Global access, must be guarded (multithreading)
125 0 : ::osl::MutexGuard aGuard( LocalSingleton::get() );
126 0 : if ( !pOptions )
127 : {
128 0 : SvStream* pStream = GetDefaultStream( STREAM_STD_READ );
129 0 : ::utl::OInputStreamWrapper aHelper( *pStream );
130 0 : com::sun::star::uno::Reference < ::com::sun::star::io::XInputStream > xOut( &aHelper );
131 :
132 : try
133 : {
134 0 : pOptions = new SvtAcceleratorConfig_Impl( xOut );
135 : }
136 0 : catch ( RuntimeException& )
137 : {
138 0 : pOptions = new SvtAcceleratorConfig_Impl();
139 : }
140 0 : catch( SAXException& )
141 : {
142 0 : pOptions = new SvtAcceleratorConfig_Impl();
143 : }
144 0 : catch( ::com::sun::star::io::IOException& )
145 : {
146 0 : pOptions = new SvtAcceleratorConfig_Impl();
147 : }
148 :
149 0 : if (pOptions)
150 0 : ItemHolder1::holdConfigItem(E_ACCELCFG);
151 :
152 0 : delete pStream;
153 : }
154 :
155 0 : ++nRefCount;
156 0 : pImp = pOptions;
157 0 : }
158 :
159 : // -----------------------------------------------------------------------
160 :
161 0 : SvtAcceleratorConfiguration::~SvtAcceleratorConfiguration()
162 : {
163 0 : if ( pImp == pOptions )
164 : {
165 : // Global access, must be guarded (multithreading)
166 0 : ::osl::MutexGuard aGuard( LocalSingleton::get() );
167 0 : if ( !--nRefCount )
168 : {
169 0 : if ( pImp->bModified )
170 : {
171 0 : String aUserConfig = SvtPathOptions().GetUserConfigPath();
172 0 : INetURLObject aObj( aUserConfig );
173 0 : aObj.insertName( rtl::OUString("GlobalKeyBindings.xml") );
174 0 : SvStream* pStream = ::utl::UcbStreamHelper::CreateStream( aObj.GetMainURL( INetURLObject::NO_DECODE ), STREAM_STD_READWRITE|STREAM_TRUNC );
175 0 : com::sun::star::uno::Reference < ::com::sun::star::io::XOutputStream > xOut( new utl::OOutputStreamWrapper( *pStream ) );
176 0 : pImp->Commit( xOut );
177 0 : delete pStream;
178 : }
179 :
180 0 : DELETEZ( pOptions );
181 0 : }
182 : }
183 : else
184 : {
185 0 : delete pImp;
186 : }
187 0 : }
188 :
189 0 : String SvtAcceleratorConfiguration::GetStreamName()
190 : {
191 0 : return rtl::OUString("KeyBindings.xml");
192 : }
193 :
194 0 : SvStream* SvtAcceleratorConfiguration::GetDefaultStream( StreamMode nMode )
195 : {
196 0 : String aUserConfig = SvtPathOptions().GetUserConfigPath();
197 0 : INetURLObject aObj( aUserConfig );
198 0 : aObj.insertName( GetStreamName() );
199 0 : return ::utl::UcbStreamHelper::CreateStream( aObj.GetMainURL( INetURLObject::NO_DECODE ), nMode );
200 : }
201 :
202 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|