[Qt-interest] question from a student who interests in Qt
buptloveqt buptloveqt
buptloveqt at gmail.com
Wed Mar 18 15:06:58 CET 2009
Hello ,
I want to write an e-mail client. I found a SMTP class example,and i
already write a pop3 class.But some mistake occours.It can't emit
signal "readyRead"or "connected".Can you help me to solve the
problem?Or send a receive email client programme to me for reference.
Thank you very much .
And thank you all the same whether you can help me or not !
Here is code:
*****************************************
Pop.h
------------------------------------------
#ifndef POP_H
#define POP_H
#include <qobject.h>
#include <qstring.h>
class QSocket;
class QTextStream;
class QDns;
class Pop : public QObject
{
Q_OBJECT
public:
Pop( const QString &, const QString &, const QString & );
~Pop();
signals:
void status( const QString & );
private slots:
void readyRead();
void connected();
void deleteMe();
void dnsLookupHelper();
private:
enum State {
USER,
PASS,
LIST,
RETR,
QUIT,
CLOSE,
};
QString from;
QString username;
QString password;
QSocket *socket;
QTextStream * t;
int state;
QString response;
QDns * mxLookup;
};
#endif
------------------------------------------------
Pop.cpp
********************************************
#include "Pop.h"
#include <qtextstream.h>
#include <qsocket.h>
#include <qdns.h>
#include <qtimer.h>
#include <qapplication.h>
#include <qmessagebox.h>
#include <iostream.h>
using namespace std;
Pop::Pop( const QString &from ,const QString &username, const QString
&password )
{
socket = new QSocket( this );
connect ( socket, SIGNAL(readyRead() ),
this, SLOT(readyRead() ) );
connect ( socket, SIGNAL( connected() ),
this, SLOT( connected() ) );
mxLookup = new QDns( from.mid( from.find( '@' )+1 ), QDns::Mx );
connect( mxLookup, SIGNAL(resultsReady()),
this, SLOT(dnsLookupHelper()) );
this->from = from;
this->username = username;
this->password = password;
state = USER;
}
Pop::~Pop()
{
delete t;
delete socket;
}
void Pop::dnsLookupHelper()
{
QValueList<QDns::MailServer> s = mxLookup->mailServers();
if ( s.isEmpty() && mxLookup->isWorking() )
return;
std::cout << tr( "Connecting to %1" ).arg( s.first().name ) << endl;
socket->connectToHost( s.first().name, 110 );
t = new QTextStream( socket );
}
void Pop::connected()
{
QMessageBox::information( qApp->activeWindow(),
tr( "OK" ),
tr( "connected" ));
std::cout << tr( "Connected to %1" ).arg( socket->peerName() ) << endl;
}
void Pop::readyRead() // {
QMessageBox::information( qApp->activeWindow(),//for debug
tr( "OK" ),
tr( "ready" ));
// Pop3 is line-oriented
if ( !socket->canReadLine() )
return;
QString responseLine;
do {
responseLine = socket->readLine();
//std::cout << responseLine << endl;
if ((responseLine.find("+OK",0,true)!=0)&&(responseLine.find("-ERR",0,true)!=0)
)
response += responseLine;
} while( socket->canReadLine() && responseLine[3] != ' ' );
responseLine.truncate( 3 );
//std::cout << response << endl;
// email.append(response);
if ( state == USER ) {
*t << "USER:<" << username << ">\r\n";
//responseLine = socket->readLine();
//std::cout << *t;
state = PASS;
} else if ( state == PASS) {
*t << "PASS:<" << password << ">\r\n";
//responseLine = socket->readLine();
//std::cout << responseLine << endl;
state = LIST;
} else if ( state == LIST) {
*t << "LIST\r\n";
//responseLine = socket->readLine();
//std::cout << responseLine << endl;
state = RETR;
}else if ( state == RETR) {
*t << "RETR 5\r\n";
//responseLine = socket->readLine();
//std::cout << responseLine << endl;
state = QUIT;
} else if ( state == QUIT) {
*t << "QUIT\r\n";
//responseLine = socket->readLine();
//std::cout << responseLine << endl;
state = CLOSE;
emit status( tr( "Message receive" ) );
} else if ( state == CLOSE ) {
// we ignore it
} else {
// something broke.
QMessageBox::warning( qApp->activeWindow(),
tr( "Qt Mail Example" ),
tr( "Unexpected reply from POP3 server:\n\n" ) +
response );
state = CLOSE;
}
response = "";
if ( state == CLOSE) {
QTimer::singleShot( 0, this, SLOT(deleteMe()) );
}
}
void Pop::deleteMe()
{
delete this;
}
-------------- next part --------------
#include "Pop.h"
#include <qtextstream.h>
#include <qsocket.h>
#include <qdns.h>
#include <qtimer.h>
#include <qapplication.h>
#include <qmessagebox.h>
#include <iostream.h>
using namespace std;
Pop::Pop( const QString &from ,const QString &username, const QString &password )
{
socket = new QSocket( this );
connect ( socket, SIGNAL(connected() ),
this, SLOT(readyRead() ) );
//std::cout << tr( "socket %1" ).arg( socket->peerName() ) << endl;
/*connect ( socket, SIGNAL( connected() ),
this, SLOT( connected() ) );
*/
mxLookup = new QDns( from.mid( from.find( '@' )+1 ), QDns::Mx );
connect( mxLookup, SIGNAL(resultsReady()),
this, SLOT(dnsLookupHelper()) );
this->from = from;
this->username = username;
this->password = password;
state = USER;
/* QMessageBox::information( qApp->activeWindow(),//for debug
tr( "OK" ),
tr( "debug" ));
*/
/*socket = new QSocket( this );
mxLookup = new QDns( from.mid( from.find( '@' )+1 ), QDns::Mx );
connect( mxLookup, SIGNAL(resultsReady()),
this, SLOT(dnsLookupHelper()) );
connect ( socket, SIGNAL( connected() ), //??readyRead ?socket?????
this, SLOT( readyRead() ) );
*/
}
Pop::~Pop()
{
delete t;
delete socket;
}
void Pop::dnsLookupHelper()
{
QValueList<QDns::MailServer> s = mxLookup->mailServers();
if ( s.isEmpty() && mxLookup->isWorking() )
return;
//emit status( tr( "Connecting to %1" ).arg( s.first().name ) );
std::cout << tr( "Connecting to %1" ).arg( s.first().name ) << endl;
socket->connectToHost( s.first().name, 110 ); //??server
t = new QTextStream( socket );
/* QMessageBox::information( qApp->activeWindow(),
tr( "OK" ),
tr( "mailserver ok! put user&pswd" ));
*/
}
void Pop::connected()
{
//emit status( tr( "Connected to %1" ).arg( socket->peerName() ) );
QMessageBox::information( qApp->activeWindow(),//for debug
tr( "OK" ),
tr( "connected" ));
std::cout << tr( "Connected to %1" ).arg( socket->peerName() ) << endl;
}
void Pop::readyRead() // ??????
{
QMessageBox::information( qApp->activeWindow(),//for debug
tr( "OK" ),
tr( "ready" ));
// Pop3 is line-oriented
if ( !socket->canReadLine() )
return;
QString responseLine;
do {
responseLine = socket->readLine();
//std::cout << responseLine << endl;
if ((responseLine.find("+OK",0,true)!=0)&&(responseLine.find("-ERR",0,true)!=0) )
response += responseLine;
} while( socket->canReadLine() && responseLine[3] != ' ' );
responseLine.truncate( 3 );
//std::cout << response << endl;
// email.append(response);
if ( state == USER ) {
*t << "USER:<" << username << ">\r\n";
//responseLine = socket->readLine();
//std::cout << *t;
state = PASS;
} else if ( state == PASS) {
*t << "PASS:<" << password << ">\r\n";
//responseLine = socket->readLine();
//std::cout << responseLine << endl;
state = LIST;
} else if ( state == LIST) {
*t << "LIST\r\n";
//responseLine = socket->readLine();
//std::cout << responseLine << endl;
state = RETR;
}else if ( state == RETR) {
*t << "RETR 5\r\n";
//responseLine = socket->readLine();
//std::cout << responseLine << endl;
state = QUIT;
} else if ( state == QUIT) {
*t << "QUIT\r\n";
//responseLine = socket->readLine();
//std::cout << responseLine << endl;
state = CLOSE;
emit status( tr( "Message receive" ) );
} else if ( state == CLOSE ) {
// we ignore it
} else {
// something broke.
QMessageBox::warning( qApp->activeWindow(),
tr( "Qt Mail Example" ),
tr( "Unexpected reply from POP3 server:\n\n" ) +
response );
state = CLOSE;
}
response = "";
if ( state == CLOSE) {
QTimer::singleShot( 0, this, SLOT(deleteMe()) );
}
//std::cout << email << endl;
//p->updateAllViews(); // Update the Email Client
}
void Pop::deleteMe()
{
delete this;
}
-------------- next part --------------
#ifndef POP_H
#define POP_H
#include <qobject.h>
#include <qstring.h>
class QSocket;
class QTextStream;
class QDns;
class Pop : public QObject
{
Q_OBJECT
public:
Pop( const QString &, const QString &, const QString & );
~Pop();
signals:
//void finished();
void status( const QString & );
private slots:
void readyRead();
void connected();
void deleteMe();
void dnsLookupHelper();
private:
enum State {
USER,
PASS,
//STAT,
LIST,
RETR,
//DELE,
QUIT,
CLOSE,
};
QString from;
QString username;
QString password;
QSocket *socket;
QTextStream * t;
int state;
QString response;
QDns * mxLookup;
};
#endif
More information about the Qt-interest-old
mailing list