KIMAP2

idlejob.cpp
1/*
2 Copyright (c) 2009 Kevin Ottens <ervin@kde.org>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19
20#include "idlejob.h"
21
22#include <QtCore/QTimer>
23
24#include "job_p.h"
25#include "message_p.h"
26#include "session_p.h"
27
28namespace KIMAP2
29{
30class IdleJobPrivate : public JobPrivate
31{
32public:
33 IdleJobPrivate(IdleJob *job, Session *session, const QString &name)
34 : JobPrivate(session, name), q(job),
35 messageCount(-1), recentCount(-1),
36 lastMessageCount(-1), lastRecentCount(-1),
37 originalSocketTimeout(-1) { }
38 ~IdleJobPrivate() { }
39
40 void emitStats()
41 {
42 emitStatsTimer.stop();
43
44 emit q->mailBoxStats(q, m_session->selectedMailBox(),
45 messageCount, recentCount);
46
47 lastMessageCount = messageCount;
48 lastRecentCount = recentCount;
49
50 messageCount = -1;
51 recentCount = -1;
52 }
53
54 void resetTimeout()
55 {
56 sessionInternal()->setSocketTimeout(originalSocketTimeout);
57 }
58
59 IdleJob *const q;
60
61 QTimer emitStatsTimer;
62
63 int messageCount;
64 int recentCount;
65
66 int lastMessageCount;
67 int lastRecentCount;
68
69 int originalSocketTimeout;
70};
71}
72
73using namespace KIMAP2;
74
75IdleJob::IdleJob(Session *session)
76 : Job(*new IdleJobPrivate(this, session, "Idle"))
77{
78 Q_D(IdleJob);
79 connect(&d->emitStatsTimer, SIGNAL(timeout()),
80 this, SLOT(emitStats()));
81
82 connect(this, SIGNAL(result(KJob*)),
83 this, SLOT(resetTimeout()));
84}
85
86IdleJob::~IdleJob()
87{
88}
89
91{
92 Q_D(IdleJob);
93 d->sessionInternal()->setSocketTimeout(d->originalSocketTimeout);
94 d->sessionInternal()->sendData("DONE");
95}
96
97void IdleJob::doStart()
98{
99 Q_D(IdleJob);
100 d->originalSocketTimeout = d->sessionInternal()->socketTimeout();
101 d->sessionInternal()->setSocketTimeout(-1);
102 d->sendCommand("IDLE", {});
103}
104
105void IdleJob::handleResponse(const Message &response)
106{
107 Q_D(IdleJob);
108
109 // We can predict it'll be handled by handleErrorReplies() so emit
110 // pending signals now (if needed) so that result() will really be
111 // the last emitted signal.
112 if (!response.content.isEmpty() &&
113 d->tags.size() == 1 &&
114 d->tags.contains(response.content.first().toString()) &&
115 (d->messageCount >= 0 || d->recentCount >= 0)) {
116 d->emitStats();
117 }
118
119 if (handleErrorReplies(response) == NotHandled) {
120 if (response.content.size() > 0 && response.content[0].toString() == "+") {
121 // Got the continuation all is fine
122 return;
123
124 } else if (response.content.size() > 2) {
125 if (response.content[2].toString() == "EXISTS") {
126 if (d->messageCount >= 0) {
127 d->emitStats();
128 }
129
130 d->messageCount = response.content[1].toString().toInt();
131 } else if (response.content[2].toString() == "RECENT") {
132 if (d->recentCount >= 0) {
133 d->emitStats();
134 }
135
136 d->recentCount = response.content[1].toString().toInt();
137 } else if (response.content[2].toString() == "FETCH") {
138 const qint64 uid = response.content[1].toString().toLongLong();
140 }
141 }
142
143 if (d->messageCount >= 0 && d->recentCount >= 0) {
144 d->emitStats();
145 } else if (d->messageCount >= 0 || d->recentCount >= 0) {
146 d->emitStatsTimer.start(200);
147 }
148 }
149}
150
152{
153 Q_D(const IdleJob);
154 return d->m_session->selectedMailBox();
155}
156
158{
159 Q_D(const IdleJob);
160 return d->lastMessageCount;
161}
162
164{
165 Q_D(const IdleJob);
166 return d->lastRecentCount;
167}
168
169#include "moc_idlejob.cpp"
Idles the connection to the IMAP server.
Definition idlejob.h:63
int lastRecentCount() const
The last recent message count that was reported.
Definition idlejob.cpp:163
void stop()
Stops the idle job.
Definition idlejob.cpp:90
void mailBoxMessageFlagsChanged(KIMAP2::IdleJob *job, qint64 uid)
Signals that the server has notified that the some messages flags have changed.
void mailBoxStats(KIMAP2::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:151
int lastMessageCount() const
The last message count that was reported.
Definition idlejob.cpp:157
QString name(StandardShortcut id)
Q_EMITQ_EMIT
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void stop()
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:21:18 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.