You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

312 lines
7.1 KiB

// kmmsgpart.cpp
#include <qdir.h>
#include "kmmsgpart.h"
#include "kmmessage.h"
#include "kmimemagic.h"
#include <kapp.h>
#include <kconfig.h>
#include <mimelib/enum.h>
#include <mimelib/body.h>
#include <mimelib/bodypart.h>
#include <mimelib/utility.h>
static KMimeMagic* sMagic = NULL;
//-----------------------------------------------------------------------------
KMMessagePart::KMMessagePart() :
mType("text"), mSubtype("plain"), mCte("7bit"), mContentDescription(),
mContentDisposition(), mBody(), mName()
{
}
//-----------------------------------------------------------------------------
KMMessagePart::~KMMessagePart()
{
}
//-----------------------------------------------------------------------------
void KMMessagePart::setEncodedBody(const QString aStr)
{
DwString dwResult, dwSrc;
QString result;
int encoding = contentTransferEncoding();
int len = aStr.size();
switch (encoding)
{
case DwMime::kCteQuotedPrintable:
dwSrc.resize(len);
memcpy((void*)dwSrc.data(), (void*)aStr.data(), len);
DwEncodeQuotedPrintable(dwSrc, dwResult);
result = dwResult.c_str();
result.detach();
break;
case DwMime::kCteBase64:
dwSrc.resize(len);
memcpy((void*)dwSrc.data(), (void*)aStr.data(), len);
DwEncodeBase64(dwSrc, dwResult);
result = dwResult.c_str();
result.detach();
break;
case DwMime::kCte7bit:
case DwMime::kCte8bit:
case DwMime::kCteBinary:
result = aStr;
break;
default:
debug("WARNING -- unknown encoding `%s'. Assuming 8bit.",
(const char*)cteStr());
result = aStr;
}
mBody = result;
}
//-----------------------------------------------------------------------------
const QString KMMessagePart::bodyDecoded(void) const
{
DwString dwResult, dwSrc;
QString result;
int encoding = contentTransferEncoding();
int len;
switch (encoding)
{
case DwMime::kCteQuotedPrintable:
dwSrc = mBody;
DwDecodeQuotedPrintable(dwSrc, dwResult);
result = dwResult.c_str();
result.detach();
break;
case DwMime::kCteBase64:
dwSrc = mBody;
DwDecodeBase64(dwSrc, dwResult);
len = dwResult.size();
result.resize(len+1);
memcpy((void*)result.data(), (void*)dwResult.c_str(), len);
break;
case DwMime::kCte7bit:
case DwMime::kCte8bit:
case DwMime::kCteBinary:
result = mBody;
break;
default:
debug("WARNING -- unknown encoding `%s'. Assuming 8bit.",
(const char*)cteStr());
result = mBody;
}
return result;
}
//-----------------------------------------------------------------------------
void KMMessagePart::magicSetType(bool aAutoDecode)
{
QString mimetype, bod;
int sep;
if (!sMagic)
{
// initialize mime magic
sMagic = new KMimeMagic(kapp->kdedir() + "/share/mimelnk/magic");
sMagic->setFollowLinks(TRUE);
}
if (aAutoDecode) bod = bodyDecoded();
else bod = mBody;
mimetype = sMagic->findBufferType(bod, bod.size()-1)->getContent();
sep = mimetype.find('/');
mType = mimetype.left(sep);
mSubtype = mimetype.mid(sep+1, 64);
}
//-----------------------------------------------------------------------------
const QString KMMessagePart::iconName(void) const
{
QString fileName, icon;
QDir dir;
fileName = KApplication::kdedir() + "/share/mimelnk/" + mType + "/" +
mSubtype + ".kdelnk";
if (dir.exists(fileName))
{
KConfig config(fileName);
config.setGroup("KDE Desktop Entry");
icon = config.readEntry("Icon");
if(icon.isEmpty()) // If no icon specified.
icon = "unknown.xpm";
}
else
{
// not found, use default
icon = "unknown.xpm";
}
return KApplication::kdedir() + "/share/icons/" + icon;
}
//-----------------------------------------------------------------------------
const QString KMMessagePart::typeStr(void) const
{
return mType;
}
//-----------------------------------------------------------------------------
int KMMessagePart::type(void) const
{
int type = DwTypeStrToEnum(DwString(mType));
return type;
}
//-----------------------------------------------------------------------------
void KMMessagePart::setTypeStr(const QString aStr)
{
mType = aStr.copy();
}
//-----------------------------------------------------------------------------
void KMMessagePart::setType(int aType)
{
DwString dwType;
DwTypeEnumToStr(aType, dwType);
mType = dwType.c_str();
mType.detach();
}
//-----------------------------------------------------------------------------
const QString KMMessagePart::subtypeStr(void) const
{
return mSubtype;
}
//-----------------------------------------------------------------------------
int KMMessagePart::subtype(void) const
{
int subtype = DwSubtypeStrToEnum(DwString(mSubtype));
return subtype;
}
//-----------------------------------------------------------------------------
void KMMessagePart::setSubtypeStr(const QString aStr)
{
mSubtype = aStr;
}
//-----------------------------------------------------------------------------
void KMMessagePart::setSubtype(int aSubtype)
{
DwString dwSubtype;
DwSubtypeEnumToStr(aSubtype, dwSubtype);
mSubtype = dwSubtype.c_str();
mSubtype.detach();
}
//-----------------------------------------------------------------------------
const QString KMMessagePart::contentTransferEncodingStr(void) const
{
return mCte;
}
//-----------------------------------------------------------------------------
int KMMessagePart::contentTransferEncoding(void) const
{
int cte = DwCteStrToEnum(DwString(mCte));
return cte;
}
//-----------------------------------------------------------------------------
void KMMessagePart::setContentTransferEncodingStr(const QString aStr)
{
mCte = aStr.copy();
}
//-----------------------------------------------------------------------------
void KMMessagePart::setContentTransferEncoding(int aCte)
{
DwString dwCte;
DwCteEnumToStr(aCte, dwCte);
mCte = dwCte.c_str();
mCte.detach();
}
//-----------------------------------------------------------------------------
const QString KMMessagePart::contentDescription(void) const
{
return mContentDescription;
}
//-----------------------------------------------------------------------------
void KMMessagePart::setContentDescription(const QString aStr)
{
mContentDescription = aStr.copy();
}
//-----------------------------------------------------------------------------
const QString KMMessagePart::contentDisposition(void) const
{
return mContentDisposition;
}
//-----------------------------------------------------------------------------
void KMMessagePart::setContentDisposition(const QString aStr)
{
mContentDisposition = aStr.copy();
}
//-----------------------------------------------------------------------------
const QString KMMessagePart::body(void) const
{
return mBody;
}
//-----------------------------------------------------------------------------
void KMMessagePart::setBody(const QString aStr)
{
mBody = aStr.copy();
}
//-----------------------------------------------------------------------------
const QString KMMessagePart::name(void) const
{
return mName;
}
//-----------------------------------------------------------------------------
void KMMessagePart::setName(const QString aStr)
{
mName = aStr.copy();
}