LCOV - code coverage report
Current view: top level - sc/source/ui/undo - areasave.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 11 98 11.2 %
Date: 2015-06-13 12:38:46 Functions: 3 20 15.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 <sfx2/linkmgr.hxx>
      21             : 
      22             : #include "areasave.hxx"
      23             : #include "arealink.hxx"
      24             : #include "document.hxx"
      25             : #include <documentlinkmgr.hxx>
      26             : 
      27           0 : ScAreaLinkSaver::ScAreaLinkSaver( const ScAreaLink& rSource ) :
      28           0 :     aFileName   ( rSource.GetFile() ),
      29           0 :     aFilterName ( rSource.GetFilter() ),
      30           0 :     aOptions    ( rSource.GetOptions() ),
      31           0 :     aSourceArea ( rSource.GetSource() ),
      32           0 :     aDestArea   ( rSource.GetDestArea() ),
      33           0 :     nRefresh    ( rSource.GetRefreshDelay() )       // seconds
      34             : {
      35           0 : }
      36             : 
      37           0 : ScAreaLinkSaver::ScAreaLinkSaver( const ScAreaLinkSaver& rCopy ) :
      38             :     aFileName   ( rCopy.aFileName ),
      39             :     aFilterName ( rCopy.aFilterName ),
      40             :     aOptions    ( rCopy.aOptions ),
      41             :     aSourceArea ( rCopy.aSourceArea ),
      42             :     aDestArea   ( rCopy.aDestArea ),
      43           0 :     nRefresh    ( rCopy.nRefresh )
      44             : {
      45           0 : }
      46             : 
      47           0 : ScAreaLinkSaver::~ScAreaLinkSaver() {}
      48             : 
      49           0 : bool ScAreaLinkSaver::IsEqualSource( const ScAreaLink& rCompare ) const
      50             : {
      51           0 :     return ( aFileName.equals(rCompare.GetFile()) &&
      52           0 :              aFilterName.equals(rCompare.GetFilter()) &&
      53           0 :              aOptions.equals(rCompare.GetOptions()) &&
      54           0 :              aSourceArea.equals(rCompare.GetSource()) &&
      55           0 :              nRefresh == rCompare.GetRefreshDelay() );
      56             : }
      57             : 
      58           0 : bool ScAreaLinkSaver::IsEqual( const ScAreaLink& rCompare ) const
      59             : {
      60           0 :     return ( IsEqualSource( rCompare ) &&
      61           0 :              aDestArea == rCompare.GetDestArea() );
      62             : }
      63             : 
      64           0 : void ScAreaLinkSaver::WriteToLink( ScAreaLink& rLink ) const
      65             : {
      66           0 :     rLink.SetDestArea( aDestArea );
      67           0 : }
      68             : 
      69           0 : void ScAreaLinkSaver::InsertNewLink( ScDocument* pDoc )
      70             : {
      71             :     // (see ScUndoRemoveAreaLink::Undo)
      72             : 
      73           0 :     sfx2::LinkManager* pLinkManager = pDoc->GetLinkManager();
      74           0 :     SfxObjectShell* pObjSh = pDoc->GetDocumentShell();
      75             : 
      76           0 :     if ( pLinkManager && pObjSh )
      77             :     {
      78             :         ScAreaLink* pLink = new ScAreaLink( pObjSh, aFileName, aFilterName, aOptions,
      79           0 :                                             aSourceArea, aDestArea.aStart, nRefresh );
      80           0 :         pLink->SetInCreate( true );
      81           0 :         pLink->SetDestArea( aDestArea );
      82           0 :         OUString aTmp1(aFilterName), aTmp2(aSourceArea);
      83           0 :         pLinkManager->InsertFileLink( *pLink, OBJECT_CLIENT_FILE, aFileName, &aTmp1, &aTmp2 );
      84           0 :         pLink->Update();
      85           0 :         pLink->SetInCreate( false );
      86             :     }
      87           0 : }
      88             : 
      89           0 : ScAreaLinkSaveCollection::ScAreaLinkSaveCollection() {}
      90             : 
      91           0 : ScAreaLinkSaveCollection::ScAreaLinkSaveCollection( const ScAreaLinkSaveCollection& r ) :
      92           0 :     maData(r.maData) {}
      93             : 
      94           0 : ScAreaLinkSaveCollection::~ScAreaLinkSaveCollection() {}
      95             : 
      96           0 : bool ScAreaLinkSaveCollection::IsEqual( const ScDocument* pDoc ) const
      97             : {
      98             :     // IsEqual can be checked in sequence.
      99             :     // Neither ref-update nor removing links will change the order.
     100             : 
     101           0 :     const sfx2::LinkManager* pLinkManager = pDoc->GetLinkManager();
     102           0 :     if (pLinkManager)
     103             :     {
     104           0 :         size_t nPos = 0;
     105           0 :         const ::sfx2::SvBaseLinks& rLinks = pLinkManager->GetLinks();
     106           0 :         sal_uInt16 nLinkCount = rLinks.size();
     107           0 :         for (sal_uInt16 i=0; i<nLinkCount; i++)
     108             :         {
     109           0 :             ::sfx2::SvBaseLink* pBase = *rLinks[i];
     110           0 :             if (pBase->ISA(ScAreaLink))
     111             :             {
     112           0 :                 if ( nPos >= size() || !(*this)[nPos]->IsEqual( *static_cast<ScAreaLink*>(pBase) ) )
     113           0 :                     return false;
     114             : 
     115           0 :                 ++nPos;
     116             :             }
     117             :         }
     118           0 :         if ( nPos < size() )
     119           0 :             return false;           // fewer links in the document than in the save collection
     120             :     }
     121             : 
     122           0 :     return true;
     123             : }
     124             : 
     125           0 : static ScAreaLink* lcl_FindLink( const ::sfx2::SvBaseLinks& rLinks, const ScAreaLinkSaver& rSaver )
     126             : {
     127           0 :     sal_uInt16 nLinkCount = rLinks.size();
     128           0 :     for (sal_uInt16 i=0; i<nLinkCount; i++)
     129             :     {
     130           0 :         ::sfx2::SvBaseLink* pBase = *rLinks[i];
     131           0 :         if ( pBase->ISA(ScAreaLink) &&
     132           0 :              rSaver.IsEqualSource( *static_cast<ScAreaLink*>(pBase) ) )
     133             :         {
     134           0 :             return static_cast<ScAreaLink*>(pBase);     // found
     135             :         }
     136             :     }
     137           0 :     return NULL;    // not found
     138             : }
     139             : 
     140           0 : void ScAreaLinkSaveCollection::Restore( ScDocument* pDoc )
     141             : {
     142             :     // The save collection may contain additional entries that are not in the document.
     143             :     // They must be inserted again.
     144             :     // Entries from the save collection must be searched via source data, as the order
     145             :     // of links changes if deleted entries are re-added to the link manager (always at the end).
     146             : 
     147           0 :     sfx2::LinkManager* pLinkManager = pDoc->GetDocLinkManager().getLinkManager(false);
     148           0 :     if (pLinkManager)
     149             :     {
     150           0 :         const ::sfx2::SvBaseLinks& rLinks = pLinkManager->GetLinks();
     151           0 :         size_t nSaveCount = size();
     152           0 :         for (size_t nPos=0; nPos<nSaveCount; ++nPos)
     153             :         {
     154           0 :             ScAreaLinkSaver* pSaver = (*this)[nPos];
     155           0 :             ScAreaLink* pLink = lcl_FindLink( rLinks, *pSaver );
     156           0 :             if ( pLink )
     157           0 :                 pSaver->WriteToLink( *pLink );          // restore output position
     158             :             else
     159           0 :                 pSaver->InsertNewLink( pDoc );          // re-insert deleted link
     160             :         }
     161             :     }
     162           0 : }
     163             : 
     164          66 : ScAreaLinkSaveCollection* ScAreaLinkSaveCollection::CreateFromDoc( const ScDocument* pDoc )
     165             : {
     166          66 :     ScAreaLinkSaveCollection* pColl = NULL;
     167             : 
     168          66 :     sfx2::LinkManager* pLinkManager = const_cast<ScDocument*>(pDoc)->GetLinkManager();
     169          66 :     if (pLinkManager)
     170             :     {
     171          66 :         const ::sfx2::SvBaseLinks& rLinks = pLinkManager->GetLinks();
     172          66 :         sal_uInt16 nLinkCount = rLinks.size();
     173          67 :         for (sal_uInt16 i=0; i<nLinkCount; i++)
     174             :         {
     175           1 :             ::sfx2::SvBaseLink* pBase = *rLinks[i];
     176           1 :             if (pBase->ISA(ScAreaLink))
     177             :             {
     178           0 :                 if (!pColl)
     179           0 :                     pColl = new ScAreaLinkSaveCollection;
     180             : 
     181           0 :                 ScAreaLinkSaver* pSaver = new ScAreaLinkSaver( *static_cast<ScAreaLink*>(pBase ));
     182           0 :                 pColl->push_back(pSaver);
     183             :             }
     184             :         }
     185             :     }
     186             : 
     187          66 :     return pColl;
     188             : }
     189             : 
     190           0 : ScAreaLinkSaver* ScAreaLinkSaveCollection::operator [](size_t nIndex)
     191             : {
     192           0 :     return &maData[nIndex];
     193             : }
     194             : 
     195           0 : const ScAreaLinkSaver* ScAreaLinkSaveCollection::operator [](size_t nIndex) const
     196             : {
     197           0 :     return &maData[nIndex];
     198             : }
     199             : 
     200           0 : size_t ScAreaLinkSaveCollection::size() const
     201             : {
     202           0 :     return maData.size();
     203             : }
     204             : 
     205           0 : void ScAreaLinkSaveCollection::push_back(ScAreaLinkSaver* p)
     206             : {
     207           0 :     maData.push_back(p);
     208         156 : }
     209             : 
     210             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11