KIMAP

idlejob.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Kevin Ottens <ervin@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "idlejob.h"
8
9#include <KLocalizedString>
10#include <QTimer>
11
12#include "job_p.h"
13#include "response_p.h"
14#include "session_p.h"
15
16namespace KIMAP
17{
18class IdleJobPrivate : public JobPrivate
19{
20public:
21 IdleJobPrivate(IdleJob *job, Session *session, const QString &name)
22 : JobPrivate(session, name)
23 , q(job)
24 {
25 }
26 ~IdleJobPrivate()
27 {
28 }
29
30 void emitStats()
31 {
32 emitStatsTimer.stop();
33
34 Q_EMIT q->mailBoxStats(q, m_session->selectedMailBox(), messageCount, recentCount);
35
36 lastMessageCount = messageCount;
37 lastRecentCount = recentCount;
38
39 messageCount = -1;
40 recentCount = -1;
41 }
42
43 void resetTimeout()
44 {
45 sessionInternal()->setSocketTimeout(originalSocketTimeout);
46 }
47
48 IdleJob *const q;
49
50 QTimer emitStatsTimer;
51
52 int messageCount = -1;
53 int recentCount = -1;
54
55 int lastMessageCount = -1;
56 int lastRecentCount = -1;
57
58 int originalSocketTimeout = -1;
59};
60}
61
62using namespace KIMAP;
63
64IdleJob::IdleJob(Session *session)
65 : Job(*new IdleJobPrivate(this, session, i18nc("name of the idle job", "Idle")))
66{
67 Q_D(IdleJob);
68 connect(&d->emitStatsTimer, &QTimer::timeout, this, [d]() {
69 d->emitStats();
70 });
71
72 connect(this, &KJob::result, this, [d]() {
73 d->resetTimeout();
74 });
75}
76
77IdleJob::~IdleJob()
78{
79}
80
82{
83 Q_D(IdleJob);
84 d->sessionInternal()->setSocketTimeout(d->originalSocketTimeout);
85 d->sessionInternal()->sendData("DONE");
86}
87
88void IdleJob::doStart()
89{
90 Q_D(IdleJob);
91 d->originalSocketTimeout = d->sessionInternal()->socketTimeout();
92 d->sessionInternal()->setSocketTimeout(-1);
93 d->tags << d->sessionInternal()->sendCommand("IDLE");
94}
95
96void IdleJob::handleResponse(const Response &response)
97{
98 Q_D(IdleJob);
99
100 // We can predict it'll be handled by handleErrorReplies() so Q_EMIT
101 // pending signals now (if needed) so that result() will really be
102 // the last emitted signal.
103 if (!response.content.isEmpty() && d->tags.size() == 1 && d->tags.contains(response.content.first().toString())
104 && (d->messageCount >= 0 || d->recentCount >= 0)) {
105 d->emitStats();
106 }
107
108 if (handleErrorReplies(response) == NotHandled) {
109 if (!response.content.isEmpty() && response.content[0].toString() == "+") {
110 // Got the continuation all is fine
111 return;
112
113 } else if (response.content.size() > 2) {
114 const QByteArray ba = response.content[2].toString();
115 if (ba == "EXISTS") {
116 if (d->messageCount >= 0) {
117 d->emitStats();
118 }
119
120 d->messageCount = response.content[1].toString().toInt();
121 } else if (ba == "RECENT") {
122 if (d->recentCount >= 0) {
123 d->emitStats();
124 }
125
126 d->recentCount = response.content[1].toString().toInt();
127 } else if (ba == "FETCH") {
128 const qint64 uid = response.content[1].toString().toLongLong();
130 }
131 }
132
133 if (d->messageCount >= 0 && d->recentCount >= 0) {
134 d->emitStats();
135 } else if (d->messageCount >= 0 || d->recentCount >= 0) {
136 d->emitStatsTimer.start(200);
137 }
138 }
139}
140
142{
143 Q_D(const IdleJob);
144 return d->m_session->selectedMailBox();
145}
146
148{
149 Q_D(const IdleJob);
150 return d->lastMessageCount;
151}
152
154{
155 Q_D(const IdleJob);
156 return d->lastRecentCount;
157}
158
159#include "moc_idlejob.cpp"
Idles the connection to the IMAP server.
Definition idlejob.h:48
void stop()
Stops the idle job.
Definition idlejob.cpp:81
int lastRecentCount() const
The last recent message count that was reported.
Definition idlejob.cpp:153
void mailBoxStats(KIMAP::IdleJob *job, const QString &mailBox, int messageCount, int recentCount)
Signals that the server has notified that the total and recent message counts have changed.
QString lastMailBox() const
The last mailbox status that was reported.
Definition idlejob.cpp:141
void mailBoxMessageFlagsChanged(KIMAP::IdleJob *job, qint64 uid)
Signals that the server has notified that the some messages flags have changed.
int lastMessageCount() const
The last message count that was reported.
Definition idlejob.cpp:147
void result(KJob *job)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString name(StandardAction id)
Q_EMITQ_EMIT
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void stop()
void timeout()
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:17 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.