Branch libreoffice-6-3
[LibreOffice.git] / include / unotools / confignode.hxx
blobbde22e9ff04bc6491a02b1ef24c7d7870bf04a18
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 { namespace sun { namespace star { namespace container { class XHierarchicalNameAccess; } } } }
26 namespace com { namespace sun { namespace star { namespace container { class XNameAccess; } } } }
27 namespace com { namespace sun { namespace star { namespace container { class XNameContainer; } } } }
28 namespace com { namespace sun { namespace star { namespace container { class XNameReplace; } } } }
29 namespace com { namespace sun { namespace star { namespace lang { class XMultiServiceFactory; } } } }
30 namespace com { namespace sun { namespace star { namespace uno { class XComponentContext; } } } }
31 namespace com { namespace sun { namespace star { namespace 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 throw();
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 throw();
84 OConfigurationNode openNode( const sal_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 throw();
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 throw();
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 throw();
115 css::uno::Any getNodeValue( const sal_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 throw();
128 bool setNodeValue( const sal_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 throw();
137 /** get the flag specifying the current escape behaviour
138 @see setEscape
140 bool getEscape() const { return m_bEscapeNames; }
142 /// invalidate the object
143 virtual void clear() throw();
145 // meta information about the node
147 /// checks whether or not the object represents a set node.
148 bool isSetNode() const;
150 /// checks whether or not a direct child with a given name exists
151 bool hasByName(const OUString& _rName) const throw();
153 /// checks whether or not a descendent (no matter if direct or indirect) with the given name exists
154 bool hasByHierarchicalName( const OUString& _rName ) const throw();
156 /// check if the objects represents a valid configuration node
157 bool isValid() const { return m_xHierarchyAccess.is(); }
159 /// check whether the object is read-only of updatable
160 bool isReadonly() const { return !m_xReplaceAccess.is(); }
162 protected:
163 // OEventListenerAdapter
164 virtual void _disposing( const css::lang::EventObject& _rSource ) override;
166 protected:
167 enum NAMEORIGIN
169 NO_CONFIGURATION, /// the name came from a configuration node
170 NO_CALLER /// the name came from a client of this class
172 OUString normalizeName(const OUString& _rName, NAMEORIGIN _eOrigin) const;
175 //= OConfigurationTreeRoot
177 /** a specialized version of a OConfigurationNode, representing the root
178 of a configuration sub tree<p/>
179 Only this class is able to commit any changes made any any OConfigurationNode
180 objects.
182 class UNOTOOLS_DLLPUBLIC OConfigurationTreeRoot : public OConfigurationNode
184 css::uno::Reference< css::util::XChangesBatch >
185 m_xCommitter;
186 protected:
187 /** ctor for a readonly node
189 OConfigurationTreeRoot(
190 const css::uno::Reference< css::uno::XInterface >& _rxRootNode
193 public:
194 /// modes to use when creating a top-level node object
195 enum CREATION_MODE
197 /// open the node (i.e. sub tree) for read access only
198 CM_READONLY,
199 /// open the node (i.e. sub tree) for read and write access, fall back to read-only if write access is not possible
200 CM_UPDATABLE
203 public:
204 /** default ctor<p/>
205 The object constructed here is invalid (i.e. <method>isValid</method> will return sal_False).
207 OConfigurationTreeRoot() :OConfigurationNode() { }
209 /** creates a configuration tree for the given path in the given mode
211 OConfigurationTreeRoot(
212 const css::uno::Reference<css::uno::XComponentContext> & i_rContext,
213 const OUString& i_rNodePath,
214 const bool i_bUpdatable
217 /** open a new top-level configuration node
219 opens a new node which is the root if an own configuration sub tree. This is what "top level" means: The
220 node does not have a parent. It does not mean that the node represents a module tree (like org.openoffice.Office.Writer
221 or such).<br/>
222 In opposite to <method>createWithServiceFactory</method>, createWithProvider expects a configuration provider
223 to work with.
225 @param _rxConfProvider configuration provider to use when retrieving the node.
226 @param _rPath path to the node the object should represent
227 @param _nDepth depth for node retrieval
228 @param _eMode specifies which privileges should be applied when retrieving the node
230 @see createWithServiceFactory
232 static OConfigurationTreeRoot createWithProvider(
233 const css::uno::Reference< css::lang::XMultiServiceFactory >& _rxConfProvider,
234 const OUString& _rPath,
235 sal_Int32 _nDepth,
236 CREATION_MODE _eMode
239 /** open a new top-level configuration node<p/>
240 opens a new node which is the root if an own configuration sub tree. This is what "top level" means: The
241 node does not have a parent. It does not mean that the node represents a module tree (like org.openoffice.Office.Writer
242 or such).<br/>
243 In opposite to <method>createWithProvider</method>, createWithProvider expects a service factory. This factory
244 is used to create a configuration provider, and this provider is used to retrieve the node
245 @see createWithProvider
246 @param _rxContext service factory to use to create the configuration provider.
247 @param _rPath path to the node the object should represent
248 @param _nDepth depth for node retrieval
249 @param _eMode specifies which privileges should be applied when retrieving the node
251 static OConfigurationTreeRoot createWithComponentContext(const css::uno::Reference< css::uno::XComponentContext >& _rxContext,
252 const OUString& _rPath, sal_Int32 _nDepth = -1, CREATION_MODE _eMode = CM_UPDATABLE);
254 /** tolerant version of the <member>createWithServiceFactory</member>
256 <p>No assertions are thrown in case of an failure to initialize the configuration service, but once
257 the configuration could be initialized, errors in the creation of the specific node (e.g. because the
258 given node path does not exist) are still asserted.</p>
260 static OConfigurationTreeRoot tryCreateWithComponentContext( const css::uno::Reference< css::uno::XComponentContext >& rxContext,
261 const OUString& _rPath, sal_Int32 _nDepth = -1, CREATION_MODE _eMode = CM_UPDATABLE );
263 /** commit all changes made on the subtree the object is the root for<p/>
264 All changes made on any OConfigurationNode object retrieved (maybe indirect) from this root
265 object are committed when calling this method.
266 @return sal_True if and only if the commit was successful
268 bool commit() const throw();
270 /// invalidate the object
271 virtual void clear() throw() override;
274 } // namespace utl
276 #endif // INCLUDED_UNOTOOLS_CONFIGNODE_HXX
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */