14 #include <fc_config.h>
21 #include <QJsonDocument>
22 #include <QNetworkAccessManager>
23 #include <QNetworkReply>
24 #include <QNetworkRequest>
45 auto manager = std::make_unique<QNetworkAccessManager>();
48 auto request = QNetworkRequest(url);
49 request.setHeader(QNetworkRequest::UserAgentHeader,
51 request.setAttribute(QNetworkRequest::RedirectPolicyAttribute,
52 QNetworkRequest::NoLessSafeRedirectPolicy);
54 auto *reply = manager->get(request);
60 QObject::connect(reply, &QNetworkReply::readyRead, [&]() {
61 while (reply->bytesAvailable() > 0) {
62 out->write(reply->read(1 << 20));
67 QObject::connect(reply, &QNetworkReply::finished, [&] {
68 if (reply->error() != QNetworkReply::NoError) {
75 auto msg = QString::fromUtf8(_(
"Failed to fetch %1: %2"))
76 .arg(url.toDisplayString())
77 .arg(reply->errorString());
84 manager->deleteLater();
101 buffer.open(QIODevice::WriteOnly);
106 QJsonParseError error;
107 auto document = QJsonDocument::fromJson(buffer.data(), &error);
108 if (error.error != QJsonParseError::NoError) {
109 cb(QString::fromUtf8(
_(
"Error parsing JSON: %1"))
110 .arg(error.errorString()));
111 return QJsonDocument();
116 return QJsonDocument();
125 buffer.open(QIODevice::WriteOnly);
142 QSaveFile out(QString::fromUtf8(filename));
143 if (!out.open(QIODevice::WriteOnly)) {
145 auto msg = QString::fromUtf8(
_(
"Could not open %1 for writing"))
#define fc_assert_ret_val(condition, val)
section_file * netfile_get_section_file(const QUrl &url, const nf_errmsg &cb)
Fetch section file from net.
bool netfile_download_file(const QUrl &url, const char *filename, const nf_errmsg &cb)
Fetch file from given URL and save as given filename.
static bool netfile_download_file_core(const QUrl &url, QIODevice *out, const nf_errmsg &cb)
Fetch file from given URL to given file stream.
QJsonDocument netfile_get_json_file(const QUrl &url, const nf_errmsg &cb)
Fetch a JSON file from the net.
std::function< void(const QString &message)> nf_errmsg
struct section_file * secfile_from_stream(QIODevice *stream, bool allow_duplicates)
Create a section file from a stream.
const char * freeciv21_version()
Returns the raw version string.