1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_sc.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir // ----------------------------------------------------------------------- 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir // INCLUDE --------------------------------------------------------------- 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir #include <sfx2/linkmgr.hxx> 38*cdf0e10cSrcweir #include <tools/debug.hxx> 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir #include "areasave.hxx" 41*cdf0e10cSrcweir #include "arealink.hxx" 42*cdf0e10cSrcweir #include "document.hxx" 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir // ----------------------------------------------------------------------- 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir ScAreaLinkSaver::ScAreaLinkSaver( const ScAreaLink& rSource ) : 47*cdf0e10cSrcweir aFileName ( rSource.GetFile() ), 48*cdf0e10cSrcweir aFilterName ( rSource.GetFilter() ), 49*cdf0e10cSrcweir aOptions ( rSource.GetOptions() ), 50*cdf0e10cSrcweir aSourceArea ( rSource.GetSource() ), 51*cdf0e10cSrcweir aDestArea ( rSource.GetDestArea() ), 52*cdf0e10cSrcweir nRefresh ( rSource.GetRefreshDelay() ) // seconds 53*cdf0e10cSrcweir { 54*cdf0e10cSrcweir } 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir ScAreaLinkSaver::ScAreaLinkSaver( const ScAreaLinkSaver& rCopy ) : 57*cdf0e10cSrcweir ScDataObject(), 58*cdf0e10cSrcweir aFileName ( rCopy.aFileName ), 59*cdf0e10cSrcweir aFilterName ( rCopy.aFilterName ), 60*cdf0e10cSrcweir aOptions ( rCopy.aOptions ), 61*cdf0e10cSrcweir aSourceArea ( rCopy.aSourceArea ), 62*cdf0e10cSrcweir aDestArea ( rCopy.aDestArea ), 63*cdf0e10cSrcweir nRefresh ( rCopy.nRefresh ) 64*cdf0e10cSrcweir { 65*cdf0e10cSrcweir } 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir ScAreaLinkSaver::~ScAreaLinkSaver() 68*cdf0e10cSrcweir { 69*cdf0e10cSrcweir } 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir ScDataObject* ScAreaLinkSaver::Clone() const 72*cdf0e10cSrcweir { 73*cdf0e10cSrcweir return new ScAreaLinkSaver( *this ); 74*cdf0e10cSrcweir } 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir sal_Bool ScAreaLinkSaver::IsEqualSource( const ScAreaLink& rCompare ) const 77*cdf0e10cSrcweir { 78*cdf0e10cSrcweir return ( aFileName == rCompare.GetFile() && 79*cdf0e10cSrcweir aFilterName == rCompare.GetFilter() && 80*cdf0e10cSrcweir aOptions == rCompare.GetOptions() && 81*cdf0e10cSrcweir aSourceArea == rCompare.GetSource() && 82*cdf0e10cSrcweir nRefresh == rCompare.GetRefreshDelay() ); 83*cdf0e10cSrcweir } 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir sal_Bool ScAreaLinkSaver::IsEqual( const ScAreaLink& rCompare ) const 86*cdf0e10cSrcweir { 87*cdf0e10cSrcweir return ( IsEqualSource( rCompare ) && 88*cdf0e10cSrcweir aDestArea == rCompare.GetDestArea() ); 89*cdf0e10cSrcweir } 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir void ScAreaLinkSaver::WriteToLink( ScAreaLink& rLink ) const 92*cdf0e10cSrcweir { 93*cdf0e10cSrcweir rLink.SetDestArea( aDestArea ); 94*cdf0e10cSrcweir } 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir void ScAreaLinkSaver::InsertNewLink( ScDocument* pDoc ) const 97*cdf0e10cSrcweir { 98*cdf0e10cSrcweir // (see ScUndoRemoveAreaLink::Undo) 99*cdf0e10cSrcweir 100*cdf0e10cSrcweir sfx2::LinkManager* pLinkManager = pDoc->GetLinkManager(); 101*cdf0e10cSrcweir SfxObjectShell* pObjSh = pDoc->GetDocumentShell(); 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir if ( pLinkManager && pObjSh ) 104*cdf0e10cSrcweir { 105*cdf0e10cSrcweir ScAreaLink* pLink = new ScAreaLink( pObjSh, aFileName, aFilterName, aOptions, 106*cdf0e10cSrcweir aSourceArea, aDestArea.aStart, nRefresh ); 107*cdf0e10cSrcweir pLink->SetInCreate( sal_True ); 108*cdf0e10cSrcweir pLink->SetDestArea( aDestArea ); 109*cdf0e10cSrcweir pLinkManager->InsertFileLink( *pLink, OBJECT_CLIENT_FILE, aFileName, &aFilterName, &aSourceArea ); 110*cdf0e10cSrcweir pLink->Update(); 111*cdf0e10cSrcweir pLink->SetInCreate( sal_False ); 112*cdf0e10cSrcweir } 113*cdf0e10cSrcweir } 114*cdf0e10cSrcweir 115*cdf0e10cSrcweir // ----------------------------------------------------------------------- 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir ScAreaLinkSaveCollection::ScAreaLinkSaveCollection() 118*cdf0e10cSrcweir { 119*cdf0e10cSrcweir } 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir ScAreaLinkSaveCollection::ScAreaLinkSaveCollection( const ScAreaLinkSaveCollection& rCopy ) : 122*cdf0e10cSrcweir ScCollection( rCopy ) 123*cdf0e10cSrcweir { 124*cdf0e10cSrcweir } 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir ScAreaLinkSaveCollection::~ScAreaLinkSaveCollection() 127*cdf0e10cSrcweir { 128*cdf0e10cSrcweir } 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir ScDataObject* ScAreaLinkSaveCollection::Clone() const 131*cdf0e10cSrcweir { 132*cdf0e10cSrcweir return new ScAreaLinkSaveCollection( *this ); 133*cdf0e10cSrcweir } 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir sal_Bool ScAreaLinkSaveCollection::IsEqual( const ScDocument* pDoc ) const 136*cdf0e10cSrcweir { 137*cdf0e10cSrcweir // IsEqual can be checked in sequence. 138*cdf0e10cSrcweir // Neither ref-update nor removing links will change the order. 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir sfx2::LinkManager* pLinkManager = const_cast<ScDocument*>(pDoc)->GetLinkManager(); 141*cdf0e10cSrcweir if (pLinkManager) 142*cdf0e10cSrcweir { 143*cdf0e10cSrcweir sal_uInt16 nPos = 0; 144*cdf0e10cSrcweir const ::sfx2::SvBaseLinks& rLinks = pLinkManager->GetLinks(); 145*cdf0e10cSrcweir sal_uInt16 nLinkCount = rLinks.Count(); 146*cdf0e10cSrcweir for (sal_uInt16 i=0; i<nLinkCount; i++) 147*cdf0e10cSrcweir { 148*cdf0e10cSrcweir ::sfx2::SvBaseLink* pBase = *rLinks[i]; 149*cdf0e10cSrcweir if (pBase->ISA(ScAreaLink)) 150*cdf0e10cSrcweir { 151*cdf0e10cSrcweir if ( nPos >= GetCount() || !(*this)[nPos]->IsEqual( *(ScAreaLink*)pBase ) ) 152*cdf0e10cSrcweir return sal_False; 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir ++nPos; 155*cdf0e10cSrcweir } 156*cdf0e10cSrcweir } 157*cdf0e10cSrcweir if ( nPos < GetCount() ) 158*cdf0e10cSrcweir return sal_False; // fewer links in the document than in the save collection 159*cdf0e10cSrcweir } 160*cdf0e10cSrcweir 161*cdf0e10cSrcweir return sal_True; 162*cdf0e10cSrcweir } 163*cdf0e10cSrcweir 164*cdf0e10cSrcweir ScAreaLink* lcl_FindLink( const ::sfx2::SvBaseLinks& rLinks, const ScAreaLinkSaver& rSaver ) 165*cdf0e10cSrcweir { 166*cdf0e10cSrcweir sal_uInt16 nLinkCount = rLinks.Count(); 167*cdf0e10cSrcweir for (sal_uInt16 i=0; i<nLinkCount; i++) 168*cdf0e10cSrcweir { 169*cdf0e10cSrcweir ::sfx2::SvBaseLink* pBase = *rLinks[i]; 170*cdf0e10cSrcweir if ( pBase->ISA(ScAreaLink) && 171*cdf0e10cSrcweir rSaver.IsEqualSource( *static_cast<ScAreaLink*>(pBase) ) ) 172*cdf0e10cSrcweir { 173*cdf0e10cSrcweir return static_cast<ScAreaLink*>(pBase); // found 174*cdf0e10cSrcweir } 175*cdf0e10cSrcweir } 176*cdf0e10cSrcweir return NULL; // not found 177*cdf0e10cSrcweir } 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir void ScAreaLinkSaveCollection::Restore( ScDocument* pDoc ) const 180*cdf0e10cSrcweir { 181*cdf0e10cSrcweir // The save collection may contain additional entries that are not in the document. 182*cdf0e10cSrcweir // They must be inserted again. 183*cdf0e10cSrcweir // Entries from the save collection must be searched via source data, as the order 184*cdf0e10cSrcweir // of links changes if deleted entries are re-added to the link manager (always at the end). 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir sfx2::LinkManager* pLinkManager = pDoc->GetLinkManager(); 187*cdf0e10cSrcweir if (pLinkManager) 188*cdf0e10cSrcweir { 189*cdf0e10cSrcweir const ::sfx2::SvBaseLinks& rLinks = pLinkManager->GetLinks(); 190*cdf0e10cSrcweir sal_uInt16 nSaveCount = GetCount(); 191*cdf0e10cSrcweir for (sal_uInt16 nPos=0; nPos<nSaveCount; nPos++) 192*cdf0e10cSrcweir { 193*cdf0e10cSrcweir ScAreaLinkSaver* pSaver = (*this)[nPos]; 194*cdf0e10cSrcweir ScAreaLink* pLink = lcl_FindLink( rLinks, *pSaver ); 195*cdf0e10cSrcweir if ( pLink ) 196*cdf0e10cSrcweir pSaver->WriteToLink( *pLink ); // restore output position 197*cdf0e10cSrcweir else 198*cdf0e10cSrcweir pSaver->InsertNewLink( pDoc ); // re-insert deleted link 199*cdf0e10cSrcweir } 200*cdf0e10cSrcweir } 201*cdf0e10cSrcweir } 202*cdf0e10cSrcweir 203*cdf0e10cSrcweir // static 204*cdf0e10cSrcweir ScAreaLinkSaveCollection* ScAreaLinkSaveCollection::CreateFromDoc( const ScDocument* pDoc ) 205*cdf0e10cSrcweir { 206*cdf0e10cSrcweir ScAreaLinkSaveCollection* pColl = NULL; 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir sfx2::LinkManager* pLinkManager = const_cast<ScDocument*>(pDoc)->GetLinkManager(); 209*cdf0e10cSrcweir if (pLinkManager) 210*cdf0e10cSrcweir { 211*cdf0e10cSrcweir const ::sfx2::SvBaseLinks& rLinks = pLinkManager->GetLinks(); 212*cdf0e10cSrcweir sal_uInt16 nLinkCount = rLinks.Count(); 213*cdf0e10cSrcweir for (sal_uInt16 i=0; i<nLinkCount; i++) 214*cdf0e10cSrcweir { 215*cdf0e10cSrcweir ::sfx2::SvBaseLink* pBase = *rLinks[i]; 216*cdf0e10cSrcweir if (pBase->ISA(ScAreaLink)) 217*cdf0e10cSrcweir { 218*cdf0e10cSrcweir if (!pColl) 219*cdf0e10cSrcweir pColl = new ScAreaLinkSaveCollection; 220*cdf0e10cSrcweir 221*cdf0e10cSrcweir ScAreaLinkSaver* pSaver = new ScAreaLinkSaver( *(ScAreaLink*)pBase ); 222*cdf0e10cSrcweir if (!pColl->Insert(pSaver)) 223*cdf0e10cSrcweir delete pSaver; 224*cdf0e10cSrcweir } 225*cdf0e10cSrcweir } 226*cdf0e10cSrcweir } 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir return pColl; 229*cdf0e10cSrcweir } 230*cdf0e10cSrcweir 231