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 <KDE/KDebug> |
23 | #include <KDE/KLocalizedString> |
24 | |
25 | #include "job_p.h" |
26 | #include "listjob.h" |
27 | #include "message_p.h" |
28 | #include "rfccodecs.h" |
29 | #include "session_p.h" |
30 | #include "imapstreamparser.h" |
31 | |
32 | namespace KIMAP |
33 | { |
34 | class NamespaceJobPrivate : public JobPrivate |
35 | { |
36 | public: |
37 | NamespaceJobPrivate( Session *session, const QString& name ) : JobPrivate( session, name ) { } |
38 | ~NamespaceJobPrivate() { } |
39 | |
40 | QList<MailBoxDescriptor> processNamespaceList( const QList<QByteArray> &namespaceList ) |
41 | { |
42 | QList<MailBoxDescriptor> result; |
43 | |
44 | foreach ( const QByteArray &namespaceItem, namespaceList ) { |
45 | ImapStreamParser parser( 0 ); |
46 | parser.setData( namespaceItem ); |
47 | |
48 | try { |
49 | QList<QByteArray> parts = parser.readParenthesizedList(); |
50 | if ( parts.size() < 2 ) { |
51 | continue; |
52 | } |
53 | MailBoxDescriptor descriptor; |
54 | descriptor.name = QString::fromUtf8( decodeImapFolderName( parts[0] ) ); |
55 | descriptor.separator = QLatin1Char( parts[1][0] ); |
56 | |
57 | result << descriptor; |
58 | } catch ( KIMAP::ImapParserException e ) { |
59 | qWarning() << "The stream parser raised an exception during namespace list parsing:" << e.what(); |
60 | qWarning() << "namespacelist:" << namespaceList; |
61 | } |
62 | |
63 | } |
64 | |
65 | return result; |
66 | } |
67 | |
68 | QList<MailBoxDescriptor> personalNamespaces; |
69 | QList<MailBoxDescriptor> userNamespaces; |
70 | QList<MailBoxDescriptor> sharedNamespaces; |
71 | }; |
72 | } |
73 | |
74 | using namespace KIMAP; |
75 | |
76 | NamespaceJob::NamespaceJob( Session *session ) |
77 | : Job( *new NamespaceJobPrivate( session, i18n( "Namespace" ) ) ) |
78 | { |
79 | } |
80 | |
81 | NamespaceJob::~NamespaceJob() |
82 | { |
83 | } |
84 | |
85 | QList<MailBoxDescriptor> NamespaceJob::personalNamespaces() const |
86 | { |
87 | Q_D( const NamespaceJob ); |
88 | return d->personalNamespaces; |
89 | } |
90 | |
91 | QList<MailBoxDescriptor> NamespaceJob::userNamespaces() const |
92 | { |
93 | Q_D( const NamespaceJob ); |
94 | return d->userNamespaces; |
95 | } |
96 | |
97 | QList<MailBoxDescriptor> NamespaceJob::sharedNamespaces() const |
98 | { |
99 | Q_D( const NamespaceJob ); |
100 | return d->sharedNamespaces; |
101 | } |
102 | |
103 | bool NamespaceJob::containsEmptyNamespace() const |
104 | { |
105 | Q_D( const NamespaceJob ); |
106 | QList<MailBoxDescriptor> completeList = d->personalNamespaces |
107 | + d->userNamespaces |
108 | + d->sharedNamespaces; |
109 | |
110 | foreach ( const MailBoxDescriptor &descriptor, completeList ) { |
111 | if ( descriptor.name.isEmpty() ) { |
112 | return true; |
113 | } |
114 | } |
115 | |
116 | return false; |
117 | } |
118 | |
119 | void NamespaceJob::doStart() |
120 | { |
121 | Q_D( NamespaceJob ); |
122 | d->tags << d->sessionInternal()->sendCommand( "NAMESPACE" ); |
123 | } |
124 | |
125 | void NamespaceJob::handleResponse( const Message &response ) |
126 | { |
127 | Q_D( NamespaceJob ); |
128 | if ( handleErrorReplies( response ) == NotHandled ) { |
129 | if ( response.content.size() >= 5 && |
130 | response.content[1].toString() == "NAMESPACE" ) { |
131 | // Personal namespaces |
132 | d->personalNamespaces = d->processNamespaceList( response.content[2].toList() ); |
133 | |
134 | // User namespaces |
135 | d->userNamespaces = d->processNamespaceList( response.content[3].toList() ); |
136 | |
137 | // Shared namespaces |
138 | d->sharedNamespaces = d->processNamespaceList( response.content[4].toList() ); |
139 | } |
140 | } |
141 | } |
142 | |