Krita Source Code Documentation
Loading...
Searching...
No Matches
KisRemoteFileFetcher.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2016 Boudewijn Rempt <boud@valdyas.org>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 *
6 */
8
9#include <QApplication>
10#include <QDebug>
11#include <QMessageBox>
12#include <QNetworkAccessManager>
13#include <QNetworkReply>
14#include <QNetworkRequest>
15#include <QProgressDialog>
16
17#include <klocalizedstring.h>
18
20
22 : QObject(parent)
23 , m_request(nullptr)
24 , m_reply(nullptr)
25{
26}
27
33
34bool KisRemoteFileFetcher::fetchFile(const QUrl &remote, QIODevice *io)
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}
108
109QByteArray KisRemoteFileFetcher::fetchFile(const QUrl &remote)
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}
133
134void KisRemoteFileFetcher::error(QNetworkReply::NetworkError error)
135{
136 Q_UNUSED(error);
137
138 qDebug() << "error" << m_reply->errorString();
139}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
void error(QNetworkReply::NetworkError error)
QNetworkRequest * m_request
KisRemoteFileFetcher(QObject *parent=0)
bool fetchFile(const QUrl &remote, QIODevice *io)
fetch the image. Shows a progress dialog
int doNotAskAgainMessageBoxWrapper(QMessageBox *messageBox, const QString &identifier)
doNotAskAgainMessageBoxWrapper takes a messagebox and an identifier and adds a Do Not Ask Again check...