Hello.
I am trying to get a bunch of files from an ftp repo.
///
/// FTP stuff
///
ftp->connectToHost("moo.cow");
ftp
->setTransferMode
(QFtp::Passive);
ftp->login("foo", "bar");
ftp->list("/*.jpg");
//ftp.cd("img_art");
connect(ftp, SIGNAL(dataTransferProgress(qint64, qint64)),
this, SLOT(my_gestor_progreso(qint64, qint64)));
connect(ftp, SIGNAL(commandStarted(int)),
this, SLOT(my_empieza_comando_ftp(int)));
connect(ftp, SIGNAL(commandFinished(int,bool)),
this, SLOT(my_acaba_comando_ftp(int,bool)));
connect(ftp, SIGNAL(done(bool)),
this, SLOT(my_hecho_ftp(bool)));
connect(ftp, SIGNAL(stateChanged(int)),
this, SLOT(my_estado_cambiado_ftp(int)));
connect(ftp,
SIGNAL(listInfo
(QUrlInfo)),
this,
SLOT(my_list_list_ftp
(QUrlInfo)));
///
/// FTP stuff
///
ftp = new QFtp;
ftp->connectToHost("moo.cow");
ftp->setTransferMode(QFtp::Passive);
ftp->login("foo", "bar");
ftp->cd(QString("/"));
lista_archivos_ftp = new QStringList;
ftp->list("/*.jpg");
//ftp.cd("img_art");
connect(ftp, SIGNAL(dataTransferProgress(qint64, qint64)),
this, SLOT(my_gestor_progreso(qint64, qint64)));
connect(ftp, SIGNAL(commandStarted(int)),
this, SLOT(my_empieza_comando_ftp(int)));
connect(ftp, SIGNAL(commandFinished(int,bool)),
this, SLOT(my_acaba_comando_ftp(int,bool)));
connect(ftp, SIGNAL(done(bool)),
this, SLOT(my_hecho_ftp(bool)));
connect(ftp, SIGNAL(stateChanged(int)),
this, SLOT(my_estado_cambiado_ftp(int)));
connect(ftp, SIGNAL(listInfo(QUrlInfo)),
this, SLOT(my_list_list_ftp(QUrlInfo)));
To copy to clipboard, switch view to plain text mode
Besides the usual stuff, I have the following relevant methods:
void kooker::my_acaba_comando_ftp(int comando, bool error)
{
if(error)
{
// booo
}
else
{
if(ftp
->currentCommand
() == QFtp::List) on_btn_descargar_clicked();
}
}
void kooker
::my_list_list_ftp(QUrlInfo url
) {
my_print_log
(QString(Q_FUNC_INFO
).
append(url.
name()));
lista_archivos_ftp->append(url.name());
}
void kooker::on_btn_descargar_clicked()
{
qDebug() << Q_FUNC_INFO;
connect(ftp, SIGNAL(commandFinished(int,bool)),
&loop, SLOT(quit()));
foreach(item, *lista_archivos_ftp)
{
QString img_local_path
= QString(photoDir
->path
().
append("/%1").
arg(item
));
{
delete file;
return;
}
ftp->get(ftp_file, file);
loop.exec();
file->close();
}
}
void kooker::my_acaba_comando_ftp(int comando, bool error)
{
if(error)
{
// booo
}
else
{
if(ftp->currentCommand() == QFtp::List)
on_btn_descargar_clicked();
}
}
void kooker::my_list_list_ftp(QUrlInfo url)
{
my_print_log(QString(Q_FUNC_INFO).append(url.name()));
lista_archivos_ftp->append(url.name());
}
void kooker::on_btn_descargar_clicked()
{
qDebug() << Q_FUNC_INFO;
QEventLoop loop;
connect(ftp, SIGNAL(commandFinished(int,bool)),
&loop, SLOT(quit()));
QString item;
foreach(item, *lista_archivos_ftp)
{
QString img_local_path = QString(photoDir->path().append("/%1").arg(item));
QFile *file = new QFile(img_local_path);
if(!file->open(QIODevice::WriteOnly))
{
delete file;
return;
}
QString ftp_file = QString("/%1").arg(item);
ftp->get(ftp_file, file);
loop.exec();
file->close();
}
}
To copy to clipboard, switch view to plain text mode
Note that, despite its name, on_btn_descargar_clicked() is called when the ftp list command completes, and not via a button. The funniest part is that, sometimes, I get a single zero bytes jpg file, it seems to hang in QFtp::get forever since the loop doesn't quit(). But some other times, I get all the files, also with zero length.
So, please, can you tell me if you see anything wrong with the code I wrote? I am having a really difficult time with this one. I've been struggling with this for days. Oh, and before you ask, I have also tried the loop-less (AKA standard) version, like this, with the same results:
void kooker::on_btn_descargar_clicked()
{
qDebug() << Q_FUNC_INFO;
// QEventLoop loop;
// connect(ftp, SIGNAL(commandFinished(int,bool)),
// &loop, SLOT(quit()));
foreach(item, *lista_archivos_ftp)
{
QString img_local_path
= QString(photoDir
->path
().
append("/%1").
arg(item
));
ftp_local_file
= new QFile(img_local_path
);
if(!ftp_local_file
->open
(QIODevice::WriteOnly)) {
delete ftp_local_file;
return;
}
ftp->get(ftp_file, ftp_local_file);
// loop.exec();
// file->close();
}
}
void kooker::my_acaba_comando_ftp(int comando, bool error)
{
if(error)
{
// booooooo
}
else
{
if(ftp
->currentCommand
() == QFtp::List) on_btn_descargar_clicked();
if(ftp
->currentCommand
() == QFtp::Get || ftp
->currentCommand
() == QFtp::Put) ftp_local_file->close();
}
}
void kooker::on_btn_descargar_clicked()
{
qDebug() << Q_FUNC_INFO;
// QEventLoop loop;
// connect(ftp, SIGNAL(commandFinished(int,bool)),
// &loop, SLOT(quit()));
QString item;
foreach(item, *lista_archivos_ftp)
{
QString img_local_path = QString(photoDir->path().append("/%1").arg(item));
ftp_local_file = new QFile(img_local_path);
if(!ftp_local_file->open(QIODevice::WriteOnly))
{
delete ftp_local_file;
return;
}
QString ftp_file = QString("/%1").arg(item);
ftp->get(ftp_file, ftp_local_file);
// loop.exec();
// file->close();
}
}
void kooker::my_acaba_comando_ftp(int comando, bool error)
{
if(error)
{
// booooooo
}
else
{
if(ftp->currentCommand() == QFtp::List)
on_btn_descargar_clicked();
if(ftp->currentCommand() == QFtp::Get || ftp->currentCommand() == QFtp::Put)
ftp_local_file->close();
}
}
To copy to clipboard, switch view to plain text mode
In this case, I seem to always get the whole bunch of files, never a single one, but also with zero length.
Any tip is welcome, I am a bit stuck with this one
Bookmarks