#include "launcherupdates.h" #include "launcherversion.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef Q_OS_WIN #include #include #endif namespace { const QString origin = QStringLiteral("https://projects-live.galeonworx.com"); const QString repository = origin + QStringLiteral("/galeon/BiLauncher"); QNetworkRequest request(const QUrl &url) { QNetworkRequest result(url); result.setTransferTimeout(30000); result.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::SameOriginRedirectPolicy); return result; } auto versionKey(const QString &version) { qsizetype suffix = 0; const auto number = QVersionNumber::fromString(version, &suffix); const int candidate = version.mid(suffix + 3).toInt(); return std::pair(number, candidate); } QString assetName(const QString &version) { const auto prefix = QStringLiteral("BiLauncher-") + version; #if defined(Q_OS_WIN) return prefix + QStringLiteral("-win64-setup.exe"); #elif defined(Q_OS_MACOS) return prefix + QStringLiteral("-macOS-universal.dmg"); #elif defined(Q_OS_LINUX) && defined(Q_PROCESSOR_X86_64) return prefix + QStringLiteral("-x86_64.AppImage"); #else return {}; #endif } } // namespace LauncherUpdates::LauncherUpdates(QObject *parent) : QObject(parent) { } LauncherUpdates::~LauncherUpdates() = default; QString LauncherUpdates::channel() const { return QStringLiteral(LAUNCHER_CHANNEL); } void LauncherUpdates::refresh(const QString &channel) { if ((channel != "release" && channel != "rc") || m_states.value(channel) == "loading") return; m_states.insert(channel, QStringLiteral("loading")); emit catalogChanged(); fetchPage(channel, 1, {}); } void LauncherUpdates::fetchPage(const QString &channel, int page, QVariantList entries) { QUrl url(origin + QStringLiteral("/api/v1/repos/galeon/BiLauncher/releases")); QUrlQuery query; query.addQueryItem(QStringLiteral("draft"), QStringLiteral("false")); query.addQueryItem(QStringLiteral("pre-release"), channel == "rc" ? QStringLiteral("true") : QStringLiteral("false")); query.addQueryItem(QStringLiteral("page"), QString::number(page)); query.addQueryItem(QStringLiteral("limit"), QStringLiteral("50")); url.setQuery(query); auto *reply = m_network.get(request(url)); connect(reply, &QNetworkReply::finished, this, [this, reply, channel, page, entries = std::move(entries)]() mutable { reply->deleteLater(); const auto document = QJsonDocument::fromJson(reply->readAll()); if (reply->error() != QNetworkReply::NoError || !document.isArray()) { qWarning() << "BiLauncher release catalog:" << reply->errorString(); m_states.insert(channel, QStringLiteral("error")); emit catalogChanged(); return; } const auto releases = document.array(); static const QRegularExpression tagPattern(QStringLiteral("^v([0-9]+\\.[0-9]+\\.[0-9]+)(-rc[0-9]+)?$")); for (const auto &item : releases) { const auto release = item.toObject(); const auto tag = release.value("tag_name").toString(); const auto match = tagPattern.match(tag); const bool candidate = channel == "rc"; const bool candidateTag = !match.captured(2).isEmpty(); if (!match.hasMatch() || release.value("draft").toBool() || release.value("prerelease").toBool() != candidate || candidateTag != candidate) continue; const auto version = tag.mid(1); const auto name = assetName(version); const auto expectedUrl = repository + QStringLiteral("/releases/download/") + tag + u'/' + name; qint64 size = 0; for (const auto &item : release.value("assets").toArray()) { const auto asset = item.toObject(); if (!name.isEmpty() && asset.value("name").toString() == name && asset.value("browser_download_url").toString() == expectedUrl) { size = asset.value("size").toInteger(); break; } } entries.append(QVariantMap{ {"version", version}, {"date", QDateTime::fromString(release.value("published_at").toString(), Qt::ISODate)}, {"changelog", repository + QStringLiteral("/src/tag/") + tag + QStringLiteral("/CHANGELOG.md")}, {"download", size > 0 ? expectedUrl : QString()}, {"size", size}, }); } if (!releases.isEmpty()) { fetchPage(channel, page + 1, std::move(entries)); return; } std::sort(entries.begin(), entries.end(), [](const QVariant &a, const QVariant &b) { return versionKey(a.toMap().value("version").toString()) > versionKey(b.toMap().value("version").toString()); }); if (channel == this->channel()) { m_latest.clear(); for (const auto &item : std::as_const(entries)) { const auto entry = item.toMap(); const auto version = entry.value("version").toString(); if (!entry.value("download").toString().isEmpty() && versionKey(version) > versionKey(QStringLiteral(LAUNCHER_VERSION))) { m_latest = version; break; } } } m_catalogs.insert(channel, entries); m_states.insert(channel, QStringLiteral("ready")); emit catalogChanged(); }); } void LauncherUpdates::install(const QString &channel, const QString &version) { if (installing()) return; QVariantMap release; for (const auto &item : m_catalogs.value(channel).toList()) { if (item.toMap().value("version") == version) { release = item.toMap(); break; } } const QUrl url(release.value("download").toString()); if (url.isEmpty()) { emit installationFailed(); return; } m_temp = std::make_unique(); if (!m_temp->isValid()) { qWarning() << "BiLauncher update:" << m_temp->errorString(); m_temp.reset(); emit installationFailed(); return; } const QString path = m_temp->filePath(url.fileName()); m_download = std::make_unique(path); if (!m_download->open(QIODevice::WriteOnly)) { qWarning() << "BiLauncher update:" << m_download->errorString(); m_download.reset(); m_temp.reset(); emit installationFailed(); return; } const qint64 size = release.value("size").toLongLong(); auto *reply = m_network.get(request(url)); reply->setReadBufferSize(256 * 1024); const auto writeChunk = [this, reply, size] { const auto chunk = reply->readAll(); if (m_download->pos() + chunk.size() > size || m_download->write(chunk) != chunk.size()) { qWarning() << "BiLauncher update: failed to write installer"; return false; } return true; }; connect(reply, &QNetworkReply::readyRead, this, [reply, writeChunk] { if (!writeChunk()) reply->abort(); }); connect(reply, &QNetworkReply::finished, this, [this, reply, path, size, writeChunk] { reply->deleteLater(); const bool downloaded = reply->error() == QNetworkReply::NoError && writeChunk() && m_download->pos() == size && m_download->flush(); m_download.reset(); if (downloaded && startInstallation(path)) { #if defined(Q_OS_WIN) || defined(Q_OS_MACOS) m_temp->setAutoRemove(false); #endif m_temp.reset(); QCoreApplication::quit(); return; } qWarning() << "BiLauncher update failed:" << path << reply->errorString(); m_temp.reset(); emit installationChanged(); emit installationFailed(); }); emit installationChanged(); } bool LauncherUpdates::startInstallation(const QString &path) { #ifdef Q_OS_WIN const auto nativePath = QDir::toNativeSeparators(path).toStdWString(); return reinterpret_cast( ShellExecuteW(nullptr, L"open", nativePath.c_str(), nullptr, nullptr, SW_SHOWNORMAL)) > 32; #elif defined(Q_OS_MACOS) return QProcess::startDetached(QStringLiteral("/usr/bin/open"), {path}); #elif defined(Q_OS_LINUX) QString destination = qEnvironmentVariable("APPIMAGE"); if (destination.isEmpty()) { const QDir home = QDir::home(); if (!home.mkpath(QStringLiteral("Applications"))) return false; destination = home.filePath(QStringLiteral("Applications/BiLauncher.AppImage")); } else { destination = QFileInfo(destination).canonicalFilePath(); if (destination.isEmpty()) return false; } QFile source(path); QSaveFile installed(destination); if (!source.open(QIODevice::ReadOnly) || !installed.open(QIODevice::WriteOnly) || !installed.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner | QFileDevice::ReadGroup | QFileDevice::ExeGroup | QFileDevice::ReadOther | QFileDevice::ExeOther)) return false; while (!source.atEnd()) { const auto chunk = source.read(256 * 1024); if (chunk.isEmpty() || installed.write(chunk) != chunk.size()) return false; } return installed.commit(); #else return false; #endif }