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 212 : ConfigurationAccess::ConfigurationAccess (
35 : const Reference<XComponentContext>& rxContext,
36 : const OUString& rsRootName,
37 : const WriteMode eMode)
38 212 : : mxRoot()
39 : {
40 : Reference<lang::XMultiServiceFactory> xProvider =
41 212 : configuration::theDefaultProvider::get( rxContext );
42 212 : Initialize(xProvider, rsRootName, eMode);
43 212 : }
44 :
45 212 : ConfigurationAccess::ConfigurationAccess (
46 : const OUString& rsRootName,
47 : const WriteMode eMode)
48 212 : : mxRoot()
49 : {
50 : Reference<lang::XMultiServiceFactory> xProvider =
51 212 : configuration::theDefaultProvider::get( ::comphelper::getProcessComponentContext() );
52 212 : Initialize(xProvider, rsRootName, eMode);
53 212 : }
54 :
55 424 : void ConfigurationAccess::Initialize (
56 : const Reference<lang::XMultiServiceFactory>& rxProvider,
57 : const OUString& rsRootName,
58 : const WriteMode eMode)
59 : {
60 : try
61 : {
62 424 : Sequence<Any> aCreationArguments(3);
63 848 : aCreationArguments[0] = makeAny(beans::PropertyValue(
64 : "nodepath",
65 : 0,
66 : makeAny(rsRootName),
67 424 : beans::PropertyState_DIRECT_VALUE));
68 848 : aCreationArguments[1] = makeAny(beans::PropertyValue(
69 : "depth",
70 : 0,
71 : makeAny((sal_Int32)-1),
72 424 : beans::PropertyState_DIRECT_VALUE));
73 848 : aCreationArguments[2] = makeAny(beans::PropertyValue(
74 : "lazywrite",
75 : 0,
76 : makeAny(true),
77 424 : beans::PropertyState_DIRECT_VALUE));
78 848 : OUString sAccessService;
79 424 : if (eMode == READ_ONLY)
80 424 : sAccessService = "com.sun.star.configuration.ConfigurationAccess";
81 : else
82 0 : sAccessService = "com.sun.star.configuration.ConfigurationUpdateAccess";
83 :
84 1272 : mxRoot = rxProvider->createInstanceWithArguments(
85 : sAccessService,
86 1272 : aCreationArguments);
87 : }
88 0 : catch (Exception&)
89 : {
90 : DBG_UNHANDLED_EXCEPTION();
91 : }
92 424 : }
93 :
94 424 : Any ConfigurationAccess::GetConfigurationNode (
95 : const OUString& sPathToNode)
96 : {
97 : return GetConfigurationNode(
98 : Reference<container::XHierarchicalNameAccess>(mxRoot, UNO_QUERY),
99 424 : sPathToNode);
100 : }
101 :
102 424 : Any ConfigurationAccess::GetConfigurationNode (
103 : const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
104 : const OUString& sPathToNode)
105 : {
106 424 : if (sPathToNode.isEmpty())
107 0 : return Any(rxNode);
108 :
109 : try
110 : {
111 424 : if (rxNode.is())
112 : {
113 424 : 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 (void)
127 : {
128 0 : Reference<util::XChangesBatch> xConfiguration (mxRoot, UNO_QUERY);
129 0 : if (xConfiguration.is())
130 0 : xConfiguration->commitChanges();
131 0 : }
132 :
133 424 : void ConfigurationAccess::ForAll (
134 : const Reference<container::XNameAccess>& rxContainer,
135 : const ::std::vector<OUString>& rArguments,
136 : const Functor& rFunctor)
137 : {
138 424 : if (rxContainer.is())
139 : {
140 424 : ::std::vector<Any> aValues(rArguments.size());
141 848 : Sequence<OUString> aKeys (rxContainer->getElementNames());
142 1484 : for (sal_Int32 nItemIndex=0; nItemIndex < aKeys.getLength(); ++nItemIndex)
143 : {
144 1060 : const OUString& rsKey (aKeys[nItemIndex]);
145 1060 : Reference<container::XNameAccess> xSetItem (rxContainer->getByName(rsKey), UNO_QUERY);
146 1060 : 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 2968 : for (sal_uInt32 nValueIndex=0; nValueIndex<aValues.size(); ++nValueIndex)
151 1908 : aValues[nValueIndex] = xSetItem->getByName(rArguments[nValueIndex]);
152 : }
153 1060 : rFunctor(rsKey, aValues);
154 1484 : }
155 : }
156 424 : }
157 :
158 848 : void ConfigurationAccess::FillList(
159 : const Reference<container::XNameAccess>& rxContainer,
160 : const OUString& rsArgument,
161 : ::std::vector<OUString>& rList)
162 : {
163 : try
164 : {
165 848 : if (rxContainer.is())
166 : {
167 848 : Sequence<OUString> aKeys (rxContainer->getElementNames());
168 848 : rList.resize(aKeys.getLength());
169 4664 : for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex)
170 : {
171 : Reference<container::XNameAccess> xSetItem (
172 3816 : rxContainer->getByName(aKeys[nItemIndex]), UNO_QUERY);
173 3816 : if (xSetItem.is())
174 : {
175 3816 : xSetItem->getByName(rsArgument) >>= rList[nItemIndex];
176 : }
177 4664 : }
178 : }
179 : }
180 0 : catch (RuntimeException&)
181 : {}
182 848 : }
183 :
184 : } } // end of namespace sd::tools
185 :
186 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|