summaryrefslogtreecommitdiff
path: root/kimap/namespacejob.cpp
blob: 47b3930f04e38ce1ea84766a7dc6a49f68df859a (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
/*
    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 "namespacejob.h"

#include <KDE/KDebug>
#include <KDE/KLocalizedString>

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

namespace KIMAP
{
  class NamespaceJobPrivate : public JobPrivate
  {
    public:
      NamespaceJobPrivate( Session *session,  const QString& name ) : JobPrivate( session, name ) { }
      ~NamespaceJobPrivate() { }

      QList<MailBoxDescriptor> processNamespaceList( const QList<QByteArray> &namespaceList )
      {
        QList<MailBoxDescriptor> result;

        foreach ( const QByteArray &namespaceItem, namespaceList ) {
            ImapStreamParser parser( 0 );
            parser.setData( namespaceItem );

            try {
                QList<QByteArray> parts = parser.readParenthesizedList();
                if ( parts.size() < 2 ) {
                    continue;
                }
                MailBoxDescriptor descriptor;
                descriptor.name = QString::fromUtf8( decodeImapFolderName( parts[0] ) );
                descriptor.separator = QChar( parts[1][0] );

                result << descriptor;
            } catch ( KIMAP::ImapParserException e ) {
                qWarning() << "The stream parser raised an exception during namespace list parsing:" << e.what();
                qWarning() << "namespacelist:" << namespaceList;
            }

        }

        return result;
      }

      QList<MailBoxDescriptor> personalNamespaces;
      QList<MailBoxDescriptor> userNamespaces;
      QList<MailBoxDescriptor> sharedNamespaces;
  };
}

using namespace KIMAP;

NamespaceJob::NamespaceJob( Session *session )
  : Job( *new NamespaceJobPrivate( session, i18n( "Namespace" ) ) )
{
}

NamespaceJob::~NamespaceJob()
{
}

QList<MailBoxDescriptor> NamespaceJob::personalNamespaces() const
{
  Q_D( const NamespaceJob );
  return d->personalNamespaces;
}

QList<MailBoxDescriptor> NamespaceJob::userNamespaces() const
{
  Q_D( const NamespaceJob );
  return d->userNamespaces;
}

QList<MailBoxDescriptor> NamespaceJob::sharedNamespaces() const
{
  Q_D( const NamespaceJob );
  return d->sharedNamespaces;
}

bool NamespaceJob::containsEmptyNamespace() const
{
  Q_D( const NamespaceJob );
  QList<MailBoxDescriptor> completeList = d->personalNamespaces
                                        + d->userNamespaces
                                        + d->sharedNamespaces;

  foreach ( const MailBoxDescriptor &descriptor, completeList ) {
    if ( descriptor.name.isEmpty() ) {
      return true;
    }
  }

  return false;
}

void NamespaceJob::doStart()
{
  Q_D( NamespaceJob );
  d->tags << d->sessionInternal()->sendCommand( "NAMESPACE" );
}

void NamespaceJob::handleResponse( const Message &response )
{
  Q_D( NamespaceJob );
  if ( handleErrorReplies( response ) == NotHandled ) {
    if ( response.content.size() >= 5 &&
         response.content[1].toString() == "NAMESPACE" ) {
      // Personal namespaces
      d->personalNamespaces = d->processNamespaceList( response.content[2].toList() );

      // User namespaces
      d->userNamespaces = d->processNamespaceList( response.content[3].toList() );

      // Shared namespaces
      d->sharedNamespaces = d->processNamespaceList( response.content[4].toList() );
    }
  }
}

#include "namespacejob.moc"