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