Gentlemen,
I posted a huge problem some time ago and I assume it was too long.
I made some examinations and I am pretty sure that it has the same roots as the situation I describe below.
PROBLEM
Let us have a program that uses QFtp to download some file.
This applies to the program below:
#ifndef FTPGET_H
#define FTPGET_H
#include <QFile>
#include <QFtp>
{
Q_OBJECT
public:
bool getFile
(const QUrl &url
);
signals:
void done();
private slots:
void ftpDone(bool error);
private:
};
#endif
#ifndef FTPGET_H
#define FTPGET_H
#include <QFile>
#include <QFtp>
class QUrl;
class FtpGet : public QObject
{
Q_OBJECT
public:
FtpGet(QObject *parent = 0);
bool getFile(const QUrl &url);
signals:
void done();
private slots:
void ftpDone(bool error);
private:
QFtp ftp;
QFile file;
};
#endif
To copy to clipboard, switch view to plain text mode
#include <QtCore>
#include <QtNetwork>
#include <iostream>
#include "ftpget.h"
using namespace std;
{
connect(&ftp, SIGNAL(done(bool)), this, SLOT(ftpDone(bool)));
}
bool FtpGet
::getFile(const QUrl &url
) {
if (!url.isValid()) {
cerr << "Error: Invalid URL" << endl;
return false;
}
if (url.scheme() != "ftp") {
cerr << "Error: URL must start with 'ftp:'" << endl;
return false;
}
if (url.path().isEmpty()) {
cerr << "Error: URL has no path" << endl;
return false;
}
if (localFileName.isEmpty())
localFileName = "ftpget.out";
file.setFileName(localFileName);
cerr << "Error: Cannot open " << qPrintable(file.fileName())
<< " for writing: " << qPrintable(file.errorString())
<< endl;
return false;
}
ftp.connectToHost(url.host(), url.port(21));
ftp.login();
ftp.get(url.path(), &file);
ftp.close();
return true;
}
void FtpGet::ftpDone(bool error)
{
if (error) {
cerr << "Error: " << qPrintable(ftp.errorString()) << endl;
} else {
cerr << "File downloaded as " << qPrintable(file.fileName())
<< endl;
}
file.close();
emit done();
}
#include <QtCore>
#include <QtNetwork>
#include <iostream>
#include "ftpget.h"
using namespace std;
FtpGet::FtpGet(QObject *parent)
: QObject(parent)
{
connect(&ftp, SIGNAL(done(bool)), this, SLOT(ftpDone(bool)));
}
bool FtpGet::getFile(const QUrl &url)
{
if (!url.isValid()) {
cerr << "Error: Invalid URL" << endl;
return false;
}
if (url.scheme() != "ftp") {
cerr << "Error: URL must start with 'ftp:'" << endl;
return false;
}
if (url.path().isEmpty()) {
cerr << "Error: URL has no path" << endl;
return false;
}
QString localFileName = QFileInfo(url.path()).fileName();
if (localFileName.isEmpty())
localFileName = "ftpget.out";
file.setFileName(localFileName);
if (!file.open(QIODevice::WriteOnly)) {
cerr << "Error: Cannot open " << qPrintable(file.fileName())
<< " for writing: " << qPrintable(file.errorString())
<< endl;
return false;
}
ftp.connectToHost(url.host(), url.port(21));
ftp.login();
ftp.get(url.path(), &file);
ftp.close();
return true;
}
void FtpGet::ftpDone(bool error)
{
if (error) {
cerr << "Error: " << qPrintable(ftp.errorString()) << endl;
} else {
cerr << "File downloaded as " << qPrintable(file.fileName())
<< endl;
}
file.close();
emit done();
}
To copy to clipboard, switch view to plain text mode
#include <QtCore>
#include <iostream>
#include "ftpget.h"
using namespace std;
int main(int argc, char *argv[])
{
if (args.count() != 2) {
cerr << "Usage: ftpget url" << endl
<< "Example:" << endl
<< " ftpget ftp://ftp.trolltech.com/mirrors" << endl;
return 1;
}
FtpGet getter;
if (!getter.
getFile(QUrl(args
[1]))) return 1;
QObject::connect(&getter,
SIGNAL(done
()),
&app,
SLOT(quit
()));
return app.exec();
}
#include <QtCore>
#include <iostream>
#include "ftpget.h"
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QStringList args = app.arguments();
if (args.count() != 2) {
cerr << "Usage: ftpget url" << endl
<< "Example:" << endl
<< " ftpget ftp://ftp.trolltech.com/mirrors" << endl;
return 1;
}
FtpGet getter;
if (!getter.getFile(QUrl(args[1])))
return 1;
QObject::connect(&getter, SIGNAL(done()), &app, SLOT(quit()));
return app.exec();
}
To copy to clipboard, switch view to plain text mode
This is an example application from one of the QT devoted books.
It runs smoothly when the user is on-line.
The problem arises when the user has some connection problems.
Try to run the application while off-line.
The console becomes blocked and it will be until we connect ourselves to web or kill the process.
My question:
How to make our application quit with a certain warning for example after 10 seconds of unsuccessful trials to connect to the web?
PS. I tried to use QFtp::abort() with QTimer but it did not work!
Bookmarks