LibreOffice Module comphelper (master)  1
configurationhelper.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
21 #include <comphelper/sequence.hxx>
22 #include <com/sun/star/beans/XPropertySet.hpp>
23 #include <com/sun/star/beans/PropertyValue.hpp>
24 #include <com/sun/star/configuration/theDefaultProvider.hpp>
25 #include <com/sun/star/container/XNameAccess.hpp>
26 #include <com/sun/star/container/XNameContainer.hpp>
27 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
28 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
29 #include <com/sun/star/util/XChangesBatch.hpp>
30 
31 
32 namespace comphelper{
33 
34 
35 css::uno::Reference< css::uno::XInterface > ConfigurationHelper::openConfig(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
36  const OUString& sPackage,
37  EConfigurationModes eMode )
38 {
39  css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider(
40  css::configuration::theDefaultProvider::get( rxContext ) );
41 
42  std::vector< css::uno::Any > lParams;
43  css::beans::PropertyValue aParam ;
44 
45  // set root path
46  aParam.Name = "nodepath";
47  aParam.Value <<= sPackage;
48  lParams.emplace_back(aParam);
49 
50  // enable all locales mode
52  {
53  aParam.Name = "locale";
54  aParam.Value <<= OUString("*");
55  lParams.emplace_back(aParam);
56  }
57 
58  // open it
59  css::uno::Reference< css::uno::XInterface > xCFG;
60 
61  bool bReadOnly(eMode & EConfigurationModes::ReadOnly);
62  if (bReadOnly)
63  xCFG = xConfigProvider->createInstanceWithArguments(
64  "com.sun.star.configuration.ConfigurationAccess",
66  else
67  xCFG = xConfigProvider->createInstanceWithArguments(
68  "com.sun.star.configuration.ConfigurationUpdateAccess",
70 
71  return xCFG;
72 }
73 
74 
75 css::uno::Any ConfigurationHelper::readRelativeKey(const css::uno::Reference< css::uno::XInterface >& xCFG ,
76  const OUString& sRelPath,
77  const OUString& sKey )
78 {
79  css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
80 
81  css::uno::Reference< css::beans::XPropertySet > xProps;
82  xAccess->getByHierarchicalName(sRelPath) >>= xProps;
83  if (!xProps.is())
84  {
85  throw css::container::NoSuchElementException(
86  "The requested path \"" + sRelPath + "\" does not exist.");
87  }
88  return xProps->getPropertyValue(sKey);
89 }
90 
91 
92 void ConfigurationHelper::writeRelativeKey(const css::uno::Reference< css::uno::XInterface >& xCFG ,
93  const OUString& sRelPath,
94  const OUString& sKey ,
95  const css::uno::Any& aValue )
96 {
97  css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
98 
99  css::uno::Reference< css::beans::XPropertySet > xProps;
100  xAccess->getByHierarchicalName(sRelPath) >>= xProps;
101  if (!xProps.is())
102  {
103  throw css::container::NoSuchElementException(
104  "The requested path \"" + sRelPath + "\" does not exist.");
105  }
106  xProps->setPropertyValue(sKey, aValue);
107 }
108 
109 
110 css::uno::Reference< css::uno::XInterface > ConfigurationHelper::makeSureSetNodeExists(const css::uno::Reference< css::uno::XInterface >& xCFG ,
111  const OUString& sRelPathToSet,
112  const OUString& sSetNode )
113 {
114  css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
115  css::uno::Reference< css::container::XNameAccess > xSet;
116  xAccess->getByHierarchicalName(sRelPathToSet) >>= xSet;
117  if (!xSet.is())
118  {
119  throw css::container::NoSuchElementException(
120  "The requested path \"" + sRelPathToSet + "\" does not exist." );
121  }
122 
123  css::uno::Reference< css::uno::XInterface > xNode;
124  if (xSet->hasByName(sSetNode))
125  xSet->getByName(sSetNode) >>= xNode;
126  else
127  {
128  css::uno::Reference< css::lang::XSingleServiceFactory > xNodeFactory(xSet, css::uno::UNO_QUERY_THROW);
129  xNode = xNodeFactory->createInstance();
130  css::uno::Reference< css::container::XNameContainer > xSetReplace(xSet, css::uno::UNO_QUERY_THROW);
131  xSetReplace->insertByName(sSetNode, css::uno::Any(xNode));
132  }
133 
134  return xNode;
135 }
136 
137 
138 css::uno::Any ConfigurationHelper::readDirectKey(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
139  const OUString& sPackage,
140  const OUString& sRelPath,
141  const OUString& sKey ,
142  EConfigurationModes eMode )
143 {
144  css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(rxContext, sPackage, eMode);
145  return ConfigurationHelper::readRelativeKey(xCFG, sRelPath, sKey);
146 }
147 
148 
149 void ConfigurationHelper::writeDirectKey(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
150  const OUString& sPackage,
151  const OUString& sRelPath,
152  const OUString& sKey ,
153  const css::uno::Any& aValue ,
154  EConfigurationModes eMode )
155 {
156  css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(rxContext, sPackage, eMode);
157  ConfigurationHelper::writeRelativeKey(xCFG, sRelPath, sKey, aValue);
159 }
160 
161 
162 void ConfigurationHelper::flush(const css::uno::Reference< css::uno::XInterface >& xCFG)
163 {
164  css::uno::Reference< css::util::XChangesBatch > xBatch(xCFG, css::uno::UNO_QUERY_THROW);
165  xBatch->commitChanges();
166 }
167 
168 } // namespace comphelper
169 
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static void writeRelativeKey(const css::uno::Reference< css::uno::XInterface > &xCFG, const OUString &sRelPath, const OUString &sKey, const css::uno::Any &aValue)
writes a new value for an existing(!) configuration key, which is searched relative to the specified ...
static css::uno::Any readDirectKey(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const OUString &sPackage, const OUString &sRelPath, const OUString &sKey, EConfigurationModes eMode)
does the same then openConfig() & readRelativeKey() together.
static void writeDirectKey(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const OUString &sPackage, const OUString &sRelPath, const OUString &sKey, const css::uno::Any &aValue, EConfigurationModes eMode)
does the same then openConfig() / writeRelativeKey() & flush() together.
all localized nodes will be interpreted as XInterface instead of interpreting it as atomic value node...
configuration will be opened readonly
EConfigurationModes
specify all possible modes, which can be used to open a configuration access.
static void flush(const css::uno::Reference< css::uno::XInterface > &xCFG)
commit all changes made on the specified configuration access.
static css::uno::Reference< css::uno::XInterface > makeSureSetNodeExists(const css::uno::Reference< css::uno::XInterface > &xCFG, const OUString &sRelPathToSet, const OUString &sSetNode)
it checks if the specified set node exists ...
static css::uno::Reference< css::uno::XInterface > openConfig(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const OUString &sPackage, EConfigurationModes eMode)
returns access to the specified configuration package.
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Copy from a container into a Sequence.
Definition: sequence.hxx:182
static css::uno::Any readRelativeKey(const css::uno::Reference< css::uno::XInterface > &xCFG, const OUString &sRelPath, const OUString &sKey)
reads the value of an existing(!) configuration key, which is searched relative to the specified conf...