1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
31 //_______________________________________________
33 #include <comphelper/configurationhelper.hxx>
34 #include <com/sun/star/beans/XPropertySet.hpp>
35 #include <com/sun/star/container/XNameAccess.hpp>
36 #include <com/sun/star/container/XNameContainer.hpp>
37 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
39 //_______________________________________________
44 namespace css
= ::com::sun::star
;
46 //_______________________________________________
49 //-----------------------------------------------
50 css::uno::Reference
< css::uno::XInterface
> ConfigurationHelper::openConfig(const css::uno::Reference
< css::lang::XMultiServiceFactory
> xSMGR
,
51 const ::rtl::OUString
& sPackage
,
54 css::uno::Reference
< css::lang::XMultiServiceFactory
> xConfigProvider(
55 xSMGR
->createInstance(::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider")), css::uno::UNO_QUERY_THROW
);
57 ::comphelper::SequenceAsVector
< css::uno::Any
> lParams
;
58 css::beans::PropertyValue aParam
;
61 aParam
.Name
= ::rtl::OUString::createFromAscii("nodepath");
62 aParam
.Value
<<= sPackage
;
63 lParams
.push_back(css::uno::makeAny(aParam
));
65 // enable all locales mode
66 if ((eMode
& ConfigurationHelper::E_ALL_LOCALES
)==ConfigurationHelper::E_ALL_LOCALES
)
68 aParam
.Name
= ::rtl::OUString::createFromAscii("locale");
69 aParam
.Value
<<= ::rtl::OUString::createFromAscii("*");
70 lParams
.push_back(css::uno::makeAny(aParam
));
73 // enable lazy writing
74 sal_Bool bLazy
= ((eMode
& ConfigurationHelper::E_LAZY_WRITE
)==ConfigurationHelper::E_LAZY_WRITE
);
75 aParam
.Name
= ::rtl::OUString::createFromAscii("lazywrite");
76 aParam
.Value
= css::uno::makeAny(bLazy
);
77 lParams
.push_back(css::uno::makeAny(aParam
));
80 css::uno::Reference
< css::uno::XInterface
> xCFG
;
82 sal_Bool bReadOnly
= ((eMode
& ConfigurationHelper::E_READONLY
)==ConfigurationHelper::E_READONLY
);
84 xCFG
= xConfigProvider
->createInstanceWithArguments(
85 ::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationAccess"),
86 lParams
.getAsConstList());
88 xCFG
= xConfigProvider
->createInstanceWithArguments(
89 ::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationUpdateAccess"),
90 lParams
.getAsConstList());
95 //-----------------------------------------------
96 css::uno::Any
ConfigurationHelper::readRelativeKey(const css::uno::Reference
< css::uno::XInterface
> xCFG
,
97 const ::rtl::OUString
& sRelPath
,
98 const ::rtl::OUString
& sKey
)
100 css::uno::Reference
< css::container::XHierarchicalNameAccess
> xAccess(xCFG
, css::uno::UNO_QUERY_THROW
);
102 css::uno::Reference
< css::beans::XPropertySet
> xProps
;
103 xAccess
->getByHierarchicalName(sRelPath
) >>= xProps
;
106 ::rtl::OUStringBuffer
sMsg(256);
107 sMsg
.appendAscii("The requested path \"");
108 sMsg
.append (sRelPath
);
109 sMsg
.appendAscii("\" does not exists." );
111 throw css::container::NoSuchElementException(
112 sMsg
.makeStringAndClear(),
113 css::uno::Reference
< css::uno::XInterface
>());
115 return xProps
->getPropertyValue(sKey
);
118 //-----------------------------------------------
119 void ConfigurationHelper::writeRelativeKey(const css::uno::Reference
< css::uno::XInterface
> xCFG
,
120 const ::rtl::OUString
& sRelPath
,
121 const ::rtl::OUString
& sKey
,
122 const css::uno::Any
& aValue
)
124 css::uno::Reference
< css::container::XHierarchicalNameAccess
> xAccess(xCFG
, css::uno::UNO_QUERY_THROW
);
126 css::uno::Reference
< css::beans::XPropertySet
> xProps
;
127 xAccess
->getByHierarchicalName(sRelPath
) >>= xProps
;
130 ::rtl::OUStringBuffer
sMsg(256);
131 sMsg
.appendAscii("The requested path \"");
132 sMsg
.append (sRelPath
);
133 sMsg
.appendAscii("\" does not exists." );
135 throw css::container::NoSuchElementException(
136 sMsg
.makeStringAndClear(),
137 css::uno::Reference
< css::uno::XInterface
>());
139 xProps
->setPropertyValue(sKey
, aValue
);
142 //-----------------------------------------------
143 css::uno::Reference
< css::uno::XInterface
> ConfigurationHelper::makeSureSetNodeExists(const css::uno::Reference
< css::uno::XInterface
> xCFG
,
144 const ::rtl::OUString
& sRelPathToSet
,
145 const ::rtl::OUString
& sSetNode
)
147 css::uno::Reference
< css::container::XHierarchicalNameAccess
> xAccess(xCFG
, css::uno::UNO_QUERY_THROW
);
148 css::uno::Reference
< css::container::XNameAccess
> xSet
;
149 xAccess
->getByHierarchicalName(sRelPathToSet
) >>= xSet
;
152 ::rtl::OUStringBuffer
sMsg(256);
153 sMsg
.appendAscii("The requested path \"");
154 sMsg
.append (sRelPathToSet
);
155 sMsg
.appendAscii("\" does not exists." );
157 throw css::container::NoSuchElementException(
158 sMsg
.makeStringAndClear(),
159 css::uno::Reference
< css::uno::XInterface
>());
162 css::uno::Reference
< css::uno::XInterface
> xNode
;
163 if (xSet
->hasByName(sSetNode
))
164 xSet
->getByName(sSetNode
) >>= xNode
;
167 css::uno::Reference
< css::lang::XSingleServiceFactory
> xNodeFactory(xSet
, css::uno::UNO_QUERY_THROW
);
168 xNode
= xNodeFactory
->createInstance();
169 css::uno::Reference
< css::container::XNameContainer
> xSetReplace(xSet
, css::uno::UNO_QUERY_THROW
);
170 xSetReplace
->insertByName(sSetNode
, css::uno::makeAny(xNode
));
176 //-----------------------------------------------
177 css::uno::Any
ConfigurationHelper::readDirectKey(const css::uno::Reference
< css::lang::XMultiServiceFactory
> xSMGR
,
178 const ::rtl::OUString
& sPackage
,
179 const ::rtl::OUString
& sRelPath
,
180 const ::rtl::OUString
& sKey
,
183 css::uno::Reference
< css::uno::XInterface
> xCFG
= ConfigurationHelper::openConfig(xSMGR
, sPackage
, eMode
);
184 return ConfigurationHelper::readRelativeKey(xCFG
, sRelPath
, sKey
);
187 //-----------------------------------------------
188 void ConfigurationHelper::writeDirectKey(const css::uno::Reference
< css::lang::XMultiServiceFactory
> xSMGR
,
189 const ::rtl::OUString
& sPackage
,
190 const ::rtl::OUString
& sRelPath
,
191 const ::rtl::OUString
& sKey
,
192 const css::uno::Any
& aValue
,
195 css::uno::Reference
< css::uno::XInterface
> xCFG
= ConfigurationHelper::openConfig(xSMGR
, sPackage
, eMode
);
196 ConfigurationHelper::writeRelativeKey(xCFG
, sRelPath
, sKey
, aValue
);
197 ConfigurationHelper::flush(xCFG
);
200 //-----------------------------------------------
201 void ConfigurationHelper::flush(const css::uno::Reference
< css::uno::XInterface
>& xCFG
)
203 css::uno::Reference
< css::util::XChangesBatch
> xBatch(xCFG
, css::uno::UNO_QUERY_THROW
);
204 xBatch
->commitChanges();
207 } // namespace comphelper