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

Generated by: LCOV version 1.10