PDA

View Full Version : QFtp with QThread issue



prasad.borkar
8th June 2011, 08:48
Hi,

I have writen a program which downloads files from particular server using QFtp.
But when I tried to make it multithreaded, its not working.
I am calling QFtp:Get method in QThread run() method but it fails to download.
Here is my sample code:

#include "ftp.h"
#include <iostream>

using namespace std;

ftp::ftp(QObject *parent) :
QThread(parent)
{
getDirectory();
connect(&vftp,SIGNAL(listInfo(QUrlInfo)),this,SLOT(ftpListI nfo(QUrlInfo)));
connect(&vftp,SIGNAL(done(bool)),this,SLOT(ftpdone(bool)));
connect(&vftp,SIGNAL(commandFinished(int,bool)),this,SLOT(c ommandDone(int,bool)));
}

//Connection code
void ftp::getDirectory(){
vftp.connectToHost("192.168.2.10",21);
vftp.login("user","pass");
QString path = ftpDetails.value(3); //some path
pendingDirs.append(path);
vftp.cd(path);
vftp.list();
}

void ftp::ftpListInfo(const QUrlInfo &urlInfo)
{
qDebug() << "Inside ftpListInfo";
qDebug() << "File name" << urlInfo.name();
if (urlInfo.isFile()) {

if (urlInfo.isReadable() & urlInfo.name().startsWith(ftpDetails.value(11))) {
fileNameList << urlInfo.name(); //Adding filenames into QStringList
}
}
}

void ftp::run()
{
QString name;
if(!fileNameList.size()>0) {
qDebug() << "fileNameList.size()=0";
return;
} else {
name = fileNameList.takeFirst();
}

QFile *file = new QFile(currentLocalDir + "/" + name);
filename = currentLocalDir + "/" + name;
qDebug() << "filename: " << filename;
if (!file->open(QIODevice::WriteOnly)) {
cerr << "Warning: Cannot open file "
<< qPrintable(
QDir::convertSeparators(file->fileName()))
<< endl;
log.writeLog("Warning: Cannot open file " + file->fileName());
return;
}


vftp.get(name, file);
qDebug() << name;
qDebug() << ftpDetails.value(4) + "/" + name;
vftp.rename(name,ftpDetails.value(4) + "/" + name);
openedFiles.append(file);

dbRecord.clear();
dbRecord << name << QString::number(file->size());
//dbRecord << urlInfo.lastModified().toString("yyyyMMddhhmmss") << "D" << "OK";
dbRecord << "20110606303300" << "D" << "OK";

if (!db.addEntry(dbRecord))
log.writeLog("Unable to insert record in FTP table");
}

void ftp::commandDone(int, bool error){

if(vftp.currentCommand() == QFtp::List) {
this->start();
}
}



I am listing all the files in QStringList and then I am starting a thread to download files one by one. But only the vftp.get() method is giving problem and not downloading the files. Is it that the QFtp get() method and QThread run() are conflicting each other, something like that.
Please help me on this.
Thanks in advance.

Santosh Reddy
8th June 2011, 09:05
prasad, please use [ Code]...[/Code] tags to post code, its really a pain to read the code.

Also try this quickly, (I have not read your complete code)


void ftp::run()
{
QString name;
if(!fileNameList.size()>0) {
qDebug() << "fileNameList.size()=0";
return;
} else {
name = fileNameList.takeFirst();
}
exec(); //Add this
}