File indexing completed on 2024-06-23 05:21:13

0001 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
0002 
0003    This file is part of the Trojita Qt IMAP e-mail client,
0004    http://trojita.flaska.net/
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public License as
0008    published by the Free Software Foundation; either version 2 of
0009    the License or (at your option) version 3 or any later version
0010    accepted by the membership of KDE e.V. (or its successor approved
0011    by the membership of KDE e.V.), which shall act as a proxy
0012    defined in Section 14 of version 3 of the license.
0013 
0014    This program is distributed in the hope that it will be useful,
0015    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017    GNU General Public License for more details.
0018 
0019    You should have received a copy of the GNU General Public License
0020    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 
0024 #include "DeleteMailboxTask.h"
0025 #include "Common/InvokeMethod.h"
0026 #include "Imap/Model/ItemRoles.h"
0027 #include "Imap/Model/Model.h"
0028 #include "Imap/Model/MailboxTree.h"
0029 #include "GetAnyConnectionTask.h"
0030 #include "KeepMailboxOpenTask.h"
0031 
0032 namespace Imap
0033 {
0034 namespace Mailbox
0035 {
0036 
0037 
0038 DeleteMailboxTask::DeleteMailboxTask(Model *model, const QString &mailbox):
0039     ImapTask(model), conn(0), mailbox(mailbox)
0040 {
0041     // If the mailbox we're about to delete is open, or scheduled to be open, let's make
0042     // sure that we are going to tell the KeepMailboxOpenTask about us.
0043     // The mailbox discovery is lazy; we most definitely do *not* want to trigger an explicit
0044     // resynchronization just for that mailbox we're going to immediately delete. That's why
0045     // we are *not* calling Model::findTaskResponsibleFor here.
0046     if (TreeItemMailbox *mailboxPtr = model->findMailboxByName(mailbox)) {
0047         if (mailboxPtr->maintainingTask) {
0048             conn = mailboxPtr->maintainingTask;
0049         }
0050     }
0051 
0052     if (!conn) {
0053         // Either the mailbox is not known or converted to an index,
0054         // or there's no task associated with the mailbox
0055         conn = model->m_taskFactory->createGetAnyConnectionTask(model);
0056     }
0057 
0058     conn->addDependentTask(this);
0059 }
0060 
0061 void DeleteMailboxTask::perform()
0062 {
0063     parser = conn->parser;
0064     markAsActiveTask();
0065 
0066     IMAP_TASK_CHECK_ABORT_DIE;
0067 
0068     tag = parser->deleteMailbox(mailbox);
0069 }
0070 
0071 bool DeleteMailboxTask::handleStateHelper(const Imap::Responses::State *const resp)
0072 {
0073     if (resp->tag.isEmpty())
0074         return false;
0075 
0076     if (resp->tag == tag) {
0077 
0078         if (resp->kind == Responses::OK) {
0079             TreeItemMailbox *mailboxPtr = model->findMailboxByName(mailbox);
0080             if (mailboxPtr) {
0081                 TreeItem *parentPtr = mailboxPtr->parent();
0082                 QModelIndex parentIndex = parentPtr == model->m_mailboxes ? QModelIndex() : parentPtr->toIndex(model);
0083                 model->beginRemoveRows(parentIndex, mailboxPtr->row(), mailboxPtr->row());
0084                 mailboxPtr->parent()->m_children.erase(mailboxPtr->parent()->m_children.begin() + mailboxPtr->row());
0085                 model->endRemoveRows();
0086                 delete mailboxPtr;
0087             } else {
0088                 QString buf;
0089                 QDebug dbg(&buf);
0090                 dbg << "The IMAP server just told us that it succeeded to delete mailbox named" <<
0091                     mailbox << ", yet we don't know of any such mailbox. Message from the server:" <<
0092                     resp->message;
0093                 log(buf);
0094             }
0095             EMIT_LATER(model, mailboxDeletionSucceded, Q_ARG(QString, mailbox));
0096             _completed();
0097         } else {
0098             EMIT_LATER(model, mailboxDeletionFailed, Q_ARG(QString, mailbox), Q_ARG(QString, resp->message));
0099             _failed(tr("Couldn't delete mailbox"));
0100         }
0101         return true;
0102     } else {
0103         return false;
0104     }
0105 }
0106 
0107 QVariant DeleteMailboxTask::taskData(const int role) const
0108 {
0109     return role == RoleTaskCompactName ? QVariant(tr("Deleting mailbox")) : QVariant();
0110 }
0111 
0112 void DeleteMailboxTask::mailboxHasPendingActions()
0113 {
0114     EMIT_LATER(model, mailboxDeletionFailed, Q_ARG(QString, mailbox),
0115                Q_ARG(QString, tr("Mailbox %1 has pending activity, so it cannot be deleted now.").arg(mailbox)));
0116     _failed(tr("Mailbox has pending activity"));
0117 }
0118 
0119 }
0120 }