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