Tamas Toth
Tamas Toth

Reputation: 359

how to use QFtp::list and QFtp::listInfo to check if directory exists

I try to make ftp uploader which creates automatically subdirectories and put files in them.

My problem is that:

I know QFtp have list() and listInfo() but I don't understand how I should use in my code.

Here is how I implemented my uploader:

ftp = new QFtp();
ftp->setTransferMode(QFtp::Passive);

ftp->connectToHost(g.ftp_host);
ftp->login(g.ftp_user,g.ftp_password);
ftp->list();
ftp->cd(g.ftp_defaultdir+g.phocadir);

for (int i = 0; i<FulluploadFilenames->size(); i++){
    ftp->mkdir(_taj->at(i)); // should check if exists
    ftp->cd(_taj->at(i));
    ftp->mkdir(_year->at(i));
    ftp->cd(_year->at(i));
    ftp->mkdir(_month->at(i));
    ftp->cd(_month->at(i));

    qdata = new QFile(FulluploadFilenames->at(i),this);
    if (qdata->open(QIODevice::ReadOnly)) {
        ftp->put(qdata,uploadFilenames->at(i));
        ftp->list();
    }

    ftp->cd("../");
    ftp->cd("../");
    ftp->cd("../");
}
ftp->close();

This make and cd to something like this: /123123123/2015/05/ and uploads file here.

Could somebody please help how to check if directory exists?

UPDATE:

Instead of checking if directories exists I forced mkdir to work in sync so it doesn't matter if fails.

void MainWindow::doUpload(){ 
ftp = new QFtp();
ftp->connectToHost(g.ftp_host);
ftp->login(g.ftp_user,g.ftp_password);
ftp->cd(g.ftp_defaultdir+g.phocadir);
ftp->list();
// making directories
for (int i = 0; i<FulluploadFilenames->size(); i++){
    runCommand(ftp, ftp->mkdir(_taj->at(i)));
    runCommand(ftp, ftp->mkdir(_taj->at(i)+"/"+_year->at(i)));
    runCommand(ftp, ftp->mkdir(_taj->at(i)+"/"+_year->at(i)+"/"+_month->at(i)));
}
// uploading files
for (int i = 0; i<FulluploadFilenames->size(); i++){
    ftp->cd(_taj->at(i)+"/"+_year->at(i)+"/"+_month->at(i));

    qdata = new QFile(FulluploadFilenames->at(i),this);
    if (qdata->open(QIODevice::ReadOnly)) {
        ftp->put(qdata,uploadFilenames->at(i));
    }

    ftp->cd("../../../");

    ui->progressBar->setValue(i+1);
    ui->progressBar->update();
    QCoreApplication::processEvents();
}
ftp->close();
}

...

void MainWindow::runCommand(QFtp *ftp, int commandId){
QEventLoop loop;
connect(ftp, SIGNAL(commandFinished(int, bool)), &loop, SLOT(quit()));
loop.exec();
}

Upvotes: 0

Views: 1883

Answers (1)

Andrei Shikalev
Andrei Shikalev

Reputation: 732

Function list() work asynchronously. After you call this function it immediately returns unique identifier. This id passed in commandStarted() signal when command started and in commandFinished() signal when list command finished. Between this two signals listInfo() signal is emitted for each directory entry found.

So before upload to directory you must check all QUrlInfo objects, passed from listInfo() signals until commandFinished() signal emitted.

Upvotes: 1

Related Questions