Поймать ошибку неверной ссылки QNetworkAccessManager - PullRequest
0 голосов
/ 06 июня 2018

Как отловить неверные ошибки ссылок?Например:

ftp: //cddis.gsfc.nasa.gov/pub/slr/data/npt_crd/gracea/2010/gracea_20100101.npt.Z - этоневерная ссылка

Почему не работает сигнал ошибки?Или как правильно определить ссылки?Пожалуйста, напишите пример правильной работы с ошибками.

screen

#ifndef WIDGET_H
#define WIDGET_H

#include "Downloader.h"
#include <QWidget>
#include <QNetworkReply>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void onDownloadButtonClicked();

    void onSelectTargetFolderButtonClicked();

    void onCancelButtonClicked();

    void onUpdateProgress(qint64 bytesReceived, qint64 bytesTotal);
    void onResult(QNetworkReply *reply);
private:
    Ui::Widget *ui;
    Downloader m_downloader;
};

#endif // WIDGET_H

Widget.cpp

#include "Widget.h"
#include "ui_Widget.h"

#include <QFileDialog>
#include <QStandardPaths>
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    connect(ui->downloadPushButton, &QPushButton::clicked, this, &Widget::onDownloadButtonClicked);
    connect(ui->selectTargetFolderPushButton, &QPushButton::clicked, this, &Widget::onSelectTargetFolderButtonClicked);
    connect(ui->cancelPushButton, &QPushButton::clicked, this, &Widget::onCancelButtonClicked);
    connect(&m_downloader, &Downloader::updateDownloadProgress, this, &Widget::onUpdateProgress);
    connect(&m_downloader.manager, &QNetworkAccessManager::finished, this, &Widget::onResult);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::onDownloadButtonClicked()
{
    m_downloader.get(ui->targetFolderLineEdit->text(), ui->urlLineEdit->text());
}

void Widget::onSelectTargetFolderButtonClicked()
{
    QString targetFolder = QFileDialog::getExistingDirectory(this,
                tr("Select folder"),
                QStandardPaths::writableLocation(QStandardPaths::DownloadLocation),
                QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
    ui->targetFolderLineEdit->setText(targetFolder);
}

void Widget::onCancelButtonClicked()
{
    m_downloader.cancelDownload();
    ui->downloadProgressBar->setMaximum(100);
    ui->downloadProgressBar->setValue(0);
}

void Widget::onUpdateProgress(qint64 bytesReceived, qint64 bytesTotal)
{
    ui->downloadProgressBar->setMaximum(bytesTotal);
    ui->downloadProgressBar->setValue(bytesReceived);
}

void Widget::onResult(QNetworkReply *reply)
{
    if (reply->error()) {
        qDebug() << "Error";
        qDebug() << reply->error();
    } else {
        qDebug() << "Done";
    }
}

Downloader.h

#ifndef DOWNLOADER_H
#define DOWNLOADER_H

#include <QNetworkAccessManager>
#include <QNetworkReply>
//class QNetworkReply;
class QFile;

class Downloader : public QObject
{
    Q_OBJECT
    using BaseClass = QObject;

public:
    explicit Downloader(QObject* parent = nullptr);

    bool get(const QString& targetFolder, const QUrl& url);

    QNetworkReply* currentReply {nullptr};
    QNetworkAccessManager manager;
    QString fullName;
public slots:
    void cancelDownload();

signals:
    void updateDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);

private slots:
    void onReadyRead();
    void onReply(QNetworkReply* reply);
    void errorSlot(QNetworkReply::NetworkError er);
private:
    QFile* m_file                 {nullptr};
};

#endif // DOWNLOADER_H

Downloader.cpp

#include "Downloader.h"
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QFile>
#include <QDir>
Downloader::Downloader(QObject* parent) :
    BaseClass(parent)
{
    connect(&manager, &QNetworkAccessManager::finished, this, &Downloader::onReply);
}
bool Downloader::get(const QString& targetFolder, const QUrl& url)
{
    qDebug() << "get";
    if (targetFolder.isEmpty() || url.isEmpty())
    {
        return false;
    }
    fullName = targetFolder + QDir::separator() + url.fileName();
    m_file = new QFile(fullName);
    if (!m_file->open(QIODevice::WriteOnly))
    {
        delete m_file;
        m_file = nullptr;
        return false;
    }
    QNetworkRequest request(url);
    request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
    currentReply = manager.get(request);
    connect(currentReply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),
                this, &Downloader::errorSlot);
    connect(currentReply, &QNetworkReply::readyRead, this, &Downloader::onReadyRead);
    connect(currentReply, &QNetworkReply::downloadProgress, this, &Downloader::updateDownloadProgress);
    return true;
}
void Downloader::onReadyRead()
{
    qDebug() << "onReadyRead";
    if (m_file)
    {
        m_file->write(currentReply->readAll());
    }
}
void Downloader::cancelDownload()
{
    qDebug() << "cancelDownload";
    if (currentReply)
    {
        currentReply->abort();
    }
}
void Downloader::onReply(QNetworkReply* reply)
{
    qDebug() << "onReply";
    if (reply->error() == QNetworkReply::NoError)
    {
        m_file->flush();
        m_file->close();
    }
    else
    {
        m_file->remove();
    }
    delete m_file;
    m_file = nullptr;
    reply->deleteLater();
}
void Downloader::errorSlot(QNetworkReply::NetworkError er)
{
    qDebug() << er;
}
...