LCOV - code coverage report
Current view: top level - sd/source/ui/framework/configuration - ConfigurationControllerBroadcaster.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 62 74 83.8 %
Date: 2014-04-11 Functions: 7 7 100.0 %
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 "ConfigurationControllerBroadcaster.hxx"
      22             : #include <com/sun/star/lang/IllegalArgumentException.hpp>
      23             : #include <com/sun/star/lang/DisposedException.hpp>
      24             : #include <tools/debug.hxx>
      25             : #include <tools/diagnose_ex.h>
      26             : 
      27             : using namespace ::com::sun::star;
      28             : using namespace ::com::sun::star::uno;
      29             : using namespace ::com::sun::star::drawing::framework;
      30             : 
      31             : namespace sd { namespace framework {
      32             : 
      33          73 : ConfigurationControllerBroadcaster::ConfigurationControllerBroadcaster (
      34             :     const Reference<XConfigurationController>& rxController)
      35             :     : mxConfigurationController(rxController),
      36          73 :     maListenerMap()
      37             : {
      38          73 : }
      39             : 
      40             : 
      41             : 
      42             : 
      43        1190 : void ConfigurationControllerBroadcaster::AddListener(
      44             :     const Reference<XConfigurationChangeListener>& rxListener,
      45             :     const OUString& rsEventType,
      46             :     const Any& rUserData)
      47             : {
      48        1190 :     if ( ! rxListener.is())
      49             :         throw lang::IllegalArgumentException("invalid listener",
      50             :             mxConfigurationController,
      51           0 :             0);
      52             : 
      53        1190 :     if (maListenerMap.find(rsEventType) == maListenerMap.end())
      54         438 :         maListenerMap[rsEventType] = ListenerList();
      55        1190 :     ListenerDescriptor aDescriptor;
      56        1190 :     aDescriptor.mxListener = rxListener;
      57        1190 :     aDescriptor.maUserData = rUserData;
      58        1190 :     maListenerMap[rsEventType].push_back(aDescriptor);
      59        1190 : }
      60             : 
      61             : 
      62             : 
      63             : 
      64         522 : void ConfigurationControllerBroadcaster::RemoveListener(
      65             :     const Reference<XConfigurationChangeListener>& rxListener)
      66             : {
      67         522 :     if ( ! rxListener.is())
      68             :         throw lang::IllegalArgumentException("invalid listener",
      69             :             mxConfigurationController,
      70           0 :             0);
      71             : 
      72         522 :     ListenerMap::iterator iMap;
      73         522 :     ListenerList::iterator iList;
      74        2725 :     for (iMap=maListenerMap.begin(); iMap!=maListenerMap.end(); ++iMap)
      75             :     {
      76        5057 :         for (iList=iMap->second.begin(); iList!=iMap->second.end(); ++iList)
      77             :         {
      78        4044 :             if (iList->mxListener == rxListener)
      79             :             {
      80        1190 :                 iMap->second.erase(iList);
      81        1190 :                 break;
      82             :             }
      83             :         }
      84             :     }
      85         522 : }
      86             : 
      87             : 
      88             : 
      89             : 
      90        1938 : void ConfigurationControllerBroadcaster::NotifyListeners (
      91             :     const ListenerList& rList,
      92             :     const ConfigurationChangeEvent& rEvent)
      93             : {
      94             :     // Create a local copy of the event in which the user data is modified
      95             :     // for every listener.
      96        1938 :     ConfigurationChangeEvent aEvent (rEvent);
      97             : 
      98        1938 :     ListenerList::const_iterator iListener;
      99        7183 :     for (iListener=rList.begin(); iListener!=rList.end(); ++iListener)
     100             :     {
     101             :         try
     102             :         {
     103        5245 :             aEvent.UserData = iListener->maUserData;
     104        5245 :             iListener->mxListener->notifyConfigurationChange(aEvent);
     105             :         }
     106           0 :         catch (const lang::DisposedException& rException)
     107             :         {
     108             :             // When the exception comes from the listener itself then
     109             :             // unregister it.
     110           0 :             if (rException.Context == iListener->mxListener)
     111           0 :                 RemoveListener(iListener->mxListener);
     112             :         }
     113           0 :         catch (const RuntimeException&)
     114             :         {
     115             :             DBG_UNHANDLED_EXCEPTION();
     116             :         }
     117        1938 :     }
     118        1938 : }
     119             : 
     120             : 
     121             : 
     122             : 
     123        2300 : void ConfigurationControllerBroadcaster::NotifyListeners (const ConfigurationChangeEvent& rEvent)
     124             : {
     125             :     // Notify the specialized listeners.
     126        2300 :     ListenerMap::const_iterator iMap (maListenerMap.find(rEvent.Type));
     127        2300 :     if (iMap != maListenerMap.end())
     128             :     {
     129             :         // Create a local list of the listeners to avoid problems with
     130             :         // concurrent changes and to be able to remove disposed listeners.
     131        1938 :         ListenerList aList (iMap->second.begin(), iMap->second.end());
     132        1938 :         NotifyListeners(aList,rEvent);
     133             :     }
     134             : 
     135             :     // Notify the universal listeners.
     136        2300 :     iMap = maListenerMap.find(OUString());
     137        2300 :     if (iMap != maListenerMap.end())
     138             :     {
     139             :         // Create a local list of the listeners to avoid problems with
     140             :         // concurrent changes and to be able to remove disposed listeners.
     141           0 :         ListenerList aList (iMap->second.begin(), iMap->second.end());
     142           0 :         NotifyListeners(aList,rEvent);
     143             :     }
     144        2300 : }
     145             : 
     146             : 
     147             : 
     148             : 
     149        1086 : void ConfigurationControllerBroadcaster::NotifyListeners (
     150             :     const OUString& rsEventType,
     151             :     const Reference<XResourceId>& rxResourceId,
     152             :     const Reference<XResource>& rxResourceObject)
     153             : {
     154        1086 :     ConfigurationChangeEvent aEvent;
     155        1086 :     aEvent.Type = rsEventType;
     156        1086 :     aEvent.ResourceId = rxResourceId;
     157        1086 :     aEvent.ResourceObject = rxResourceObject;
     158             :     try
     159             :     {
     160        1086 :         NotifyListeners(aEvent);
     161             :     }
     162           0 :     catch (const lang::DisposedException&)
     163             :     {
     164        1086 :     }
     165        1086 : }
     166             : 
     167             : 
     168             : 
     169             : 
     170             : 
     171          73 : void ConfigurationControllerBroadcaster::DisposeAndClear (void)
     172             : {
     173          73 :     lang::EventObject aEvent;
     174          73 :     aEvent.Source = mxConfigurationController;
     175        1085 :     while (!maListenerMap.empty())
     176             :     {
     177         939 :         ListenerMap::iterator iMap (maListenerMap.begin());
     178         939 :         if (iMap == maListenerMap.end())
     179           0 :             break;
     180             : 
     181             :         // When the first vector is empty then remove it from the map.
     182         939 :         if (iMap->second.empty())
     183             :         {
     184         438 :             maListenerMap.erase(iMap);
     185         876 :             continue;
     186             :         }
     187             :         else
     188             :         {
     189             :             Reference<lang::XEventListener> xListener (
     190         501 :                 iMap->second.front().mxListener, UNO_QUERY);
     191         501 :             if (xListener.is())
     192             :             {
     193             :                 // Tell the listener that the configuration controller is
     194             :                 // being disposed and remove the listener (for all event
     195             :                 // types).
     196             :                 try
     197             :                 {
     198         501 :                     RemoveListener(iMap->second.front().mxListener);
     199         501 :                     xListener->disposing(aEvent);
     200             :                 }
     201           0 :                 catch (const RuntimeException&)
     202             :                 {
     203             :                     DBG_UNHANDLED_EXCEPTION();
     204             :                 }
     205             :             }
     206             :             else
     207             :             {
     208             :                 // Remove just this reference to the listener.
     209           0 :                 iMap->second.erase(iMap->second.begin());
     210         501 :             }
     211             :         }
     212          73 :     }
     213          73 : }
     214             : 
     215             : 
     216             : 
     217             : 
     218             : } } // end of namespace sd::framework
     219             : 
     220             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10