⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 emailclient.cpp

📁 Qtopia下的邮件处理程序
💻 CPP
📖 第 1 页 / 共 5 页
字号:
MailAccount* EmailClient::smtpForMail(Email *mail){    mail->setReplyTo( "" );    /*  Let's see if we the emailAddress matches a SMTP account */    MailAccount *account = accountList->getSmtpRefByMail( mail->fromEmail() );    if ( account != NULL ) {	mail->setFromAccount( account->id() );	return account;    }    /*  Let's try using a default account instead */    account = accountList->defaultMailServer();    if ( account != NULL ) {	return account;    }    /* No default either.  Try any and setup a reply-to */    account = accountList->getSmtpRef();    if ( account != NULL ) {	mail->setReplyTo( mail->fromEmail() );	qWarning( QString( mail->fromEmail() + " used as reply-to" ).toLatin1() );	mail->setFromAccount( account->id() );	return account;    }    /*  No SMTP-account defined	*/    return NULL;}// send all messages in outbox, by looping through the outbox, sending// each message that belongs to the current found accountvoid EmailClient::sendAllQueuedMail(){    Email *mail;    bool needAccount = false;    QListIterator<Email*> it = mailboxList->mailbox( OutboxString )->entryIterator();        while ( it.hasNext() ) {	mail = it.next();	if ( mail->type() & MailMessage::Email )	    needAccount = true;    }    if ( needAccount && !verifyAccounts(true) )    {	qDebug("Queued mail requires valid email accounts but none available.");	moveOutboxMailsToDrafts();	return;    }    QList<Email*> queuedMessages;    queuedUuids.clear();    smtpAccount = NULL;    it = mailboxList->mailbox(OutboxString)->entryIterator();    while ( it.hasNext() ) {	mail = it.next();	//mail not previously sent, and not unfinished, add to queue		if ( !mail->status(EFlag_Sent) && !mail->unfinished() && (mail->hasRecipients()) ) {	    /* The first mail determines which range of mails to first	       send.  As we allow use of several smtp accounts we may	       need more than one connection, but the total number of connections	       needed will never exceed the number of smtp accounts	    */	    if ( smtpAccount == NULL ) {		smtpAccount = smtpForMail( mail );		queuedMessages.append(mail);		queuedUuids.append( mail->uuid() );	    } else if ( smtpForMail(mail) == smtpAccount ) {		queuedMessages.append(mail);		queuedUuids.append( mail->uuid() );	    }	}    }    if (queuedMessages.count() > 0) {	emailHandler->setSmtpAccount(smtpAccount);	sending = true;// 	sendMailButton->setVisible(false);	cancelButton->setVisible(true);	if (!receiving)	    queueStatus = 3;	sendSingle = false;	isSending(true);	emailHandler->sendMail(&queuedMessages);    } else {	qWarning("no more messages to send");    }}void EmailClient::sendSingleMail(Email *mail){    if (sending) {	qWarning("sending in progress, no action performed");	return;    }    bool needAccount = false;    if ( mail->type() & MailMessage::Email )	needAccount = true;    if ( needAccount && !verifyAccounts(true) ) {	qDebug("Mail requires valid email accounts but none available.");	moveOutboxMailsToDrafts();	return;    }    QList<Email*> queuedMessages;    queuedUuids.clear();    smtpAccount = smtpForMail( mail );    queuedMessages.append(mail);    queuedUuids.append( mail->uuid() );    emailHandler->setSmtpAccount(smtpAccount);    sending = true;//     sendMailButton->setVisible(false);    cancelButton->setVisible(true);    if (!receiving)    	queueStatus = 3;    sendSingle = true;    isSending(true);    emailHandler->sendMail(&queuedMessages);}bool EmailClient::verifyAccounts(bool outgoing){    bool ok = true;	    if (accountList->count() == 0) { 	QMessageBox box(tr( "No account selected" ), tr("<qt>You must create an account</qt>"), QMessageBox::Warning,			QMessageBox::Ok | QMessageBox::Default , QMessageBox::NoButton, QMessageBox::NoButton );	box.exec();	ok = false;    } else if (outgoing && accountList->getSmtpRef() == NULL) {#ifdef QTOPIA_PHONE 	QMessageBox box(tr("No SMTP Server"), tr("<qt>No valid SMTP server defined.<br><br>No emails could be sent.</qt>"), QMessageBox::Warning,			QMessageBox::Ok | QMessageBox::Default, QMessageBox::NoButton, QMessageBox::NoButton,			qApp->activeWindow() );#else	 	QMessageBox box(tr("No SMTP Server"), tr("<qt>No valid SMTP server defined</qt>"), QMessageBox::Warning,			QMessageBox::Ok | QMessageBox::Default, QMessageBox::NoButton, QMessageBox::NoButton );#endif	box.exec();	ok = false;    } else if ( !outgoing && mailAccount == NULL ) { 	QMessageBox box(tr("No POP or IMAP accounts defined"), tr("<qt>Get Mail only works with POP or IMAP</qt>"), QMessageBox::Warning,			QMessageBox::Ok | QMessageBox::Default, QMessageBox::NoButton, QMessageBox::NoButton );        ok = false;    }    return ok;}//some mail are obviously sent, but are all mail in the outbox sentvoid EmailClient::mailSent(int count){    Email *mail;	    if (count == -1) {	sending = false;// 	sendMailButton->setVisible(true);	cancelButton->setVisible(false);	isSending(false);	return;    }    EmailFolderList *mailbox = mailboxList->mailbox(OutboxString);        QListIterator<QUuid> qit( queuedUuids );    while ( qit.hasNext() ) {	mail = mailbox->email( qit.next() );	if ( mail ) {	    mail->setStatus(EFlag_Sent, true);	    mail->setDirty( true );		    if ( !moveMailToFolder(mail, mailbox, mailboxList->mailbox(SentString) ) )		break;	    //no point continuing to move	}    }        if ( !sendSingle ) {	//loop through, if not all messages sent, start over        QListIterator<Email*> it = mailboxList->mailbox(OutboxString)->entryIterator();	while ( it.hasNext() ) {	    mail = it.next();	    if ( !mail->status(EFlag_Sent) ) {		sendAllQueuedMail();		return;	    }	}    }    queuedUuids.clear();    sending = false;//     sendMailButton->setVisible(true);    cancelButton->setVisible(false);    isSending(false);}void EmailClient::addMailToDownloadList(Email *mail){    if ( mail->status(EFlag_Downloaded) || mail->fromAccount() != mailAccount->id() )	return;    if ( (mailAccount->maxMailSize() > -1) && (mail->size() > (uint) ( mailAccount->maxMailSize() * 1024 ) ) )	return;	    if ( mailAccount->accountType() == MailAccount::IMAP ) {	Mailbox *box = mailAccount->getMailboxRef( mail->fromMailbox() );	if ( box ) {	    FolderSyncSetting fs = box->folderSync();	    if ( fs & Sync_OnlyHeaders ) {		return;	    } else if ( fs & Sync_OnlyNew ) {		if ( mail->status(EFlag_IMAP_Seen) )		    return;	    }	}    }    mailDownloadList.sizeInsert(mail->serverUid(), mail->size(), mail->uuid(), mail->fromMailbox() );}void EmailClient::getNewMail(){    if ( !verifyAccounts(false) )	return;    receiving = true;    previewingMail = true;    getMailButton->setVisible(false);    cancelButton->setVisible(true);    selectAccountMenu->setEnabled(false);    statusLabelHeader = mailAccount->accountName();    //get any previous mails not downloaded and add to queue    mailDownloadList.clear();    Email *mail;    QListIterator<Email*> it = mailboxList->mailbox(InboxString)->entryIterator();    while ( it.hasNext() ) {	mail = it.next();	if ( !mail->status(EFlag_Downloaded) )	    addMailToDownloadList( mail );    }    emailHandler->setMailAccount(mailAccount);    if (!sending)	queueStatus = 1;    quitSent = false;    emailHandler->getMailHeaders();    isReceiving(true);}void EmailClient::getAllNewMail(){    allAccounts = true;    accountIdCount = 0;    mailAccount = accountList->at(accountIdCount);    while ( mailAccount != NULL ) {	if ( !mailAccount->canCollectMail() ) {	    accountIdCount++;	    mailAccount = accountList->at(accountIdCount);	} else	    break;    }    getNewMail();}void EmailClient::getSingleMail(Email *mail){    if (receiving) {	QString user = mailAccount->id();	if ( user == mail->fromAccount() ) {	    mailDownloadList.append(mail->serverUid(), mail->size(), mail->uuid(), mail->fromMailbox() );	    setTotalPopSize( mailDownloadList.size() );	} else {	    qWarning("receiving in progress, no action performed");	}	return;    }    mailAccount = accountList->getPopRefByAccount( mail->fromAccount() );    if (mailAccount == NULL) {	QString temp = tr("<qt>Mail was retrieved from account %1<br>Redefine this account to get this mail</qt>").arg(mail->fromAccount()) + "</qt>";	QMessageBox::warning(qApp->activeWindow(),tr("Account not defined"),	    temp, tr("OK"));	return;    }    receiving = true;    previewingMail = false;    allAccounts = false;    getMailButton->setVisible(false);    cancelButton->setVisible(true);    selectAccountMenu->setEnabled(false);    statusLabelHeader = mailAccount->accountName();    mailDownloadList.clear();    mailDownloadList.sizeInsert(mail->serverUid(), mail->size(), mail->uuid(), mail->fromMailbox() );    emailHandler->setMailAccount(mailAccount);    quitSent = false;    setTotalPopSize( mailDownloadList.size() );    isReceiving(true);    emailHandler->getMailByList(&mailDownloadList, true);}void EmailClient::unresolvedUidlArrived(QString &user, QStringList &list){    Email *mail;    QString msg = tr("<qt>%1<br>The following messages have been deleted "		     "from the server by another email client and can not be completed:<br>").arg(user);    QString mailList = "";       QListIterator<Email*> it = mailboxList->mailbox(InboxString)->entryIterator();   while ( it.hasNext() ) {        mail = it.next();	if ( ( !mail->status(EFlag_Downloaded) ) && (mail->fromAccount() == mailAccount->id() ) ) {	    if ( (list.contains( mail->serverUid() ) ) ) {		mailList += mail->fromName() + " - "  + mail->subject() + "<br>";	    }	}    }    QMessageBox::warning(qApp->activeWindow(),	tr("Unresolved mail"), msg + mailList + "</qt>", tr("OK"));}void EmailClient::readReplyRequested(Email* mail){#ifdef QTOPIA_PHONE# ifndef QTOPIA_NO_MMS    QString netCfg;    QListIterator<MailAccount*> it = accountList->accountIterator();    while (it.hasNext()) {        MailAccount *account = it.next();        if (account->accountType() == MailAccount::MMS) {            netCfg = account->networkConfig();            break;        }    }    QSettings mmsConf(netCfg, QSettings::IniFormat);    mmsConf.beginGroup("MMS");    if (mmsConf.value("AllowDeliveryReport", "n").toString() == "y") {        QString msg(tr("<qt>Do you wish to send a Read Reply?</qt>"));        if (QMessageBox::information(this, tr("Multimedia Message"), msg,            QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {                Email rrmail;                rrmail.setType(MailMessage::MMS);                rrmail.setTo(QStringList(mail->from()));                rrmail.setSubject(mail->subject());                rrmail.setExtraHeader("X-Mms-Message-Class", "Auto");                rrmail.setExtraHeader("X-Mms-Delivery-Report", "No");                rrmail.setExtraHeader("X-Mms-Read-Reply", "No");                QString msg = tr("Sent MMS \"%1\" was read on: %2", "%1 = subject %2 = date");                msg = msg.arg(mail->subject());                msg = msg.arg(QDateTime::currentDateTime().toString());                MailMessagePart part;                part.setContentType("text/plain");                part.setEncodedBody(msg, EightBit);                rrmail.addMessagePart(part);                rrmail.setStatus(EFlag_Outgoing | EFlag_Downloaded, true);                rrmail.setUuid( mailboxList->mailbox(OutboxString)->generateUuid() );                rrmail.encodeMail();                if ( !mailboxList->mailbox(OutboxString)->addMail(rrmail) ) {                    accessError(mailboxList->mailbox(OutboxString) );                    return;                }                sendSingleMail(&rrmail);            }    }# endif#endif}/*  This function is basically here to ensure the header only    mail is replaced so that the ListWidget remains the same */void EmailClient::mailUpdated(Email *entry, const QString &mailbox){    if(readMailWidget()->isVisible() )        readMailWidget()->mailUpdated( entry );    updateQuery( entry, mailbox );    updateFolderCount( mailbox );}void EmailClient::mailRemoved(const QUuid &uuid, const QString &mailbox){    Folder *folder = folderView->currentFolder();    if (!folder)	return;    if ( folder->mailbox() == mailbox ) {	EmailListItem *item = messageView()->getRef( uuid );	if ( item ) {	    EmailListItem *newItem = item;	    newItem = 0;	    int row = messageView()->row( item );	    if (row > 0) //try below		newItem = (EmailListItem *)messageView()->item( row - 1, 0 );	    if (newItem == NULL) //try above		newItem = (EmailListItem *)messageView()->item( row + 1, 0 );	    	    messageView()->removeRow( messageView()->row( item ) );	    if ( newItem ) {		messageView()->clearSelection();                messageView()->setItemSelected( newItem, true );		messageView()->setCurrentItem( newItem );	    }	    if ( newItem )		messageView()->setItemSelected( newItem, true );	    if ( readMailWidget()->isVisible() ) {		readMailWidget()->update( messageView() );	    }	} else {	    qWarning("hmm, message already removed from view?? ");	}    }    updateFolderCount( mailbox );	// Need to update count of associated folder in folderlistview}/*  Mail arrived from server, treated a bit differently than from disk */void EmailClient::mailArrived(const Email &m){    Email mail(m);#ifndef QT_NO_COP

⌨️ 快捷键说明

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