mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-05 09:33:25 -08:00
Preserve spoiler files and serialize update reloads
This commit is contained in:
parent
3a656c9c4a
commit
95f4e688e7
4 changed files with 401 additions and 210 deletions
|
|
@ -3,252 +3,385 @@
|
||||||
#include "../../../../main.h"
|
#include "../../../../main.h"
|
||||||
#include "../../../settings/cache_settings.h"
|
#include "../../../settings/cache_settings.h"
|
||||||
|
|
||||||
#include <QChar>
|
#include <QCoreApplication>
|
||||||
#include <QCryptographicHash>
|
#include <QCryptographicHash>
|
||||||
#include <QDateTime>
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
#include <QFutureWatcher>
|
#include <QFutureWatcher>
|
||||||
#include <QIODevice>
|
|
||||||
#include <QList>
|
|
||||||
#include <QLocale>
|
|
||||||
#include <QMessageLogger>
|
|
||||||
#include <QNetworkAccessManager>
|
#include <QNetworkAccessManager>
|
||||||
#include <QNetworkReply>
|
#include <QNetworkReply>
|
||||||
#include <QNetworkRequest>
|
#include <QNetworkRequest>
|
||||||
#include <QSharedPointer>
|
#include <QSaveFile>
|
||||||
#include <QSystemTrayIcon>
|
#include <QSystemTrayIcon>
|
||||||
#include <QTimeZone>
|
#include <QTimer>
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
|
#include <QXmlStreamReader>
|
||||||
#include <QtConcurrentRun>
|
#include <QtConcurrentRun>
|
||||||
#include <QtGlobal>
|
#include <exception>
|
||||||
#include <functional>
|
|
||||||
#include <libcockatrice/card/database/card_database.h>
|
#include <libcockatrice/card/database/card_database.h>
|
||||||
#include <libcockatrice/card/database/card_database_manager.h>
|
#include <libcockatrice/card/database/card_database_manager.h>
|
||||||
#include <libcockatrice/network/network_manager.h>
|
#include <libcockatrice/network/network_manager.h>
|
||||||
#include <libcockatrice/settings/download_settings.h>
|
#include <libcockatrice/settings/download_settings.h>
|
||||||
#include <libcockatrice/settings/paths_settings.h>
|
#include <libcockatrice/settings/paths_settings.h>
|
||||||
#include <tuple>
|
|
||||||
#include <utility>
|
|
||||||
#include <version_string.h>
|
#include <version_string.h>
|
||||||
|
|
||||||
#define SPOILERS_STATUS_URL "https://raw.githubusercontent.com/Cockatrice/Magic-Spoiler/files/SpoilerSeasonEnabled"
|
namespace
|
||||||
#define SPOILERS_URL "https://raw.githubusercontent.com/Cockatrice/Magic-Spoiler/files/spoiler.xml"
|
|
||||||
|
|
||||||
SpoilerBackgroundUpdater::SpoilerBackgroundUpdater(QObject *apParent) : QObject(apParent), cardUpdateProcess(nullptr)
|
|
||||||
{
|
{
|
||||||
isSpoilerDownloadEnabled = SettingsCache::instance().downloads().getDownloadSpoilersStatus();
|
const QUrl SPOILERS_STATUS_URL(
|
||||||
if (isSpoilerDownloadEnabled) {
|
QStringLiteral("https://raw.githubusercontent.com/Cockatrice/Magic-Spoiler/files/SpoilerSeasonEnabled"));
|
||||||
// Start the process of checking if we're in spoiler season
|
const QUrl SPOILERS_URL(QStringLiteral("https://raw.githubusercontent.com/Cockatrice/Magic-Spoiler/files/spoiler.xml"));
|
||||||
// File exists means we're in spoiler season
|
// Request coordination stays on the GUI thread; reload workers never access it.
|
||||||
startSpoilerDownloadProcess(SPOILERS_STATUS_URL, false);
|
QPointer<SpoilerBackgroundUpdater> currentUpdater;
|
||||||
|
QPointer<QFutureWatcher<bool>> activeReload;
|
||||||
|
quint64 newestOperation = 0;
|
||||||
|
|
||||||
|
bool removeSpoilerFile(const QString &path)
|
||||||
|
{
|
||||||
|
QFile file(path);
|
||||||
|
if (!file.exists()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!file.remove()) {
|
||||||
|
qCWarning(SpoilerBackgroundUpdaterLog) << "Could not remove spoiler database" << path << file.errorString();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
SpoilerBackgroundUpdater::SpoilerBackgroundUpdater(QObject *parent, bool forceReload)
|
||||||
|
: QObject(parent), enabledAtStart(SettingsCache::instance().downloads().getDownloadSpoilersStatus()),
|
||||||
|
forceReload(forceReload), targetPath(SettingsCache::instance().paths().getSpoilerCardDatabasePath()),
|
||||||
|
operationId(++newestOperation), manager(NetworkManagerFactory::create(this)), notificationIcon(trayIcon)
|
||||||
|
{
|
||||||
|
connect(&SettingsCache::instance().downloads(), &DownloadSettings::downloadSpoilerStatusChanged, this,
|
||||||
|
&SpoilerBackgroundUpdater::cancelIfSettingsChanged);
|
||||||
|
connect(&SettingsCache::instance().paths(), &PathsSettings::cardDatabasePathChanged, this,
|
||||||
|
&SpoilerBackgroundUpdater::cancelIfSettingsChanged);
|
||||||
|
// Even a disabled/no-op operation completes after its caller can connect signals.
|
||||||
|
QTimer::singleShot(0, this, &SpoilerBackgroundUpdater::begin);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpoilerBackgroundUpdater::begin()
|
||||||
|
{
|
||||||
|
if (completed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (cancelled || operationId != newestOperation ||
|
||||||
|
(!reloadOnly && (!enabledAtStart || !SettingsCache::instance().downloads().getDownloadSpoilersStatus())) ||
|
||||||
|
targetPath != SettingsCache::instance().paths().getSpoilerCardDatabasePath()) {
|
||||||
|
finish();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const auto previous = currentUpdater;
|
||||||
|
if (activeReload) {
|
||||||
|
// The existing database API stores its final load status after its parser
|
||||||
|
// mutex is released. Do not overlap updater-owned calls to that API.
|
||||||
|
connect(activeReload, &QFutureWatcher<bool>::finished, this, &SpoilerBackgroundUpdater::begin,
|
||||||
|
static_cast<Qt::ConnectionType>(Qt::QueuedConnection | Qt::SingleShotConnection));
|
||||||
|
if (previous && previous != this) {
|
||||||
|
previous->cancel();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
currentUpdater = this;
|
||||||
|
QPointer<SpoilerBackgroundUpdater> guard(this);
|
||||||
|
if (previous && previous != this) {
|
||||||
|
previous->cancel();
|
||||||
|
}
|
||||||
|
if (!guard) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!isCurrent()) {
|
||||||
|
finish();
|
||||||
|
} else if (reloadOnly) {
|
||||||
|
reloadDatabase(false);
|
||||||
} else {
|
} else {
|
||||||
qCInfo(SpoilerBackgroundUpdaterLog) << "Spoilers Disabled";
|
downloadFromURL(SPOILERS_STATUS_URL, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpoilerBackgroundUpdater::startSpoilerDownloadProcess(QString url, bool saveResults)
|
bool SpoilerBackgroundUpdater::isCurrent() const
|
||||||
{
|
{
|
||||||
auto spoilerURL = QUrl(url);
|
return !completed && !cancelled && currentUpdater == this && operationId == newestOperation &&
|
||||||
downloadFromURL(spoilerURL, saveResults);
|
(reloadOnly || SettingsCache::instance().downloads().getDownloadSpoilersStatus()) &&
|
||||||
|
targetPath == SettingsCache::instance().paths().getSpoilerCardDatabasePath();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpoilerBackgroundUpdater::downloadFromURL(QUrl url, bool saveResults)
|
void SpoilerBackgroundUpdater::cancelIfSettingsChanged()
|
||||||
{
|
{
|
||||||
auto *nam = NetworkManagerFactory::create(this);
|
if ((!reloadOnly && !SettingsCache::instance().downloads().getDownloadSpoilersStatus()) ||
|
||||||
auto request = QNetworkRequest(url);
|
targetPath != SettingsCache::instance().paths().getSpoilerCardDatabasePath()) {
|
||||||
request.setHeader(QNetworkRequest::UserAgentHeader, QString("Cockatrice %1").arg(VERSION_STRING));
|
cancel();
|
||||||
QNetworkReply *reply = nam->get(request);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpoilerBackgroundUpdater::cancel()
|
||||||
|
{
|
||||||
|
if (completed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
cancelled = true;
|
||||||
|
const auto reply = activeReply;
|
||||||
|
activeReply.clear();
|
||||||
|
QPointer<SpoilerBackgroundUpdater> guard(this);
|
||||||
|
if (reply) {
|
||||||
|
reply->abort();
|
||||||
|
if (reply) {
|
||||||
|
reply->deleteLater();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// A running reload owns no updater state. Allow its completion to retire its
|
||||||
|
// watcher before a heap-owned updater is released by the caller.
|
||||||
|
if (guard && !reloading) {
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpoilerBackgroundUpdater::downloadFromURL(const QUrl &url, bool saveResults)
|
||||||
|
{
|
||||||
|
QNetworkRequest request(url);
|
||||||
|
request.setHeader(QNetworkRequest::UserAgentHeader, QString("Cockatrice %1").arg(VERSION_STRING));
|
||||||
|
request.setTransferTimeout(60000);
|
||||||
|
activeReply = manager->get(request);
|
||||||
if (saveResults) {
|
if (saveResults) {
|
||||||
// This will write out to the file (used for spoiler.xml)
|
connect(activeReply, &QNetworkReply::finished, this,
|
||||||
connect(reply, &QNetworkReply::finished, this, &SpoilerBackgroundUpdater::actDownloadFinishedSpoilersFile);
|
&SpoilerBackgroundUpdater::actDownloadFinishedSpoilersFile);
|
||||||
} else {
|
} else {
|
||||||
// This will check the status (used to see if we're in spoiler season or not)
|
connect(activeReply, &QNetworkReply::finished, this,
|
||||||
connect(reply, &QNetworkReply::finished, this, &SpoilerBackgroundUpdater::actCheckIfSpoilerSeasonEnabled);
|
&SpoilerBackgroundUpdater::actCheckIfSpoilerSeasonEnabled);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpoilerBackgroundUpdater::actCheckIfSpoilerSeasonEnabled()
|
||||||
|
{
|
||||||
|
auto *reply = qobject_cast<QNetworkReply *>(sender());
|
||||||
|
if (!reply || reply != activeReply) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
activeReply.clear();
|
||||||
|
reply->deleteLater();
|
||||||
|
if (!isCurrent()) {
|
||||||
|
finish();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (reply->error() == QNetworkReply::NoError) {
|
||||||
|
downloadFromURL(SPOILERS_URL, true);
|
||||||
|
} else if (reply->error() == QNetworkReply::ContentNotFoundError) {
|
||||||
|
const bool existed = QFileInfo::exists(targetPath);
|
||||||
|
seasonEnded = existed;
|
||||||
|
if (existed && !removeSpoilerFile(targetPath)) {
|
||||||
|
finish(tr("The spoiler season has ended, but the old spoiler database could not be removed."));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (existed || forceReload) {
|
||||||
|
reloadDatabase(false);
|
||||||
|
} else {
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
finish(tr("Could not check spoiler availability: %1").arg(reply->errorString()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpoilerBackgroundUpdater::actDownloadFinishedSpoilersFile()
|
void SpoilerBackgroundUpdater::actDownloadFinishedSpoilersFile()
|
||||||
{
|
{
|
||||||
// Check for server reply
|
auto *reply = qobject_cast<QNetworkReply *>(sender());
|
||||||
auto *reply = dynamic_cast<QNetworkReply *>(sender());
|
if (!reply || reply != activeReply) {
|
||||||
QNetworkReply::NetworkError errorCode = reply->error();
|
return;
|
||||||
|
}
|
||||||
if (errorCode == QNetworkReply::NoError) {
|
activeReply.clear();
|
||||||
spoilerData = reply->readAll();
|
reply->deleteLater();
|
||||||
|
if (!isCurrent()) {
|
||||||
// Save the spoiler.xml file to the disk
|
finish();
|
||||||
saveDownloadedFile(spoilerData);
|
return;
|
||||||
|
}
|
||||||
reply->deleteLater();
|
if (reply->error() != QNetworkReply::NoError) {
|
||||||
emit spoilerCheckerDone();
|
finish(tr("Could not download spoilers: %1").arg(reply->errorString()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
spoilerData = reply->readAll();
|
||||||
|
QString error;
|
||||||
|
const SaveResult result = saveDownloadedFile(spoilerData, error);
|
||||||
|
if (result == SaveResult::Failed) {
|
||||||
|
finish(isCurrent() ? error : QString());
|
||||||
|
} else if (result == SaveResult::Written || forceReload) {
|
||||||
|
reloadDatabase(result == SaveResult::Written);
|
||||||
} else {
|
} else {
|
||||||
qCWarning(SpoilerBackgroundUpdaterLog) << "Error downloading spoilers file" << errorCode;
|
finish();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QString SpoilerBackgroundUpdater::validateDownloadedFile(const QByteArray &data) const
|
||||||
|
{
|
||||||
|
QXmlStreamReader xml(data);
|
||||||
|
if (!xml.readNextStartElement() || xml.name() != QStringLiteral("cockatrice_carddatabase")) {
|
||||||
|
return tr("The download is not a Cockatrice card database. The previous spoiler file was preserved.");
|
||||||
|
}
|
||||||
|
bool versionOk = false;
|
||||||
|
const int version = xml.attributes().value(QStringLiteral("version")).toInt(&versionOk);
|
||||||
|
if (!versionOk || (version != 3 && version != 4)) {
|
||||||
|
return tr("The spoiler database uses an unsupported format. The previous spoiler file was preserved.");
|
||||||
|
}
|
||||||
|
bool hasCards = false;
|
||||||
|
int depth = 1;
|
||||||
|
while (!xml.atEnd()) {
|
||||||
|
const auto token = xml.readNext();
|
||||||
|
if (token == QXmlStreamReader::StartElement) {
|
||||||
|
++depth;
|
||||||
|
if (depth == 2 && xml.name() == QStringLiteral("cards")) {
|
||||||
|
hasCards = true;
|
||||||
|
}
|
||||||
|
} else if (token == QXmlStreamReader::EndElement) {
|
||||||
|
--depth;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (xml.hasError() || !hasCards) {
|
||||||
|
return tr("The spoiler database is incomplete or invalid. The previous spoiler file was preserved.");
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
SpoilerBackgroundUpdater::SaveResult SpoilerBackgroundUpdater::saveDownloadedFile(const QByteArray &data,
|
||||||
|
QString &error)
|
||||||
|
{
|
||||||
|
error = validateDownloadedFile(data);
|
||||||
|
if (!error.isEmpty()) {
|
||||||
|
return SaveResult::Failed;
|
||||||
|
}
|
||||||
|
if (!isCurrent()) {
|
||||||
|
error = tr("The spoiler update was cancelled because its settings changed.");
|
||||||
|
return SaveResult::Failed;
|
||||||
|
}
|
||||||
|
if (getHash(targetPath) == getHash(data)) {
|
||||||
|
return SaveResult::Unchanged;
|
||||||
|
}
|
||||||
|
const QDir directory = QFileInfo(targetPath).dir();
|
||||||
|
if (!directory.exists() && !directory.mkpath(QStringLiteral("."))) {
|
||||||
|
error = tr("Could not create the spoiler database directory: %1").arg(directory.absolutePath());
|
||||||
|
return SaveResult::Failed;
|
||||||
|
}
|
||||||
|
QSaveFile file(targetPath);
|
||||||
|
file.setDirectWriteFallback(false);
|
||||||
|
if (!file.open(QIODevice::WriteOnly) || file.write(data) != data.size() || !file.flush()) {
|
||||||
|
error = tr("Could not save the spoiler database to %1: %2. The previous file was preserved.")
|
||||||
|
.arg(targetPath, file.errorString());
|
||||||
|
return SaveResult::Failed;
|
||||||
|
}
|
||||||
|
if (!isCurrent()) {
|
||||||
|
file.cancelWriting();
|
||||||
|
return SaveResult::Failed;
|
||||||
|
}
|
||||||
|
if (!file.commit()) {
|
||||||
|
error = tr("Could not replace the spoiler database at %1: %2. The previous file was preserved.")
|
||||||
|
.arg(targetPath, file.errorString());
|
||||||
|
return SaveResult::Failed;
|
||||||
|
}
|
||||||
|
return SaveResult::Written;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpoilerBackgroundUpdater::reloadDatabase(bool updated)
|
||||||
|
{
|
||||||
|
reloading = true;
|
||||||
|
auto *database = CardDatabaseManager::getInstance();
|
||||||
|
// The job outlives an individual request, so destroying that request cannot
|
||||||
|
// let a newer updater overlap the worker that is still completing.
|
||||||
|
auto *watcher = new QFutureWatcher<bool>(QCoreApplication::instance());
|
||||||
|
activeReload = watcher;
|
||||||
|
connect(watcher, &QFutureWatcher<bool>::finished, watcher, [watcher] {
|
||||||
|
if (activeReload == watcher) {
|
||||||
|
activeReload.clear();
|
||||||
|
}
|
||||||
|
watcher->deleteLater();
|
||||||
|
});
|
||||||
|
connect(watcher, &QFutureWatcher<bool>::finished, this, [this, watcher, database, updated] {
|
||||||
|
bool loaded = false;
|
||||||
|
try {
|
||||||
|
loaded = watcher->result() && database->getLoadStatus() == Ok;
|
||||||
|
} catch (const std::exception &error) {
|
||||||
|
qCWarning(SpoilerBackgroundUpdaterLog) << "Spoiler database reload failed:" << error.what();
|
||||||
|
}
|
||||||
|
reloading = false;
|
||||||
|
if (!isCurrent()) {
|
||||||
|
finish();
|
||||||
|
} else if (!loaded) {
|
||||||
|
finish(tr("Could not reload card data after updating spoilers. Use Update Spoilers to retry the reload."));
|
||||||
|
} else {
|
||||||
|
finish({}, updated);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
watcher->setFuture(QtConcurrent::run([database] {
|
||||||
|
database->reloadCardDatabasesAndNotify();
|
||||||
|
return true;
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpoilerBackgroundUpdater::finish(const QString &error, bool updated)
|
||||||
|
{
|
||||||
|
if (completed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
completed = true;
|
||||||
|
spoilerData = QByteArray();
|
||||||
|
if (currentUpdater == this) {
|
||||||
|
currentUpdater.clear();
|
||||||
|
}
|
||||||
|
QPointer<SpoilerBackgroundUpdater> guard(this);
|
||||||
|
if (!error.isEmpty() && !cancelled) {
|
||||||
|
qCWarning(SpoilerBackgroundUpdaterLog) << error;
|
||||||
|
if (notificationIcon) {
|
||||||
|
notificationIcon->showMessage(tr("Spoiler update failed"), error);
|
||||||
|
}
|
||||||
|
if (!guard) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
emit spoilerUpdateFailed(error);
|
||||||
|
} else if (updated && !cancelled) {
|
||||||
|
if (notificationIcon) {
|
||||||
|
notificationIcon->showMessage(tr("Spoilers have been updated!"),
|
||||||
|
tr("The card database now includes the downloaded spoilers."));
|
||||||
|
}
|
||||||
|
if (!guard) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
emit spoilersUpdatedSuccessfully();
|
||||||
|
} else if (seasonEnded && !cancelled && notificationIcon) {
|
||||||
|
notificationIcon->showMessage(tr("Spoiler season has ended"),
|
||||||
|
tr("The spoiler database was removed. Please run Oracle to get released cards."));
|
||||||
|
}
|
||||||
|
if (guard) {
|
||||||
emit spoilerCheckerDone();
|
emit spoilerCheckerDone();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SpoilerBackgroundUpdater::deleteSpoilerFile()
|
bool SpoilerBackgroundUpdater::deleteSpoilerFile()
|
||||||
{
|
{
|
||||||
QString fileName = SettingsCache::instance().paths().getSpoilerCardDatabasePath();
|
const QString path = SettingsCache::instance().paths().getSpoilerCardDatabasePath();
|
||||||
QFileInfo fi(fileName);
|
if (QFileInfo::exists(path) && !removeSpoilerFile(path)) {
|
||||||
QDir fileDir(fi.path());
|
|
||||||
QFile file(fileName);
|
|
||||||
|
|
||||||
// Delete the spoiler.xml file
|
|
||||||
if (file.exists() && file.remove()) {
|
|
||||||
qCInfo(SpoilerBackgroundUpdaterLog) << "Deleting spoiler.xml";
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
qCInfo(SpoilerBackgroundUpdaterLog) << "Error: Spoiler.xml not found or not deleted";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SpoilerBackgroundUpdater::actCheckIfSpoilerSeasonEnabled()
|
|
||||||
{
|
|
||||||
auto *response = dynamic_cast<QNetworkReply *>(sender());
|
|
||||||
QNetworkReply::NetworkError errorCode = response->error();
|
|
||||||
|
|
||||||
if (errorCode == QNetworkReply::ContentNotFoundError) {
|
|
||||||
// Spoiler season is offline at this point, so the spoiler.xml file can be safely deleted
|
|
||||||
// The user should run Oracle to get the latest card information
|
|
||||||
if (deleteSpoilerFile() && trayIcon) {
|
|
||||||
trayIcon->showMessage(tr("Spoilers season has ended"), tr("Deleting spoiler.xml. Please run Oracle"));
|
|
||||||
}
|
|
||||||
|
|
||||||
qCInfo(SpoilerBackgroundUpdaterLog) << "Spoiler Season Offline";
|
|
||||||
emit spoilerCheckerDone();
|
|
||||||
} else if (errorCode == QNetworkReply::NoError) {
|
|
||||||
qCInfo(SpoilerBackgroundUpdaterLog) << "Spoiler Service Online";
|
|
||||||
startSpoilerDownloadProcess(SPOILERS_URL, true);
|
|
||||||
} else if (errorCode == QNetworkReply::HostNotFoundError) {
|
|
||||||
if (trayIcon) {
|
|
||||||
trayIcon->showMessage(tr("Spoilers download failed"), tr("No internet connection"));
|
|
||||||
}
|
|
||||||
|
|
||||||
qCWarning(SpoilerBackgroundUpdaterLog) << "Spoiler download failed due to no internet connection";
|
|
||||||
emit spoilerCheckerDone();
|
|
||||||
} else {
|
|
||||||
if (trayIcon) {
|
|
||||||
trayIcon->showMessage(tr("Spoilers download failed"), tr("Error") + " " + (short)errorCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
qCWarning(SpoilerBackgroundUpdaterLog) << "Spoiler download failed with reason" << errorCode;
|
|
||||||
emit spoilerCheckerDone();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool SpoilerBackgroundUpdater::saveDownloadedFile(QByteArray data)
|
|
||||||
{
|
|
||||||
QString fileName = SettingsCache::instance().paths().getSpoilerCardDatabasePath();
|
|
||||||
QFileInfo fi(fileName);
|
|
||||||
QDir fileDir(fi.path());
|
|
||||||
|
|
||||||
if (!fileDir.exists() && !fileDir.mkpath(fileDir.absolutePath())) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
// Explicit disable must clear the in-memory view even when another actor has
|
||||||
// Check if the data matches. If it does, then spoilers are up to date.
|
// already removed the file. Queue behind an older reload through the same
|
||||||
if (getHash(fileName) == getHash(data)) {
|
// updater lifecycle, so its final snapshot cannot restore disabled spoilers.
|
||||||
if (trayIcon) {
|
auto *updater = new SpoilerBackgroundUpdater(QCoreApplication::instance(), true);
|
||||||
trayIcon->showMessage(tr("Spoilers already up to date"), tr("No new spoilers added"));
|
updater->reloadOnly = true;
|
||||||
}
|
connect(updater, &SpoilerBackgroundUpdater::spoilerCheckerDone, updater, &QObject::deleteLater);
|
||||||
|
|
||||||
qCInfo(SpoilerBackgroundUpdaterLog) << "Spoilers Up to Date";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
QFile file(fileName);
|
|
||||||
if (!file.open(QIODevice::WriteOnly)) {
|
|
||||||
qCWarning(SpoilerBackgroundUpdaterLog) << "Spoiler Service Error: File open (w) failed for" << fileName;
|
|
||||||
file.close();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (file.write(data) == -1) {
|
|
||||||
qCWarning(SpoilerBackgroundUpdaterLog) << "Spoiler Service Error: File write (w) failed for" << fileName;
|
|
||||||
file.close();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
file.close();
|
|
||||||
|
|
||||||
// Data written, so reload the card database
|
|
||||||
qCInfo(SpoilerBackgroundUpdaterLog) << "Spoiler Service Data Written";
|
|
||||||
// Spoilers can introduce sets the config has never seen; the loader never
|
|
||||||
// runs checkUnknownSets() itself, so run it once the finished snapshot has
|
|
||||||
// been swapped in.
|
|
||||||
auto *watcher = new QFutureWatcher<void>(this);
|
|
||||||
connect(watcher, &QFutureWatcher<void>::finished, this, [watcher] {
|
|
||||||
watcher->deleteLater();
|
|
||||||
CardDatabaseManager::getInstance()->checkUnknownSets();
|
|
||||||
});
|
|
||||||
watcher->setFuture(QtConcurrent::run([] { CardDatabaseManager::getInstance()->loadCardDatabases(); }));
|
|
||||||
|
|
||||||
// If the user has notifications enabled, let them know
|
|
||||||
// when the database was last updated
|
|
||||||
if (trayIcon) {
|
|
||||||
QList<QByteArray> lines = data.split('\n');
|
|
||||||
|
|
||||||
for (const QByteArray &line : lines) {
|
|
||||||
if (line.contains("Created At:")) {
|
|
||||||
QString timeStamp = QString(line).replace("Created At:", "").trimmed();
|
|
||||||
timeStamp.chop(6); // Remove " (UTC)"
|
|
||||||
|
|
||||||
auto utcTime = QLocale().toDateTime(timeStamp, "ddd, MMM dd yyyy, hh:mm:ss");
|
|
||||||
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
|
|
||||||
utcTime.setTimeZone(QTimeZone::UTC);
|
|
||||||
#else
|
|
||||||
utcTime.setTimeSpec(Qt::UTC);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
QString localTime = utcTime.toLocalTime().toString("MMM d, hh:mm");
|
|
||||||
|
|
||||||
trayIcon->showMessage(tr("Spoilers have been updated!"), tr("Last change:") + " " + localTime);
|
|
||||||
emit spoilersUpdatedSuccessfully();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray SpoilerBackgroundUpdater::getHash(const QString fileName)
|
QByteArray SpoilerBackgroundUpdater::getHash(const QString fileName)
|
||||||
{
|
{
|
||||||
QFile file(fileName);
|
QFile file(fileName);
|
||||||
|
if (!file.open(QIODevice::ReadOnly)) {
|
||||||
if (file.open(QFile::ReadOnly)) {
|
return {};
|
||||||
// Only read the first 512 bytes (enough to get the "created" tag)
|
|
||||||
const QByteArray bytes = file.read(512);
|
|
||||||
|
|
||||||
QCryptographicHash hash(QCryptographicHash::Algorithm::Md5);
|
|
||||||
hash.addData(bytes);
|
|
||||||
|
|
||||||
qCInfo(SpoilerBackgroundUpdaterLog) << "File Hash =" << hash.result();
|
|
||||||
|
|
||||||
file.close();
|
|
||||||
return hash.result();
|
|
||||||
} else {
|
|
||||||
qCWarning(SpoilerBackgroundUpdaterLog) << "getHash ReadOnly failed!";
|
|
||||||
file.close();
|
|
||||||
return QByteArray();
|
|
||||||
}
|
}
|
||||||
|
QCryptographicHash hash(QCryptographicHash::Sha256);
|
||||||
|
return hash.addData(&file) ? hash.result() : QByteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray SpoilerBackgroundUpdater::getHash(QByteArray data)
|
QByteArray SpoilerBackgroundUpdater::getHash(QByteArray data)
|
||||||
{
|
{
|
||||||
// Only read the first 512 bytes (enough to get the "created" tag)
|
return QCryptographicHash::hash(data, QCryptographicHash::Sha256);
|
||||||
const QByteArray bytes = data.left(512);
|
|
||||||
|
|
||||||
QCryptographicHash hash(QCryptographicHash::Algorithm::Md5);
|
|
||||||
hash.addData(bytes);
|
|
||||||
|
|
||||||
qCInfo(SpoilerBackgroundUpdaterLog) << "Data Hash =" << hash.result();
|
|
||||||
|
|
||||||
return hash.result();
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
* @file spoiler_background_updater.h
|
* @file spoiler_background_updater.h
|
||||||
* @ingroup Client
|
* @ingroup Client
|
||||||
*/
|
*/
|
||||||
//! \todo Document this file.
|
|
||||||
|
|
||||||
#ifndef COCKATRICE_SPOILER_DOWNLOADER_H
|
#ifndef COCKATRICE_SPOILER_DOWNLOADER_H
|
||||||
#define COCKATRICE_SPOILER_DOWNLOADER_H
|
#define COCKATRICE_SPOILER_DOWNLOADER_H
|
||||||
|
|
@ -10,10 +9,13 @@
|
||||||
#include <QByteArray>
|
#include <QByteArray>
|
||||||
#include <QLoggingCategory>
|
#include <QLoggingCategory>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
#include <QPointer>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <qtmetamacros.h>
|
#include <qtmetamacros.h>
|
||||||
|
|
||||||
class QProcess;
|
class QNetworkAccessManager;
|
||||||
|
class QNetworkReply;
|
||||||
|
class QSystemTrayIcon;
|
||||||
class QUrl;
|
class QUrl;
|
||||||
|
|
||||||
inline Q_LOGGING_CATEGORY(SpoilerBackgroundUpdaterLog, "spoiler_background_updater");
|
inline Q_LOGGING_CATEGORY(SpoilerBackgroundUpdaterLog, "spoiler_background_updater");
|
||||||
|
|
@ -22,7 +24,7 @@ class SpoilerBackgroundUpdater : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit SpoilerBackgroundUpdater(QObject *apParent = nullptr);
|
explicit SpoilerBackgroundUpdater(QObject *parent = nullptr, bool forceReload = false);
|
||||||
inline QString getCardUpdaterBinaryName()
|
inline QString getCardUpdaterBinaryName()
|
||||||
{
|
{
|
||||||
return "oracle";
|
return "oracle";
|
||||||
|
|
@ -32,20 +34,45 @@ public:
|
||||||
static bool deleteSpoilerFile();
|
static bool deleteSpoilerFile();
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
|
void begin();
|
||||||
void actDownloadFinishedSpoilersFile();
|
void actDownloadFinishedSpoilersFile();
|
||||||
void actCheckIfSpoilerSeasonEnabled();
|
void actCheckIfSpoilerSeasonEnabled();
|
||||||
|
void cancelIfSettingsChanged();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool isSpoilerDownloadEnabled;
|
enum class SaveResult
|
||||||
QProcess *cardUpdateProcess;
|
{
|
||||||
|
Failed,
|
||||||
|
Unchanged,
|
||||||
|
Written
|
||||||
|
};
|
||||||
|
|
||||||
|
const bool enabledAtStart;
|
||||||
|
const bool forceReload;
|
||||||
|
const QString targetPath;
|
||||||
|
const quint64 operationId;
|
||||||
|
QNetworkAccessManager *manager;
|
||||||
|
QPointer<QNetworkReply> activeReply;
|
||||||
|
QPointer<QSystemTrayIcon> notificationIcon;
|
||||||
QByteArray spoilerData;
|
QByteArray spoilerData;
|
||||||
void startSpoilerDownloadProcess(QString url, bool saveResults);
|
bool completed = false;
|
||||||
void downloadFromURL(QUrl url, bool saveResults);
|
bool cancelled = false;
|
||||||
bool saveDownloadedFile(QByteArray data);
|
bool reloading = false;
|
||||||
|
bool seasonEnded = false;
|
||||||
|
bool reloadOnly = false;
|
||||||
|
|
||||||
|
bool isCurrent() const;
|
||||||
|
void cancel();
|
||||||
|
void finish(const QString &error = {}, bool updated = false);
|
||||||
|
void downloadFromURL(const QUrl &url, bool saveResults);
|
||||||
|
void reloadDatabase(bool updated);
|
||||||
|
QString validateDownloadedFile(const QByteArray &data) const;
|
||||||
|
SaveResult saveDownloadedFile(const QByteArray &data, QString &error);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void spoilersUpdatedSuccessfully();
|
void spoilersUpdatedSuccessfully();
|
||||||
void spoilerCheckerDone();
|
void spoilerCheckerDone();
|
||||||
|
void spoilerUpdateFailed(const QString &message);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // COCKATRICE_SPOILER_DOWNLOADER_H
|
#endif // COCKATRICE_SPOILER_DOWNLOADER_H
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
#include <QAbstractItemModel>
|
#include <QAbstractItemModel>
|
||||||
#include <QAbstractItemView>
|
#include <QAbstractItemView>
|
||||||
#include <QAction>
|
#include <QAction>
|
||||||
|
#include <QCoreApplication>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
|
|
@ -332,16 +333,33 @@ void DeckEditorSettingsPage::updateSpoilers()
|
||||||
updateNowButton->setDisabled(true);
|
updateNowButton->setDisabled(true);
|
||||||
updateNowButton->setText(tr("Updating..."));
|
updateNowButton->setText(tr("Updating..."));
|
||||||
|
|
||||||
// Create a new SBU that will act as if the client was just reloaded
|
// The explicit update also retries a previous reload of an unchanged file.
|
||||||
auto *sbu = new SpoilerBackgroundUpdater();
|
// Application ownership lets it finish if the settings page is closed.
|
||||||
connect(sbu, &SpoilerBackgroundUpdater::spoilerCheckerDone, this, &DeckEditorSettingsPage::unlockSettings);
|
auto *sbu = new SpoilerBackgroundUpdater(QCoreApplication::instance(), true);
|
||||||
connect(sbu, &SpoilerBackgroundUpdater::spoilersUpdatedSuccessfully, this, &DeckEditorSettingsPage::unlockSettings);
|
spoilerUpdater = sbu;
|
||||||
|
connect(sbu, &SpoilerBackgroundUpdater::spoilerCheckerDone, this, [this, sbu] {
|
||||||
|
if (spoilerUpdater == sbu) {
|
||||||
|
spoilerUpdater.clear();
|
||||||
|
unlockSettings();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
connect(sbu, &SpoilerBackgroundUpdater::spoilerCheckerDone, sbu, &QObject::deleteLater);
|
||||||
|
connect(sbu, &SpoilerBackgroundUpdater::spoilerUpdateFailed, this, [this, sbu](const QString &message) {
|
||||||
|
if (spoilerUpdater != sbu) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto *notice = new QMessageBox(QMessageBox::Warning, tr("Spoiler update"), message, QMessageBox::Ok, this);
|
||||||
|
notice->setTextFormat(Qt::PlainText);
|
||||||
|
notice->setAttribute(Qt::WA_DeleteOnClose);
|
||||||
|
notice->open();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeckEditorSettingsPage::unlockSettings()
|
void DeckEditorSettingsPage::unlockSettings()
|
||||||
{
|
{
|
||||||
updateNowButton->setDisabled(false);
|
updateNowButton->setEnabled(SettingsCache::instance().downloads().getDownloadSpoilersStatus());
|
||||||
updateNowButton->setText(tr("Update Spoilers"));
|
updateNowButton->setText(tr("Update Spoilers"));
|
||||||
|
lastUpdatedLabel.setText(tr("Last Change") + ": " + getLastUpdateTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
QString DeckEditorSettingsPage::getLastUpdateTime()
|
QString DeckEditorSettingsPage::getLastUpdateTime()
|
||||||
|
|
@ -379,12 +397,22 @@ void DeckEditorSettingsPage::setSpoilersEnabled(bool anInput)
|
||||||
mpSpoilerSavePathLineEdit->setEnabled(anInput);
|
mpSpoilerSavePathLineEdit->setEnabled(anInput);
|
||||||
mpSpoilerPathButton->setEnabled(anInput);
|
mpSpoilerPathButton->setEnabled(anInput);
|
||||||
lastUpdatedLabel.setEnabled(anInput);
|
lastUpdatedLabel.setEnabled(anInput);
|
||||||
updateNowButton->setEnabled(anInput);
|
updateNowButton->setEnabled(anInput && !spoilerUpdater);
|
||||||
infoOnSpoilersLabel.setEnabled(anInput);
|
infoOnSpoilersLabel.setEnabled(anInput);
|
||||||
|
|
||||||
if (!anInput) {
|
if (!anInput && (sender() == &mcDownloadSpoilersCheckBox ||
|
||||||
SpoilerBackgroundUpdater::deleteSpoilerFile();
|
QFileInfo::exists(SettingsCache::instance().paths().getSpoilerCardDatabasePath()))) {
|
||||||
|
if (!SpoilerBackgroundUpdater::deleteSpoilerFile()) {
|
||||||
|
auto *notice = new QMessageBox(QMessageBox::Warning, tr("Spoiler update"),
|
||||||
|
tr("The spoiler database could not be removed. Check its file permissions "
|
||||||
|
"and try disabling spoilers again."),
|
||||||
|
QMessageBox::Ok, this);
|
||||||
|
notice->setTextFormat(Qt::PlainText);
|
||||||
|
notice->setAttribute(Qt::WA_DeleteOnClose);
|
||||||
|
notice->open();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
lastUpdatedLabel.setText(tr("Last Change") + ": " + getLastUpdateTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeckEditorSettingsPage::retranslateUi()
|
void DeckEditorSettingsPage::retranslateUi()
|
||||||
|
|
@ -396,7 +424,7 @@ void DeckEditorSettingsPage::retranslateUi()
|
||||||
lastUpdatedLabel.setText(tr("Last Change") + ": " + getLastUpdateTime());
|
lastUpdatedLabel.setText(tr("Last Change") + ": " + getLastUpdateTime());
|
||||||
infoOnSpoilersLabel.setText(tr("Spoilers download automatically on launch") + "\n" +
|
infoOnSpoilersLabel.setText(tr("Spoilers download automatically on launch") + "\n" +
|
||||||
tr("Press the button to manually update without relaunching") + "\n\n" +
|
tr("Press the button to manually update without relaunching") + "\n\n" +
|
||||||
tr("Do not close settings until manual update is complete"));
|
tr("Updates continue if you close settings"));
|
||||||
picDownloadCheckBox.setText(tr("Download card pictures on the fly"));
|
picDownloadCheckBox.setText(tr("Download card pictures on the fly"));
|
||||||
urlLinkLabel.setText(QString("<a href='%1'>%2</a>").arg(WIKI_CUSTOM_PIC_URL).arg(tr("How to add a custom URL")));
|
urlLinkLabel.setText(QString("<a href='%1'>%2</a>").arg(WIKI_CUSTOM_PIC_URL).arg(tr("How to add a custom URL")));
|
||||||
resetDownloadURLs.setText(tr("Reset Download URLs"));
|
resetDownloadURLs.setText(tr("Reset Download URLs"));
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
#include <QCheckBox>
|
#include <QCheckBox>
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
|
#include <QPointer>
|
||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <qtmetamacros.h>
|
#include <qtmetamacros.h>
|
||||||
|
|
@ -15,6 +16,7 @@ class QLineEdit;
|
||||||
class QListWidget;
|
class QListWidget;
|
||||||
class QListWidgetItem;
|
class QListWidgetItem;
|
||||||
class QModelIndex;
|
class QModelIndex;
|
||||||
|
class SpoilerBackgroundUpdater;
|
||||||
|
|
||||||
class DeckEditorSettingsPage : public AbstractSettingsPage
|
class DeckEditorSettingsPage : public AbstractSettingsPage
|
||||||
{
|
{
|
||||||
|
|
@ -54,6 +56,7 @@ private:
|
||||||
QLabel infoOnSpoilersLabel;
|
QLabel infoOnSpoilersLabel;
|
||||||
QPushButton *mpSpoilerPathButton;
|
QPushButton *mpSpoilerPathButton;
|
||||||
QPushButton *updateNowButton;
|
QPushButton *updateNowButton;
|
||||||
|
QPointer<SpoilerBackgroundUpdater> spoilerUpdater;
|
||||||
|
|
||||||
/** @brief Adds a list item for the given URL, storing the raw URL alongside its displayed label. */
|
/** @brief Adds a list item for the given URL, storing the raw URL alongside its displayed label. */
|
||||||
QListWidgetItem *addUrlItem(const QString &url);
|
QListWidgetItem *addUrlItem(const QString &url);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue