File indexing completed on 2024-04-28 16:30:34

0001 /***************************************************************************
0002  * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr
0003  * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  ***************************************************************************/
0006 /** @file
0007 * This file implements classes SKGTransactionMng.
0008 *
0009 * @author Stephane MANKOWSKI / Guillaume DE BURE
0010 */
0011 #include "skgtransactionmng.h"
0012 #include "skgdocument.h"
0013 #include "skgerror.h"
0014 
0015 SKGTransactionMng::SKGTransactionMng(SKGDocument* iDocument,
0016                                      const QString& iName,
0017                                      SKGError* iError,
0018                                      int iNbStep,
0019                                      bool iRefreshViews)
0020 {
0021     m_parentDocument = iDocument;
0022     m_error = iError;
0023     m_errorInBeginTransaction = false;
0024     if ((m_parentDocument != nullptr) && (m_error != nullptr)) {
0025         *m_error = m_parentDocument->beginTransaction(iName, iNbStep, QDateTime::currentDateTime(), iRefreshViews);
0026         m_errorInBeginTransaction = (m_error->isFailed());
0027     }
0028 }
0029 
0030 SKGTransactionMng::~SKGTransactionMng()
0031 {
0032     if ((m_parentDocument != nullptr) && (m_error != nullptr)) {
0033         // close the transaction based on error
0034         if (!m_errorInBeginTransaction) {
0035             if (m_error->isSucceeded()) {
0036                 SKGError opError = *m_error;  // In case of the message is not empty
0037                 *m_error = m_parentDocument->endTransaction(true);
0038                 if (m_error->isSucceeded()) {
0039                     *m_error = opError;
0040                 }
0041             } else {
0042                 m_parentDocument->endTransaction(false);
0043             }
0044         }
0045         m_parentDocument = nullptr;
0046         m_error = nullptr;
0047     }
0048 }