summaryrefslogtreecommitdiff
path: root/kimap/listjob.cpp
blob: 322fc3766424fadb5cd68d7469551a05a9054569 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
    Copyright (c) 2009 Kevin Ottens <ervin@kde.org>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "listjob.h"

#include <boost/bind.hpp>
#include <QtCore/QTimer>
#include <KDE/KLocalizedString>

#include "job_p.h"
#include "message_p.h"
#include "rfccodecs.h"
#include "session_p.h"

namespace KIMAP
{
  class ListJobPrivate : public JobPrivate
  {
    public:
      ListJobPrivate( ListJob *job, Session *session, const QString& name ) : JobPrivate( session, name ), q( job ), option( ListJob::NoOption ) { }
      ~ListJobPrivate() { }

      void emitPendings()
      {
        if ( pendingDescriptors.isEmpty() ) {
          return;
        }

        emit q->mailBoxesReceived( pendingDescriptors, pendingFlags );

        pendingDescriptors.clear();
        pendingFlags.clear();
      }

      ListJob * const q;

      ListJob::Option option;
      QList<MailBoxDescriptor> namespaces;
      QByteArray command;

      QTimer emitPendingsTimer;
      QList<MailBoxDescriptor> pendingDescriptors;
      QList< QList<QByteArray> > pendingFlags;
  };
}

using namespace KIMAP;

ListJob::ListJob( Session *session )
  : Job( *new ListJobPrivate( this, session, i18n( "List" ) ) )
{
  Q_D( ListJob );
  connect( &d->emitPendingsTimer, SIGNAL(timeout()),
           this, SLOT(emitPendings()) );
}

ListJob::~ListJob()
{
}

void ListJob::setIncludeUnsubscribed( bool include )
{
  Q_D( ListJob );
  if ( include ) {
    d->option = ListJob::IncludeUnsubscribed;
  } else {
    d->option = ListJob::NoOption;
  }
}

bool ListJob::isIncludeUnsubscribed() const
{
  Q_D( const ListJob );
  return ( d->option == ListJob::IncludeUnsubscribed );
}

void ListJob::setOption( Option option )
{
  Q_D( ListJob );
  d->option = option;
}

ListJob::Option ListJob::option() const
{
  Q_D( const ListJob );
  return d->option;
}

void ListJob::setQueriedNamespaces( const QList<MailBoxDescriptor> &namespaces )
{
  Q_D( ListJob );
  d->namespaces = namespaces;
}

QList<MailBoxDescriptor> ListJob::queriedNamespaces() const
{
  Q_D( const ListJob );
  return d->namespaces;
}

QList<MailBoxDescriptor> ListJob::mailBoxes() const
{
  return QList<MailBoxDescriptor>();
}

QMap< MailBoxDescriptor, QList<QByteArray> > ListJob::flags() const
{
  return QMap< MailBoxDescriptor, QList<QByteArray> >();
}

void ListJob::doStart()
{
  Q_D( ListJob );

  switch ( d->option ) {
    break;
  case IncludeUnsubscribed:
    d->command = "LIST";
    break;
  case IncludeFolderRoleFlags:
    d->command = "XLIST";
    break;
  case NoOption:
  default:
    d->command = "LSUB";
  }

  d->emitPendingsTimer.start( 100 );

  if ( d->namespaces.isEmpty() ) {
    d->tags << d->sessionInternal()->sendCommand( d->command, "\"\" *" );
  } else {
    foreach ( const MailBoxDescriptor &descriptor, d->namespaces ) {
      QString parameters = "\"\" \"%1\"";

      if ( descriptor.name.endsWith( descriptor.separator ) ) {
        QString name = encodeImapFolderName( descriptor.name );
        name.chop( 1 );
        d->tags << d->sessionInternal()->sendCommand( d->command,
                                                      parameters.arg( name ).toUtf8() );
      }

      d->tags << d->sessionInternal()->sendCommand( d->command,
                                                    parameters.arg( descriptor.name + '*' ).toUtf8() );
    }
  }
}

void ListJob::handleResponse( const Message &response )
{
  Q_D( ListJob );

  // We can predict it'll be handled by handleErrorReplies() so stop
  // the timer now so that result() will really be the last emitted signal.
  if ( !response.content.isEmpty() &&
       d->tags.size() == 1 &&
       d->tags.contains( response.content.first().toString() ) ) {
    d->emitPendingsTimer.stop();
    d->emitPendings();
  }

  if ( handleErrorReplies( response ) == NotHandled ) {
    if ( response.content.size() >= 5 && response.content[1].toString() == d->command ) {
      QList<QByteArray> flags = response.content[2].toList();
      std::transform( flags.begin(), flags.end(), flags.begin(), boost::bind( &QByteArray::toLower, _1 ) );
      QByteArray separator = response.content[3].toString();
      if ( separator.isEmpty() ) {
        // Defaults to / for servers reporting an empty list
        // it's supposedly not a problem as servers doing that
        // only do it for mailboxes with no child.
        separator = "/"; //krazy:exclude=doublequote_chars since a QByteArray
      }
      Q_ASSERT( separator.size() == 1 );
      QByteArray fullName;
      for ( int i=4; i<response.content.size(); i++ ) {
        fullName += response.content[i].toString() + ' ';
      }
      fullName.chop( 1 );

      fullName = decodeImapFolderName( fullName );

      MailBoxDescriptor mailBoxDescriptor;
      mailBoxDescriptor.separator = QChar( separator[0] );
      mailBoxDescriptor.name = QString::fromUtf8( fullName );
      convertInboxName( mailBoxDescriptor );

      d->pendingDescriptors << mailBoxDescriptor;
      d->pendingFlags << flags;
    }
  }
}

void ListJob::convertInboxName(KIMAP::MailBoxDescriptor& descriptor)
{
    //Inbox must be case sensitive, according to the RFC, so make it always uppercase
    QStringList pathParts = descriptor.name.split( descriptor.separator );
    if ( !pathParts.isEmpty() &&
         pathParts[0].compare( QLatin1String( "INBOX" ), Qt::CaseInsensitive ) == 0 ) {
       pathParts.removeAt( 0 );
       descriptor.name = QLatin1String( "INBOX" );
       if ( !pathParts.isEmpty() ) {
        descriptor.name += descriptor.separator + pathParts.join( descriptor.separator );
       }
    }
}

#include "listjob.moc"