File indexing completed on 2024-06-23 05:21:16
0001 /* Copyright (C) 2013 Yasser Aziza <yasser.aziza@gmail.com> 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 "UpdateFlagsOfAllMessagesTask.h" 0025 #include "Imap/Model/ItemRoles.h" 0026 #include "Imap/Model/MailboxTree.h" 0027 #include "Imap/Model/Model.h" 0028 #include "KeepMailboxOpenTask.h" 0029 0030 namespace Imap 0031 { 0032 namespace Mailbox 0033 { 0034 0035 UpdateFlagsOfAllMessagesTask::UpdateFlagsOfAllMessagesTask(Model *model, const QModelIndex &mailboxIndex, 0036 const FlagsOperation flagOperation, const QString &flags): 0037 ImapTask(model), flagOperation(flagOperation), flags(flags), mailboxIndex(mailboxIndex) 0038 { 0039 Q_ASSERT(mailboxIndex.isValid()); 0040 Q_ASSERT(flagOperation == Imap::Mailbox::FLAG_ADD || flagOperation == Imap::Mailbox::FLAG_ADD_SILENT); 0041 conn = model->findTaskResponsibleFor(mailboxIndex); 0042 conn->addDependentTask(this); 0043 } 0044 0045 void UpdateFlagsOfAllMessagesTask::perform() 0046 { 0047 Q_ASSERT(conn); 0048 parser = conn->parser; 0049 0050 markAsActiveTask(); 0051 IMAP_TASK_CHECK_ABORT_DIE; 0052 0053 Sequence seq = Sequence::startingAt(1); 0054 tag = parser->store(seq, toImapString(flagOperation), flags); 0055 } 0056 0057 bool UpdateFlagsOfAllMessagesTask::handleStateHelper(const Imap::Responses::State *const resp) 0058 { 0059 if (resp->tag.isEmpty()) 0060 return false; 0061 0062 if (resp->tag == tag) { 0063 if (resp->kind == Responses::OK) { 0064 TreeItemMailbox *mailbox = model->mailboxForSomeItem(mailboxIndex); 0065 if (!mailbox) { 0066 // There isn't much to be done here -- let's assume that the index has disappeared. 0067 // The flags will be resynced the next time we open that mailbox. 0068 _failed(QStringLiteral("Mailbox is gone")); 0069 return true; 0070 } 0071 Q_ASSERT(mailbox); 0072 TreeItemMsgList *list = dynamic_cast<TreeItemMsgList*>(mailbox->m_children [0]); 0073 Q_ASSERT(list); 0074 0075 Q_FOREACH (TreeItem *item, list->m_children) { 0076 TreeItemMessage *message = dynamic_cast<TreeItemMessage *>(item); 0077 Q_ASSERT(message); 0078 0079 if (message->uid() == 0) { 0080 // UID not determined yet, so we cannot really modify its flags 0081 continue; 0082 } 0083 0084 Q_ASSERT(flagOperation == Imap::Mailbox::FLAG_ADD || flagOperation == Imap::Mailbox::FLAG_ADD_SILENT); 0085 QStringList newFlags = message->m_flags; 0086 if (!newFlags.contains(flags)) { 0087 newFlags << flags; 0088 message->setFlags(list, model->normalizeFlags(newFlags)); 0089 model->cache()->setMsgFlags(mailbox->mailbox(), message->uid(), newFlags); 0090 QModelIndex messageIndex = model->createIndex(message->m_offset, 0, message); 0091 0092 // emitting dataChanged() separately for each message in the mailbox: 0093 // Trojita model assmues that dataChanged is emitted individually 0094 model->dataChanged(messageIndex, messageIndex); 0095 } 0096 } 0097 model->emitMessageCountChanged(mailbox); 0098 list->fetchNumbers(model); 0099 _completed(); 0100 } else { 0101 _failed(tr("Failed to update Mailbox FLAGS")); 0102 } 0103 return true; 0104 } 0105 return false; 0106 } 0107 0108 QVariant UpdateFlagsOfAllMessagesTask::taskData(const int role) const 0109 { 0110 return role == RoleTaskCompactName ? QVariant(tr("Saving mailbox state")) : QVariant(); 0111 } 0112 0113 } 0114 }