Update ooo320-m1
[ooovba.git] / sd / source / ui / tools / ConfigurationAccess.cxx
blob7d794e8a1b288af899725c1321b12187962607a3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ConfigurationAccess.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "precompiled_sd.hxx"
33 #include "tools/ConfigurationAccess.hxx"
34 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35 #include <com/sun/star/beans/PropertyValue.hpp>
36 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
37 #include <com/sun/star/util/XChangesBatch.hpp>
38 #include <comphelper/processfactory.hxx>
40 using namespace ::com::sun::star;
41 using namespace ::com::sun::star::uno;
42 using ::rtl::OUString;
44 namespace sd { namespace tools {
46 ConfigurationAccess::ConfigurationAccess (
47 const Reference<XComponentContext>& rxContext,
48 const OUString& rsRootName,
49 const WriteMode eMode)
50 : mxRoot()
52 Reference<lang::XMultiComponentFactory> xFactory (rxContext->getServiceManager());
53 if (xFactory.is())
55 Reference<lang::XMultiServiceFactory> xProvider (
56 xFactory->createInstanceWithContext(
57 OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider"),
58 rxContext),
59 UNO_QUERY);
60 if (xProvider.is())
61 Initialize(xProvider, rsRootName, eMode);
68 ConfigurationAccess::ConfigurationAccess (
69 const OUString& rsRootName,
70 const WriteMode eMode)
71 : mxRoot()
73 Reference<lang::XMultiServiceFactory> xProvider (
74 ::comphelper::getProcessServiceFactory()->createInstance(
75 OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider")),
76 UNO_QUERY);
77 if (xProvider.is())
78 Initialize(xProvider, rsRootName, eMode);
84 void ConfigurationAccess::Initialize (
85 const Reference<lang::XMultiServiceFactory>& rxProvider,
86 const OUString& rsRootName,
87 const WriteMode eMode)
89 try
91 Sequence<Any> aCreationArguments(3);
92 aCreationArguments[0] = makeAny(beans::PropertyValue(
93 OUString(RTL_CONSTASCII_USTRINGPARAM("nodepath")),
94 0,
95 makeAny(rsRootName),
96 beans::PropertyState_DIRECT_VALUE));
97 aCreationArguments[1] = makeAny(beans::PropertyValue(
98 OUString(RTL_CONSTASCII_USTRINGPARAM("depth")),
99 0,
100 makeAny((sal_Int32)-1),
101 beans::PropertyState_DIRECT_VALUE));
102 aCreationArguments[2] = makeAny(beans::PropertyValue(
103 OUString(RTL_CONSTASCII_USTRINGPARAM("lazywrite")),
105 makeAny(true),
106 beans::PropertyState_DIRECT_VALUE));
107 OUString sAccessService;
108 if (eMode == READ_ONLY)
109 sAccessService = OUString(RTL_CONSTASCII_USTRINGPARAM(
110 "com.sun.star.configuration.ConfigurationAccess"));
111 else
112 sAccessService = OUString(RTL_CONSTASCII_USTRINGPARAM(
113 "com.sun.star.configuration.ConfigurationUpdateAccess"));
115 mxRoot = rxProvider->createInstanceWithArguments(
116 sAccessService,
117 aCreationArguments);
119 catch (Exception& rException)
121 OSL_TRACE ("caught exception while opening configuration: %s",
122 ::rtl::OUStringToOString(rException.Message,
123 RTL_TEXTENCODING_UTF8).getStr());
130 Any ConfigurationAccess::GetConfigurationNode (
131 const OUString& sPathToNode)
133 return GetConfigurationNode(
134 Reference<container::XHierarchicalNameAccess>(mxRoot, UNO_QUERY),
135 sPathToNode);
141 Any ConfigurationAccess::GetConfigurationNode (
142 const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
143 const OUString& sPathToNode)
145 if (sPathToNode.getLength() == 0)
146 return Any(rxNode);
150 if (rxNode.is())
152 return rxNode->getByHierarchicalName(sPathToNode);
155 catch (Exception& rException)
157 OSL_TRACE ("caught exception while getting configuration node %s: %s",
158 ::rtl::OUStringToOString(sPathToNode, RTL_TEXTENCODING_UTF8).getStr(),
159 ::rtl::OUStringToOString(rException.Message, RTL_TEXTENCODING_UTF8).getStr());
162 return Any();
168 void ConfigurationAccess::CommitChanges (void)
170 Reference<util::XChangesBatch> xConfiguration (mxRoot, UNO_QUERY);
171 if (xConfiguration.is())
172 xConfiguration->commitChanges();
178 void ConfigurationAccess::ForAll (
179 const Reference<container::XNameAccess>& rxContainer,
180 const ::std::vector<OUString>& rArguments,
181 const Functor& rFunctor)
183 if (rxContainer.is())
185 ::std::vector<Any> aValues(rArguments.size());
186 Sequence<OUString> aKeys (rxContainer->getElementNames());
187 for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex)
189 const OUString& rsKey (aKeys[nItemIndex]);
190 Reference<container::XNameAccess> xSetItem (rxContainer->getByName(rsKey), UNO_QUERY);
191 if (xSetItem.is())
193 // Get from the current item of the container the children
194 // that match the names in the rArguments list.
195 for (sal_uInt32 nValueIndex=0; nValueIndex<aValues.size(); ++nValueIndex)
196 aValues[nValueIndex] = xSetItem->getByName(rArguments[nValueIndex]);
198 rFunctor(rsKey, aValues);
206 void ConfigurationAccess::FillList(
207 const Reference<container::XNameAccess>& rxContainer,
208 const ::rtl::OUString& rsArgument,
209 ::std::vector<OUString>& rList)
213 if (rxContainer.is())
215 Sequence<OUString> aKeys (rxContainer->getElementNames());
216 rList.resize(aKeys.getLength());
217 for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex)
219 Reference<container::XNameAccess> xSetItem (
220 rxContainer->getByName(aKeys[nItemIndex]), UNO_QUERY);
221 if (xSetItem.is())
223 xSetItem->getByName(rsArgument) >>= rList[nItemIndex];
228 catch (RuntimeException&)
233 } } // end of namespace sd::tools