QFtp using trouble. File used by another program.



  • Hi everyone.
    I use the following class to download files. Yes, this is from book of Jasmin Blanchette =)

    Spider.h
    @#ifndef SPIDER_H
    #define SPIDER_H

    #include <QtNetwork/QFtp>
    #include <QStringList>

    class QFile;

    class Spider : public QObject
    {
    Q_OBJECT

    public:
    Spider(QObject *parent = 0);

    bool getDirectory(const QUrl &url, const QString savePath);

    signals:
    void done();

    private slots:
    void ftpDone(bool error);
    void ftpListInfo(const QUrlInfo &urlInfo);

    private:
    void processNextDirectory();

    QFtp ftp;
    QList<QFile *> openedFiles;
    QString currentDir;
    QString currentLocalDir;
    QStringList pendingDirs;
    QString savePath;
    };

    #endif
    @

    Spider.cpp
    @#include <QtCore>
    #include <QtNetwork>
    #include <iostream>

    #include "spider.h"

    Spider::Spider(QObject *parent)
    : QObject(parent)
    {
    connect(&ftp, SIGNAL(done(bool)), this, SLOT(ftpDone(bool)));
    connect(&ftp, SIGNAL(listInfo(const QUrlInfo &)),
    this, SLOT(ftpListInfo(const QUrlInfo &)));
    }

    bool Spider::getDirectory(const QUrl &url, const QString savePath)
    {
    this->savePath = savePath;
    if (!url.isValid()) {
    std::cerr << "Error: Invalid URL" << std::endl;
    return false;
    }

    if (url.scheme() != "ftp") {
    std::cerr << "Error: URL must start with 'ftp:'" << std::endl;
    return false;
    }

    ftp.connectToHost(url.host(), url.port(21));
    ftp.login();

    QString path = url.path();
    if (path.isEmpty())
    path = "/";

    pendingDirs.append(path);
    processNextDirectory();

    return true;
    }

    void Spider::ftpDone(bool error)
    {
    if (error) {
    std::cerr << "Error: " << qPrintable(ftp.errorString())
    << std::endl;
    } else {
    std::cout << "Downloaded " << qPrintable(currentDir) << " to "
    << qPrintable(QDir::toNativeSeparators(
    QDir(currentLocalDir).canonicalPath())) << std::endl;
    }

    qDeleteAll(openedFiles);
    openedFiles.clear();

    processNextDirectory();
    }

    void Spider::ftpListInfo(const QUrlInfo &urlInfo)
    {
    if (urlInfo.isFile()) {
    if (urlInfo.isReadable()) {
    QFile *file = new QFile(currentLocalDir + "/"
    + urlInfo.name());

    if (!file->open(QIODevice::WriteOnly)) {
    std::cerr << "Warning: Cannot write file "
    << qPrintable(QDir::toNativeSeparators(
    file->fileName()))
    << ": " << qPrintable(file->errorString())
    << std::endl;
    return;
    }

    ftp.get(urlInfo.name(), file);
    openedFiles.append(file);
    }
    } else if (urlInfo.isDir() && !urlInfo.isSymLink()) {
    pendingDirs.append(currentDir + "/" + urlInfo.name());
    }
    }

    void Spider::processNextDirectory()
    {
    if (!pendingDirs.isEmpty()) {
    currentDir = pendingDirs.takeFirst();
    currentLocalDir = savePath + currentDir;
    qDebug() << "load to:" << currentLocalDir;
    QDir(".").mkpath(currentLocalDir);

    ftp.cd(currentDir);
    ftp.list();
    } else {
    emit done();
    }
    }
    @

    If the server does not open the file in a program, then it is successfully downloaded.
    If the file is opened for writing, the program gives an error "The process cannot access the bile because it is being used by another process", but then the file can be copied!


  • Moderators

    welcome to devnet

    I am wondering, if this is really a problem of the program you posted above.
    If the server at the other end cannot access a file and, therefore, will not send this file, there is nothing you can do with your client.

    [edit, koahnig]



  • Thanks.
    After your reply i again checked the server settings and i found out that this is a server side problem. Files opened, for example, in notepad, successfully downloaded, but files opened in MS Word, not. I can't download files not through Total Commnder or through browser or through Explorer or through Qt program. So, MS Word locks file when open it for editing and there's nothing to do.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.