diff options
Diffstat (limited to 'examples/network/mail')
-rw-r--r-- | examples/network/mail/composer.cpp | 65 | ||||
-rw-r--r-- | examples/network/mail/composer.h | 41 | ||||
-rw-r--r-- | examples/network/mail/mail.doc | 25 | ||||
-rw-r--r-- | examples/network/mail/mail.pro | 12 | ||||
-rw-r--r-- | examples/network/mail/main.cpp | 23 | ||||
-rw-r--r-- | examples/network/mail/smtp.cpp | 127 | ||||
-rw-r--r-- | examples/network/mail/smtp.h | 58 |
7 files changed, 351 insertions, 0 deletions
diff --git a/examples/network/mail/composer.cpp b/examples/network/mail/composer.cpp new file mode 100644 index 0000000..4c2bb36 --- /dev/null +++ b/examples/network/mail/composer.cpp @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "composer.h" +#include "smtp.h" + +#include <qlineedit.h> +#include <qmultilineedit.h> +#include <qpushbutton.h> +#include <qlabel.h> +#include <qlayout.h> + +Composer::Composer( QWidget *parent ) + : QWidget( parent ) +{ + QGridLayout * layout = new QGridLayout( this, 1, 1, 6 ); + + layout->addWidget( new QLabel( tr( "From:" ), this ), 0, 0 ); + from = new QLineEdit( this ); + layout->addWidget( from, 0, 1 ); + + layout->addWidget( new QLabel( tr( "To:" ), this ), 1, 0 ); + to = new QLineEdit( this ); + layout->addWidget( to, 1, 1 ); + + layout->addWidget( new QLabel( tr( "Subject:" ), this ), 2, 0 ); + subject = new QLineEdit( this ); + layout->addWidget( subject, 2, 1 ); + + message = new QMultiLineEdit( this ); + layout->addMultiCellWidget( message, 3, 3, 0, 1 ); + + send = new QPushButton( tr( "&Send" ), this ); + layout->addWidget( send, 4, 0 ); + connect( send, SIGNAL( clicked() ), this, SLOT( sendMessage() ) ); + + sendStatus = new QLabel( this ); + layout->addWidget( sendStatus, 4, 1 ); +} + + +void Composer::sendMessage() +{ + send->setEnabled( FALSE ); + sendStatus->setText( tr( "Looking up mail servers" ) ); + Smtp *smtp = new Smtp( from->text(), to->text(), + subject->text(), + message->text() ); + connect( smtp, SIGNAL(destroyed()), + this, SLOT(enableSend()) ); + connect( smtp, SIGNAL(status(const QString &)), + sendStatus, SLOT(setText(const QString &)) ); +} + + +void Composer::enableSend() +{ + send->setEnabled( TRUE ); +} diff --git a/examples/network/mail/composer.h b/examples/network/mail/composer.h new file mode 100644 index 0000000..fa457ce --- /dev/null +++ b/examples/network/mail/composer.h @@ -0,0 +1,41 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#ifndef COMPOSER_H +#define COMPOSER_H + +#include <qwidget.h> + + +class QLineEdit; +class QMultiLineEdit; +class QLabel; +class QPushButton; + + +class Composer : public QWidget +{ + Q_OBJECT + +public: + Composer( QWidget *parent = 0 ); + +private slots: + void sendMessage(); + void enableSend(); + +private: + QLineEdit *from, *to, *subject; + QMultiLineEdit *message; + QLabel * sendStatus; + QPushButton * send; +}; + + +#endif diff --git a/examples/network/mail/mail.doc b/examples/network/mail/mail.doc new file mode 100644 index 0000000..009f155 --- /dev/null +++ b/examples/network/mail/mail.doc @@ -0,0 +1,25 @@ +/* +*/ + +/*! \page mail-example.html + + \ingroup network-examples + + \title A simple mail client + + This example shows how to use the QSocket class. The client can only be + used to send mails. The interesting part is the implementation of the + SMTP protocol. + + <hr> + + Header file (smtp.h): + + \include network/mail/smtp.h + + <hr> + + Implementation (smtp.cpp): + + \include network/mail/smtp.cpp +*/ diff --git a/examples/network/mail/mail.pro b/examples/network/mail/mail.pro new file mode 100644 index 0000000..39c9de5 --- /dev/null +++ b/examples/network/mail/mail.pro @@ -0,0 +1,12 @@ +TEMPLATE = app +TARGET = mail + +CONFIG += qt warn_on release + +REQUIRES = network full-config + +HEADERS = composer.h \ + smtp.h +SOURCES = composer.cpp \ + main.cpp \ + smtp.cpp diff --git a/examples/network/mail/main.cpp b/examples/network/mail/main.cpp new file mode 100644 index 0000000..ac60756 --- /dev/null +++ b/examples/network/mail/main.cpp @@ -0,0 +1,23 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> + +#include "composer.h" + +int main( int argc, char **argv ) +{ + QApplication a( argc, argv ); + + Composer c; + a.setMainWidget( &c ); + c.resize( 400, 500 ); + c.show(); + return a.exec(); +} diff --git a/examples/network/mail/smtp.cpp b/examples/network/mail/smtp.cpp new file mode 100644 index 0000000..f801070 --- /dev/null +++ b/examples/network/mail/smtp.cpp @@ -0,0 +1,127 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "smtp.h" + +#include <qtextstream.h> +#include <qsocket.h> +#include <qdns.h> +#include <qtimer.h> +#include <qapplication.h> +#include <qmessagebox.h> +#include <qregexp.h> + + +Smtp::Smtp( const QString &from, const QString &to, + const QString &subject, + const QString &body ) +{ + socket = new QSocket( this ); + connect ( socket, SIGNAL( readyRead() ), + this, SLOT( readyRead() ) ); + connect ( socket, SIGNAL( connected() ), + this, SLOT( connected() ) ); + + mxLookup = new QDns( to.mid( to.find( '@' )+1 ), QDns::Mx ); + connect( mxLookup, SIGNAL(resultsReady()), + this, SLOT(dnsLookupHelper()) ); + + message = QString::fromLatin1( "From: " ) + from + + QString::fromLatin1( "\nTo: " ) + to + + QString::fromLatin1( "\nSubject: " ) + subject + + QString::fromLatin1( "\n\n" ) + body + "\n"; + message.replace( QString::fromLatin1( "\n" ), + QString::fromLatin1( "\r\n" ) ); + message.replace( QString::fromLatin1( "\r\n.\r\n" ), + QString::fromLatin1( "\r\n..\r\n" ) ); + + this->from = from; + rcpt = to; + + state = Init; +} + + +Smtp::~Smtp() +{ + delete t; + delete socket; +} + + +void Smtp::dnsLookupHelper() +{ + QValueList<QDns::MailServer> s = mxLookup->mailServers(); + if ( s.isEmpty() ) { + if ( !mxLookup->isWorking() ) + emit status( tr( "Error in MX record lookup" ) ); + return; + } + + emit status( tr( "Connecting to %1" ).arg( s.first().name ) ); + + socket->connectToHost( s.first().name, 25 ); + t = new QTextStream( socket ); +} + + +void Smtp::connected() +{ + emit status( tr( "Connected to %1" ).arg( socket->peerName() ) ); +} + +void Smtp::readyRead() +{ + // SMTP is line-oriented + if ( !socket->canReadLine() ) + return; + + QString responseLine; + do { + responseLine = socket->readLine(); + response += responseLine; + } while( socket->canReadLine() && responseLine[3] != ' ' ); + responseLine.truncate( 3 ); + + if ( state == Init && responseLine[0] == '2' ) { + // banner was okay, let's go on + *t << "HELO there\r\n"; + state = Mail; + } else if ( state == Mail && responseLine[0] == '2' ) { + // HELO response was okay (well, it has to be) + *t << "MAIL FROM: <" << from << ">\r\n"; + state = Rcpt; + } else if ( state == Rcpt && responseLine[0] == '2' ) { + *t << "RCPT TO: <" << rcpt << ">\r\n"; + state = Data; + } else if ( state == Data && responseLine[0] == '2' ) { + *t << "DATA\r\n"; + state = Body; + } else if ( state == Body && responseLine[0] == '3' ) { + *t << message << ".\r\n"; + state = Quit; + } else if ( state == Quit && responseLine[0] == '2' ) { + *t << "QUIT\r\n"; + // here, we just close. + state = Close; + emit status( tr( "Message sent" ) ); + } else if ( state == Close ) { + deleteLater(); + return; + } else { + // something broke. + QMessageBox::warning( qApp->activeWindow(), + tr( "Qt Mail Example" ), + tr( "Unexpected reply from SMTP server:\n\n" ) + + response ); + state = Close; + } + + response = ""; +} diff --git a/examples/network/mail/smtp.h b/examples/network/mail/smtp.h new file mode 100644 index 0000000..0a469a5 --- /dev/null +++ b/examples/network/mail/smtp.h @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#ifndef SMTP_H +#define SMTP_H + +#include <qobject.h> +#include <qstring.h> + +class QSocket; +class QTextStream; +class QDns; + +class Smtp : public QObject +{ + Q_OBJECT + +public: + Smtp( const QString &from, const QString &to, + const QString &subject, const QString &body ); + ~Smtp(); + +signals: + void status( const QString & ); + +private slots: + void dnsLookupHelper(); + void readyRead(); + void connected(); + +private: + enum State { + Init, + Mail, + Rcpt, + Data, + Body, + Quit, + Close + }; + + QString message; + QString from; + QString rcpt; + QSocket *socket; + QTextStream * t; + int state; + QString response; + QDns * mxLookup; +}; + +#endif |