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.
202 lines
7.1 KiB
202 lines
7.1 KiB
15 years ago
|
/***************************************************************************
|
||
|
* Copyright (C) 2005 by Spiros Georgaras *
|
||
|
* sngeorgaras@otenet.gr *
|
||
|
* *
|
||
|
* This program is free software; you can redistribute it and/or modify *
|
||
|
* it under the terms of the GNU General Public License as published by *
|
||
|
* the Free Software Foundation; either version 2 of the License, or *
|
||
|
* (at your option) any later version. *
|
||
|
* *
|
||
|
* This program is distributed in the hope that it will be useful, *
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||
|
* GNU General Public License for more details. *
|
||
|
* *
|
||
|
* You should have received a copy of the GNU General Public License *
|
||
|
* along with this program; if not, write to the *
|
||
|
* Free Software Foundation, Inc., *
|
||
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
|
||
|
***************************************************************************/
|
||
|
|
||
|
|
||
|
#include <config.h>
|
||
12 years ago
|
#include "tdefile_mhtml.h"
|
||
15 years ago
|
|
||
|
#include <kgenericfactory.h>
|
||
|
#include <kmdcodec.h>
|
||
15 years ago
|
#include <tqstring.h>
|
||
|
#include <tqcstring.h>
|
||
|
#include <tqfile.h>
|
||
13 years ago
|
#include <tqtextstream.h>
|
||
|
#include <tqtextcodec.h>
|
||
15 years ago
|
|
||
|
typedef KGenericFactory<mhtmlPlugin> mhtmlFactory;
|
||
|
|
||
12 years ago
|
K_EXPORT_COMPONENT_FACTORY(tdefile_mhtml, mhtmlFactory( "tdefile_mhtml" ))
|
||
15 years ago
|
|
||
14 years ago
|
mhtmlPlugin::mhtmlPlugin(TQObject *parent, const char *name,
|
||
15 years ago
|
const TQStringList &args)
|
||
14 years ago
|
: KFilePlugin(parent, name, args)
|
||
15 years ago
|
{
|
||
|
KFileMimeTypeInfo* info = addMimeTypeInfo( "application/x-mimearchive" );
|
||
|
KFileMimeTypeInfo::GroupInfo* group = 0L;
|
||
|
group = addGroupInfo(info, "mhtmlInfo", i18n("Document Information"));
|
||
|
KFileMimeTypeInfo::ItemInfo* item;
|
||
15 years ago
|
item = addItemInfo(group, "Subject", i18n("Subject"), TQVariant::String);
|
||
|
item = addItemInfo(group, "Sender", i18n("Sender"), TQVariant::String);
|
||
|
item = addItemInfo(group, "Recipient", i18n("Recipient"), TQVariant::String);
|
||
|
item = addItemInfo(group, "CopyTo", i18n("CC"), TQVariant::String);
|
||
|
item = addItemInfo(group, "BlindCopyTo", i18n("BCC"), TQVariant::String);
|
||
|
item = addItemInfo(group, "Date", i18n("Date"), TQVariant::String);
|
||
15 years ago
|
}
|
||
|
|
||
|
bool mhtmlPlugin::readInfo( KFileMetaInfo& info, uint /*what*/)
|
||
|
{
|
||
15 years ago
|
TQString mSender;
|
||
|
TQString mRecipient;
|
||
|
TQString mCopyTo;
|
||
|
TQString mBlindCopyTo;
|
||
|
TQString mSubject;
|
||
|
TQString mDate;
|
||
15 years ago
|
bool canUnfold;
|
||
|
if ( info.path().isEmpty() ) // remote file
|
||
|
return false;
|
||
|
|
||
15 years ago
|
TQFile f(info.path());
|
||
15 years ago
|
if (!f.open(IO_ReadOnly)) return false;
|
||
15 years ago
|
TQTextStream stream(&f);
|
||
|
TQString l=stream.readLine();
|
||
15 years ago
|
int nFieldsFound = 0;
|
||
|
while(!l.isEmpty()){
|
||
|
if(l.startsWith("From: ")) {
|
||
|
mSender=l.mid(6);
|
||
|
nFieldsFound |= 1;
|
||
|
canUnfold=TRUE;
|
||
|
} else if(l.startsWith("To: ")) {
|
||
|
mRecipient=l.mid(4);
|
||
|
nFieldsFound |= 2;
|
||
|
canUnfold=TRUE;
|
||
|
} else if(l.startsWith("Subject: ")) {
|
||
|
mSubject=l.mid(9);
|
||
|
nFieldsFound |= 4;
|
||
|
canUnfold=TRUE;
|
||
|
} else if(l.startsWith("Cc: ")) {
|
||
|
mCopyTo=l.mid(4);
|
||
|
nFieldsFound |= 8;
|
||
|
canUnfold=TRUE;
|
||
|
} else if(l.startsWith("Bcc: ")) {
|
||
|
mBlindCopyTo=l.mid(5);
|
||
|
nFieldsFound |= 16;
|
||
|
canUnfold=TRUE;
|
||
|
} else if(l.startsWith("Date: ")) {
|
||
|
mDate=l.mid(6);
|
||
|
nFieldsFound |= 32;
|
||
|
canUnfold=FALSE;
|
||
|
}else if(l.startsWith(" ") || l.startsWith("\t")){
|
||
|
// unfold field
|
||
|
if(canUnfold){
|
||
15 years ago
|
TQString tmp=l.stripWhiteSpace();
|
||
15 years ago
|
if(nFieldsFound & 16) mBlindCopyTo=mBlindCopyTo+" "+tmp;
|
||
|
else if(nFieldsFound & 8) mCopyTo=mCopyTo+" "+tmp;
|
||
|
else if(nFieldsFound & 4) mSubject=mSubject+" "+tmp;
|
||
|
else if(nFieldsFound & 2) mRecipient=mRecipient+" "+tmp;
|
||
|
else if(nFieldsFound & 1) mSender=mSender+" "+tmp;
|
||
|
}
|
||
|
}else canUnfold=FALSE;
|
||
|
// break out of the loop once the six fields have been found
|
||
|
if ( nFieldsFound == 32+16+8+4+2+1 )
|
||
|
break;
|
||
|
l=stream.readLine();
|
||
|
}
|
||
|
f.close();
|
||
|
KFileMetaInfoGroup group = appendGroup(info, "mhtmlInfo");
|
||
|
appendItem(group, "Subject", decodeRFC2047Phrase(mSubject,FALSE));
|
||
|
appendItem(group, "Sender", decodeRFC2047Phrase(mSender));
|
||
|
appendItem(group, "Recipient", decodeRFC2047Phrase(mRecipient));
|
||
|
appendItem(group, "CopyTo", decodeRFC2047Phrase(mCopyTo));
|
||
|
appendItem(group, "BlindCopyTo", decodeRFC2047Phrase(mBlindCopyTo));
|
||
|
appendItem(group, "Date", mDate);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString mhtmlPlugin::decodeRFC2047Phrase(const TQString &msg, bool removeLessGreater){
|
||
14 years ago
|
int st=msg.find("=?");
|
||
15 years ago
|
int en=-1;
|
||
15 years ago
|
TQString msgCopy=msg;
|
||
|
TQString decodedText=msgCopy.left(st);
|
||
|
TQString encodedText=msgCopy.mid(st);
|
||
14 years ago
|
st=encodedText.find("=?");
|
||
15 years ago
|
while(st!=-1){
|
||
14 years ago
|
en=encodedText.find("?=");
|
||
|
while(encodedText.mid(en+2,1)!=" " && en+2<(int)encodedText.length()) en=encodedText.find("?=",en+1);
|
||
15 years ago
|
if(en==-1) break;
|
||
|
decodedText+=encodedText.left(st);
|
||
15 years ago
|
TQString tmp=encodedText.mid(st,en-st+2);
|
||
15 years ago
|
encodedText=encodedText.mid(en+2);
|
||
|
decodedText+=decodeRFC2047String(tmp);
|
||
14 years ago
|
st=encodedText.find("=?",st+1);
|
||
15 years ago
|
}
|
||
|
decodedText += encodedText;
|
||
|
// remove unwanted '<' and '>'
|
||
|
if(removeLessGreater){
|
||
|
if(decodedText.stripWhiteSpace().startsWith("<") && decodedText.stripWhiteSpace().endsWith(">")){
|
||
15 years ago
|
TQString tmp=decodedText.stripWhiteSpace();
|
||
15 years ago
|
tmp=tmp.mid(1,tmp.length()-2);
|
||
|
decodedText=tmp;
|
||
|
}else{
|
||
15 years ago
|
TQString dec=decodedText;
|
||
|
TQString tmp;
|
||
15 years ago
|
|
||
14 years ago
|
st=decodedText.find("<");
|
||
15 years ago
|
while(st!=-1){
|
||
14 years ago
|
st=dec.find("<",st);
|
||
15 years ago
|
if(st==0 || (st!=0 && (dec.mid(st-2,2)==", "))){
|
||
14 years ago
|
en=dec.find(">",st);
|
||
|
if(en==-1 && dec.find(",",st)<en){
|
||
15 years ago
|
st++;
|
||
|
continue;
|
||
|
}
|
||
|
dec=dec.left(st)+dec.mid(st+1,en-st-1)+dec.mid(en+1);
|
||
|
}else if(st!=-1) st++;
|
||
|
}
|
||
|
decodedText=dec;
|
||
|
}
|
||
|
}
|
||
|
return decodedText;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString mhtmlPlugin::decodeRFC2047String(const TQString &msg){
|
||
|
TQString charset;
|
||
|
TQString encoding;
|
||
|
TQString notEncodedText;
|
||
|
TQString encodedText;
|
||
|
TQString decodedText;
|
||
15 years ago
|
int encEnd=0;
|
||
14 years ago
|
if(msg.startsWith("=?") && (encEnd=msg.findRev("?="))!=-1){
|
||
15 years ago
|
notEncodedText=msg.mid(encEnd+2);
|
||
|
encodedText=msg.left(encEnd);
|
||
|
encodedText=encodedText.mid(2,encodedText.length()-2);
|
||
14 years ago
|
int questionMark=encodedText.find('?');
|
||
15 years ago
|
if(questionMark==-1) return msg;
|
||
|
charset=encodedText.left(questionMark).lower();
|
||
|
encoding=encodedText.mid(questionMark+1,1).lower();
|
||
|
if(encoding!="b" && encoding!="q") return msg;
|
||
|
encodedText=encodedText.mid(questionMark+3);
|
||
14 years ago
|
if(charset.find(" ")!=-1 && encodedText.find(" ")!=-1) return msg;
|
||
15 years ago
|
TQCString tmpIn;
|
||
|
TQCString tmpOut;
|
||
15 years ago
|
tmpIn = encodedText.local8Bit();
|
||
|
if(encoding=="q")tmpOut=KCodecs::quotedPrintableDecode(tmpIn);
|
||
|
else tmpOut=KCodecs::base64Decode(tmpIn);
|
||
|
if(charset!="us-ascii"){
|
||
15 years ago
|
TQTextCodec *codec = TQTextCodec::codecForName(charset.local8Bit());
|
||
15 years ago
|
if(!codec) return msg;
|
||
|
decodedText=codec->toUnicode(tmpOut);
|
||
14 years ago
|
decodedText=decodedText.replace("_"," ");
|
||
|
}else decodedText=tmpOut.replace("_"," ");
|
||
15 years ago
|
return decodedText + notEncodedText;
|
||
|
}else return msg;
|
||
|
}
|
||
12 years ago
|
#include "tdefile_mhtml.moc"
|
||
15 years ago
|
|