Debian Codegraph @

Dekko/backend/mail/SearchService.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 "SearchService.h"
19 #include <QDebug>
20 #include <QTimer>
21
22 SearchService::SearchService(QObject *parent) : QObject(parent),
23 m_searchAction(new QMailSearchAction(this)), m_userTerminated(false)
24 {
25 connect(m_searchAction.data(), &QMailSearchAction::messageIdsMatched, this, &SearchService::messageIdsMatched);
26 connect(m_searchAction.data(), &QMailSearchAction::activityChanged, this, &SearchService::searchActivityChanged);
27 }
28
29 void SearchService::search(QueryConfiguration &config)
30 {
31 if (config.isValid()) {
32 m_queue.enqueue(SearchQuery(m_searchAction, config));
33 processNewQuery();
34 }
35 }
36
37 void SearchService::cancel()
38 {
39 if (m_searchAction->isRunning()) {
40 m_searchAction->cancelOperation();
41 }
42 m_queue.clear();
43 m_userTerminated = true;
44 emit statusChanged(Canceled, m_searchAction->status().text);
45 }
46
47 void SearchService::processNewQuery()
48 {
49 if (m_searchAction->isRunning()) {
50 // already one in queue so cancel & execute the next
51 m_searchAction->cancelOperation();
52 QTimer::singleShot(0, this, SLOT(executeNextQuery()));
53 } else {
54 if (m_queue.count() > 1) {
55 QTimer::singleShot(0, this, SLOT(executeNextQuery()));
56 } else {
57 executeQuery();
58 }
59 }
60 }
61
62 void SearchService::executeNextQuery()
63 {
64 if (m_queue.isEmpty()) {
65 return;
66 }
67 // we don't want the current head as that has just been cancelled
68 m_queue.dequeue();
69 executeQuery();
70 }
71
72 void SearchService::executeQuery()
73 {
74 if (m_queue.isEmpty()) {
75 return;
76 }
77 m_queue.head().execute();
78 }
79
80 void SearchService::searchActivityChanged(QMailServiceAction::Activity activity)
81 {
82 switch (activity) {
83 case QMailServiceAction::Failed:
84 {
85 qDebug() << "Search failed - ErrorCode[" << m_searchAction->status().errorCode << "]" << m_searchAction->status().text;
86 if (!m_userTerminated) {
87 emit statusChanged(Failed, m_searchAction->status().text);
88 }
89 reset();
90 break;
91 }
92 case QMailServiceAction::Successful:
93 {
94 qDebug() << "Search Successful";
95 emit statusChanged(Done);
96 reset();
97 break;
98 }
99 case QMailServiceAction::InProgress:
100 {
101 qDebug() << "Search In progress -" << m_searchAction->status().text;
102 emit statusChanged(InProgress);
103 break;
104 }
105 case QMailServiceAction::Pending:
106 {
107 qDebug() << "Search Pending -" << m_searchAction->status().text;
108 emit statusChanged(InProgress);
109 break;
110 }
111 }
112 }
113
114 void SearchService::reset()
115 {
116 m_queue.clear();
117 m_userTerminated = false;
118 }
119
120
121 void SearchQuery::execute()
122 {
123 action->searchMessages(qConfig.key, qConfig.qeury, qConfig.spec, qConfig.sortKey);
124 }