Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFtp using trouble. File used by another program.
Forum Updated to NodeBB v4.3 + New Features

QFtp using trouble. File used by another program.

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shaedar
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      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]

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Shaedar
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved