bump product version to 4.1.6.2
[LibreOffice.git] / comphelper / source / misc / configurationhelper.cxx
blob14b82de407fff56b5699497a406568b454bf17ce
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 .
20 #include <comphelper/configurationhelper.hxx>
21 #include <comphelper/processfactory.hxx>
22 #include <com/sun/star/beans/XPropertySet.hpp>
23 #include <com/sun/star/configuration/theDefaultProvider.hpp>
24 #include <com/sun/star/container/XNameAccess.hpp>
25 #include <com/sun/star/container/XNameContainer.hpp>
26 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
29 namespace comphelper{
31 //-----------------------------------------------
32 css::uno::Reference< css::uno::XInterface > ConfigurationHelper::openConfig(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
33 const OUString& sPackage,
34 sal_Int32 eMode )
36 css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider(
37 css::configuration::theDefaultProvider::get( rxContext ) );
39 ::comphelper::SequenceAsVector< css::uno::Any > lParams;
40 css::beans::PropertyValue aParam ;
42 // set root path
43 aParam.Name = "nodepath";
44 aParam.Value <<= sPackage;
45 lParams.push_back(css::uno::makeAny(aParam));
47 // enable all locales mode
48 if ((eMode & ConfigurationHelper::E_ALL_LOCALES)==ConfigurationHelper::E_ALL_LOCALES)
50 aParam.Name = "locale";
51 aParam.Value <<= OUString("*");
52 lParams.push_back(css::uno::makeAny(aParam));
55 // enable lazy writing
56 sal_Bool bLazy = ((eMode & ConfigurationHelper::E_LAZY_WRITE)==ConfigurationHelper::E_LAZY_WRITE);
57 aParam.Name = "lazywrite";
58 aParam.Value = css::uno::makeAny(bLazy);
59 lParams.push_back(css::uno::makeAny(aParam));
61 // open it
62 css::uno::Reference< css::uno::XInterface > xCFG;
64 sal_Bool bReadOnly = ((eMode & ConfigurationHelper::E_READONLY)==ConfigurationHelper::E_READONLY);
65 if (bReadOnly)
66 xCFG = xConfigProvider->createInstanceWithArguments(
67 OUString("com.sun.star.configuration.ConfigurationAccess"),
68 lParams.getAsConstList());
69 else
70 xCFG = xConfigProvider->createInstanceWithArguments(
71 OUString("com.sun.star.configuration.ConfigurationUpdateAccess"),
72 lParams.getAsConstList());
74 return xCFG;
77 //-----------------------------------------------
78 css::uno::Any ConfigurationHelper::readRelativeKey(const css::uno::Reference< css::uno::XInterface > xCFG ,
79 const OUString& sRelPath,
80 const OUString& sKey )
82 css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
84 css::uno::Reference< css::beans::XPropertySet > xProps;
85 xAccess->getByHierarchicalName(sRelPath) >>= xProps;
86 if (!xProps.is())
88 OUStringBuffer sMsg(256);
89 sMsg.appendAscii("The requested path \"");
90 sMsg.append (sRelPath );
91 sMsg.appendAscii("\" does not exists." );
93 throw css::container::NoSuchElementException(
94 sMsg.makeStringAndClear(),
95 css::uno::Reference< css::uno::XInterface >());
97 return xProps->getPropertyValue(sKey);
100 //-----------------------------------------------
101 void ConfigurationHelper::writeRelativeKey(const css::uno::Reference< css::uno::XInterface > xCFG ,
102 const OUString& sRelPath,
103 const OUString& sKey ,
104 const css::uno::Any& aValue )
106 css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
108 css::uno::Reference< css::beans::XPropertySet > xProps;
109 xAccess->getByHierarchicalName(sRelPath) >>= xProps;
110 if (!xProps.is())
112 OUStringBuffer sMsg(256);
113 sMsg.appendAscii("The requested path \"");
114 sMsg.append (sRelPath );
115 sMsg.appendAscii("\" does not exists." );
117 throw css::container::NoSuchElementException(
118 sMsg.makeStringAndClear(),
119 css::uno::Reference< css::uno::XInterface >());
121 xProps->setPropertyValue(sKey, aValue);
124 //-----------------------------------------------
125 css::uno::Reference< css::uno::XInterface > ConfigurationHelper::makeSureSetNodeExists(const css::uno::Reference< css::uno::XInterface > xCFG ,
126 const OUString& sRelPathToSet,
127 const OUString& sSetNode )
129 css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
130 css::uno::Reference< css::container::XNameAccess > xSet;
131 xAccess->getByHierarchicalName(sRelPathToSet) >>= xSet;
132 if (!xSet.is())
134 OUStringBuffer sMsg(256);
135 sMsg.appendAscii("The requested path \"");
136 sMsg.append (sRelPathToSet );
137 sMsg.appendAscii("\" does not exists." );
139 throw css::container::NoSuchElementException(
140 sMsg.makeStringAndClear(),
141 css::uno::Reference< css::uno::XInterface >());
144 css::uno::Reference< css::uno::XInterface > xNode;
145 if (xSet->hasByName(sSetNode))
146 xSet->getByName(sSetNode) >>= xNode;
147 else
149 css::uno::Reference< css::lang::XSingleServiceFactory > xNodeFactory(xSet, css::uno::UNO_QUERY_THROW);
150 xNode = xNodeFactory->createInstance();
151 css::uno::Reference< css::container::XNameContainer > xSetReplace(xSet, css::uno::UNO_QUERY_THROW);
152 xSetReplace->insertByName(sSetNode, css::uno::makeAny(xNode));
155 return xNode;
158 //-----------------------------------------------
159 css::uno::Any ConfigurationHelper::readDirectKey(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
160 const OUString& sPackage,
161 const OUString& sRelPath,
162 const OUString& sKey ,
163 sal_Int32 eMode )
165 css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(rxContext, sPackage, eMode);
166 return ConfigurationHelper::readRelativeKey(xCFG, sRelPath, sKey);
169 //-----------------------------------------------
170 void ConfigurationHelper::writeDirectKey(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
171 const OUString& sPackage,
172 const OUString& sRelPath,
173 const OUString& sKey ,
174 const css::uno::Any& aValue ,
175 sal_Int32 eMode )
177 css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(rxContext, sPackage, eMode);
178 ConfigurationHelper::writeRelativeKey(xCFG, sRelPath, sKey, aValue);
179 ConfigurationHelper::flush(xCFG);
182 //-----------------------------------------------
183 void ConfigurationHelper::flush(const css::uno::Reference< css::uno::XInterface >& xCFG)
185 css::uno::Reference< css::util::XChangesBatch > xBatch(xCFG, css::uno::UNO_QUERY_THROW);
186 xBatch->commitChanges();
189 } // namespace comphelper
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */