KIMAP2

listjob.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 "listjob.h"
21 
22 #include "job_p.h"
23 #include "message_p.h"
24 #include "rfccodecs.h"
25 #include "session_p.h"
26 
27 static const int _kimap_mailBoxDescriptorId = qRegisterMetaType<KIMAP2::MailBoxDescriptor>();
28 
29 namespace KIMAP2
30 {
31 class ListJobPrivate : public JobPrivate
32 {
33 public:
34  ListJobPrivate(ListJob *job, Session *session, const QString &name) : JobPrivate(session, name), q(job), option(ListJob::NoOption) { }
35  ~ListJobPrivate() { }
36 
37  ListJob *const q;
38 
39  ListJob::Option option;
40  QList<MailBoxDescriptor> namespaces;
41  QByteArray command;
42 };
43 }
44 
45 using namespace KIMAP2;
46 
47 ListJob::ListJob(Session *session)
48  : Job(*new ListJobPrivate(this, session, "List"))
49 {
50 }
51 
52 ListJob::~ListJob()
53 {
54 }
55 
56 void ListJob::setOption(Option option)
57 {
58  Q_D(ListJob);
59  d->option = option;
60 }
61 
62 ListJob::Option ListJob::option() const
63 {
64  Q_D(const ListJob);
65  return d->option;
66 }
67 
68 void ListJob::setQueriedNamespaces(const QList<MailBoxDescriptor> &namespaces)
69 {
70  Q_D(ListJob);
71  d->namespaces = namespaces;
72 }
73 
74 QList<MailBoxDescriptor> ListJob::queriedNamespaces() const
75 {
76  Q_D(const ListJob);
77  return d->namespaces;
78 }
79 
80 void ListJob::doStart()
81 {
82  Q_D(ListJob);
83 
84  switch (d->option) {
85  break;
86  case IncludeUnsubscribed:
87  d->command = "LIST";
88  break;
89  case IncludeFolderRoleFlags:
90  d->command = "XLIST";
91  break;
92  case NoOption:
93  default:
94  d->command = "LSUB";
95  }
96 
97  if (d->namespaces.isEmpty()) {
98  d->sendCommand(d->command, "\"\" *");
99  } else {
100  foreach (const MailBoxDescriptor &descriptor, d->namespaces) {
101  QString parameters = QStringLiteral("\"\" \"%1\"");
102 
103  if (descriptor.name.endsWith(descriptor.separator)) {
104  QString name = encodeImapFolderName(descriptor.name);
105  name.chop(1);
106  d->sendCommand(d->command,
107  parameters.arg(name).toUtf8());
108  }
109 
110  d->sendCommand(d->command,
111  parameters.arg(descriptor.name + QLatin1Char('*')).toUtf8());
112  }
113  }
114 }
115 
116 void ListJob::handleResponse(const Message &response)
117 {
118  Q_D(ListJob);
119 
120  if (handleErrorReplies(response) == NotHandled) {
121  if (response.content.size() >= 5 && response.content[1].toString() == d->command) {
122  QList<QByteArray> flags = response.content[2].toList();
123  for (QList<QByteArray>::iterator it = flags.begin(), itEnd = flags.end(); it != itEnd; ++it) {
124  *it = it->toLower();
125  }
126  QByteArray separator = response.content[3].toString();
127  if (separator.isEmpty()) {
128  // Defaults to / for servers reporting an empty list
129  // it's supposedly not a problem as servers doing that
130  // only do it for mailboxes with no child.
131  separator = "/"; //krazy:exclude=doublequote_chars since a QByteArray
132  }
133  Q_ASSERT(separator.size() == 1);
135  for (int i = 4; i < response.content.size(); i++) {
136  fullName += response.content[i].toString() + ' ';
137  }
138  fullName.chop(1);
139 
140  fullName = decodeImapFolderName(fullName);
141 
142  MailBoxDescriptor mailBoxDescriptor;
143  mailBoxDescriptor.separator = QLatin1Char(separator[0]);
144  mailBoxDescriptor.name = QString::fromUtf8(fullName);
145  convertInboxName(mailBoxDescriptor);
146 
147  emit resultReceived(mailBoxDescriptor, flags);
148  }
149  }
150 }
151 
152 void ListJob::convertInboxName(KIMAP2::MailBoxDescriptor &descriptor)
153 {
154  //Inbox must be case sensitive, according to the RFC, so make it always uppercase
155  QStringList pathParts = descriptor.name.split(descriptor.separator);
156  if (!pathParts.isEmpty() &&
157  pathParts[0].compare(QLatin1String("INBOX"), Qt::CaseInsensitive) == 0) {
158  pathParts.removeAt(0);
159  descriptor.name = QStringLiteral("INBOX");
160  if (!pathParts.isEmpty()) {
161  descriptor.name += descriptor.separator + pathParts.join(descriptor.separator);
162  }
163  }
164 }
165 #include "moc_listjob.cpp"
QString fromUtf8(const char *str, int size)
CaseInsensitive
void chop(int n)
void removeAt(int i)
QByteArray toUtf8() const const
bool isEmpty() const const
QString join(const QString &separator) const const
bool isEmpty() const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString name(StandardShortcut id)
QList::iterator begin()
KIMAP_EXPORT QByteArray decodeImapFolderName(const QByteArray &inSrc)
Converts an UTF-7 encoded IMAP mailbox to a QByteArray.
Definition: rfccodecs.cpp:70
int size() const const
QList::iterator end()
Provides handlers for various RFC/MIME encodings.
QString fullName(const PartType &type)
Q_D(Todo)
KIMAP_EXPORT QByteArray encodeImapFolderName(const QByteArray &src)
Converts an Unicode IMAP mailbox to a QByteArray which can be used in IMAP communication.
Definition: rfccodecs.cpp:197
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.