1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/container/XNameAccess.hpp>
26 #include <com/sun/star/util/XChangesBatch.hpp>
27 #include <comphelper/processfactory.hxx>
28 #include <comphelper/propertysequence.hxx>
29 #include <tools/diagnose_ex.h>
30 #include <sal/log.hxx>
32 using namespace ::com::sun::star
;
33 using namespace ::com::sun::star::uno
;
35 namespace sd
{ namespace tools
{
37 ConfigurationAccess::ConfigurationAccess (
38 const Reference
<XComponentContext
>& rxContext
,
39 const OUString
& rsRootName
,
40 const WriteMode eMode
)
43 Reference
<lang::XMultiServiceFactory
> xProvider
=
44 configuration::theDefaultProvider::get( rxContext
);
45 Initialize(xProvider
, rsRootName
, eMode
);
48 ConfigurationAccess::ConfigurationAccess (
49 const OUString
& rsRootName
,
50 const WriteMode eMode
)
53 Reference
<lang::XMultiServiceFactory
> xProvider
=
54 configuration::theDefaultProvider::get( ::comphelper::getProcessComponentContext() );
55 Initialize(xProvider
, rsRootName
, eMode
);
58 void ConfigurationAccess::Initialize (
59 const Reference
<lang::XMultiServiceFactory
>& rxProvider
,
60 const OUString
& rsRootName
,
61 const WriteMode eMode
)
65 Sequence
<Any
> aCreationArguments(comphelper::InitAnyPropertySequence(
67 {"nodepath", makeAny(rsRootName
)},
68 {"depth", makeAny(sal_Int32(-1))}
71 OUString sAccessService
;
72 if (eMode
== READ_ONLY
)
73 sAccessService
= "com.sun.star.configuration.ConfigurationAccess";
75 sAccessService
= "com.sun.star.configuration.ConfigurationUpdateAccess";
77 mxRoot
= rxProvider
->createInstanceWithArguments(
83 DBG_UNHANDLED_EXCEPTION("sd.tools");
87 Any
ConfigurationAccess::GetConfigurationNode (
88 const OUString
& sPathToNode
)
90 return GetConfigurationNode(
91 Reference
<container::XHierarchicalNameAccess
>(mxRoot
, UNO_QUERY
),
95 Any
ConfigurationAccess::GetConfigurationNode (
96 const css::uno::Reference
<css::container::XHierarchicalNameAccess
>& rxNode
,
97 const OUString
& sPathToNode
)
99 if (sPathToNode
.isEmpty())
106 return rxNode
->getByHierarchicalName(sPathToNode
);
109 catch (const Exception
&)
111 css::uno::Any
ex( cppu::getCaughtException() );
112 SAL_WARN("sd", "caught exception while getting configuration node" << sPathToNode
<< ": " << exceptionToString(ex
));
118 void ConfigurationAccess::CommitChanges()
120 Reference
<util::XChangesBatch
> xConfiguration (mxRoot
, UNO_QUERY
);
121 if (xConfiguration
.is())
122 xConfiguration
->commitChanges();
125 void ConfigurationAccess::ForAll (
126 const Reference
<container::XNameAccess
>& rxContainer
,
127 const ::std::vector
<OUString
>& rArguments
,
128 const Functor
& rFunctor
)
130 if (!rxContainer
.is())
133 ::std::vector
<Any
> aValues(rArguments
.size());
134 Sequence
<OUString
> aKeys (rxContainer
->getElementNames());
135 for (sal_Int32 nItemIndex
=0; nItemIndex
< aKeys
.getLength(); ++nItemIndex
)
137 const OUString
& rsKey (aKeys
[nItemIndex
]);
138 Reference
<container::XNameAccess
> xSetItem (rxContainer
->getByName(rsKey
), UNO_QUERY
);
141 // Get from the current item of the container the children
142 // that match the names in the rArguments list.
143 for (size_t nValueIndex
=0; nValueIndex
<aValues
.size(); ++nValueIndex
)
144 aValues
[nValueIndex
] = xSetItem
->getByName(rArguments
[nValueIndex
]);
146 rFunctor(rsKey
, aValues
);
150 void ConfigurationAccess::FillList(
151 const Reference
<container::XNameAccess
>& rxContainer
,
152 const OUString
& rsArgument
,
153 ::std::vector
<OUString
>& rList
)
157 if (rxContainer
.is())
159 Sequence
<OUString
> aKeys (rxContainer
->getElementNames());
160 rList
.resize(aKeys
.getLength());
161 for (sal_Int32 nItemIndex
=0; nItemIndex
<aKeys
.getLength(); ++nItemIndex
)
163 Reference
<container::XNameAccess
> xSetItem (
164 rxContainer
->getByName(aKeys
[nItemIndex
]), UNO_QUERY
);
167 xSetItem
->getByName(rsArgument
) >>= rList
[nItemIndex
];
172 catch (RuntimeException
&)
176 } } // end of namespace sd::tools
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */