LCOV - code coverage report
Current view: top level - libreoffice/configmgr/source - rootaccess.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 105 148 70.9 %
Date: 2012-12-27 Functions: 21 29 72.4 %
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        9106 : RootAccess::RootAccess(
      61             :     Components & components, OUString const & pathRepresentation,
      62             :     OUString const & locale, bool update):
      63             :     Access(components), pathRepresentation_(pathRepresentation),
      64        9106 :     locale_(locale), update_(update), finalized_(false), alive_(true)
      65             : {
      66        9106 :     lock_ = lock();
      67        9106 : }
      68             : 
      69      121154 : Path RootAccess::getAbsolutePath() {
      70      121154 :     getNode();
      71      121154 :     return path_;
      72             : }
      73             : 
      74        8741 : void RootAccess::initBroadcaster(
      75             :     Modifications::Node const & modifications, Broadcaster * broadcaster)
      76             : {
      77             :     assert(broadcaster != 0);
      78        8741 :     comphelper::SequenceAsVector< css::util::ElementChange > changes;
      79             :     initBroadcasterAndChanges(
      80        8741 :         modifications, broadcaster, changesListeners_.empty() ? 0 : &changes);
      81        8741 :     if (!changes.empty()) {
      82         100 :         css::util::ChangesSet set(changes.getAsConstList());
      83         600 :         for (ChangesListeners::iterator i(changesListeners_.begin());
      84         400 :              i != changesListeners_.end(); ++i)
      85             :         {
      86         100 :             cppu::OWeakObject* pSource = static_cast< cppu::OWeakObject * >(this);
      87         100 :             css::uno::Reference< css::uno::XInterface > xBase( pSource, css::uno::UNO_QUERY );
      88             :             broadcaster->addChangesNotification(
      89         100 :                 *i,
      90             :                 css::util::ChangesEvent(
      91         200 :                     pSource, makeAny( xBase ), set));
      92         200 :         }
      93        8741 :     }
      94        8741 : }
      95             : 
      96     1237863 : void RootAccess::acquire() throw () {
      97     1237863 :     Access::acquire();
      98     1237863 : }
      99             : 
     100     1236936 : void RootAccess::release() throw () {
     101     1236936 :     Access::release();
     102     1236936 : }
     103             : 
     104           0 : OUString RootAccess::getAbsolutePathRepresentation() {
     105           0 :     getNode(); // turn pathRepresentation_ into canonic form
     106           0 :     return pathRepresentation_;
     107             : }
     108             : 
     109       18828 : OUString RootAccess::getLocale() const {
     110       18828 :     return locale_;
     111             : }
     112             : 
     113       16629 : bool RootAccess::isUpdate() const {
     114       16629 :     return update_;
     115             : }
     116             : 
     117         767 : void RootAccess::setAlive(bool b) {
     118         767 :     alive_ = b;
     119         767 : }
     120             : 
     121         803 : 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         803 :         osl::MutexGuard g(*lock_);
     128         803 :         checkLocalizedPropertyAccess();
     129         803 :         if (!aListener.is()) {
     130             :             throw css::uno::RuntimeException(
     131             :                 OUString("null listener"),
     132           0 :                 static_cast< cppu::OWeakObject * >(this));
     133             :         }
     134         803 :         if (!isDisposed()) {
     135         803 :             changesListeners_.insert(aListener);
     136         803 :             return;
     137         803 :         }
     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         404 : void RootAccess::removeChangesListener(
     146             :     css::uno::Reference< css::util::XChangesListener > const & aListener)
     147             :     throw (css::uno::RuntimeException)
     148             : {
     149             :     assert(thisIs(IS_ANY));
     150         404 :     osl::MutexGuard g(*lock_);
     151         404 :     checkLocalizedPropertyAccess();
     152         404 :     ChangesListeners::iterator i(changesListeners_.find(aListener));
     153         404 :     if (i != changesListeners_.end()) {
     154         404 :         changesListeners_.erase(i);
     155         404 :     }
     156         404 : }
     157             : 
     158        1211 : void RootAccess::commitChanges()
     159             :     throw (css::lang::WrappedTargetException, css::uno::RuntimeException)
     160             : {
     161             :     assert(thisIs(IS_UPDATE));
     162        1211 :     if (!alive_)
     163             :     {
     164        1211 :         return;
     165             :     }
     166        1211 :     Broadcaster bc;
     167             :     {
     168        1211 :         osl::MutexGuard g(*lock_);
     169             : 
     170        1211 :         checkLocalizedPropertyAccess();
     171             :         int finalizedLayer;
     172        1211 :         Modifications globalMods;
     173             :         commitChildChanges(
     174        1211 :             ((getComponents().resolvePathRepresentation(
     175             :                   pathRepresentation_, 0, 0, &finalizedLayer)
     176        4844 :               == node_) &&
     177             :              finalizedLayer == Data::NO_LAYER),
     178        3633 :             &globalMods);
     179        1211 :         getComponents().writeModifications();
     180        1211 :         getComponents().initGlobalBroadcaster(globalMods, this, &bc);
     181             :     }
     182        1211 :     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       25017 : RootAccess::~RootAccess()
     207             : {
     208        8339 :     osl::MutexGuard g(*lock_);
     209        8339 :     if (alive_)
     210        8339 :         getComponents().removeRootAccess(this);
     211       16678 : }
     212             : 
     213        5110 : Path RootAccess::getRelativePath() {
     214        5110 :     return Path();
     215             : }
     216             : 
     217         126 : OUString RootAccess::getRelativePathRepresentation() {
     218         126 :     return OUString();
     219             : }
     220             : 
     221      393189 : rtl::Reference< Node > RootAccess::getNode() {
     222      393189 :     if (!node_.is()) {
     223        8711 :         OUString canonic;
     224             :         int finalizedLayer;
     225        8711 :         node_ = getComponents().resolvePathRepresentation(
     226       17422 :             pathRepresentation_, &canonic, &path_, &finalizedLayer);
     227        8711 :         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        8711 :         pathRepresentation_ = canonic;
     239             :         assert(!path_.empty() || node_->kind() == Node::KIND_ROOT);
     240        8711 :         if (!path_.empty()) {
     241        8292 :             name_ = path_.back();
     242             :         }
     243        8711 :         finalized_ = finalizedLayer != Data::NO_LAYER;
     244             :     }
     245      393189 :     return node_;
     246             : }
     247             : 
     248       16077 : bool RootAccess::isFinalized() {
     249       16077 :     getNode();
     250       16077 :     return finalized_;
     251             : }
     252             : 
     253           0 : OUString RootAccess::getNameInternal() {
     254           0 :     getNode();
     255           0 :     return name_;
     256             : }
     257             : 
     258       73816 : rtl::Reference< RootAccess > RootAccess::getRootAccess() {
     259       73816 :     return this;
     260             : }
     261             : 
     262       10220 : rtl::Reference< Access > RootAccess::getParentAccess() {
     263       10220 :     return rtl::Reference< Access >();
     264             : }
     265             : 
     266           0 : void RootAccess::addTypes(std::vector< css::uno::Type > * types) const {
     267             :     assert(types != 0);
     268           0 :     types->push_back(cppu::UnoType< css::util::XChangesNotifier >::get());
     269           0 :     types->push_back(cppu::UnoType< css::util::XChangesBatch >::get());
     270           0 : }
     271             : 
     272          34 : void RootAccess::addSupportedServiceNames(
     273             :     std::vector< OUString > * services)
     274             : {
     275             :     assert(services != 0);
     276             :     services->push_back(
     277          34 :         OUString("com.sun.star.configuration.AccessRootElement"));
     278          34 :     if (update_) {
     279             :         services->push_back(
     280          34 :             OUString("com.sun.star.configuration.UpdateRootElement"));
     281             :     }
     282          34 : }
     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       29792 : css::uno::Any RootAccess::queryInterface(css::uno::Type const & aType)
     302             :     throw (css::uno::RuntimeException)
     303             : {
     304             :     assert(thisIs(IS_ANY));
     305       29792 :     osl::MutexGuard g(*lock_);
     306       29792 :     checkLocalizedPropertyAccess();
     307       29792 :     css::uno::Any res(Access::queryInterface(aType));
     308       29792 :     if (res.hasValue()) {
     309             :         return res;
     310             :     }
     311             :     res = cppu::queryInterface(
     312        3627 :         aType, static_cast< css::util::XChangesNotifier * >(this));
     313        3627 :     if (res.hasValue()) {
     314             :         return res;
     315             :     }
     316        1915 :     if (!res.hasValue() && update_) {
     317             :         res = cppu::queryInterface(
     318        1665 :             aType, static_cast< css::util::XChangesBatch * >(this));
     319             :     }
     320       29792 :     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