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 :
21 : #include "tools/ConfigurationAccess.hxx"
22 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 : #include <com/sun/star/beans/PropertyValue.hpp>
24 : #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
25 : #include <com/sun/star/configuration/theDefaultProvider.hpp>
26 : #include <com/sun/star/util/XChangesBatch.hpp>
27 : #include <comphelper/processfactory.hxx>
28 : #include <tools/diagnose_ex.h>
29 :
30 : using namespace ::com::sun::star;
31 : using namespace ::com::sun::star::uno;
32 :
33 : namespace sd { namespace tools {
34 :
35 0 : ConfigurationAccess::ConfigurationAccess (
36 : const Reference<XComponentContext>& rxContext,
37 : const OUString& rsRootName,
38 : const WriteMode eMode)
39 0 : : mxRoot()
40 : {
41 : Reference<lang::XMultiServiceFactory> xProvider =
42 0 : configuration::theDefaultProvider::get( rxContext );
43 0 : Initialize(xProvider, rsRootName, eMode);
44 0 : }
45 :
46 :
47 :
48 :
49 0 : ConfigurationAccess::ConfigurationAccess (
50 : const OUString& rsRootName,
51 : const WriteMode eMode)
52 0 : : mxRoot()
53 : {
54 : Reference<lang::XMultiServiceFactory> xProvider =
55 0 : configuration::theDefaultProvider::get( ::comphelper::getProcessComponentContext() );
56 0 : Initialize(xProvider, rsRootName, eMode);
57 0 : }
58 :
59 :
60 :
61 :
62 0 : void ConfigurationAccess::Initialize (
63 : const Reference<lang::XMultiServiceFactory>& rxProvider,
64 : const OUString& rsRootName,
65 : const WriteMode eMode)
66 : {
67 : try
68 : {
69 0 : Sequence<Any> aCreationArguments(3);
70 0 : aCreationArguments[0] = makeAny(beans::PropertyValue(
71 : "nodepath",
72 : 0,
73 : makeAny(rsRootName),
74 0 : beans::PropertyState_DIRECT_VALUE));
75 0 : aCreationArguments[1] = makeAny(beans::PropertyValue(
76 : "depth",
77 : 0,
78 : makeAny((sal_Int32)-1),
79 0 : beans::PropertyState_DIRECT_VALUE));
80 0 : aCreationArguments[2] = makeAny(beans::PropertyValue(
81 : "lazywrite",
82 : 0,
83 : makeAny(true),
84 0 : beans::PropertyState_DIRECT_VALUE));
85 0 : OUString sAccessService;
86 0 : if (eMode == READ_ONLY)
87 0 : sAccessService = "com.sun.star.configuration.ConfigurationAccess";
88 : else
89 0 : sAccessService = "com.sun.star.configuration.ConfigurationUpdateAccess";
90 :
91 0 : mxRoot = rxProvider->createInstanceWithArguments(
92 : sAccessService,
93 0 : aCreationArguments);
94 : }
95 0 : catch (Exception&)
96 : {
97 : DBG_UNHANDLED_EXCEPTION();
98 : }
99 0 : }
100 :
101 :
102 :
103 :
104 0 : Any ConfigurationAccess::GetConfigurationNode (
105 : const OUString& sPathToNode)
106 : {
107 : return GetConfigurationNode(
108 : Reference<container::XHierarchicalNameAccess>(mxRoot, UNO_QUERY),
109 0 : sPathToNode);
110 : }
111 :
112 :
113 :
114 :
115 0 : Any ConfigurationAccess::GetConfigurationNode (
116 : const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
117 : const OUString& sPathToNode)
118 : {
119 0 : if (sPathToNode.isEmpty())
120 0 : return Any(rxNode);
121 :
122 : try
123 : {
124 0 : if (rxNode.is())
125 : {
126 0 : return rxNode->getByHierarchicalName(sPathToNode);
127 : }
128 : }
129 0 : catch (const Exception& rException)
130 : {
131 : OSL_TRACE ("caught exception while getting configuration node %s: %s",
132 : OUStringToOString(sPathToNode, RTL_TEXTENCODING_UTF8).getStr(),
133 : OUStringToOString(rException.Message, RTL_TEXTENCODING_UTF8).getStr());
134 : }
135 :
136 0 : return Any();
137 : }
138 :
139 :
140 :
141 :
142 0 : void ConfigurationAccess::CommitChanges (void)
143 : {
144 0 : Reference<util::XChangesBatch> xConfiguration (mxRoot, UNO_QUERY);
145 0 : if (xConfiguration.is())
146 0 : xConfiguration->commitChanges();
147 0 : }
148 :
149 :
150 :
151 :
152 0 : void ConfigurationAccess::ForAll (
153 : const Reference<container::XNameAccess>& rxContainer,
154 : const ::std::vector<OUString>& rArguments,
155 : const Functor& rFunctor)
156 : {
157 0 : if (rxContainer.is())
158 : {
159 0 : ::std::vector<Any> aValues(rArguments.size());
160 0 : Sequence<OUString> aKeys (rxContainer->getElementNames());
161 0 : for (sal_Int32 nItemIndex=0; nItemIndex < aKeys.getLength(); ++nItemIndex)
162 : {
163 0 : const OUString& rsKey (aKeys[nItemIndex]);
164 0 : Reference<container::XNameAccess> xSetItem (rxContainer->getByName(rsKey), UNO_QUERY);
165 0 : if (xSetItem.is())
166 : {
167 : // Get from the current item of the container the children
168 : // that match the names in the rArguments list.
169 0 : for (sal_uInt32 nValueIndex=0; nValueIndex<aValues.size(); ++nValueIndex)
170 0 : aValues[nValueIndex] = xSetItem->getByName(rArguments[nValueIndex]);
171 : }
172 0 : rFunctor(rsKey, aValues);
173 0 : }
174 : }
175 0 : }
176 :
177 :
178 :
179 :
180 0 : void ConfigurationAccess::FillList(
181 : const Reference<container::XNameAccess>& rxContainer,
182 : const OUString& rsArgument,
183 : ::std::vector<OUString>& rList)
184 : {
185 : try
186 : {
187 0 : if (rxContainer.is())
188 : {
189 0 : Sequence<OUString> aKeys (rxContainer->getElementNames());
190 0 : rList.resize(aKeys.getLength());
191 0 : for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex)
192 : {
193 : Reference<container::XNameAccess> xSetItem (
194 0 : rxContainer->getByName(aKeys[nItemIndex]), UNO_QUERY);
195 0 : if (xSetItem.is())
196 : {
197 0 : xSetItem->getByName(rsArgument) >>= rList[nItemIndex];
198 : }
199 0 : }
200 : }
201 : }
202 0 : catch (RuntimeException&)
203 : {}
204 0 : }
205 :
206 :
207 : } } // end of namespace sd::tools
208 :
209 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|