<commit_before>#include "updater.h"
#include "platform.h"

#ifdef Q_OS_LINUX
#include "archives/gzipreadfilter.h"
#include "archives/tarextractor.h"
#else
#include "archives/zipextractor.h"
#endif

#include <QBuffer>
#include <QCryptographicHash>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMessageBox>
#include <QNetworkRequest>
#include <QProcess>
#include <QStringBuilder>
#include <QTemporaryFile>
#include <QUrlQuery>

Updater::Updater(QObject *parent) : QObject(parent), url(QStringLiteral("https://openrct2.org/altapi/"))
{
    connect(&net, &QNetworkAccessManager::sslErrors, [](QNetworkReply * reply, const QList<QSslError> & errors){Q_UNUSED(reply); qDebug() << errors;});
}

void Updater::download() {
    if (update != nullptr) update->abort(), update = nullptr;
    if (api != nullptr) api->abort(), api = nullptr;
    if (bundle != nullptr) bundle->abort(), bundle = nullptr;

    QUrlQuery query;
    query.addQueryItem(QStringLiteral("command"), QStringLiteral("get-latest-download"));
    query.addQueryItem(QStringLiteral("flavourId"), QStringLiteral(OPENRCT2_FLAVOR));

    QVariant stableVar = settings.value(QStringLiteral("stable"));
    bool stable = stableVar.isValid() && stableVar.toBool();
    query.addQueryItem(QStringLiteral("gitBranch"), stable ? QStringLiteral("master") : QStringLiteral("develop"));

    url.setQuery(query.query());

    QNetworkRequest urequest(QStringLiteral("https://api.github.com/repos/LRFLEW/OpenRCT2Launcher/releases/latest"));
    update = net.get(urequest);
    connect(update, &QNetworkReply::finished, this, &Updater::receivedUpdate);

    QNetworkRequest request(url);
    api = net.get(request);
    connect(api, &QNetworkReply::finished, this, &Updater::receivedAPI);
}

void Updater::receivedUpdate() {
    if (update->error() != QNetworkReply::NoError) {
        //emit error(update->errorString());
        return;
    }

    QByteArray data = update->readAll();
    update->close();
    update->deleteLater();
    update = nullptr;

    QJsonDocument response = QJsonDocument::fromJson(data);
    QJsonObject robj = response.object();
    QString tag = robj[QStringLiteral("tag_name")].toString();

    if (tag.startsWith('v')) {
        QStringRef ver = tag.midRef(1);
        if (ver.compare(QLatin1String(APP_VERSION)) > 0) {
            // Show Update Notification
            QMessageBox msg;
            msg.setText(QStringLiteral("<font size=4>") % tr("New Update is Available") % QStringLiteral("</font>"));
            msg.setInformativeText(
                        QStringLiteral("<font size=4 style=\"white-space:nowrap;\" align=center><p>") %
                        tr("There is a new update available for the launcher.") %
                        QStringLiteral("<br />") %
                        tr("It is recomended that you update.") %
                        QStringLiteral("</p><p>") %
                        tr("Installed Version: ") %
                        QStringLiteral(APP_VERSION) %
                        QStringLiteral("<br />") %<commit_msg>Added qDebug for api.github.com errors to aid future debugging<commit_after>#include "updater.h"
#include "platform.h"

#ifdef Q_OS_LINUX
#include "archives/gzipreadfilter.h"
#include "archives/tarextractor.h"
#else
#include "archives/zipextractor.h"
#endif

#include <QBuffer>
#include <QCryptographicHash>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMessageBox>
#include <QNetworkRequest>
#include <QProcess>
#include <QStringBuilder>
#include <QTemporaryFile>
#include <QUrlQuery>

Updater::Updater(QObject *parent) : QObject(parent), url(QStringLiteral("https://openrct2.org/altapi/"))
{
    connect(&net, &QNetworkAccessManager::sslErrors, [](QNetworkReply * reply, const QList<QSslError> & errors){Q_UNUSED(reply); qDebug() << errors;});
}

void Updater::download() {
    if (update != nullptr) update->abort(), update = nullptr;
    if (api != nullptr) api->abort(), api = nullptr;
    if (bundle != nullptr) bundle->abort(), bundle = nullptr;

    QUrlQuery query;
    query.addQueryItem(QStringLiteral("command"), QStringLiteral("get-latest-download"));
    query.addQueryItem(QStringLiteral("flavourId"), QStringLiteral(OPENRCT2_FLAVOR));

    QVariant stableVar = settings.value(QStringLiteral("stable"));
    bool stable = stableVar.isValid() && stableVar.toBool();
    query.addQueryItem(QStringLiteral("gitBranch"), stable ? QStringLiteral("master") : QStringLiteral("develop"));

    url.setQuery(query.query());

    QNetworkRequest urequest(QStringLiteral("https://api.github.com/repos/LRFLEW/OpenRCT2Launcher/releases/latest"));
    update = net.get(urequest);
    connect(update, &QNetworkReply::finished, this, &Updater::receivedUpdate);

    QNetworkRequest request(url);
    api = net.get(request);
    connect(api, &QNetworkReply::finished, this, &Updater::receivedAPI);
}

void Updater::receivedUpdate() {
    if (update->error() != QNetworkReply::NoError) {
        // Don't emit, so the error is mostly silent
        qDebug() << update->errorString();
        return;
    }

    QByteArray data = update->readAll();
    update->close();
    update->deleteLater();
    update = nullptr;

    QJsonDocument response = QJsonDocument::fromJson(data);
    QJsonObject robj = response.object();
    QString tag = robj[QStringLiteral("tag_name")].toString();

    if (tag.startsWith('v')) {
        QStringRef ver = tag.midRef(1);
        if (ver.compare(QLatin1String(APP_VERSION)) > 0) {
            // Show Update Notification
            QMessageBox msg;
            msg.setText(QStringLiteral("<font size=4>") % tr("New Update is Available") % QStringLiteral("</font>"));
            msg.setInformativeText(
                        QStringLiteral("<font size=4 style=\"white-space:nowrap;\" align=center><p>") %
                        tr("There is a new update available for the launcher.") %
                        QStringLiteral("<br />") %
                        tr("It is recomended that you update.") %
                        QStringLiteral("</p><p>") %
                        tr("Installed Version: ") %
                        QStringLiteral(APP_VERSION) %
                        QStringLiteral("<br />") %
{'fraction_non_alphanumeric': 0.1488621997471555, 'fraction_numerical': 0.0025284450063211127, 'mean_word_length': 3.5630857966834895, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 65, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7094739', 'n_tokens_mistral': 1897, 'n_tokens_neox': 1880, 'n_words': 370}