merge the formfield patch from ooo-build
[ooovba.git] / extensions / source / oooimprovement / myconfigurationhelper.cxx
blob9a5b1569478bb14f17ce2ad7c3d8f37969bf46df
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: myconfigurationhelper.cxx,v $
10 * $Revision: 1.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
34 #include "myconfigurationhelper.hxx"
35 #include <com/sun/star/beans/XPropertySet.hpp>
36 #include <com/sun/star/container/XNameAccess.hpp>
37 #include <com/sun/star/container/XNameContainer.hpp>
38 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
39 #include <rtl/ustrbuf.hxx>
40 #include <vector>
43 namespace css = ::com::sun::star;
44 using namespace ::com::sun::star::lang;
45 using namespace ::com::sun::star::uno;
46 using ::rtl::OUString;
47 using ::rtl::OUStringBuffer;
48 using ::std::vector;
51 namespace
53 static const Sequence<Any> sequenceFromVector(const vector<Any>& vec)
55 Sequence<Any> result(vec.size());
56 for(size_t idx = 0; idx < vec.size(); ++idx)
57 result[idx] = vec[idx];
58 return result;
61 static const OUString noSuchElement(const OUString& path)
63 OUStringBuffer buf(256);
64 buf.appendAscii("The requested path \"");
65 buf.append(path);
66 buf.appendAscii("\" does not exists.");
67 return buf.makeStringAndClear();
71 namespace oooimprovement
73 Reference<XInterface> MyConfigurationHelper::openConfig(
74 const Reference<XMultiServiceFactory> xSMGR,
75 const OUString& sPackage,
76 sal_Int32 eMode)
78 Reference<XMultiServiceFactory> xConfigProvider(
79 xSMGR->createInstance(OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider")),
80 UNO_QUERY_THROW);
82 vector<Any> lParams;
83 css::beans::PropertyValue aParam;
85 // set root path
86 aParam.Name = OUString::createFromAscii("nodepath");
87 aParam.Value <<= sPackage;
88 lParams.push_back(makeAny(aParam));
90 // enable all locales mode
91 if ((eMode & MyConfigurationHelper::E_ALL_LOCALES)==MyConfigurationHelper::E_ALL_LOCALES)
93 aParam.Name = OUString::createFromAscii("locale");
94 aParam.Value <<= OUString::createFromAscii("*");
95 lParams.push_back(makeAny(aParam));
98 // enable lazy writing
99 sal_Bool bLazy = ((eMode & MyConfigurationHelper::E_LAZY_WRITE)==MyConfigurationHelper::E_LAZY_WRITE);
100 aParam.Name = OUString::createFromAscii("lazywrite");
101 aParam.Value = makeAny(bLazy);
102 lParams.push_back(makeAny(aParam));
104 // open it
105 Reference<XInterface> xCFG;
107 sal_Bool bReadOnly = ((eMode & MyConfigurationHelper::E_READONLY)==MyConfigurationHelper::E_READONLY);
108 if (bReadOnly)
109 xCFG = xConfigProvider->createInstanceWithArguments(
110 OUString::createFromAscii("com.sun.star.configuration.ConfigurationAccess"),
111 sequenceFromVector(lParams));
112 else
113 xCFG = xConfigProvider->createInstanceWithArguments(
114 OUString::createFromAscii("com.sun.star.configuration.ConfigurationUpdateAccess"),
115 sequenceFromVector(lParams));
116 return xCFG;
119 Any MyConfigurationHelper::readRelativeKey(
120 const Reference<XInterface> xCFG,
121 const OUString& sRelPath,
122 const OUString& sKey)
124 Reference<css::container::XHierarchicalNameAccess> xAccess(xCFG, UNO_QUERY_THROW);
126 Reference<css::beans::XPropertySet> xProps;
127 xAccess->getByHierarchicalName(sRelPath) >>= xProps;
128 if (!xProps.is())
129 throw css::container::NoSuchElementException(
130 noSuchElement(sRelPath),
131 Reference<XInterface>());
132 return xProps->getPropertyValue(sKey);
135 void MyConfigurationHelper::writeRelativeKey(
136 const Reference<XInterface> xCFG,
137 const OUString& sRelPath,
138 const OUString& sKey,
139 const Any& aValue)
141 Reference<css::container::XHierarchicalNameAccess> xAccess(xCFG, UNO_QUERY_THROW);
143 Reference<css::beans::XPropertySet> xProps;
144 xAccess->getByHierarchicalName(sRelPath) >>= xProps;
145 if (!xProps.is())
146 throw css::container::NoSuchElementException(
147 noSuchElement(sRelPath),
148 Reference<XInterface>());
149 xProps->setPropertyValue(sKey, aValue);
152 Any MyConfigurationHelper::readDirectKey(
153 const Reference<XMultiServiceFactory> xSMGR,
154 const OUString& sPackage,
155 const OUString& sRelPath,
156 const OUString& sKey,
157 sal_Int32 eMode)
159 Reference<XInterface> xCFG = MyConfigurationHelper::openConfig(xSMGR, sPackage, eMode);
160 return MyConfigurationHelper::readRelativeKey(xCFG, sRelPath, sKey);
163 void MyConfigurationHelper::writeDirectKey(
164 const Reference<XMultiServiceFactory> xSMGR,
165 const OUString& sPackage,
166 const OUString& sRelPath,
167 const OUString& sKey,
168 const Any& aValue,
169 sal_Int32 eMode)
171 Reference<XInterface> xCFG = MyConfigurationHelper::openConfig(xSMGR, sPackage, eMode);
172 MyConfigurationHelper::writeRelativeKey(xCFG, sRelPath, sKey, aValue);
173 MyConfigurationHelper::flush(xCFG);
176 void MyConfigurationHelper::flush(const Reference<XInterface>& xCFG)
178 Reference<css::util::XChangesBatch> xBatch(xCFG, UNO_QUERY_THROW);
179 xBatch->commitChanges();