Mailcommon

expiremovejob.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 "expiremovejob.h"
8#include "akonadi/messageflags.h"
9#include "mailcommon_debug.h"
10#include <Akonadi/ItemModifyJob>
11#include <Akonadi/ItemMoveJob>
12#include <KLocalizedString>
13#include <PimCommon/BroadcastStatus>
14
15using PimCommon::BroadcastStatus;
16
17ExpireMoveJob::ExpireMoveJob(QObject *parent)
18 : QObject{parent}
19{
20}
21
22ExpireMoveJob::~ExpireMoveJob() = default;
23
24Akonadi::Item::List ExpireMoveJob::removedMsgs() const
25{
26 return mRemovedMsgs;
27}
28
29void ExpireMoveJob::setRemovedMsgs(const Akonadi::Item::List &newRemovedMsgs)
30{
31 mRemovedMsgs = newRemovedMsgs;
32}
33
34void ExpireMoveJob::start()
35{
36 if (mRemovedMsgs.isEmpty()) {
37 qCDebug(MAILCOMMON_LOG) << "ExpireMoveJob: nothing to do. Bug ?";
38 finished();
39 return;
40 }
42 ids.reserve(100);
43 for (const Akonadi::Item &item : mRemovedMsgs) {
44 ids.append(item);
45 if (ids.count() >= 100) {
46 auto job = new Akonadi::ItemMoveJob(ids, mMoveToFolder, this);
47 connect(job, &Akonadi::ItemMoveJob::result, this, &ExpireMoveJob::slotMoveDone);
48
49 mRunningJobs.append(job);
50 ids.clear();
51 ids.reserve(100);
52 }
53 }
54 if (!ids.isEmpty()) {
55 auto job = new Akonadi::ItemMoveJob(ids, mMoveToFolder, this);
56 mRunningJobs.append(job);
57 connect(job, &Akonadi::ItemMoveJob::result, this, &ExpireMoveJob::slotMoveDone);
58 }
59}
60
61void ExpireMoveJob::finished()
62{
63 Q_EMIT expireMovedDone();
65}
66
67QString ExpireMoveJob::srcFolderName() const
68{
69 return mSrcFolderName;
70}
71
72void ExpireMoveJob::setSrcFolderName(const QString &newSrcFolderName)
73{
74 mSrcFolderName = newSrcFolderName;
75}
76
77Akonadi::Collection ExpireMoveJob::moveToFolder() const
78{
79 return mMoveToFolder;
80}
81
82void ExpireMoveJob::setMoveToFolder(const Akonadi::Collection &newMoveToFolder)
83{
84 mMoveToFolder = newMoveToFolder;
85}
86
87void ExpireMoveJob::slotMoveDone(KJob *job)
88{
89 if (job->error()) {
90 qCCritical(MAILCOMMON_LOG) << job->error() << job->errorString();
92 return;
93 }
94 auto itemjob = qobject_cast<Akonadi::ItemMoveJob *>(job);
95 if (itemjob) {
96 const Akonadi::Item::List lst = itemjob->items();
97 if (!lst.isEmpty()) {
99 for (Akonadi::Item item : lst) {
100 if (!item.hasFlag(Akonadi::MessageFlags::Seen)) {
101 item.setFlag(Akonadi::MessageFlags::Seen);
102 newLst << item;
103 }
104 }
105 if (!newLst.isEmpty()) {
106 auto modifyJob = new Akonadi::ItemModifyJob(newLst, this);
107 modifyJob->disableRevisionCheck();
108 connect(modifyJob, &Akonadi::ItemModifyJob::result, this, &ExpireMoveJob::slotExpireDone);
109 } else {
110 slotExpireDone(job);
111 }
112 }
113 } else {
114 slotExpireDone(job);
115 }
116}
117
118void ExpireMoveJob::slotExpireDone(KJob *job)
119{
120 if (job->error()) {
121 qCCritical(MAILCOMMON_LOG) << job->error() << job->errorString();
122 }
123 QString msg;
124 const int error = job->error();
125 switch (error) {
126 case KJob::NoError:
127 msg = i18np("Moved 1 old message from folder %2 to folder %3.",
128 "Moved %1 old messages from folder %2 to folder %3.",
129 mRemovedMsgs.count(),
130 mSrcFolderName,
131 mMoveToFolder.name());
132 break;
133
135 msg = i18n(
136 "Moving old messages from folder %1 to folder %2 was "
137 "canceled.",
138 mSrcFolderName,
139 mMoveToFolder.name());
140 break;
141
142 default: // any other error
143 msg = i18n("Moving old messages from folder %1 to folder %2 failed.", mSrcFolderName, mMoveToFolder.name());
144 break;
145 }
146
147 mRunningJobs.removeOne(job);
148 if (mRunningJobs.isEmpty()) {
149 finished();
150 }
151 BroadcastStatus::instance()->setStatusMsg(msg);
152}
153
154#include "moc_expiremovejob.cpp"
QString name() const
virtual QString errorString() const
int error() const
QString i18np(const char *singular, const char *plural, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
AKONADI_MIME_EXPORT const char Seen[]
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()
T qobject_cast(QObject *object)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:00:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.