1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 
31 //_______________________________________________
32 // includes
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>
38 
39 //_______________________________________________
40 // namespace
41 
42 namespace comphelper{
43 
44 namespace css = ::com::sun::star;
45 
46 //_______________________________________________
47 // definitions
48 
49 //-----------------------------------------------
50 css::uno::Reference< css::uno::XInterface > ConfigurationHelper::openConfig(const css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR   ,
51                                                                             const ::rtl::OUString&                                       sPackage,
52                                                                                   sal_Int32                                              eMode   )
53 {
54     css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider(
55         xSMGR->createInstance(::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider")), css::uno::UNO_QUERY_THROW);
56 
57     ::comphelper::SequenceAsVector< css::uno::Any > lParams;
58     css::beans::PropertyValue                       aParam ;
59 
60     // set root path
61     aParam.Name    = ::rtl::OUString::createFromAscii("nodepath");
62     aParam.Value <<= sPackage;
63     lParams.push_back(css::uno::makeAny(aParam));
64 
65     // enable all locales mode
66     if ((eMode & ConfigurationHelper::E_ALL_LOCALES)==ConfigurationHelper::E_ALL_LOCALES)
67     {
68         aParam.Name    = ::rtl::OUString::createFromAscii("locale");
69         aParam.Value <<= ::rtl::OUString::createFromAscii("*");
70         lParams.push_back(css::uno::makeAny(aParam));
71     }
72 
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));
78 
79     // open it
80     css::uno::Reference< css::uno::XInterface > xCFG;
81 
82     sal_Bool bReadOnly = ((eMode & ConfigurationHelper::E_READONLY)==ConfigurationHelper::E_READONLY);
83     if (bReadOnly)
84         xCFG = xConfigProvider->createInstanceWithArguments(
85                 ::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationAccess"),
86                 lParams.getAsConstList());
87     else
88         xCFG = xConfigProvider->createInstanceWithArguments(
89                 ::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationUpdateAccess"),
90                 lParams.getAsConstList());
91 
92     return xCFG;
93 }
94 
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    )
99 {
100     css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
101 
102     css::uno::Reference< css::beans::XPropertySet > xProps;
103     xAccess->getByHierarchicalName(sRelPath) >>= xProps;
104     if (!xProps.is())
105     {
106         ::rtl::OUStringBuffer sMsg(256);
107         sMsg.appendAscii("The requested path \"");
108         sMsg.append     (sRelPath               );
109         sMsg.appendAscii("\" does not exists."  );
110 
111         throw css::container::NoSuchElementException(
112                     sMsg.makeStringAndClear(),
113                     css::uno::Reference< css::uno::XInterface >());
114     }
115     return xProps->getPropertyValue(sKey);
116 }
117 
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  )
123 {
124     css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
125 
126     css::uno::Reference< css::beans::XPropertySet > xProps;
127     xAccess->getByHierarchicalName(sRelPath) >>= xProps;
128     if (!xProps.is())
129     {
130         ::rtl::OUStringBuffer sMsg(256);
131         sMsg.appendAscii("The requested path \"");
132         sMsg.append     (sRelPath               );
133         sMsg.appendAscii("\" does not exists."  );
134 
135         throw css::container::NoSuchElementException(
136                     sMsg.makeStringAndClear(),
137                     css::uno::Reference< css::uno::XInterface >());
138     }
139     xProps->setPropertyValue(sKey, aValue);
140 }
141 
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     )
146 {
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;
150     if (!xSet.is())
151     {
152         ::rtl::OUStringBuffer sMsg(256);
153         sMsg.appendAscii("The requested path \"");
154         sMsg.append     (sRelPathToSet          );
155         sMsg.appendAscii("\" does not exists."  );
156 
157         throw css::container::NoSuchElementException(
158                     sMsg.makeStringAndClear(),
159                     css::uno::Reference< css::uno::XInterface >());
160     }
161 
162     css::uno::Reference< css::uno::XInterface > xNode;
163     if (xSet->hasByName(sSetNode))
164         xSet->getByName(sSetNode) >>= xNode;
165     else
166     {
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));
171     }
172 
173     return xNode;
174 }
175 
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    ,
181                                                        sal_Int32                                              eMode   )
182 {
183     css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(xSMGR, sPackage, eMode);
184     return ConfigurationHelper::readRelativeKey(xCFG, sRelPath, sKey);
185 }
186 
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  ,
193                                                sal_Int32                                              eMode   )
194 {
195     css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(xSMGR, sPackage, eMode);
196     ConfigurationHelper::writeRelativeKey(xCFG, sRelPath, sKey, aValue);
197     ConfigurationHelper::flush(xCFG);
198 }
199 
200 //-----------------------------------------------
201 void ConfigurationHelper::flush(const css::uno::Reference< css::uno::XInterface >& xCFG)
202 {
203     css::uno::Reference< css::util::XChangesBatch > xBatch(xCFG, css::uno::UNO_QUERY_THROW);
204     xBatch->commitChanges();
205 }
206 
207 } // namespace comphelper
208