Mailcommon

expiredeletejob.cpp
1/**
2 * SPDX-FileCopyrightText: 2024 Laurent Montel <montel@kde.org>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include "expiredeletejob.h"
8#include "mailcommon_debug.h"
9#include <Akonadi/ItemDeleteJob>
10#include <KLocalizedString>
11#include <PimCommon/BroadcastStatus>
12using PimCommon::BroadcastStatus;
13
14ExpireDeleteJob::ExpireDeleteJob(QObject *parent)
15 : QObject{parent}
16{
17}
18
19ExpireDeleteJob::~ExpireDeleteJob() = default;
20
21Akonadi::Item::List ExpireDeleteJob::removedMsgs() const
22{
23 return mRemovedMsgs;
24}
25
26void ExpireDeleteJob::setRemovedMsgs(const Akonadi::Item::List &newRemovedMsgs)
27{
28 mRemovedMsgs = newRemovedMsgs;
29}
30
31void ExpireDeleteJob::start()
32{
33 if (mRemovedMsgs.isEmpty()) {
34 qCDebug(MAILCOMMON_LOG) << "ExpireDeleteJob: nothing to do";
35 finished();
36 return;
37 }
39 ids.reserve(100);
40 for (const Akonadi::Item &item : mRemovedMsgs) {
41 ids.append(item);
42 if (ids.count() >= 100) {
43 auto job = new Akonadi::ItemDeleteJob(ids, this);
44 mRunningJobs.append(job);
45 connect(job, &Akonadi::ItemDeleteJob::result, this, &ExpireDeleteJob::slotExpireDone);
46 ids.clear();
47 ids.reserve(100);
48 }
49 }
50 if (!ids.isEmpty()) {
51 auto job = new Akonadi::ItemDeleteJob(ids, this);
52 mRunningJobs.append(job);
53 connect(job, &Akonadi::ItemDeleteJob::result, this, &ExpireDeleteJob::slotExpireDone);
54 }
55}
56
57void ExpireDeleteJob::slotExpireDone(KJob *job)
58{
59 if (job->error()) {
60 qCCritical(MAILCOMMON_LOG) << job->error() << job->errorString();
61 }
62 QString msg;
63 const int error = job->error();
64 switch (error) {
65 case KJob::NoError:
66 msg = i18np("Removed 1 old message from folder %2.", "Removed %1 old messages from folder %2.", mRemovedMsgs.count(), mSourceFolderName);
67 break;
68
70 msg = i18n("Removing old messages from folder %1 was canceled.", mSourceFolderName);
71 break;
72
73 default: // any other error
74 msg = i18n("Removing old messages from folder %1 failed.", mSourceFolderName);
75 break;
76 }
77
78 // TODO cancel all job when user cancel it.
79 // Return when we have an error.
80
81 mRunningJobs.removeOne(job);
82 if (mRunningJobs.isEmpty()) {
83 finished();
84 }
85 BroadcastStatus::instance()->setStatusMsg(msg);
86}
87
88QString ExpireDeleteJob::sourceFolderName() const
89{
90 return mSourceFolderName;
91}
92
93void ExpireDeleteJob::setSourceFolderName(const QString &newSourceFolderName)
94{
95 mSourceFolderName = newSourceFolderName;
96}
97
98void ExpireDeleteJob::finished()
99{
100 Q_EMIT expireDeleteDone();
101 deleteLater();
102}
103
104#include "moc_expiredeletejob.cpp"
virtual QString errorString() const
int error() const
void result(KJob *job)
QString i18np(const char *singular, const char *plural, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
void append(QList< T > &&value)
void clear()
qsizetype count() const const
bool isEmpty() const const
bool removeOne(const AT &t)
void reserve(qsizetype size)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:12:19 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.