Warning, file /pim/mailcommon/src/job/expiremovejob.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /**
0002  * SPDX-FileCopyrightText: 2024 Laurent Montel <montel@kde.org>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "expiremovejob.h"
0008 #include "akonadi/messageflags.h"
0009 #include "mailcommon_debug.h"
0010 #include <Akonadi/ItemModifyJob>
0011 #include <Akonadi/ItemMoveJob>
0012 #include <KLocalizedString>
0013 #include <PimCommon/BroadcastStatus>
0014 
0015 using PimCommon::BroadcastStatus;
0016 
0017 ExpireMoveJob::ExpireMoveJob(QObject *parent)
0018     : QObject{parent}
0019 {
0020 }
0021 
0022 ExpireMoveJob::~ExpireMoveJob() = default;
0023 
0024 Akonadi::Item::List ExpireMoveJob::removedMsgs() const
0025 {
0026     return mRemovedMsgs;
0027 }
0028 
0029 void ExpireMoveJob::setRemovedMsgs(const Akonadi::Item::List &newRemovedMsgs)
0030 {
0031     mRemovedMsgs = newRemovedMsgs;
0032 }
0033 
0034 void ExpireMoveJob::start()
0035 {
0036     if (mRemovedMsgs.isEmpty()) {
0037         qCDebug(MAILCOMMON_LOG) << "ExpireMoveJob: nothing to do. Bug ?";
0038         finished();
0039         return;
0040     }
0041     Akonadi::Item::List ids;
0042     ids.reserve(100);
0043     for (const Akonadi::Item &item : mRemovedMsgs) {
0044         ids.append(item);
0045         if (ids.count() >= 100) {
0046             auto job = new Akonadi::ItemMoveJob(ids, mMoveToFolder, this);
0047             connect(job, &Akonadi::ItemMoveJob::result, this, &ExpireMoveJob::slotMoveDone);
0048 
0049             mRunningJobs.append(job);
0050             ids.clear();
0051             ids.reserve(100);
0052         }
0053     }
0054     if (!ids.isEmpty()) {
0055         auto job = new Akonadi::ItemMoveJob(ids, mMoveToFolder, this);
0056         mRunningJobs.append(job);
0057         connect(job, &Akonadi::ItemMoveJob::result, this, &ExpireMoveJob::slotMoveDone);
0058     }
0059 }
0060 
0061 void ExpireMoveJob::finished()
0062 {
0063     Q_EMIT expireMovedDone();
0064     deleteLater();
0065 }
0066 
0067 QString ExpireMoveJob::srcFolderName() const
0068 {
0069     return mSrcFolderName;
0070 }
0071 
0072 void ExpireMoveJob::setSrcFolderName(const QString &newSrcFolderName)
0073 {
0074     mSrcFolderName = newSrcFolderName;
0075 }
0076 
0077 Akonadi::Collection ExpireMoveJob::moveToFolder() const
0078 {
0079     return mMoveToFolder;
0080 }
0081 
0082 void ExpireMoveJob::setMoveToFolder(const Akonadi::Collection &newMoveToFolder)
0083 {
0084     mMoveToFolder = newMoveToFolder;
0085 }
0086 
0087 void ExpireMoveJob::slotMoveDone(KJob *job)
0088 {
0089     if (job->error()) {
0090         qCCritical(MAILCOMMON_LOG) << job->error() << job->errorString();
0091         deleteLater();
0092         return;
0093     }
0094     auto itemjob = qobject_cast<Akonadi::ItemMoveJob *>(job);
0095     if (itemjob) {
0096         const Akonadi::Item::List lst = itemjob->items();
0097         if (!lst.isEmpty()) {
0098             Akonadi::Item::List newLst;
0099             for (Akonadi::Item item : lst) {
0100                 if (!item.hasFlag(Akonadi::MessageFlags::Seen)) {
0101                     item.setFlag(Akonadi::MessageFlags::Seen);
0102                     newLst << item;
0103                 }
0104             }
0105             if (!newLst.isEmpty()) {
0106                 auto modifyJob = new Akonadi::ItemModifyJob(newLst, this);
0107                 modifyJob->disableRevisionCheck();
0108                 connect(modifyJob, &Akonadi::ItemModifyJob::result, this, &ExpireMoveJob::slotExpireDone);
0109             } else {
0110                 slotExpireDone(job);
0111             }
0112         }
0113     } else {
0114         slotExpireDone(job);
0115     }
0116 }
0117 
0118 void ExpireMoveJob::slotExpireDone(KJob *job)
0119 {
0120     if (job->error()) {
0121         qCCritical(MAILCOMMON_LOG) << job->error() << job->errorString();
0122     }
0123     QString msg;
0124     const int error = job->error();
0125     switch (error) {
0126     case KJob::NoError:
0127         msg = i18np("Moved 1 old message from folder %2 to folder %3.",
0128                     "Moved %1 old messages from folder %2 to folder %3.",
0129                     mRemovedMsgs.count(),
0130                     mSrcFolderName,
0131                     mMoveToFolder.name());
0132         break;
0133 
0134     case Akonadi::Job::UserCanceled:
0135         msg = i18n(
0136             "Moving old messages from folder %1 to folder %2 was "
0137             "canceled.",
0138             mSrcFolderName,
0139             mMoveToFolder.name());
0140         break;
0141 
0142     default: // any other error
0143         msg = i18n("Moving old messages from folder %1 to folder %2 failed.", mSrcFolderName, mMoveToFolder.name());
0144         break;
0145     }
0146 
0147     mRunningJobs.removeOne(job);
0148     if (mRunningJobs.isEmpty()) {
0149         finished();
0150     }
0151     BroadcastStatus::instance()->setStatusMsg(msg);
0152 }
0153 
0154 #include "moc_expiremovejob.cpp"