LCOV - code coverage report
Current view: top level - sd/source/ui/framework/configuration - Configuration.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 99 132 75.0 %
Date: 2014-11-03 Functions: 21 24 87.5 %
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 "framework/Configuration.hxx"
      21             : 
      22             : #include "framework/FrameworkHelper.hxx"
      23             : 
      24             : #include <facreg.hxx>
      25             : 
      26             : using namespace ::com::sun::star;
      27             : using namespace ::com::sun::star::uno;
      28             : using namespace ::com::sun::star::drawing::framework;
      29             : using ::sd::framework::FrameworkHelper;
      30             : 
      31             : namespace {
      32             : /** Use the XResourceId::compareTo() method to implement a compare operator
      33             :     for STL containers.
      34             : */
      35             : class XResourceIdLess
      36             :     :   public ::std::binary_function <Reference<XResourceId>, Reference<XResourceId>, bool>
      37             : {
      38             : public:
      39       15366 :     bool operator () (const Reference<XResourceId>& rId1, const Reference<XResourceId>& rId2) const
      40             :     {
      41       15366 :         return rId1->compareTo(rId2) == -1;
      42             :     }
      43             : };
      44             : 
      45             : } // end of anonymous namespace
      46             : 
      47             : namespace sd { namespace framework {
      48             : 
      49       21106 : class Configuration::ResourceContainer
      50             :     : public ::std::set<Reference<XResourceId>, XResourceIdLess>
      51             : {
      52             : public:
      53         638 :     ResourceContainer (void) {}
      54             : };
      55             : 
      56             : //----- Service ---------------------------------------------------------------
      57             : 
      58           2 : Reference<XInterface> SAL_CALL Configuration_createInstance (
      59             :     const Reference<XComponentContext>& rxContext) throw (css::uno::Exception)
      60             : {
      61             :     (void)rxContext;
      62           2 :     return Reference<XInterface>(static_cast<XWeak*>(new Configuration(NULL,false)));
      63             : }
      64             : 
      65          36 : OUString Configuration_getImplementationName (void) throw(RuntimeException)
      66             : {
      67          36 :     return OUString("com.sun.star.comp.Draw.framework.configuration.Configuration");
      68             : }
      69             : 
      70           2 : Sequence<OUString> SAL_CALL Configuration_getSupportedServiceNames (void)
      71             :     throw (RuntimeException)
      72             : {
      73           2 :     static const OUString sServiceName("com.sun.star.drawing.framework.Configuration");
      74           2 :     return Sequence<OUString>(&sServiceName, 1);
      75             : }
      76             : 
      77             : //===== Configuration =========================================================
      78             : 
      79         638 : Configuration::Configuration (
      80             :     const Reference<XConfigurationControllerBroadcaster>& rxBroadcaster,
      81             :     bool bBroadcastRequestEvents)
      82             :     : ConfigurationInterfaceBase(MutexOwner::maMutex),
      83           0 :       mpResourceContainer(new ResourceContainer()),
      84             :       mxBroadcaster(rxBroadcaster),
      85         638 :       mbBroadcastRequestEvents(bBroadcastRequestEvents)
      86             : {
      87         638 : }
      88             : 
      89       10234 : Configuration::Configuration (
      90             :     const Reference<XConfigurationControllerBroadcaster>& rxBroadcaster,
      91             :     bool bBroadcastRequestEvents,
      92             :     const ResourceContainer& rResourceContainer)
      93             :     : ConfigurationInterfaceBase(MutexOwner::maMutex),
      94       10234 :       mpResourceContainer(new ResourceContainer(rResourceContainer)),
      95             :       mxBroadcaster(rxBroadcaster),
      96       20468 :       mbBroadcastRequestEvents(bBroadcastRequestEvents)
      97             : {
      98       10234 : }
      99             : 
     100       21744 : Configuration::~Configuration (void)
     101             : {
     102       21744 : }
     103             : 
     104       10872 : void SAL_CALL Configuration::disposing (void)
     105             : {
     106       10872 :     ::osl::MutexGuard aGuard (maMutex);
     107       10872 :     mpResourceContainer->clear();
     108       10872 :     mxBroadcaster = NULL;
     109       10872 : }
     110             : 
     111             : //----- XConfiguration --------------------------------------------------------
     112             : 
     113        1988 : void SAL_CALL Configuration::addResource (const Reference<XResourceId>& rxResourceId)
     114             :     throw (RuntimeException, std::exception)
     115             : {
     116        1988 :     ThrowIfDisposed();
     117             : 
     118        1988 :     if ( ! rxResourceId.is() || rxResourceId->getResourceURL().isEmpty())
     119           0 :         throw ::com::sun::star::lang::IllegalArgumentException();
     120             : 
     121        1988 :     if (mpResourceContainer->find(rxResourceId) == mpResourceContainer->end())
     122             :     {
     123             :         SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": Configuration::addResource() " <<
     124             :             OUStringToOString(
     125             :                 FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr());
     126        1944 :         mpResourceContainer->insert(rxResourceId);
     127        1944 :         PostEvent(rxResourceId, true);
     128             :     }
     129        1988 : }
     130             : 
     131        2524 : void SAL_CALL Configuration::removeResource (const Reference<XResourceId>& rxResourceId)
     132             :     throw (RuntimeException, std::exception)
     133             : {
     134        2524 :     ThrowIfDisposed();
     135             : 
     136        2524 :     if ( ! rxResourceId.is() || rxResourceId->getResourceURL().isEmpty())
     137           0 :         throw ::com::sun::star::lang::IllegalArgumentException();
     138             : 
     139        2524 :     ResourceContainer::iterator iResource (mpResourceContainer->find(rxResourceId));
     140        2524 :     if (iResource != mpResourceContainer->end())
     141             :     {
     142             :         SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": Configuration::removeResource() " <<
     143             :             OUStringToOString(
     144             :                 FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr());
     145        1944 :         PostEvent(rxResourceId,false);
     146        1944 :         mpResourceContainer->erase(iResource);
     147             :     }
     148        2524 : }
     149             : 
     150       21094 : Sequence<Reference<XResourceId> > SAL_CALL Configuration::getResources (
     151             :     const Reference<XResourceId>& rxAnchorId,
     152             :     const OUString& rsResourceURLPrefix,
     153             :     AnchorBindingMode eMode)
     154             :     throw (::com::sun::star::uno::RuntimeException, std::exception)
     155             : {
     156       21094 :     ::osl::MutexGuard aGuard (maMutex);
     157       21094 :     ThrowIfDisposed();
     158             : 
     159       21094 :     bool bFilterResources (!rsResourceURLPrefix.isEmpty());
     160             : 
     161             :     // Collect the matching resources in a vector.
     162       42188 :     ::std::vector<Reference<XResourceId> > aResources;
     163       21094 :     ResourceContainer::const_iterator iResource;
     164      324150 :     for (iResource=mpResourceContainer->begin();
     165      216100 :          iResource!=mpResourceContainer->end();
     166             :          ++iResource)
     167             :     {
     168       86956 :         if ( ! (*iResource)->isBoundTo(rxAnchorId,eMode))
     169       60842 :             continue;
     170             : 
     171       26114 :         if (bFilterResources)
     172             :         {
     173             :             // Apply the given resource prefix as filter.
     174             : 
     175             :             // Make sure that the resource is bound directly to the anchor.
     176       12954 :             if (eMode != AnchorBindingMode_DIRECT
     177       12954 :                 && ! (*iResource)->isBoundTo(rxAnchorId, AnchorBindingMode_DIRECT))
     178             :             {
     179           0 :                 continue;
     180             :             }
     181             : 
     182             :             // Make sure that the resource URL matches the given prefix.
     183       12954 :             if ( ! (*iResource)->getResourceURL().match(rsResourceURLPrefix))
     184             :             {
     185        2650 :                 continue;
     186             :             }
     187             :         }
     188             : 
     189       23464 :         aResources.push_back(*iResource);
     190             :     }
     191             : 
     192             :     // Copy the resources from the vector into a new sequence.
     193       21094 :     Sequence<Reference<XResourceId> > aResult (aResources.size());
     194       44558 :     for (sal_uInt32 nIndex=0; nIndex<aResources.size(); ++nIndex)
     195       23464 :         aResult[nIndex] = aResources[nIndex];
     196             : 
     197       42188 :     return aResult;
     198             : }
     199             : 
     200           0 : sal_Bool SAL_CALL Configuration::hasResource (const Reference<XResourceId>& rxResourceId)
     201             :     throw (RuntimeException, std::exception)
     202             : {
     203           0 :     ::osl::MutexGuard aGuard (maMutex);
     204           0 :     ThrowIfDisposed();
     205             : 
     206           0 :     return rxResourceId.is()
     207           0 :         && mpResourceContainer->find(rxResourceId) != mpResourceContainer->end();
     208             : }
     209             : 
     210             : //----- XCloneable ------------------------------------------------------------
     211             : 
     212       10234 : Reference<util::XCloneable> SAL_CALL Configuration::createClone (void)
     213             :     throw (RuntimeException, std::exception)
     214             : {
     215       10234 :     ::osl::MutexGuard aGuard (maMutex);
     216       10234 :     ThrowIfDisposed();
     217             : 
     218             :     Configuration* pConfiguration = new Configuration(
     219             :         mxBroadcaster,
     220             :         mbBroadcastRequestEvents,
     221       10234 :         *mpResourceContainer);
     222             : 
     223       10234 :     return Reference<util::XCloneable>(pConfiguration);
     224             : }
     225             : 
     226             : //----- XNamed ----------------------------------------------------------------
     227             : 
     228           0 : OUString SAL_CALL Configuration::getName (void)
     229             :     throw (RuntimeException, std::exception)
     230             : {
     231           0 :     ::osl::MutexGuard aGuard (maMutex);
     232           0 :     OUString aString;
     233             : 
     234           0 :     if (rBHelper.bDisposed || rBHelper.bInDispose)
     235           0 :         aString += "DISPOSED ";
     236           0 :     aString += "Configuration[";
     237             : 
     238           0 :     ResourceContainer::const_iterator iResource;
     239           0 :     for (iResource=mpResourceContainer->begin();
     240           0 :          iResource!=mpResourceContainer->end();
     241             :          ++iResource)
     242             :     {
     243           0 :         if (iResource != mpResourceContainer->begin())
     244           0 :             aString += ", ";
     245           0 :         aString += FrameworkHelper::ResourceIdToString(*iResource);
     246             :     }
     247           0 :     aString += "]";
     248             : 
     249           0 :     return aString;
     250             : }
     251             : 
     252           0 : void SAL_CALL Configuration::setName (const OUString& rsName)
     253             :     throw (RuntimeException, std::exception)
     254             : {
     255             :     (void)rsName; // rsName is ignored.
     256           0 : }
     257             : 
     258        3888 : void Configuration::PostEvent (
     259             :     const Reference<XResourceId>& rxResourceId,
     260             :     const bool bActivation)
     261             : {
     262             :     OSL_ASSERT(rxResourceId.is());
     263             : 
     264        3888 :     if (mxBroadcaster.is())
     265             :     {
     266        2008 :         ConfigurationChangeEvent aEvent;
     267        2008 :         aEvent.ResourceId = rxResourceId;
     268        2008 :         if (bActivation)
     269        1004 :             if (mbBroadcastRequestEvents)
     270        1004 :                 aEvent.Type = FrameworkHelper::msResourceActivationRequestEvent;
     271             :             else
     272           0 :                 aEvent.Type = FrameworkHelper::msResourceActivationEvent;
     273             :         else
     274        1004 :             if (mbBroadcastRequestEvents)
     275        1004 :                 aEvent.Type = FrameworkHelper::msResourceDeactivationRequestEvent;
     276             :             else
     277           0 :                 aEvent.Type = FrameworkHelper::msResourceDeactivationEvent;
     278        2008 :         aEvent.Configuration = this;
     279             : 
     280        2008 :         mxBroadcaster->notifyEvent(aEvent);
     281             :     }
     282        3888 : }
     283             : 
     284       35840 : void Configuration::ThrowIfDisposed (void) const
     285             :     throw (::com::sun::star::lang::DisposedException)
     286             : {
     287       35840 :     if (rBHelper.bDisposed || rBHelper.bInDispose)
     288             :     {
     289             :         throw lang::DisposedException ("Configuration object has already been disposed",
     290           0 :             const_cast<uno::XWeak*>(static_cast<const uno::XWeak*>(this)));
     291             :     }
     292       35840 : }
     293             : 
     294         696 : bool AreConfigurationsEquivalent (
     295             :     const Reference<XConfiguration>& rxConfiguration1,
     296             :     const Reference<XConfiguration>& rxConfiguration2)
     297             : {
     298         696 :     if (rxConfiguration1.is() != rxConfiguration2.is())
     299           0 :         return false;
     300         696 :     if ( ! rxConfiguration1.is() && ! rxConfiguration2.is())
     301           0 :         return true;
     302             : 
     303             :     // Get the lists of resources from the two given configurations.
     304             :     const Sequence<Reference<XResourceId> > aResources1(
     305         696 :         rxConfiguration1->getResources(
     306         696 :             NULL, OUString(), AnchorBindingMode_INDIRECT));
     307             :     const Sequence<Reference<XResourceId> > aResources2(
     308         696 :         rxConfiguration2->getResources(
     309        1392 :             NULL, OUString(), AnchorBindingMode_INDIRECT));
     310             : 
     311             :     // When the number of resources differ then the configurations can not
     312             :     // be equivalent.
     313         696 :     const sal_Int32 nCount (aResources1.getLength());
     314         696 :     const sal_Int32 nCount2 (aResources2.getLength());
     315         696 :     if (nCount != nCount2)
     316         290 :         return false;
     317             : 
     318             :     // Comparison of the two lists of resource ids relies on their
     319             :     // ordering.
     320        1182 :     for (sal_Int32 nIndex=0; nIndex<nCount; ++nIndex)
     321             :     {
     322         776 :         const Reference<XResourceId> xResource1 (aResources1[nIndex]);
     323        1552 :         const Reference<XResourceId> xResource2 (aResources2[nIndex]);
     324         776 :         if (xResource1.is() && xResource2.is())
     325             :         {
     326         776 :             if (xResource1->compareTo(xResource2) != 0)
     327           0 :                 return false;
     328             :         }
     329           0 :         else if (xResource1.is() != xResource2.is())
     330             :         {
     331           0 :             return false;
     332             :         }
     333         776 :     }
     334             : 
     335        1102 :     return true;
     336             : }
     337             : 
     338         114 : } } // end of namespace sd::framework
     339             : 
     340             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10