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_configmgr.hxx" 29 #include "sal/config.h" 30 31 #include <vector> 32 33 #include "com/sun/star/container/XChild.hpp" 34 #include "com/sun/star/lang/NoSupportException.hpp" 35 #include "com/sun/star/lang/XUnoTunnel.hpp" 36 #include "com/sun/star/uno/Any.hxx" 37 #include "com/sun/star/uno/Reference.hxx" 38 #include "com/sun/star/uno/RuntimeException.hpp" 39 #include "com/sun/star/uno/Sequence.hxx" 40 #include "com/sun/star/uno/Type.hxx" 41 #include "com/sun/star/uno/XInterface.hpp" 42 #include "cppu/unotype.hxx" 43 #include "cppuhelper/queryinterface.hxx" 44 #include "cppuhelper/weak.hxx" 45 #include "osl/diagnose.h" 46 #include "osl/mutex.hxx" 47 #include "rtl/ref.hxx" 48 #include "rtl/string.h" 49 #include "rtl/ustrbuf.hxx" 50 #include "rtl/ustring.h" 51 #include "rtl/ustring.hxx" 52 #include "rtl/uuid.h" 53 #include "sal/types.h" 54 55 #include "access.hxx" 56 #include "childaccess.hxx" 57 #include "components.hxx" 58 #include "data.hxx" 59 #include "groupnode.hxx" 60 #include "localizedpropertynode.hxx" 61 #include "localizedvaluenode.hxx" 62 #include "lock.hxx" 63 #include "modifications.hxx" 64 #include "node.hxx" 65 #include "path.hxx" 66 #include "propertynode.hxx" 67 #include "rootaccess.hxx" 68 #include "setnode.hxx" 69 #include "type.hxx" 70 71 namespace configmgr { 72 73 namespace { 74 75 namespace css = com::sun::star; 76 77 } 78 79 css::uno::Sequence< sal_Int8 > ChildAccess::getTunnelId() { 80 static css::uno::Sequence< sal_Int8 > id; 81 if (id.getLength() == 0) { 82 css::uno::Sequence< sal_Int8 > uuid(16); 83 rtl_createUuid( 84 reinterpret_cast< sal_uInt8 * >(uuid.getArray()), 0, false); 85 id = uuid; 86 } 87 return id; 88 } 89 90 ChildAccess::ChildAccess( 91 Components & components, rtl::Reference< RootAccess > const & root, 92 rtl::Reference< Access > const & parent, rtl::OUString const & name, 93 rtl::Reference< Node > const & node): 94 Access(components), root_(root), parent_(parent), name_(name), node_(node), 95 inTransaction_(false) 96 { 97 OSL_ASSERT(root.is() && parent.is() && node.is()); 98 } 99 100 ChildAccess::ChildAccess( 101 Components & components, rtl::Reference< RootAccess > const & root, 102 rtl::Reference< Node > const & node): 103 Access(components), root_(root), node_(node), inTransaction_(false) 104 { 105 OSL_ASSERT(root.is() && node.is()); 106 } 107 108 Path ChildAccess::getAbsolutePath() { 109 OSL_ASSERT(getParentAccess().is()); 110 Path path(getParentAccess()->getAbsolutePath()); 111 path.push_back(name_); 112 return path; 113 } 114 115 Path ChildAccess::getRelativePath() { 116 Path path; 117 rtl::Reference< Access > parent(getParentAccess()); 118 if (parent.is()) { 119 path = parent->getRelativePath(); 120 } 121 path.push_back(name_); 122 return path; 123 } 124 125 rtl::OUString ChildAccess::getRelativePathRepresentation() { 126 rtl::OUStringBuffer path; 127 rtl::Reference< Access > parent(getParentAccess()); 128 if (parent.is()) { 129 path.append(parent->getRelativePathRepresentation()); 130 if (path.getLength() != 0) { 131 path.append(sal_Unicode('/')); 132 } 133 } 134 path.append(Data::createSegment(node_->getTemplateName(), name_)); 135 return path.makeStringAndClear(); 136 } 137 138 rtl::Reference< Node > ChildAccess::getNode() { 139 return node_; 140 } 141 142 bool ChildAccess::isFinalized() { 143 return node_->getFinalized() != Data::NO_LAYER || 144 (parent_.is() && parent_->isFinalized()); 145 } 146 147 rtl::OUString ChildAccess::getNameInternal() { 148 return name_; 149 } 150 151 rtl::Reference< RootAccess > ChildAccess::getRootAccess() { 152 return root_; 153 } 154 155 rtl::Reference< Access > ChildAccess::getParentAccess() { 156 return parent_; 157 } 158 159 void ChildAccess::acquire() throw () { 160 Access::acquire(); 161 } 162 163 void ChildAccess::release() throw () { 164 Access::release(); 165 } 166 167 css::uno::Reference< css::uno::XInterface > ChildAccess::getParent() 168 throw (css::uno::RuntimeException) 169 { 170 OSL_ASSERT(thisIs(IS_ANY)); 171 osl::MutexGuard g(lock); 172 checkLocalizedPropertyAccess(); 173 return static_cast< cppu::OWeakObject * >(parent_.get()); 174 } 175 176 void ChildAccess::setParent(css::uno::Reference< css::uno::XInterface > const &) 177 throw (css::lang::NoSupportException, css::uno::RuntimeException) 178 { 179 OSL_ASSERT(thisIs(IS_ANY)); 180 osl::MutexGuard g(lock); 181 checkLocalizedPropertyAccess(); 182 throw css::lang::NoSupportException( 183 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("setParent")), 184 static_cast< cppu::OWeakObject * >(this)); 185 } 186 187 sal_Int64 ChildAccess::getSomething( 188 css::uno::Sequence< sal_Int8 > const & aIdentifier) 189 throw (css::uno::RuntimeException) 190 { 191 OSL_ASSERT(thisIs(IS_ANY)); 192 osl::MutexGuard g(lock); 193 checkLocalizedPropertyAccess(); 194 return aIdentifier == getTunnelId() 195 ? reinterpret_cast< sal_Int64 >(this) : 0; 196 } 197 198 void ChildAccess::bind( 199 rtl::Reference< RootAccess > const & root, 200 rtl::Reference< Access > const & parent, rtl::OUString const & name) 201 throw () 202 { 203 OSL_ASSERT( 204 !parent_.is() && root.is() && parent.is() && name.getLength() != 0); 205 root_ = root; 206 parent_ = parent; 207 name_ = name; 208 } 209 210 void ChildAccess::unbind() throw () { 211 OSL_ASSERT(parent_.is()); 212 parent_->releaseChild(name_); 213 parent_.clear(); 214 inTransaction_ = true; 215 } 216 217 void ChildAccess::committed() { 218 inTransaction_ = false; 219 } 220 221 void ChildAccess::setNode(rtl::Reference< Node > const & node) { 222 node_ = node; 223 } 224 225 void ChildAccess::setProperty( 226 css::uno::Any const & value, Modifications * localModifications) 227 { 228 OSL_ASSERT(localModifications != 0); 229 Type type = TYPE_ERROR; 230 bool nillable = false; 231 switch (node_->kind()) { 232 case Node::KIND_PROPERTY: 233 { 234 PropertyNode * prop = dynamic_cast< PropertyNode * >(node_.get()); 235 type = prop->getStaticType(); 236 nillable = prop->isNillable(); 237 } 238 break; 239 case Node::KIND_LOCALIZED_PROPERTY: 240 { 241 rtl::OUString locale(getRootAccess()->getLocale()); 242 if (!Components::allLocales(locale)) { 243 rtl::Reference< ChildAccess > child(getChild(locale)); 244 if (child.is()) { 245 child->setProperty(value, localModifications); 246 } else { 247 insertLocalizedValueChild( 248 locale, value, localModifications); 249 } 250 return; 251 } 252 } 253 break; 254 case Node::KIND_LOCALIZED_VALUE: 255 { 256 LocalizedPropertyNode * locprop = 257 dynamic_cast< LocalizedPropertyNode * >(getParentNode().get()); 258 type = locprop->getStaticType(); 259 nillable = locprop->isNillable(); 260 } 261 break; 262 default: 263 break; 264 } 265 checkValue(value, type, nillable); 266 getParentAccess()->markChildAsModified(this); 267 changedValue_.reset(new css::uno::Any(value)); 268 localModifications->add(getRelativePath()); 269 } 270 271 css::uno::Any ChildAccess::asValue() { 272 if (changedValue_.get() != 0) { 273 return *changedValue_; 274 } 275 switch (node_->kind()) { 276 case Node::KIND_PROPERTY: 277 return dynamic_cast< PropertyNode * >(node_.get())->getValue( 278 getComponents()); 279 case Node::KIND_LOCALIZED_PROPERTY: 280 { 281 rtl::OUString locale(getRootAccess()->getLocale()); 282 if (!Components::allLocales(locale)) { 283 // Find best match using an adaption of RFC 4647 lookup matching 284 // rules, removing "-" or "_" delimited segments from the end; 285 // defaults are the "en-US" locale, the "en" locale, the empty 286 // string locale, the first child (if any), or a nil value (even 287 // though it may be illegal for the given property), in that 288 // order: 289 rtl::Reference< ChildAccess > child; 290 for (;;) { 291 child = getChild(locale); 292 if (child.is() || locale.getLength() == 0) { 293 break; 294 } 295 sal_Int32 i = locale.getLength() - 1; 296 while (i > 0 && locale[i] != '-' && locale[i] != '_') { 297 --i; 298 } 299 if (i == 0) { 300 break; 301 } 302 locale = locale.copy(0, i); 303 } 304 if (!child.is()) { 305 child = getChild( 306 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("en-US"))); 307 if (!child.is()) { 308 child = getChild( 309 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("en"))); 310 if (!child.is()) { 311 child = getChild(rtl::OUString()); 312 if (!child.is()) { 313 std::vector< rtl::Reference< ChildAccess > > 314 all(getAllChildren()); 315 if (!all.empty()) { 316 child = all.front(); 317 } 318 } 319 } 320 } 321 } 322 return child.is() ? child->asValue() : css::uno::Any(); 323 } 324 } 325 break; 326 case Node::KIND_LOCALIZED_VALUE: 327 return dynamic_cast< LocalizedValueNode * >(node_.get())->getValue(); 328 default: 329 break; 330 } 331 return css::uno::makeAny( 332 css::uno::Reference< css::uno::XInterface >( 333 static_cast< cppu::OWeakObject * >(this))); 334 } 335 336 void ChildAccess::commitChanges(bool valid, Modifications * globalModifications) 337 { 338 OSL_ASSERT(globalModifications != 0); 339 commitChildChanges(valid, globalModifications); 340 if (valid && changedValue_.get() != 0) { 341 Path path(getAbsolutePath()); 342 getComponents().addModification(path); 343 globalModifications->add(path); 344 switch (node_->kind()) { 345 case Node::KIND_PROPERTY: 346 dynamic_cast< PropertyNode * >(node_.get())->setValue( 347 Data::NO_LAYER, *changedValue_); 348 break; 349 case Node::KIND_LOCALIZED_VALUE: 350 dynamic_cast< LocalizedValueNode * >(node_.get())->setValue( 351 Data::NO_LAYER, *changedValue_); 352 break; 353 default: 354 OSL_ASSERT(false); // this cannot happen 355 break; 356 } 357 } 358 changedValue_.reset(); 359 } 360 361 ChildAccess::~ChildAccess() { 362 osl::MutexGuard g(lock); 363 if (parent_.is()) { 364 parent_->releaseChild(name_); 365 } 366 } 367 368 void ChildAccess::addTypes(std::vector< css::uno::Type > * types) const { 369 OSL_ASSERT(types != 0); 370 types->push_back(cppu::UnoType< css::container::XChild >::get()); 371 types->push_back(cppu::UnoType< css::lang::XUnoTunnel >::get()); 372 } 373 374 void ChildAccess::addSupportedServiceNames( 375 std::vector< rtl::OUString > * services) 376 { 377 OSL_ASSERT(services != 0); 378 services->push_back( 379 getParentNode()->kind() == Node::KIND_GROUP 380 ? rtl::OUString( 381 RTL_CONSTASCII_USTRINGPARAM( 382 "com.sun.star.configuration.GroupElement")) 383 : rtl::OUString( 384 RTL_CONSTASCII_USTRINGPARAM( 385 "com.sun.star.configuration.SetElement"))); 386 } 387 388 css::uno::Any ChildAccess::queryInterface(css::uno::Type const & aType) 389 throw (css::uno::RuntimeException) 390 { 391 OSL_ASSERT(thisIs(IS_ANY)); 392 osl::MutexGuard g(lock); 393 checkLocalizedPropertyAccess(); 394 css::uno::Any res(Access::queryInterface(aType)); 395 return res.hasValue() 396 ? res 397 : cppu::queryInterface( 398 aType, static_cast< css::container::XChild * >(this), 399 static_cast< css::lang::XUnoTunnel * >(this)); 400 } 401 402 } 403