File indexing completed on 2024-11-17 04:45:06

0001 /*
0002     SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
0003     SPDX-FileContributor: Kevin Ottens <kevin@kdab.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "expungecollectiontask.h"
0009 
0010 #include "imapresource_debug.h"
0011 
0012 #include <KIMAP/ExpungeJob>
0013 #include <KIMAP/SelectJob>
0014 #include <KIMAP/Session>
0015 
0016 #include "noselectattribute.h"
0017 
0018 ExpungeCollectionTask::ExpungeCollectionTask(const ResourceStateInterface::Ptr &resource, QObject *parent)
0019     : ResourceTask(CancelIfNoSession, resource, parent)
0020 {
0021 }
0022 
0023 ExpungeCollectionTask::~ExpungeCollectionTask() = default;
0024 
0025 void ExpungeCollectionTask::doStart(KIMAP::Session *session)
0026 {
0027     // Prevent expunging items from noselect folders.
0028     if (collection().hasAttribute("noselect")) {
0029         NoSelectAttribute *noselect = static_cast<NoSelectAttribute *>(collection().attribute("noselect"));
0030         if (noselect->noSelect()) {
0031             qCDebug(IMAPRESOURCE_LOG) << "No Select folder";
0032             taskDone();
0033             return;
0034         }
0035     }
0036 
0037     const QString mailBox = mailBoxForCollection(collection());
0038 
0039     if (session->selectedMailBox() != mailBox) {
0040         auto select = new KIMAP::SelectJob(session);
0041         select->setMailBox(mailBox);
0042 
0043         connect(select, &KIMAP::SelectJob::result, this, &ExpungeCollectionTask::onSelectDone);
0044 
0045         select->start();
0046     } else {
0047         triggerExpungeJob(session);
0048     }
0049 }
0050 
0051 void ExpungeCollectionTask::onSelectDone(KJob *job)
0052 {
0053     if (job->error()) {
0054         cancelTask(job->errorString());
0055     } else {
0056         auto select = static_cast<KIMAP::SelectJob *>(job);
0057         if (select->isOpenReadOnly()) {
0058             qCDebug(IMAPRESOURCE_LOG) << "Mailbox is opened readonly, not expunging";
0059             taskDone();
0060         } else {
0061             triggerExpungeJob(select->session());
0062         }
0063     }
0064 }
0065 
0066 void ExpungeCollectionTask::triggerExpungeJob(KIMAP::Session *session)
0067 {
0068     auto expunge = new KIMAP::ExpungeJob(session);
0069 
0070     connect(expunge, &KIMAP::ExpungeJob::result, this, &ExpungeCollectionTask::onExpungeDone);
0071 
0072     expunge->start();
0073 }
0074 
0075 void ExpungeCollectionTask::onExpungeDone(KJob *job)
0076 {
0077     if (job->error()) {
0078         cancelTask(job->errorString());
0079     } else {
0080         taskDone();
0081     }
0082 }
0083 
0084 #include "moc_expungecollectiontask.cpp"