mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-04-27 07:48:01 -07:00
Oracle: support AllSets.json.zip; Fix #624
Imported unzip methods from OSDaB-Zip project; Adds a soft dependency on zlib to oracle; if lib if not found, oracle will continue to compile and work but without zip support.
This commit is contained in:
parent
b608d81037
commit
781f8a6603
12 changed files with 4029 additions and 3 deletions
|
|
@ -6,6 +6,7 @@
|
|||
#include <QtConcurrent>
|
||||
#endif
|
||||
#include <QAbstractButton>
|
||||
#include <QBuffer>
|
||||
#include <QCheckBox>
|
||||
#include <QDir>
|
||||
#include <QFileDialog>
|
||||
|
|
@ -25,7 +26,14 @@
|
|||
#include "oraclewizard.h"
|
||||
#include "oracleimporter.h"
|
||||
|
||||
#define ALLSETS_URL "http://mtgjson.com/json/AllSets.json"
|
||||
#ifdef HAS_ZLIB
|
||||
#include "zip/unzip.h"
|
||||
#define ZIP_SIGNATURE "PK"
|
||||
#define ALLSETS_URL "http://mtgjson.com/json/AllSets.json.zip"
|
||||
#else
|
||||
#define ALLSETS_URL "http://mtgjson.com/json/AllSets.json"
|
||||
#endif
|
||||
|
||||
|
||||
OracleWizard::OracleWizard(QWidget *parent)
|
||||
: QWizard(parent)
|
||||
|
|
@ -141,7 +149,12 @@ void LoadSetsPage::actLoadSetsFile()
|
|||
{
|
||||
QFileDialog dialog(this, tr("Load sets file"));
|
||||
dialog.setFileMode(QFileDialog::ExistingFile);
|
||||
dialog.setNameFilter("Sets JSON file (*.json)");
|
||||
|
||||
#ifdef HAS_ZLIB
|
||||
dialog.setNameFilter(tr("Sets JSON file (*.json *.zip)"));
|
||||
#else
|
||||
dialog.setNameFilter(tr("Sets JSON file (*.json)"));
|
||||
#endif
|
||||
|
||||
if(!fileLineEdit->text().isEmpty() && QFile::exists(fileLineEdit->text()))
|
||||
dialog.selectFile(fileLineEdit->text());
|
||||
|
|
@ -194,7 +207,7 @@ bool LoadSetsPage::validatePage()
|
|||
return false;
|
||||
}
|
||||
|
||||
if (!setsFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
if (!setsFile.open(QIODevice::ReadOnly)) {
|
||||
QMessageBox::critical(0, tr("Error"), tr("Cannot open file '%1'.").arg(fileLineEdit->text()));
|
||||
return false;
|
||||
}
|
||||
|
|
@ -256,6 +269,58 @@ void LoadSetsPage::readSetsFromByteArray(QByteArray data)
|
|||
progressLabel->show();
|
||||
progressBar->show();
|
||||
|
||||
// unzip the file if needed
|
||||
if(data.startsWith(ZIP_SIGNATURE))
|
||||
{
|
||||
#ifdef HAS_ZLIB
|
||||
// zipped file
|
||||
QBuffer *inBuffer = new QBuffer(&data);
|
||||
QBuffer *outBuffer = new QBuffer(this);
|
||||
QString fileName;
|
||||
UnZip::ErrorCode ec;
|
||||
UnZip uz;
|
||||
|
||||
ec = uz.openArchive(inBuffer);
|
||||
if (ec != UnZip::Ok) {
|
||||
QMessageBox::critical(this, tr("Error"), tr("Failed to open Zip archive: %1").arg(uz.formatError(ec)));
|
||||
|
||||
wizard()->enableButtons();
|
||||
setEnabled(true);
|
||||
return;
|
||||
}
|
||||
|
||||
if(uz.fileList().size() != 1)
|
||||
{
|
||||
QMessageBox::critical(this, tr("Error"), tr("The Zip archive doesn't contain exactly one file"));
|
||||
|
||||
wizard()->enableButtons();
|
||||
setEnabled(true);
|
||||
return;
|
||||
}
|
||||
fileName = uz.fileList().at(0);
|
||||
|
||||
outBuffer->open(QBuffer::ReadWrite);
|
||||
ec = uz.extractFile(fileName, outBuffer);
|
||||
if (ec != UnZip::Ok) {
|
||||
QMessageBox::critical(this, tr("Error"), tr("Zip extraction failed: %1").arg(uz.formatError(ec)));
|
||||
uz.closeArchive();
|
||||
|
||||
wizard()->enableButtons();
|
||||
setEnabled(true);
|
||||
return;
|
||||
}
|
||||
|
||||
future = QtConcurrent::run(wizard()->importer, &OracleImporter::readSetsFromByteArray, outBuffer->data());
|
||||
watcher.setFuture(future);
|
||||
return;
|
||||
#else
|
||||
QMessageBox::critical(this, tr("Error"), tr("Sorry, this version of Oracle does not support zipped files."));
|
||||
|
||||
wizard()->enableButtons();
|
||||
setEnabled(true);
|
||||
return;
|
||||
#endif
|
||||
}
|
||||
// Start the computation.
|
||||
future = QtConcurrent::run(wizard()->importer, &OracleImporter::readSetsFromByteArray, data);
|
||||
watcher.setFuture(future);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue