00001 #include "abstractmail.h"
00002 #include "imapwrapper.h"
00003 #include "pop3wrapper.h"
00004 #include "nntpwrapper.h"
00005 #include "mhwrapper.h"
00006 #include "mailtypes.h"
00007
00008 #include <opie2/odebug.h>
00009
00010 #include <qfile.h>
00011 #include <qtextstream.h>
00012 #include <stdlib.h>
00013 #include <libetpan/mailmime_content.h>
00014 #include <libetpan/mailmime.h>
00015
00016 using namespace Opie::Core;
00017 AbstractMail* AbstractMail::getWrapper(IMAPaccount *a)
00018 {
00019 return new IMAPwrapper(a);
00020 }
00021
00022 AbstractMail* AbstractMail::getWrapper(POP3account *a)
00023 {
00024 return new POP3wrapper(a);
00025 }
00026
00027 AbstractMail* AbstractMail::getWrapper(NNTPaccount *a)
00028 {
00029 return new NNTPwrapper(a);
00030 }
00031
00032 AbstractMail* AbstractMail::getWrapper(const QString&a,const QString&name)
00033 {
00034 return new MHwrapper(a,name);
00035 }
00036
00037 AbstractMail* AbstractMail::getWrapper(Account*a)
00038 {
00039 if (!a) return 0;
00040 switch (a->getType()) {
00041 case MAILLIB::A_IMAP:
00042 return new IMAPwrapper((IMAPaccount*)a);
00043 break;
00044 case MAILLIB::A_POP3:
00045 return new POP3wrapper((POP3account*)a);
00046 break;
00047 case MAILLIB::A_NNTP:
00048 return new NNTPwrapper((NNTPaccount*)a);
00049 break;
00050 default:
00051 return 0;
00052 }
00053 }
00054
00055 encodedString* AbstractMail::decode_String(const encodedString*text,const QString&enc)
00056 {
00057 odebug << "Decode string start" << oendl;
00058 char*result_text;
00059 size_t index = 0;
00060
00061 size_t target_length = 0;
00062 result_text = 0;
00063 int mimetype = MAILMIME_MECHANISM_7BIT;
00064 if (enc.lower()=="quoted-printable") {
00065 mimetype = MAILMIME_MECHANISM_QUOTED_PRINTABLE;
00066 } else if (enc.lower()=="base64") {
00067 mimetype = MAILMIME_MECHANISM_BASE64;
00068 } else if (enc.lower()=="8bit") {
00069 mimetype = MAILMIME_MECHANISM_8BIT;
00070 } else if (enc.lower()=="binary") {
00071 mimetype = MAILMIME_MECHANISM_BINARY;
00072 }
00073
00074 int err = mailmime_part_parse(text->Content(),text->Length(),&index,mimetype,
00075 &result_text,&target_length);
00076
00077 encodedString* result = new encodedString();
00078 if (err == MAILIMF_NO_ERROR) {
00079 result->setContent(result_text,target_length);
00080 }
00081 odebug << "Decode string finished" << oendl;
00082 return result;
00083 }
00084
00085 QString AbstractMail::convert_String(const char*text)
00086 {
00087 size_t index = 0;
00088 char*res = 0;
00089 int err = MAILIMF_NO_ERROR;
00090
00091 if (!text) {
00092 return QString("");
00093 }
00094 QString result(text);
00095
00096 err = mailmime_encoded_phrase_parse("iso-8859-1",
00097 text, strlen(text),&index, "iso-8859-1",&res);
00098
00099 if (err == MAILIMF_NO_ERROR && res && strlen(res)) {
00100 result = QString(res);
00101
00102 }
00103 if (res) free(res);
00104 return result;
00105 }
00106
00107
00108 QString AbstractMail::gen_attachment_id()
00109 {
00110 QFile file( "/proc/sys/kernel/random/uuid" );
00111 if (!file.open(IO_ReadOnly ) )
00112 return QString::null;
00113
00114 QTextStream stream(&file);
00115
00116 return "{" + stream.read().stripWhiteSpace() + "}";
00117 }
00118
00119 int AbstractMail::createMbox(const QString&,const FolderP&,const QString& ,bool)
00120 {
00121 return 0;
00122 }
00123
00124 QString AbstractMail::defaultLocalfolder()
00125 {
00126 QString f = getenv( "HOME" );
00127 f += "/Applications/opiemail/localmail";
00128 return f;
00129 }
00130
00131 QString AbstractMail::draftFolder()
00132 {
00133 return QString("Drafts");
00134 }
00135
00136
00137 void AbstractMail::deleteMails(const QString &,const QValueList<Opie::Core::OSmartPointer<RecMail> > &)
00138 {
00139 }
00140
00141 void AbstractMail::mvcpAllMails(const FolderP&fromFolder,
00142 const QString&targetFolder,AbstractMail*targetWrapper,bool moveit)
00143 {
00144 QValueList<RecMailP> t;
00145 listMessages(fromFolder->getName(),t);
00146 encodedString*st = 0;
00147 while (t.count()>0) {
00148 RecMailP r = (*t.begin());
00149 st = fetchRawBody(r);
00150 if (st) {
00151 targetWrapper->storeMessage(st->Content(),st->Length(),targetFolder);
00152 delete st;
00153 }
00154 t.remove(t.begin());
00155 }
00156 if (moveit) {
00157 deleteAllMail(fromFolder);
00158 }
00159 }
00160
00161 void AbstractMail::mvcpMail(const RecMailP&mail,const QString&targetFolder,AbstractMail*targetWrapper,bool moveit)
00162 {
00163 encodedString*st = 0;
00164 st = fetchRawBody(mail);
00165 if (st) {
00166 targetWrapper->storeMessage(st->Content(),st->Length(),targetFolder);
00167 delete st;
00168 }
00169 if (moveit) {
00170 deleteMail(mail);
00171 }
00172 }