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

Generated by: LCOV version 1.10