Debian Codegraph @

Dekko/backend/mail/SenderIdentities.cpp

raw symbols SCIP
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 "SenderIdentities.h"
19 #include <qmailaccount.h>
20
21 SenderIdentities::SenderIdentities(QObject *parent) : QObject(parent), m_selectedIndex(-1),
22 m_accountsModel(Q_NULLPTR), m_accountIdentities(Q_NULLPTR), m_model(Q_NULLPTR)
23 {
24 m_model = new QQmlObjectListModel<IdentityWrapper>(this);
25 }
26
27 QObject *SenderIdentities::model() const { return m_model; }
28
29 int SenderIdentities::selectedIndex() const
30 {
31 return m_selectedIndex;
32 }
33
34 QObject *SenderIdentities::selectedAccount() const
35 {
36 if (m_selectedIndex < 0 || m_selectedIndex > m_model->count()) {
37 return new QObject();
38 }
39 return m_model->at(m_selectedIndex);
40 }
41
42 QObject *SenderIdentities::accountsModel() const
43 {
44 return m_accountsModel;
45 }
46
47 bool SenderIdentities::isEmpty()
48 {
49 return m_model->isEmpty();
50 }
51
52 bool SenderIdentities::hasAddress(const QString &address)
53 {
54 foreach(IdentityWrapper *w, m_model->toList()) {
55 if (w->get_identity()->get_email() == address) {
56 return true;
57 }
58 }
59 return false;
60 }
61
62 Identities *SenderIdentities::identities() const
63 {
64 return m_accountIdentities;
65 }
66
67 int SenderIdentities::determinePreferredIdentity(const QMailMessage &message)
68 {
69 qDebug() << __func__;
70 int preferred = -1;
71 if (m_model->isEmpty()) {
72 return preferred;
73 }
74 // find the account were after
75 foreach(Account *account, m_accountsModel->toList()) {
76 if (account->accountId() != message.parentAccountId()) {
77 continue;
78 }
79 // first see if we can match an identity to the message recipients
80 const int pi = m_accountIdentities->determineBestIdentityFromMessage(message);
81 if (pi > 0) {
82 preferred = findIndexOf(pi);
83 break;
84 }
85
86 if (preferred == -1) {
87 // else see if an identity for this account has the default flag
88 QVariantMap d = m_accountIdentities->get(m_accountIdentities->defaultIdentity());
89 if (d["parentId"].toUInt() == message.parentAccountId().toULongLong()) {
90 preferred = findIndexOf(d["id"].toInt());
91 break;
92 }
93 }
94
95 if (preferred == -1) {
96 auto ids = m_accountIdentities->getAllForAccount(message.parentAccountId().toULongLong());
97 int id = ids.first().value("id").toInt();
98 preferred = findIndexOf(id);
99 }
100 }
101 return preferred;
102 }
103
104 void SenderIdentities::setSelectedIndex(int selectedIndex)
105 {
106 if (m_selectedIndex == selectedIndex)
107 return;
108
109 m_selectedIndex = selectedIndex;
110 qDebug() << "SelectedIndex: " << m_selectedIndex;
111 emit selectedIndexChanged();
112 }
113
114 void SenderIdentities::setAccountsModel(QObject *accountsModel)
115 {
116 if (accountsModel == Q_NULLPTR) {
117 return;
118 }
119 if (m_accountsModel) {
120 disconnect(m_accountsModel, 0, this, 0);
121 }
122 QQmlObjectListModel<Account> *model = static_cast<QQmlObjectListModel<Account> *>(accountsModel);
123 if (m_accountsModel == model)
124 return;
125
126 m_accountsModel = model;
127 connect(m_accountsModel, &QQmlObjectListModel<Account>::countChanged, this, &SenderIdentities::accountsChanged);
128 emit modelsChanged();
129 reset();
130 }
131
132 void SenderIdentities::setSelectedIndexFromAccountId(quint64 accountId)
133 {
134 qDebug() << __func__;
135 if (m_model->isEmpty()) {
136 setSelectedIndex(-1);
137 return;
138 }
139 // find the account were after
140 foreach(Account *account, m_accountsModel->toList()) {
141 if (account->accountId() == QMailAccountId(accountId)) {
142 setSelectedIndex(m_accountsModel->indexOf(account));
143 return;
144 }
145 }
146 }
147
148 void SenderIdentities::reset()
149 {
150 m_model->clear();
151 if (!m_accountIdentities || !m_accountsModel) {
152 return;
153 }
154 foreach(Account *account, m_accountsModel->toList()) {
155 IdentityList il = m_accountIdentities->getAllForAccount(account->accountId().toULongLong());
156 if (il.isEmpty()) {
157 // Hmm there are no identities but the accounts is still enabled.
158 // we need to fall back to the outgoing data in the mailstore
159 m_model->append(createIdentityFromAccount(account));
160 } else {
161 for (const QVariantMap &i : il) {
162 m_model->append(createIdentity(account, i));
163 }
164 }
165 }
166 //TODO set default index/identity
167 setSelectedIndex(findIndexOf(m_accountIdentities->defaultIdentity()));
168 }
169
170 void SenderIdentities::setIdentities(Identities *accountIdentities)
171 {
172 if (m_accountIdentities == accountIdentities)
173 return;
174
175 m_accountIdentities = accountIdentities;
176 connect(m_accountIdentities, &Identities::identitiesChanged, this , &SenderIdentities::reset);
177 emit modelsChanged();
178 reset();
179 }
180
181 void SenderIdentities::accountsChanged()
182 {
183 reset();
184 }
185
186 int SenderIdentities::findIndexOf(const quint64 &id)
187 {
188 foreach(IdentityWrapper *identity, m_model->toList()) {
189 if (identity->get_identity()->get_accountId() == id) {
190 return m_model->indexOf(identity);
191 }
192 }
193 return -1;
194 }
195
196 IdentityWrapper* SenderIdentities::createIdentityFromAccount(Account *account)
197 {
198 IdentityWrapper *wrapper = new IdentityWrapper(Q_NULLPTR);
199 wrapper->set_account(account);
200 Identity *identity = new Identity(wrapper);
201 identity->set_accountId(100 + m_model->count());
202 identity->set_parentId(account->accountId().toULongLong());
203 identity->set_name(account->qMailAccount()->name());
204 identity->set_email(account->qMailAccount()->fromAddress().toString());
205 identity->set_signature(account->qMailAccount()->signature());
206 wrapper->set_identity(identity);
207 return wrapper;
208 }
209
210 IdentityWrapper* SenderIdentities::createIdentity(Account *account, const QVariantMap &identity)
211 {
212 IdentityWrapper *wrapper = new IdentityWrapper(Q_NULLPTR);
213 wrapper->set_account(account);
214 wrapper->set_identity(Identity::fromMap(wrapper, identity));
215 return wrapper;
216 }
217
218