Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

imapbase.cpp

Go to the documentation of this file.
00001 #include <qsocket.h>
00002 #include <qtimer.h>
00003 
00004 #include "imapbase.h"
00005 
00006 IMAPBase::IMAPBase(const Account &account)
00007         : QObject(), _account(account)
00008 {
00009         _connected = false;
00010         _writingAllowed = false;
00011         _socket = new QSocket(this);
00012 
00013         connect(_socket, SIGNAL(readyRead()), SLOT(slotDataAvailiable()));
00014         connect(_socket, SIGNAL(hostFound()), SLOT(slotHostFound()));
00015         connect(_socket, SIGNAL(connected()), SLOT(slotConnected()));
00016         connect(_socket, SIGNAL(connectionClosed()), SLOT(slotDisconnected()));
00017         connect(_socket, SIGNAL(error(int)), SLOT(slotError(int)));
00018 
00019         QTimer *commandTimer = new QTimer(this);
00020         commandTimer->start(200);
00021         connect(commandTimer, SIGNAL(timeout()), SLOT(writeCommands()));
00022 }
00023 
00024 void IMAPBase::sendCommand(const QString &command)
00025 {
00026         if (!_connected) makeConnect();
00027         _commandQueue.append(command);
00028 }
00029 
00030 void IMAPBase::disconnect()
00031 {
00032         _connected = false;
00033         delete _socket;
00034         emit disconnected();
00035 }
00036 
00037 void IMAPBase::makeConnect()
00038 {
00039         emit lookingUpHost();
00040         if (_socket == NULL) _socket = new QSocket(this);
00041 
00042         Q_UINT16 port = _account.imapPort().toUInt();
00043         _socket->connectToHost(_account.imapServer(), port);
00044 }
00045 
00046 void IMAPBase::writeCommands()
00047 {
00048         if (!_connected) return;
00049         if (_commandQueue.isEmpty()) return;
00050         if (!_writingAllowed) return;
00051 
00052         QStringList::Iterator it;
00053         for (it = _commandQueue.begin(); it != _commandQueue.end(); it++) {
00054                 if (!(*it).isEmpty() && _writingAllowed) {
00055 #ifndef QT_NO_DEBUG
00056                         qDebug("IMAP > " + (*it).stripWhiteSpace().local8Bit ());
00057 #endif
00058                         _socket->writeBlock((*it).latin1(), (*it).length());
00059                         _writingAllowed = false;
00060                         if (( *it ). find ( QRegExp ( "^[a-z][0-9]+ " )) == 0 )
00061                                 _lasttag = (*it).left(2);
00062 
00063                         connect(_socket, SIGNAL(readyRead()), SLOT(slotDataAvailiable()));
00064                         _commandQueue.remove(it);
00065                         break;
00066                 }
00067         }
00068 }
00069 
00070 void IMAPBase::slotError(int err)
00071 {
00072         if (err == QSocket::ErrConnectionRefused) {
00073                 emit error(IMAPErrConnectionRefused);
00074         } else if (err == QSocket::ErrHostNotFound) {
00075                 emit error(IMAPErrHostNotFound);
00076         } else if (err == QSocket::ErrSocketRead) {
00077                 emit error(IMAPErrSocketRead);
00078         } else {
00079                 emit error(IMAPErrUnknownError);
00080         }
00081 }
00082 
00083 void IMAPBase::slotHostFound()
00084 {
00085         emit hostFound();
00086 }
00087 
00088 void IMAPBase::slotConnected()
00089 {
00090         _connected = true;
00091         emit connected();
00092 }
00093 
00094 void IMAPBase::slotDisconnected()
00095 {
00096         _connected = false;
00097         emit disconnected();
00098 }
00099 
00100 #include <unistd.h>
00101 
00102 void IMAPBase::slotDataAvailiable()
00103 {
00104         static bool firstline = true;
00105 
00106         while (_socket->canReadLine()) {
00107                 QString tmp = _socket-> readLine ( );
00108         
00109                 _data += tmp;
00110                 qDebug ( "New Line [%d]: '%s'\n", _connected ? 1 : 0, tmp.latin1( ));
00111                 
00112                 if ( firstline || tmp. left(2) == _lasttag ) {
00113                         firstline = false;
00114                                         
00115 //              if ( _socket-> atEnd ( )) 
00116                         qDebug ( "at end -> emitting\n" );
00117                 
00118                         QObject::disconnect(_socket, SIGNAL(readyRead()), this, SLOT(slotDataAvailiable()));
00119                         emit dataReceived(_data);
00120                         _data = QString::null;
00121                         _writingAllowed = true;
00122                 }
00123         }
00124 }
00125 
00126 void IMAPBase::tryRead ( QString &data )
00127 {
00128         qDebug ( "Trying to read...\n" );
00129 
00130         while ( _socket-> canReadLine ( ))
00131                 data += _socket-> readLine ( );
00132 }

Generated on Sat Nov 5 16:18:07 2005 for OPIE by  doxygen 1.4.2