Debian Codegraph @

Dekko/backend/mail/service/ClientService.h

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 #ifndef CLIENTSERVICE_H
19 #define CLIENTSERVICE_H
20
21 #include <QObject>
22 #include <QTimer>
23 #include <QPointer>
24 #include <QmlObjectListModel.h>
25 #include <PriorityQueue.h>
26 #include <qmailstore.h>
27 #include <qmailserviceaction.h>
28 #include <qmailaccount.h>
29 #include "ClientServiceAction.h"
30 #include "Folder.h"
31
32 class ClientServiceWatcher;
33 /** @short ClientService is responsible for all message related actions
34 *
35 * Created actions are placed in priority based queues and their lifetime is managed by this component.
36 *
37 * There shouldn't be any need to directly use this class but rather the exposed methods of Client singleton.
38 *
39 * The queues are available as listmodels to provide easy qml debug widgets when trying to diagnose queue based issues
40 * since the listmodels are qlist based and pretty fast there's no real impact to using them
41 */
42 class ClientService : public QObject
43 {
44 Q_OBJECT
45 /** @short list model of recent actions that are undoable */
46 Q_PROPERTY(QObject *undoActionQueue READ undoQueue NOTIFY queueChanged)
47 Q_PROPERTY(bool hasUndoableAction READ hasUndoableAction NOTIFY undoCountChanged)
48 Q_PROPERTY(QString undoableActionDescription READ undoDescription NOTIFY undoCountChanged)
49
50 /** @short model of queued service actions */
51 Q_PROPERTY(QObject *serviceActionQueue READ serviceQueue NOTIFY queueChanged)
52
53 public:
54 explicit ClientService(QObject *parent = 0);
55 QObject *undoQueue() { return m_undoQueue; }
56 QObject *serviceQueue() { return m_serviceQueue; }
57 bool hasUndoableAction();
58 QString undoDescription();
59 void deleteMessages(const QMailMessageIdList &ids);
60 void restoreMessage(const QMailMessageId &id);
61 void markMessagesImportant(const QMailMessageIdList &msgIds, const bool important);
62 void markMessagesRead(const QMailMessageIdList &msgIds, const bool read);
63 void markMessagesTodo(const QMailMessageIdList &msgIds, const bool todo);
64 void markMessagesDone(const QMailMessageIdList &msgIds, const bool done);
65 void markMessagesReplied(const QMailMessageIdList &idList, const bool all);
66 void markMessageForwarded(const QMailMessageIdList &idList);
67 void markFolderRead(const QMailFolderId &id);
68 void downloadMessagePart(const QMailMessageId &id, const QString &location);
69 void downloadMessages(const QMailMessageIdList &msgIds);
70 void sendMessage(const QMailMessage &msg);
71 void createStandardFolders(const QMailAccountId &id);
72 void moveToStandardFolder(const QMailMessageIdList &msgIds, const Folder::FolderType &folder, const bool userTriggered);
73 void moveToFolder(const QMailMessageIdList &msgIds, const QMailFolderId &folder);
74 void synchronizeAccount(const QMailAccountId &id);
75 void emptyTrash(const QMailAccountIdList &ids);
76 void syncFolders(const QMailAccountId &accountId, const QMailFolderIdList &folders);
77 void removeMessage(const QMailMessageId &id, const QMailStore::MessageRemovalOption &option);
78
79 int countMessages(const QMailMessageKey &key);
80
81 signals:
82 void undoCountChanged();
83 void updatesRolledBack();
84 void queueChanged();
85 void messagePartFetched(const quint64 &message, const QString &location);
86 void messagePartFetchFailed(const quint64 &message, const QString &location);
87 void messagesFetched(const QMailMessageIdList &ids);
88 void messageFetchFailed(const QMailMessageIdList &ids);
89 void messagesSent(const QMailMessageIdList &ids);
90 void messageSendingFailed(const QMailMessageIdList &ids, QMailServiceAction::Status::ErrorCode error);
91 void accountSynced(const quint64 &id);
92 void syncAccountFailed(const quint64 &id);
93 void actionFailed(const quint64 &id, const QMailServiceAction::Status &status);
94 void standardFoldersCreated(const quint64 &id, const bool created);
95 void foldersSynced(const quint64 &accountId, const QMailFolderIdList &folders);
96 void foldersSyncFailed(const quint64 &accountId, const QMailFolderIdList &folders);
97
98 public slots:
99 void undoActions();
100 void sendAnyQueuedMail();
101
102 private slots:
103 void processNextAction();
104 /** @short Process the next action in the action queue */
105 void processNextServiceAction();
106 /** @short Get's called on ClientServiceAction::complete() */
107 void exportMailStoreUpdate();
108 void exportMailStoreUpdate(const QMailAccountIdList &ids);
109 void exportMailStoreUpdate(const QMailAccountId &id);
110
111 void undoableCountChanged();
112 void rollBackMailStoreUpdates(const QMailAccountIdList &accounts);
113 void markSentRead(const QMailMessageIdList &ids);
114
115 private:
116 void connectServiceAction(QMailServiceAction* action);
117 void enqueue(ClientServiceAction *action);
118 bool exportQueuedForAccountId(const QMailAccountId &id);
119
120 private:
121 QQmlObjectListModel<ClientServiceAction> *m_undoQueue;
122 QQmlObjectListModel<ClientServiceAction> *m_serviceQueue;
123 QPointer<ClientServiceWatcher> m_serviceWatcher;
124 QQmlObjectListModel<ClientServiceAction> *m_storageQueue;
125 QQmlObjectListModel<ClientServiceAction> *m_transmitQueue;
126 QTimer *m_undoTimer;
127
128 QMailRetrievalAction *m_exportAction;
129 };
130
131 /** @short ClientServiceWatcher watches the activity of a queues running action
132 *
133 * It will dequeue on a action finishing and call for the next action to be processed
134 */
135 class ClientServiceWatcher : public QObject
136 {
137 Q_OBJECT
138 public:
139 explicit ClientServiceWatcher(QObject *parent = 0) : QObject(parent) {}
140
141 void setWatchQueue(QQmlObjectListModel<ClientServiceAction> *queue) { m_queue = queue; }
142
143 signals:
144 void processNext();
145 void messagePartFetched(const quint64 &message, const QString &location);
146 void messagePartFetchFailed(const quint64 &message, const QString &location);
147 void messagesFetched(const QMailMessageIdList &ids);
148 void messageFetchFailed(const QMailMessageIdList &ids);
149 void checkSendMailQueue();
150 void messagesSent();
151 void messageSendingFailed();
152 void accountSynced(const quint64 &id);
153 void syncAccountFailed(const quint64 &id);
154 void standardFoldersCreated(const quint64 &id, const bool created);
155 void foldersSynced(const quint64 &accountId, const QMailFolderIdList &folders);
156 void foldersSyncFailed(const quint64 &accountId, const QMailFolderIdList &folders);
157 void actionFailed(const quint64 &id, const QMailServiceAction::Status &status);
158
159 public slots:
160 void activityChanged(QMailServiceAction::Activity activity){
161 if (QMailServiceAction *action = m_queue->first()->action()) {
162 if (activity == QMailServiceAction::Successful) {
163 qDebug() << "Service action successful";
164 if (action->metaObject()->className() == QStringLiteral("QMailRetrievalAction")) {
165 if (m_queue->first()->serviceActionType() == ClientServiceAction::ExportAction) {
166 // qDebug() << "Export action complete";
167 } else if (m_queue->first()->serviceActionType() == ClientServiceAction::RetrievePartAction) {
168 // qDebug() << "FetchPart successful";
169 ClientServiceAction *clientAction = m_queue->at(0);
170 emit messagePartFetched(clientAction->messageId(), clientAction->location());
171 } else if (m_queue->first()->serviceActionType() == ClientServiceAction::RetrieveAction) {
172 // qDebug() << "Fetch messages successful";
173 ClientServiceAction *clientAction = m_queue->at(0);
174 emit messagesFetched(clientAction->messageIds());
175 } else if (m_queue->first()->serviceActionType() == ClientServiceAction::SyncAccountAction) {
176 // qDebug() << "Sync account successful";
177 ClientServiceAction *clientAction = m_queue->at(0);
178 emit accountSynced(clientAction->accountId().toULongLong());
179 } else if (m_queue->first()->serviceActionType() == ClientServiceAction::SyncFolderAction) {
180 // qDebug() << "Sync folder successful";
181 ClientServiceAction *clientAction = m_queue->at(0);
182 emit foldersSynced(clientAction->accountId().toULongLong(), clientAction->folderIds());
183
184 } else if (m_queue->first()->serviceActionType() == ClientServiceAction::CreateStandardFolders) {
185 ClientServiceAction *clientAction = m_queue->at(0);
186 emit standardFoldersCreated(clientAction->accountId().toULongLong(), true);
187 }
188 m_queue->dequeue();
189 emit processNext();
190 } else if (action->metaObject()->className() == QStringLiteral("QMailStorageAction")) {
191 if (m_queue->first()->serviceActionType() == ClientServiceAction::OutboxAction) {
192 qDebug() << "Message stored in outbox";
193 emit checkSendMailQueue();
194 }
195 m_queue->dequeue();
196 emit processNext();
197
198 } else if (action->metaObject()->className() == QStringLiteral("QMailTransmitAction")) {
199 if (m_queue->first()->serviceActionType() == ClientServiceAction::SendAction) {
200 qDebug() << "Success sending pending messages";
201 emit messagesSent();
202 }
203 m_queue->dequeue();
204 emit processNext();
205 }
206
207 } else if (activity == QMailServiceAction::Failed) {
208 const QMailServiceAction::Status status(action->status());
209 if (action->metaObject()->className() == QStringLiteral("QMailRetrievalAction")) {
210 if (m_queue->first()->serviceActionType() == ClientServiceAction::ExportAction) {
211 qDebug() << "Export action failed " << status.accountId << "Reason: " << status.text;
212 // because this is a silent action. i.e the changes can be exported in a future
213 // export it's not critical to interrupt the UI right now. Let's just dequeue the job
214 } else if (m_queue->first()->serviceActionType() == ClientServiceAction::RetrievePartAction) {
215 ClientServiceAction *fetch = m_queue->at(0);
216 emit messagePartFetchFailed(fetch->messageId(), fetch->location());
217 } else if (m_queue->first()->serviceActionType() == ClientServiceAction::RetrieveAction) {
218 ClientServiceAction *clientAction = m_queue->at(0);
219 emit messageFetchFailed(clientAction->messageIds());
220 } else if (m_queue->first()->serviceActionType() == ClientServiceAction::SyncAccountAction) {
221 // qDebug() << "Sync account failed";
222 ClientServiceAction *clientAction = m_queue->at(0);
223 emit syncAccountFailed(clientAction->accountId().toULongLong());
224 emit actionFailed(clientAction->accountId().toULongLong(), action->status());
225 } else if (m_queue->first()->serviceActionType() == ClientServiceAction::SyncFolderAction) {
226 // qDebug() << "Sync folder failed";
227 ClientServiceAction *clientAction = m_queue->at(0);
228 emit foldersSyncFailed(clientAction->accountId().toULongLong(), clientAction->folderIds());
229
230
231 } else if (m_queue->first()->serviceActionType() == ClientServiceAction::CreateStandardFolders) {
232 ClientServiceAction *clientAction = m_queue->at(0);
233 emit standardFoldersCreated(clientAction->accountId().toULongLong(), false);
234 }
235 m_queue->dequeue();
236 emit processNext();
237 } else if (action->metaObject()->className() == QStringLiteral("QMailStorageAction")) {
238 if (m_queue->first()->serviceActionType() == ClientServiceAction::OutboxAction) {
239 qDebug() << "Failed while storing message in outbox: " << action->status().text;
240 emit checkSendMailQueue();
241 }
242 m_queue->dequeue();
243 emit processNext();
244 } else if (action->metaObject()->className() == QStringLiteral("QMailTransmitAction")) {
245 if (m_queue->first()->serviceActionType() == ClientServiceAction::SendAction) {
246 qDebug() << "Failed sending pending messages: " << action->status().text;
247 emit messageSendingFailed();
248 }
249 m_queue->dequeue();
250 emit processNext();
251 }
252 }
253 } else {
254 qDebug() << "Not sure what this is here :-(";
255 }
256 }
257
258 private:
259 QPointer<QQmlObjectListModel<ClientServiceAction> > m_queue;
260
261 };
262
263 #endif // CLIENTSERVICE_H