Krita Source Code Documentation
Loading...
Searching...
No Matches
KisRemoteFileFetcher Class Reference

The KisRemoteFileFetcher class can fetch a remote file and blocks until the file is downloaded. More...

#include <KisRemoteFileFetcher.h>

+ Inheritance diagram for KisRemoteFileFetcher:

Public Member Functions

bool fetchFile (const QUrl &remote, QIODevice *io)
 fetch the image. Shows a progress dialog
 
 KisRemoteFileFetcher (QObject *parent=0)
 
 ~KisRemoteFileFetcher () override
 

Static Public Member Functions

static QByteArray fetchFile (const QUrl &remote)
 

Private Slots

void error (QNetworkReply::NetworkError error)
 

Private Attributes

QNetworkReply * m_reply
 
QNetworkRequest * m_request
 

Detailed Description

The KisRemoteFileFetcher class can fetch a remote file and blocks until the file is downloaded.

Definition at line 20 of file KisRemoteFileFetcher.h.

Constructor & Destructor Documentation

◆ KisRemoteFileFetcher()

KisRemoteFileFetcher::KisRemoteFileFetcher ( QObject * parent = 0)
explicit

Definition at line 21 of file KisRemoteFileFetcher.cpp.

22 : QObject(parent)
23 , m_request(nullptr)
24 , m_reply(nullptr)
25{
26}
QNetworkRequest * m_request

◆ ~KisRemoteFileFetcher()

KisRemoteFileFetcher::~KisRemoteFileFetcher ( )
override

Definition at line 28 of file KisRemoteFileFetcher.cpp.

29{
30 delete m_request;
31 delete m_reply;
32}

References m_reply, and m_request.

Member Function Documentation

◆ error

void KisRemoteFileFetcher::error ( QNetworkReply::NetworkError error)
privateslot

Definition at line 134 of file KisRemoteFileFetcher.cpp.

135{
136 Q_UNUSED(error);
137
138 qDebug() << "error" << m_reply->errorString();
139}
void error(QNetworkReply::NetworkError error)

References error(), and m_reply.

◆ fetchFile() [1/2]

QByteArray KisRemoteFileFetcher::fetchFile ( const QUrl & remote)
static

Definition at line 109 of file KisRemoteFileFetcher.cpp.

110{
111 QByteArray ba;
112 QEventLoop loop;
113
114 QNetworkAccessManager manager(nullptr);
115 connect(&manager, &QNetworkAccessManager::finished, &loop, &QEventLoop::quit);
116
117 QNetworkRequest *request = new QNetworkRequest(remote);
118 request->setRawHeader("User-Agent", QString("Krita-%1").arg(qApp->applicationVersion()).toUtf8());
119
120 QNetworkReply *reply = manager.get(*request);
121
122 loop.exec();
123
124 if (reply->error() != QNetworkReply::NoError) {
125 ba = reply->readAll();
126 }
127
128 reply->setParent(nullptr);
129
130 return ba;
131
132}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))

References connect().

◆ fetchFile() [2/2]

bool KisRemoteFileFetcher::fetchFile ( const QUrl & remote,
QIODevice * io )

fetch the image. Shows a progress dialog

Definition at line 34 of file KisRemoteFileFetcher.cpp.

35{
36 Q_ASSERT(!remote.isLocalFile());
37
38 if (remote.scheme() != "data") {
39 QMessageBox msgBox;
40 msgBox.setWindowTitle(i18nc("@title:window", "Krita"));
41 msgBox.setIcon(QMessageBox::Question);
42 msgBox.setText(i18nc("Fetching remote image",
43 "Do you want to download the image from %1?\nClick \"Show Details\" to view the full link "
44 "to the image.")
45 .arg(remote.host()));
46 msgBox.setDetailedText(remote.toDisplayString());
47 msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
48 msgBox.setDefaultButton(QMessageBox::No);
49 const int res = KisMessageBoxWrapper::doNotAskAgainMessageBoxWrapper(&msgBox, "KisRemoteFileFetcher");
50
51 if (res != QMessageBox::Yes) {
52 return false;
53 }
54 }
55
56 QNetworkAccessManager manager(this);
57 m_request = new QNetworkRequest(remote);
58 m_request->setRawHeader("User-Agent", QString("Krita-%1").arg(qApp->applicationVersion()).toUtf8());
59 m_reply = manager.get(*m_request);
60
61 QLocale loc;
62
63 QProgressDialog progress;
64 progress.setWindowTitle(i18nc("@title:window", "Krita"));
65 progress.setLabelText(i18nc("Fetching remote image", "Downloading image from %1...").arg(remote.host()));
66 progress.setMinimum(0);
67 progress.setMaximum(0);
68 progress.setWindowModality(Qt::ApplicationModal);
69 progress.setWindowFlag(Qt::CustomizeWindowHint, true);
70 progress.setWindowFlag(Qt::WindowCloseButtonHint, false);
71 connect(m_reply, &QNetworkReply::finished, &progress, &QProgressDialog::accept);
72 connect(m_reply, &QNetworkReply::errorOccurred, &progress, &QProgressDialog::cancel);
73 connect(m_reply, &QNetworkReply::downloadProgress, &progress, [&](const int ist, const int max) {
74 progress.setMaximum(max);
75 progress.setValue(ist);
76 progress.setLabelText(i18nc("Fetching remote image", "Downloading image from %1... (%2 / %3)")
77 .arg(remote.host())
78 .arg(loc.formattedDataSize(ist))
79 .arg(loc.formattedDataSize(max)));
80 });
81
82 connect(&progress, &QProgressDialog::canceled, m_reply, &QNetworkReply::abort);
83
84 progress.exec();
85
86 // avoid double free on manager destruction
87 m_reply->setParent(nullptr);
88
89 if (m_reply->error() != QNetworkReply::NoError) {
90 QMessageBox msgBox;
91 msgBox.setWindowTitle(i18nc("@title:window", "Krita"));
92 msgBox.setIcon(QMessageBox::Critical);
93 msgBox.setText(i18nc("Fetching remote image", "Could not download %1.").arg(remote.toDisplayString()));
94 msgBox.setDetailedText(m_reply->errorString());
95 msgBox.setDefaultButton(QMessageBox::Ok);
96 msgBox.exec();
97 return false;
98 }
99
100 if (!io->isOpen()) {
101 io->open(QIODevice::WriteOnly);
102 }
103 io->write(m_reply->readAll());
104 io->close();
105
106 return true;
107}
int doNotAskAgainMessageBoxWrapper(QMessageBox *messageBox, const QString &identifier)
doNotAskAgainMessageBoxWrapper takes a messagebox and an identifier and adds a Do Not Ask Again check...

References connect(), KisMessageBoxWrapper::doNotAskAgainMessageBoxWrapper(), m_reply, and m_request.

Member Data Documentation

◆ m_reply

QNetworkReply* KisRemoteFileFetcher::m_reply
private

Definition at line 38 of file KisRemoteFileFetcher.h.

◆ m_request

QNetworkRequest* KisRemoteFileFetcher::m_request
private

Definition at line 37 of file KisRemoteFileFetcher.h.


The documentation for this class was generated from the following files: