Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sd / source / ui / tools / ConfigurationAccess.cxx
blob1492db1a2de356a22d708fb9cc45cfdecf0e4fbb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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 <comphelper/propertysequence.hxx>
28 #include <tools/diagnose_ex.h>
30 using namespace ::com::sun::star;
31 using namespace ::com::sun::star::uno;
33 namespace sd { namespace tools {
35 ConfigurationAccess::ConfigurationAccess (
36 const Reference<XComponentContext>& rxContext,
37 const OUString& rsRootName,
38 const WriteMode eMode)
39 : mxRoot()
41 Reference<lang::XMultiServiceFactory> xProvider =
42 configuration::theDefaultProvider::get( rxContext );
43 Initialize(xProvider, rsRootName, eMode);
46 ConfigurationAccess::ConfigurationAccess (
47 const OUString& rsRootName,
48 const WriteMode eMode)
49 : mxRoot()
51 Reference<lang::XMultiServiceFactory> xProvider =
52 configuration::theDefaultProvider::get( ::comphelper::getProcessComponentContext() );
53 Initialize(xProvider, rsRootName, eMode);
56 void ConfigurationAccess::Initialize (
57 const Reference<lang::XMultiServiceFactory>& rxProvider,
58 const OUString& rsRootName,
59 const WriteMode eMode)
61 try
63 Sequence<Any> aCreationArguments(comphelper::InitAnyPropertySequence(
65 {"nodepath", makeAny(rsRootName)},
66 {"depth", makeAny(sal_Int32(-1))}
67 }));
69 OUString sAccessService;
70 if (eMode == READ_ONLY)
71 sAccessService = "com.sun.star.configuration.ConfigurationAccess";
72 else
73 sAccessService = "com.sun.star.configuration.ConfigurationUpdateAccess";
75 mxRoot = rxProvider->createInstanceWithArguments(
76 sAccessService,
77 aCreationArguments);
79 catch (Exception&)
81 DBG_UNHANDLED_EXCEPTION("sd.tools");
85 Any ConfigurationAccess::GetConfigurationNode (
86 const OUString& sPathToNode)
88 return GetConfigurationNode(
89 Reference<container::XHierarchicalNameAccess>(mxRoot, UNO_QUERY),
90 sPathToNode);
93 Any ConfigurationAccess::GetConfigurationNode (
94 const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
95 const OUString& sPathToNode)
97 if (sPathToNode.isEmpty())
98 return Any(rxNode);
102 if (rxNode.is())
104 return rxNode->getByHierarchicalName(sPathToNode);
107 catch (const Exception& rException)
109 SAL_WARN("sd", "caught exception while getting configuration node" << sPathToNode << ": " << rException);
112 return Any();
115 void ConfigurationAccess::CommitChanges()
117 Reference<util::XChangesBatch> xConfiguration (mxRoot, UNO_QUERY);
118 if (xConfiguration.is())
119 xConfiguration->commitChanges();
122 void ConfigurationAccess::ForAll (
123 const Reference<container::XNameAccess>& rxContainer,
124 const ::std::vector<OUString>& rArguments,
125 const Functor& rFunctor)
127 if (rxContainer.is())
129 ::std::vector<Any> aValues(rArguments.size());
130 Sequence<OUString> aKeys (rxContainer->getElementNames());
131 for (sal_Int32 nItemIndex=0; nItemIndex < aKeys.getLength(); ++nItemIndex)
133 const OUString& rsKey (aKeys[nItemIndex]);
134 Reference<container::XNameAccess> xSetItem (rxContainer->getByName(rsKey), UNO_QUERY);
135 if (xSetItem.is())
137 // Get from the current item of the container the children
138 // that match the names in the rArguments list.
139 for (size_t nValueIndex=0; nValueIndex<aValues.size(); ++nValueIndex)
140 aValues[nValueIndex] = xSetItem->getByName(rArguments[nValueIndex]);
142 rFunctor(rsKey, aValues);
147 void ConfigurationAccess::FillList(
148 const Reference<container::XNameAccess>& rxContainer,
149 const OUString& rsArgument,
150 ::std::vector<OUString>& rList)
154 if (rxContainer.is())
156 Sequence<OUString> aKeys (rxContainer->getElementNames());
157 rList.resize(aKeys.getLength());
158 for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex)
160 Reference<container::XNameAccess> xSetItem (
161 rxContainer->getByName(aKeys[nItemIndex]), UNO_QUERY);
162 if (xSetItem.is())
164 xSetItem->getByName(rsArgument) >>= rList[nItemIndex];
169 catch (RuntimeException&)
173 } } // end of namespace sd::tools
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */