1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: configurationhelper.cxx,v $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
34 //_______________________________________________
36 #include <comphelper/configurationhelper.hxx>
37 #include <com/sun/star/beans/XPropertySet.hpp>
38 #include <com/sun/star/container/XNameAccess.hpp>
39 #include <com/sun/star/container/XNameContainer.hpp>
40 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
42 //_______________________________________________
47 namespace css
= ::com::sun::star
;
49 //_______________________________________________
52 //-----------------------------------------------
53 css::uno::Reference
< css::uno::XInterface
> ConfigurationHelper::openConfig(const css::uno::Reference
< css::lang::XMultiServiceFactory
> xSMGR
,
54 const ::rtl::OUString
& sPackage
,
57 css::uno::Reference
< css::lang::XMultiServiceFactory
> xConfigProvider(
58 xSMGR
->createInstance(::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider")), css::uno::UNO_QUERY_THROW
);
60 ::comphelper::SequenceAsVector
< css::uno::Any
> lParams
;
61 css::beans::PropertyValue aParam
;
64 aParam
.Name
= ::rtl::OUString::createFromAscii("nodepath");
65 aParam
.Value
<<= sPackage
;
66 lParams
.push_back(css::uno::makeAny(aParam
));
68 // enable all locales mode
69 if ((eMode
& ConfigurationHelper::E_ALL_LOCALES
)==ConfigurationHelper::E_ALL_LOCALES
)
71 aParam
.Name
= ::rtl::OUString::createFromAscii("locale");
72 aParam
.Value
<<= ::rtl::OUString::createFromAscii("*");
73 lParams
.push_back(css::uno::makeAny(aParam
));
76 // enable lazy writing
77 sal_Bool bLazy
= ((eMode
& ConfigurationHelper::E_LAZY_WRITE
)==ConfigurationHelper::E_LAZY_WRITE
);
78 aParam
.Name
= ::rtl::OUString::createFromAscii("lazywrite");
79 aParam
.Value
= css::uno::makeAny(bLazy
);
80 lParams
.push_back(css::uno::makeAny(aParam
));
83 css::uno::Reference
< css::uno::XInterface
> xCFG
;
85 sal_Bool bReadOnly
= ((eMode
& ConfigurationHelper::E_READONLY
)==ConfigurationHelper::E_READONLY
);
87 xCFG
= xConfigProvider
->createInstanceWithArguments(
88 ::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationAccess"),
89 lParams
.getAsConstList());
91 xCFG
= xConfigProvider
->createInstanceWithArguments(
92 ::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationUpdateAccess"),
93 lParams
.getAsConstList());
98 //-----------------------------------------------
99 css::uno::Any
ConfigurationHelper::readRelativeKey(const css::uno::Reference
< css::uno::XInterface
> xCFG
,
100 const ::rtl::OUString
& sRelPath
,
101 const ::rtl::OUString
& sKey
)
103 css::uno::Reference
< css::container::XHierarchicalNameAccess
> xAccess(xCFG
, css::uno::UNO_QUERY_THROW
);
105 css::uno::Reference
< css::beans::XPropertySet
> xProps
;
106 xAccess
->getByHierarchicalName(sRelPath
) >>= xProps
;
109 ::rtl::OUStringBuffer
sMsg(256);
110 sMsg
.appendAscii("The requested path \"");
111 sMsg
.append (sRelPath
);
112 sMsg
.appendAscii("\" does not exists." );
114 throw css::container::NoSuchElementException(
115 sMsg
.makeStringAndClear(),
116 css::uno::Reference
< css::uno::XInterface
>());
118 return xProps
->getPropertyValue(sKey
);
121 //-----------------------------------------------
122 void ConfigurationHelper::writeRelativeKey(const css::uno::Reference
< css::uno::XInterface
> xCFG
,
123 const ::rtl::OUString
& sRelPath
,
124 const ::rtl::OUString
& sKey
,
125 const css::uno::Any
& aValue
)
127 css::uno::Reference
< css::container::XHierarchicalNameAccess
> xAccess(xCFG
, css::uno::UNO_QUERY_THROW
);
129 css::uno::Reference
< css::beans::XPropertySet
> xProps
;
130 xAccess
->getByHierarchicalName(sRelPath
) >>= xProps
;
133 ::rtl::OUStringBuffer
sMsg(256);
134 sMsg
.appendAscii("The requested path \"");
135 sMsg
.append (sRelPath
);
136 sMsg
.appendAscii("\" does not exists." );
138 throw css::container::NoSuchElementException(
139 sMsg
.makeStringAndClear(),
140 css::uno::Reference
< css::uno::XInterface
>());
142 xProps
->setPropertyValue(sKey
, aValue
);
145 //-----------------------------------------------
146 css::uno::Reference
< css::uno::XInterface
> ConfigurationHelper::makeSureSetNodeExists(const css::uno::Reference
< css::uno::XInterface
> xCFG
,
147 const ::rtl::OUString
& sRelPathToSet
,
148 const ::rtl::OUString
& sSetNode
)
150 css::uno::Reference
< css::container::XHierarchicalNameAccess
> xAccess(xCFG
, css::uno::UNO_QUERY_THROW
);
151 css::uno::Reference
< css::container::XNameAccess
> xSet
;
152 xAccess
->getByHierarchicalName(sRelPathToSet
) >>= xSet
;
155 ::rtl::OUStringBuffer
sMsg(256);
156 sMsg
.appendAscii("The requested path \"");
157 sMsg
.append (sRelPathToSet
);
158 sMsg
.appendAscii("\" does not exists." );
160 throw css::container::NoSuchElementException(
161 sMsg
.makeStringAndClear(),
162 css::uno::Reference
< css::uno::XInterface
>());
165 css::uno::Reference
< css::uno::XInterface
> xNode
;
166 if (xSet
->hasByName(sSetNode
))
167 xSet
->getByName(sSetNode
) >>= xNode
;
170 css::uno::Reference
< css::lang::XSingleServiceFactory
> xNodeFactory(xSet
, css::uno::UNO_QUERY_THROW
);
171 xNode
= xNodeFactory
->createInstance();
172 css::uno::Reference
< css::container::XNameContainer
> xSetReplace(xSet
, css::uno::UNO_QUERY_THROW
);
173 xSetReplace
->insertByName(sSetNode
, css::uno::makeAny(xNode
));
179 //-----------------------------------------------
180 css::uno::Any
ConfigurationHelper::readDirectKey(const css::uno::Reference
< css::lang::XMultiServiceFactory
> xSMGR
,
181 const ::rtl::OUString
& sPackage
,
182 const ::rtl::OUString
& sRelPath
,
183 const ::rtl::OUString
& sKey
,
186 css::uno::Reference
< css::uno::XInterface
> xCFG
= ConfigurationHelper::openConfig(xSMGR
, sPackage
, eMode
);
187 return ConfigurationHelper::readRelativeKey(xCFG
, sRelPath
, sKey
);
190 //-----------------------------------------------
191 void ConfigurationHelper::writeDirectKey(const css::uno::Reference
< css::lang::XMultiServiceFactory
> xSMGR
,
192 const ::rtl::OUString
& sPackage
,
193 const ::rtl::OUString
& sRelPath
,
194 const ::rtl::OUString
& sKey
,
195 const css::uno::Any
& aValue
,
198 css::uno::Reference
< css::uno::XInterface
> xCFG
= ConfigurationHelper::openConfig(xSMGR
, sPackage
, eMode
);
199 ConfigurationHelper::writeRelativeKey(xCFG
, sRelPath
, sKey
, aValue
);
200 ConfigurationHelper::flush(xCFG
);
203 //-----------------------------------------------
204 void ConfigurationHelper::flush(const css::uno::Reference
< css::uno::XInterface
>& xCFG
)
206 css::uno::Reference
< css::util::XChangesBatch
> xBatch(xCFG
, css::uno::UNO_QUERY_THROW
);
207 xBatch
->commitChanges();
210 } // namespace comphelper