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 "selectjob.h"
21
22#include <KDE/KLocalizedString>
23#include <kdebug.h>
24
25#include "job_p.h"
26#include "message_p.h"
27#include "session_p.h"
28#include "rfccodecs.h"
29
30namespace KIMAP
31{
32 class SelectJobPrivate : public JobPrivate
33 {
34 public:
35 SelectJobPrivate( Session *session, const QString& name )
36 : JobPrivate( session, name ), readOnly( false ), messageCount( -1 ), recentCount( -1 ),
37 firstUnseenIndex( -1 ), uidValidity( -1 ), nextUid( -1 ), highestmodseq( 0 ),
38 condstoreEnabled( false ) { }
39 ~SelectJobPrivate() { }
40
41 QString mailBox;
42 bool readOnly;
43
44 QList<QByteArray> flags;
45 QList<QByteArray> permanentFlags;
46 int messageCount;
47 int recentCount;
48 int firstUnseenIndex;
49 qint64 uidValidity;
50 qint64 nextUid;
51 quint64 highestmodseq;
52 bool condstoreEnabled;
53 };
54}
55
56using namespace KIMAP;
57
58SelectJob::SelectJob( Session *session )
59 : Job( *new SelectJobPrivate( session, i18nc( "name of the select job", "Select" ) ) )
60{
61}
62
63SelectJob::~SelectJob()
64{
65}
66
67void SelectJob::setMailBox( const QString &mailBox )
68{
69 Q_D( SelectJob );
70 d->mailBox = mailBox;
71}
72
73QString SelectJob::mailBox() const
74{
75 Q_D( const SelectJob );
76 return d->mailBox;
77}
78
79void SelectJob::setOpenReadOnly( bool readOnly )
80{
81 Q_D( SelectJob );
82 d->readOnly = readOnly;
83}
84
85bool SelectJob::isOpenReadOnly() const
86{
87 Q_D( const SelectJob );
88 return d->readOnly;
89}
90
91QList<QByteArray> SelectJob::flags() const
92{
93 Q_D( const SelectJob );
94 return d->flags;
95}
96
97QList<QByteArray> SelectJob::permanentFlags() const
98{
99 Q_D( const SelectJob );
100 return d->permanentFlags;
101}
102
103int SelectJob::messageCount() const
104{
105 Q_D( const SelectJob );
106 return d->messageCount;
107}
108
109int SelectJob::recentCount() const
110{
111 Q_D( const SelectJob );
112 return d->recentCount;
113}
114
115int SelectJob::firstUnseenIndex() const
116{
117 Q_D( const SelectJob );
118 return d->firstUnseenIndex;
119}
120
121qint64 SelectJob::uidValidity() const
122{
123 Q_D( const SelectJob );
124 return d->uidValidity;
125}
126
127qint64 SelectJob::nextUid() const
128{
129 Q_D( const SelectJob );
130 return d->nextUid;
131}
132
133quint64 SelectJob::highestModSequence() const
134{
135 Q_D( const SelectJob );
136 return d->highestmodseq;
137}
138
139void SelectJob::setCondstoreEnabled( bool enable )
140{
141 Q_D( SelectJob );
142 d->condstoreEnabled = enable;
143}
144
145bool SelectJob::condstoreEnabled() const
146{
147 Q_D( const SelectJob );
148 return d->condstoreEnabled;
149}
150
151
152void SelectJob::doStart()
153{
154 Q_D( SelectJob );
155
156 QByteArray command = "SELECT";
157 if ( d->readOnly ) {
158 command = "EXAMINE";
159 }
160
161 QByteArray params = '\"'+KIMAP::encodeImapFolderName( d->mailBox.toUtf8() )+'\"';
162
163 if ( d->condstoreEnabled ) {
164 params += " (CONDSTORE)";
165 }
166
167 d->tags << d->sessionInternal()->sendCommand( command, params );
168}
169
170void SelectJob::handleResponse( const Message &response )
171{
172 Q_D( SelectJob );
173
174 if ( handleErrorReplies( response ) == NotHandled ) {
175 if ( response.content.size() >= 2 ) {
176 QByteArray code = response.content[1].toString();
177
178 if ( code == "OK" ) {
179 if ( response.responseCode.size() < 2 ) {
180 return;
181 }
182
183 code = response.responseCode[0].toString();
184
185 if ( code == "PERMANENTFLAGS" ) {
186 d->permanentFlags = response.responseCode[1].toList();
187 } else if ( code == "HIGHESTMODSEQ" ) {
188 bool isInt;
189 quint64 value = response.responseCode[1].toString().toULongLong( &isInt );
190 if ( !isInt ) {
191 return;
192 }
193 d->highestmodseq = value;
194 } else {
195 bool isInt;
196 qint64 value = response.responseCode[1].toString().toLongLong( &isInt );
197 if ( !isInt ) {
198 return;
199 }
200 if ( code == "UIDVALIDITY" ) {
201 d->uidValidity = value;
202 } else if ( code == "UNSEEN" ) {
203 d->firstUnseenIndex = value;
204 } else if ( code == "UIDNEXT" ) {
205 d->nextUid = value;
206 }
207 }
208 } else if ( code == "FLAGS" ) {
209 d->flags = response.content[2].toList();
210 } else {
211 bool isInt;
212 int value = response.content[1].toString().toInt( &isInt );
213 if ( !isInt || response.content.size() < 3 ) {
214 return;
215 }
216
217 code = response.content[2].toString();
218 if ( code == "EXISTS" ) {
219 d->messageCount = value;
220 } else if ( code == "RECENT" ) {
221 d->recentCount = value;
222 }
223 }
224 } else {
225 kDebug() << response.toString();
226 }
227 } else {
228 Q_ASSERT( error() || d->m_session->selectedMailBox() == d->mailBox );
229 }
230}
231