| 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 "MessageFilterCollection.h" |
| 19 | #include "qmailstore.h" |
| 20 | #include <QTimer> |
| 21 | #include <QDateTime> |
| 22 | |
| 23 | MessageFilterCollection::MessageFilterCollection(QObject *parent) : QObject(parent), m_children(0), |
| 24 | m_filter(None), m_firstRun(true) |
| 25 | { |
| 26 | m_children = new QQmlObjectListModel<MessageSet>(this); |
| 27 | connect(QMailStore::instance(), |
| 28 | SIGNAL(accountsAdded(QMailAccountIdList)), |
| 29 | this, |
| 30 | SLOT(reset())); |
| 31 | connect(QMailStore::instance(), |
| 32 | SIGNAL(accountsRemoved(QMailAccountIdList)), |
| 33 | this, |
| 34 | SLOT(reset())); |
| 35 | } |
| 36 | |
| 37 | void MessageFilterCollection::setFilter(MessageFilterCollection::Filter filter) |
| 38 | { |
| 39 | if (m_filter == filter) |
| 40 | return; |
| 41 | |
| 42 | m_filter = filter; |
| 43 | emit filterChanged(filter); |
| 44 | if (m_firstRun) { |
| 45 | QTimer::singleShot(350, this, SLOT(reset())); |
| 46 | m_firstRun = false; |
| 47 | } else { |
| 48 | reset(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void MessageFilterCollection::reset() |
| 53 | { |
| 54 | if (!m_children->isEmpty()) { |
| 55 | m_children->clear(); |
| 56 | } |
| 57 | switch(m_filter) { |
| 58 | case None: |
| 59 | return; |
| 60 | case StandardFolders: |
| 61 | { |
| 62 | createStandardFolderCollection(); |
| 63 | return; |
| 64 | } |
| 65 | case SmartFolders: |
| 66 | { |
| 67 | createSmartFolderCollection(); |
| 68 | return; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void MessageFilterCollection::createStandardFolderCollection() |
| 74 | { |
| 75 | // InboxSet |
| 76 | QMailMessageKey inboxKey; |
| 77 | StandardFolderSet *inbox = new StandardFolderSet(); // model will take ownership |
| 78 | inbox->setType(StandardFolderSet::SpecialUseInboxFolder); |
| 79 | inbox->init(tr("All inboxes"), inboxKey); |
| 80 | inbox->setSupportedActions(MessageSet::SyncAction | MessageSet::MarkAsRead | MessageSet::Expunge | MessageSet::SearchFolder | MessageSet::ShowProperties); |
| 81 | m_children->append(inbox); |
| 82 | |
| 83 | // Drafts |
| 84 | QMailMessageKey draftsKey; |
| 85 | draftsKey &= QMailMessageKey(QMailMessageKey::status(QMailMessage::Draft, QMailDataComparator::Includes)); |
| 86 | draftsKey &= QMailMessageKey(QMailMessageKey::status(QMailMessage::Trash | QMailMessage::Outbox, QMailDataComparator::Excludes)); |
| 87 | StandardFolderSet *drafts = new StandardFolderSet(); |
| 88 | drafts->setType(StandardFolderSet::SpecialUseDraftsFolder); |
| 89 | drafts->initNoDecendents(tr("Drafts"), draftsKey); |
| 90 | drafts->setSupportedActions(MessageSet::ShowProperties | MessageSet::SearchFolder); |
| 91 | m_children->append(drafts); |
| 92 | |
| 93 | // Junk |
| 94 | QMailMessageKey junkKey; |
| 95 | junkKey &= QMailMessageKey(QMailMessageKey::status(QMailMessage::Junk, QMailDataComparator::Includes)); |
| 96 | junkKey &= QMailMessageKey(QMailMessageKey::status(QMailMessage::Trash, QMailDataComparator::Excludes)); |
| 97 | StandardFolderSet *junk = new StandardFolderSet(); |
| 98 | junk->setType(StandardFolderSet::SpecialUseJunkFolder); |
| 99 | junk->initNoDecendents(tr("Spam"), junkKey); |
| 100 | junk->setSupportedActions(MessageSet::SyncAction | MessageSet::MarkAsRead | MessageSet::SearchFolder | MessageSet::ShowProperties); |
| 101 | m_children->append(junk); |
| 102 | |
| 103 | // Outbox |
| 104 | QMailMessageKey outboxKey; |
| 105 | outboxKey &= QMailMessageKey(QMailMessageKey::status(QMailMessage::Outbox, QMailDataComparator::Includes)); |
| 106 | outboxKey &= QMailMessageKey(QMailMessageKey::status(QMailMessage::Trash, QMailDataComparator::Excludes)); |
| 107 | StandardFolderSet *outbox = new StandardFolderSet(); |
| 108 | outbox->setType(StandardFolderSet::SpecialUseOutboxFolder); |
| 109 | outbox->initNoDecendents(tr("Outbox"), outboxKey); |
| 110 | outbox->setSupportedActions(MessageSet::MarkAsRead | MessageSet::ExportPending | MessageSet::SearchFolder | MessageSet::ShowProperties); |
| 111 | m_children->append(outbox); |
| 112 | |
| 113 | // Sent |
| 114 | QMailMessageKey sentKey; |
| 115 | sentKey &= QMailMessageKey(QMailMessageKey::status(QMailMessage::Sent, QMailDataComparator::Includes)); |
| 116 | sentKey &= QMailMessageKey(QMailMessageKey::status(QMailMessage::Trash, QMailDataComparator::Excludes)); |
| 117 | StandardFolderSet *sent = new StandardFolderSet(); |
| 118 | sent->setType(StandardFolderSet::SpecialUseSentFolder); |
| 119 | sent->initNoDecendents(tr("Sent"), sentKey); |
| 120 | sent->setSupportedActions(MessageSet::SyncAction | MessageSet::MarkAsRead | MessageSet::SearchFolder | MessageSet::ShowProperties); |
| 121 | m_children->append(sent); |
| 122 | |
| 123 | // Trash |
| 124 | QMailMessageKey trashKey; |
| 125 | trashKey &= QMailMessageKey(QMailMessageKey::status(QMailMessage::Trash, QMailDataComparator::Includes)); |
| 126 | StandardFolderSet *trash = new StandardFolderSet(); |
| 127 | trash->setType(StandardFolderSet::SpecialUseTrashFolder); |
| 128 | trash->initNoDecendents(tr("Trash"), trashKey); |
| 129 | trash->setSupportedActions(MessageSet::SyncAction | MessageSet::MarkAsRead | MessageSet::SearchFolder | MessageSet::ShowProperties | MessageSet::EmptyTrash); |
| 130 | m_children->append(trash); |
| 131 | } |
| 132 | |
| 133 | void MessageFilterCollection::createSmartFolderCollection() |
| 134 | { |
| 135 | QMailMessageKey todayKey; |
| 136 | todayKey &= QMailMessageKey(QMailMessageKey::receptionTimeStamp(QDateTime(QDate::currentDate()), QMailDataComparator::GreaterThanEqual)); |
| 137 | todayKey &= QMailMessageKey(QMailMessageKey::status(QMailMessage::Removed, QMailDataComparator::Excludes)); |
| 138 | SmartFolderSet *todaySet = new SmartFolderSet(); |
| 139 | todaySet->setType(SmartFolderSet::SmartTodayFolder); |
| 140 | todaySet->init(tr("Today, %1").arg(QDateTime::currentDateTime().toString("ddd d")), todayKey); |
| 141 | todaySet->setSupportedActions(MessageSet::MarkAsRead | MessageSet::SearchFolder | MessageSet::ShowProperties); |
| 142 | m_children->append(todaySet); |
| 143 | |
| 144 | QMailMessageKey todoKey; |
| 145 | todoKey &= QMailMessageKey(QMailMessageKey::status(QMailMessage::Todo, QMailDataComparator::Includes)); |
| 146 | todoKey &= QMailMessageKey(QMailMessageKey::status(QMailMessage::Removed, QMailDataComparator::Excludes)); |
| 147 | SmartFolderSet *todoSet = new SmartFolderSet(); |
| 148 | todoSet->setType(SmartFolderSet::SmartTodoFolder); |
| 149 | todoSet->init(tr("To-do"), todoKey); |
| 150 | todoSet->setSupportedActions(MessageSet::SearchFolder | MessageSet::ShowProperties | MessageSet::MarkAsDone); |
| 151 | m_children->append(todoSet); |
| 152 | |
| 153 | QMailMessageKey doneKey; |
| 154 | doneKey &= QMailMessageKey(QMailMessageKey::customField(QStringLiteral("task-done"), QStringLiteral("1"), QMailDataComparator::Includes)); |
| 155 | doneKey &= QMailMessageKey(QMailMessageKey::status(QMailMessage::Removed, QMailDataComparator::Excludes)); |
| 156 | SmartFolderSet *doneSet = new SmartFolderSet(); |
| 157 | doneSet->setType(SmartFolderSet::SmartDoneFolder); |
| 158 | doneSet->init(tr("Done"), doneKey); |
| 159 | doneSet->setSupportedActions(MessageSet::SearchFolder | MessageSet::ShowProperties); |
| 160 | m_children->append(doneSet); |
| 161 | } |
| 162 |