summaryrefslogtreecommitdiffstats
path: root/examples/network/mail
diff options
context:
space:
mode:
Diffstat (limited to 'examples/network/mail')
-rw-r--r--examples/network/mail/composer.cpp65
-rw-r--r--examples/network/mail/composer.h41
-rw-r--r--examples/network/mail/mail.doc25
-rw-r--r--examples/network/mail/mail.pro12
-rw-r--r--examples/network/mail/main.cpp23
-rw-r--r--examples/network/mail/smtp.cpp127
-rw-r--r--examples/network/mail/smtp.h58
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 000000000..f5ec2383c
--- /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 TQt. 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( TQWidget *parent )
+ : TQWidget( parent )
+{
+ TQGridLayout * layout = new TQGridLayout( this, 1, 1, 6 );
+
+ layout->addWidget( new TQLabel( tr( "From:" ), this ), 0, 0 );
+ from = new TQLineEdit( this );
+ layout->addWidget( from, 0, 1 );
+
+ layout->addWidget( new TQLabel( tr( "To:" ), this ), 1, 0 );
+ to = new TQLineEdit( this );
+ layout->addWidget( to, 1, 1 );
+
+ layout->addWidget( new TQLabel( tr( "Subject:" ), this ), 2, 0 );
+ subject = new TQLineEdit( this );
+ layout->addWidget( subject, 2, 1 );
+
+ message = new TQMultiLineEdit( this );
+ layout->addMultiCellWidget( message, 3, 3, 0, 1 );
+
+ send = new TQPushButton( tr( "&Send" ), this );
+ layout->addWidget( send, 4, 0 );
+ connect( send, SIGNAL( clicked() ), this, SLOT( sendMessage() ) );
+
+ sendStatus = new TQLabel( 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 TQString &)),
+ sendStatus, SLOT(setText(const TQString &)) );
+}
+
+
+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 000000000..6ef323139
--- /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 TQt. This example
+** program may be used, distributed and modified without limitation.
+**
+*****************************************************************************/
+
+#ifndef COMPOSER_H
+#define COMPOSER_H
+
+#include <qwidget.h>
+
+
+class TQLineEdit;
+class TQMultiLineEdit;
+class TQLabel;
+class TQPushButton;
+
+
+class Composer : public TQWidget
+{
+ Q_OBJECT
+
+public:
+ Composer( TQWidget *parent = 0 );
+
+private slots:
+ void sendMessage();
+ void enableSend();
+
+private:
+ TQLineEdit *from, *to, *subject;
+ TQMultiLineEdit *message;
+ TQLabel * sendStatus;
+ TQPushButton * send;
+};
+
+
+#endif
diff --git a/examples/network/mail/mail.doc b/examples/network/mail/mail.doc
new file mode 100644
index 000000000..009f15520
--- /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 000000000..39c9de535
--- /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 000000000..c80507699
--- /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 TQt. This example
+** program may be used, distributed and modified without limitation.
+**
+*****************************************************************************/
+
+#include <qapplication.h>
+
+#include "composer.h"
+
+int main( int argc, char **argv )
+{
+ TQApplication 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 000000000..a0256867b
--- /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 TQt. 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 TQString &from, const TQString &to,
+ const TQString &subject,
+ const TQString &body )
+{
+ socket = new TQSocket( this );
+ connect ( socket, SIGNAL( readyRead() ),
+ this, SLOT( readyRead() ) );
+ connect ( socket, SIGNAL( connected() ),
+ this, SLOT( connected() ) );
+
+ mxLookup = new TQDns( to.mid( to.find( '@' )+1 ), TQDns::Mx );
+ connect( mxLookup, SIGNAL(resultsReady()),
+ this, SLOT(dnsLookupHelper()) );
+
+ message = TQString::fromLatin1( "From: " ) + from +
+ TQString::fromLatin1( "\nTo: " ) + to +
+ TQString::fromLatin1( "\nSubject: " ) + subject +
+ TQString::fromLatin1( "\n\n" ) + body + "\n";
+ message.replace( TQString::fromLatin1( "\n" ),
+ TQString::fromLatin1( "\r\n" ) );
+ message.replace( TQString::fromLatin1( "\r\n.\r\n" ),
+ TQString::fromLatin1( "\r\n..\r\n" ) );
+
+ this->from = from;
+ rcpt = to;
+
+ state = Init;
+}
+
+
+Smtp::~Smtp()
+{
+ delete t;
+ delete socket;
+}
+
+
+void Smtp::dnsLookupHelper()
+{
+ TQValueList<TQDns::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 TQTextStream( socket );
+}
+
+
+void Smtp::connected()
+{
+ emit status( tr( "Connected to %1" ).arg( socket->peerName() ) );
+}
+
+void Smtp::readyRead()
+{
+ // SMTP is line-oriented
+ if ( !socket->canReadLine() )
+ return;
+
+ TQString 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 << "TQUIT\r\n";
+ // here, we just close.
+ state = Close;
+ emit status( tr( "Message sent" ) );
+ } else if ( state == Close ) {
+ deleteLater();
+ return;
+ } else {
+ // something broke.
+ TQMessageBox::warning( qApp->activeWindow(),
+ tr( "TQt 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 000000000..66cf5fda5
--- /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 TQt. This example
+** program may be used, distributed and modified without limitation.
+**
+*****************************************************************************/
+
+#ifndef SMTP_H
+#define SMTP_H
+
+#include <qobject.h>
+#include <qstring.h>
+
+class TQSocket;
+class TQTextStream;
+class TQDns;
+
+class Smtp : public TQObject
+{
+ Q_OBJECT
+
+public:
+ Smtp( const TQString &from, const TQString &to,
+ const TQString &subject, const TQString &body );
+ ~Smtp();
+
+signals:
+ void status( const TQString & );
+
+private slots:
+ void dnsLookupHelper();
+ void readyRead();
+ void connected();
+
+private:
+ enum State {
+ Init,
+ Mail,
+ Rcpt,
+ Data,
+ Body,
+ Quit,
+ Close
+ };
+
+ TQString message;
+ TQString from;
+ TQString rcpt;
+ TQSocket *socket;
+ TQTextStream * t;
+ int state;
+ TQString response;
+ TQDns * mxLookup;
+};
+
+#endif