• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • kdenetwork
  • Sitemap
  • Contact Us
 

kopete/protocols/messenger/libpapillon

mimeheader.cpp

Go to the documentation of this file.
00001 /*
00002    mimeheader.cpp - Create/Manage a MIME header.
00003 
00004    Copyright (c) 2006 by Michaƫl Larouche <larouche@kde.org>
00005 
00006    *************************************************************************
00007    *                                                                       *
00008    * This library is free software; you can redistribute it and/or         *
00009    * modify it under the terms of the GNU Lesser General Public            *
00010    * License as published by the Free Software Foundation; either          *
00011    * version 2 of the License, or (at your option) any later version.      *
00012    *                                                                       *
00013    *************************************************************************
00014 */
00015 #include "Papillon/MimeHeader"
00016 
00017 // Qt includes
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 &copy)
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     // A MIME header is separated from its body with a empty field (\r\n),
00063     // so stop when we found an empty line.
00064     while( !line.isEmpty() )
00065     {
00066         int firstColonPosition = line.indexOf( QChar(':') );
00067         // This is a MIME line
00068         if( firstColonPosition != -1 )
00069         {
00070             QString key = line.left(firstColonPosition);
00071             QString value = line.mid(firstColonPosition+1).trimmed(); // Skip the space
00072 
00073             // Look for charset value.
00074             if( key.toLower() == QLatin1String("content-type") )
00075             {
00076                 // Only extract charset if present.
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     // Always put MIME-Version and Content-Type first
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         // Append charset to Content-Type field if needed
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 }

kopete/protocols/messenger/libpapillon

Skip menu "kopete/protocols/messenger/libpapillon"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdenetwork

Skip menu "kdenetwork"
  • kget
  • kopete
  •   kopete
  •   libkopete
  •       libpapillon
  • krfb
Generated for kdenetwork by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal