Oracle / card xml improvements (#3934)

* fix #1610

* fix #2679; partially fix #3647

* Fix tests

* Remove debug code
This commit is contained in:
ctrlaltca 2020-03-30 21:56:03 +02:00 committed by GitHub
parent a135ad064a
commit 27b7ebe208
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 131 additions and 10 deletions

View file

@ -459,10 +459,10 @@ int OracleImporter::startImport()
return setIndex;
}
bool OracleImporter::saveToFile(const QString &fileName)
bool OracleImporter::saveToFile(const QString &fileName, const QString &sourceUrl, const QString &sourceVersion)
{
CockatriceXml4Parser parser;
return parser.saveToFile(sets, cards, fileName);
return parser.saveToFile(sets, cards, fileName, sourceUrl, sourceVersion);
}
void OracleImporter::clear()

View file

@ -110,7 +110,7 @@ public:
explicit OracleImporter(const QString &_dataDir, QObject *parent = nullptr);
bool readSetsFromByteArray(const QByteArray &data);
int startImport();
bool saveToFile(const QString &fileName);
bool saveToFile(const QString &fileName, const QString &sourceUrl, const QString &sourceVersion);
int importCardsFromSet(CardSetPtr currentSet, const QList<QVariant> &cards, bool skipSpecialNums = true);
QList<SetToDownload> &getSets()
{

View file

@ -39,6 +39,7 @@
// Xz stream header: 0xFD + "7zXZ"
#define XZ_SIGNATURE "\xFD\x37\x7A\x58\x5A"
#define ALLSETS_URL_FALLBACK "https://www.mtgjson.com/files/AllPrintings.json"
#define MTGJSON_VERSION_URL "https://www.mtgjson.com/files/version.json"
#ifdef HAS_LZMA
#define ALLSETS_URL "https://www.mtgjson.com/files/AllPrintings.json.xz"
@ -331,14 +332,46 @@ bool LoadSetsPage::validatePage()
wizard()->disableButtons();
setEnabled(false);
wizard()->setCardSourceUrl(setsFile.fileName());
wizard()->setCardSourceVersion("unknown");
readSetsFromByteArray(setsFile.readAll());
}
return false;
}
#include <iostream>
void LoadSetsPage::downloadSetsFile(QUrl url)
{
wizard()->setCardSourceVersion("unknown");
QString urlString = url.toString();
if (urlString == ALLSETS_URL || urlString == ALLSETS_URL_FALLBACK) {
QUrl versionUrl = QUrl::fromUserInput(MTGJSON_VERSION_URL);
QNetworkReply *versionReply = wizard()->nam->get(QNetworkRequest(versionUrl));
connect(versionReply, &QNetworkReply::finished, [this, versionReply]() {
if (versionReply->error() == QNetworkReply::NoError) {
QByteArray jsonData = versionReply->readAll();
QJsonParseError jsonError;
QJsonDocument jsonResponse = QJsonDocument::fromJson(jsonData, &jsonError);
if (jsonError.error == QJsonParseError::NoError) {
QVariantMap jsonMap = jsonResponse.toVariant().toMap();
QString versionString = jsonMap["version"].toString();
if (versionString.isEmpty()) {
versionString = "unknown";
}
wizard()->setCardSourceVersion(versionString);
}
}
versionReply->deleteLater();
});
}
wizard()->setCardSourceUrl(url.toString());
QNetworkReply *reply = wizard()->nam->get(QNetworkRequest(url));
connect(reply, SIGNAL(finished()), this, SLOT(actDownloadFinishedSetsFile()));
@ -597,7 +630,7 @@ bool SaveSetsPage::validatePage()
return false;
}
if (wizard()->importer->saveToFile(fileName)) {
if (wizard()->importer->saveToFile(fileName, wizard()->getCardSourceUrl(), wizard()->getCardSourceVersion())) {
ok = true;
} else {
QMessageBox::critical(this, tr("Error"), tr("The file could not be saved to %1").arg(fileName));

View file

@ -38,6 +38,22 @@ public:
{
return !tokensData.isEmpty();
}
void setCardSourceUrl(const QString &sourceUrl)
{
cardSourceUrl = sourceUrl;
}
void setCardSourceVersion(const QString &sourceVersion)
{
cardSourceVersion = sourceVersion;
}
const QString &getCardSourceUrl() const
{
return cardSourceUrl;
}
const QString &getCardSourceVersion() const
{
return cardSourceVersion;
}
bool saveTokensToFile(const QString &fileName);
public:
@ -50,6 +66,8 @@ private slots:
private:
QByteArray tokensData;
QString cardSourceUrl;
QString cardSourceVersion;
protected:
void changeEvent(QEvent *event) override;