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 13493 : 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 13493 : css::configuration::theDefaultProvider::get( rxContext ) );
38 :
39 26986 : ::comphelper::SequenceAsVector< css::uno::Any > lParams;
40 26986 : css::beans::PropertyValue aParam ;
41 :
42 : // set root path
43 13493 : aParam.Name = "nodepath";
44 13493 : aParam.Value <<= sPackage;
45 13493 : lParams.push_back(css::uno::makeAny(aParam));
46 :
47 : // enable all locales mode
48 13493 : if ((eMode & ConfigurationHelper::E_ALL_LOCALES)==ConfigurationHelper::E_ALL_LOCALES)
49 : {
50 1502 : aParam.Name = "locale";
51 1502 : aParam.Value <<= OUString("*");
52 1502 : lParams.push_back(css::uno::makeAny(aParam));
53 : }
54 :
55 : // enable lazy writing
56 13493 : bool bLazy = ((eMode & ConfigurationHelper::E_LAZY_WRITE)==ConfigurationHelper::E_LAZY_WRITE);
57 13493 : aParam.Name = "lazywrite";
58 13493 : aParam.Value = css::uno::makeAny(bLazy);
59 13493 : lParams.push_back(css::uno::makeAny(aParam));
60 :
61 : // open it
62 13493 : css::uno::Reference< css::uno::XInterface > xCFG;
63 :
64 13493 : bool bReadOnly = ((eMode & ConfigurationHelper::E_READONLY)==ConfigurationHelper::E_READONLY);
65 13493 : if (bReadOnly)
66 25227 : xCFG = xConfigProvider->createInstanceWithArguments(
67 : OUString("com.sun.star.configuration.ConfigurationAccess"),
68 16818 : lParams.getAsConstList());
69 : else
70 15252 : xCFG = xConfigProvider->createInstanceWithArguments(
71 : OUString("com.sun.star.configuration.ConfigurationUpdateAccess"),
72 10168 : lParams.getAsConstList());
73 :
74 26986 : return xCFG;
75 : }
76 :
77 :
78 8465 : css::uno::Any ConfigurationHelper::readRelativeKey(const css::uno::Reference< css::uno::XInterface > xCFG ,
79 : const OUString& sRelPath,
80 : const OUString& sKey )
81 : {
82 8465 : css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
83 :
84 16930 : css::uno::Reference< css::beans::XPropertySet > xProps;
85 8465 : xAccess->getByHierarchicalName(sRelPath) >>= xProps;
86 8465 : 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 16930 : return xProps->getPropertyValue(sKey);
98 : }
99 :
100 :
101 2049 : 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 2049 : css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
107 :
108 4098 : css::uno::Reference< css::beans::XPropertySet > xProps;
109 2049 : xAccess->getByHierarchicalName(sRelPath) >>= xProps;
110 2049 : 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 4098 : xProps->setPropertyValue(sKey, aValue);
122 2049 : }
123 :
124 :
125 13098 : 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 13098 : css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
130 26196 : css::uno::Reference< css::container::XNameAccess > xSet;
131 13098 : xAccess->getByHierarchicalName(sRelPathToSet) >>= xSet;
132 13098 : 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 13098 : css::uno::Reference< css::uno::XInterface > xNode;
145 13098 : if (xSet->hasByName(sSetNode))
146 12784 : xSet->getByName(sSetNode) >>= xNode;
147 : else
148 : {
149 314 : css::uno::Reference< css::lang::XSingleServiceFactory > xNodeFactory(xSet, css::uno::UNO_QUERY_THROW);
150 314 : xNode = xNodeFactory->createInstance();
151 628 : css::uno::Reference< css::container::XNameContainer > xSetReplace(xSet, css::uno::UNO_QUERY_THROW);
152 628 : xSetReplace->insertByName(sSetNode, css::uno::makeAny(xNode));
153 : }
154 :
155 26196 : return xNode;
156 : }
157 :
158 :
159 6286 : 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 6286 : css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(rxContext, sPackage, eMode);
166 6286 : return ConfigurationHelper::readRelativeKey(xCFG, sRelPath, sKey);
167 : }
168 :
169 :
170 2049 : 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 2049 : css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(rxContext, sPackage, eMode);
178 2049 : ConfigurationHelper::writeRelativeKey(xCFG, sRelPath, sKey, aValue);
179 2049 : ConfigurationHelper::flush(xCFG);
180 2049 : }
181 :
182 :
183 16987 : void ConfigurationHelper::flush(const css::uno::Reference< css::uno::XInterface >& xCFG)
184 : {
185 16987 : css::uno::Reference< css::util::XChangesBatch > xBatch(xCFG, css::uno::UNO_QUERY_THROW);
186 16987 : xBatch->commitChanges();
187 16987 : }
188 :
189 : } // namespace comphelper
190 :
191 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|