clazy: fix QString(QLatin1String(...))
[trojita.git] / src / Imap / Tasks / ImapTask.cpp
blob15f5ea319f97402617ae1e61772797d65fffedb7
1 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
3 This file is part of the Trojita Qt IMAP e-mail client,
4 http://trojita.flaska.net/
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of
9 the License or (at your option) version 3 or any later version
10 accepted by the membership of KDE e.V. (or its successor approved
11 by the membership of KDE e.V.), which shall act as a proxy
12 defined in Section 14 of version 3 of the license.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "ImapTask.h"
24 #include "Common/InvokeMethod.h"
25 #include "Imap/Model/Model.h"
26 #include "Imap/Model/TaskPresentationModel.h"
27 #include "KeepMailboxOpenTask.h"
29 namespace Imap
31 namespace Mailbox
34 ImapTask::ImapTask(Model *model) :
35 QObject(model), parser(0), parentTask(0), model(model), _finished(false), _dead(false), _aborted(false)
37 connect(this, &QObject::destroyed, model, &Model::slotTaskDying);
38 CHECK_TASK_TREE;
41 ImapTask::~ImapTask()
45 /** @short Schedule another task to get a go when this one completes
47 This function informs the current task (this) that when it terminates successfully, the dependant task (@arg task) shall be started.
48 Subclasses are free to reimplement this method (@see KeepMailboxOpenTask), but they must not forget to update the parentTask of
49 the depending task.
51 void ImapTask::addDependentTask(ImapTask *task)
53 CHECK_TASK_TREE
54 Q_ASSERT(task);
55 dependentTasks.append(task);
56 task->updateParentTask(this);
57 CHECK_TASK_TREE
60 /** @short Set this task's parent to the specified value */
61 void ImapTask::updateParentTask(ImapTask *newParent)
63 Q_ASSERT(!parentTask);
64 Q_ASSERT(newParent);
65 parentTask = newParent;
66 CHECK_TASK_TREE
67 model->m_taskModel->slotTaskGotReparented(this);
68 if (parser) {
69 Q_ASSERT(!model->accessParser(parser).activeTasks.contains(this));
70 //log(tr("Reparented to %1").arg(newParent->debugIdentification()));
72 CHECK_TASK_TREE
75 /** @short Tells the Model that we're from now on an active task */
76 void ImapTask::markAsActiveTask(const TaskActivatingPosition place)
78 CHECK_TASK_TREE
79 Q_ASSERT(parser);
80 switch (place) {
81 case TASK_APPEND:
82 model->accessParser(parser).activeTasks.append(this);
83 break;
84 case TASK_PREPEND:
85 model->accessParser(parser).activeTasks.prepend(this);
86 break;
88 if (parentTask) {
89 parentTask->dependentTasks.removeAll(this);
91 // As we're an active task, we no longer have a parent task
92 parentTask = 0;
93 model->m_taskModel->slotTaskGotReparented(this);
95 if (model->accessParser(parser).maintainingTask && model->accessParser(parser).maintainingTask != this) {
96 // Got to inform the currently responsible maintaining task about our demise
97 connect(this, &QObject::destroyed, model->accessParser(parser).maintainingTask.data(), &KeepMailboxOpenTask::slotTaskDeleted);
100 log(QStringLiteral("Activated"));
101 CHECK_TASK_TREE
104 bool ImapTask::handleState(const Imap::Responses::State *const resp)
106 handleResponseCode(resp);
107 return handleStateHelper(resp);
110 bool ImapTask::handleStateHelper(const Imap::Responses::State *const resp)
112 Q_UNUSED(resp);
113 return false;
116 bool ImapTask::handleCapability(const Imap::Responses::Capability *const resp)
118 Q_UNUSED(resp);
119 return false;
122 bool ImapTask::handleNumberResponse(const Imap::Responses::NumberResponse *const resp)
124 Q_UNUSED(resp);
125 return false;
128 bool ImapTask::handleList(const Imap::Responses::List *const resp)
130 Q_UNUSED(resp);
131 return false;
134 bool ImapTask::handleFlags(const Imap::Responses::Flags *const resp)
136 Q_UNUSED(resp);
137 return false;
140 bool ImapTask::handleSearch(const Imap::Responses::Search *const resp)
142 Q_UNUSED(resp);
143 return false;
146 bool ImapTask::handleESearch(const Imap::Responses::ESearch *const resp)
148 Q_UNUSED(resp);
149 return false;
152 bool ImapTask::handleStatus(const Imap::Responses::Status *const resp)
154 Q_UNUSED(resp);
155 return false;
158 bool ImapTask::handleFetch(const Imap::Responses::Fetch *const resp)
160 Q_UNUSED(resp);
161 return false;
164 bool ImapTask::handleNamespace(const Imap::Responses::Namespace *const resp)
166 Q_UNUSED(resp);
167 return false;
170 bool ImapTask::handleSort(const Imap::Responses::Sort *const resp)
172 Q_UNUSED(resp);
173 return false;
176 bool ImapTask::handleThread(const Imap::Responses::Thread *const resp)
178 Q_UNUSED(resp);
179 return false;
182 bool ImapTask::handleId(const Responses::Id *const resp)
184 Q_UNUSED(resp);
185 return false;
188 bool ImapTask::handleEnabled(const Responses::Enabled *const resp)
190 Q_UNUSED(resp);
191 return false;
194 bool ImapTask::handleVanished(const Responses::Vanished *const resp)
196 Q_UNUSED(resp);
197 return false;
200 bool ImapTask::handleGenUrlAuth(const Responses::GenUrlAuth *const resp)
202 Q_UNUSED(resp);
203 return false;
206 bool ImapTask::handleSocketEncryptedResponse(const Imap::Responses::SocketEncryptedResponse *const resp)
208 Q_UNUSED(resp);
209 return false;
212 bool ImapTask::handleSocketDisconnectedResponse(const Imap::Responses::SocketDisconnectedResponse *const resp)
214 Q_UNUSED(resp);
215 return false;
218 bool ImapTask::handleParseErrorResponse(const Imap::Responses::ParseErrorResponse *const resp)
220 Q_UNUSED(resp);
221 return false;
224 void ImapTask::_completed()
226 _finished = true;
227 log(QStringLiteral("Completed"));
228 Q_FOREACH(ImapTask* task, dependentTasks) {
229 if (!task->isFinished())
230 task->perform();
232 emit completed(this);
235 void ImapTask::_failed(const QString &errorMessage)
237 _finished = true;
238 killAllPendingTasks(errorMessage);
239 log(QString::fromUtf8("Failed: %1").arg(errorMessage));
240 emit failed(errorMessage);
243 void ImapTask::killAllPendingTasks(const QString &message)
245 Q_FOREACH(ImapTask *task, dependentTasks) {
246 task->die(message);
250 void ImapTask::handleResponseCode(const Imap::Responses::State *const resp)
252 using namespace Imap::Responses;
253 // Check for common stuff like ALERT and CAPABILITIES update
254 switch (resp->respCode) {
255 case ALERT:
256 EMIT_LATER(model, alertReceived, Q_ARG(QString, tr("The server sent the following ALERT:\n%1").arg(resp->message)));
257 break;
258 case CAPABILITIES:
260 const RespData<QStringList> *const caps = dynamic_cast<const RespData<QStringList>* const>(resp->respCodeData.data());
261 if (caps) {
262 model->updateCapabilities(parser, caps->data);
265 break;
266 case BADCHARSET:
267 case PARSE:
268 qDebug() << "The server was having troubles with parsing message data:" << resp->message;
269 break;
270 default:
271 // do nothing here, it must be handled later
272 break;
276 bool ImapTask::isReadyToRun() const
278 return false;
281 void ImapTask::die(const QString &message)
283 _dead = true;
284 if (!_finished)
285 _failed(message);
288 void ImapTask::abort()
290 _aborted = true;
291 Q_FOREACH(ImapTask* task, dependentTasks) {
292 task->abort();
296 QString ImapTask::debugIdentification() const
298 return QString();
301 void ImapTask::log(const QString &message, const Common::LogKind kind)
303 Q_ASSERT(model);
304 QString dbg = debugIdentification();
305 if (!dbg.isEmpty()) {
306 dbg.prepend(QLatin1Char(' '));
308 model->logTrace(parser ? parser->parserId() : 0, kind, QString::fromUtf8(metaObject()->className()) + dbg, message);
309 model->m_taskModel->slotTaskMighHaveChanged(this);