getmetadatajob.cpp
00001 /* 00002 Copyright (c) 2009 Andras Mantia <amantia@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 "getmetadatajob.h" 00021 00022 #include <KDE/KLocale> 00023 #include <KDE/KDebug> 00024 00025 #include "metadatajobbase_p.h" 00026 #include "message_p.h" 00027 #include "session_p.h" 00028 #include "rfccodecs.h" 00029 00030 namespace KIMAP 00031 { 00032 class GetMetaDataJobPrivate : public MetaDataJobBasePrivate 00033 { 00034 public: 00035 GetMetaDataJobPrivate( Session *session, const QString& name ) : MetaDataJobBasePrivate(session, name), maxSize(-1), depth("0") { } 00036 ~GetMetaDataJobPrivate() { } 00037 00038 qint64 maxSize; 00039 QByteArray depth; 00040 QList<QByteArray> entries; 00041 QList<QByteArray> attributes; 00042 QMap<QString, QMap<QByteArray, QMap<QByteArray, QByteArray> > > metadata; 00043 // ^ mailbox ^ entry ^attribute ^ value 00044 }; 00045 } 00046 00047 using namespace KIMAP; 00048 00049 GetMetaDataJob::GetMetaDataJob( Session *session ) 00050 : MetaDataJobBase( *new GetMetaDataJobPrivate(session, i18n("GetMetaData")) ) 00051 { 00052 } 00053 00054 GetMetaDataJob::~GetMetaDataJob() 00055 { 00056 } 00057 00058 void GetMetaDataJob::doStart() 00059 { 00060 Q_D(GetMetaDataJob); 00061 QByteArray parameters; 00062 parameters = '\"' + KIMAP::encodeImapFolderName( d->mailBox.toUtf8() ) + "\" "; 00063 00064 QByteArray command = "GETMETADATA"; 00065 if (d->serverCapability == Annotatemore) { 00066 d->m_name = i18n("GetAnnotation"); 00067 command = "GETANNOTATION"; 00068 if (d->entries.size() > 1) 00069 parameters += '('; 00070 Q_FOREACH(const QByteArray &entry, d->entries) { 00071 parameters += '\"' + entry + "\" "; 00072 } 00073 if (d->entries.size() > 1) 00074 parameters[parameters.length() -1 ] = ')'; 00075 else 00076 parameters.truncate(parameters.length() -1); 00077 00078 parameters += ' '; 00079 00080 if (d->attributes.size() > 1) 00081 parameters += '('; 00082 Q_FOREACH(const QByteArray &attribute, d->attributes) { 00083 parameters += '\"' + attribute + "\" "; 00084 } 00085 if (d->attributes.size() > 1) 00086 parameters[parameters.length() -1 ] = ')'; 00087 else 00088 parameters.truncate(parameters.length() -1); 00089 00090 } else { 00091 if (d->depth != "0") { 00092 parameters += "(DEPTH " + d->depth; 00093 } 00094 if (d->maxSize != -1) { 00095 parameters += "(MAXSIZE " + QByteArray::number(d->maxSize) + ')'; 00096 } 00097 if (d->depth != "0") { 00098 parameters += " )"; 00099 } 00100 00101 if (d->entries.size() > 1) 00102 parameters += '('; 00103 Q_FOREACH(const QByteArray &entry, d->entries) { 00104 parameters += '\"' + entry + "\" "; 00105 } 00106 if (d->entries.size() > 1) 00107 parameters[parameters.length() -1 ] = ')'; 00108 } 00109 00110 if (d->entries.isEmpty()) { 00111 parameters += ')'; 00112 } 00113 00114 d->tags << d->sessionInternal()->sendCommand( command, parameters ); 00115 // kDebug() << "SENT: " << command << " " << parameters; 00116 } 00117 00118 void GetMetaDataJob::handleResponse( const Message &response ) 00119 { 00120 Q_D(GetMetaDataJob); 00121 // kDebug() << "GOT: " << response.toString(); 00122 00123 //TODO: handle NO error messages having [METADATA MAXSIZE NNN], [METADATA TOOMANY], [METADATA NOPRIVATE] (see rfc5464) 00124 // or [ANNOTATEMORE TOOBIG], [ANNOTATEMORE TOOMANY] respectively 00125 if (handleErrorReplies(response) == NotHandled ) { 00126 if ( response.content.size() >= 4 ) { 00127 if (d->serverCapability == Annotatemore && response.content[1].toString() == "ANNOTATION" ) { 00128 QString mailBox = QString::fromUtf8( KIMAP::decodeImapFolderName( response.content[2].toString() ) ); 00129 00130 int i = 3; 00131 while (i < response.content.size() - 1) { 00132 QByteArray entry = response.content[i].toString(); 00133 QList<QByteArray> attributes = response.content[i + 1].toList(); 00134 int j = 0; 00135 while ( j < attributes.size() - 1) { 00136 d->metadata[mailBox][entry][attributes[j]] = attributes[j + 1]; 00137 j += 2; 00138 } 00139 i += 2; 00140 } 00141 } else 00142 if (d->serverCapability == Metadata && response.content[1].toString() == "METADATA" ) { 00143 QString mailBox = QString::fromUtf8( KIMAP::decodeImapFolderName( response.content[2].toString() ) ); 00144 00145 QList<QByteArray> entries = response.content[3].toList(); 00146 int i = 0; 00147 while ( i < entries.size() - 1) { 00148 d->metadata[mailBox][entries[i]][""] = entries[i + 1]; 00149 i += 2; 00150 } 00151 } 00152 } 00153 } 00154 } 00155 00156 void GetMetaDataJob::addEntry(const QByteArray &entry, const QByteArray &attribute) 00157 { 00158 Q_D(GetMetaDataJob); 00159 if (d->serverCapability == Annotatemore && attribute.isNull()) 00160 qWarning() << "In ANNOTATEMORE mode an attribute must be specified with addEntry!"; 00161 d->entries.append(entry); 00162 d->attributes.append(attribute); 00163 } 00164 00165 void GetMetaDataJob::setMaximumSize(qint64 size) 00166 { 00167 Q_D(GetMetaDataJob); 00168 d->maxSize = size; 00169 } 00170 00171 void GetMetaDataJob::setDepth(Depth depth) 00172 { 00173 Q_D(GetMetaDataJob); 00174 00175 switch (depth) 00176 { 00177 case OneLevel: 00178 d->depth = "1"; //krazy:exclude=doublequote_chars 00179 break; 00180 case AllLevels: 00181 d->depth = "infinity"; 00182 break; 00183 default: 00184 d->depth = "0"; //krazy:exclude=doublequote_chars 00185 } 00186 } 00187 00188 QByteArray GetMetaDataJob::metaData(const QString &mailBox, const QByteArray &entry, const QByteArray &attribute) const 00189 { 00190 Q_D(const GetMetaDataJob); 00191 QByteArray attr = attribute; 00192 00193 if (d->serverCapability == Metadata) 00194 attr = ""; 00195 00196 QByteArray result; 00197 if (d->metadata.contains(mailBox)) { 00198 if (d->metadata[mailBox].contains(entry)) { 00199 result = d->metadata[mailBox][entry].value(attr); 00200 } 00201 } 00202 00203 return result; 00204 } 00205 00206 QMap<QByteArray, QMap<QByteArray, QByteArray> > GetMetaDataJob::allMetaData(const QString &mailBox) const 00207 { 00208 Q_D(const GetMetaDataJob); 00209 return d->metadata[mailBox]; 00210 } 00211 00212 #include "getmetadatajob.moc"