1*5b190011SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*5b190011SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*5b190011SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*5b190011SAndrew Rist  * distributed with this work for additional information
6*5b190011SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*5b190011SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*5b190011SAndrew Rist  * "License"); you may not use this file except in compliance
9*5b190011SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*5b190011SAndrew Rist  *
11*5b190011SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*5b190011SAndrew Rist  *
13*5b190011SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*5b190011SAndrew Rist  * software distributed under the License is distributed on an
15*5b190011SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5b190011SAndrew Rist  * KIND, either express or implied.  See the License for the
17*5b190011SAndrew Rist  * specific language governing permissions and limitations
18*5b190011SAndrew Rist  * under the License.
19*5b190011SAndrew Rist  *
20*5b190011SAndrew Rist  *************************************************************/
21*5b190011SAndrew Rist 
22*5b190011SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "precompiled_sd.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "tools/ConfigurationAccess.hxx"
27cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
29cdf0e10cSrcweir #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
30cdf0e10cSrcweir #include <com/sun/star/util/XChangesBatch.hpp>
31cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
32cdf0e10cSrcweir #include <tools/diagnose_ex.h>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir using namespace ::com::sun::star;
35cdf0e10cSrcweir using namespace ::com::sun::star::uno;
36cdf0e10cSrcweir using ::rtl::OUString;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir namespace sd { namespace tools {
39cdf0e10cSrcweir 
ConfigurationAccess(const Reference<XComponentContext> & rxContext,const OUString & rsRootName,const WriteMode eMode)40cdf0e10cSrcweir ConfigurationAccess::ConfigurationAccess (
41cdf0e10cSrcweir     const Reference<XComponentContext>& rxContext,
42cdf0e10cSrcweir     const OUString& rsRootName,
43cdf0e10cSrcweir     const WriteMode eMode)
44cdf0e10cSrcweir     : mxRoot()
45cdf0e10cSrcweir {
46cdf0e10cSrcweir     Reference<lang::XMultiComponentFactory> xFactory (rxContext->getServiceManager());
47cdf0e10cSrcweir     if (xFactory.is())
48cdf0e10cSrcweir     {
49cdf0e10cSrcweir         Reference<lang::XMultiServiceFactory> xProvider (
50cdf0e10cSrcweir             xFactory->createInstanceWithContext(
51cdf0e10cSrcweir                 OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider"),
52cdf0e10cSrcweir                 rxContext),
53cdf0e10cSrcweir             UNO_QUERY);
54cdf0e10cSrcweir         if (xProvider.is())
55cdf0e10cSrcweir             Initialize(xProvider, rsRootName, eMode);
56cdf0e10cSrcweir     }
57cdf0e10cSrcweir }
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 
ConfigurationAccess(const OUString & rsRootName,const WriteMode eMode)62cdf0e10cSrcweir ConfigurationAccess::ConfigurationAccess (
63cdf0e10cSrcweir     const OUString& rsRootName,
64cdf0e10cSrcweir     const WriteMode eMode)
65cdf0e10cSrcweir     : mxRoot()
66cdf0e10cSrcweir {
67cdf0e10cSrcweir     Reference<lang::XMultiServiceFactory> xProvider (
68cdf0e10cSrcweir         ::comphelper::getProcessServiceFactory()->createInstance(
69cdf0e10cSrcweir             OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider")),
70cdf0e10cSrcweir         UNO_QUERY);
71cdf0e10cSrcweir     if (xProvider.is())
72cdf0e10cSrcweir         Initialize(xProvider, rsRootName, eMode);
73cdf0e10cSrcweir }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 
Initialize(const Reference<lang::XMultiServiceFactory> & rxProvider,const OUString & rsRootName,const WriteMode eMode)78cdf0e10cSrcweir void ConfigurationAccess::Initialize (
79cdf0e10cSrcweir     const Reference<lang::XMultiServiceFactory>& rxProvider,
80cdf0e10cSrcweir     const OUString& rsRootName,
81cdf0e10cSrcweir     const WriteMode eMode)
82cdf0e10cSrcweir {
83cdf0e10cSrcweir     try
84cdf0e10cSrcweir     {
85cdf0e10cSrcweir         Sequence<Any> aCreationArguments(3);
86cdf0e10cSrcweir         aCreationArguments[0] = makeAny(beans::PropertyValue(
87cdf0e10cSrcweir             OUString(RTL_CONSTASCII_USTRINGPARAM("nodepath")),
88cdf0e10cSrcweir             0,
89cdf0e10cSrcweir             makeAny(rsRootName),
90cdf0e10cSrcweir             beans::PropertyState_DIRECT_VALUE));
91cdf0e10cSrcweir         aCreationArguments[1] = makeAny(beans::PropertyValue(
92cdf0e10cSrcweir             OUString(RTL_CONSTASCII_USTRINGPARAM("depth")),
93cdf0e10cSrcweir             0,
94cdf0e10cSrcweir             makeAny((sal_Int32)-1),
95cdf0e10cSrcweir             beans::PropertyState_DIRECT_VALUE));
96cdf0e10cSrcweir         aCreationArguments[2] = makeAny(beans::PropertyValue(
97cdf0e10cSrcweir             OUString(RTL_CONSTASCII_USTRINGPARAM("lazywrite")),
98cdf0e10cSrcweir             0,
99cdf0e10cSrcweir             makeAny(true),
100cdf0e10cSrcweir             beans::PropertyState_DIRECT_VALUE));
101cdf0e10cSrcweir         OUString sAccessService;
102cdf0e10cSrcweir         if (eMode == READ_ONLY)
103cdf0e10cSrcweir             sAccessService = OUString(RTL_CONSTASCII_USTRINGPARAM(
104cdf0e10cSrcweir                 "com.sun.star.configuration.ConfigurationAccess"));
105cdf0e10cSrcweir         else
106cdf0e10cSrcweir             sAccessService = OUString(RTL_CONSTASCII_USTRINGPARAM(
107cdf0e10cSrcweir                 "com.sun.star.configuration.ConfigurationUpdateAccess"));
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         mxRoot = rxProvider->createInstanceWithArguments(
110cdf0e10cSrcweir             sAccessService,
111cdf0e10cSrcweir             aCreationArguments);
112cdf0e10cSrcweir     }
113cdf0e10cSrcweir     catch (Exception&)
114cdf0e10cSrcweir     {
115cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
116cdf0e10cSrcweir     }
117cdf0e10cSrcweir }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 
GetConfigurationNode(const OUString & sPathToNode)122cdf0e10cSrcweir Any ConfigurationAccess::GetConfigurationNode (
123cdf0e10cSrcweir     const OUString& sPathToNode)
124cdf0e10cSrcweir {
125cdf0e10cSrcweir     return GetConfigurationNode(
126cdf0e10cSrcweir         Reference<container::XHierarchicalNameAccess>(mxRoot, UNO_QUERY),
127cdf0e10cSrcweir         sPathToNode);
128cdf0e10cSrcweir }
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 
GetConfigurationNode(const css::uno::Reference<css::container::XHierarchicalNameAccess> & rxNode,const OUString & sPathToNode)133cdf0e10cSrcweir Any ConfigurationAccess::GetConfigurationNode (
134cdf0e10cSrcweir     const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
135cdf0e10cSrcweir     const OUString& sPathToNode)
136cdf0e10cSrcweir {
137cdf0e10cSrcweir     if (sPathToNode.getLength() == 0)
138cdf0e10cSrcweir         return Any(rxNode);
139cdf0e10cSrcweir 
140cdf0e10cSrcweir     try
141cdf0e10cSrcweir     {
142cdf0e10cSrcweir         if (rxNode.is())
143cdf0e10cSrcweir         {
144cdf0e10cSrcweir             return rxNode->getByHierarchicalName(sPathToNode);
145cdf0e10cSrcweir         }
146cdf0e10cSrcweir     }
147cdf0e10cSrcweir     catch (Exception& rException)
148cdf0e10cSrcweir     {
149cdf0e10cSrcweir         OSL_TRACE ("caught exception while getting configuration node %s: %s",
150cdf0e10cSrcweir             ::rtl::OUStringToOString(sPathToNode, RTL_TEXTENCODING_UTF8).getStr(),
151cdf0e10cSrcweir             ::rtl::OUStringToOString(rException.Message, RTL_TEXTENCODING_UTF8).getStr());
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir     return Any();
155cdf0e10cSrcweir }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 
CommitChanges(void)160cdf0e10cSrcweir void ConfigurationAccess::CommitChanges (void)
161cdf0e10cSrcweir {
162cdf0e10cSrcweir     Reference<util::XChangesBatch> xConfiguration (mxRoot, UNO_QUERY);
163cdf0e10cSrcweir     if (xConfiguration.is())
164cdf0e10cSrcweir         xConfiguration->commitChanges();
165cdf0e10cSrcweir }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 
ForAll(const Reference<container::XNameAccess> & rxContainer,const::std::vector<OUString> & rArguments,const Functor & rFunctor)170cdf0e10cSrcweir void ConfigurationAccess::ForAll (
171cdf0e10cSrcweir     const Reference<container::XNameAccess>& rxContainer,
172cdf0e10cSrcweir     const ::std::vector<OUString>& rArguments,
173cdf0e10cSrcweir     const Functor& rFunctor)
174cdf0e10cSrcweir {
175cdf0e10cSrcweir     if (rxContainer.is())
176cdf0e10cSrcweir     {
177cdf0e10cSrcweir         ::std::vector<Any> aValues(rArguments.size());
178cdf0e10cSrcweir         Sequence<OUString> aKeys (rxContainer->getElementNames());
179cdf0e10cSrcweir         for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex)
180cdf0e10cSrcweir         {
181cdf0e10cSrcweir             const OUString& rsKey (aKeys[nItemIndex]);
182cdf0e10cSrcweir             Reference<container::XNameAccess> xSetItem (rxContainer->getByName(rsKey), UNO_QUERY);
183cdf0e10cSrcweir             if (xSetItem.is())
184cdf0e10cSrcweir             {
185cdf0e10cSrcweir                 // Get from the current item of the container the children
186cdf0e10cSrcweir                 // that match the names in the rArguments list.
187cdf0e10cSrcweir                 for (sal_uInt32 nValueIndex=0; nValueIndex<aValues.size(); ++nValueIndex)
188cdf0e10cSrcweir                     aValues[nValueIndex] = xSetItem->getByName(rArguments[nValueIndex]);
189cdf0e10cSrcweir             }
190cdf0e10cSrcweir             rFunctor(rsKey, aValues);
191cdf0e10cSrcweir         }
192cdf0e10cSrcweir     }
193cdf0e10cSrcweir }
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 
FillList(const Reference<container::XNameAccess> & rxContainer,const::rtl::OUString & rsArgument,::std::vector<OUString> & rList)198cdf0e10cSrcweir void ConfigurationAccess::FillList(
199cdf0e10cSrcweir     const Reference<container::XNameAccess>& rxContainer,
200cdf0e10cSrcweir     const ::rtl::OUString& rsArgument,
201cdf0e10cSrcweir     ::std::vector<OUString>& rList)
202cdf0e10cSrcweir {
203cdf0e10cSrcweir     try
204cdf0e10cSrcweir     {
205cdf0e10cSrcweir         if (rxContainer.is())
206cdf0e10cSrcweir         {
207cdf0e10cSrcweir             Sequence<OUString> aKeys (rxContainer->getElementNames());
208cdf0e10cSrcweir             rList.resize(aKeys.getLength());
209cdf0e10cSrcweir             for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex)
210cdf0e10cSrcweir             {
211cdf0e10cSrcweir                 Reference<container::XNameAccess> xSetItem (
212cdf0e10cSrcweir                     rxContainer->getByName(aKeys[nItemIndex]), UNO_QUERY);
213cdf0e10cSrcweir                 if (xSetItem.is())
214cdf0e10cSrcweir                 {
215cdf0e10cSrcweir                     xSetItem->getByName(rsArgument) >>= rList[nItemIndex];
216cdf0e10cSrcweir                 }
217cdf0e10cSrcweir             }
218cdf0e10cSrcweir         }
219cdf0e10cSrcweir     }
220cdf0e10cSrcweir     catch (RuntimeException&)
221cdf0e10cSrcweir     {}
222cdf0e10cSrcweir }
223cdf0e10cSrcweir 
224cdf0e10cSrcweir 
225cdf0e10cSrcweir } } // end of namespace sd::tools
226cdf0e10cSrcweir 
227