xref: /AOO41X/main/comphelper/source/misc/configurationhelper.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir //_______________________________________________
32*cdf0e10cSrcweir // includes
33*cdf0e10cSrcweir #include <comphelper/configurationhelper.hxx>
34*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir //_______________________________________________
40*cdf0e10cSrcweir // namespace
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir namespace comphelper{
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir namespace css = ::com::sun::star;
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir //_______________________________________________
47*cdf0e10cSrcweir // definitions
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir //-----------------------------------------------
50*cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface > ConfigurationHelper::openConfig(const css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR   ,
51*cdf0e10cSrcweir                                                                             const ::rtl::OUString&                                       sPackage,
52*cdf0e10cSrcweir                                                                                   sal_Int32                                              eMode   )
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir     css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider(
55*cdf0e10cSrcweir         xSMGR->createInstance(::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider")), css::uno::UNO_QUERY_THROW);
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir     ::comphelper::SequenceAsVector< css::uno::Any > lParams;
58*cdf0e10cSrcweir     css::beans::PropertyValue                       aParam ;
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir     // set root path
61*cdf0e10cSrcweir     aParam.Name    = ::rtl::OUString::createFromAscii("nodepath");
62*cdf0e10cSrcweir     aParam.Value <<= sPackage;
63*cdf0e10cSrcweir     lParams.push_back(css::uno::makeAny(aParam));
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir     // enable all locales mode
66*cdf0e10cSrcweir     if ((eMode & ConfigurationHelper::E_ALL_LOCALES)==ConfigurationHelper::E_ALL_LOCALES)
67*cdf0e10cSrcweir     {
68*cdf0e10cSrcweir         aParam.Name    = ::rtl::OUString::createFromAscii("locale");
69*cdf0e10cSrcweir         aParam.Value <<= ::rtl::OUString::createFromAscii("*");
70*cdf0e10cSrcweir         lParams.push_back(css::uno::makeAny(aParam));
71*cdf0e10cSrcweir     }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir     // enable lazy writing
74*cdf0e10cSrcweir     sal_Bool bLazy = ((eMode & ConfigurationHelper::E_LAZY_WRITE)==ConfigurationHelper::E_LAZY_WRITE);
75*cdf0e10cSrcweir     aParam.Name    = ::rtl::OUString::createFromAscii("lazywrite");
76*cdf0e10cSrcweir     aParam.Value   = css::uno::makeAny(bLazy);
77*cdf0e10cSrcweir     lParams.push_back(css::uno::makeAny(aParam));
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir     // open it
80*cdf0e10cSrcweir     css::uno::Reference< css::uno::XInterface > xCFG;
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir     sal_Bool bReadOnly = ((eMode & ConfigurationHelper::E_READONLY)==ConfigurationHelper::E_READONLY);
83*cdf0e10cSrcweir     if (bReadOnly)
84*cdf0e10cSrcweir         xCFG = xConfigProvider->createInstanceWithArguments(
85*cdf0e10cSrcweir                 ::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationAccess"),
86*cdf0e10cSrcweir                 lParams.getAsConstList());
87*cdf0e10cSrcweir     else
88*cdf0e10cSrcweir         xCFG = xConfigProvider->createInstanceWithArguments(
89*cdf0e10cSrcweir                 ::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationUpdateAccess"),
90*cdf0e10cSrcweir                 lParams.getAsConstList());
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     return xCFG;
93*cdf0e10cSrcweir }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir //-----------------------------------------------
96*cdf0e10cSrcweir css::uno::Any ConfigurationHelper::readRelativeKey(const css::uno::Reference< css::uno::XInterface > xCFG    ,
97*cdf0e10cSrcweir                                                    const ::rtl::OUString&                            sRelPath,
98*cdf0e10cSrcweir                                                    const ::rtl::OUString&                            sKey    )
99*cdf0e10cSrcweir {
100*cdf0e10cSrcweir     css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir     css::uno::Reference< css::beans::XPropertySet > xProps;
103*cdf0e10cSrcweir     xAccess->getByHierarchicalName(sRelPath) >>= xProps;
104*cdf0e10cSrcweir     if (!xProps.is())
105*cdf0e10cSrcweir     {
106*cdf0e10cSrcweir         ::rtl::OUStringBuffer sMsg(256);
107*cdf0e10cSrcweir         sMsg.appendAscii("The requested path \"");
108*cdf0e10cSrcweir         sMsg.append     (sRelPath               );
109*cdf0e10cSrcweir         sMsg.appendAscii("\" does not exists."  );
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir         throw css::container::NoSuchElementException(
112*cdf0e10cSrcweir                     sMsg.makeStringAndClear(),
113*cdf0e10cSrcweir                     css::uno::Reference< css::uno::XInterface >());
114*cdf0e10cSrcweir     }
115*cdf0e10cSrcweir     return xProps->getPropertyValue(sKey);
116*cdf0e10cSrcweir }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir //-----------------------------------------------
119*cdf0e10cSrcweir void ConfigurationHelper::writeRelativeKey(const css::uno::Reference< css::uno::XInterface > xCFG    ,
120*cdf0e10cSrcweir                                            const ::rtl::OUString&                            sRelPath,
121*cdf0e10cSrcweir                                            const ::rtl::OUString&                            sKey    ,
122*cdf0e10cSrcweir                                            const css::uno::Any&                              aValue  )
123*cdf0e10cSrcweir {
124*cdf0e10cSrcweir     css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     css::uno::Reference< css::beans::XPropertySet > xProps;
127*cdf0e10cSrcweir     xAccess->getByHierarchicalName(sRelPath) >>= xProps;
128*cdf0e10cSrcweir     if (!xProps.is())
129*cdf0e10cSrcweir     {
130*cdf0e10cSrcweir         ::rtl::OUStringBuffer sMsg(256);
131*cdf0e10cSrcweir         sMsg.appendAscii("The requested path \"");
132*cdf0e10cSrcweir         sMsg.append     (sRelPath               );
133*cdf0e10cSrcweir         sMsg.appendAscii("\" does not exists."  );
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir         throw css::container::NoSuchElementException(
136*cdf0e10cSrcweir                     sMsg.makeStringAndClear(),
137*cdf0e10cSrcweir                     css::uno::Reference< css::uno::XInterface >());
138*cdf0e10cSrcweir     }
139*cdf0e10cSrcweir     xProps->setPropertyValue(sKey, aValue);
140*cdf0e10cSrcweir }
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir //-----------------------------------------------
143*cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface > ConfigurationHelper::makeSureSetNodeExists(const css::uno::Reference< css::uno::XInterface > xCFG         ,
144*cdf0e10cSrcweir                                                                                        const ::rtl::OUString&                            sRelPathToSet,
145*cdf0e10cSrcweir                                                                                        const ::rtl::OUString&                            sSetNode     )
146*cdf0e10cSrcweir {
147*cdf0e10cSrcweir     css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
148*cdf0e10cSrcweir     css::uno::Reference< css::container::XNameAccess > xSet;
149*cdf0e10cSrcweir     xAccess->getByHierarchicalName(sRelPathToSet) >>= xSet;
150*cdf0e10cSrcweir     if (!xSet.is())
151*cdf0e10cSrcweir     {
152*cdf0e10cSrcweir         ::rtl::OUStringBuffer sMsg(256);
153*cdf0e10cSrcweir         sMsg.appendAscii("The requested path \"");
154*cdf0e10cSrcweir         sMsg.append     (sRelPathToSet          );
155*cdf0e10cSrcweir         sMsg.appendAscii("\" does not exists."  );
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir         throw css::container::NoSuchElementException(
158*cdf0e10cSrcweir                     sMsg.makeStringAndClear(),
159*cdf0e10cSrcweir                     css::uno::Reference< css::uno::XInterface >());
160*cdf0e10cSrcweir     }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir     css::uno::Reference< css::uno::XInterface > xNode;
163*cdf0e10cSrcweir     if (xSet->hasByName(sSetNode))
164*cdf0e10cSrcweir         xSet->getByName(sSetNode) >>= xNode;
165*cdf0e10cSrcweir     else
166*cdf0e10cSrcweir     {
167*cdf0e10cSrcweir         css::uno::Reference< css::lang::XSingleServiceFactory > xNodeFactory(xSet, css::uno::UNO_QUERY_THROW);
168*cdf0e10cSrcweir         xNode = xNodeFactory->createInstance();
169*cdf0e10cSrcweir         css::uno::Reference< css::container::XNameContainer > xSetReplace(xSet, css::uno::UNO_QUERY_THROW);
170*cdf0e10cSrcweir         xSetReplace->insertByName(sSetNode, css::uno::makeAny(xNode));
171*cdf0e10cSrcweir     }
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir     return xNode;
174*cdf0e10cSrcweir }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir //-----------------------------------------------
177*cdf0e10cSrcweir css::uno::Any ConfigurationHelper::readDirectKey(const css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR   ,
178*cdf0e10cSrcweir                                                  const ::rtl::OUString&                                       sPackage,
179*cdf0e10cSrcweir                                                  const ::rtl::OUString&                                       sRelPath,
180*cdf0e10cSrcweir                                                  const ::rtl::OUString&                                       sKey    ,
181*cdf0e10cSrcweir                                                        sal_Int32                                              eMode   )
182*cdf0e10cSrcweir {
183*cdf0e10cSrcweir     css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(xSMGR, sPackage, eMode);
184*cdf0e10cSrcweir     return ConfigurationHelper::readRelativeKey(xCFG, sRelPath, sKey);
185*cdf0e10cSrcweir }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir //-----------------------------------------------
188*cdf0e10cSrcweir void ConfigurationHelper::writeDirectKey(const css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR   ,
189*cdf0e10cSrcweir                                          const ::rtl::OUString&                                       sPackage,
190*cdf0e10cSrcweir                                          const ::rtl::OUString&                                       sRelPath,
191*cdf0e10cSrcweir                                          const ::rtl::OUString&                                       sKey    ,
192*cdf0e10cSrcweir                                          const css::uno::Any&                                         aValue  ,
193*cdf0e10cSrcweir                                                sal_Int32                                              eMode   )
194*cdf0e10cSrcweir {
195*cdf0e10cSrcweir     css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(xSMGR, sPackage, eMode);
196*cdf0e10cSrcweir     ConfigurationHelper::writeRelativeKey(xCFG, sRelPath, sKey, aValue);
197*cdf0e10cSrcweir     ConfigurationHelper::flush(xCFG);
198*cdf0e10cSrcweir }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir //-----------------------------------------------
201*cdf0e10cSrcweir void ConfigurationHelper::flush(const css::uno::Reference< css::uno::XInterface >& xCFG)
202*cdf0e10cSrcweir {
203*cdf0e10cSrcweir     css::uno::Reference< css::util::XChangesBatch > xBatch(xCFG, css::uno::UNO_QUERY_THROW);
204*cdf0e10cSrcweir     xBatch->commitChanges();
205*cdf0e10cSrcweir }
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir } // namespace comphelper
208