KIMAP2

selectjob.cpp
1 /*
2  Copyright (c) 2009 Kevin Ottens <[email protected]>
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 "kimap_debug.h"
23 
24 #include "job_p.h"
25 #include "message_p.h"
26 #include "session_p.h"
27 #include "rfccodecs.h"
28 
29 namespace KIMAP2
30 {
31 class SelectJobPrivate : public JobPrivate
32 {
33 public:
34  SelectJobPrivate(Session *session, const QString &name)
35  : JobPrivate(session, name), readOnly(false), messageCount(-1), recentCount(-1),
36  firstUnseenIndex(-1), uidValidity(-1), nextUid(-1), highestmodseq(0),
37  condstoreEnabled(false) { }
38  ~SelectJobPrivate() { }
39 
40  QString mailBox;
41  bool readOnly;
42 
43  QList<QByteArray> flags;
44  QList<QByteArray> permanentFlags;
45  int messageCount;
46  int recentCount;
47  int firstUnseenIndex;
48  qint64 uidValidity;
49  qint64 nextUid;
50  quint64 highestmodseq;
51  bool condstoreEnabled;
52 };
53 }
54 
55 using namespace KIMAP2;
56 
57 SelectJob::SelectJob(Session *session)
58  : Job(*new SelectJobPrivate(session, "Select"))
59 {
60 }
61 
62 SelectJob::~SelectJob()
63 {
64 }
65 
66 void SelectJob::setMailBox(const QString &mailBox)
67 {
68  Q_D(SelectJob);
69  d->mailBox = mailBox;
70 }
71 
72 QString SelectJob::mailBox() const
73 {
74  Q_D(const SelectJob);
75  return d->mailBox;
76 }
77 
78 void SelectJob::setOpenReadOnly(bool readOnly)
79 {
80  Q_D(SelectJob);
81  d->readOnly = readOnly;
82 }
83 
84 bool SelectJob::isOpenReadOnly() const
85 {
86  Q_D(const SelectJob);
87  return d->readOnly;
88 }
89 
90 QList<QByteArray> SelectJob::flags() const
91 {
92  Q_D(const SelectJob);
93  return d->flags;
94 }
95 
96 QList<QByteArray> SelectJob::permanentFlags() const
97 {
98  Q_D(const SelectJob);
99  return d->permanentFlags;
100 }
101 
102 int SelectJob::messageCount() const
103 {
104  Q_D(const SelectJob);
105  return d->messageCount;
106 }
107 
108 int SelectJob::recentCount() const
109 {
110  Q_D(const SelectJob);
111  return d->recentCount;
112 }
113 
114 int SelectJob::firstUnseenIndex() const
115 {
116  Q_D(const SelectJob);
117  return d->firstUnseenIndex;
118 }
119 
120 qint64 SelectJob::uidValidity() const
121 {
122  Q_D(const SelectJob);
123  return d->uidValidity;
124 }
125 
126 qint64 SelectJob::nextUid() const
127 {
128  Q_D(const SelectJob);
129  return d->nextUid;
130 }
131 
132 quint64 SelectJob::highestModSequence() const
133 {
134  Q_D(const SelectJob);
135  return d->highestmodseq;
136 }
137 
138 void SelectJob::setCondstoreEnabled(bool enable)
139 {
140  Q_D(SelectJob);
141  d->condstoreEnabled = enable;
142 }
143 
144 bool SelectJob::condstoreEnabled() const
145 {
146  Q_D(const SelectJob);
147  return d->condstoreEnabled;
148 }
149 
150 void SelectJob::doStart()
151 {
152  Q_D(SelectJob);
153 
154  QByteArray command = "SELECT";
155  if (d->readOnly) {
156  command = "EXAMINE";
157  }
158 
159  QByteArray params = '\"' + KIMAP2::encodeImapFolderName(d->mailBox.toUtf8()) + '\"';
160 
161  if (d->condstoreEnabled) {
162  params += " (CONDSTORE)";
163  }
164 
165  d->sendCommand(command, params);
166 }
167 
168 void SelectJob::handleResponse(const Message &response)
169 {
170  Q_D(SelectJob);
171 
172  if (handleErrorReplies(response) == NotHandled) {
173  if (response.content.size() >= 2) {
174  QByteArray code = response.content[1].toString();
175 
176  if (code == "OK") {
177  if (response.responseCode.size() < 2) {
178  return;
179  }
180 
181  code = response.responseCode[0].toString();
182 
183  if (code == "PERMANENTFLAGS") {
184  d->permanentFlags = response.responseCode[1].toList();
185  } else if (code == "HIGHESTMODSEQ") {
186  bool isInt;
187  quint64 value = response.responseCode[1].toString().toULongLong(&isInt);
188  if (!isInt) {
189  return;
190  }
191  d->highestmodseq = value;
192  } else {
193  bool isInt;
194  qint64 value = response.responseCode[1].toString().toLongLong(&isInt);
195  if (!isInt) {
196  return;
197  }
198  if (code == "UIDVALIDITY") {
199  d->uidValidity = value;
200  } else if (code == "UNSEEN") {
201  d->firstUnseenIndex = value;
202  } else if (code == "UIDNEXT") {
203  d->nextUid = value;
204  }
205  }
206  } else if (code == "FLAGS") {
207  d->flags = response.content[2].toList();
208  } else {
209  bool isInt;
210  int value = response.content[1].toString().toInt(&isInt);
211  if (!isInt || response.content.size() < 3) {
212  return;
213  }
214 
215  code = response.content[2].toString();
216  if (code == "EXISTS") {
217  d->messageCount = value;
218  } else if (code == "RECENT") {
219  d->recentCount = value;
220  }
221  }
222  } else {
223  qCDebug(KIMAP2_LOG) << response.toString();
224  }
225  } else {
226  Q_ASSERT(error() || d->m_session->selectedMailBox() == d->mailBox);
227  }
228 }
KIMAP2_EXPORT QString encodeImapFolderName(const QString &src)
Converts an Unicode IMAP mailbox to a QString which can be used in IMAP communication.
Definition: rfccodecs.cpp:192
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QString name(StandardShortcut id)
qulonglong toULongLong(bool *ok, int base) const const
Provides handlers for various RFC/MIME encodings.
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Feb 5 2023 04:11:00 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.