LibreOffice Module framework (master) 1
configaccess.cxx
Go to the documentation of this file.
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#include <jobs/configaccess.hxx>
21#include <services.h>
22
23#include <com/sun/star/configuration/theDefaultProvider.hpp>
24#include <com/sun/star/beans/PropertyValue.hpp>
25#include <com/sun/star/util/XChangesBatch.hpp>
26#include <utility>
27
29
30namespace framework{
31
43ConfigAccess::ConfigAccess( /*IN*/ css::uno::Reference< css::uno::XComponentContext > xContext,
44 /*IN*/ OUString sRoot )
45 : m_xContext (std::move( xContext))
46 , m_sRoot (std::move( sRoot ))
47 , m_eMode ( E_CLOSED )
48{
49}
50
58{
59 close();
60}
61
70{
71 std::unique_lock g(m_mutex);
72 return m_eMode;
73}
74
89void ConfigAccess::open( /*IN*/ EOpenMode eMode )
90{
91 std::unique_lock g(m_mutex);
92
93 // check if configuration is already open in the right mode.
94 // By the way: Don't allow closing by using this method!
95 if ( eMode == E_CLOSED || m_eMode == eMode )
96 return;
97
98 // We have to close the old access point without any question here.
99 // It will be open again using the new mode.
100 // can be called without checks! It does the checks by itself ...
101 // e.g. for already closed or not opened configuration.
102 // Flushing of all made changes will be done here too.
103 closeImpl();
104
105 // create the configuration provider, which provides sub access points
106 css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider = css::configuration::theDefaultProvider::get(m_xContext);
107 css::beans::PropertyValue aParam;
108 aParam.Name = "nodepath";
109 aParam.Value <<= m_sRoot;
110
111 css::uno::Sequence< css::uno::Any > lParams{ css::uno::Any(aParam) };
112
113 // open it
114 try
115 {
116 if (eMode==E_READONLY)
117 m_xConfig = xConfigProvider->createInstanceWithArguments(SERVICENAME_CFGREADACCESS , lParams);
118 else
119 if (eMode==E_READWRITE)
120 m_xConfig = xConfigProvider->createInstanceWithArguments(SERVICENAME_CFGUPDATEACCESS, lParams);
121 }
122 catch(const css::uno::Exception&)
123 {
124 TOOLS_INFO_EXCEPTION("fwk", "open config");
125 }
126
128 if (m_xConfig.is())
129 m_eMode = eMode;
130}
131
138{
139 std::unique_lock g(m_mutex);
140 closeImpl();
141}
142
144{
145 // check already closed configuration
146 if (m_xConfig.is())
147 {
148 css::uno::Reference< css::util::XChangesBatch > xFlush(m_xConfig, css::uno::UNO_QUERY);
149 if (xFlush.is())
150 xFlush->commitChanges();
151 m_xConfig.clear();
153 }
154}
155
175const css::uno::Reference< css::uno::XInterface >& ConfigAccess::cfg()
176{
177 // must be synchronized from outside!
178 // => no lock here ...
179 return m_xConfig;
180}
181
182} // namespace framework
183
184/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void open(EOpenMode eMode)
open the configuration access in the specified mode @descr We set the opened configuration access as ...
css::uno::Reference< css::uno::XInterface > m_xConfig
hold an opened configuration alive
EOpenMode m_eMode
represent the current open mode
css::uno::Reference< css::uno::XComponentContext > m_xContext
reference to the uno service manager It's necessary to instantiate own needed services.
EOpenMode getMode() const
return the internal mode of this instance @descr May be the outside user need any information about s...
void close()
close the internal opened configuration access and flush all changes @descr It checks,...
~ConfigAccess()
last chance to close an open configuration access point @descr In case our user forgot to close this ...
OUString m_sRoot
knows the root of the opened config access point
const css::uno::Reference< css::uno::XInterface > & cfg()
provides an access to the internal wrapped configuration access @descr It's not allowed to safe this ...
EOpenMode
represent the possible modes of the internal wrapped configuration access
@ E_READONLY
config access is open for reading only
@ E_CLOSED
config isn't used yet
@ E_READWRITE
config access is open for reading/writing data
ConfigAccess(css::uno::Reference< css::uno::XComponentContext > xContext, OUString sRoot)
open the configuration of this job @descr We open the configuration of this job only.
#define TOOLS_INFO_EXCEPTION(area, stream)
css::uno::Reference< css::uno::XComponentContext > m_xContext
Mode eMode
constexpr OUStringLiteral SERVICENAME_CFGREADACCESS
Definition: services.h:30
constexpr OUStringLiteral SERVICENAME_CFGUPDATEACCESS
Definition: services.h:29