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