File indexing completed on 2024-06-16 04:47:16

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 is a plugin for delete operation.
0008  *
0009  * @author Stephane MANKOWSKI / Guillaume DE BURE
0010  */
0011 #include "skgdeleteplugin.h"
0012 
0013 #include <qwidget.h>
0014 
0015 #include <kaboutdata.h>
0016 #include <kactioncollection.h>
0017 #include <klocalizedstring.h>
0018 #include <kmessagebox.h>
0019 #include <kpluginfactory.h>
0020 #include <kstandardaction.h>
0021 
0022 #include "skgerror.h"
0023 #include "skgmainpanel.h"
0024 #include "skgobjectbase.h"
0025 #include "skgtraces.h"
0026 #include "skgtransactionmng.h"
0027 
0028 /**
0029  * This plugin factory.
0030  */
0031 K_PLUGIN_CLASS_WITH_JSON(SKGDeletePlugin, "metadata.json")
0032 
0033 SKGDeletePlugin::SKGDeletePlugin(QWidget* iWidget, QObject* iParent, const QVariantList& /*iArg*/) :
0034     SKGInterfacePlugin(iParent), m_currentDocument(nullptr)
0035 {
0036     Q_UNUSED(iWidget)
0037     SKGTRACEINFUNC(10)
0038 }
0039 
0040 SKGDeletePlugin::~SKGDeletePlugin()
0041 {
0042     SKGTRACEINFUNC(10)
0043     m_currentDocument = nullptr;
0044 }
0045 
0046 bool SKGDeletePlugin::setupActions(SKGDocument* iDocument)
0047 {
0048     SKGTRACEINFUNC(10)
0049 
0050     m_currentDocument = iDocument;
0051 
0052     setComponentName(QStringLiteral("skg_delete"), title());
0053     setXMLFile(QStringLiteral("skg_delete.rc"));
0054 
0055     // Menu
0056     QStringList tmp;
0057     m_currentDocument->getDistinctValues(QStringLiteral("sqlite_master"), QStringLiteral("name"), QStringLiteral("type='table' AND name!='parameters'"), tmp);
0058     auto actDelete = new QAction(SKGServices::fromTheme(QStringLiteral("edit-delete")), i18nc("Verb, delete an item", "Delete"), this);
0059     connect(actDelete, &QAction::triggered, this, &SKGDeletePlugin::onDelete);
0060     actionCollection()->setDefaultShortcut(actDelete, Qt::Key_Delete);
0061     registerGlobalAction(QStringLiteral("edit_delete"), actDelete, tmp, 1, -1, 200, true);
0062     return true;
0063 }
0064 
0065 QString SKGDeletePlugin::title() const
0066 {
0067     return i18nc("Verb, delete an item", "Delete");
0068 }
0069 
0070 int SKGDeletePlugin::getOrder() const
0071 {
0072     return 5;
0073 }
0074 
0075 void SKGDeletePlugin::onDelete()
0076 {
0077     SKGError err;
0078     SKGTRACEINFUNCRC(10, err)
0079     if ((SKGMainPanel::getMainPanel() != nullptr) && (m_currentDocument != nullptr)) {
0080         SKGObjectBase::SKGListSKGObjectBase selection = SKGMainPanel::getMainPanel()->getSelectedObjects();
0081         int nb = selection.count();
0082         if (nb > 0) {
0083             if (selection[0].getRealTable() == QStringLiteral("doctransaction")) {
0084                 err = m_currentDocument->beginTransaction(QStringLiteral("#INTERNAL#"));
0085                 IFOKDO(err, m_currentDocument->executeSqliteOrder(QStringLiteral("CREATE TRIGGER fkdc_doctransaction_doctransaction_i_parent_id "
0086                         "BEFORE DELETE ON doctransaction "
0087                         "FOR EACH ROW BEGIN "
0088                         "    DELETE FROM doctransaction WHERE OLD.t_name!='#INTERNAL#' AND doctransaction.id = OLD.i_parent; "
0089                         "END")));
0090                 for (int i = 0; !err && i < nb; ++i) {
0091                     err = selection.at(i).remove();
0092                 }
0093                 m_currentDocument->executeSqliteOrder(QStringLiteral("DROP TRIGGER IF EXISTS fkdc_doctransaction_doctransaction_i_parent_id"));
0094                 SKGENDTRANSACTION(m_currentDocument,  err)
0095             } else {
0096                 SKGBEGINPROGRESSTRANSACTION(*m_currentDocument, i18nc("Verb, delete an item", "Delete"), err, nb)
0097                 for (int i = 0; !err && i < nb; ++i) {
0098                     err = selection.at(i).remove();
0099                     if (err && err.getReturnCode() == ERR_FORCEABLE) {
0100                         QApplication::setOverrideCursor(QCursor(Qt::ArrowCursor));
0101 #ifdef SKG_KF_5102
0102                         int rc = KMessageBox::questionTwoActions(SKGMainPanel::getMainPanel(),
0103                                  err.getFullMessage() % '\n' % i18nc("Question",  "Do you want to force the deletion ?"),
0104                                  i18nc("Question",  "Do you want to force the deletion ?"),
0105                                  KStandardGuiItem::del(), KStandardGuiItem::cancel(),
0106                                  QStringLiteral("forcedelete"));
0107                         QApplication::restoreOverrideCursor();
0108                         if (rc == KMessageBox::PrimaryAction) {
0109 #else
0110                         int rc = KMessageBox::questionYesNo(SKGMainPanel::getMainPanel(),
0111                                                             err.getFullMessage() % '\n' % i18nc("Question",  "Do you want to force the deletion ?"),
0112                                                             i18nc("Question",  "Do you want to force the deletion ?"),
0113                                                             KStandardGuiItem::yes(), KStandardGuiItem::no(),
0114                                                             QStringLiteral("forcedelete"));
0115                         QApplication::restoreOverrideCursor();
0116                         if (rc == KMessageBox::Yes) {
0117 #endif
0118                             err = selection.at(i).remove(true, true);
0119                         }
0120                     }
0121                     IFOKDO(err, m_currentDocument->stepForward(i + 1))
0122                 }
0123             }
0124         }
0125 
0126         //
0127         KMessageBox::enableMessage(QStringLiteral("forcedelete"));
0128 
0129         // status bar
0130         IFOKDO(err, SKGError(0, i18nc("Successful message after an user action", "Objects deleted.")))
0131     }
0132 
0133     // Display error
0134     SKGMainPanel::displayErrorMessage(err, true);
0135 }
0136 
0137 #include <skgdeleteplugin.moc>