build fix
[LibreOffice.git] / include / unotools / confignode.hxx
bloba59e30454a3cc60bd1d1e6bb7906f85b3b49d907
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 <com/sun/star/container/XHierarchicalNameAccess.hpp>
24 #include <com/sun/star/container/XNameAccess.hpp>
25 #include <com/sun/star/container/XNameContainer.hpp>
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27 #include <com/sun/star/util/XChangesBatch.hpp>
28 #include <com/sun/star/uno/XComponentContext.hpp>
29 #include <unotools/eventlisteneradapter.hxx>
31 namespace comphelper
33 class ComponentContext;
36 namespace utl
39 //= OConfigurationNode
41 class OConfigurationTreeRoot;
42 /** a small wrapper around a configuration node.<p/>
43 Nodes in the terminology used herein are <em>inner</em> nodes of a configuration
44 tree, which means <em>no leafs</em>.
46 class UNOTOOLS_DLLPUBLIC OConfigurationNode : public ::utl::OEventListenerAdapter
48 private:
49 css::uno::Reference< css::container::XHierarchicalNameAccess >
50 m_xHierarchyAccess; /// accessing children grandchildren (mandatory interface of our UNO object)
51 css::uno::Reference< css::container::XNameAccess >
52 m_xDirectAccess; /// accessing children (mandatory interface of our UNO object)
53 css::uno::Reference< css::container::XNameReplace >
54 m_xReplaceAccess; /// replacing child values
55 css::uno::Reference< css::container::XNameContainer >
56 m_xContainerAccess; /// modifying set nodes (optional interface of our UNO object)
57 bool m_bEscapeNames; /// escape names before accessing children ?
59 OConfigurationNode insertNode(const OUString& _rName,const css::uno::Reference< css::uno::XInterface >& _xNode) const throw();
61 protected:
62 /// constructs a node object with an interface representing a node
63 OConfigurationNode(
64 const css::uno::Reference< css::uno::XInterface >& _rxNode
67 const css::uno::Reference< css::container::XNameAccess >&
68 getUNONode() const { return m_xDirectAccess; }
70 public:
71 /// constructs an empty and invalid node object
72 OConfigurationNode() :m_bEscapeNames(false) { }
73 /// copy ctor
74 OConfigurationNode(const OConfigurationNode& _rSource);
75 /// move ctor
76 OConfigurationNode(OConfigurationNode&& _rSource);
78 /// assigment
79 OConfigurationNode& operator=(const OConfigurationNode& _rSource);
80 OConfigurationNode& operator=(OConfigurationNode&& _rSource);
82 /// dtor
83 virtual ~OConfigurationNode() override {}
85 /// returns the local name of the node
86 OUString getLocalName() const;
88 /** open a sub node
89 @param _rPath access path of the to-be-opened sub node. May be a hierarchical path.
91 OConfigurationNode openNode(const OUString& _rPath) const throw();
93 OConfigurationNode openNode( const sal_Char* _pAsciiPath ) const
95 return openNode( OUString::createFromAscii( _pAsciiPath ) );
98 /** create a new child node
100 If the object represents a set node, this method may be used to create a new child. For non-set-nodes, the
101 method will fail.<br/>
102 Unless the respective operations on the pure configuration API, the to-be-created node immediately
103 becomes a part of its hierarchy, no explicit insertion is necessary.
104 @param _rName name for the new child. Must be level-1-depth.
106 OConfigurationNode createNode(const OUString& _rName) const throw();
108 /** remove an existent child nod
110 If the object represents a set node, this method may be used to delete an existent child. For non-set-nodes,
111 the method will fail.
113 bool removeNode(const OUString& _rName) const throw();
115 /** retrieves the content of a descendant
117 the returned value may contain anything from an interface (if <arg>_rPath</arg> refers to inner node of
118 the configuration tree) to any explicit value (e.g. string, integer) or even void.<br/>
119 Unfortunately, this implies that if a void value is returned, you won't have a clue if this means
120 "the path does not exist" (besides the assertion made :), or if the value is really void.
122 css::uno::Any getNodeValue(const OUString& _rPath) const throw();
124 css::uno::Any getNodeValue( const sal_Char* _pAsciiPath ) const
126 return getNodeValue( OUString::createFromAscii( _pAsciiPath ) );
129 /** write a node value<p/>
130 The value given is written into the node specified by the given relative path.<br/>
131 In opposite to <method>getNodeValue</method>, _rName must refer to a leaf in the configuration tree, not an inner
132 node.
133 @return sal_True if and only if the write was successful.
135 bool setNodeValue(const OUString& _rPath, const css::uno::Any& _rValue) const throw();
137 bool setNodeValue( const sal_Char* _pAsciiPath, const css::uno::Any& _rValue ) const
139 return setNodeValue( OUString::createFromAscii( _pAsciiPath ), _rValue );
142 /// return the names of the existing children
143 css::uno::Sequence< OUString >
144 getNodeNames() const throw();
146 /** get the flag specifying the current escape behaviour
147 @see setEscape
149 bool getEscape() const { return m_bEscapeNames; }
151 /// invalidate the object
152 virtual void clear() throw();
154 // meta information about the node
156 /// checks whether or not the object represents a set node.
157 bool isSetNode() const;
159 /// checks whether or not a direct child with a given name exists
160 bool hasByName(const OUString& _rName) const throw();
161 bool hasByName( const sal_Char* _pAsciiName ) const { return hasByName( OUString::createFromAscii( _pAsciiName ) ); }
163 /// checks whether or not a descendent (no matter if direct or indirect) with the given name exists
164 bool hasByHierarchicalName( const OUString& _rName ) const throw();
166 /// check if the objects represents a valid configuration node
167 bool isValid() const { return m_xHierarchyAccess.is(); }
169 /// check whether the object is read-only of updatable
170 bool isReadonly() const { return !m_xReplaceAccess.is(); }
172 protected:
173 // OEventListenerAdapter
174 virtual void _disposing( const css::lang::EventObject& _rSource ) override;
176 protected:
177 enum NAMEORIGIN
179 NO_CONFIGURATION, /// the name came from a configuration node
180 NO_CALLER /// the name came from a client of this class
182 OUString normalizeName(const OUString& _rName, NAMEORIGIN _eOrigin) const;
185 //= OConfigurationTreeRoot
187 /** a specialized version of a OConfigurationNode, representing the root
188 of a configuration sub tree<p/>
189 Only this class is able to commit any changes made any any OConfigurationNode
190 objects.
192 class UNOTOOLS_DLLPUBLIC OConfigurationTreeRoot : public OConfigurationNode
194 css::uno::Reference< css::util::XChangesBatch >
195 m_xCommitter;
196 protected:
197 /** ctor for a readonly node
199 OConfigurationTreeRoot(
200 const css::uno::Reference< css::uno::XInterface >& _rxRootNode
203 public:
204 /// modes to use when creating a top-level node object
205 enum CREATION_MODE
207 /// open the node (i.e. sub tree) for read access only
208 CM_READONLY,
209 /// open the node (i.e. sub tree) for read and write access, fall back to read-only if write access is not possible
210 CM_UPDATABLE
213 public:
214 /** default ctor<p/>
215 The object constructed here is invalid (i.e. <method>isValid</method> will return sal_False).
217 OConfigurationTreeRoot() :OConfigurationNode() { }
219 /** creates a configuration tree for the given path in the given mode
221 OConfigurationTreeRoot(
222 const css::uno::Reference<css::uno::XComponentContext> & i_rContext,
223 const OUString& i_rNodePath,
224 const bool i_bUpdatable
227 /// copy ctor
228 OConfigurationTreeRoot(const OConfigurationTreeRoot& _rSource)
229 :OConfigurationNode(_rSource), m_xCommitter(_rSource.m_xCommitter) { }
231 /** open a new top-level configuration node
233 opens a new node which is the root if an own configuration sub tree. This is what "top level" means: The
234 node does not have a parent. It does not mean that the node represents a module tree (like org.openoffice.Office.Writer
235 or such).<br/>
236 In opposite to <method>createWithServiceFactory</method>, createWithProvider expects a configuration provider
237 to work with.
239 @param _rxConfProvider configuration provider to use when retrieving the node.
240 @param _rPath path to the node the object should represent
241 @param _nDepth depth for node retrieval
242 @param _eMode specifies which privileges should be applied when retrieving the node
244 @see createWithServiceFactory
246 static OConfigurationTreeRoot createWithProvider(
247 const css::uno::Reference< css::lang::XMultiServiceFactory >& _rxConfProvider,
248 const OUString& _rPath,
249 sal_Int32 _nDepth,
250 CREATION_MODE _eMode,
251 bool _bLazyWrite = true
254 /** open a new top-level configuration node<p/>
255 opens a new node which is the root if an own configuration sub tree. This is what "top level" means: The
256 node does not have a parent. It does not mean that the node represents a module tree (like org.openoffice.Office.Writer
257 or such).<br/>
258 In opposite to <method>createWithProvider</method>, createWithProvider expects a service factory. This factory
259 is used to create a configuration provider, and this provider is used to retrieve the node
260 @see createWithProvider
261 @param _rxContext service factory to use to create the configuration provider.
262 @param _rPath path to the node the object should represent
263 @param _nDepth depth for node retrieval
264 @param _eMode specifies which privileges should be applied when retrieving the node
266 static OConfigurationTreeRoot createWithComponentContext(const css::uno::Reference< css::uno::XComponentContext >& _rxContext,
267 const OUString& _rPath, sal_Int32 _nDepth = -1, CREATION_MODE _eMode = CM_UPDATABLE, bool _bLazyWrite = true);
269 /** tolerant version of the <member>createWithServiceFactory</member>
271 <p>No assertions are thrown in case of an failure to initialize the configuration service, but once
272 the configuration could be initialized, errors in the creation of the specific node (e.g. because the
273 given node path does not exist) are still asserted.</p>
275 static OConfigurationTreeRoot tryCreateWithComponentContext( const css::uno::Reference< css::uno::XComponentContext >& rxContext,
276 const OUString& _rPath, sal_Int32 _nDepth = -1, CREATION_MODE _eMode = CM_UPDATABLE );
278 /** commit all changes made on the subtree the object is the root for<p/>
279 All changes made on any OConfigurationNode object retrieved (maybe indirect) from this root
280 object are committed when calling this method.
281 @return sal_True if and only if the commit was successful
283 bool commit() const throw();
285 /// invalidate the object
286 virtual void clear() throw() override;
289 } // namespace utl
291 #endif // INCLUDED_UNOTOOLS_CONFIGNODE_HXX
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */