23#include <osl/diagnose.h>
25#include <com/sun/star/configuration/theDefaultProvider.hpp>
26#include <com/sun/star/lang/XSingleServiceFactory.hpp>
27#include <com/sun/star/lang/XComponent.hpp>
28#include <com/sun/star/util/XChangesBatch.hpp>
29#include <com/sun/star/util/XStringEscape.hpp>
30#include <com/sun/star/lang/XServiceInfo.hpp>
31#include <com/sun/star/container/XNamed.hpp>
32#include <com/sun/star/container/XNameContainer.hpp>
33#include <com/sun/star/container/XHierarchicalNameAccess.hpp>
49 :m_bEscapeNames(false)
51 OSL_ENSURE(_rxNode.is(),
"OConfigurationNode::OConfigurationNode: invalid node interface!");
55 m_xHierarchyAccess.set(_rxNode, UNO_QUERY);
56 m_xDirectAccess.set(_rxNode, UNO_QUERY);
59 if (!m_xHierarchyAccess.is() || !m_xDirectAccess.is())
61 m_xHierarchyAccess =
nullptr;
62 m_xDirectAccess =
nullptr;
66 m_xReplaceAccess.set(_rxNode, UNO_QUERY);
67 m_xContainerAccess.set(_rxNode, UNO_QUERY);
70 Reference< XComponent > xConfigNodeComp(m_xDirectAccess, UNO_QUERY);
71 if (xConfigNodeComp.is())
72 startComponentListening(xConfigNodeComp);
75 m_bEscapeNames = isSetNode() && Reference< XStringEscape >::query(m_xDirectAccess).is();
80 , m_xHierarchyAccess(_rSource.m_xHierarchyAccess)
81 , m_xDirectAccess(_rSource.m_xDirectAccess)
82 , m_xReplaceAccess(_rSource.m_xReplaceAccess)
83 , m_xContainerAccess(_rSource.m_xContainerAccess)
84 , m_bEscapeNames(_rSource.m_bEscapeNames)
87 if (xConfigNodeComp.is())
93 , m_xHierarchyAccess(
std::move(_rSource.m_xHierarchyAccess))
94 , m_xDirectAccess(
std::move(_rSource.m_xDirectAccess))
95 , m_xReplaceAccess(
std::move(_rSource.m_xReplaceAccess))
96 , m_xContainerAccess(
std::move(_rSource.m_xContainerAccess))
97 , m_bEscapeNames(
std::move(_rSource.m_bEscapeNames))
100 if (xConfigNodeComp.is())
115 if (xConfigNodeComp.is())
132 if (xConfigNodeComp.is())
140 Reference< XComponent > xDisposingSource(_rSource.Source, UNO_QUERY);
142 if (xDisposingSource.get() == xConfigNodeComp.get())
152 sLocalName = xNamed->getName();
163 OUString
sName(_rName);
167 if (xEscaper.is() && !
sName.isEmpty())
187 OSL_ENSURE(
m_xDirectAccess.is(),
"OConfigurationNode::getNodeNames: object is invalid!");
195 std::transform(std::cbegin(aReturn), std::cend(aReturn), aReturn.getArray(),
196 [
this](
const OUString& rName) -> OUString { return normalizeName(rName, NO_CONFIGURATION); });
209 OSL_ENSURE(m_xContainerAccess.is(),
"OConfigurationNode::removeNode: object is invalid!");
210 if (m_xContainerAccess.is())
214 OUString
sName = normalizeName(_rName, NO_CALLER);
215 m_xContainerAccess->removeByName(
sName);
218 catch (NoSuchElementException&)
220 SAL_WARN(
"unotools",
"OConfigurationNode::removeNode: there is no element named: " << _rName );
236 OUString
sName = normalizeName(_rName, NO_CALLER);
237 m_xContainerAccess->insertByName(
sName,
Any(_xNode));
247 Reference< XComponent > xChildComp(_xNode, UNO_QUERY);
249 try { xChildComp->dispose(); }
catch(
Exception&) { }
257 Reference< XSingleServiceFactory > xChildFactory(m_xContainerAccess, UNO_QUERY);
258 OSL_ENSURE(xChildFactory.is(),
"OConfigurationNode::createNode: object is invalid or read-only!");
260 if (xChildFactory.is())
262 Reference< XInterface > xNewChild;
265 xNewChild = xChildFactory->createInstance();
271 return insertNode(_rName,xNewChild);
279 OSL_ENSURE(m_xDirectAccess.is(),
"OConfigurationNode::openNode: object is invalid!");
280 OSL_ENSURE(m_xHierarchyAccess.is(),
"OConfigurationNode::openNode: object is invalid!");
283 OUString sNormalized = normalizeName(_rPath, NO_CALLER);
285 Reference< XInterface > xNode;
286 if (m_xDirectAccess.is() && m_xDirectAccess->hasByName(sNormalized))
289 m_xDirectAccess->getByName(sNormalized), css::uno::UNO_QUERY);
291 OSL_FAIL(
"OConfigurationNode::openNode: could not open the node!");
293 else if (m_xHierarchyAccess.is())
296 m_xHierarchyAccess->getByHierarchicalName(_rPath),
297 css::uno::UNO_QUERY);
299 OSL_FAIL(
"OConfigurationNode::openNode: could not open the node!");
304 catch(
const NoSuchElementException&)
306 SAL_WARN(
"unotools",
"OConfigurationNode::openNode: there is no element named " << _rPath );
310 TOOLS_WARN_EXCEPTION(
"unotools",
"OConfigurationNode::openNode: caught an exception while retrieving the node!");
321 try { bIsSet = xSI->supportsService(
"com.sun.star.configuration.SetAccess"); }
329 OSL_ENSURE( m_xHierarchyAccess.is(),
"OConfigurationNode::hasByHierarchicalName: no hierarchy access!" );
332 if ( m_xHierarchyAccess.is() )
334 OUString
sName = normalizeName( _rName, NO_CALLER );
335 return m_xHierarchyAccess->hasByHierarchicalName(
sName );
346 OSL_ENSURE(m_xDirectAccess.is(),
"OConfigurationNode::hasByName: object is invalid!");
349 OUString
sName = normalizeName(_rName, NO_CALLER);
350 if (m_xDirectAccess.is())
351 return m_xDirectAccess->hasByName(
sName);
361 bool bResult =
false;
363 OSL_ENSURE(m_xReplaceAccess.is(),
"OConfigurationNode::setNodeValue: object is invalid!");
364 if (m_xReplaceAccess.is())
369 OUString sNormalizedName = normalizeName(_rPath, NO_CALLER);
370 if (m_xReplaceAccess->hasByName(sNormalizedName))
372 m_xReplaceAccess->replaceByName(sNormalizedName, _rValue);
377 else if (m_xHierarchyAccess.is() && m_xHierarchyAccess->hasByHierarchicalName(_rPath))
379 OSL_ASSERT(!_rPath.isEmpty());
381 OUString sParentPath, sLocalName;
385 OConfigurationNode aParentAccess = openNode(sParentPath);
386 if (aParentAccess.isValid())
387 bResult = aParentAccess.setNodeValue(sLocalName, _rValue);
391 m_xReplaceAccess->replaceByName(sLocalName, _rValue);
399 TOOLS_WARN_EXCEPTION(
"unotools",
"OConfigurationNode::setNodeValue: could not replace the value");
408 OSL_ENSURE(m_xDirectAccess.is(),
"OConfigurationNode::hasByName: object is invalid!");
409 OSL_ENSURE(m_xHierarchyAccess.is(),
"OConfigurationNode::hasByName: object is invalid!");
413 OUString sNormalizedPath = normalizeName(_rPath, NO_CALLER);
414 if (m_xDirectAccess.is() && m_xDirectAccess->hasByName(sNormalizedPath) )
416 aReturn = m_xDirectAccess->getByName(sNormalizedPath);
418 else if (m_xHierarchyAccess.is())
420 aReturn = m_xHierarchyAccess->getByHierarchicalName(_rPath);
423 catch(
const NoSuchElementException&)
443 Reference< XMultiServiceFactory > lcl_getConfigProvider(
const Reference<XComponentContext> & i_rContext )
450 catch (
const Exception& )
457 Reference< XInterface > lcl_createConfigurationRoot(
const Reference< XMultiServiceFactory >& i_rxConfigProvider,
458 const OUString& i_rNodePath,
const bool i_bUpdatable,
const sal_Int32 i_nDepth )
464 aArgs.
put(
"nodepath", i_rNodePath );
465 aArgs.
put(
"depth", i_nDepth );
467 OUString sAccessService( i_bUpdatable ?
468 OUString(
"com.sun.star.configuration.ConfigurationUpdateAccess" ) :
469 OUString(
"com.sun.star.configuration.ConfigurationAccess" ));
471 Reference< XInterface > xRoot(
477 catch (
const Exception& )
486 :OConfigurationNode( _rxRootNode )
487 ,m_xCommitter( _rxRootNode, UNO_QUERY )
492 :OConfigurationNode( lcl_createConfigurationRoot( lcl_getConfigProvider( i_rContext ),
493 i_rNodePath, i_bUpdatable, -1 ) )
499 OSL_ENSURE( m_xCommitter.is(),
"OConfigurationTreeRoot::OConfigurationTreeRoot: could not create an updatable node!" );
511 OSL_ENSURE(
isValid(),
"OConfigurationTreeRoot::commit: object is invalid!");
514 OSL_ENSURE(
m_xCommitter.is(),
"OConfigurationTreeRoot::commit: I'm a readonly node!");
532 Reference< XInterface > xRoot( lcl_createConfigurationRoot(
533 _rxConfProvider, _rPath, _eMode !=
CM_READONLY, _nDepth ) );
541 return createWithProvider( lcl_getConfigProvider( _rxContext ), _rPath, _nDepth, _eMode );
545 const OUString& _rPath, sal_Int32 _nDepth ,
CREATION_MODE _eMode )
547 OSL_ENSURE( rxContext.is(),
"OConfigurationTreeRoot::tryCreateWithComponentContext: invalid XComponentContext!" );
css::uno::Sequence< css::uno::Any > getWrappedPropertyValues() const
bool put(const OUString &_rValueName, const VALUE_TYPE &_rValue)
a small wrapper around a configuration node.
OUString normalizeName(const OUString &_rName, NAMEORIGIN _eOrigin) const
virtual void clear() noexcept
invalidate the object
OConfigurationNode insertNode(const OUString &_rName, const css::uno::Reference< css::uno::XInterface > &_xNode) const noexcept
escape names before accessing children ?
css::uno::Reference< css::container::XNameReplace > m_xReplaceAccess
accessing children (mandatory interface of our UNO object)
bool m_bEscapeNames
modifying set nodes (optional interface of our UNO object)
css::uno::Sequence< OUString > getNodeNames() const noexcept
return the names of the existing children
bool isValid() const
check if the objects represents a valid configuration node
css::uno::Any getNodeValue(const OUString &_rPath) const noexcept
retrieves the content of a descendant
OConfigurationNode()
constructs an empty and invalid node object
css::uno::Reference< css::container::XHierarchicalNameAccess > m_xHierarchyAccess
css::uno::Reference< css::container::XNameContainer > m_xContainerAccess
replacing child values
bool hasByName(const OUString &_rName) const noexcept
checks whether or not a direct child with a given name exists
bool hasByHierarchicalName(const OUString &_rName) const noexcept
checks whether or not a descendent (no matter if direct or indirect) with the given name exists
bool isSetNode() const
checks whether or not the object represents a set node.
bool removeNode(const OUString &_rName) const noexcept
remove an existent child nod
css::uno::Reference< css::container::XNameAccess > m_xDirectAccess
accessing children grandchildren (mandatory interface of our UNO object)
OUString getLocalName() const
returns the local name of the node
const css::uno::Reference< css::container::XNameAccess > & getUNONode() const
bool setNodeValue(const OUString &_rPath, const css::uno::Any &_rValue) const noexcept
write a node valueThe value given is written into the node specified by the given relative path.
virtual void _disposing(const css::lang::EventObject &_rSource) override
OConfigurationNode createNode(const OUString &_rName) const noexcept
create a new child node
OConfigurationNode & operator=(const OConfigurationNode &_rSource)
assignment
@ NO_CALLER
the name came from a configuration node
OConfigurationNode openNode(const OUString &_rPath) const noexcept
open a sub node
a specialized version of an OConfigurationNode, representing the root of a configuration sub treeOnly...
OConfigurationTreeRoot()
default ctorThe object constructed here is invalid (i.e.
static OConfigurationTreeRoot createWithComponentContext(const css::uno::Reference< css::uno::XComponentContext > &_rxContext, const OUString &_rPath, sal_Int32 _nDepth=-1, CREATION_MODE _eMode=CM_UPDATABLE)
open a new top-level configuration nodeopens a new node which is the root if an own configuration sub...
css::uno::Reference< css::util::XChangesBatch > m_xCommitter
virtual void clear() noexcept override
invalidate the object
static OConfigurationTreeRoot createWithProvider(const css::uno::Reference< css::lang::XMultiServiceFactory > &_rxConfProvider, const OUString &_rPath, sal_Int32 _nDepth, CREATION_MODE _eMode)
open a new top-level configuration node
CREATION_MODE
modes to use when creating a top-level node object
@ CM_READONLY
open the node (i.e. sub tree) for read access only
bool commit() const noexcept
commit all changes made on the subtree the object is the root forAll changes made on any OConfigurati...
static OConfigurationTreeRoot tryCreateWithComponentContext(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const OUString &_rPath, sal_Int32 _nDepth=-1, CREATION_MODE _eMode=CM_UPDATABLE)
tolerant version of the <member>createWithServiceFactory</member>
base class for non-UNO dispose listeners
void stopAllComponentListening()
void startComponentListening(const css::uno::Reference< css::lang::XComponent > &_rxComp)
#define TOOLS_WARN_EXCEPTION(area, stream)
#define ENSURE_OR_RETURN(c, m, r)
#define DBG_UNHANDLED_EXCEPTION(...)
#define SAL_WARN(area, stream)
OUString get(TranslateId sContextAndId, const std::locale &loc)
bool splitLastFromConfigurationPath(std::u16string_view _sInPath, OUString &_rsOutPath, OUString &_rsLocalName)
extract the local nodename and the parent nodepath from a configuration path.