LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/sd/source/ui/toolpanel/controls - MasterPageObserver.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 87 114 76.3 %
Date: 2013-07-09 Functions: 17 21 81.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 "MasterPageObserver.hxx"
      22             : 
      23             : #include <algorithm>
      24             : #include "drawdoc.hxx"
      25             : #include "sdpage.hxx"
      26             : #include <boost/unordered_map.hpp>
      27             : #include <set>
      28             : #include <vector>
      29             : #include <svl/lstner.hxx>
      30             : #include <osl/doublecheckedlocking.h>
      31             : #include <osl/getglobalmutex.hxx>
      32             : 
      33             : 
      34             : namespace sd {
      35             : 
      36          12 : class MasterPageObserver::Implementation
      37             :     : public SfxListener
      38             : {
      39             : public:
      40             :     /** The single instance of this class.  It is created on demand when
      41             :         Instance() is called for the first time.
      42             :     */
      43             :     static MasterPageObserver* mpInstance;
      44             : 
      45             :     /** The master page observer will listen to events of this document and
      46             :         detect changes of the use of master pages.
      47             :     */
      48             :     void RegisterDocument (SdDrawDocument& rDocument);
      49             : 
      50             :     /** The master page observer will stop to listen to events of this
      51             :         document.
      52             :     */
      53             :     void UnregisterDocument (SdDrawDocument& rDocument);
      54             : 
      55             :     /** Add a listener that is informed of master pages that are newly
      56             :         assigned to slides or become unassigned.
      57             :         @param rEventListener
      58             :             The event listener to call for future events.  Call
      59             :             RemoveEventListener() before the listener is destroyed.
      60             :     */
      61             :     void AddEventListener (const Link& rEventListener);
      62             : 
      63             :     /** Remove the given listener from the list of listeners.
      64             :         @param rEventListener
      65             :             After this method returns the given listener is not called back
      66             :             from this object.  Passing a listener that has not
      67             :             been registered before is safe and is silently ignored.
      68             :     */
      69             :     void RemoveEventListener (const Link& rEventListener);
      70             : 
      71             :     /** Return a set of the names of master pages for the given document.
      72             :         This convenience method exists because this set is part of the
      73             :         internal data structure and thus takes no time to create.
      74             :     */
      75             :     inline MasterPageObserver::MasterPageNameSet GetMasterPageNames (
      76             :         SdDrawDocument& rDocument);
      77             : 
      78             : private:
      79             :     ::std::vector<Link> maListeners;
      80             : 
      81             :     struct DrawDocHash {
      82         100 :         size_t operator()(SdDrawDocument* argument) const
      83         100 :         { return reinterpret_cast<sal_uIntPtr>(argument); }
      84             :     };
      85             :     typedef ::boost::unordered_map<SdDrawDocument*,
      86             :                             MasterPageObserver::MasterPageNameSet,
      87             :                             DrawDocHash>
      88             :         MasterPageContainer;
      89             :     MasterPageContainer maUsedMasterPages;
      90             : 
      91             :     virtual void Notify(
      92             :         SfxBroadcaster& rBroadcaster,
      93             :         const SfxHint& rHint);
      94             : 
      95             :     void AnalyzeUsedMasterPages (SdDrawDocument& rDocument);
      96             : 
      97             :     void SendEvent (MasterPageObserverEvent& rEvent);
      98             : };
      99             : 
     100             : MasterPageObserver* MasterPageObserver::Implementation::mpInstance = NULL;
     101             : 
     102             : 
     103             : 
     104             : //===== MasterPageObserver ====================================================
     105             : 
     106          26 : MasterPageObserver&  MasterPageObserver::Instance (void)
     107             : {
     108          26 :     if (Implementation::mpInstance == NULL)
     109             :     {
     110             :         ::osl::GetGlobalMutex aMutexFunctor;
     111           4 :         ::osl::MutexGuard aGuard (aMutexFunctor());
     112           4 :         if (Implementation::mpInstance == NULL)
     113             :         {
     114           4 :             MasterPageObserver* pInstance = new MasterPageObserver ();
     115           4 :             SdGlobalResourceContainer::Instance().AddResource (
     116           8 :                 ::std::auto_ptr<SdGlobalResource>(pInstance));
     117             :             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
     118           4 :             Implementation::mpInstance = pInstance;
     119           4 :         }
     120             :     }
     121             :     else
     122             :     {
     123             :         OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
     124             :     }
     125             : 
     126             :     DBG_ASSERT(Implementation::mpInstance!=NULL,
     127             :         "MasterPageObserver::Instance(): instance is NULL");
     128          26 :     return *Implementation::mpInstance;
     129             : }
     130             : 
     131             : 
     132             : 
     133             : 
     134          13 : void MasterPageObserver::RegisterDocument (SdDrawDocument& rDocument)
     135             : {
     136          13 :     mpImpl->RegisterDocument (rDocument);
     137          13 : }
     138             : 
     139             : 
     140             : 
     141             : 
     142          13 : void MasterPageObserver::UnregisterDocument (SdDrawDocument& rDocument)
     143             : {
     144          13 :     mpImpl->UnregisterDocument (rDocument);
     145          13 : }
     146             : 
     147             : 
     148             : 
     149             : 
     150           0 : void MasterPageObserver::AddEventListener (const Link& rEventListener)
     151             : {
     152             : 
     153           0 :     mpImpl->AddEventListener (rEventListener);
     154           0 : }
     155             : 
     156             : 
     157             : 
     158             : 
     159           0 : void MasterPageObserver::RemoveEventListener (const Link& rEventListener)
     160             : {
     161           0 :     mpImpl->RemoveEventListener (rEventListener);
     162           0 : }
     163             : 
     164             : 
     165             : 
     166             : 
     167           4 : MasterPageObserver::MasterPageObserver (void)
     168           4 :     : mpImpl (new Implementation())
     169           4 : {}
     170             : 
     171             : 
     172             : 
     173             : 
     174           8 : MasterPageObserver::~MasterPageObserver (void)
     175           8 : {}
     176             : 
     177             : 
     178             : 
     179             : 
     180             : //===== MasterPageObserver::Implementation ====================================
     181             : 
     182          13 : void MasterPageObserver::Implementation::RegisterDocument (
     183             :     SdDrawDocument& rDocument)
     184             : {
     185             :     // Gather the names of all the master pages in the given document.
     186          13 :     MasterPageContainer::mapped_type aMasterPageSet;
     187          13 :     sal_uInt16 nMasterPageCount = rDocument.GetMasterSdPageCount(PK_STANDARD);
     188          15 :     for (sal_uInt16 nIndex=0; nIndex<nMasterPageCount; nIndex++)
     189             :     {
     190           2 :         SdPage* pMasterPage = rDocument.GetMasterSdPage (nIndex, PK_STANDARD);
     191           2 :         if (pMasterPage != NULL)
     192           2 :             aMasterPageSet.insert (pMasterPage->GetName());
     193             :     }
     194             : 
     195          13 :     maUsedMasterPages[&rDocument] = aMasterPageSet;
     196             : 
     197          13 :     StartListening (rDocument);
     198          13 : }
     199             : 
     200             : 
     201             : 
     202             : 
     203          13 : void MasterPageObserver::Implementation::UnregisterDocument (
     204             :     SdDrawDocument& rDocument)
     205             : {
     206          13 :     EndListening (rDocument);
     207             : 
     208          13 :     MasterPageContainer::iterator aMasterPageDescriptor(maUsedMasterPages.find(&rDocument));
     209          13 :     if(aMasterPageDescriptor != maUsedMasterPages.end())
     210          13 :         maUsedMasterPages.erase(aMasterPageDescriptor);
     211          13 : }
     212             : 
     213             : 
     214             : 
     215             : 
     216           0 : void MasterPageObserver::Implementation::AddEventListener (
     217             :     const Link& rEventListener)
     218             : {
     219           0 :     if (::std::find (
     220             :         maListeners.begin(),
     221             :         maListeners.end(),
     222           0 :         rEventListener) == maListeners.end())
     223             :     {
     224           0 :         maListeners.push_back (rEventListener);
     225             : 
     226             :         // Tell the new listener about all the master pages that are
     227             :         // currently in use.
     228           0 :         MasterPageContainer::iterator aDocumentIterator;
     229           0 :         for (aDocumentIterator=maUsedMasterPages.begin();
     230           0 :              aDocumentIterator!=maUsedMasterPages.end();
     231             :              ++aDocumentIterator)
     232             :         {
     233           0 :             ::std::set<String>::reverse_iterator aNameIterator;
     234           0 :             for (aNameIterator=aDocumentIterator->second.rbegin();
     235           0 :                  aNameIterator!=aDocumentIterator->second.rend();
     236             :                  ++aNameIterator)
     237             :             {
     238             :               MasterPageObserverEvent aEvent (
     239             :                   MasterPageObserverEvent::ET_MASTER_PAGE_EXISTS,
     240           0 :                   *aDocumentIterator->first,
     241           0 :                   *aNameIterator);
     242           0 :               SendEvent (aEvent);
     243             :             }
     244             :         }
     245             :     }
     246           0 : }
     247             : 
     248             : 
     249             : 
     250             : 
     251           0 : void MasterPageObserver::Implementation::RemoveEventListener (
     252             :     const Link& rEventListener)
     253             : {
     254             :     maListeners.erase (
     255             :         ::std::find (
     256             :             maListeners.begin(),
     257             :             maListeners.end(),
     258           0 :             rEventListener));
     259           0 : }
     260             : 
     261             : 
     262             : 
     263             : 
     264             : MasterPageObserver::MasterPageNameSet
     265             :     MasterPageObserver::Implementation::GetMasterPageNames (
     266             :         SdDrawDocument& rDocument)
     267             : {
     268             :     MasterPageContainer::iterator aMasterPageDescriptor (
     269             :         maUsedMasterPages.find(&rDocument));
     270             :     if (aMasterPageDescriptor != maUsedMasterPages.end())
     271             :         return aMasterPageDescriptor->second;
     272             :     else
     273             :         // Not found so return an empty set.
     274             :         return MasterPageObserver::MasterPageNameSet();
     275             : }
     276             : 
     277             : 
     278             : 
     279             : 
     280        1244 : void MasterPageObserver::Implementation::Notify(
     281             :     SfxBroadcaster& rBroadcaster,
     282             :     const SfxHint& rHint)
     283             : {
     284        1244 :     if (rHint.ISA(SdrHint))
     285             :     {
     286        1244 :         SdrHint& rSdrHint (*PTR_CAST(SdrHint,&rHint));
     287        1244 :         switch (rSdrHint.GetKind())
     288             :         {
     289             :             case HINT_PAGEORDERCHG:
     290             :                 // Process the modified set of pages only when the number of
     291             :                 // standard and notes master pages are equal.  This test
     292             :                 // filters out events that are sent in between the insertion
     293             :                 // of a new standard master page and a new notes master
     294             :                 // page.
     295          96 :                 if (rBroadcaster.ISA(SdDrawDocument))
     296             :                 {
     297             :                     SdDrawDocument& rDocument (
     298          96 :                         static_cast<SdDrawDocument&>(rBroadcaster));
     299         192 :                     if (rDocument.GetMasterSdPageCount(PK_STANDARD)
     300          96 :                         == rDocument.GetMasterSdPageCount(PK_NOTES))
     301             :                     {
     302          74 :                         AnalyzeUsedMasterPages (rDocument);
     303             :                     }
     304             :                 }
     305          96 :                 break;
     306             : 
     307             :             default:
     308        1148 :                 break;
     309             :         }
     310             :     }
     311        1244 : }
     312             : 
     313             : 
     314             : 
     315             : 
     316          74 : void MasterPageObserver::Implementation::AnalyzeUsedMasterPages (
     317             :     SdDrawDocument& rDocument)
     318             : {
     319             :     // Create a set of names of the master pages used by the given document.
     320          74 :     sal_uInt16 nMasterPageCount = rDocument.GetMasterSdPageCount(PK_STANDARD);
     321          74 :     ::std::set<String> aCurrentMasterPages;
     322         115 :     for (sal_uInt16 nIndex=0; nIndex<nMasterPageCount; nIndex++)
     323             :     {
     324          41 :         SdPage* pMasterPage = rDocument.GetMasterSdPage (nIndex, PK_STANDARD);
     325          41 :         if (pMasterPage != NULL)
     326          41 :             aCurrentMasterPages.insert (pMasterPage->GetName());
     327             :         OSL_TRACE("currently used master page %d is %s",
     328             :             nIndex,
     329             :             OUStringToOString(pMasterPage->GetName(),
     330             :                 RTL_TEXTENCODING_UTF8).getStr());
     331             :     }
     332             : 
     333             :     typedef ::std::vector<String> StringList;
     334         148 :     StringList aNewMasterPages;
     335         148 :     StringList aRemovedMasterPages;
     336             :     MasterPageContainer::iterator aOldMasterPagesDescriptor (
     337          74 :         maUsedMasterPages.find(&rDocument));
     338          74 :     if (aOldMasterPagesDescriptor != maUsedMasterPages.end())
     339             :     {
     340          74 :         StringList::iterator I;
     341             : 
     342          74 :         ::std::set<String>::iterator J;
     343          74 :         int i=0;
     344         282 :         for (J=aOldMasterPagesDescriptor->second.begin();
     345         208 :              J!=aOldMasterPagesDescriptor->second.end();
     346             :              ++J)
     347             :             OSL_TRACE("old used master page %d is %s",
     348             :             i++,
     349             :             OUStringToOString(*J,
     350             :                 RTL_TEXTENCODING_UTF8).getStr());
     351             : 
     352             :         // Send events about the newly used master pages.
     353             :         ::std::set_difference (
     354             :             aCurrentMasterPages.begin(),
     355             :             aCurrentMasterPages.end(),
     356          74 :             aOldMasterPagesDescriptor->second.begin(),
     357          74 :             aOldMasterPagesDescriptor->second.end(),
     358         222 :             ::std::back_insert_iterator<StringList>(aNewMasterPages));
     359          85 :         for (I=aNewMasterPages.begin(); I!=aNewMasterPages.end(); ++I)
     360             :         {
     361             :             OSL_TRACE("    added master page %s",
     362             :                 OUStringToOString(*I,
     363             :                     RTL_TEXTENCODING_UTF8).getStr());
     364             : 
     365             :             MasterPageObserverEvent aEvent (
     366             :                 MasterPageObserverEvent::ET_MASTER_PAGE_ADDED,
     367             :                 rDocument,
     368          11 :                 *I);
     369          11 :             SendEvent (aEvent);
     370             :         }
     371             : 
     372             :         // Send events about master pages that are not used any longer.
     373             :         ::std::set_difference (
     374          74 :             aOldMasterPagesDescriptor->second.begin(),
     375          74 :             aOldMasterPagesDescriptor->second.end(),
     376             :             aCurrentMasterPages.begin(),
     377             :             aCurrentMasterPages.end(),
     378         222 :             ::std::back_insert_iterator<StringList>(aRemovedMasterPages));
     379          74 :         for (I=aRemovedMasterPages.begin(); I!=aRemovedMasterPages.end(); ++I)
     380             :         {
     381             :             OSL_TRACE("    removed master page %s",
     382             :                 OUStringToOString(*I,
     383             :                     RTL_TEXTENCODING_UTF8).getStr());
     384             : 
     385             :             MasterPageObserverEvent aEvent (
     386             :                 MasterPageObserverEvent::ET_MASTER_PAGE_REMOVED,
     387             :                 rDocument,
     388           0 :                 *I);
     389           0 :             SendEvent (aEvent);
     390             :         }
     391             : 
     392             :         // Store the new list of master pages.
     393          74 :         aOldMasterPagesDescriptor->second = aCurrentMasterPages;
     394          74 :     }
     395          74 : }
     396             : 
     397             : 
     398             : 
     399             : 
     400          11 : void MasterPageObserver::Implementation::SendEvent (
     401             :     MasterPageObserverEvent& rEvent)
     402             : {
     403          11 :     ::std::vector<Link>::iterator aLink (maListeners.begin());
     404          11 :     ::std::vector<Link>::iterator aEnd (maListeners.end());
     405          22 :     while (aLink!=aEnd)
     406             :     {
     407           0 :         aLink->Call (&rEvent);
     408           0 :         ++aLink;
     409             :     }
     410          11 : }
     411             : 
     412             : 
     413          33 : } // end of namespace sd
     414             : 
     415             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10