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

Generated by: LCOV version 1.10