| 1 | /* Copyright (C) 2016 - 2017 Dan Chapman <[email protected]> |
| 2 | |
| 3 | This file is part of Dekko email client for Ubuntu devices |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU General Public License as |
| 7 | published by the Free Software Foundation; either version 2 of |
| 8 | the License or (at your option) version 3 |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | #include "FolderList.h" |
| 19 | #include <algorithm> |
| 20 | #include <qmailfolder.h> |
| 21 | #include <qmailfolderkey.h> |
| 22 | #include <qmailstore.h> |
| 23 | |
| 24 | FolderList::FolderList(QObject *parent) : QObject(parent), m_model(0) |
| 25 | { |
| 26 | m_model = new QQmlObjectListModel<Folder>(this); |
| 27 | emit modelChanged(); |
| 28 | connect(this, &FolderList::accountIdChanged, this, &FolderList::reset); |
| 29 | reset(); |
| 30 | } |
| 31 | |
| 32 | void FolderList::reset() |
| 33 | { |
| 34 | m_model->clear(); |
| 35 | QMailFolderKey folderKey = QMailFolderKey::parentAccountId(m_accountId); |
| 36 | QMailFolderIdList folderList = QMailStore::instance()->queryFolders(folderKey); |
| 37 | std::sort(folderList.begin(), folderList.end(), FolderList::sortIdList); |
| 38 | |
| 39 | QMailMessageKey excludeKey = QMailMessageKey::status(QMailMessage::Removed, QMailDataComparator::Excludes); |
| 40 | QMailAccount account(m_accountId); |
| 41 | |
| 42 | QMailFolderId inboxId = account.standardFolder(QMailFolder::InboxFolder); |
| 43 | addFolder(inboxId, excludeKey, folderList); |
| 44 | |
| 45 | QMailFolderId draftsId = account.standardFolder(QMailFolder::DraftsFolder); |
| 46 | if (!draftsId.isValid()) { |
| 47 | insertFolder(QMailFolder::LocalStorageFolderId, Folder::SpecialUseDraftsFolder, |
| 48 | QMailMessageKey::status(QMailMessage::Draft) & |
| 49 | ~QMailMessageKey::status(QMailMessage::Outbox) & |
| 50 | ~QMailMessageKey::status(QMailMessage::Trash) & |
| 51 | excludeKey); |
| 52 | } else { |
| 53 | addFolder(draftsId, excludeKey, folderList); |
| 54 | } |
| 55 | |
| 56 | QMailFolderId sentFolderId = account.standardFolder(QMailFolder::SentFolder); |
| 57 | if (!sentFolderId.isValid()) { |
| 58 | insertFolder(QMailFolder::LocalStorageFolderId, Folder::SpecialUseSentFolder, |
| 59 | QMailMessageKey::status(QMailMessage::Sent) & |
| 60 | ~QMailMessageKey::status(QMailMessage::Trash) & |
| 61 | excludeKey); |
| 62 | } else { |
| 63 | addFolder(sentFolderId, excludeKey, folderList); |
| 64 | } |
| 65 | |
| 66 | // Take trash and childs |
| 67 | QMailFolderId trashFolderId = account.standardFolder(QMailFolder::TrashFolder); |
| 68 | if (!trashFolderId.isValid()) { |
| 69 | insertFolder(QMailFolder::LocalStorageFolderId, Folder::SpecialUseTrashFolder, |
| 70 | QMailMessageKey::status(QMailMessage::Trash) & |
| 71 | excludeKey); |
| 72 | } else { |
| 73 | addFolder(trashFolderId, excludeKey, folderList); |
| 74 | } |
| 75 | |
| 76 | insertFolder(QMailFolder::LocalStorageFolderId, Folder::SpecialUseOutboxFolder, |
| 77 | QMailMessageKey::status(QMailMessage::Outbox) & |
| 78 | ~QMailMessageKey::status(QMailMessage::Trash) & |
| 79 | excludeKey); |
| 80 | // Add the remaining folders, they are already ordered |
| 81 | foreach (const QMailFolderId& folderId, folderList) { |
| 82 | Folder::FolderType folderType = Folder::folderTypeFromId(folderId); |
| 83 | if (folderType != Folder::SpecialUseTrashFolder) { |
| 84 | excludeKey &= QMailMessageKey::status(QMailMessage::Trash, QMailDataComparator::Excludes); |
| 85 | } |
| 86 | insertFolder(folderId, folderType, excludeKey); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void FolderList::setAccountId(const quint64 &id) |
| 91 | { |
| 92 | m_accountId = QMailAccountId(id); |
| 93 | if (QMailAccountId(id).isValid()) { |
| 94 | m_accountId = QMailAccountId(id); |
| 95 | emit accountIdChanged(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void FolderList::addFolder(const QMailFolderId &folderId, QMailMessageKey key, QMailFolderIdList &folderList) |
| 100 | { |
| 101 | Folder::FolderType fType = Folder::folderTypeFromId(folderId); |
| 102 | auto isStandardFolder = [](const Folder::FolderType &folderType) { |
| 103 | return folderType == Folder::SpecialUseInboxFolder || folderType == Folder::SpecialUseDraftsFolder |
| 104 | || folderType == Folder::SpecialUseJunkFolder || folderType == Folder::SpecialUseTrashFolder |
| 105 | || folderType == Folder::SpecialUseOutboxFolder || folderType == Folder::SpecialUseSentFolder; |
| 106 | }; |
| 107 | |
| 108 | int i = folderList.indexOf(folderId); |
| 109 | if (i != -1) { |
| 110 | insertFolder(folderList[i], fType, key); |
| 111 | folderList.removeAt(i); |
| 112 | int n = i; |
| 113 | while (n < folderList.size() && QMailFolder(folderList[n]).parentFolderId() == folderId) { |
| 114 | if (isStandardFolder(fType)) { |
| 115 | n++; |
| 116 | } else { |
| 117 | Folder::FolderType folderType = Folder::folderTypeFromId(folderList[n]); |
| 118 | if (folderType != Folder::SpecialUseTrashFolder) { |
| 119 | key &= QMailMessageKey::status(QMailMessage::Trash, QMailDataComparator::Excludes); |
| 120 | } |
| 121 | insertFolder(folderList[n], folderType, key); |
| 122 | folderList.removeAt(n); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void FolderList::insertFolder(const QMailFolderId &id, const Folder::FolderType &type, const QMailMessageKey &key) |
| 129 | { |
| 130 | QMailMessageKey msgKey = QMailMessageKey::parentFolderId(id) & key; |
| 131 | Folder *f = new Folder(0, id, msgKey, type); |
| 132 | m_model->append(f); |
| 133 | } |
| 134 | |
| 135 | bool FolderList::sortIdList(const QMailFolderId &a, const QMailFolderId &b) |
| 136 | { |
| 137 | QMailFolder folderA(a), folderB(b); |
| 138 | if (folderA.parentFolderId() == folderB.parentFolderId()) { |
| 139 | // check they have different names |
| 140 | return folderA.displayName().compare(folderB.displayName(), Qt::CaseInsensitive) < 0; |
| 141 | } else if (folderA.parentAccountId() != folderB.parentAccountId()) { |
| 142 | return folderA.parentAccountId() < folderB.parentAccountId(); |
| 143 | } else { |
| 144 | QMailFolderIdList aParents; |
| 145 | QMailFolderId pId = a; |
| 146 | while (pId.isValid()) { |
| 147 | if (!(QMailFolder(pId).status() & QMailFolder::NonMail)) { |
| 148 | aParents.append(pId); |
| 149 | } |
| 150 | pId = QMailFolder(pId).parentFolderId(); |
| 151 | } |
| 152 | if (aParents.contains(b)) { |
| 153 | return false; |
| 154 | } |
| 155 | QMailFolderId cId; |
| 156 | QMailFolderId bLP; |
| 157 | pId = b; |
| 158 | while (pId.isValid()) { |
| 159 | if (aParents.contains(pId)) { |
| 160 | cId = pId; |
| 161 | break; |
| 162 | } |
| 163 | QMailFolder folderB(pId); |
| 164 | if (!(folderB.status() & QMailFolder::NonMail)) { |
| 165 | bLP = pId; |
| 166 | } |
| 167 | pId = folderB.parentFolderId(); |
| 168 | } |
| 169 | |
| 170 | if (cId.isValid()) { |
| 171 | int idIsParentOfA = aParents.indexOf(cId); |
| 172 | if (idIsParentOfA == 0) { |
| 173 | // a is ancestor of b |
| 174 | return true; |
| 175 | } |
| 176 | // Common ancestor found |
| 177 | return sortIdList(aParents[idIsParentOfA - 1], bLP); |
| 178 | } else { |
| 179 | QMailFolder topA(aParents.last()), topB(bLP); |
| 180 | // No common ancestor found |
| 181 | return topA.displayName().compare(topB.displayName(), Qt::CaseInsensitive) < 0; |
| 182 | } |
| 183 | } |
| 184 | } |