Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / unotools / source / config / accelcfg.cxx
blob9c48b7f5e06ee2975f26e6805d2e6f7ef7c8324f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
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>
39 #include <unotools/accelcfg.hxx>
40 #include <unotools/xmlaccelcfg.hxx>
41 #include <unotools/pathoptions.hxx>
42 #include "itemholder1.hxx"
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;
50 using ::rtl::OUString;
53 static SvtAcceleratorConfig_Impl* pOptions = NULL;
54 static sal_Int32 nRefCount = 0;
56 class SvtAcceleratorConfig_Impl
58 public:
60 SvtAcceleratorItemList aList;
61 bool bModified;
63 SvtAcceleratorConfig_Impl()
64 : bModified( sal_False )
67 SvtAcceleratorConfig_Impl( Reference< XInputStream >& xInputStream );
68 bool Commit( Reference< XOutputStream >& xOutputStream );
71 // -----------------------------------------------------------------------
73 SvtAcceleratorConfig_Impl::SvtAcceleratorConfig_Impl( Reference< XInputStream >& rInputStream )
74 : bModified( false )
76 Reference< XParser > xParser = Parser::create( ::comphelper::getProcessComponentContext() );
78 // connect stream to input stream to the parser
79 InputSource aInputSource;
80 aInputSource.aInputStream = rInputStream;
82 // get filter
83 Reference< XDocumentHandler > xFilter( new OReadAccelatorDocumentHandler( aList ));
85 // connect parser and filter
86 xParser->setDocumentHandler( xFilter );
87 xParser->parseStream( aInputSource );
90 bool SvtAcceleratorConfig_Impl::Commit( Reference< XOutputStream >& rOutputStream )
92 Reference< XWriter > xWriter = Writer::create( ::comphelper::getProcessComponentContext() );
94 xWriter->setOutputStream( rOutputStream );
95 try
97 OWriteAccelatorDocumentHandler aWriteHandler( aList, Reference<XDocumentHandler>(xWriter, UNO_QUERY_THROW) );
98 aWriteHandler.WriteAcceleratorDocument();
99 rOutputStream->flush();
100 return true;
102 catch ( RuntimeException& )
105 catch ( SAXException& )
108 catch ( ::com::sun::star::io::IOException& )
112 return false;
115 namespace
117 class LocalSingleton : public rtl::Static< osl::Mutex, LocalSingleton >
122 SvtAcceleratorConfiguration::SvtAcceleratorConfiguration()
124 // Global access, must be guarded (multithreading)
125 ::osl::MutexGuard aGuard( LocalSingleton::get() );
126 if ( !pOptions )
128 SvStream* pStream = GetDefaultStream( STREAM_STD_READ );
129 ::utl::OInputStreamWrapper aHelper( *pStream );
130 com::sun::star::uno::Reference < ::com::sun::star::io::XInputStream > xOut( &aHelper );
134 pOptions = new SvtAcceleratorConfig_Impl( xOut );
136 catch ( RuntimeException& )
138 pOptions = new SvtAcceleratorConfig_Impl();
140 catch( SAXException& )
142 pOptions = new SvtAcceleratorConfig_Impl();
144 catch( ::com::sun::star::io::IOException& )
146 pOptions = new SvtAcceleratorConfig_Impl();
149 if (pOptions)
150 ItemHolder1::holdConfigItem(E_ACCELCFG);
152 delete pStream;
155 ++nRefCount;
156 pImp = pOptions;
159 // -----------------------------------------------------------------------
161 SvtAcceleratorConfiguration::~SvtAcceleratorConfiguration()
163 if ( pImp == pOptions )
165 // Global access, must be guarded (multithreading)
166 ::osl::MutexGuard aGuard( LocalSingleton::get() );
167 if ( !--nRefCount )
169 if ( pImp->bModified )
171 String aUserConfig = SvtPathOptions().GetUserConfigPath();
172 INetURLObject aObj( aUserConfig );
173 aObj.insertName( rtl::OUString("GlobalKeyBindings.xml") );
174 SvStream* pStream = ::utl::UcbStreamHelper::CreateStream( aObj.GetMainURL( INetURLObject::NO_DECODE ), STREAM_STD_READWRITE|STREAM_TRUNC );
175 com::sun::star::uno::Reference < ::com::sun::star::io::XOutputStream > xOut( new utl::OOutputStreamWrapper( *pStream ) );
176 pImp->Commit( xOut );
177 delete pStream;
180 DELETEZ( pOptions );
183 else
185 delete pImp;
189 String SvtAcceleratorConfiguration::GetStreamName()
191 return rtl::OUString("KeyBindings.xml");
194 SvStream* SvtAcceleratorConfiguration::GetDefaultStream( StreamMode nMode )
196 String aUserConfig = SvtPathOptions().GetUserConfigPath();
197 INetURLObject aObj( aUserConfig );
198 aObj.insertName( GetStreamName() );
199 return ::utl::UcbStreamHelper::CreateStream( aObj.GetMainURL( INetURLObject::NO_DECODE ), nMode );
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */