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_extensions.hxx"
26 
27 #include "myconfigurationhelper.hxx"
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/container/XNameAccess.hpp>
30 #include <com/sun/star/container/XNameContainer.hpp>
31 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
32 #include <rtl/ustrbuf.hxx>
33 #include <vector>
34 
35 
36 namespace css = ::com::sun::star;
37 using namespace ::com::sun::star::lang;
38 using namespace ::com::sun::star::uno;
39 using ::rtl::OUString;
40 using ::rtl::OUStringBuffer;
41 using ::std::vector;
42 
43 
44 namespace
45 {
sequenceFromVector(const vector<Any> & vec)46     static const Sequence<Any> sequenceFromVector(const vector<Any>& vec)
47     {
48         Sequence<Any> result(vec.size());
49         for(size_t idx = 0; idx < vec.size(); ++idx)
50             result[idx] = vec[idx];
51         return result;
52     };
53 
noSuchElement(const OUString & path)54     static const OUString noSuchElement(const OUString& path)
55     {
56         OUStringBuffer buf(256);
57         buf.appendAscii("The requested path \"");
58         buf.append(path);
59         buf.appendAscii("\" does not exists.");
60         return buf.makeStringAndClear();
61     };
62 }
63 
64 namespace oooimprovement
65 {
openConfig(const Reference<XMultiServiceFactory> xSMGR,const OUString & sPackage,sal_Int32 eMode)66     Reference<XInterface> MyConfigurationHelper::openConfig(
67         const Reference<XMultiServiceFactory> xSMGR,
68         const OUString& sPackage,
69         sal_Int32 eMode)
70     {
71         Reference<XMultiServiceFactory> xConfigProvider(
72             xSMGR->createInstance(OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider")),
73             UNO_QUERY_THROW);
74 
75         vector<Any> lParams;
76         css::beans::PropertyValue aParam;
77 
78         // set root path
79         aParam.Name = OUString::createFromAscii("nodepath");
80         aParam.Value <<= sPackage;
81         lParams.push_back(makeAny(aParam));
82 
83         // enable all locales mode
84         if ((eMode & MyConfigurationHelper::E_ALL_LOCALES)==MyConfigurationHelper::E_ALL_LOCALES)
85         {
86             aParam.Name = OUString::createFromAscii("locale");
87             aParam.Value <<= OUString::createFromAscii("*");
88             lParams.push_back(makeAny(aParam));
89         }
90 
91         // enable lazy writing
92         sal_Bool bLazy = ((eMode & MyConfigurationHelper::E_LAZY_WRITE)==MyConfigurationHelper::E_LAZY_WRITE);
93         aParam.Name = OUString::createFromAscii("lazywrite");
94         aParam.Value = makeAny(bLazy);
95         lParams.push_back(makeAny(aParam));
96 
97         // open it
98         Reference<XInterface> xCFG;
99 
100         sal_Bool bReadOnly = ((eMode & MyConfigurationHelper::E_READONLY)==MyConfigurationHelper::E_READONLY);
101         if (bReadOnly)
102             xCFG = xConfigProvider->createInstanceWithArguments(
103                 OUString::createFromAscii("com.sun.star.configuration.ConfigurationAccess"),
104                 sequenceFromVector(lParams));
105         else
106             xCFG = xConfigProvider->createInstanceWithArguments(
107                 OUString::createFromAscii("com.sun.star.configuration.ConfigurationUpdateAccess"),
108                 sequenceFromVector(lParams));
109         return xCFG;
110     }
111 
readRelativeKey(const Reference<XInterface> xCFG,const OUString & sRelPath,const OUString & sKey)112     Any MyConfigurationHelper::readRelativeKey(
113         const Reference<XInterface> xCFG,
114         const OUString& sRelPath,
115         const OUString& sKey)
116     {
117         Reference<css::container::XHierarchicalNameAccess> xAccess(xCFG, UNO_QUERY_THROW);
118 
119         Reference<css::beans::XPropertySet> xProps;
120         xAccess->getByHierarchicalName(sRelPath) >>= xProps;
121         if (!xProps.is())
122             throw css::container::NoSuchElementException(
123                 noSuchElement(sRelPath),
124                 Reference<XInterface>());
125         return xProps->getPropertyValue(sKey);
126     }
127 
writeRelativeKey(const Reference<XInterface> xCFG,const OUString & sRelPath,const OUString & sKey,const Any & aValue)128     void MyConfigurationHelper::writeRelativeKey(
129         const Reference<XInterface> xCFG,
130         const OUString& sRelPath,
131         const OUString& sKey,
132         const Any& aValue)
133     {
134         Reference<css::container::XHierarchicalNameAccess> xAccess(xCFG, UNO_QUERY_THROW);
135 
136         Reference<css::beans::XPropertySet> xProps;
137         xAccess->getByHierarchicalName(sRelPath) >>= xProps;
138         if (!xProps.is())
139             throw css::container::NoSuchElementException(
140                 noSuchElement(sRelPath),
141                 Reference<XInterface>());
142         xProps->setPropertyValue(sKey, aValue);
143     }
144 
readDirectKey(const Reference<XMultiServiceFactory> xSMGR,const OUString & sPackage,const OUString & sRelPath,const OUString & sKey,sal_Int32 eMode)145     Any MyConfigurationHelper::readDirectKey(
146         const Reference<XMultiServiceFactory> xSMGR,
147         const OUString& sPackage,
148         const OUString& sRelPath,
149         const OUString& sKey,
150         sal_Int32 eMode)
151     {
152         Reference<XInterface> xCFG = MyConfigurationHelper::openConfig(xSMGR, sPackage, eMode);
153         return MyConfigurationHelper::readRelativeKey(xCFG, sRelPath, sKey);
154     }
155 
writeDirectKey(const Reference<XMultiServiceFactory> xSMGR,const OUString & sPackage,const OUString & sRelPath,const OUString & sKey,const Any & aValue,sal_Int32 eMode)156     void MyConfigurationHelper::writeDirectKey(
157         const Reference<XMultiServiceFactory> xSMGR,
158         const OUString& sPackage,
159         const OUString& sRelPath,
160         const OUString& sKey,
161         const Any& aValue,
162         sal_Int32 eMode)
163     {
164         Reference<XInterface> xCFG = MyConfigurationHelper::openConfig(xSMGR, sPackage, eMode);
165         MyConfigurationHelper::writeRelativeKey(xCFG, sRelPath, sKey, aValue);
166         MyConfigurationHelper::flush(xCFG);
167     }
168 
flush(const Reference<XInterface> & xCFG)169     void MyConfigurationHelper::flush(const Reference<XInterface>& xCFG)
170     {
171         Reference<css::util::XChangesBatch> xBatch(xCFG, UNO_QUERY_THROW);
172         xBatch->commitChanges();
173     }
174 }
175