cid#1607171 Data race condition
[LibreOffice.git] / sc / source / ui / undo / refundo.cxx
blobd87098720fd82334881bac25bac9ab3f7aebf1c7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <refundo.hxx>
21 #include <document.hxx>
22 #include <dbdata.hxx>
23 #include <rangenam.hxx>
24 #include <detdata.hxx>
25 #include <prnsave.hxx>
26 #include <chartlis.hxx>
27 #include <dpobject.hxx>
28 #include <areasave.hxx>
29 #include <unoreflist.hxx>
30 #include <scopetools.hxx>
31 #include <refupdatecontext.hxx>
33 ScRefUndoData::ScRefUndoData( const ScDocument* pDoc ) :
34 pPrintRanges(pDoc->CreatePrintRangeSaver())
36 const ScDBCollection* pOldDBColl = pDoc->GetDBCollection();
37 if (pOldDBColl && !pOldDBColl->empty())
38 pDBCollection.reset(new ScDBCollection(*pOldDBColl));
40 const ScRangeName* pOldRanges = pDoc->GetRangeName();
41 if (pOldRanges && !pOldRanges->empty())
42 pRangeName.reset(new ScRangeName(*pOldRanges));
44 // when handling Pivot solely keep the range?
46 const ScDPCollection* pOldDP = pDoc->GetDPCollection();
47 if (pOldDP && pOldDP->GetCount())
48 pDPCollection.reset(new ScDPCollection(*pOldDP));
50 const ScDetOpList* pOldDetOp = pDoc->GetDetOpList();
51 if (pOldDetOp && pOldDetOp->Count())
52 pDetOpList.reset(new ScDetOpList(*pOldDetOp));
54 const ScChartListenerCollection* pOldChartLisColl = pDoc->GetChartListenerCollection();
55 if (pOldChartLisColl)
56 pChartListenerCollection.reset(new ScChartListenerCollection(*pOldChartLisColl));
58 pAreaLinks = ScAreaLinkSaveCollection::CreateFromDoc(pDoc); // returns NULL if empty
60 const_cast<ScDocument*>(pDoc)->BeginUnoRefUndo();
63 ScRefUndoData::~ScRefUndoData()
65 pDBCollection.reset();
66 pRangeName.reset();
67 pPrintRanges.reset();
68 pDPCollection.reset();
69 pDetOpList.reset();
70 pChartListenerCollection.reset();
71 pAreaLinks.reset();
74 void ScRefUndoData::DeleteUnchanged( const ScDocument* pDoc )
76 if (pDBCollection)
78 ScDBCollection* pNewDBColl = pDoc->GetDBCollection();
79 if ( pNewDBColl && *pDBCollection == *pNewDBColl )
80 pDBCollection.reset();
82 if (pRangeName)
84 ScRangeName* pNewRanges = pDoc->GetRangeName();
85 if ( pNewRanges && *pRangeName == *pNewRanges )
86 pRangeName.reset();
89 if (pPrintRanges)
91 std::unique_ptr<ScPrintRangeSaver> pNewRanges = pDoc->CreatePrintRangeSaver();
92 if ( pNewRanges && *pPrintRanges == *pNewRanges )
93 pPrintRanges.reset();
96 if (pDPCollection)
98 ScDPCollection* pNewDP = const_cast<ScDocument*>(pDoc)->GetDPCollection(); //! const
99 if ( pNewDP && pDPCollection->RefsEqual(*pNewDP) )
100 pDPCollection.reset();
103 if (pDetOpList)
105 ScDetOpList* pNewDetOp = pDoc->GetDetOpList();
106 if ( pNewDetOp && *pDetOpList == *pNewDetOp )
107 pDetOpList.reset();
110 if ( pChartListenerCollection )
112 ScChartListenerCollection* pNewChartListenerCollection =
113 pDoc->GetChartListenerCollection();
114 if ( pNewChartListenerCollection &&
115 *pChartListenerCollection == *pNewChartListenerCollection )
116 pChartListenerCollection.reset();
119 if (pAreaLinks)
121 if ( pAreaLinks->IsEqual( pDoc ) )
122 pAreaLinks.reset();
125 if ( pDoc->HasUnoRefUndo() )
127 pUnoRefs = const_cast<ScDocument*>(pDoc)->EndUnoRefUndo();
128 if ( pUnoRefs && pUnoRefs->IsEmpty() )
130 pUnoRefs.reset();
135 void ScRefUndoData::DoUndo( ScDocument* pDoc, bool bUndoRefFirst )
137 if (pDBCollection)
138 pDoc->SetDBCollection( std::unique_ptr<ScDBCollection>(new ScDBCollection(*pDBCollection)) );
139 if (pRangeName)
140 pDoc->SetRangeName( std::unique_ptr<ScRangeName>(new ScRangeName(*pRangeName)) );
142 if (pPrintRanges)
143 pDoc->RestorePrintRanges(*pPrintRanges);
145 if (pDPCollection)
147 ScDPCollection* pDocDP = pDoc->GetDPCollection();
148 if (pDocDP)
149 pDPCollection->WriteRefsTo( *pDocDP );
152 if (pDetOpList)
153 pDoc->SetDetOpList( std::unique_ptr<ScDetOpList>(new ScDetOpList(*pDetOpList)) );
155 // bUndoRefFirst is bSetChartRangeLists
156 if ( pChartListenerCollection )
157 pDoc->SetChartListenerCollection( std::make_unique<ScChartListenerCollection>(
158 *pChartListenerCollection ), bUndoRefFirst );
160 if (pDBCollection || pRangeName)
162 sc::AutoCalcSwitch aACSwitch(*pDoc, false);
163 pDoc->CompileAll();
165 sc::SetFormulaDirtyContext aCxt;
166 pDoc->SetAllFormulasDirty(aCxt);
169 if (pAreaLinks)
170 pAreaLinks->Restore( pDoc );
172 if ( pUnoRefs )
173 pUnoRefs->Undo( pDoc );
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */