| 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 "Accounts.h" |
| 19 | #include <QDebug> |
| 20 | #include "qmailstore.h" |
| 21 | |
| 22 | Q_LOGGING_CATEGORY(D_ACCOUNTS_LIST, "dekko.accounts.list") |
| 23 | |
| 24 | Accounts::Accounts(QObject *parent) : QObject(parent), |
| 25 | m_filter(Enabled), m_model(0) |
| 26 | { |
| 27 | m_model = new QQmlObjectListModel<Account>(this); |
| 28 | emit modelChanged(); |
| 29 | connect(QMailStore::instance(), |
| 30 | SIGNAL(accountsAdded(QMailAccountIdList)), |
| 31 | this, |
| 32 | SLOT(accountsAdded(QMailAccountIdList))); |
| 33 | connect(QMailStore::instance(), |
| 34 | SIGNAL(accountsRemoved(QMailAccountIdList)), |
| 35 | this, |
| 36 | SLOT(accountsRemoved(QMailAccountIdList))); |
| 37 | connect(QMailStore::instance(), |
| 38 | SIGNAL(accountsUpdated(QMailAccountIdList)), |
| 39 | this, |
| 40 | SLOT(accountsUpdated(QMailAccountIdList))); |
| 41 | reset(); |
| 42 | } |
| 43 | |
| 44 | quint64 Accounts::maskForFilter(Accounts::Filters filter) |
| 45 | { |
| 46 | switch(filter) { |
| 47 | case Enabled: |
| 48 | return QMailAccount::Enabled; |
| 49 | case CanReceive: |
| 50 | return QMailAccount::MessageSource; |
| 51 | case CanSend: |
| 52 | return QMailAccount::MessageSink; |
| 53 | case Synchronized: |
| 54 | return QMailAccount::Synchronized; |
| 55 | case HasPersitentConnection: |
| 56 | return QMailAccount::HasPersistentConnection; |
| 57 | } |
| 58 | Q_UNREACHABLE(); |
| 59 | } |
| 60 | |
| 61 | Accounts::Filters Accounts::filter() const |
| 62 | { |
| 63 | return m_filter; |
| 64 | } |
| 65 | |
| 66 | bool Accounts::deleteAccount(const quint64 &id) |
| 67 | { |
| 68 | return QMailStore::instance()->removeAccount(QMailAccountId(id)); |
| 69 | } |
| 70 | |
| 71 | bool Accounts::hasAccount(const quint64 &id) |
| 72 | { |
| 73 | foreach(const Account *a, m_model->toList()) { |
| 74 | if (a->accountId().toULongLong() == id) { |
| 75 | return true; |
| 76 | } |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | Account *Accounts::get(const quint64 &id) |
| 82 | { |
| 83 | foreach(Account *a, m_model->toList()) { |
| 84 | if (a->accountId().toULongLong() == id) { |
| 85 | return a; |
| 86 | } |
| 87 | } |
| 88 | return new Account(); |
| 89 | } |
| 90 | |
| 91 | void Accounts::setFilter(Accounts::Filters filter) |
| 92 | { |
| 93 | if (m_filter == filter) |
| 94 | return; |
| 95 | |
| 96 | m_filter = filter; |
| 97 | reset(); |
| 98 | emit filterChanged(filter); |
| 99 | } |
| 100 | |
| 101 | void Accounts::accountsAdded(const QMailAccountIdList &ids) |
| 102 | { |
| 103 | qCDebug(D_ACCOUNTS_LIST) << "Adding accounts " << ids << "to list"; |
| 104 | QMailAccountKey passKey; |
| 105 | if (filter() != Enabled) |
| 106 | // We need to ensure we only show enabled accounts |
| 107 | // This means we can do filters like CanReceive | Enabled |
| 108 | passKey = QMailAccountKey::status(maskForFilter(filter()) | QMailAccount::Enabled) & QMailAccountKey::id(ids); |
| 109 | else |
| 110 | passKey = QMailAccountKey::status(maskForFilter(filter())) & QMailAccountKey::id(ids); |
| 111 | |
| 112 | QMailAccountIdList results = QMailStore::instance()->queryAccounts(passKey); |
| 113 | if(results.isEmpty()) |
| 114 | return; |
| 115 | |
| 116 | // TODO: once sortkey is implemented update the model with the sorted result |
| 117 | // for now we just append the new account |
| 118 | Q_FOREACH(const QMailAccountId &id, ids) { |
| 119 | if (!m_idList.contains(id)) { |
| 120 | Account *account = new Account(); |
| 121 | account->setId(id.toULongLong()); |
| 122 | m_model->append(account); |
| 123 | m_idList.append(id); |
| 124 | } else { |
| 125 | qCDebug(D_ACCOUNTS_LIST) << __func__ << "Account with same id already in model"; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void Accounts::accountsUpdated(const QMailAccountIdList &ids) |
| 131 | { |
| 132 | QMailAccountKey idKey(QMailAccountKey::id(ids)); |
| 133 | QMailAccountIdList validIds = QMailStore::instance()->queryAccounts(idKey & QMailAccountKey::status(maskForFilter(filter()))); |
| 134 | // We maybe here because a an account has been enabled/disabled or some other filter we use |
| 135 | // in this model and we now need to remove or add an account to the model |
| 136 | |
| 137 | // remove any that shouldn't be in the model |
| 138 | Q_FOREACH(const QMailAccountId &id, ids) { |
| 139 | |
| 140 | if (!validIds.contains(id)) { |
| 141 | int index = m_idList.indexOf(id); |
| 142 | if (index == -1) { |
| 143 | continue; |
| 144 | } |
| 145 | m_model->remove(index); |
| 146 | m_idList.removeAt(index); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // go over the valid ids and either insert any missing or update any existing |
| 151 | Q_FOREACH(const QMailAccountId &id, validIds) { |
| 152 | int index = m_idList.indexOf(id); |
| 153 | if (index == -1) { |
| 154 | // Not here so create. |
| 155 | Account *account = new Account(); |
| 156 | account->setId(id.toULongLong()); |
| 157 | m_model->append(account); |
| 158 | m_idList.append(id); |
| 159 | } else { |
| 160 | // just notify somethings changed |
| 161 | m_model->at(index)->emitAccountChanged(); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | void Accounts::accountsRemoved(const QMailAccountIdList &ids) |
| 167 | { |
| 168 | qCDebug(D_ACCOUNTS_LIST) << "Removing accounts" << ids << "from list"; |
| 169 | Q_FOREACH(const QMailAccountId &id, ids) { |
| 170 | int index = m_idList.indexOf(id); |
| 171 | if (index == -1) { |
| 172 | // This id#s not in this model |
| 173 | continue; |
| 174 | } |
| 175 | // Remove both from model & idlist; |
| 176 | m_model->remove(index); |
| 177 | m_idList.removeAt(index); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void Accounts::reset() |
| 182 | { |
| 183 | qCDebug(D_ACCOUNTS_LIST) << "Resetting accounts list"; |
| 184 | m_model->clear(); |
| 185 | m_idList.clear(); |
| 186 | init(); |
| 187 | } |
| 188 | |
| 189 | void Accounts::init() |
| 190 | { |
| 191 | qCDebug(D_ACCOUNTS_LIST) << "Initialising accounts list"; |
| 192 | auto idList = QMailStore::instance()->queryAccounts(QMailAccountKey::status(maskForFilter(filter())) ,QMailAccountSortKey::name()); |
| 193 | Q_FOREACH(const QMailAccountId &id, idList) { |
| 194 | int index = m_idList.indexOf(id); |
| 195 | if (index == -1) { |
| 196 | // Not here so create. |
| 197 | Account *account = new Account(this); |
| 198 | account->setId(id.toULongLong()); |
| 199 | m_model->append(account); |
| 200 | m_idList.append(id); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 |