LCOV - code coverage report
Current view: top level - sd/source/ui/sidebar - MasterPageObserver.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 89 114 78.1 %
Date: 2015-06-13 12:38:46 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 <set>
      26             : #include <unordered_map>
      27             : #include <vector>
      28             : #include <svl/lstner.hxx>
      29             : #include <osl/doublecheckedlocking.h>
      30             : #include <osl/getglobalmutex.hxx>
      31             : 
      32             : namespace sd {
      33             : 
      34          18 : 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         213 :         size_t operator()(SdDrawDocument* argument) const
      74         213 :         { return reinterpret_cast<unsigned long>(argument); }
      75             :     };
      76             :     typedef std::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          82 : MasterPageObserver&  MasterPageObserver::Instance()
      96             : {
      97          82 :     if (Implementation::mpInstance == NULL)
      98             :     {
      99             :         ::osl::GetGlobalMutex aMutexFunctor;
     100           8 :         ::osl::MutexGuard aGuard (aMutexFunctor());
     101           8 :         if (Implementation::mpInstance == NULL)
     102             :         {
     103           8 :             MasterPageObserver* pInstance = new MasterPageObserver ();
     104           8 :             SdGlobalResourceContainer::Instance().AddResource (
     105          16 :                 ::std::unique_ptr<SdGlobalResource>(pInstance));
     106             :             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
     107           8 :             Implementation::mpInstance = pInstance;
     108           8 :         }
     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          82 :     return *Implementation::mpInstance;
     118             : }
     119             : 
     120          41 : void MasterPageObserver::RegisterDocument (SdDrawDocument& rDocument)
     121             : {
     122          41 :     mpImpl->RegisterDocument (rDocument);
     123          41 : }
     124             : 
     125          41 : void MasterPageObserver::UnregisterDocument (SdDrawDocument& rDocument)
     126             : {
     127          41 :     mpImpl->UnregisterDocument (rDocument);
     128          41 : }
     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           8 : MasterPageObserver::MasterPageObserver()
     142           8 :     : mpImpl (new Implementation())
     143           8 : {}
     144             : 
     145          10 : MasterPageObserver::~MasterPageObserver()
     146          10 : {}
     147             : 
     148             : //===== MasterPageObserver::Implementation ====================================
     149             : 
     150          41 : void MasterPageObserver::Implementation::RegisterDocument (
     151             :     SdDrawDocument& rDocument)
     152             : {
     153             :     // Gather the names of all the master pages in the given document.
     154          41 :     MasterPageContainer::mapped_type aMasterPageSet;
     155          41 :     sal_uInt16 nMasterPageCount = rDocument.GetMasterSdPageCount(PK_STANDARD);
     156          66 :     for (sal_uInt16 nIndex=0; nIndex<nMasterPageCount; nIndex++)
     157             :     {
     158          25 :         SdPage* pMasterPage = rDocument.GetMasterSdPage (nIndex, PK_STANDARD);
     159          25 :         if (pMasterPage != NULL)
     160          25 :             aMasterPageSet.insert (pMasterPage->GetName());
     161             :     }
     162             : 
     163          41 :     maUsedMasterPages[&rDocument] = aMasterPageSet;
     164             : 
     165          41 :     StartListening (rDocument);
     166          41 : }
     167             : 
     168          41 : void MasterPageObserver::Implementation::UnregisterDocument (
     169             :     SdDrawDocument& rDocument)
     170             : {
     171          41 :     EndListening (rDocument);
     172             : 
     173          41 :     MasterPageContainer::iterator aMasterPageDescriptor(maUsedMasterPages.find(&rDocument));
     174          41 :     if(aMasterPageDescriptor != maUsedMasterPages.end())
     175          34 :         maUsedMasterPages.erase(aMasterPageDescriptor);
     176          41 : }
     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        2008 : void MasterPageObserver::Implementation::Notify(
     221             :     SfxBroadcaster& rBroadcaster,
     222             :     const SfxHint& rHint)
     223             : {
     224        2008 :     const SdrHint* pSdrHint = dynamic_cast<const SdrHint*>(&rHint);
     225        2008 :     if (pSdrHint)
     226             :     {
     227        2008 :         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         165 :                 if (rBroadcaster.ISA(SdDrawDocument))
     236             :                 {
     237             :                     SdDrawDocument& rDocument (
     238         165 :                         static_cast<SdDrawDocument&>(rBroadcaster));
     239         330 :                     if (rDocument.GetMasterSdPageCount(PK_STANDARD)
     240         165 :                         == rDocument.GetMasterSdPageCount(PK_NOTES))
     241             :                     {
     242         131 :                         AnalyzeUsedMasterPages (rDocument);
     243             :                     }
     244             :                 }
     245         165 :                 break;
     246             : 
     247             :             default:
     248        1843 :                 break;
     249             :         }
     250             :     }
     251        2008 : }
     252             : 
     253         131 : void MasterPageObserver::Implementation::AnalyzeUsedMasterPages (
     254             :     SdDrawDocument& rDocument)
     255             : {
     256             :     // Create a set of names of the master pages used by the given document.
     257         131 :     sal_uInt16 nMasterPageCount = rDocument.GetMasterSdPageCount(PK_STANDARD);
     258         131 :     ::std::set<OUString> aCurrentMasterPages;
     259         211 :     for (sal_uInt16 nIndex=0; nIndex<nMasterPageCount; nIndex++)
     260             :     {
     261          80 :         SdPage* pMasterPage = rDocument.GetMasterSdPage (nIndex, PK_STANDARD);
     262          80 :         if (pMasterPage != NULL)
     263          80 :             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         262 :     StringList aNewMasterPages;
     272         262 :     StringList aRemovedMasterPages;
     273             :     MasterPageContainer::iterator aOldMasterPagesDescriptor (
     274         131 :         maUsedMasterPages.find(&rDocument));
     275         131 :     if (aOldMasterPagesDescriptor != maUsedMasterPages.end())
     276             :     {
     277         131 :         StringList::iterator I;
     278             : 
     279         131 :         ::std::set<OUString>::iterator J;
     280         131 :         int i=0;
     281         519 :         for (J=aOldMasterPagesDescriptor->second.begin();
     282         388 :              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         131 :             aOldMasterPagesDescriptor->second.begin(),
     294         131 :             aOldMasterPagesDescriptor->second.end(),
     295         393 :             ::std::back_insert_iterator<StringList>(aNewMasterPages));
     296         153 :         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          22 :                 *I);
     306          22 :             SendEvent (aEvent);
     307             :         }
     308             : 
     309             :         // Send events about master pages that are not used any longer.
     310             :         ::std::set_difference (
     311         131 :             aOldMasterPagesDescriptor->second.begin(),
     312         131 :             aOldMasterPagesDescriptor->second.end(),
     313             :             aCurrentMasterPages.begin(),
     314             :             aCurrentMasterPages.end(),
     315         393 :             ::std::back_insert_iterator<StringList>(aRemovedMasterPages));
     316         136 :         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           5 :                 *I);
     326           5 :             SendEvent (aEvent);
     327             :         }
     328             : 
     329             :         // Store the new list of master pages.
     330         131 :         aOldMasterPagesDescriptor->second = aCurrentMasterPages;
     331         131 :     }
     332         131 : }
     333             : 
     334          27 : void MasterPageObserver::Implementation::SendEvent (
     335             :     MasterPageObserverEvent& rEvent)
     336             : {
     337          27 :     ::std::vector<Link<>>::iterator aLink (maListeners.begin());
     338          27 :     ::std::vector<Link<>>::iterator aEnd (maListeners.end());
     339          54 :     while (aLink!=aEnd)
     340             :     {
     341           0 :         aLink->Call (&rEvent);
     342           0 :         ++aLink;
     343             :     }
     344          27 : }
     345             : 
     346          66 : } // end of namespace sd
     347             : 
     348             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11