1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <jobs/configaccess.hxx>
23 #include <com/sun/star/configuration/theDefaultProvider.hpp>
24 #include <com/sun/star/beans/PropertyValue.hpp>
25 #include <com/sun/star/util/XChangesBatch.hpp>
28 #include <comphelper/diagnose_ex.hxx>
33 @short open the configuration of this job
34 @descr We open the configuration of this job only. Not the whole package or the whole
35 job set. We are interested on our own properties only.
36 We set the opened configuration access as our member. So any following method,
37 which needs cfg access, can use it. That prevent us against multiple open/close requests.
38 But you can use this method to upgrade an already opened configuration too.
41 force opening of the configuration access in readonly or in read/write mode
43 ConfigAccess::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
)
52 @short last chance to close an open configuration access point
53 @descr In case our user forgot to close this configuration point
54 in the right way, normally he will run into some trouble -
57 ConfigAccess::~ConfigAccess()
63 @short return the internal mode of this instance
64 @descr May be the outside user need any information about successfully opened
65 or closed config access point objects. He can control the internal mode to do so.
67 @return The internal open state of this object.
69 ConfigAccess::EOpenMode
ConfigAccess::getMode() const
71 std::unique_lock
g(m_mutex
);
76 @short open the configuration access in the specified mode
77 @descr We set the opened configuration access as our member. So any following method,
78 which needs cfg access, can use it. That prevent us against multiple open/close requests.
79 But you can use this method to upgrade an already opened configuration too.
80 It's possible to open a config access in READONLY mode first and "open" it at a second
81 time within the mode READWRITE. Then we will upgrade it. Downgrade will be possible too.
83 But note: closing will be done explicitly by calling method close() ... not by
84 downgrading with mode CLOSED!
87 force (re)opening of the configuration access in readonly or in read/write mode
89 void ConfigAccess::open( /*IN*/ EOpenMode eMode
)
91 std::unique_lock
g(m_mutex
);
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
)
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.
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
;
111 css::uno::Sequence
< css::uno::Any
> lParams
{ css::uno::Any(aParam
) };
116 if (eMode
==E_READONLY
)
117 m_xConfig
= xConfigProvider
->createInstanceWithArguments(SERVICENAME_CFGREADACCESS
, lParams
);
119 if (eMode
==E_READWRITE
)
120 m_xConfig
= xConfigProvider
->createInstanceWithArguments(SERVICENAME_CFGUPDATEACCESS
, lParams
);
122 catch(const css::uno::Exception
&)
124 TOOLS_INFO_EXCEPTION("fwk", "open config");
133 @short close the internal opened configuration access and flush all changes
134 @descr It checks, if the given access is valid and react in the right way.
135 It flushes all changes ... so nobody else must know this state.
137 void ConfigAccess::close()
139 std::unique_lock
g(m_mutex
);
143 void ConfigAccess::closeImpl()
145 // check already closed configuration
148 css::uno::Reference
< css::util::XChangesBatch
> xFlush(m_xConfig
, css::uno::UNO_QUERY
);
150 xFlush
->commitChanges();
157 @short provides an access to the internal wrapped configuration access
158 @descr It's not allowed to safe this c++ (!) reference outside. You have
159 to use it directly. Further you must use our public lock member m_aLock
160 to synchronize your code with our internal structures and our interface
161 methods. Acquire it before you call cfg() and release it afterwards immediately.
163 E.g.: ConfigAccess aAccess(...);
164 Guard aReadLock(aAccess.m_aLock);
165 Reference< XPropertySet > xSet(aAccess.cfg(), UNO_QUERY);
166 Any aProp = xSet->getPropertyValue("...");
169 @attention During this time it's not allowed to call the methods open() or close()!
170 Otherwise you will change your own referenced config access. Anything will
173 @return A c++(!) reference to the uno instance of the configuration access point.
175 const css::uno::Reference
< css::uno::XInterface
>& ConfigAccess::cfg()
177 // must be synchronized from outside!
178 // => no lock here ...
182 } // namespace framework
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */