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