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 .
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
; } } } }
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
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();
56 /// constructs a node object with an interface representing a node
58 const css::uno::Reference
< css::uno::XInterface
>& _rxNode
61 const css::uno::Reference
< css::container::XNameAccess
>&
62 getUNONode() const { return m_xDirectAccess
; }
65 /// constructs an empty and invalid node object
66 OConfigurationNode() :m_bEscapeNames(false) { }
68 OConfigurationNode(const OConfigurationNode
& _rSource
);
70 OConfigurationNode(OConfigurationNode
&& _rSource
);
73 OConfigurationNode
& operator=(const OConfigurationNode
& _rSource
);
74 OConfigurationNode
& operator=(OConfigurationNode
&& _rSource
);
76 /// returns the local name of the node
77 OUString
getLocalName() const;
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
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
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(); }
163 // OEventListenerAdapter
164 virtual void _disposing( const css::lang::EventObject
& _rSource
) override
;
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 an 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
182 class UNOTOOLS_DLLPUBLIC OConfigurationTreeRoot final
: public OConfigurationNode
184 css::uno::Reference
< css::util::XChangesBatch
>
186 /** ctor for a readonly node
188 OConfigurationTreeRoot(
189 const css::uno::Reference
< css::uno::XInterface
>& _rxRootNode
193 /// modes to use when creating a top-level node object
196 /// open the node (i.e. sub tree) for read access only
198 /// open the node (i.e. sub tree) for read and write access, fall back to read-only if write access is not possible
204 The object constructed here is invalid (i.e. <method>isValid</method> will return sal_False).
206 OConfigurationTreeRoot() :OConfigurationNode() { }
208 /** creates a configuration tree for the given path in the given mode
210 OConfigurationTreeRoot(
211 const css::uno::Reference
<css::uno::XComponentContext
> & i_rContext
,
212 const OUString
& i_rNodePath
,
213 const bool i_bUpdatable
216 /** open a new top-level configuration node
218 opens a new node which is the root if an own configuration sub tree. This is what "top level" means: The
219 node does not have a parent. It does not mean that the node represents a module tree (like org.openoffice.Office.Writer
221 In opposite to <method>createWithServiceFactory</method>, createWithProvider expects a configuration provider
224 @param _rxConfProvider configuration provider to use when retrieving the node.
225 @param _rPath path to the node the object should represent
226 @param _nDepth depth for node retrieval
227 @param _eMode specifies which privileges should be applied when retrieving the node
229 @see createWithServiceFactory
231 static OConfigurationTreeRoot
createWithProvider(
232 const css::uno::Reference
< css::lang::XMultiServiceFactory
>& _rxConfProvider
,
233 const OUString
& _rPath
,
238 /** open a new top-level configuration node<p/>
239 opens a new node which is the root if an own configuration sub tree. This is what "top level" means: The
240 node does not have a parent. It does not mean that the node represents a module tree (like org.openoffice.Office.Writer
242 In opposite to <method>createWithProvider</method>, createWithProvider expects a service factory. This factory
243 is used to create a configuration provider, and this provider is used to retrieve the node
244 @see createWithProvider
245 @param _rxContext service factory to use to create the configuration provider.
246 @param _rPath path to the node the object should represent
247 @param _nDepth depth for node retrieval
248 @param _eMode specifies which privileges should be applied when retrieving the node
250 static OConfigurationTreeRoot
createWithComponentContext(const css::uno::Reference
< css::uno::XComponentContext
>& _rxContext
,
251 const OUString
& _rPath
, sal_Int32 _nDepth
= -1, CREATION_MODE _eMode
= CM_UPDATABLE
);
253 /** tolerant version of the <member>createWithServiceFactory</member>
255 <p>No assertions are thrown in case of a failure to initialize the configuration service, but once
256 the configuration could be initialized, errors in the creation of the specific node (e.g. because the
257 given node path does not exist) are still asserted.</p>
259 static OConfigurationTreeRoot
tryCreateWithComponentContext( const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
,
260 const OUString
& _rPath
, sal_Int32 _nDepth
= -1, CREATION_MODE _eMode
= CM_UPDATABLE
);
262 /** commit all changes made on the subtree the object is the root for<p/>
263 All changes made on any OConfigurationNode object retrieved (maybe indirect) from this root
264 object are committed when calling this method.
265 @return sal_True if and only if the commit was successful
267 bool commit() const throw();
269 /// invalidate the object
270 virtual void clear() throw() override
;
275 #endif // INCLUDED_UNOTOOLS_CONFIGNODE_HXX
277 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */