File indexing completed on 2024-12-22 05:01:15

0001 /*
0002    SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "undosendcreatejob.h"
0008 #include "kmail_undo_send_debug.h"
0009 
0010 #include <MessageComposer/SendLaterRemoveJob>
0011 
0012 #include <KLocalizedString>
0013 #include <KNotification>
0014 #include <QTimer>
0015 #include <chrono>
0016 using namespace std::chrono_literals;
0017 
0018 UndoSendCreateJob::UndoSendCreateJob(QObject *parent)
0019     : QObject(parent)
0020 {
0021 }
0022 
0023 UndoSendCreateJob::~UndoSendCreateJob() = default;
0024 
0025 bool UndoSendCreateJob::canStart() const
0026 {
0027     if (mAkonadiIndex < 0 || mDelay <= 0) {
0028         return false;
0029     }
0030     return true;
0031 }
0032 
0033 bool UndoSendCreateJob::start()
0034 {
0035     if (!canStart()) {
0036         qCWarning(KMAIL_UNDO_SEND_LOG) << "Impossible to start undosendcreatejob";
0037         deleteLater();
0038         return false;
0039     }
0040     mTimer = new QTimer(this);
0041     connect(mTimer, &QTimer::timeout, this, &UndoSendCreateJob::slotTimeOut);
0042     mTimer->setSingleShot(true);
0043     mTimer->start(mDelay * 1s);
0044     mNotification = new KNotification(QStringLiteral("undosend"), KNotification::Persistent, this);
0045     mNotification->setText(mSubject);
0046 
0047     auto undoSendAction = mNotification->addAction(i18n("Undo send"));
0048     connect(undoSendAction, &KNotificationAction::activated, this, &UndoSendCreateJob::undoSendEmail);
0049 
0050     connect(mNotification, &KNotification::closed, this, &UndoSendCreateJob::slotNotificationClosed);
0051     mNotification->sendEvent();
0052 
0053     return true;
0054 }
0055 
0056 void UndoSendCreateJob::slotTimeOut()
0057 {
0058     qCDebug(KMAIL_UNDO_SEND_LOG) << "undo send timeout";
0059     mNotification->close();
0060     deleteLater();
0061 }
0062 
0063 void UndoSendCreateJob::slotNotificationClosed()
0064 {
0065     qCDebug(KMAIL_UNDO_SEND_LOG) << "undo send slotNotificationClosed";
0066     mTimer->stop();
0067     deleteLater();
0068 }
0069 
0070 void UndoSendCreateJob::undoSendEmail()
0071 {
0072     mTimer->stop();
0073     auto job = new MessageComposer::SendLaterRemoveJob(mAkonadiIndex, this);
0074     job->start();
0075 }
0076 
0077 QString UndoSendCreateJob::subject() const
0078 {
0079     return mSubject;
0080 }
0081 
0082 void UndoSendCreateJob::setMessageInfoText(const QString &subject)
0083 {
0084     mSubject = subject;
0085 }
0086 
0087 int UndoSendCreateJob::delay() const
0088 {
0089     return mDelay;
0090 }
0091 
0092 void UndoSendCreateJob::setDelay(int delay)
0093 {
0094     mDelay = delay;
0095 }
0096 
0097 qint64 UndoSendCreateJob::akonadiIndex() const
0098 {
0099     return mAkonadiIndex;
0100 }
0101 
0102 void UndoSendCreateJob::setAkonadiIndex(qint64 akonadiIndex)
0103 {
0104     mAkonadiIndex = akonadiIndex;
0105 }
0106 
0107 #include "moc_undosendcreatejob.cpp"