kopete/protocols/messenger/libpapillon
mimeheader.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "Papillon/MimeHeader"
00016
00017
00018 #include <QtCore/QSharedData>
00019 #include <QtCore/QHash>
00020 #include <QtCore/QLatin1String>
00021 #include <QtCore/QTextStream>
00022 #include <QtCore/QStringList>
00023
00024 namespace Papillon
00025 {
00026
00027 class MimeHeader::Private : public QSharedData
00028 {
00029 public:
00030 Private()
00031 {}
00032
00033 QHash<QString, QVariant> hashMimeHeader;
00034 QString charset;
00035 };
00036
00037 MimeHeader::MimeHeader()
00038 : d(new Private)
00039 {}
00040
00041 MimeHeader::~MimeHeader()
00042 {}
00043
00044 MimeHeader::MimeHeader(const MimeHeader ©)
00045 : d(copy.d)
00046 {}
00047
00048 MimeHeader &MimeHeader::operator=(const MimeHeader &other)
00049 {
00050 d = other.d;
00051 return *this;
00052 }
00053
00054 MimeHeader MimeHeader::parseMimeHeader(const QString &data)
00055 {
00056 MimeHeader parsedMimeHeader;
00057 QString line, temp;
00058 temp = data;
00059 QTextStream parser(&temp);
00060
00061 line = parser.readLine();
00062
00063
00064 while( !line.isEmpty() )
00065 {
00066 int firstColonPosition = line.indexOf( QChar(':') );
00067
00068 if( firstColonPosition != -1 )
00069 {
00070 QString key = line.left(firstColonPosition);
00071 QString value = line.mid(firstColonPosition+1).trimmed();
00072
00073
00074 if( key.toLower() == QLatin1String("content-type") )
00075 {
00076
00077 if( value.indexOf( QChar(';') ) != 1 )
00078 {
00079 QStringList args = value.split( QChar(';') );
00080 value = args[0];
00081 QString charset = args[1].section( QChar('='), 1, 1 );
00082 parsedMimeHeader.setCharset(charset);
00083 }
00084 }
00085 parsedMimeHeader.setValue(key, QVariant(value));
00086 }
00087 line = parser.readLine();
00088 }
00089
00090 return parsedMimeHeader;
00091 }
00092
00093
00094 bool MimeHeader::isValid() const
00095 {
00096 return d->hashMimeHeader.isEmpty();
00097 }
00098
00099 bool MimeHeader::hasKey(const QString &key) const
00100 {
00101 return d->hashMimeHeader.contains(key);
00102 }
00103
00104 QVariant MimeHeader::value(const QString &key) const
00105 {
00106 return d->hashMimeHeader.value(key);
00107 }
00108
00109 void MimeHeader::setValue(const QString &key, const QVariant &value)
00110 {
00111 d->hashMimeHeader.insert(key, value);
00112 }
00113
00114 QString MimeHeader::mimeVersion() const
00115 {
00116 return value( QLatin1String("MIME-Version") ).toString();
00117 }
00118 void MimeHeader::setMimeVersion(const QString &mimeVersion)
00119 {
00120 setValue( QLatin1String("MIME-Version"), mimeVersion );
00121 }
00122
00123 QString MimeHeader::contentType() const
00124 {
00125 return value( QLatin1String("Content-Type") ).toString();
00126 }
00127 void MimeHeader::setContentType(const QString &contentType)
00128 {
00129 setValue( QLatin1String("Content-Type"), contentType );
00130 }
00131
00132 QString MimeHeader::charset() const
00133 {
00134 return d->charset;
00135 }
00136 void MimeHeader::setCharset(const QString &charset)
00137 {
00138 d->charset = charset;
00139 }
00140
00141 QString MimeHeader::toString() const
00142 {
00143 QString result;
00144 QHash<QString, QVariant> generateHash = d->hashMimeHeader;
00145 QString currentKey = QLatin1String("MIME-Version");
00146
00147 if( hasKey(currentKey) )
00148 result += QString("%1: %2\r\n").arg( currentKey ).arg( generateHash.take(currentKey).toString() );
00149
00150 currentKey = QLatin1String("Content-Type");
00151 if( hasKey(currentKey) )
00152 {
00153 result += QString("%1: %2").arg( currentKey ).arg( generateHash.take(currentKey).toString() );
00154
00155 if( !charset().isEmpty() )
00156 result += QString("; charset=%1").arg( charset() );
00157 result += QLatin1String("\r\n");
00158 }
00159
00160 QHash<QString, QVariant>::ConstIterator it, itEnd = generateHash.constEnd();
00161 for( it = generateHash.constBegin(); it != itEnd; ++it )
00162 {
00163 QString temp;
00164 temp = QString("%1: %2\r\n").arg( it.key() ).arg( it.value().toString() );
00165
00166 result += temp;
00167 }
00168
00169 return result;
00170 }
00171
00172 }