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 #include "precompiled_sd.hxx" 29 30 #include "tools/ConfigurationAccess.hxx" 31 #include <com/sun/star/lang/XMultiServiceFactory.hpp> 32 #include <com/sun/star/beans/PropertyValue.hpp> 33 #include <com/sun/star/container/XHierarchicalNameAccess.hpp> 34 #include <com/sun/star/util/XChangesBatch.hpp> 35 #include <comphelper/processfactory.hxx> 36 #include <tools/diagnose_ex.h> 37 38 using namespace ::com::sun::star; 39 using namespace ::com::sun::star::uno; 40 using ::rtl::OUString; 41 42 namespace sd { namespace tools { 43 44 ConfigurationAccess::ConfigurationAccess ( 45 const Reference<XComponentContext>& rxContext, 46 const OUString& rsRootName, 47 const WriteMode eMode) 48 : mxRoot() 49 { 50 Reference<lang::XMultiComponentFactory> xFactory (rxContext->getServiceManager()); 51 if (xFactory.is()) 52 { 53 Reference<lang::XMultiServiceFactory> xProvider ( 54 xFactory->createInstanceWithContext( 55 OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider"), 56 rxContext), 57 UNO_QUERY); 58 if (xProvider.is()) 59 Initialize(xProvider, rsRootName, eMode); 60 } 61 } 62 63 64 65 66 ConfigurationAccess::ConfigurationAccess ( 67 const OUString& rsRootName, 68 const WriteMode eMode) 69 : mxRoot() 70 { 71 Reference<lang::XMultiServiceFactory> xProvider ( 72 ::comphelper::getProcessServiceFactory()->createInstance( 73 OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider")), 74 UNO_QUERY); 75 if (xProvider.is()) 76 Initialize(xProvider, rsRootName, eMode); 77 } 78 79 80 81 82 void ConfigurationAccess::Initialize ( 83 const Reference<lang::XMultiServiceFactory>& rxProvider, 84 const OUString& rsRootName, 85 const WriteMode eMode) 86 { 87 try 88 { 89 Sequence<Any> aCreationArguments(3); 90 aCreationArguments[0] = makeAny(beans::PropertyValue( 91 OUString(RTL_CONSTASCII_USTRINGPARAM("nodepath")), 92 0, 93 makeAny(rsRootName), 94 beans::PropertyState_DIRECT_VALUE)); 95 aCreationArguments[1] = makeAny(beans::PropertyValue( 96 OUString(RTL_CONSTASCII_USTRINGPARAM("depth")), 97 0, 98 makeAny((sal_Int32)-1), 99 beans::PropertyState_DIRECT_VALUE)); 100 aCreationArguments[2] = makeAny(beans::PropertyValue( 101 OUString(RTL_CONSTASCII_USTRINGPARAM("lazywrite")), 102 0, 103 makeAny(true), 104 beans::PropertyState_DIRECT_VALUE)); 105 OUString sAccessService; 106 if (eMode == READ_ONLY) 107 sAccessService = OUString(RTL_CONSTASCII_USTRINGPARAM( 108 "com.sun.star.configuration.ConfigurationAccess")); 109 else 110 sAccessService = OUString(RTL_CONSTASCII_USTRINGPARAM( 111 "com.sun.star.configuration.ConfigurationUpdateAccess")); 112 113 mxRoot = rxProvider->createInstanceWithArguments( 114 sAccessService, 115 aCreationArguments); 116 } 117 catch (Exception&) 118 { 119 DBG_UNHANDLED_EXCEPTION(); 120 } 121 } 122 123 124 125 126 Any ConfigurationAccess::GetConfigurationNode ( 127 const OUString& sPathToNode) 128 { 129 return GetConfigurationNode( 130 Reference<container::XHierarchicalNameAccess>(mxRoot, UNO_QUERY), 131 sPathToNode); 132 } 133 134 135 136 137 Any ConfigurationAccess::GetConfigurationNode ( 138 const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode, 139 const OUString& sPathToNode) 140 { 141 if (sPathToNode.getLength() == 0) 142 return Any(rxNode); 143 144 try 145 { 146 if (rxNode.is()) 147 { 148 return rxNode->getByHierarchicalName(sPathToNode); 149 } 150 } 151 catch (Exception& rException) 152 { 153 OSL_TRACE ("caught exception while getting configuration node %s: %s", 154 ::rtl::OUStringToOString(sPathToNode, RTL_TEXTENCODING_UTF8).getStr(), 155 ::rtl::OUStringToOString(rException.Message, RTL_TEXTENCODING_UTF8).getStr()); 156 } 157 158 return Any(); 159 } 160 161 162 163 164 void ConfigurationAccess::CommitChanges (void) 165 { 166 Reference<util::XChangesBatch> xConfiguration (mxRoot, UNO_QUERY); 167 if (xConfiguration.is()) 168 xConfiguration->commitChanges(); 169 } 170 171 172 173 174 void ConfigurationAccess::ForAll ( 175 const Reference<container::XNameAccess>& rxContainer, 176 const ::std::vector<OUString>& rArguments, 177 const Functor& rFunctor) 178 { 179 if (rxContainer.is()) 180 { 181 ::std::vector<Any> aValues(rArguments.size()); 182 Sequence<OUString> aKeys (rxContainer->getElementNames()); 183 for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex) 184 { 185 const OUString& rsKey (aKeys[nItemIndex]); 186 Reference<container::XNameAccess> xSetItem (rxContainer->getByName(rsKey), UNO_QUERY); 187 if (xSetItem.is()) 188 { 189 // Get from the current item of the container the children 190 // that match the names in the rArguments list. 191 for (sal_uInt32 nValueIndex=0; nValueIndex<aValues.size(); ++nValueIndex) 192 aValues[nValueIndex] = xSetItem->getByName(rArguments[nValueIndex]); 193 } 194 rFunctor(rsKey, aValues); 195 } 196 } 197 } 198 199 200 201 202 void ConfigurationAccess::FillList( 203 const Reference<container::XNameAccess>& rxContainer, 204 const ::rtl::OUString& rsArgument, 205 ::std::vector<OUString>& rList) 206 { 207 try 208 { 209 if (rxContainer.is()) 210 { 211 Sequence<OUString> aKeys (rxContainer->getElementNames()); 212 rList.resize(aKeys.getLength()); 213 for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex) 214 { 215 Reference<container::XNameAccess> xSetItem ( 216 rxContainer->getByName(aKeys[nItemIndex]), UNO_QUERY); 217 if (xSetItem.is()) 218 { 219 xSetItem->getByName(rsArgument) >>= rList[nItemIndex]; 220 } 221 } 222 } 223 } 224 catch (RuntimeException&) 225 {} 226 } 227 228 229 } } // end of namespace sd::tools 230 231