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