namespacejob.cpp
00001 /* 00002 Copyright (c) 2009 Kevin Ottens <ervin@kde.org> 00003 00004 This library is free software; you can redistribute it and/or modify it 00005 under the terms of the GNU Library General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or (at your 00007 option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, but WITHOUT 00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00012 License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to the 00016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 02110-1301, USA. 00018 */ 00019 00020 #include "namespacejob.h" 00021 00022 #include <KDE/KDebug> 00023 #include <KDE/KLocale> 00024 00025 #include "job_p.h" 00026 #include "listjob.h" 00027 #include "message_p.h" 00028 #include "rfccodecs.h" 00029 #include "session_p.h" 00030 #include "imapstreamparser.h" 00031 00032 namespace KIMAP 00033 { 00034 class NamespaceJobPrivate : public JobPrivate 00035 { 00036 public: 00037 NamespaceJobPrivate( Session *session, const QString& name ) : JobPrivate(session, name) { } 00038 ~NamespaceJobPrivate() { } 00039 00040 QList<MailBoxDescriptor> processNamespaceList( const QList<QByteArray> &namespaceList ) 00041 { 00042 QList<MailBoxDescriptor> result; 00043 00044 foreach ( const QByteArray &namespaceItem, namespaceList ) { 00045 ImapStreamParser parser( 0 ); 00046 parser.setData( namespaceItem ); 00047 00048 try { 00049 QList<QByteArray> parts = parser.readParenthesizedList(); 00050 if ( parts.size() < 2 ) { 00051 continue; 00052 } 00053 MailBoxDescriptor descriptor; 00054 descriptor.name = QString::fromUtf8( decodeImapFolderName( parts[0] ) ); 00055 descriptor.separator = QChar( parts[1][0] ); 00056 00057 result << descriptor; 00058 } catch (KIMAP::ImapParserException e) { 00059 qWarning() << "The stream parser raised an exception during namespace list parsing:" << e.what(); 00060 qWarning() << "namespacelist:" << namespaceList; 00061 } 00062 00063 } 00064 00065 return result; 00066 } 00067 00068 QList<MailBoxDescriptor> personalNamespaces; 00069 QList<MailBoxDescriptor> userNamespaces; 00070 QList<MailBoxDescriptor> sharedNamespaces; 00071 }; 00072 } 00073 00074 using namespace KIMAP; 00075 00076 NamespaceJob::NamespaceJob( Session *session ) 00077 : Job( *new NamespaceJobPrivate(session, i18n("Namespace")) ) 00078 { 00079 } 00080 00081 NamespaceJob::~NamespaceJob() 00082 { 00083 } 00084 00085 QList<MailBoxDescriptor> NamespaceJob::personalNamespaces() const 00086 { 00087 Q_D(const NamespaceJob); 00088 return d->personalNamespaces; 00089 } 00090 00091 QList<MailBoxDescriptor> NamespaceJob::userNamespaces() const 00092 { 00093 Q_D(const NamespaceJob); 00094 return d->userNamespaces; 00095 } 00096 00097 QList<MailBoxDescriptor> NamespaceJob::sharedNamespaces() const 00098 { 00099 Q_D(const NamespaceJob); 00100 return d->sharedNamespaces; 00101 } 00102 00103 bool NamespaceJob::containsEmptyNamespace() const 00104 { 00105 Q_D(const NamespaceJob); 00106 QList<MailBoxDescriptor> completeList = d->personalNamespaces 00107 + d->userNamespaces 00108 + d->sharedNamespaces; 00109 00110 foreach ( const MailBoxDescriptor &descriptor, completeList ) { 00111 if ( descriptor.name.isEmpty() ) { 00112 return true; 00113 } 00114 } 00115 00116 return false; 00117 } 00118 00119 void NamespaceJob::doStart() 00120 { 00121 Q_D(NamespaceJob); 00122 d->tags << d->sessionInternal()->sendCommand( "NAMESPACE" ); 00123 } 00124 00125 void NamespaceJob::handleResponse( const Message &response ) 00126 { 00127 Q_D(NamespaceJob); 00128 if (handleErrorReplies(response) == NotHandled) { 00129 if ( response.content.size() >= 5 00130 && response.content[1].toString()=="NAMESPACE" ) { 00131 // Personal namespaces 00132 d->personalNamespaces = d->processNamespaceList( response.content[2].toList() ); 00133 00134 // User namespaces 00135 d->userNamespaces = d->processNamespaceList( response.content[3].toList() ); 00136 00137 // Shared namespaces 00138 d->sharedNamespaces = d->processNamespaceList( response.content[4].toList() ); 00139 } 00140 } 00141 } 00142 00143 #include "namespacejob.moc"