File indexing completed on 2024-11-24 04:44:18

0001 /*
0002     SPDX-FileCopyrightText: 2014 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
0003     SPDX-FileContributor: Kevin Krammer <kevin.krammer@kdab.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "kolabremovetagtask.h"
0009 #include "imapflags.h"
0010 #include "kolabresource_debug.h"
0011 #include "kolabresource_trace.h"
0012 
0013 #include <KIMAP/SelectJob>
0014 #include <KIMAP/Session>
0015 #include <KIMAP/StoreJob>
0016 
0017 KolabRemoveTagTask::KolabRemoveTagTask(const ResourceStateInterface::Ptr &resource, QObject *parent)
0018     : KolabRelationResourceTask(resource, parent)
0019 {
0020 }
0021 
0022 void KolabRemoveTagTask::startRelationTask(KIMAP::Session *session)
0023 {
0024     // The imap specs do not allow for a single message to be deleted. We can only
0025     // set the \Deleted flag. The message will actually be deleted when EXPUNGE will
0026     // be issued on the next retrieveItems().
0027 
0028     const QString mailBox = mailBoxForCollection(relationCollection());
0029 
0030     qCDebug(KOLABRESOURCE_LOG) << "Deleting tag " << resourceState()->tag().name() << " from " << mailBox;
0031 
0032     if (session->selectedMailBox() != mailBox) {
0033         auto select = new KIMAP::SelectJob(session);
0034         select->setMailBox(mailBox);
0035 
0036         connect(select, &KJob::result, this, &KolabRemoveTagTask::onSelectDone);
0037 
0038         select->start();
0039     } else {
0040         triggerStoreJob(session);
0041     }
0042 }
0043 
0044 void KolabRemoveTagTask::triggerStoreJob(KIMAP::Session *session)
0045 {
0046     KIMAP::ImapSet set;
0047     set.add(resourceState()->tag().remoteId().toLong());
0048     qCDebug(KOLABRESOURCE_TRACE) << set.toImapSequenceSet();
0049 
0050     auto store = new KIMAP::StoreJob(session);
0051     store->setUidBased(true);
0052     store->setSequenceSet(set);
0053     store->setFlags(QList<QByteArray>() << ImapFlags::Deleted);
0054     store->setMode(KIMAP::StoreJob::AppendFlags);
0055     connect(store, &KJob::result, this, &KolabRemoveTagTask::onStoreFlagsDone);
0056     store->start();
0057 }
0058 
0059 void KolabRemoveTagTask::onSelectDone(KJob *job)
0060 {
0061     if (job->error()) {
0062         qCWarning(KOLABRESOURCE_LOG) << "Failed to select mailbox: " << job->errorString();
0063         cancelTask(job->errorString());
0064     } else {
0065         auto select = static_cast<KIMAP::SelectJob *>(job);
0066         triggerStoreJob(select->session());
0067     }
0068 }
0069 
0070 void KolabRemoveTagTask::onStoreFlagsDone(KJob *job)
0071 {
0072     qCDebug(KOLABRESOURCE_TRACE);
0073     // TODO use UID EXPUNGE if available
0074     if (job->error()) {
0075         qCWarning(KOLABRESOURCE_LOG) << "Failed to append flags: " << job->errorString();
0076         cancelTask(job->errorString());
0077     } else {
0078         changeProcessed();
0079     }
0080 }
0081 
0082 #include "moc_kolabremovetagtask.cpp"