1*dde7d3faSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*dde7d3faSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*dde7d3faSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*dde7d3faSAndrew Rist * distributed with this work for additional information
6*dde7d3faSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*dde7d3faSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*dde7d3faSAndrew Rist * "License"); you may not use this file except in compliance
9*dde7d3faSAndrew Rist * with the License. You may obtain a copy of the License at
10*dde7d3faSAndrew Rist *
11*dde7d3faSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*dde7d3faSAndrew Rist *
13*dde7d3faSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*dde7d3faSAndrew Rist * software distributed under the License is distributed on an
15*dde7d3faSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*dde7d3faSAndrew Rist * KIND, either express or implied. See the License for the
17*dde7d3faSAndrew Rist * specific language governing permissions and limitations
18*dde7d3faSAndrew Rist * under the License.
19*dde7d3faSAndrew Rist *
20*dde7d3faSAndrew Rist *************************************************************/
21*dde7d3faSAndrew Rist
22*dde7d3faSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir //_______________________________________________
28cdf0e10cSrcweir // includes
29cdf0e10cSrcweir #include <comphelper/configurationhelper.hxx>
30cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
31cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
32cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
33cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
34cdf0e10cSrcweir
35cdf0e10cSrcweir //_______________________________________________
36cdf0e10cSrcweir // namespace
37cdf0e10cSrcweir
38cdf0e10cSrcweir namespace comphelper{
39cdf0e10cSrcweir
40cdf0e10cSrcweir namespace css = ::com::sun::star;
41cdf0e10cSrcweir
42cdf0e10cSrcweir //_______________________________________________
43cdf0e10cSrcweir // definitions
44cdf0e10cSrcweir
45cdf0e10cSrcweir //-----------------------------------------------
openConfig(const css::uno::Reference<css::lang::XMultiServiceFactory> xSMGR,const::rtl::OUString & sPackage,sal_Int32 eMode)46cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface > ConfigurationHelper::openConfig(const css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR ,
47cdf0e10cSrcweir const ::rtl::OUString& sPackage,
48cdf0e10cSrcweir sal_Int32 eMode )
49cdf0e10cSrcweir {
50cdf0e10cSrcweir css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider(
51cdf0e10cSrcweir xSMGR->createInstance(::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider")), css::uno::UNO_QUERY_THROW);
52cdf0e10cSrcweir
53cdf0e10cSrcweir ::comphelper::SequenceAsVector< css::uno::Any > lParams;
54cdf0e10cSrcweir css::beans::PropertyValue aParam ;
55cdf0e10cSrcweir
56cdf0e10cSrcweir // set root path
57cdf0e10cSrcweir aParam.Name = ::rtl::OUString::createFromAscii("nodepath");
58cdf0e10cSrcweir aParam.Value <<= sPackage;
59cdf0e10cSrcweir lParams.push_back(css::uno::makeAny(aParam));
60cdf0e10cSrcweir
61cdf0e10cSrcweir // enable all locales mode
62cdf0e10cSrcweir if ((eMode & ConfigurationHelper::E_ALL_LOCALES)==ConfigurationHelper::E_ALL_LOCALES)
63cdf0e10cSrcweir {
64cdf0e10cSrcweir aParam.Name = ::rtl::OUString::createFromAscii("locale");
65cdf0e10cSrcweir aParam.Value <<= ::rtl::OUString::createFromAscii("*");
66cdf0e10cSrcweir lParams.push_back(css::uno::makeAny(aParam));
67cdf0e10cSrcweir }
68cdf0e10cSrcweir
69cdf0e10cSrcweir // enable lazy writing
70cdf0e10cSrcweir sal_Bool bLazy = ((eMode & ConfigurationHelper::E_LAZY_WRITE)==ConfigurationHelper::E_LAZY_WRITE);
71cdf0e10cSrcweir aParam.Name = ::rtl::OUString::createFromAscii("lazywrite");
72cdf0e10cSrcweir aParam.Value = css::uno::makeAny(bLazy);
73cdf0e10cSrcweir lParams.push_back(css::uno::makeAny(aParam));
74cdf0e10cSrcweir
75cdf0e10cSrcweir // open it
76cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface > xCFG;
77cdf0e10cSrcweir
78cdf0e10cSrcweir sal_Bool bReadOnly = ((eMode & ConfigurationHelper::E_READONLY)==ConfigurationHelper::E_READONLY);
79cdf0e10cSrcweir if (bReadOnly)
80cdf0e10cSrcweir xCFG = xConfigProvider->createInstanceWithArguments(
81cdf0e10cSrcweir ::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationAccess"),
82cdf0e10cSrcweir lParams.getAsConstList());
83cdf0e10cSrcweir else
84cdf0e10cSrcweir xCFG = xConfigProvider->createInstanceWithArguments(
85cdf0e10cSrcweir ::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationUpdateAccess"),
86cdf0e10cSrcweir lParams.getAsConstList());
87cdf0e10cSrcweir
88cdf0e10cSrcweir return xCFG;
89cdf0e10cSrcweir }
90cdf0e10cSrcweir
91cdf0e10cSrcweir //-----------------------------------------------
readRelativeKey(const css::uno::Reference<css::uno::XInterface> xCFG,const::rtl::OUString & sRelPath,const::rtl::OUString & sKey)92cdf0e10cSrcweir css::uno::Any ConfigurationHelper::readRelativeKey(const css::uno::Reference< css::uno::XInterface > xCFG ,
93cdf0e10cSrcweir const ::rtl::OUString& sRelPath,
94cdf0e10cSrcweir const ::rtl::OUString& sKey )
95cdf0e10cSrcweir {
96cdf0e10cSrcweir css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
97cdf0e10cSrcweir
98cdf0e10cSrcweir css::uno::Reference< css::beans::XPropertySet > xProps;
99cdf0e10cSrcweir xAccess->getByHierarchicalName(sRelPath) >>= xProps;
100cdf0e10cSrcweir if (!xProps.is())
101cdf0e10cSrcweir {
102cdf0e10cSrcweir ::rtl::OUStringBuffer sMsg(256);
103cdf0e10cSrcweir sMsg.appendAscii("The requested path \"");
104cdf0e10cSrcweir sMsg.append (sRelPath );
105cdf0e10cSrcweir sMsg.appendAscii("\" does not exists." );
106cdf0e10cSrcweir
107cdf0e10cSrcweir throw css::container::NoSuchElementException(
108cdf0e10cSrcweir sMsg.makeStringAndClear(),
109cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface >());
110cdf0e10cSrcweir }
111cdf0e10cSrcweir return xProps->getPropertyValue(sKey);
112cdf0e10cSrcweir }
113cdf0e10cSrcweir
114cdf0e10cSrcweir //-----------------------------------------------
writeRelativeKey(const css::uno::Reference<css::uno::XInterface> xCFG,const::rtl::OUString & sRelPath,const::rtl::OUString & sKey,const css::uno::Any & aValue)115cdf0e10cSrcweir void ConfigurationHelper::writeRelativeKey(const css::uno::Reference< css::uno::XInterface > xCFG ,
116cdf0e10cSrcweir const ::rtl::OUString& sRelPath,
117cdf0e10cSrcweir const ::rtl::OUString& sKey ,
118cdf0e10cSrcweir const css::uno::Any& aValue )
119cdf0e10cSrcweir {
120cdf0e10cSrcweir css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
121cdf0e10cSrcweir
122cdf0e10cSrcweir css::uno::Reference< css::beans::XPropertySet > xProps;
123cdf0e10cSrcweir xAccess->getByHierarchicalName(sRelPath) >>= xProps;
124cdf0e10cSrcweir if (!xProps.is())
125cdf0e10cSrcweir {
126cdf0e10cSrcweir ::rtl::OUStringBuffer sMsg(256);
127cdf0e10cSrcweir sMsg.appendAscii("The requested path \"");
128cdf0e10cSrcweir sMsg.append (sRelPath );
129cdf0e10cSrcweir sMsg.appendAscii("\" does not exists." );
130cdf0e10cSrcweir
131cdf0e10cSrcweir throw css::container::NoSuchElementException(
132cdf0e10cSrcweir sMsg.makeStringAndClear(),
133cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface >());
134cdf0e10cSrcweir }
135cdf0e10cSrcweir xProps->setPropertyValue(sKey, aValue);
136cdf0e10cSrcweir }
137cdf0e10cSrcweir
138cdf0e10cSrcweir //-----------------------------------------------
makeSureSetNodeExists(const css::uno::Reference<css::uno::XInterface> xCFG,const::rtl::OUString & sRelPathToSet,const::rtl::OUString & sSetNode)139cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface > ConfigurationHelper::makeSureSetNodeExists(const css::uno::Reference< css::uno::XInterface > xCFG ,
140cdf0e10cSrcweir const ::rtl::OUString& sRelPathToSet,
141cdf0e10cSrcweir const ::rtl::OUString& sSetNode )
142cdf0e10cSrcweir {
143cdf0e10cSrcweir css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
144cdf0e10cSrcweir css::uno::Reference< css::container::XNameAccess > xSet;
145cdf0e10cSrcweir xAccess->getByHierarchicalName(sRelPathToSet) >>= xSet;
146cdf0e10cSrcweir if (!xSet.is())
147cdf0e10cSrcweir {
148cdf0e10cSrcweir ::rtl::OUStringBuffer sMsg(256);
149cdf0e10cSrcweir sMsg.appendAscii("The requested path \"");
150cdf0e10cSrcweir sMsg.append (sRelPathToSet );
151cdf0e10cSrcweir sMsg.appendAscii("\" does not exists." );
152cdf0e10cSrcweir
153cdf0e10cSrcweir throw css::container::NoSuchElementException(
154cdf0e10cSrcweir sMsg.makeStringAndClear(),
155cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface >());
156cdf0e10cSrcweir }
157cdf0e10cSrcweir
158cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface > xNode;
159cdf0e10cSrcweir if (xSet->hasByName(sSetNode))
160cdf0e10cSrcweir xSet->getByName(sSetNode) >>= xNode;
161cdf0e10cSrcweir else
162cdf0e10cSrcweir {
163cdf0e10cSrcweir css::uno::Reference< css::lang::XSingleServiceFactory > xNodeFactory(xSet, css::uno::UNO_QUERY_THROW);
164cdf0e10cSrcweir xNode = xNodeFactory->createInstance();
165cdf0e10cSrcweir css::uno::Reference< css::container::XNameContainer > xSetReplace(xSet, css::uno::UNO_QUERY_THROW);
166cdf0e10cSrcweir xSetReplace->insertByName(sSetNode, css::uno::makeAny(xNode));
167cdf0e10cSrcweir }
168cdf0e10cSrcweir
169cdf0e10cSrcweir return xNode;
170cdf0e10cSrcweir }
171cdf0e10cSrcweir
172cdf0e10cSrcweir //-----------------------------------------------
readDirectKey(const css::uno::Reference<css::lang::XMultiServiceFactory> xSMGR,const::rtl::OUString & sPackage,const::rtl::OUString & sRelPath,const::rtl::OUString & sKey,sal_Int32 eMode)173cdf0e10cSrcweir css::uno::Any ConfigurationHelper::readDirectKey(const css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR ,
174cdf0e10cSrcweir const ::rtl::OUString& sPackage,
175cdf0e10cSrcweir const ::rtl::OUString& sRelPath,
176cdf0e10cSrcweir const ::rtl::OUString& sKey ,
177cdf0e10cSrcweir sal_Int32 eMode )
178cdf0e10cSrcweir {
179cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(xSMGR, sPackage, eMode);
180cdf0e10cSrcweir return ConfigurationHelper::readRelativeKey(xCFG, sRelPath, sKey);
181cdf0e10cSrcweir }
182cdf0e10cSrcweir
183cdf0e10cSrcweir //-----------------------------------------------
writeDirectKey(const css::uno::Reference<css::lang::XMultiServiceFactory> xSMGR,const::rtl::OUString & sPackage,const::rtl::OUString & sRelPath,const::rtl::OUString & sKey,const css::uno::Any & aValue,sal_Int32 eMode)184cdf0e10cSrcweir void ConfigurationHelper::writeDirectKey(const css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR ,
185cdf0e10cSrcweir const ::rtl::OUString& sPackage,
186cdf0e10cSrcweir const ::rtl::OUString& sRelPath,
187cdf0e10cSrcweir const ::rtl::OUString& sKey ,
188cdf0e10cSrcweir const css::uno::Any& aValue ,
189cdf0e10cSrcweir sal_Int32 eMode )
190cdf0e10cSrcweir {
191cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(xSMGR, sPackage, eMode);
192cdf0e10cSrcweir ConfigurationHelper::writeRelativeKey(xCFG, sRelPath, sKey, aValue);
193cdf0e10cSrcweir ConfigurationHelper::flush(xCFG);
194cdf0e10cSrcweir }
195cdf0e10cSrcweir
196cdf0e10cSrcweir //-----------------------------------------------
flush(const css::uno::Reference<css::uno::XInterface> & xCFG)197cdf0e10cSrcweir void ConfigurationHelper::flush(const css::uno::Reference< css::uno::XInterface >& xCFG)
198cdf0e10cSrcweir {
199cdf0e10cSrcweir css::uno::Reference< css::util::XChangesBatch > xBatch(xCFG, css::uno::UNO_QUERY_THROW);
200cdf0e10cSrcweir xBatch->commitChanges();
201cdf0e10cSrcweir }
202cdf0e10cSrcweir
203cdf0e10cSrcweir } // namespace comphelper
204