Originally Posted by
fullmetalcoder
The impossibility to block the ftp transfer is indeed an issue I overlooked which renders QThreadPool useless. However it implies that you don't need to create a thread per connection as QFtp does everything asynchronously anyway. Instead a single thread sending the get commands and waiting for them to finish (storing (command id, file name) pairs for further processing) would do.
Also I am quite sure the above code will fail as the QFile to which you want to write the data pumped from QFtp does not point anywhere and is not opened in write mode.
Thanks again. I have a solution due to your suggestion. One thing I did find out is that it does not work to call QFtp::get() from within the run() method of a QThread as while the loop in the run() method is executing the signals from QFtp are apparently not received by my class derived from QThread (as far as I can see anyways).
I've posted my (possibly not the best) solution below, in case anyone runs into the same problem. I simply run my downloader as a regular class and when a get() command finishes I launch the next ftp request. As files finish I can notify my data processing class by emitting a signal from my Downloader class (not shown).
void FileDownloader::getNext() {
//m_files is a QString vector holding all the filenames to download.
//m_cur_file is a QFile* declared as a class variable.
//m_counter is an int declared as a class variable.
if(m_counter == m_files.size()) {
emit isDone();
return;
}
else {
m_cur_file
= new QFile(m_files.
at(m_counter
++));
if( !m_cur_file
->open
(QIODevice::WriteOnly)) { qDebug() << "Unable to open " << m_cur_file->fileName();
}
else {
m_ftp->get(filename, m_cur_file);
}
}
}
void FileDownloader::commandFinished(int commandId, bool error) {
....
if (m_ftp
->currentCommand
() == QFtp::Get) { if (error ) {
qDebug() << "Error downloading " << m_cur_file->fileName() << "\n";
m_cur_file->close();
m_cur_file->remove();
}
else {
m_cur_file->close();
}
delete m_cur_file;
m_cur_file = 0;
getNext(); //Now fetch the next file - getNext() stops the process
//once all files are received.
}
}
void FileDownloader::getNext() {
//m_files is a QString vector holding all the filenames to download.
//m_cur_file is a QFile* declared as a class variable.
//m_counter is an int declared as a class variable.
if(m_counter == m_files.size()) {
emit isDone();
return;
}
else {
m_cur_file = new QFile(m_files.at(m_counter++));
if( !m_cur_file->open(QIODevice::WriteOnly)) {
qDebug() << "Unable to open " << m_cur_file->fileName();
}
else {
m_ftp->get(filename, m_cur_file);
}
}
}
void FileDownloader::commandFinished(int commandId, bool error) {
....
if (m_ftp->currentCommand() == QFtp::Get) {
if (error ) {
qDebug() << "Error downloading " << m_cur_file->fileName() << "\n";
m_cur_file->close();
m_cur_file->remove();
}
else {
m_cur_file->close();
}
delete m_cur_file;
m_cur_file = 0;
getNext(); //Now fetch the next file - getNext() stops the process
//once all files are received.
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks