Line data Source code
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 :
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>
27 :
28 :
29 : namespace comphelper{
30 :
31 :
32 0 : css::uno::Reference< css::uno::XInterface > ConfigurationHelper::openConfig(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
33 : const OUString& sPackage,
34 : sal_Int32 eMode )
35 : {
36 : css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider(
37 0 : css::configuration::theDefaultProvider::get( rxContext ) );
38 :
39 0 : ::comphelper::SequenceAsVector< css::uno::Any > lParams;
40 0 : css::beans::PropertyValue aParam ;
41 :
42 : // set root path
43 0 : aParam.Name = "nodepath";
44 0 : aParam.Value <<= sPackage;
45 0 : lParams.push_back(css::uno::makeAny(aParam));
46 :
47 : // enable all locales mode
48 0 : if ((eMode & ConfigurationHelper::E_ALL_LOCALES)==ConfigurationHelper::E_ALL_LOCALES)
49 : {
50 0 : aParam.Name = "locale";
51 0 : aParam.Value <<= OUString("*");
52 0 : lParams.push_back(css::uno::makeAny(aParam));
53 : }
54 :
55 : // enable lazy writing
56 0 : bool bLazy = ((eMode & ConfigurationHelper::E_LAZY_WRITE)==ConfigurationHelper::E_LAZY_WRITE);
57 0 : aParam.Name = "lazywrite";
58 0 : aParam.Value = css::uno::makeAny(bLazy);
59 0 : lParams.push_back(css::uno::makeAny(aParam));
60 :
61 : // open it
62 0 : css::uno::Reference< css::uno::XInterface > xCFG;
63 :
64 0 : bool bReadOnly = ((eMode & ConfigurationHelper::E_READONLY)==ConfigurationHelper::E_READONLY);
65 0 : if (bReadOnly)
66 0 : xCFG = xConfigProvider->createInstanceWithArguments(
67 : OUString("com.sun.star.configuration.ConfigurationAccess"),
68 0 : lParams.getAsConstList());
69 : else
70 0 : xCFG = xConfigProvider->createInstanceWithArguments(
71 : OUString("com.sun.star.configuration.ConfigurationUpdateAccess"),
72 0 : lParams.getAsConstList());
73 :
74 0 : return xCFG;
75 : }
76 :
77 :
78 0 : css::uno::Any ConfigurationHelper::readRelativeKey(const css::uno::Reference< css::uno::XInterface > xCFG ,
79 : const OUString& sRelPath,
80 : const OUString& sKey )
81 : {
82 0 : css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
83 :
84 0 : css::uno::Reference< css::beans::XPropertySet > xProps;
85 0 : xAccess->getByHierarchicalName(sRelPath) >>= xProps;
86 0 : if (!xProps.is())
87 : {
88 0 : OUStringBuffer sMsg(256);
89 0 : sMsg.appendAscii("The requested path \"");
90 0 : sMsg.append (sRelPath );
91 0 : sMsg.appendAscii("\" does not exists." );
92 :
93 : throw css::container::NoSuchElementException(
94 : sMsg.makeStringAndClear(),
95 0 : css::uno::Reference< css::uno::XInterface >());
96 : }
97 0 : return xProps->getPropertyValue(sKey);
98 : }
99 :
100 :
101 0 : 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 )
105 : {
106 0 : css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
107 :
108 0 : css::uno::Reference< css::beans::XPropertySet > xProps;
109 0 : xAccess->getByHierarchicalName(sRelPath) >>= xProps;
110 0 : if (!xProps.is())
111 : {
112 0 : OUStringBuffer sMsg(256);
113 0 : sMsg.appendAscii("The requested path \"");
114 0 : sMsg.append (sRelPath );
115 0 : sMsg.appendAscii("\" does not exists." );
116 :
117 : throw css::container::NoSuchElementException(
118 : sMsg.makeStringAndClear(),
119 0 : css::uno::Reference< css::uno::XInterface >());
120 : }
121 0 : xProps->setPropertyValue(sKey, aValue);
122 0 : }
123 :
124 :
125 0 : css::uno::Reference< css::uno::XInterface > ConfigurationHelper::makeSureSetNodeExists(const css::uno::Reference< css::uno::XInterface > xCFG ,
126 : const OUString& sRelPathToSet,
127 : const OUString& sSetNode )
128 : {
129 0 : css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
130 0 : css::uno::Reference< css::container::XNameAccess > xSet;
131 0 : xAccess->getByHierarchicalName(sRelPathToSet) >>= xSet;
132 0 : if (!xSet.is())
133 : {
134 0 : OUStringBuffer sMsg(256);
135 0 : sMsg.appendAscii("The requested path \"");
136 0 : sMsg.append (sRelPathToSet );
137 0 : sMsg.appendAscii("\" does not exists." );
138 :
139 : throw css::container::NoSuchElementException(
140 : sMsg.makeStringAndClear(),
141 0 : css::uno::Reference< css::uno::XInterface >());
142 : }
143 :
144 0 : css::uno::Reference< css::uno::XInterface > xNode;
145 0 : if (xSet->hasByName(sSetNode))
146 0 : xSet->getByName(sSetNode) >>= xNode;
147 : else
148 : {
149 0 : css::uno::Reference< css::lang::XSingleServiceFactory > xNodeFactory(xSet, css::uno::UNO_QUERY_THROW);
150 0 : xNode = xNodeFactory->createInstance();
151 0 : css::uno::Reference< css::container::XNameContainer > xSetReplace(xSet, css::uno::UNO_QUERY_THROW);
152 0 : xSetReplace->insertByName(sSetNode, css::uno::makeAny(xNode));
153 : }
154 :
155 0 : return xNode;
156 : }
157 :
158 :
159 0 : 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 )
164 : {
165 0 : css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(rxContext, sPackage, eMode);
166 0 : return ConfigurationHelper::readRelativeKey(xCFG, sRelPath, sKey);
167 : }
168 :
169 :
170 0 : 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 )
176 : {
177 0 : css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(rxContext, sPackage, eMode);
178 0 : ConfigurationHelper::writeRelativeKey(xCFG, sRelPath, sKey, aValue);
179 0 : ConfigurationHelper::flush(xCFG);
180 0 : }
181 :
182 :
183 0 : void ConfigurationHelper::flush(const css::uno::Reference< css::uno::XInterface >& xCFG)
184 : {
185 0 : css::uno::Reference< css::util::XChangesBatch > xBatch(xCFG, css::uno::UNO_QUERY_THROW);
186 0 : xBatch->commitChanges();
187 0 : }
188 :
189 : } // namespace comphelper
190 :
191 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|