writemail.cpp
来自「Qtopia下的邮件处理程序」· C++ 代码 · 共 1,183 行 · 第 1/3 页
CPP
1,183 行
/******************************************************************************** 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 "writemail.h"#include "addresslist.h"#include "accountlist.h"#include "account.h"#ifdef QTOPIA_PHONE#ifndef QTOPIA_NO_MMS#include "mmscomposer.h"#endif#include "composer.h"#else#include "pdacomposer.h"#endif#include "qtmailwindow.h"#include <QValueSpaceItem>#include <qtopia/qtopiaapplication.h>#include <QSettings>#include <QMenu>#include <qtopia/qsoftmenubar.h>#include <qtopia/qdocumentselector.h>#include <qtopia/qtopiaglobal.h>#include <QTabWidget>#include <QToolBar>#include <QMenuBar>#include <QStackedWidget>#include <QMessageBox>#include <QDateTime>#include <QBoxLayout>#ifdef QTOPIA_PHONE#include <QTextEdit>#endif#include <QDir>#include <QFileInfo>#include <qtopia/qmimetype.h>#include <QToolTip>#include <QWhatsThis>#include <QStackedWidget>#ifndef Q_OS_WIN32#ifndef Q_OS_MACX#include <sys/vfs.h> // sharp 1862#endif#endif#ifdef QTOPIA_PHONE#include "detailspage.h"#endifstatic QString createFrom(QString name, QString email){ QString from; if ( !name.trimmed().isEmpty() ) from = Email::quoteString( name.trimmed() ); if ( email.isEmpty() ) return from; // Error no email address specified if ( email[0] == '<' ) from += " " + email; else from += " <" + email + ">"; return from;}//===========================================================================SelectListWidget::SelectListWidget( QDialog *parent ) : QListWidget( parent ){ mParent = parent; setFrameStyle( QFrame::NoFrame ); mKeyBackPressed = false;}bool SelectListWidget::keyBackPressed(){ return mKeyBackPressed;}void SelectListWidget::keyPressEvent(QKeyEvent *e){ if( e->key() == Qt::Key_Back ) { mKeyBackPressed = true; mParent->reject(); e->accept(); return; } QListWidget::keyPressEvent( e );}//===========================================================================WriteMail::WriteMail(QWidget* parent, const char* name, Qt::WFlags fl ) : QMainWindow( parent, fl ), mail(),_detailsOnly(false){ setObjectName(name); /* because of the way QtMail has been structured, even though WriteMail is a main window, setting its caption has no effect because its parent is also a main window. have to set it through the real main window. */#ifdef QTOPIA_DESKTOP m_mainWindow = this;#else m_mainWindow = QTMailWindow::singleton();#endif init(); readConfig(); //setCaption( tr("Write mail") );}WriteMail::~WriteMail(){ saveConfig(); delete m_composerInterface; m_composerInterface = 0;}void WriteMail::setAccountList(AccountList *list){ accountList = list;#ifdef QTOPIA_PHONE if (m_detailsPage ) m_detailsPage->setFromFields( accountList->emailAccounts() );#else m_composerInterface->setFromFields( accountList->emailAccounts() );#endif QStringList::Iterator it; QStringList as = accountList->emailAccounts(); for( it = as.begin() ; it != as.end() ; ++it ) qDebug((*it).toAscii());}void WriteMail::init(){ widgetStack = new QStackedWidget(this); setCentralWidget(widgetStack); #ifdef QTOPIA_PHONE // stage 1 - composing m_composerInterface = 0; m_composerWidget = 0; // setup when a composer is set // stage 2 - specifying recipients and other message details // also setup when a composer is set m_detailsPage = 0; keyEventAccepted = false;#else m_composerInterface = new PDAComposer( this, "pdaComposer" ); connect( m_composerInterface, SIGNAL(contentChanged()), this, SLOT(messageChanged()) ); connect( m_composerInterface, SIGNAL(recipientsChanged()), this, SLOT(messageChanged()) ); m_mainWindow->setWindowTitle( tr("Write Mail") );#endif // stage 3 - sending /* TODO : composers should handle this wrapLines = new QAction( tr("Wrap lines"), QString::null, 0, this, 0); wrapLines->setToggleAction( true ); connect(wrapLines, SIGNAL( activated() ), this, SLOT( wrapToggled() ) ); */#ifdef QTOPIA_PHONE m_cancelAction = new QAction(QIcon(":image/contextbar/cancel"),tr("Cancel"),this);#else m_cancelAction = new QAction(QIcon(":icon/discard"),tr("Discard"),this);#endif connect( m_cancelAction, SIGNAL(triggered()), this, SLOT(discard()) ); m_draftAction = new QAction(QIcon(":icon/draft"),tr("Save in drafts"),this); connect( m_draftAction, SIGNAL(triggered()), this, SLOT(draft()) ); m_draftAction->setWhatsThis( tr("Save this message as a draft.") );#ifdef QTOPIA_PHONE /* m_selectComposerAction = new QAction( tr("Message Type"), QString::null, 0, this, 0 ); connect( m_selectComposerAction, SIGNAL(activated()), this, SLOT(selectComposer()) ); */ m_previousAction = new QAction( QIcon(":image/contextbar/previous"),tr("Previous"),this); connect( m_previousAction, SIGNAL(triggered()), this, SLOT(previousStage()) );#else setCentralWidget( m_composerInterface ); // TODO : PDA QToolBar *bar = new QToolBar( this ); //bar->setHorizontalStretchable( true ); QMenuBar *menu = new QMenuBar( bar ); QMenu *mailMenu = menu->addMenu( tr( "&Message" )); QMenu *fieldMenu = menu->addMenu( tr("&Options")); QAction *enqueueAction = new QAction( QIcon(":icon/enqueue"),tr("Send"),this); connect( enqueueAction, SIGNAL(triggered()), this, SLOT(enqueue()) ); enqueueAction->setWhatsThis( tr("Add this mail to the queue of outgoing mails. All enqueued mails are stored in the outbox.")); QAction *attachAction = new QAction(QIcon(":icon/attach"),tr("Attachments"),this); connect( attachAction, SIGNAL(triggered()), m_composerInterface, SLOT(selectAttachments()) ); // TODO : PDA mailMenu->addAction(enqueueAction); mailMenu->addAction(m_draftAction); mailMenu->addAction(attachAction); mailMenu->addSeparator(); mailMenu->addAction(m_cancelAction); m_ccAction = new QAction( tr("Show CC"), this); connect( m_ccAction, SIGNAL(toggled(bool)), m_composerInterface, SLOT(toggleCc(bool)) ); m_ccAction->setCheckable( true ); fieldMenu->addAction(m_ccAction); m_bccAction = new QAction( tr("Show BCC"),this); connect( m_bccAction, SIGNAL(toggled(bool)), m_composerInterface, SLOT(toggleBcc(bool)) ); m_bccAction->setCheckable( true ); fieldMenu->addAction(m_bccAction); m_fromAction = new QAction( tr("Show From"),this); connect( m_fromAction, SIGNAL(toggled(bool)), m_composerInterface, SLOT(toggleFrom(bool)) ); m_fromAction->setCheckable( true ); fieldMenu->addAction(m_fromAction); fieldMenu->addSeparator(); m_wrapAction = new QAction( tr("Word Wrap"),this); connect( m_wrapAction, SIGNAL(toggled(bool)), m_composerInterface, SLOT(setWordWrap(bool)) ); m_wrapAction->setCheckable( true ); fieldMenu->addAction(m_wrapAction); bar->addAction(enqueueAction); bar->addAction(m_draftAction); bar->addAction(attachAction); bar->addSeparator(); bar->addAction(m_cancelAction);#endif}void WriteMail::nextStage(){#ifdef QTOPIA_PHONE QWidget *curPage = widgetStack->currentWidget(); if( !curPage ) { composeStage(); } else if( curPage == m_composerWidget ) { if( !m_composerInterface ) qDebug("BUG: NULL composer interface in composer stage"); if( !m_composerInterface->hasContent() ) { discard(); //QTimer::singleShot( 0, this, SIGNAL(discardMail()) ); return; } detailsStage(); } else if( curPage == m_detailsPage ) { sendStage(); } else { qDebug("BUG : WriteMail::nextStage() called in unknown stage."); }#endif}void WriteMail::sendStage(){ enqueue(); }void WriteMail::detailsStage(){#ifdef QTOPIA_PHONE if( !changed() && !_detailsOnly) { discard(); return; } m_detailsPage->setType( m_composerInterface->type() ); if(_detailsOnly) //fix resize problem if not showing composer widgetStack->setCurrentWidget(m_composerWidget); widgetStack->setCurrentWidget(m_detailsPage); QTimer::singleShot( 0, this, SLOT(updateUI()) );#endif}void WriteMail::composeStage(){#ifdef QTOPIA_PHONE if( composer().isEmpty() ) setComposer( ComposerFactory::defaultInterface() ); widgetStack->setCurrentWidget(m_composerWidget); setComposerFocus(); QTimer::singleShot( 0, this, SLOT(updateUI()) );#endif}bool WriteMail::hasRecipients() const // Also check from field is not empty{#ifdef QTOPIA_PHONE if ((m_composerInterface && m_composerInterface->type() == MailMessage::Email) && m_detailsPage->from().trimmed().isEmpty() ) return false; return !m_detailsPage->to().trimmed().isEmpty();#else if ( m_composerInterface->from().trimmed().isEmpty() ) return false; return !m_composerInterface->to().trimmed().isEmpty() || !m_composerInterface->cc().trimmed().isEmpty() || !m_composerInterface->bcc().trimmed().isEmpty();#endif}bool WriteMail::isComplete() const{ return changed() && hasRecipients();}/* QtMailWindow calls this method when it receives a close event, meaning the user just closed the window. For the PDA Autosave is called, where the emailclient needs to determine what to do ( add to draft or outbox)*/bool WriteMail::tryAccept(){ int r = -1; // ideally, you'd also check to see if the message is the same as it was when we started working on it if( hasMessageChanged ) { r = QMessageBox::warning( this, tr("Save to drafts"), "<qt>" + tr("Do you wish to save the message to drafts?") + "</qt>", tr("Yes"), tr("No"), 0, 0, 1 ) ; } if( !r ) draft(); else // empty or user chose not to save discard(); return true;}void WriteMail::previousStage(){#ifdef QTOPIA_PHONE QWidget *curPage = widgetStack->currentWidget(); if( curPage == m_composerWidget ) return; // no previous if( !curPage ) composeStage(); else if( curPage == m_detailsPage ) composeStage(); else qDebug("BUG : WriteMail::nextStage() called in unknown stage.");#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?