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 <comphelper/configurationhelper.hxx>
21 #include <comphelper/processfactory.hxx>
22 #include <comphelper/sequence.hxx>
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <com/sun/star/configuration/theDefaultProvider.hpp>
25 #include <com/sun/star/container/XNameAccess.hpp>
26 #include <com/sun/star/container/XNameContainer.hpp>
27 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
33 css::uno::Reference
< css::uno::XInterface
> ConfigurationHelper::openConfig(const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
,
34 const OUString
& sPackage
,
37 css::uno::Reference
< css::lang::XMultiServiceFactory
> xConfigProvider(
38 css::configuration::theDefaultProvider::get( rxContext
) );
40 ::std::vector
< css::uno::Any
> lParams
;
41 css::beans::PropertyValue aParam
;
44 aParam
.Name
= "nodepath";
45 aParam
.Value
<<= sPackage
;
46 lParams
.push_back(css::uno::makeAny(aParam
));
48 // enable all locales mode
49 if ((eMode
& ConfigurationHelper::E_ALL_LOCALES
)==ConfigurationHelper::E_ALL_LOCALES
)
51 aParam
.Name
= "locale";
52 aParam
.Value
<<= OUString("*");
53 lParams
.push_back(css::uno::makeAny(aParam
));
56 // enable lazy writing
57 bool bLazy
= ((eMode
& ConfigurationHelper::E_LAZY_WRITE
)==ConfigurationHelper::E_LAZY_WRITE
);
58 aParam
.Name
= "lazywrite";
59 aParam
.Value
= css::uno::makeAny(bLazy
);
60 lParams
.push_back(css::uno::makeAny(aParam
));
63 css::uno::Reference
< css::uno::XInterface
> xCFG
;
65 bool bReadOnly
= ((eMode
& ConfigurationHelper::E_READONLY
)==ConfigurationHelper::E_READONLY
);
67 xCFG
= xConfigProvider
->createInstanceWithArguments(
68 OUString("com.sun.star.configuration.ConfigurationAccess"),
69 comphelper::containerToSequence(lParams
));
71 xCFG
= xConfigProvider
->createInstanceWithArguments(
72 OUString("com.sun.star.configuration.ConfigurationUpdateAccess"),
73 comphelper::containerToSequence(lParams
));
79 css::uno::Any
ConfigurationHelper::readRelativeKey(const css::uno::Reference
< css::uno::XInterface
> xCFG
,
80 const OUString
& sRelPath
,
81 const OUString
& sKey
)
83 css::uno::Reference
< css::container::XHierarchicalNameAccess
> xAccess(xCFG
, css::uno::UNO_QUERY_THROW
);
85 css::uno::Reference
< css::beans::XPropertySet
> xProps
;
86 xAccess
->getByHierarchicalName(sRelPath
) >>= xProps
;
89 OUStringBuffer
sMsg(256);
90 sMsg
.appendAscii("The requested path \"");
91 sMsg
.append (sRelPath
);
92 sMsg
.appendAscii("\" does not exists." );
94 throw css::container::NoSuchElementException(
95 sMsg
.makeStringAndClear());
97 return xProps
->getPropertyValue(sKey
);
101 void ConfigurationHelper::writeRelativeKey(const css::uno::Reference
< css::uno::XInterface
> xCFG
,
102 const OUString
& sRelPath
,
103 const OUString
& sKey
,
104 const css::uno::Any
& aValue
)
106 css::uno::Reference
< css::container::XHierarchicalNameAccess
> xAccess(xCFG
, css::uno::UNO_QUERY_THROW
);
108 css::uno::Reference
< css::beans::XPropertySet
> xProps
;
109 xAccess
->getByHierarchicalName(sRelPath
) >>= xProps
;
112 OUStringBuffer
sMsg(256);
113 sMsg
.appendAscii("The requested path \"");
114 sMsg
.append (sRelPath
);
115 sMsg
.appendAscii("\" does not exists." );
117 throw css::container::NoSuchElementException(
118 sMsg
.makeStringAndClear());
120 xProps
->setPropertyValue(sKey
, aValue
);
124 css::uno::Reference
< css::uno::XInterface
> ConfigurationHelper::makeSureSetNodeExists(const css::uno::Reference
< css::uno::XInterface
> xCFG
,
125 const OUString
& sRelPathToSet
,
126 const OUString
& sSetNode
)
128 css::uno::Reference
< css::container::XHierarchicalNameAccess
> xAccess(xCFG
, css::uno::UNO_QUERY_THROW
);
129 css::uno::Reference
< css::container::XNameAccess
> xSet
;
130 xAccess
->getByHierarchicalName(sRelPathToSet
) >>= xSet
;
133 OUStringBuffer
sMsg(256);
134 sMsg
.appendAscii("The requested path \"");
135 sMsg
.append (sRelPathToSet
);
136 sMsg
.appendAscii("\" does not exists." );
138 throw css::container::NoSuchElementException(
139 sMsg
.makeStringAndClear());
142 css::uno::Reference
< css::uno::XInterface
> xNode
;
143 if (xSet
->hasByName(sSetNode
))
144 xSet
->getByName(sSetNode
) >>= xNode
;
147 css::uno::Reference
< css::lang::XSingleServiceFactory
> xNodeFactory(xSet
, css::uno::UNO_QUERY_THROW
);
148 xNode
= xNodeFactory
->createInstance();
149 css::uno::Reference
< css::container::XNameContainer
> xSetReplace(xSet
, css::uno::UNO_QUERY_THROW
);
150 xSetReplace
->insertByName(sSetNode
, css::uno::makeAny(xNode
));
157 css::uno::Any
ConfigurationHelper::readDirectKey(const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
,
158 const OUString
& sPackage
,
159 const OUString
& sRelPath
,
160 const OUString
& sKey
,
163 css::uno::Reference
< css::uno::XInterface
> xCFG
= ConfigurationHelper::openConfig(rxContext
, sPackage
, eMode
);
164 return ConfigurationHelper::readRelativeKey(xCFG
, sRelPath
, sKey
);
168 void ConfigurationHelper::writeDirectKey(const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
,
169 const OUString
& sPackage
,
170 const OUString
& sRelPath
,
171 const OUString
& sKey
,
172 const css::uno::Any
& aValue
,
175 css::uno::Reference
< css::uno::XInterface
> xCFG
= ConfigurationHelper::openConfig(rxContext
, sPackage
, eMode
);
176 ConfigurationHelper::writeRelativeKey(xCFG
, sRelPath
, sKey
, aValue
);
177 ConfigurationHelper::flush(xCFG
);
181 void ConfigurationHelper::flush(const css::uno::Reference
< css::uno::XInterface
>& xCFG
)
183 css::uno::Reference
< css::util::XChangesBatch
> xBatch(xCFG
, css::uno::UNO_QUERY_THROW
);
184 xBatch
->commitChanges();
187 } // namespace comphelper
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */