LCOV - code coverage report
Current view: top level - libreoffice/configmgr/source - rootaccess.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 109 148 73.6 %
Date: 2012-12-17 Functions: 22 29 75.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "sal/config.h"
      21             : 
      22             : #include <cassert>
      23             : #include <vector>
      24             : 
      25             : #include "com/sun/star/lang/DisposedException.hpp"
      26             : #include "com/sun/star/lang/EventObject.hpp"
      27             : #include "com/sun/star/lang/WrappedTargetException.hpp"
      28             : #include "com/sun/star/uno/Any.hxx"
      29             : #include "com/sun/star/uno/Reference.hxx"
      30             : #include "com/sun/star/uno/RuntimeException.hpp"
      31             : #include "com/sun/star/uno/Type.hxx"
      32             : #include "com/sun/star/uno/XInterface.hpp"
      33             : #include "com/sun/star/util/ChangesEvent.hpp"
      34             : #include "com/sun/star/util/ChangesSet.hpp"
      35             : #include "com/sun/star/util/ElementChange.hpp"
      36             : #include "com/sun/star/util/XChangesBatch.hpp"
      37             : #include "com/sun/star/util/XChangesListener.hpp"
      38             : #include "com/sun/star/util/XChangesNotifier.hpp"
      39             : #include "comphelper/sequenceasvector.hxx"
      40             : #include "cppu/unotype.hxx"
      41             : #include "cppuhelper/queryinterface.hxx"
      42             : #include "cppuhelper/weak.hxx"
      43             : #include "osl/mutex.hxx"
      44             : #include "rtl/ref.hxx"
      45             : #include "rtl/ustring.h"
      46             : #include "rtl/ustring.hxx"
      47             : 
      48             : #include "broadcaster.hxx"
      49             : #include "childaccess.hxx"
      50             : #include "components.hxx"
      51             : #include "data.hxx"
      52             : #include "lock.hxx"
      53             : #include "modifications.hxx"
      54             : #include "node.hxx"
      55             : #include "path.hxx"
      56             : #include "rootaccess.hxx"
      57             : 
      58             : namespace configmgr {
      59             : 
      60       29798 : RootAccess::RootAccess(
      61             :     Components & components, OUString const & pathRepresentation,
      62             :     OUString const & locale, bool update):
      63             :     Access(components), pathRepresentation_(pathRepresentation),
      64       29798 :     locale_(locale), update_(update), finalized_(false), alive_(true)
      65             : {
      66       29798 :     lock_ = lock();
      67       29798 : }
      68             : 
      69      259142 : Path RootAccess::getAbsolutePath() {
      70      259142 :     getNode();
      71      259142 :     return path_;
      72             : }
      73             : 
      74       21028 : void RootAccess::initBroadcaster(
      75             :     Modifications::Node const & modifications, Broadcaster * broadcaster)
      76             : {
      77             :     assert(broadcaster != 0);
      78       21028 :     comphelper::SequenceAsVector< css::util::ElementChange > changes;
      79             :     initBroadcasterAndChanges(
      80       21028 :         modifications, broadcaster, changesListeners_.empty() ? 0 : &changes);
      81       21028 :     if (!changes.empty()) {
      82         655 :         css::util::ChangesSet set(changes.getAsConstList());
      83        3930 :         for (ChangesListeners::iterator i(changesListeners_.begin());
      84        2620 :              i != changesListeners_.end(); ++i)
      85             :         {
      86         655 :             cppu::OWeakObject* pSource = static_cast< cppu::OWeakObject * >(this);
      87         655 :             css::uno::Reference< css::uno::XInterface > xBase( pSource, css::uno::UNO_QUERY );
      88             :             broadcaster->addChangesNotification(
      89         655 :                 *i,
      90             :                 css::util::ChangesEvent(
      91        1310 :                     pSource, makeAny( xBase ), set));
      92        1310 :         }
      93       21028 :     }
      94       21028 : }
      95             : 
      96     3496075 : void RootAccess::acquire() throw () {
      97     3496075 :     Access::acquire();
      98     3496075 : }
      99             : 
     100     3493077 : void RootAccess::release() throw () {
     101     3493077 :     Access::release();
     102     3493077 : }
     103             : 
     104           0 : OUString RootAccess::getAbsolutePathRepresentation() {
     105           0 :     getNode(); // turn pathRepresentation_ into canonic form
     106           0 :     return pathRepresentation_;
     107             : }
     108             : 
     109       80034 : OUString RootAccess::getLocale() const {
     110       80034 :     return locale_;
     111             : }
     112             : 
     113       51822 : bool RootAccess::isUpdate() const {
     114       51822 :     return update_;
     115             : }
     116             : 
     117        2468 : void RootAccess::setAlive(bool b) {
     118        2468 :     alive_ = b;
     119        2468 : }
     120             : 
     121        2701 : void RootAccess::addChangesListener(
     122             :     css::uno::Reference< css::util::XChangesListener > const & aListener)
     123             :     throw (css::uno::RuntimeException)
     124             : {
     125             :     assert(thisIs(IS_ANY));
     126             :     {
     127        2701 :         osl::MutexGuard g(*lock_);
     128        2701 :         checkLocalizedPropertyAccess();
     129        2701 :         if (!aListener.is()) {
     130             :             throw css::uno::RuntimeException(
     131             :                 OUString("null listener"),
     132           0 :                 static_cast< cppu::OWeakObject * >(this));
     133             :         }
     134        2701 :         if (!isDisposed()) {
     135        2701 :             changesListeners_.insert(aListener);
     136        2701 :             return;
     137        2701 :         }
     138             :     }
     139             :     try {
     140           0 :         aListener->disposing(
     141           0 :             css::lang::EventObject(static_cast< cppu::OWeakObject * >(this)));
     142           0 :     } catch (css::lang::DisposedException &) {}
     143             : }
     144             : 
     145        1281 : void RootAccess::removeChangesListener(
     146             :     css::uno::Reference< css::util::XChangesListener > const & aListener)
     147             :     throw (css::uno::RuntimeException)
     148             : {
     149             :     assert(thisIs(IS_ANY));
     150        1281 :     osl::MutexGuard g(*lock_);
     151        1281 :     checkLocalizedPropertyAccess();
     152        1281 :     ChangesListeners::iterator i(changesListeners_.find(aListener));
     153        1281 :     if (i != changesListeners_.end()) {
     154        1281 :         changesListeners_.erase(i);
     155        1281 :     }
     156        1281 : }
     157             : 
     158        2607 : void RootAccess::commitChanges()
     159             :     throw (css::lang::WrappedTargetException, css::uno::RuntimeException)
     160             : {
     161             :     assert(thisIs(IS_UPDATE));
     162        2607 :     if (!alive_)
     163             :     {
     164        2607 :         return;
     165             :     }
     166        2607 :     Broadcaster bc;
     167             :     {
     168        2607 :         osl::MutexGuard g(*lock_);
     169             : 
     170        2607 :         checkLocalizedPropertyAccess();
     171             :         int finalizedLayer;
     172        2607 :         Modifications globalMods;
     173             :         commitChildChanges(
     174        2607 :             ((getComponents().resolvePathRepresentation(
     175             :                   pathRepresentation_, 0, 0, &finalizedLayer)
     176       10428 :               == node_) &&
     177             :              finalizedLayer == Data::NO_LAYER),
     178        7821 :             &globalMods);
     179        2607 :         getComponents().writeModifications();
     180        2607 :         getComponents().initGlobalBroadcaster(globalMods, this, &bc);
     181             :     }
     182        2607 :     bc.send();
     183             : }
     184             : 
     185           0 : sal_Bool RootAccess::hasPendingChanges() throw (css::uno::RuntimeException) {
     186             :     assert(thisIs(IS_UPDATE));
     187           0 :     osl::MutexGuard g(*lock_);
     188           0 :     checkLocalizedPropertyAccess();
     189             :     //TODO: Optimize:
     190           0 :     std::vector< css::util::ElementChange > changes;
     191           0 :     reportChildChanges(&changes);
     192           0 :     return !changes.empty();
     193             : }
     194             : 
     195           0 : css::util::ChangesSet RootAccess::getPendingChanges()
     196             :     throw (css::uno::RuntimeException)
     197             : {
     198             :     assert(thisIs(IS_UPDATE));
     199           0 :     osl::MutexGuard g(*lock_);
     200           0 :     checkLocalizedPropertyAccess();
     201           0 :     comphelper::SequenceAsVector< css::util::ElementChange > changes;
     202           0 :     reportChildChanges(&changes);
     203           0 :     return changes.getAsConstList();
     204             : }
     205             : 
     206       81990 : RootAccess::~RootAccess()
     207             : {
     208       27330 :     osl::MutexGuard g(*lock_);
     209       27330 :     if (alive_)
     210       27330 :         getComponents().removeRootAccess(this);
     211       54660 : }
     212             : 
     213       13604 : Path RootAccess::getRelativePath() {
     214       13604 :     return Path();
     215             : }
     216             : 
     217        1113 : OUString RootAccess::getRelativePathRepresentation() {
     218        1113 :     return OUString();
     219             : }
     220             : 
     221     1060676 : rtl::Reference< Node > RootAccess::getNode() {
     222     1060676 :     if (!node_.is()) {
     223       28939 :         OUString canonic;
     224             :         int finalizedLayer;
     225       28939 :         node_ = getComponents().resolvePathRepresentation(
     226       57878 :             pathRepresentation_, &canonic, &path_, &finalizedLayer);
     227       28939 :         if (!node_.is()) {
     228             :             throw css::uno::RuntimeException(
     229             :                 (OUString("cannot find ") +
     230           0 :                  pathRepresentation_),
     231           0 :                 0);
     232             :                 // RootAccess::queryInterface indirectly calls
     233             :                 // RootAccess::getNode, so if this RootAccess were passed out in
     234             :                 // RuntimeException.Context, client code that called
     235             :                 // queryInterface on it would cause trouble; therefore,
     236             :                 // RuntimeException.Context is left null here
     237             :         }
     238       28939 :         pathRepresentation_ = canonic;
     239             :         assert(!path_.empty() || node_->kind() == Node::KIND_ROOT);
     240       28939 :         if (!path_.empty()) {
     241       27987 :             name_ = path_.back();
     242             :         }
     243       28939 :         finalized_ = finalizedLayer != Data::NO_LAYER;
     244             :     }
     245     1060676 :     return node_;
     246             : }
     247             : 
     248       40891 : bool RootAccess::isFinalized() {
     249       40891 :     getNode();
     250       40891 :     return finalized_;
     251             : }
     252             : 
     253           0 : OUString RootAccess::getNameInternal() {
     254           0 :     getNode();
     255           0 :     return name_;
     256             : }
     257             : 
     258      212170 : rtl::Reference< RootAccess > RootAccess::getRootAccess() {
     259      212170 :     return this;
     260             : }
     261             : 
     262       27208 : rtl::Reference< Access > RootAccess::getParentAccess() {
     263       27208 :     return rtl::Reference< Access >();
     264             : }
     265             : 
     266           3 : void RootAccess::addTypes(std::vector< css::uno::Type > * types) const {
     267             :     assert(types != 0);
     268           3 :     types->push_back(cppu::UnoType< css::util::XChangesNotifier >::get());
     269           3 :     types->push_back(cppu::UnoType< css::util::XChangesBatch >::get());
     270           3 : }
     271             : 
     272         160 : void RootAccess::addSupportedServiceNames(
     273             :     std::vector< OUString > * services)
     274             : {
     275             :     assert(services != 0);
     276             :     services->push_back(
     277         160 :         OUString("com.sun.star.configuration.AccessRootElement"));
     278         160 :     if (update_) {
     279             :         services->push_back(
     280         160 :             OUString("com.sun.star.configuration.UpdateRootElement"));
     281             :     }
     282         160 : }
     283             : 
     284           0 : void RootAccess::initDisposeBroadcaster(Broadcaster * broadcaster) {
     285             :     assert(broadcaster != 0);
     286           0 :     for (ChangesListeners::iterator i(changesListeners_.begin());
     287           0 :          i != changesListeners_.end(); ++i)
     288             :     {
     289             :         broadcaster->addDisposeNotification(
     290           0 :             i->get(),
     291           0 :             css::lang::EventObject(static_cast< cppu::OWeakObject * >(this)));
     292             :     }
     293           0 :     Access::initDisposeBroadcaster(broadcaster);
     294           0 : }
     295             : 
     296           0 : void RootAccess::clearListeners() throw() {
     297           0 :     changesListeners_.clear();
     298           0 :     Access::clearListeners();
     299           0 : }
     300             : 
     301       91153 : css::uno::Any RootAccess::queryInterface(css::uno::Type const & aType)
     302             :     throw (css::uno::RuntimeException)
     303             : {
     304             :     assert(thisIs(IS_ANY));
     305       91153 :     osl::MutexGuard g(*lock_);
     306       91153 :     checkLocalizedPropertyAccess();
     307       91153 :     css::uno::Any res(Access::queryInterface(aType));
     308       91153 :     if (res.hasValue()) {
     309             :         return res;
     310             :     }
     311             :     res = cppu::queryInterface(
     312       17253 :         aType, static_cast< css::util::XChangesNotifier * >(this));
     313       17253 :     if (res.hasValue()) {
     314             :         return res;
     315             :     }
     316       12089 :     if (!res.hasValue() && update_) {
     317             :         res = cppu::queryInterface(
     318       11435 :             aType, static_cast< css::util::XChangesBatch * >(this));
     319             :     }
     320       91153 :     return res;
     321             : }
     322             : 
     323           0 : OUString RootAccess::getImplementationName()
     324             :     throw (css::uno::RuntimeException)
     325             : {
     326             :     assert(thisIs(IS_ANY));
     327           0 :     osl::MutexGuard g(*lock_);
     328           0 :     checkLocalizedPropertyAccess();
     329           0 :     return OUString("configmgr.RootAccess");
     330             : }
     331             : 
     332             : }
     333             : 
     334             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10