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