save downloaded xmls (#4736)
This commit is contained in:
parent
304ed3cd60
commit
f5f8acf1fd
3 changed files with 63 additions and 22 deletions
|
@ -300,7 +300,7 @@ void LoadSetsPage::actLoadSetsFile()
|
||||||
bool LoadSetsPage::validatePage()
|
bool LoadSetsPage::validatePage()
|
||||||
{
|
{
|
||||||
// once the import is finished, we call next(); skip validation
|
// once the import is finished, we call next(); skip validation
|
||||||
if (wizard()->importer->getSets().count() > 0) {
|
if (wizard()->downloadedPlainXml || wizard()->importer->getSets().count() > 0) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -354,7 +354,6 @@ bool LoadSetsPage::validatePage()
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
void LoadSetsPage::downloadSetsFile(const QUrl &url)
|
void LoadSetsPage::downloadSetsFile(const QUrl &url)
|
||||||
{
|
{
|
||||||
wizard()->setCardSourceVersion("unknown");
|
wizard()->setCardSourceVersion("unknown");
|
||||||
|
@ -449,12 +448,20 @@ void LoadSetsPage::readSetsFromByteArray(QByteArray data)
|
||||||
progressLabel->show();
|
progressLabel->show();
|
||||||
progressBar->show();
|
progressBar->show();
|
||||||
|
|
||||||
|
wizard()->downloadedPlainXml = false;
|
||||||
|
wizard()->xmlData.clear();
|
||||||
|
readSetsFromByteArrayRef(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LoadSetsPage::readSetsFromByteArrayRef(QByteArray &data)
|
||||||
|
{
|
||||||
// unzip the file if needed
|
// unzip the file if needed
|
||||||
if (data.startsWith(XZ_SIGNATURE)) {
|
if (data.startsWith(XZ_SIGNATURE)) {
|
||||||
#ifdef HAS_LZMA
|
#ifdef HAS_LZMA
|
||||||
// zipped file
|
// zipped file
|
||||||
auto *inBuffer = new QBuffer(&data);
|
auto *inBuffer = new QBuffer(&data);
|
||||||
auto *outBuffer = new QBuffer(this);
|
auto newData = QByteArray();
|
||||||
|
auto *outBuffer = new QBuffer(&newData);
|
||||||
inBuffer->open(QBuffer::ReadOnly);
|
inBuffer->open(QBuffer::ReadOnly);
|
||||||
outBuffer->open(QBuffer::WriteOnly);
|
outBuffer->open(QBuffer::WriteOnly);
|
||||||
XzDecompressor xz;
|
XzDecompressor xz;
|
||||||
|
@ -462,11 +469,8 @@ void LoadSetsPage::readSetsFromByteArray(QByteArray data)
|
||||||
zipDownloadFailed(tr("Xz extraction failed."));
|
zipDownloadFailed(tr("Xz extraction failed."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const auto &outBufferData = outBuffer->data();
|
data.clear();
|
||||||
|
readSetsFromByteArrayRef(newData);
|
||||||
future = QtConcurrent::run(
|
|
||||||
[this, &outBufferData] { return wizard()->importer->readSetsFromByteArray(outBufferData); });
|
|
||||||
watcher.setFuture(future);
|
|
||||||
return;
|
return;
|
||||||
#else
|
#else
|
||||||
zipDownloadFailed(tr("Sorry, this version of Oracle does not support xz compressed files."));
|
zipDownloadFailed(tr("Sorry, this version of Oracle does not support xz compressed files."));
|
||||||
|
@ -481,7 +485,8 @@ void LoadSetsPage::readSetsFromByteArray(QByteArray data)
|
||||||
#ifdef HAS_ZLIB
|
#ifdef HAS_ZLIB
|
||||||
// zipped file
|
// zipped file
|
||||||
auto *inBuffer = new QBuffer(&data);
|
auto *inBuffer = new QBuffer(&data);
|
||||||
auto *outBuffer = new QBuffer(this);
|
auto newData = QByteArray();
|
||||||
|
auto *outBuffer = new QBuffer(&newData);
|
||||||
QString fileName;
|
QString fileName;
|
||||||
UnZip::ErrorCode ec;
|
UnZip::ErrorCode ec;
|
||||||
UnZip uz;
|
UnZip uz;
|
||||||
|
@ -505,11 +510,8 @@ void LoadSetsPage::readSetsFromByteArray(QByteArray data)
|
||||||
uz.closeArchive();
|
uz.closeArchive();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const auto &outBufferData = outBuffer->data();
|
data.clear();
|
||||||
|
readSetsFromByteArrayRef(newData);
|
||||||
future = QtConcurrent::run(
|
|
||||||
[this, &outBufferData] { return wizard()->importer->readSetsFromByteArray(outBufferData); });
|
|
||||||
watcher.setFuture(future);
|
|
||||||
return;
|
return;
|
||||||
#else
|
#else
|
||||||
zipDownloadFailed(tr("Sorry, this version of Oracle does not support zipped files."));
|
zipDownloadFailed(tr("Sorry, this version of Oracle does not support zipped files."));
|
||||||
|
@ -520,10 +522,23 @@ void LoadSetsPage::readSetsFromByteArray(QByteArray data)
|
||||||
progressBar->hide();
|
progressBar->hide();
|
||||||
return;
|
return;
|
||||||
#endif
|
#endif
|
||||||
}
|
} else if (data.startsWith("{")) {
|
||||||
// Start the computation.
|
// Start the computation.
|
||||||
future = QtConcurrent::run([this, &data] { return wizard()->importer->readSetsFromByteArray(data); });
|
jsonData = std::move(data);
|
||||||
|
future = QtConcurrent::run([this] { return wizard()->importer->readSetsFromByteArray(std::move(jsonData)); });
|
||||||
watcher.setFuture(future);
|
watcher.setFuture(future);
|
||||||
|
} else if (data.startsWith("<")) {
|
||||||
|
// save xml file and don't do any processing
|
||||||
|
wizard()->downloadedPlainXml = true;
|
||||||
|
wizard()->xmlData = std::move(data);
|
||||||
|
importFinished();
|
||||||
|
} else {
|
||||||
|
wizard()->enableButtons();
|
||||||
|
setEnabled(true);
|
||||||
|
progressLabel->hide();
|
||||||
|
progressBar->hide();
|
||||||
|
QMessageBox::critical(this, tr("Error"), tr("Failed to interpret downloaded data."));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void LoadSetsPage::zipDownloadFailed(const QString &message)
|
void LoadSetsPage::zipDownloadFailed(const QString &message)
|
||||||
|
@ -553,7 +568,7 @@ void LoadSetsPage::importFinished()
|
||||||
progressLabel->hide();
|
progressLabel->hide();
|
||||||
progressBar->hide();
|
progressBar->hide();
|
||||||
|
|
||||||
if (watcher.future().result()) {
|
if (wizard()->downloadedPlainXml || watcher.future().result()) {
|
||||||
wizard()->next();
|
wizard()->next();
|
||||||
} else {
|
} else {
|
||||||
QMessageBox::critical(this, tr("Error"),
|
QMessageBox::critical(this, tr("Error"),
|
||||||
|
@ -590,6 +605,12 @@ void SaveSetsPage::initializePage()
|
||||||
{
|
{
|
||||||
messageLog->clear();
|
messageLog->clear();
|
||||||
|
|
||||||
|
retranslateUi();
|
||||||
|
if (wizard()->downloadedPlainXml) {
|
||||||
|
messageLog->hide();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
messageLog->show();
|
||||||
connect(wizard()->importer, SIGNAL(setIndexChanged(int, int, const QString &)), this,
|
connect(wizard()->importer, SIGNAL(setIndexChanged(int, int, const QString &)), this,
|
||||||
SLOT(updateTotalProgress(int, int, const QString &)));
|
SLOT(updateTotalProgress(int, int, const QString &)));
|
||||||
|
|
||||||
|
@ -601,7 +622,12 @@ void SaveSetsPage::initializePage()
|
||||||
void SaveSetsPage::retranslateUi()
|
void SaveSetsPage::retranslateUi()
|
||||||
{
|
{
|
||||||
setTitle(tr("Sets imported"));
|
setTitle(tr("Sets imported"));
|
||||||
|
if (wizard()->downloadedPlainXml) {
|
||||||
|
setSubTitle(tr("A cockatrice database file of %1 MB has been downloaded.")
|
||||||
|
.arg(qRound(wizard()->xmlData.size() / 1000000.0)));
|
||||||
|
} else {
|
||||||
setSubTitle(tr("The following sets have been found:"));
|
setSubTitle(tr("The following sets have been found:"));
|
||||||
|
}
|
||||||
|
|
||||||
saveLabel->setText(tr("Press \"Save\" to store the imported cards in the Cockatrice database."));
|
saveLabel->setText(tr("Press \"Save\" to store the imported cards in the Cockatrice database."));
|
||||||
pathLabel->setText(tr("The card database will be saved at the following location:") + "<br>" +
|
pathLabel->setText(tr("The card database will be saved at the following location:") + "<br>" +
|
||||||
|
@ -646,7 +672,19 @@ bool SaveSetsPage::validatePage()
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!wizard()->importer->saveToFile(fileName, wizard()->getCardSourceUrl(), wizard()->getCardSourceVersion())) {
|
if (wizard()->downloadedPlainXml) {
|
||||||
|
QFile file(fileName);
|
||||||
|
if (!file.open(QIODevice::WriteOnly)) {
|
||||||
|
qDebug() << "File write (w) failed for" << fileName;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (file.write(wizard()->xmlData) < 1) {
|
||||||
|
qDebug() << "File write (w) failed for" << fileName;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
wizard()->xmlData.clear();
|
||||||
|
} else if (!wizard()->importer->saveToFile(fileName, wizard()->getCardSourceUrl(),
|
||||||
|
wizard()->getCardSourceVersion())) {
|
||||||
QMessageBox::critical(this, tr("Error"), tr("The file could not be saved to %1").arg(fileName));
|
QMessageBox::critical(this, tr("Error"), tr("The file could not be saved to %1").arg(fileName));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,6 +60,8 @@ public:
|
||||||
OracleImporter *importer;
|
OracleImporter *importer;
|
||||||
QSettings *settings;
|
QSettings *settings;
|
||||||
QNetworkAccessManager *nam;
|
QNetworkAccessManager *nam;
|
||||||
|
bool downloadedPlainXml = false;
|
||||||
|
QByteArray xmlData;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void updateLanguage();
|
void updateLanguage();
|
||||||
|
@ -113,6 +115,7 @@ protected:
|
||||||
void initializePage() override;
|
void initializePage() override;
|
||||||
bool validatePage() override;
|
bool validatePage() override;
|
||||||
void readSetsFromByteArray(QByteArray data);
|
void readSetsFromByteArray(QByteArray data);
|
||||||
|
void readSetsFromByteArrayRef(QByteArray &data);
|
||||||
void downloadSetsFile(const QUrl &url);
|
void downloadSetsFile(const QUrl &url);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -127,6 +130,7 @@ private:
|
||||||
|
|
||||||
QFutureWatcher<bool> watcher;
|
QFutureWatcher<bool> watcher;
|
||||||
QFuture<bool> future;
|
QFuture<bool> future;
|
||||||
|
QByteArray jsonData;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void actLoadSetsFile();
|
void actLoadSetsFile();
|
||||||
|
|
|
@ -80,12 +80,11 @@ QVariant Json::parse(const QString &json, bool &success)
|
||||||
|
|
||||||
// Return an empty QVariant if the JSON data is either null or empty
|
// Return an empty QVariant if the JSON data is either null or empty
|
||||||
if (!json.isNull() || !json.isEmpty()) {
|
if (!json.isNull() || !json.isEmpty()) {
|
||||||
QString data = json;
|
|
||||||
// We'll start from index 0
|
// We'll start from index 0
|
||||||
int index = 0;
|
int index = 0;
|
||||||
|
|
||||||
// Parse the first value
|
// Parse the first value
|
||||||
QVariant value = Json::parseValue(data, index, success);
|
QVariant value = Json::parseValue(json, index, success);
|
||||||
|
|
||||||
// Return the parsed value
|
// Return the parsed value
|
||||||
return value;
|
return value;
|
||||||
|
|
Loading…
Reference in a new issue