📄 emailhandler.cpp
字号:
/******************************************************************************** Copyright (C) 2000-2006 TROLLTECH ASA. All rights reserved.**** This file is part of the Phone Edition of the Qtopia Toolkit.**** Licensees holding a valid license agreement from Trolltech or any of its** authorized distributors may use this file in accordance with** the License Agreement provided with the Licensed Software.**** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for** information about Trolltech's Commercial License Agreements.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.********** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "emailhandler.h"#ifndef QTOPIA_NO_MMS#include "mmsclient.h"#endif#include <qtopia/qmimetype.h>#include <QFileInfo>#include <QApplication>#include <QMessageBox>#include <QString>#include <QDebug>#include <stdlib.h>// just grabbed from example mail program, rememeber to test...static void replace( QString& str, const QString& before, const QString& after ){ if ( before.length() == 0 ) return; int maxsize; if ( before.length() > after.length() ) maxsize = str.length(); else maxsize = ( str.length() / before.length() ) * after.length(); QChar *buf = new QChar[maxsize + 1]; // + 1 in case maxsize is 0 const QChar *strBuf = str.unicode(); int prev = 0; int cur = 0; int i = 0; bool changed = false; // indexOf may be -1 while ( (uint) (cur = str.indexOf(before, prev)) < (uint) str.length() ) { if ( cur > prev ) { memcpy( buf + i, strBuf + prev, sizeof(QChar) * (cur - prev) ); i += cur - prev; } if ( after.length() > 0 ) { memcpy( buf + i, after.unicode(), sizeof(QChar) * after.length() ); i += after.length(); } prev = cur + before.length(); changed = true; } if ( changed ) { memcpy( buf + i, strBuf + prev, sizeof(QChar) * (str.length() - prev) ); i += str.length() - prev; str = QString( buf, i ); } delete[] buf;}EmailHandler::EmailHandler(){ mailAccount = 0; smtpAccount = 0; smsAccount = 0; mmsAccount = 0; smtpClient = new SmtpClient();#ifdef QTOPIA_PHONE smsClient = new SmsClient();#ifndef QTOPIA_NO_MMS mmsClient = new MmsClient();#endif#endif popClient = new PopClient(); imapClient = new ImapClient(); connectClient(smtpClient, Sending, SIGNAL(smtpError(int,QString&)));#ifdef QTOPIA_PHONE connectClient(smsClient, Sending, SIGNAL(smsError(int,QString&))); connect(smsClient, SIGNAL(newMessage(const QString&,const QString&,const QUuid&,uint,bool)), this, SLOT(messageArrived(const QString&,const QString&,const QUuid&,uint,bool)) );#ifndef QTOPIA_NO_MMS connectClient(mmsClient, Sending|Receiving, SIGNAL(mmsError(int,QString&))); connect(mmsClient, SIGNAL(newMessage(Email)), this, SLOT(messageArrived(Email)) ); connect(mmsClient, SIGNAL(newMessage(const QString&,const QString&,const QUuid&,uint,bool)), this, SLOT(messageArrived(const QString&,const QString&,const QUuid&,uint,bool)) );#endif#endif connectClient(popClient, Receiving, SIGNAL(popError(int,QString&))); connect(popClient, SIGNAL(newMessage(const QString&,const QString&,const QUuid&,uint,bool)), this, SLOT(messageArrived(const QString&,const QString&,const QUuid&,uint,bool)) ); connectClient(imapClient, Receiving, SIGNAL(popError(int,QString&))); connect(imapClient, SIGNAL(newMessage(const QString&,const QString&,const QUuid&,uint,bool,const QString&,uint)), this, SLOT(messageArrived(const QString&,const QString&,const QUuid&,uint,bool,const QString&,uint)) ); receiving = false;}void EmailHandler::connectClient(Client *client, int type, QString sigName){ connect(client, SIGNAL(errorOccurred(int,QString&)),sigName.toAscii() ); connect(client, SIGNAL(mailSent(int)), this, SIGNAL(mailSent(int)) ); if (type & Receiving) connect(client, SIGNAL(updateStatus(const QString&)), this, SIGNAL(updatePopStatus(const QString&)) ); if (type & Sending) connect(client, SIGNAL(updateStatus(const QString&)), this, SIGNAL(updateSendingStatus(const QString&)) ); connect(client, SIGNAL(mailTransferred(int)), this, SIGNAL(mailTransferred(int)) ); connect(client, SIGNAL( unresolvedUidlList(QStringList&) ), this, SLOT( unresolvedUidl(QStringList&) ) ); connect(client, SIGNAL(serverFolders()), this, SIGNAL(serverFolders()) ); //relaying size information connect(client, SIGNAL(downloadedSize(int)), this, SIGNAL(downloadedSize(int)) ); connect(client, SIGNAL(mailboxSize(int)), this, SIGNAL(mailboxSize(int)) ); connect(client, SIGNAL(transferredSize(int)), this, SIGNAL(transferredSize(int)) ); connect(client, SIGNAL( failedList(QStringList&) ), this, SIGNAL( failedList(QStringList&) ) );}void EmailHandler::sendMail(QList<Email*>* mailList){ Email *currentMail; QString temp; QString userName; if (smtpAccount) userName = "<" + smtpAccount->emailAddress() + ">"; QStringList combinedList; bool allOk = (mailList->count() > 0);#ifdef QTOPIA_PHONE bool smsOk = false;#ifndef QTOPIA_NO_MMS bool mmsOk = false;#endif#endif bool smtpOk = false; int allMailSize = 0; for (int i = 0; i < mailList->count(); i++) { currentMail = mailList->at(i); currentMail->readFromFile(); if ( currentMail->encodeMail() ) { QStringList mailTo = currentMail->to(), mailCc = currentMail->cc(), mailBcc = currentMail->bcc(); if ( !currentMail->hasRecipients() ) { temp = tr("No recipients specified for\n mail with subject:\n" "%1\nNO mail has been sent.") .arg( currentMail->subject() ); QMessageBox::warning(qApp->activeWindow(), tr("Mail encoding error"), temp, tr("OK")); allOk = false; } else {#ifdef QTOPIA_PHONE#ifndef QTOPIA_NO_MMS QString tmp = currentMail->header("X-Mms-Message-Type"); qDebug() << ">>>>>>>>>>" << tmp; if (currentMail->header("X-Mms-Message-Type").contains("m-send-req")) { qDebug("Detected MMS message"); if (allOk) mmsOk = true; mmsClient->addMail(*currentMail); continue; }#endif // TODO : EMS // find any valid sms numbers in the mail QStringList smsList = smsClient->separateSmsAddresses( mailTo ); smsList += smsClient->separateSmsAddresses( mailCc ); smsList += smsClient->separateSmsAddresses( mailBcc ); QString mPlainBody = smsClient->formatOutgoing ( currentMail->subject(), currentMail->plainTextBody() ); for (QStringList::Iterator it = smsList.begin(); it != smsList.end() && allOk; ++it) { if (smsClient->smsAddress( *it )) { if (!smsClient->validSmsAddress( *it )) { temp = "<qt>" + tr("Invalid sms recipient specified for\n " "mail with subject:\n%1\n" "NO mail has been sent.") .arg( currentMail->subject() ) + "</qt>"; QMessageBox::warning(qApp->activeWindow(), tr("Mail encoding error"), temp, tr("OK")); allOk = false; smsOk = false; } else { if (allOk) smsOk = true; smsClient->addMail( *it, mPlainBody ); allMailSize += mPlainBody.length(); continue; } } }// if( smsList.count() )// {// currentMail->setTo( mailTo );// currentMail->setCc( mailCc );// currentMail->setBcc( mailBcc );// } if (!allOk) continue;#endif if(mailBcc.count() > 0) { currentMail->setBcc(QStringList()); currentMail->encodeMail(); } combinedList = mailTo; combinedList += mailCc; combinedList += mailBcc; if (combinedList.isEmpty()) continue; int pos, endPos; for (QStringList::Iterator it = combinedList.begin(); it != combinedList.end(); ++it) { if ( (pos = (*it).indexOf('<', 0 , Qt::CaseInsensitive)) != -1) { pos++; endPos = (*it).indexOf('>', pos, Qt::CaseInsensitive); *it = (*it).mid(pos, endPos - pos).trimmed(); } } if ( smtpAccount && smtpAccount->useSig() ) { currentMail->setPlainTextBody( currentMail->plainTextBody() + "\n" + smtpAccount->sig() ); // Need to re-encode after body text has been modified currentMail->encodeMail(); } QString mBody = currentMail->toRFC822(); allMailSize += mBody.length();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -