PDA

View Full Version : I don't receive QFtp::done(bool) signal when off-line, I can't QFtp::abort() its task



mtrpoland
23rd September 2007, 12:54
Hi everybody,
after having two-week-lasting holiday I came back to continue my small project.
My application is intended to download a small file at startup depending on url given in settings. If no settings are present, there is a hard-coded link.

Everything works fine if we are on-line. The problem grows when we start my application while off-line.

The method below is called in constructor:

bool MyWidget::DownloadData() {
settings = new QSettings("MTR","MTR::Mastermind 1.0 PL");
if(settings->value("conciseDataURL") == QVariant())
settings->setValue("conciseDataURL", QVariant("ftp://ftp*********************"));
else
settings->setValue("conciseDataURL", settings->value("conciseDataURL"));

QUrl urlek(settings->value("conciseDataURL").toString()); //we posses an url to the file with data

bool errorUrl = false;
if(!urlek.isValid()) errorUrl = true;
if(urlek.scheme() != "ftp") errorUrl = true;
if(urlek.path().isEmpty()) errorUrl = true;
if(errorUrl) return false;

file.setFileName(QString("name.ofFile"));
if(!file.open(QIODevice::WriteOnly)) return false;

connect(&ftp, SIGNAL(done(bool)),this,SLOT(downloadingDone(bool) ));

ftp.connectToHost(urlek.host(), urlek.port(21));
ftp.login();
ftp.get(urlek.path(), &file);
ftp.close();

return true;
}

If we are off-line, there should be emitted done(bool) signal with its argument of type bool equal true.

Here you have body of downloadingDone(bool) slot:

void MyWidget::downloadingDone(bool weveGotError) {
file.close(); // we close device as our download has finished (either succedded or failed)
if(weveGotError) return; // if we failed, we stop our code

file.open(QIODevice::ReadOnly);
QDataStream in(&file);
int magicNumber;
QString host;
QString login;
QString password;
in >> magicNumber >> host >> login >> password;
file.close();
//usuwanie pliku
file.remove();
DownloadFromDatabase(host,login,password);
}

Then DownloadFromDatabase() :

bool MyWidget::DownloadFromDatabase(const QString &host,const QString &login,const QString &password) {
if(!createConnection(host,login,password)) return false;
QSqlQuery query;
query.exec("SELECT mGR_secsOfPlay, mGR_timeAndDate, mGR_playername FROM mastermindGlobalResults ORDER BY mGR_secsOfPlay ASC");
rekordyGLobalne.clear();
while(query.next()) {
MTR::Record r(query.value(2).toString(),query.value(0).toUInt( ),query.value(1).toDateTime());
rekordyGLobalne.append(r);
}
return true;
}

Then


bool MyWidget::createConnection(const QString &host,const QString &login,const QString &password) {
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName(host);
db.setDatabaseName("********");
db.setUserName(login);
db.setPassword(password);
if(!db.open()) return false;

return true;
}

If I close my application while being off-line all the time the application runs, the program freezes. In other words it stops responding to user interactions and does not dissapear. What's more, all the time the console output is blocked!

Here you have destructor:

MyWidget::~MyWidget() {
ftp.abort();
QVariant var(packRecords(rekordyLokalne));
settings->setValue("rekordyLokalne",var);
settings->sync();
}