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