job_8cpp_source

namespacejob.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 "namespacejob.h"
21
22#include "kimap_debug.h"
23
24#include "job_p.h"
25#include "listjob.h"
26#include "message_p.h"
27#include "rfccodecs.h"
28#include "session_p.h"
29
30static QList<QByteArray> parseParenthesizedList(const QByteArray &data)
31{
32 QList<QByteArray> result;
33 if (data.isEmpty()) {
34 return result;
35 }
36 if (data.at(0) != '(') {
37 return result;
38 }
39 bool parsingString = false;
40 int start = 0;
41 for (int pos = 1; pos < data.size(); pos++) {
42 //Match unescaped quotes
43 if (data.at(pos) == '\"' && data.at(pos - 1) != '\\') {
44 if (parsingString) {
45 parsingString = false;
46 result << data.mid(start, pos - start);
47 } else {
48 parsingString = true;
49 start = pos + 1;
50 }
51 }
52 }
53 return result;
54}
55
56namespace KIMAP2
57{
58class NamespaceJobPrivate : public JobPrivate
59{
60public:
61 NamespaceJobPrivate(Session *session, const QString &name) : JobPrivate(session, name) { }
62 ~NamespaceJobPrivate() { }
63
64 QList<MailBoxDescriptor> processNamespaceList(const QList<QByteArray> &namespaceList)
65 {
67
68 foreach (const QByteArray &namespaceItem, namespaceList) {
69 QList<QByteArray> parts = parseParenthesizedList(namespaceItem);
70 if (parts.size() < 2) {
71 qWarning() << "Not enough parts in namespace item " << namespaceItem;
72 continue;
73 }
74 MailBoxDescriptor descriptor;
75 descriptor.name = QString::fromUtf8(decodeImapFolderName(parts[0]));
76 descriptor.separator = QLatin1Char(parts[1][0]);
77
78 result << descriptor;
79 }
80
81 return result;
82 }
83
84 QList<MailBoxDescriptor> personalNamespaces;
85 QList<MailBoxDescriptor> userNamespaces;
86 QList<MailBoxDescriptor> sharedNamespaces;
87};
88}
89
90using namespace KIMAP2;
91
92NamespaceJob::NamespaceJob(Session *session)
93 : Job(*new NamespaceJobPrivate(session, "Namespace"))
94{
95}
96
97NamespaceJob::~NamespaceJob()
98{
99}
100
101QList<MailBoxDescriptor> NamespaceJob::personalNamespaces() const
102{
103 Q_D(const NamespaceJob);
104 return d->personalNamespaces;
105}
106
107QList<MailBoxDescriptor> NamespaceJob::userNamespaces() const
108{
109 Q_D(const NamespaceJob);
110 return d->userNamespaces;
111}
112
113QList<MailBoxDescriptor> NamespaceJob::sharedNamespaces() const
114{
115 Q_D(const NamespaceJob);
116 return d->sharedNamespaces;
117}
118
119bool NamespaceJob::containsEmptyNamespace() const
120{
121 Q_D(const NamespaceJob);
122 QList<MailBoxDescriptor> completeList = d->personalNamespaces
123 + d->userNamespaces
124 + d->sharedNamespaces;
125
126 foreach (const MailBoxDescriptor &descriptor, completeList) {
127 if (descriptor.name.isEmpty()) {
128 return true;
129 }
130 }
131
132 return false;
133}
134
135void NamespaceJob::doStart()
136{
137 Q_D(NamespaceJob);
138 d->sendCommand("NAMESPACE", {});
139}
140
141void NamespaceJob::handleResponse(const Message &response)
142{
143 Q_D(NamespaceJob);
144 if (handleErrorReplies(response) == NotHandled) {
145 if (response.content.size() >= 5 &&
146 response.content[1].toString() == "NAMESPACE") {
147 // Personal namespaces
148 d->personalNamespaces = d->processNamespaceList(response.content[2].toList());
149
150 // User namespaces
151 d->userNamespaces = d->processNamespaceList(response.content[3].toList());
152
153 // Shared namespaces
154 d->sharedNamespaces = d->processNamespaceList(response.content[4].toList());
155 }
156 }
157}
Q_SCRIPTABLE Q_NOREPLY void start()
QString name(StandardShortcut id)
char at(qsizetype i) const const
bool isEmpty() const const
QByteArray mid(qsizetype pos, qsizetype len) const const
qsizetype size() const const
qsizetype size() const const
QString fromUtf8(QByteArrayView str)
bool isEmpty() const const
This file is part of the IMAP support library and defines the RfcCodecs class.
KIMAP2_EXPORT QByteArray decodeImapFolderName(const QByteArray &inSrc)
Converts an UTF-7 encoded IMAP mailbox to a QByteArray.
Definition rfccodecs.cpp:70
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.