mmsclient.cpp

来自「Qtopia下的邮件处理程序」· C++ 代码 · 共 661 行 · 第 1/2 页

CPP
661 行
字号
/******************************************************************************** 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 "mmsclient.h"#include "mmscomms.h"#ifdef MMSCOMMS_HTTP# include "mmscomms_http.h"#endif#include "maillist.h"#include <qtopia/wap/mmsmessage.h>#include <QHttp>#include <QUrl>#include <QDebug>#include <qtopia/qdrmcontentagent.h>#include "drmattachmentlink.h"#ifdef QTOPIA_DESKTOP#include <qcopenvelope_qd.h>#else#include <qtopia/qtopiaapplication.h>#include <qtopia/qcopenvelope.h>#endifQString getPath(const QString& fn, bool isdir=false);int MmsClient::txnId = 1;MmsClient::MmsClient()    : account(0), comms(0), mailList(0), quitRecv(false){}MmsClient::~MmsClient(){}void MmsClient::setAccount(MailAccount *_account){    if (account == _account)	return;    if (comms && comms->isActive()) {	qWarning("MMS connection is already use.");	return;    }    qDebug("MMS client account changed");    account = _account;    delete comms;#ifdef MMSCOMMS_HTTP    qDebug( "MMSCommsHttp" );    comms = new MmsCommsHttp(account, this);#endif    connect(comms, SIGNAL(notificationInd(const MMSMessage&)),	    this, SLOT(notificationInd(const MMSMessage&)));    connect(comms, SIGNAL(deliveryInd(const MMSMessage&)),	    this, SLOT(deliveryInd(const MMSMessage&)));    connect(comms, SIGNAL(sendConf(const MMSMessage&)),	    this, SLOT(sendConf(const MMSMessage&)));    connect(comms, SIGNAL(retrieveConf(const MMSMessage&,int)),	    this, SLOT(retrieveConf(const MMSMessage&,int)));    connect(comms, SIGNAL(statusChange(const QString&)),	    this, SLOT(statusChange(const QString&)));    connect(comms, SIGNAL(error(int,const QString&)),	    this, SLOT(commsError(int,const QString&)));    connect(comms, SIGNAL(transferSize(int)),	    this, SLOT(transferSize(int)));    connect(comms, SIGNAL(transfersComplete()),	    this, SLOT(transfersComplete()));}void MmsClient::newConnection(){    qDebug("MmsClient::newConnection");    messagesSent = 0;    messagesRecv = 0;    sendNextMessage();    quitRecv = false;}void MmsClient::quit(){    qDebug("MmsClient::quit()");    if (comms && comms->isActive()) {	quitRecv = true;    } else {	emit mailTransferred(0);    }}void MmsClient::addMail(const MailMessage &mail){    MMSMessage mms = convertToMms(mail);    QSettings conf(networkConfig(), QSettings::IniFormat);    conf.beginGroup("MMS");    QString visibility = conf.value("Visibility").toString();    if (visibility == "show")	mms.addField("X-Mms-Sender-Visibility", "Show");    else if (visibility == "hidden")	mms.addField("X-Mms-Sender-Visibility", "Hide");    int exp = conf.value("Expiry", 0).toInt();    if (exp)	mms.addField("X-Mms-Expiry", exp*3600);    outgoing.append(mms);}void MmsClient::sendNextMessage(){    if (!outgoing.count())	return;    qDebug("Sending MMS message");    MMSMessage &msg = outgoing.first();    comms->sendMessage(msg);}void MmsClient::sendConf(const MMSMessage &msg){    const WspField *field = msg.field("X-Mms-Transaction-Id");    qDebug() << "Recieved send conf, txn id" << (field ? field->value : QLatin1String("unknown"));    field = msg.field("X-Mms-Response-Status");    if (field && field->value != "Ok") {	// ### handle send error	errorHandling(0, field->value);    } else {	messagesSent++;	outgoing.removeFirst();	if (outgoing.count()) {	    sendNextMessage();	} else {	    emit mailSent(messagesSent);	    emit mailSent(-1);	}    }}void MmsClient::notificationInd(const MMSMessage &mmsMsg){    QString details;    const WspField *field = mmsMsg.field("X-Mms-Content-Location");    if (!field) {	qWarning("Invalid m-notification-ind");	return;    }    QString location = field->value;    int size = 0;    field = mmsMsg.field("X-Mms-Message-Size");    if (field)	size = field->value.toInt();    const QList<WspField> &headers = mmsMsg.headers();    QList<WspField>::ConstIterator it;    for (it = headers.begin(); it != headers.end(); ++it) {	details += (*it).name + ": " + (*it).value + "\n";    }    MailMessage mail;    mail.fromRFC822(details);    mail.setFrom(decodeRecipient(mail.from()));        // fix phone numbers    mail.setType(MailMessage::MMS);    mail.setSize(size);    mail.setDateTime(QDateTime::currentDateTime());    mail.setServerUid(location);    internalId = QUuid();    mail.setUuid(internalId);    mail.setStatus(EFlag_Downloaded, false);    mail.setStatus(EFlag_Incoming, true);    mail.setFromAccount(account->id());    mail.setFromMailbox("");    emit newMessage(mail);}void MmsClient::sendNotifyResp(const MailMessage &notification, const QString &status){    qDebug("MmsClient::sendNotifyResp");    MMSMessage resp;    resp.setType(MMSMessage::MNotifyResp);    resp.addField("X-Mms-MMS-Version", "1.0");    QString field = notification.header("X-Mms-Transaction-Id");    resp.addField("X-Mms-Transaction-Id", field.mid(field.indexOf(':')+1));    resp.addField("X-Mms-Status", status);    QSettings conf(networkConfig(), QSettings::IniFormat);    conf.beginGroup("MMS");    QString delRpt = conf.value("AllowDeliveryReport", "n").toString();    if (delRpt != QLatin1String("y"))	resp.addField("X-Mms-Report-Allowed", "No");    comms->sendMessage(resp);}void MmsClient::resetNewMailCount(){    QSettings mailconf("Trolltech", "qtmail");    mailconf.beginGroup("MMS");    int count = mailconf.value("newMmsCount").toInt();    if (count) {        count = 0;        mailconf.setValue("newMmsCount", count);        QCopEnvelope e("QPE/System", "newMmsCount(int)");        e << count;    }}void MmsClient::setSelectedMails(MailList *list, bool connected){    Q_UNUSED(connected);    qDebug("MmsClient::setSelectedMails");    mailList = list;    messagesRecv = 0;    getNextMessage();}void MmsClient::getNextMessage(){    if (!mailList)	return;    QString *msg;    //messagesRecv is reset when doing a read-reply    //so just get next message/*     if (!messagesRecv) 	msg = mailList->first();    else */	msg = mailList->next();    if (msg) {	internalId = mailList->currentId();	qDebug() << "Get msg" << msg;	QUrl url(*msg);	comms->retrieveMessage(url);    } else {	emit mailTransferred(messagesRecv);	mailList = 0;    }}void MmsClient::retrieveConf(const MMSMessage &msg, int size){    qDebug("Request finished");    messagesRecv++;    sendAcknowledge(msg);    Email mail = convertToEmail(msg, size);    emit newMessage(mail);    getNextMessage();}void MmsClient::sendAcknowledge(const MMSMessage &retr){    qDebug("MmsClient::sendAcknowledge");    MMSMessage resp;    resp.setType(MMSMessage::MAckowledgeInd);    resp.addField("X-Mms-MMS-Version", "1.0");    const WspField *field = retr.field("X-Mms-Transaction-Id");    if (field)	resp.addField("X-Mms-Transaction-Id", field->value);    QSettings conf(networkConfig(), QSettings::IniFormat);    conf.beginGroup("MMS");    QString delRpt = conf.value("AllowDeliveryReport", "n").toString();    if (delRpt != QLatin1String("y"))	resp.addField("X-Mms-Report-Allowed", "No");    comms->sendMessage(resp);}void MmsClient::deliveryInd(const MMSMessage &mmsMsg){    QString details;    const QList<WspField> &headers = mmsMsg.headers();    QList<WspField>::ConstIterator it;    for (it = headers.begin(); it != headers.end(); ++it) {	details += (*it).name + ": " + (*it).value + "\n";    }    MailMessage mail;    mail.fromRFC822(details);    internalId = QUuid();    mail.setUuid(internalId);    mail.setStatus(EFlag_Downloaded, true);    mail.setStatus(EFlag_Incoming, true);    mail.setFromAccount(account->id());    mail.setFromMailbox("");    emit newMessage(mail);}void MmsClient::transferSize(int done){    emit transferredSize(done);}void MmsClient::statusChange(const QString &status){    emit updateStatus(status);}void MmsClient::commsError(int code, const QString &str){    QString tmp(str);    emit updateStatus(tr("Error occurred"));    emit errorOccurred(code, tmp);}void MmsClient::transfersComplete(){    qDebug("All transfers complete");    if (quitRecv) {	emit mailTransferred(0);	quitRecv = false;    }}void MmsClient::errorHandling(int code, QString msg){

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?