Update git submodules
[LibreOffice.git] / include / unotools / confignode.hxx
blobf334aa149794edf6d107da445c1e15d45afead8c
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 .
19 #ifndef INCLUDED_UNOTOOLS_CONFIGNODE_HXX
20 #define INCLUDED_UNOTOOLS_CONFIGNODE_HXX
22 #include <unotools/unotoolsdllapi.h>
23 #include <unotools/eventlisteneradapter.hxx>
25 namespace com::sun::star::container { class XHierarchicalNameAccess; }
26 namespace com::sun::star::container { class XNameAccess; }
27 namespace com::sun::star::container { class XNameContainer; }
28 namespace com::sun::star::container { class XNameReplace; }
29 namespace com::sun::star::lang { class XMultiServiceFactory; }
30 namespace com::sun::star::uno { class XComponentContext; }
31 namespace com::sun::star::util { class XChangesBatch; }
33 namespace utl
36 /** a small wrapper around a configuration node.<p/>
37 Nodes in the terminology used herein are <em>inner</em> nodes of a configuration
38 tree, which means <em>no leafs</em>.
40 class UNOTOOLS_DLLPUBLIC OConfigurationNode : public ::utl::OEventListenerAdapter
42 private:
43 css::uno::Reference< css::container::XHierarchicalNameAccess >
44 m_xHierarchyAccess; /// accessing children grandchildren (mandatory interface of our UNO object)
45 css::uno::Reference< css::container::XNameAccess >
46 m_xDirectAccess; /// accessing children (mandatory interface of our UNO object)
47 css::uno::Reference< css::container::XNameReplace >
48 m_xReplaceAccess; /// replacing child values
49 css::uno::Reference< css::container::XNameContainer >
50 m_xContainerAccess; /// modifying set nodes (optional interface of our UNO object)
51 bool m_bEscapeNames; /// escape names before accessing children ?
53 OConfigurationNode insertNode(const OUString& _rName,const css::uno::Reference< css::uno::XInterface >& _xNode) const noexcept;
55 protected:
56 /// constructs a node object with an interface representing a node
57 OConfigurationNode(
58 const css::uno::Reference< css::uno::XInterface >& _rxNode
61 const css::uno::Reference< css::container::XNameAccess >&
62 getUNONode() const { return m_xDirectAccess; }
64 public:
65 /// constructs an empty and invalid node object
66 OConfigurationNode() :m_bEscapeNames(false) { }
67 /// copy ctor
68 OConfigurationNode(const OConfigurationNode& _rSource);
69 /// move ctor
70 OConfigurationNode(OConfigurationNode&& _rSource);
72 /// assignment
73 OConfigurationNode& operator=(const OConfigurationNode& _rSource);
74 OConfigurationNode& operator=(OConfigurationNode&& _rSource);
76 /// returns the local name of the node
77 OUString getLocalName() const;
79 /** open a sub node
80 @param _rPath access path of the to-be-opened sub node. May be a hierarchical path.
82 OConfigurationNode openNode(const OUString& _rPath) const noexcept;
84 OConfigurationNode openNode( const char* _pAsciiPath ) const
86 return openNode( OUString::createFromAscii( _pAsciiPath ) );
89 /** create a new child node
91 If the object represents a set node, this method may be used to create a new child. For non-set-nodes, the
92 method will fail.<br/>
93 Unless the respective operations on the pure configuration API, the to-be-created node immediately
94 becomes a part of its hierarchy, no explicit insertion is necessary.
95 @param _rName name for the new child. Must be level-1-depth.
97 OConfigurationNode createNode(const OUString& _rName) const noexcept;
99 /** remove an existent child nod
101 If the object represents a set node, this method may be used to delete an existent child. For non-set-nodes,
102 the method will fail.
104 bool removeNode(const OUString& _rName) const noexcept;
106 /** retrieves the content of a descendant
108 the returned value may contain anything from an interface (if <arg>_rPath</arg> refers to inner node of
109 the configuration tree) to any explicit value (e.g. string, integer) or even void.<br/>
110 Unfortunately, this implies that if a void value is returned, you won't have a clue if this means
111 "the path does not exist" (besides the assertion made :), or if the value is really void.
113 css::uno::Any getNodeValue(const OUString& _rPath) const noexcept;
115 css::uno::Any getNodeValue( const char* _pAsciiPath ) const
117 return getNodeValue( OUString::createFromAscii( _pAsciiPath ) );
120 /** write a node value<p/>
121 The value given is written into the node specified by the given relative path.<br/>
122 In opposite to <method>getNodeValue</method>, _rName must refer to a leaf in the configuration tree, not an inner
123 node.
124 @return sal_True if and only if the write was successful.
126 bool setNodeValue(const OUString& _rPath, const css::uno::Any& _rValue) const noexcept;
128 bool setNodeValue( const char* _pAsciiPath, const css::uno::Any& _rValue ) const
130 return setNodeValue( OUString::createFromAscii( _pAsciiPath ), _rValue );
133 /// return the names of the existing children
134 css::uno::Sequence< OUString >
135 getNodeNames() const noexcept;
137 /// invalidate the object
138 virtual void clear() noexcept;
140 // meta information about the node
142 /// checks whether or not the object represents a set node.
143 bool isSetNode() const;
145 /// checks whether or not a direct child with a given name exists
146 bool hasByName(const OUString& _rName) const noexcept;
148 /// checks whether or not a descendent (no matter if direct or indirect) with the given name exists
149 bool hasByHierarchicalName( const OUString& _rName ) const noexcept;
151 /// check if the objects represents a valid configuration node
152 bool isValid() const { return m_xHierarchyAccess.is(); }
154 /// check whether the object is read-only of updatable
155 bool isReadonly() const { return !m_xReplaceAccess.is(); }
157 protected:
158 // OEventListenerAdapter
159 virtual void _disposing( const css::lang::EventObject& _rSource ) override;
161 protected:
162 enum NAMEORIGIN
164 NO_CONFIGURATION, /// the name came from a configuration node
165 NO_CALLER /// the name came from a client of this class
167 OUString normalizeName(const OUString& _rName, NAMEORIGIN _eOrigin) const;
170 //= OConfigurationTreeRoot
172 /** a specialized version of an OConfigurationNode, representing the root
173 of a configuration sub tree<p/>
174 Only this class is able to commit any changes made any any OConfigurationNode
175 objects.
177 class UNOTOOLS_DLLPUBLIC OConfigurationTreeRoot final : public OConfigurationNode
179 css::uno::Reference< css::util::XChangesBatch >
180 m_xCommitter;
181 /** ctor for a readonly node
183 OConfigurationTreeRoot(
184 const css::uno::Reference< css::uno::XInterface >& _rxRootNode
187 public:
188 /// modes to use when creating a top-level node object
189 enum CREATION_MODE
191 /// open the node (i.e. sub tree) for read access only
192 CM_READONLY,
193 /// open the node (i.e. sub tree) for read and write access, fall back to read-only if write access is not possible
194 CM_UPDATABLE
197 public:
198 /** default ctor<p/>
199 The object constructed here is invalid (i.e. <method>isValid</method> will return sal_False).
201 OConfigurationTreeRoot() {}
203 /** creates a configuration tree for the given path in the given mode
205 OConfigurationTreeRoot(
206 const css::uno::Reference<css::uno::XComponentContext> & i_rContext,
207 const OUString& i_rNodePath,
208 const bool i_bUpdatable
211 /** open a new top-level configuration node
213 opens a new node which is the root if an own configuration sub tree. This is what "top level" means: The
214 node does not have a parent. It does not mean that the node represents a module tree (like org.openoffice.Office.Writer
215 or such).<br/>
216 In opposite to <method>createWithServiceFactory</method>, createWithProvider expects a configuration provider
217 to work with.
219 @param _rxConfProvider configuration provider to use when retrieving the node.
220 @param _rPath path to the node the object should represent
221 @param _nDepth depth for node retrieval
222 @param _eMode specifies which privileges should be applied when retrieving the node
224 @see createWithServiceFactory
226 static OConfigurationTreeRoot createWithProvider(
227 const css::uno::Reference< css::lang::XMultiServiceFactory >& _rxConfProvider,
228 const OUString& _rPath,
229 sal_Int32 _nDepth,
230 CREATION_MODE _eMode
233 /** open a new top-level configuration node<p/>
234 opens a new node which is the root if an own configuration sub tree. This is what "top level" means: The
235 node does not have a parent. It does not mean that the node represents a module tree (like org.openoffice.Office.Writer
236 or such).<br/>
237 In opposite to <method>createWithProvider</method>, createWithProvider expects a service factory. This factory
238 is used to create a configuration provider, and this provider is used to retrieve the node
239 @see createWithProvider
240 @param _rxContext service factory to use to create the configuration provider.
241 @param _rPath path to the node the object should represent
242 @param _nDepth depth for node retrieval
243 @param _eMode specifies which privileges should be applied when retrieving the node
245 static OConfigurationTreeRoot createWithComponentContext(const css::uno::Reference< css::uno::XComponentContext >& _rxContext,
246 const OUString& _rPath, sal_Int32 _nDepth = -1, CREATION_MODE _eMode = CM_UPDATABLE);
248 /** tolerant version of the <member>createWithServiceFactory</member>
250 <p>No assertions are thrown in case of a failure to initialize the configuration service, but once
251 the configuration could be initialized, errors in the creation of the specific node (e.g. because the
252 given node path does not exist) are still asserted.</p>
254 static OConfigurationTreeRoot tryCreateWithComponentContext( const css::uno::Reference< css::uno::XComponentContext >& rxContext,
255 const OUString& _rPath, sal_Int32 _nDepth = -1, CREATION_MODE _eMode = CM_UPDATABLE );
257 /** commit all changes made on the subtree the object is the root for<p/>
258 All changes made on any OConfigurationNode object retrieved (maybe indirect) from this root
259 object are committed when calling this method.
260 @return sal_True if and only if the commit was successful
262 bool commit() const noexcept;
264 /// invalidate the object
265 virtual void clear() noexcept override;
268 } // namespace utl
270 #endif // INCLUDED_UNOTOOLS_CONFIGNODE_HXX
272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */