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 "listjob.h" |
21 | |
22 | #include <boost/bind.hpp> |
23 | #include <QtCore/QTimer> |
24 | #include <KDE/KLocalizedString> |
25 | |
26 | #include "job_p.h" |
27 | #include "message_p.h" |
28 | #include "rfccodecs.h" |
29 | #include "session_p.h" |
30 | |
31 | namespace KIMAP |
32 | { |
33 | class ListJobPrivate : public JobPrivate |
34 | { |
35 | public: |
36 | ListJobPrivate( ListJob *job, Session *session, const QString& name ) : JobPrivate( session, name ), q( job ), option( ListJob::NoOption ) { } |
37 | ~ListJobPrivate() { } |
38 | |
39 | void emitPendings() |
40 | { |
41 | if ( pendingDescriptors.isEmpty() ) { |
42 | return; |
43 | } |
44 | |
45 | emit q->mailBoxesReceived( pendingDescriptors, pendingFlags ); |
46 | |
47 | pendingDescriptors.clear(); |
48 | pendingFlags.clear(); |
49 | } |
50 | |
51 | ListJob * const q; |
52 | |
53 | ListJob::Option option; |
54 | QList<MailBoxDescriptor> namespaces; |
55 | QByteArray command; |
56 | |
57 | QTimer emitPendingsTimer; |
58 | QList<MailBoxDescriptor> pendingDescriptors; |
59 | QList< QList<QByteArray> > pendingFlags; |
60 | }; |
61 | } |
62 | |
63 | using namespace KIMAP; |
64 | |
65 | ListJob::ListJob( Session *session ) |
66 | : Job( *new ListJobPrivate( this, session, i18n( "List" ) ) ) |
67 | { |
68 | Q_D( ListJob ); |
69 | connect( &d->emitPendingsTimer, SIGNAL(timeout()), |
70 | this, SLOT(emitPendings()) ); |
71 | } |
72 | |
73 | ListJob::~ListJob() |
74 | { |
75 | } |
76 | |
77 | void ListJob::setIncludeUnsubscribed( bool include ) |
78 | { |
79 | Q_D( ListJob ); |
80 | if ( include ) { |
81 | d->option = ListJob::IncludeUnsubscribed; |
82 | } else { |
83 | d->option = ListJob::NoOption; |
84 | } |
85 | } |
86 | |
87 | bool ListJob::isIncludeUnsubscribed() const |
88 | { |
89 | Q_D( const ListJob ); |
90 | return ( d->option == ListJob::IncludeUnsubscribed ); |
91 | } |
92 | |
93 | void ListJob::setOption( Option option ) |
94 | { |
95 | Q_D( ListJob ); |
96 | d->option = option; |
97 | } |
98 | |
99 | ListJob::Option ListJob::option() const |
100 | { |
101 | Q_D( const ListJob ); |
102 | return d->option; |
103 | } |
104 | |
105 | void ListJob::setQueriedNamespaces( const QList<MailBoxDescriptor> &namespaces ) |
106 | { |
107 | Q_D( ListJob ); |
108 | d->namespaces = namespaces; |
109 | } |
110 | |
111 | QList<MailBoxDescriptor> ListJob::queriedNamespaces() const |
112 | { |
113 | Q_D( const ListJob ); |
114 | return d->namespaces; |
115 | } |
116 | |
117 | QList<MailBoxDescriptor> ListJob::mailBoxes() const |
118 | { |
119 | return QList<MailBoxDescriptor>(); |
120 | } |
121 | |
122 | QMap< MailBoxDescriptor, QList<QByteArray> > ListJob::flags() const |
123 | { |
124 | return QMap< MailBoxDescriptor, QList<QByteArray> >(); |
125 | } |
126 | |
127 | void ListJob::doStart() |
128 | { |
129 | Q_D( ListJob ); |
130 | |
131 | switch ( d->option ) { |
132 | break; |
133 | case IncludeUnsubscribed: |
134 | d->command = "LIST" ; |
135 | break; |
136 | case IncludeFolderRoleFlags: |
137 | d->command = "XLIST" ; |
138 | break; |
139 | case NoOption: |
140 | default: |
141 | d->command = "LSUB" ; |
142 | } |
143 | |
144 | d->emitPendingsTimer.start( 100 ); |
145 | |
146 | if ( d->namespaces.isEmpty() ) { |
147 | d->tags << d->sessionInternal()->sendCommand( d->command, "\"\" *" ); |
148 | } else { |
149 | foreach ( const MailBoxDescriptor &descriptor, d->namespaces ) { |
150 | QString parameters = QLatin1String("\"\" \"%1\"" ); |
151 | |
152 | if ( descriptor.name.endsWith( descriptor.separator ) ) { |
153 | QString name = encodeImapFolderName( descriptor.name ); |
154 | name.chop( 1 ); |
155 | d->tags << d->sessionInternal()->sendCommand( d->command, |
156 | parameters.arg( name ).toUtf8() ); |
157 | } |
158 | |
159 | d->tags << d->sessionInternal()->sendCommand( d->command, |
160 | parameters.arg( descriptor.name + QLatin1Char('*') ).toUtf8() ); |
161 | } |
162 | } |
163 | } |
164 | |
165 | void ListJob::handleResponse( const Message &response ) |
166 | { |
167 | Q_D( ListJob ); |
168 | |
169 | // We can predict it'll be handled by handleErrorReplies() so stop |
170 | // the timer now so that result() will really be the last emitted signal. |
171 | if ( !response.content.isEmpty() && |
172 | d->tags.size() == 1 && |
173 | d->tags.contains( response.content.first().toString() ) ) { |
174 | d->emitPendingsTimer.stop(); |
175 | d->emitPendings(); |
176 | } |
177 | |
178 | if ( handleErrorReplies( response ) == NotHandled ) { |
179 | if ( response.content.size() >= 5 && response.content[1].toString() == d->command ) { |
180 | QList<QByteArray> flags = response.content[2].toList(); |
181 | std::transform( flags.begin(), flags.end(), flags.begin(), boost::bind( &QByteArray::toLower, _1 ) ); |
182 | QByteArray separator = response.content[3].toString(); |
183 | if ( separator.isEmpty() ) { |
184 | // Defaults to / for servers reporting an empty list |
185 | // it's supposedly not a problem as servers doing that |
186 | // only do it for mailboxes with no child. |
187 | separator = "/" ; //krazy:exclude=doublequote_chars since a QByteArray |
188 | } |
189 | Q_ASSERT( separator.size() == 1 ); |
190 | QByteArray fullName; |
191 | for ( int i=4; i<response.content.size(); i++ ) { |
192 | fullName += response.content[i].toString() + ' '; |
193 | } |
194 | fullName.chop( 1 ); |
195 | |
196 | fullName = decodeImapFolderName( fullName ); |
197 | |
198 | MailBoxDescriptor mailBoxDescriptor; |
199 | mailBoxDescriptor.separator = QLatin1Char( separator[0] ); |
200 | mailBoxDescriptor.name = QString::fromUtf8( fullName ); |
201 | convertInboxName( mailBoxDescriptor ); |
202 | |
203 | d->pendingDescriptors << mailBoxDescriptor; |
204 | d->pendingFlags << flags; |
205 | } |
206 | } |
207 | } |
208 | |
209 | void ListJob::convertInboxName(KIMAP::MailBoxDescriptor& descriptor) |
210 | { |
211 | //Inbox must be case sensitive, according to the RFC, so make it always uppercase |
212 | QStringList pathParts = descriptor.name.split( descriptor.separator ); |
213 | if ( !pathParts.isEmpty() && |
214 | pathParts[0].compare( QLatin1String( "INBOX" ), Qt::CaseInsensitive ) == 0 ) { |
215 | pathParts.removeAt( 0 ); |
216 | descriptor.name = QLatin1String( "INBOX" ); |
217 | if ( !pathParts.isEmpty() ) { |
218 | descriptor.name += descriptor.separator + pathParts.join( descriptor.separator ); |
219 | } |
220 | } |
221 | } |
222 | #include "moc_listjob.cpp" |
223 | |