KIMAP Library
setmetadatajob.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 "setmetadatajob.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 SetMetaDataJobPrivate : public MetaDataJobBasePrivate 00033 { 00034 public: 00035 SetMetaDataJobPrivate( Session *session, const QString& name ) : MetaDataJobBasePrivate(session, name), metaDataErrors(0), maxAcceptedSize(-1) { } 00036 ~SetMetaDataJobPrivate() { } 00037 00038 QMap<QByteArray, QByteArray> entries; 00039 QMap<QByteArray, QByteArray>::ConstIterator entriesIt; 00040 QByteArray entryName; 00041 SetMetaDataJob::MetaDataErrors metaDataErrors; 00042 qint64 maxAcceptedSize; 00043 }; 00044 } 00045 00046 using namespace KIMAP; 00047 00048 SetMetaDataJob::SetMetaDataJob( Session *session ) 00049 : MetaDataJobBase( *new SetMetaDataJobPrivate(session, i18n("SetMetaData")) ) 00050 { 00051 } 00052 00053 SetMetaDataJob::~SetMetaDataJob() 00054 { 00055 } 00056 00057 void SetMetaDataJob::doStart() 00058 { 00059 Q_D(SetMetaDataJob); 00060 QByteArray parameters; 00061 parameters = '\"' + KIMAP::encodeImapFolderName( d->mailBox.toUtf8() ) + "\" "; 00062 d->entriesIt = d->entries.constBegin(); 00063 00064 QByteArray command = "SETMETADATA"; 00065 if (d->serverCapability == Annotatemore) { 00066 command = "SETANNOTATION"; 00067 parameters += '\"' + d->entryName + "\" ("; 00068 d->m_name = i18n("SetAnnotation"); 00069 if (!d->entries.isEmpty()) { 00070 for (; d->entriesIt != d->entries.constEnd(); ++d->entriesIt) { 00071 parameters += '\"' + d->entriesIt.key() + "\" \"" + d->entriesIt.value() + "\" "; 00072 } 00073 parameters[parameters.length() - 1] = ')'; 00074 } 00075 } else { 00076 parameters += '('; 00077 if (!d->entries.isEmpty()) { 00078 parameters += '\"' + d->entriesIt.key() + '\"'; 00079 parameters += ' '; 00080 parameters +=" {" + QByteArray::number(d->entriesIt.value().size()) + '}'; 00081 } 00082 } 00083 00084 if (d->entries.isEmpty()) { 00085 parameters += ')'; 00086 } 00087 00088 d->tags << d->sessionInternal()->sendCommand( command, parameters ); 00089 // kDebug() << "SENT: " << command << " " << parameters; 00090 } 00091 00092 void SetMetaDataJob::handleResponse( const Message &response ) 00093 { 00094 Q_D(SetMetaDataJob); 00095 00096 //TODO: Test if a server can really return more then one untagged NO response. If not, no need to OR the error codes 00097 if ( !response.content.isEmpty() 00098 && d->tags.contains( response.content.first().toString() ) ) { 00099 if ( response.content[1].toString() == "NO" ) { 00100 setError( UserDefinedError ); 00101 setErrorText( i18n("%1 failed, server replied: %2", d->m_name, response.toString().constData()) ); 00102 if (response.content[2].toString() == "[ANNOTATEMORE TOOMANY]" || response.content[2].toString() == "[METADATA TOOMANY]") { 00103 d->metaDataErrors |= TooMany; 00104 } else if (response.content[2].toString() == "[ANNOTATEMORE TOOBIG]" || response.content[2].toString().startsWith("[METADATA MAXSIZE")) { 00105 d->metaDataErrors |= TooBig; 00106 d->maxAcceptedSize = -1; 00107 if (response.content[2].toString().startsWith("[METADATA MAXSIZE")) { //krazy:exclude=strings 00108 QByteArray max = response.content[2].toString(); 00109 max.replace("[METADATA MAXSIZE",""); //krazy:exclude=doublequote_chars 00110 max.replace("]", ""); //krazy:exclude=doublequote_chars 00111 d->maxAcceptedSize = max.toLongLong(); 00112 } 00113 } else if (response.content[2].toString() == "[METADATA NOPRIVATE]") { 00114 d->metaDataErrors |= NoPrivate; 00115 } 00116 } else if ( response.content.size() < 2 ) { 00117 setErrorText( i18n("%1 failed, malformed reply from the server.", d->m_name) ); 00118 } else if ( response.content[1].toString() != "OK" ) { 00119 setError( UserDefinedError ); 00120 setErrorText( i18n("%1 failed, server replied: %2", d->m_name, response.toString().constData()) ); 00121 } 00122 emitResult(); 00123 } else if ( d->serverCapability == Metadata && response.content[0].toString() == "+" ) { 00124 QByteArray content = d->entriesIt.value(); 00125 ++d->entriesIt; 00126 if (d->entriesIt == d->entries.constEnd()) { 00127 content += ')'; 00128 } else { 00129 content +=" {" + QByteArray::number(d->entriesIt.value().size()) + '}'; 00130 } 00131 // kDebug() << "SENT: " << content; 00132 d->sessionInternal()->sendData( content ); 00133 } 00134 } 00135 00136 void SetMetaDataJob::addMetaData(const QByteArray &name, const QByteArray &value) 00137 { 00138 Q_D(SetMetaDataJob); 00139 d->entries[name] = value; 00140 } 00141 00142 void SetMetaDataJob::setEntry(const QByteArray &entry) 00143 { 00144 Q_D(SetMetaDataJob); 00145 d->entryName = entry; 00146 } 00147 00148 SetMetaDataJob::MetaDataErrors SetMetaDataJob::metaDataErrors() const 00149 { 00150 Q_D(const SetMetaDataJob); 00151 return d->metaDataErrors; 00152 } 00153 00154 #include "setmetadatajob.moc"