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