Debian Codegraph @

src/App/AutoTransaction.cpp

raw
1 /****************************************************************************
2 * Copyright (c) 2019 Zheng Lei (realthunder) <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ****************************************************************************/
22
23
24 #include <Base/Interpreter.h>
25
26 #include "AutoTransaction.h"
27 #include "Application.h"
28 #include "Document.h"
29 #include "Transactions.h"
30
31
32 FC_LOG_LEVEL_INIT("App", true, true)
33
34 using namespace App;
35
36 static int _TransactionLock;
37 static int _TransactionClosed;
38
39 AutoTransaction::AutoTransaction(const char* name, bool tmpName)
40 {
41 auto& app = GetApplication();
42 if (name && app._activeTransactionGuard >= 0) {
43 if (!app.getActiveTransaction() || (!tmpName && app._activeTransactionTmpName)) {
44 FC_LOG("auto transaction '" << name << "', " << tmpName);
45 tid = app.setActiveTransaction(name);
46 app._activeTransactionTmpName = tmpName;
47 }
48 }
49 // We use negative transaction guard to disable auto transaction from here
50 // and any stack below. This is to support user setting active transaction
51 // before having any existing AutoTransaction on stack, or 'persist'
52 // transaction that can out live AutoTransaction.
53 if (app._activeTransactionGuard < 0) {
54 --app._activeTransactionGuard;
55 }
56 else if (tid || app._activeTransactionGuard > 0) {
57 ++app._activeTransactionGuard;
58 }
59 else if (app.getActiveTransaction()) {
60 FC_LOG("auto transaction disabled because of '" << app._activeTransactionName << "'");
61 --app._activeTransactionGuard;
62 }
63 else {
64 ++app._activeTransactionGuard;
65 }
66 FC_TRACE("construct auto Transaction " << app._activeTransactionGuard);
67 }
68
69 AutoTransaction::~AutoTransaction()
70 {
71 auto& app = GetApplication();
72 FC_TRACE("before destruct auto Transaction " << app._activeTransactionGuard);
73 if (app._activeTransactionGuard < 0) {
74 ++app._activeTransactionGuard;
75 }
76 else if (!app._activeTransactionGuard) {
77 #ifdef FC_DEBUG
78 FC_ERR("Transaction guard error");
79 #endif
80 }
81 else if (--app._activeTransactionGuard == 0) {
82 try {
83 // We don't call close() here, because close() only closes
84 // transaction that we opened during construction time. However,
85 // when _activeTransactionGuard reaches zero here, we are supposed
86 // to close any transaction opened.
87 app.closeActiveTransaction();
88 }
89 catch (Base::Exception& e) {
90 e.reportException();
91 }
92 catch (...) {
93 }
94 }
95 FC_TRACE("destruct auto Transaction " << app._activeTransactionGuard);
96 }
97
98 void AutoTransaction::close(bool abort)
99 {
100 if (tid || abort) {
101 GetApplication().closeActiveTransaction(abort, abort ? 0 : tid);
102 tid = 0;
103 }
104 }
105
106 void AutoTransaction::setEnable(bool enable)
107 {
108 auto& app = GetApplication();
109 if (!app._activeTransactionGuard) {
110 return;
111 }
112 if ((enable && app._activeTransactionGuard > 0)
113 || (!enable && app._activeTransactionGuard < 0)) {
114 return;
115 }
116 app._activeTransactionGuard = -app._activeTransactionGuard;
117 FC_TRACE("toggle auto Transaction " << app._activeTransactionGuard);
118 if (!enable && app._activeTransactionTmpName) {
119 bool close = true;
120 for (auto& v : app.DocMap) {
121 if (v.second->hasPendingTransaction()) {
122 close = false;
123 break;
124 }
125 }
126 if (close) {
127 app.closeActiveTransaction();
128 }
129 }
130 }
131
132 int Application::setActiveTransaction(const char* name, bool persist)
133 {
134
135 if (!name || !name[0]) {
136 name = "Command";
137 }
138
139 this->signalBeforeOpenTransaction(name);
140 if (_activeTransactionGuard > 0 && getActiveTransaction()) {
141 if (_activeTransactionTmpName) {
142 FC_LOG("transaction rename to '" << name << "'");
143 for (auto& v : DocMap) {
144 v.second->renameTransaction(name, _activeTransactionID);
145 }
146 }
147 else {
148 if (persist) {
149 AutoTransaction::setEnable(false);
150 }
151 return 0;
152 }
153 }
154 else if (_TransactionLock) {
155 if (FC_LOG_INSTANCE.isEnabled(FC_LOGLEVEL_LOG)) {
156 FC_WARN("Transaction locked, ignore new transaction '" << name << "'");
157 }
158 return 0;
159 }
160 else {
161 FC_LOG("set active transaction '" << name << "'");
162 _activeTransactionID = 0;
163 for (auto& v : DocMap) {
164 v.second->_commitTransaction();
165 }
166 _activeTransactionID = Transaction::getNewID();
167 }
168 _activeTransactionTmpName = false;
169 _activeTransactionName = name;
170 if (persist) {
171 AutoTransaction::setEnable(false);
172 }
173 return _activeTransactionID;
174 }
175
176 const char* Application::getActiveTransaction(int* id) const
177 {
178 int tid = 0;
179 if (Transaction::getLastID() == _activeTransactionID) {
180 tid = _activeTransactionID;
181 }
182 if (id) {
183 *id = tid;
184 }
185 return tid ? _activeTransactionName.c_str() : nullptr;
186 }
187
188 void Application::closeActiveTransaction(bool abort, int id)
189 {
190 if (!id) {
191 id = _activeTransactionID;
192 }
193 if (!id) {
194 return;
195 }
196
197 if (_activeTransactionGuard > 0 && !abort) {
198 FC_LOG("ignore close transaction");
199 return;
200 }
201
202 if (_TransactionLock) {
203 if (_TransactionClosed >= 0) {
204 _TransactionLock = abort ? -1 : 1;
205 }
206 FC_LOG("pending " << (abort ? "abort" : "close") << " transaction");
207 return;
208 }
209
210 FC_LOG("close transaction '" << _activeTransactionName << "' " << abort);
211 _activeTransactionID = 0;
212
213 TransactionSignaller signaller(abort, false);
214 for (auto& v : DocMap) {
215 if (v.second->getTransactionID(true) != id) {
216 continue;
217 }
218 if (abort) {
219 v.second->_abortTransaction();
220 }
221 else {
222 v.second->_commitTransaction();
223 }
224 }
225 }
226
227 ////////////////////////////////////////////////////////////////////////
228
229 TransactionLocker::TransactionLocker(bool lock)
230 : active(lock)
231 {
232 if (lock) {
233 ++_TransactionLock;
234 }
235 }
236
237 TransactionLocker::~TransactionLocker()
238 {
239 if (active) {
240 try {
241 activate(false);
242 return;
243 }
244 catch (Base::Exception& e) {
245 e.reportException();
246 }
247 catch (Py::Exception&) {
248 Base::PyException e;
249 e.reportException();
250 }
251 catch (std::exception& e) {
252 FC_ERR(e.what());
253 }
254 catch (...) {
255 }
256 FC_ERR("Exception when unlocking transaction");
257 }
258 }
259
260 void TransactionLocker::activate(bool enable)
261 {
262 if (active == enable) {
263 return;
264 }
265
266 active = enable;
267 if (active) {
268 ++_TransactionLock;
269 return;
270 }
271
272 if (--_TransactionLock != 0) {
273 return;
274 }
275
276 if (_TransactionClosed) {
277 bool abort = (_TransactionClosed < 0);
278 _TransactionClosed = 0;
279 GetApplication().closeActiveTransaction(abort);
280 }
281 }
282
283 bool TransactionLocker::isLocked()
284 {
285 return _TransactionLock > 0;
286 }