Local8Bit + timeout timer

Local 8 bit encoding support for ftp filenames.
Connection timeout timer.
This commit is contained in:
Dmitry Belyaev 2015-06-13 23:12:59 +03:00
parent 75b21b033f
commit 13132f5567
1 changed files with 32 additions and 13 deletions

View File

@ -39,8 +39,10 @@
** **
****************************************************************************/ ****************************************************************************/
//#define QFTPPI_DEBUG #ifdef DEBUG
//#define QFTPDTP_DEBUG #define QFTPPI_DEBUG
#define QFTPDTP_DEBUG
#endif
#include "qftp.h" #include "qftp.h"
#include "qabstractsocket.h" #include "qabstractsocket.h"
@ -57,6 +59,7 @@
#include "qhash.h" #include "qhash.h"
#include "qtcpserver.h" #include "qtcpserver.h"
#include "qlocale.h" #include "qlocale.h"
#include <QTimer>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -186,6 +189,7 @@ private slots:
void delayedCloseFinished(); void delayedCloseFinished();
void readyRead(); void readyRead();
void error(QAbstractSocket::SocketError); void error(QAbstractSocket::SocketError);
void check_connectToHost_timeout();
void dtpConnectState(int); void dtpConnectState(int);
@ -221,6 +225,7 @@ private:
bool waitForDtpToClose; bool waitForDtpToClose;
QByteArray bytesFromSocket; QByteArray bytesFromSocket;
QTimer timer;
friend class QFtpDTP; friend class QFtpDTP;
}; };
@ -822,6 +827,9 @@ void QFtpPI::connectToHost(const QString &host, quint16 port)
dtp.setProperty("_q_networksession", property("_q_networksession")); dtp.setProperty("_q_networksession", property("_q_networksession"));
#endif #endif
commandSocket.connectToHost(host, port); commandSocket.connectToHost(host, port);
// connection timeout
connect(&timer, SIGNAL(timeout()), this, SLOT(check_connectToHost_timeout()));
timer.start(55000);
} }
/* /*
@ -880,6 +888,8 @@ void QFtpPI::hostFound()
void QFtpPI::connected() void QFtpPI::connected()
{ {
timer.stop();
state = Begin; state = Begin;
#if defined(QFTPPI_DEBUG) #if defined(QFTPPI_DEBUG)
// qDebug("QFtpPI state: %d [connected()]", state); // qDebug("QFtpPI state: %d [connected()]", state);
@ -918,6 +928,15 @@ void QFtpPI::error(QAbstractSocket::SocketError e)
} }
} }
void QFtpPI::check_connectToHost_timeout()
{
if (!commandSocket.waitForConnected(1)){
emit connectState(QFtp::Unconnected);
emit error(QFtp::ConnectionRefused,
QFtp::tr("Connection timed out to host %1").arg(commandSocket.peerName()));
}
}
void QFtpPI::readyRead() void QFtpPI::readyRead()
{ {
if (waitForDtpToClose) if (waitForDtpToClose)
@ -1212,7 +1231,7 @@ bool QFtpPI::startNextCmd()
qDebug("QFtpPI send: %s", currentCmd.left(currentCmd.length()-2).toLatin1().constData()); qDebug("QFtpPI send: %s", currentCmd.left(currentCmd.length()-2).toLatin1().constData());
#endif #endif
state = Waiting; state = Waiting;
commandSocket.write(currentCmd.toLatin1()); commandSocket.write(currentCmd.toLocal8Bit());
return true; return true;
} }
@ -2311,39 +2330,39 @@ void QFtpPrivate::_q_piError(int errorCode, const QString &text)
error = QFtp::Error(errorCode); error = QFtp::Error(errorCode);
switch (q->currentCommand()) { switch (q->currentCommand()) {
case QFtp::ConnectToHost: case QFtp::ConnectToHost:
errorString = QString::fromLatin1(QT_TRANSLATE_NOOP("QFtp", "Connecting to host failed:\n%1")) errorString = QFtp::tr("Connecting to host failed:\n%1")
.arg(text); .arg(text);
break; break;
case QFtp::Login: case QFtp::Login:
errorString = QString::fromLatin1(QT_TRANSLATE_NOOP("QFtp", "Login failed:\n%1")) errorString = QFtp::tr("Login failed:\n%1")
.arg(text); .arg(text);
break; break;
case QFtp::List: case QFtp::List:
errorString = QString::fromLatin1(QT_TRANSLATE_NOOP("QFtp", "Listing directory failed:\n%1")) errorString = QFtp::tr("Listing directory failed:\n%1")
.arg(text); .arg(text);
break; break;
case QFtp::Cd: case QFtp::Cd:
errorString = QString::fromLatin1(QT_TRANSLATE_NOOP("QFtp", "Changing directory failed:\n%1")) errorString = QFtp::tr("Changing directory failed:\n%1")
.arg(text); .arg(text);
break; break;
case QFtp::Get: case QFtp::Get:
errorString = QString::fromLatin1(QT_TRANSLATE_NOOP("QFtp", "Downloading file failed:\n%1")) errorString = QFtp::tr("Downloading file failed:\n%1")
.arg(text); .arg(text);
break; break;
case QFtp::Put: case QFtp::Put:
errorString = QString::fromLatin1(QT_TRANSLATE_NOOP("QFtp", "Uploading file failed:\n%1")) errorString = QFtp::tr("Uploading file failed:\n%1")
.arg(text); .arg(text);
break; break;
case QFtp::Remove: case QFtp::Remove:
errorString = QString::fromLatin1(QT_TRANSLATE_NOOP("QFtp", "Removing file failed:\n%1")) errorString = QFtp::tr("Removing file failed:\n%1")
.arg(text); .arg(text);
break; break;
case QFtp::Mkdir: case QFtp::Mkdir:
errorString = QString::fromLatin1(QT_TRANSLATE_NOOP("QFtp", "Creating directory failed:\n%1")) errorString = QFtp::tr("Creating directory failed:\n%1")
.arg(text); .arg(text);
break; break;
case QFtp::Rmdir: case QFtp::Rmdir:
errorString = QString::fromLatin1(QT_TRANSLATE_NOOP("QFtp", "Removing directory failed:\n%1")) errorString = QFtp::tr("Removing directory failed:\n%1")
.arg(text); .arg(text);
break; break;
default: default:
@ -2371,7 +2390,7 @@ void QFtpPrivate::_q_piConnectState(int connectState)
emit q_func()->stateChanged(state); emit q_func()->stateChanged(state);
if (close_waitForStateChange) { if (close_waitForStateChange) {
close_waitForStateChange = false; close_waitForStateChange = false;
_q_piFinished(QLatin1String(QT_TRANSLATE_NOOP("QFtp", "Connection closed"))); _q_piFinished(QFtp::tr("Connection closed"));
} }
} }