mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-01-09 19:57:28 -08:00
Automatic Spoiler Season (#2991)
* oracle now can be run in spoiler or normal mode * tests for travis * only run on relaunch * spoilers in client (not oracle now) and tray icon shows when done * spoiler status will be checked before downloading spoiler file * only download if they care about spoilers * reload db on spoiler download * manual update button, code cleanup, and fix enabling sets when new * cleanup, nullchecks, and fixes to spoiler * reload DB even if not in spoiler season; necessary as we have a check elsewhere to prevent the reload if spoiler check happens * Implement changes from 2991#issuecomment-356169374 * Change implicit nullptrs, alert on file deletion, minor changes * make reload thread safe and minor changes from 2991#issuecomment-356450302 * Fix locking * Disable update now button while process running
This commit is contained in:
@@ -37,21 +37,28 @@
|
||||
#endif
|
||||
|
||||
#define TOKENS_URL "https://raw.githubusercontent.com/Cockatrice/Magic-Token/master/tokens.xml"
|
||||
#define SPOILERS_URL "https://raw.githubusercontent.com/Cockatrice/Magic-Spoiler/files/spoiler.xml"
|
||||
|
||||
|
||||
OracleWizard::OracleWizard(QWidget *parent)
|
||||
: QWizard(parent)
|
||||
OracleWizard::OracleWizard(QWidget *parent) : QWizard(parent)
|
||||
{
|
||||
settings = new QSettings(settingsCache->getSettingsPath()+"global.ini",QSettings::IniFormat, this);
|
||||
connect(settingsCache, SIGNAL(langChanged()), this, SLOT(updateLanguage()));
|
||||
|
||||
importer = new OracleImporter(settingsCache->getDataPath(), this);
|
||||
|
||||
addPage(new IntroPage);
|
||||
addPage(new LoadSetsPage);
|
||||
addPage(new SaveSetsPage);
|
||||
addPage(new LoadTokensPage);
|
||||
addPage(new SaveTokensPage);
|
||||
if (! isSpoilersOnly)
|
||||
{
|
||||
addPage(new IntroPage);
|
||||
addPage(new LoadSetsPage);
|
||||
addPage(new SaveSetsPage);
|
||||
addPage(new LoadTokensPage);
|
||||
addPage(new SaveTokensPage);
|
||||
}
|
||||
else
|
||||
{
|
||||
addPage(new LoadSpoilersPage);
|
||||
addPage(new SaveSpoilersPage);
|
||||
}
|
||||
|
||||
retranslateUi();
|
||||
}
|
||||
@@ -65,7 +72,10 @@ void OracleWizard::updateLanguage()
|
||||
void OracleWizard::changeEvent(QEvent *event)
|
||||
{
|
||||
if (event->type() == QEvent::LanguageChange)
|
||||
{
|
||||
retranslateUi();
|
||||
}
|
||||
|
||||
QDialog::changeEvent(event);
|
||||
}
|
||||
|
||||
@@ -75,7 +85,9 @@ void OracleWizard::retranslateUi()
|
||||
QWizard::setButtonText(QWizard::FinishButton, tr("Save"));
|
||||
|
||||
for (int i = 0; i < pageIds().count(); i++)
|
||||
{
|
||||
dynamic_cast<OracleWizardPage *>(page(i))->retranslateUi();
|
||||
}
|
||||
}
|
||||
|
||||
void OracleWizard::accept()
|
||||
@@ -98,22 +110,23 @@ void OracleWizard::disableButtons()
|
||||
bool OracleWizard::saveTokensToFile(const QString & fileName)
|
||||
{
|
||||
QFile file(fileName);
|
||||
if(!file.open(QIODevice::WriteOnly))
|
||||
if (!file.open(QIODevice::WriteOnly))
|
||||
{
|
||||
qDebug() << "File open (w) failed for" << fileName;
|
||||
return false;
|
||||
}
|
||||
if(file.write(tokensData) == -1)
|
||||
|
||||
if (file.write(tokensData) == -1)
|
||||
{
|
||||
qDebug() << "File write (w) failed for" << fileName;
|
||||
return false;
|
||||
}
|
||||
|
||||
file.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
IntroPage::IntroPage(QWidget *parent)
|
||||
: OracleWizardPage(parent)
|
||||
IntroPage::IntroPage(QWidget *parent) : OracleWizardPage(parent)
|
||||
{
|
||||
label = new QLabel(this);
|
||||
label->setWordWrap(true);
|
||||
@@ -122,16 +135,21 @@ IntroPage::IntroPage(QWidget *parent)
|
||||
versionLabel = new QLabel(this);
|
||||
languageBox = new QComboBox(this);
|
||||
QString setLanguage = settingsCache->getLang();
|
||||
|
||||
QStringList qmFiles = findQmFiles();
|
||||
for (int i = 0; i < qmFiles.size(); i++) {
|
||||
for (int i = 0; i < qmFiles.size(); i++)
|
||||
{
|
||||
QString langName = languageName(qmFiles[i]);
|
||||
languageBox->addItem(langName, qmFiles[i]);
|
||||
if ((qmFiles[i] == setLanguage) || (setLanguage.isEmpty() && langName == QCoreApplication::translate("i18n", DEFAULT_LANG_NAME)))
|
||||
{
|
||||
languageBox->setCurrentIndex(i);
|
||||
}
|
||||
}
|
||||
|
||||
connect(languageBox, SIGNAL(currentIndexChanged(int)), this, SLOT(languageBoxChanged(int)));
|
||||
|
||||
QGridLayout *layout = new QGridLayout(this);
|
||||
auto *layout = new QGridLayout(this);
|
||||
layout->addWidget(label, 0, 0, 1, 2);
|
||||
layout->addWidget(languageLabel, 1, 0);
|
||||
layout->addWidget(languageBox, 1, 1);
|
||||
@@ -150,8 +168,10 @@ QStringList IntroPage::findQmFiles()
|
||||
|
||||
QString IntroPage::languageName(const QString &qmFile)
|
||||
{
|
||||
if(qmFile == DEFAULT_LANG_CODE)
|
||||
if (qmFile == DEFAULT_LANG_CODE)
|
||||
{
|
||||
return DEFAULT_LANG_NAME;
|
||||
}
|
||||
|
||||
QTranslator translator;
|
||||
translator.load(translationPrefix + "_" + qmFile + ".qm", translationPath);
|
||||
@@ -173,8 +193,7 @@ void IntroPage::retranslateUi()
|
||||
versionLabel->setText(tr("Version:") + QString(" %1").arg(VERSION_STRING));
|
||||
}
|
||||
|
||||
LoadSetsPage::LoadSetsPage(QWidget *parent)
|
||||
: OracleWizardPage(parent), nam(0)
|
||||
LoadSetsPage::LoadSetsPage(QWidget *parent) : OracleWizardPage(parent), nam(nullptr)
|
||||
{
|
||||
urlRadioButton = new QRadioButton(this);
|
||||
fileRadioButton = new QRadioButton(this);
|
||||
@@ -193,7 +212,7 @@ LoadSetsPage::LoadSetsPage(QWidget *parent)
|
||||
fileButton = new QPushButton(this);
|
||||
connect(fileButton, SIGNAL(clicked()), this, SLOT(actLoadSetsFile()));
|
||||
|
||||
QGridLayout *layout = new QGridLayout(this);
|
||||
auto *layout = new QGridLayout(this);
|
||||
layout->addWidget(urlRadioButton, 0, 0);
|
||||
layout->addWidget(urlLineEdit, 0, 1);
|
||||
layout->addWidget(urlButton, 1, 1, Qt::AlignRight);
|
||||
@@ -245,11 +264,15 @@ void LoadSetsPage::actLoadSetsFile()
|
||||
dialog.setNameFilter(tr("Sets JSON file (*.json)"));
|
||||
#endif
|
||||
|
||||
if(!fileLineEdit->text().isEmpty() && QFile::exists(fileLineEdit->text()))
|
||||
if (!fileLineEdit->text().isEmpty() && QFile::exists(fileLineEdit->text()))
|
||||
{
|
||||
dialog.selectFile(fileLineEdit->text());
|
||||
}
|
||||
|
||||
if (!dialog.exec())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
fileLineEdit->setText(dialog.selectedFiles().at(0));
|
||||
}
|
||||
@@ -257,14 +280,16 @@ void LoadSetsPage::actLoadSetsFile()
|
||||
bool LoadSetsPage::validatePage()
|
||||
{
|
||||
// once the import is finished, we call next(); skip validation
|
||||
if(wizard()->importer->getSets().count() > 0)
|
||||
if (wizard()->importer->getSets().count() > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// else, try to import sets
|
||||
if(urlRadioButton->isChecked())
|
||||
if (urlRadioButton->isChecked())
|
||||
{
|
||||
QUrl url = QUrl::fromUserInput(urlLineEdit->text());
|
||||
if(!url.isValid())
|
||||
if (!url.isValid())
|
||||
{
|
||||
QMessageBox::critical(this, tr("Error"), tr("The provided URL is not valid."));
|
||||
return false;
|
||||
@@ -282,16 +307,19 @@ bool LoadSetsPage::validatePage()
|
||||
setEnabled(false);
|
||||
|
||||
downloadSetsFile(url);
|
||||
} else if(fileRadioButton->isChecked()) {
|
||||
}
|
||||
else if (fileRadioButton->isChecked())
|
||||
{
|
||||
QFile setsFile(fileLineEdit->text());
|
||||
if(!setsFile.exists())
|
||||
if (!setsFile.exists())
|
||||
{
|
||||
QMessageBox::critical(this, tr("Error"), tr("Please choose a file."));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!setsFile.open(QIODevice::ReadOnly)) {
|
||||
QMessageBox::critical(0, tr("Error"), tr("Cannot open file '%1'.").arg(fileLineEdit->text()));
|
||||
if (!setsFile.open(QIODevice::ReadOnly))
|
||||
{
|
||||
QMessageBox::critical(nullptr, tr("Error"), tr("Cannot open file '%1'.").arg(fileLineEdit->text()));
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -301,13 +329,16 @@ bool LoadSetsPage::validatePage()
|
||||
readSetsFromByteArray(setsFile.readAll());
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void LoadSetsPage::downloadSetsFile(QUrl url)
|
||||
{
|
||||
if(!nam)
|
||||
if (!nam)
|
||||
{
|
||||
nam = new QNetworkAccessManager(this);
|
||||
}
|
||||
QNetworkReply *reply = nam->get(QNetworkRequest(url));
|
||||
|
||||
connect(reply, SIGNAL(finished()), this, SLOT(actDownloadFinishedSetsFile()));
|
||||
@@ -316,10 +347,10 @@ void LoadSetsPage::downloadSetsFile(QUrl url)
|
||||
|
||||
void LoadSetsPage::actDownloadProgressSetsFile(qint64 received, qint64 total)
|
||||
{
|
||||
if(total > 0)
|
||||
if (total > 0)
|
||||
{
|
||||
progressBar->setMaximum(total);
|
||||
progressBar->setValue(received);
|
||||
progressBar->setMaximum(static_cast<int>(total));
|
||||
progressBar->setValue(static_cast<int>(received));
|
||||
}
|
||||
progressLabel->setText(tr("Downloading (%1MB)").arg((int) received / (1024 * 1024)));
|
||||
}
|
||||
@@ -327,9 +358,10 @@ void LoadSetsPage::actDownloadProgressSetsFile(qint64 received, qint64 total)
|
||||
void LoadSetsPage::actDownloadFinishedSetsFile()
|
||||
{
|
||||
// check for a reply
|
||||
QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
|
||||
auto *reply = dynamic_cast<QNetworkReply *>(sender());
|
||||
QNetworkReply::NetworkError errorCode = reply->error();
|
||||
if (errorCode != QNetworkReply::NoError) {
|
||||
if (errorCode != QNetworkReply::NoError)
|
||||
{
|
||||
QMessageBox::critical(this, tr("Error"), tr("Network error: %1.").arg(reply->errorString()));
|
||||
|
||||
wizard()->enableButtons();
|
||||
@@ -340,7 +372,8 @@ void LoadSetsPage::actDownloadFinishedSetsFile()
|
||||
}
|
||||
|
||||
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
if (statusCode == 301 || statusCode == 302) {
|
||||
if (statusCode == 301 || statusCode == 302)
|
||||
{
|
||||
QUrl redirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
|
||||
qDebug() << "following redirect url:" << redirectUrl.toString();
|
||||
downloadSetsFile(redirectUrl);
|
||||
@@ -352,10 +385,14 @@ void LoadSetsPage::actDownloadFinishedSetsFile()
|
||||
progressBar->hide();
|
||||
|
||||
// save allsets.json url, but only if the user customized it and download was successfull
|
||||
if(urlLineEdit->text() != QString(ALLSETS_URL))
|
||||
if (urlLineEdit->text() != QString(ALLSETS_URL))
|
||||
{
|
||||
wizard()->settings->setValue("allsetsurl", urlLineEdit->text());
|
||||
}
|
||||
else
|
||||
{
|
||||
wizard()->settings->remove("allsetsurl");
|
||||
}
|
||||
|
||||
readSetsFromByteArray(reply->readAll());
|
||||
reply->deleteLater();
|
||||
@@ -376,19 +413,20 @@ void LoadSetsPage::readSetsFromByteArray(QByteArray data)
|
||||
{
|
||||
#ifdef HAS_ZLIB
|
||||
// zipped file
|
||||
QBuffer *inBuffer = new QBuffer(&data);
|
||||
QBuffer *outBuffer = new QBuffer(this);
|
||||
auto *inBuffer = new QBuffer(&data);
|
||||
auto *outBuffer = new QBuffer(this);
|
||||
QString fileName;
|
||||
UnZip::ErrorCode ec;
|
||||
UnZip uz;
|
||||
|
||||
ec = uz.openArchive(inBuffer);
|
||||
if (ec != UnZip::Ok) {
|
||||
if (ec != UnZip::Ok)
|
||||
{
|
||||
zipDownloadFailed(tr("Failed to open Zip archive: %1.").arg(uz.formatError(ec)));
|
||||
return;
|
||||
}
|
||||
|
||||
if(uz.fileList().size() != 1)
|
||||
if (uz.fileList().size() != 1)
|
||||
{
|
||||
zipDownloadFailed(tr("Zip extraction failed: the Zip archive doesn't contain exactly one file."));
|
||||
return;
|
||||
@@ -397,7 +435,8 @@ void LoadSetsPage::readSetsFromByteArray(QByteArray data)
|
||||
|
||||
outBuffer->open(QBuffer::ReadWrite);
|
||||
ec = uz.extractFile(fileName, outBuffer);
|
||||
if (ec != UnZip::Ok) {
|
||||
if (ec != UnZip::Ok)
|
||||
{
|
||||
zipDownloadFailed(tr("Zip extraction failed: %1.").arg(uz.formatError(ec)));
|
||||
uz.closeArchive();
|
||||
return;
|
||||
@@ -429,7 +468,8 @@ void LoadSetsPage::zipDownloadFailed(const QString &message)
|
||||
progressBar->hide();
|
||||
|
||||
QMessageBox::StandardButton reply;
|
||||
reply = QMessageBox::question(this, tr("Error"), message + "<br/>" + tr("Do you want to try to download a fresh copy of the uncompressed file instead?"), QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
|
||||
reply = static_cast<QMessageBox::StandardButton>(QMessageBox::question(this, tr("Error"), message + "<br/>" + tr("Do you want to try to download a fresh copy of the uncompressed file instead?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes));
|
||||
|
||||
if (reply == QMessageBox::Yes)
|
||||
{
|
||||
urlRadioButton->setChecked(true);
|
||||
@@ -446,16 +486,17 @@ void LoadSetsPage::importFinished()
|
||||
progressLabel->hide();
|
||||
progressBar->hide();
|
||||
|
||||
if(watcher.future().result())
|
||||
if (watcher.future().result())
|
||||
{
|
||||
wizard()->next();
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
QMessageBox::critical(this, tr("Error"), tr("The file was retrieved successfully, but it does not contain any sets data."));
|
||||
}
|
||||
}
|
||||
|
||||
SaveSetsPage::SaveSetsPage(QWidget *parent)
|
||||
: OracleWizardPage(parent)
|
||||
SaveSetsPage::SaveSetsPage(QWidget *parent) : OracleWizardPage(parent)
|
||||
{
|
||||
defaultPathCheckBox = new QCheckBox(this);
|
||||
defaultPathCheckBox->setChecked(true);
|
||||
@@ -463,7 +504,7 @@ SaveSetsPage::SaveSetsPage(QWidget *parent)
|
||||
messageLog = new QTextEdit(this);
|
||||
messageLog->setReadOnly(true);
|
||||
|
||||
QGridLayout *layout = new QGridLayout(this);
|
||||
auto *layout = new QGridLayout(this);
|
||||
layout->addWidget(defaultPathCheckBox, 0, 0);
|
||||
layout->addWidget(messageLog, 1, 0);
|
||||
|
||||
@@ -472,7 +513,7 @@ SaveSetsPage::SaveSetsPage(QWidget *parent)
|
||||
|
||||
void SaveSetsPage::cleanupPage()
|
||||
{
|
||||
disconnect(wizard()->importer, SIGNAL(setIndexChanged(int, int, const QString &)), 0, 0);
|
||||
disconnect(wizard()->importer, SIGNAL(setIndexChanged(int, int, const QString &)), nullptr, nullptr);
|
||||
}
|
||||
|
||||
void SaveSetsPage::initializePage()
|
||||
@@ -482,7 +523,9 @@ void SaveSetsPage::initializePage()
|
||||
connect(wizard()->importer, SIGNAL(setIndexChanged(int, int, const QString &)), this, SLOT(updateTotalProgress(int, int, const QString &)));
|
||||
|
||||
if (!wizard()->importer->startImport())
|
||||
{
|
||||
QMessageBox::critical(this, tr("Error"), tr("No set has been imported."));
|
||||
}
|
||||
}
|
||||
|
||||
void SaveSetsPage::retranslateUi()
|
||||
@@ -496,11 +539,15 @@ void SaveSetsPage::retranslateUi()
|
||||
|
||||
void SaveSetsPage::updateTotalProgress(int cardsImported, int /* setIndex */, const QString &setName)
|
||||
{
|
||||
if (setName.isEmpty()) {
|
||||
if (setName.isEmpty())
|
||||
{
|
||||
messageLog->append("<b>" + tr("Import finished: %1 cards.").arg(wizard()->importer->getCardList().size()) + "</b>");
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
messageLog->append(tr("%1: %2 cards imported").arg(setName).arg(cardsImported));
|
||||
}
|
||||
|
||||
messageLog->verticalScrollBar()->setValue(messageLog->verticalScrollBar()->maximum());
|
||||
}
|
||||
|
||||
@@ -511,39 +558,51 @@ bool SaveSetsPage::validatePage()
|
||||
QString windowName = tr("Save card database");
|
||||
QString fileType = tr("XML; card database (*.xml)");
|
||||
|
||||
do {
|
||||
do
|
||||
{
|
||||
QString fileName;
|
||||
if (defaultPathCheckBox->isChecked())
|
||||
{
|
||||
fileName = defaultPath;
|
||||
}
|
||||
else
|
||||
{
|
||||
fileName = QFileDialog::getSaveFileName(this, windowName, defaultPath, fileType);
|
||||
}
|
||||
|
||||
if (fileName.isEmpty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
QFileInfo fi(fileName);
|
||||
QDir fileDir(fi.path());
|
||||
if (!fileDir.exists() && !fileDir.mkpath(fileDir.absolutePath())) {
|
||||
if (!fileDir.exists() && !fileDir.mkpath(fileDir.absolutePath()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (wizard()->importer->saveToFile(fileName))
|
||||
{
|
||||
ok = true;
|
||||
QMessageBox::information(this,
|
||||
tr("Success"),
|
||||
tr("The card database has been saved successfully to\n%1").arg(fileName));
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
QMessageBox::critical(this, tr("Error"), tr("The file could not be saved to %1").arg(fileName));;
|
||||
if (defaultPathCheckBox->isChecked())
|
||||
{
|
||||
defaultPathCheckBox->setChecked(false);
|
||||
}
|
||||
}
|
||||
} while (!ok);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
LoadTokensPage::LoadTokensPage(QWidget *parent)
|
||||
: OracleWizardPage(parent), nam(0)
|
||||
LoadSpoilersPage::LoadSpoilersPage(QWidget *parent) : OracleWizardPage(parent), nam(nullptr)
|
||||
{
|
||||
urlLabel = new QLabel(this);
|
||||
urlLineEdit = new QLineEdit(this);
|
||||
@@ -554,7 +613,152 @@ LoadTokensPage::LoadTokensPage(QWidget *parent)
|
||||
urlButton = new QPushButton(this);
|
||||
connect(urlButton, SIGNAL(clicked()), this, SLOT(actRestoreDefaultUrl()));
|
||||
|
||||
QGridLayout *layout = new QGridLayout(this);
|
||||
auto *layout = new QGridLayout(this);
|
||||
layout->addWidget(urlLabel, 0, 0);
|
||||
layout->addWidget(urlLineEdit, 0, 1);
|
||||
layout->addWidget(urlButton, 1, 1, Qt::AlignRight);
|
||||
layout->addWidget(progressLabel, 2, 0);
|
||||
layout->addWidget(progressBar, 2, 1);
|
||||
}
|
||||
|
||||
void LoadSpoilersPage::actRestoreDefaultUrl()
|
||||
{
|
||||
urlLineEdit->setText(SPOILERS_URL);
|
||||
}
|
||||
|
||||
void LoadSpoilersPage::initializePage()
|
||||
{
|
||||
urlLineEdit->setText(wizard()->settings->value("spoilersurl", SPOILERS_URL).toString());
|
||||
|
||||
progressLabel->hide();
|
||||
progressBar->hide();
|
||||
}
|
||||
|
||||
void LoadSpoilersPage::actDownloadProgressSpoilersFile(qint64 received, qint64 total)
|
||||
{
|
||||
if (total > 0)
|
||||
{
|
||||
progressBar->setMaximum(static_cast<int>(total));
|
||||
progressBar->setValue(static_cast<int>(received));
|
||||
}
|
||||
|
||||
progressLabel->setText(tr("Downloading (%1MB)").arg((int) received / (1024 * 1024)));
|
||||
}
|
||||
|
||||
void LoadSpoilersPage::actDownloadFinishedSpoilersFile()
|
||||
{
|
||||
// Check for server reply
|
||||
auto *reply = dynamic_cast<QNetworkReply *>(sender());
|
||||
QNetworkReply::NetworkError errorCode = reply->error();
|
||||
|
||||
if (errorCode != QNetworkReply::NoError)
|
||||
{
|
||||
QMessageBox::critical(this, tr("Error"), tr("Network error: %1.").arg(reply->errorString()));
|
||||
|
||||
wizard()->enableButtons();
|
||||
setEnabled(true);
|
||||
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
|
||||
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
if (statusCode == 301 || statusCode == 302)
|
||||
{
|
||||
QUrl redirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
|
||||
qDebug() << "following redirect url:" << redirectUrl.toString();
|
||||
downloadSpoilersFile(redirectUrl);
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
|
||||
progressLabel->hide();
|
||||
progressBar->hide();
|
||||
|
||||
// save spoiler.xml url, but only if the user customized it and download was successful
|
||||
if (urlLineEdit->text() != QString(SPOILERS_URL))
|
||||
{
|
||||
wizard()->settings->setValue("spoilersurl", urlLineEdit->text());
|
||||
}
|
||||
else
|
||||
{
|
||||
wizard()->settings->remove("spoilersurl");
|
||||
}
|
||||
|
||||
wizard()->setTokensData(reply->readAll());
|
||||
reply->deleteLater();
|
||||
|
||||
wizard()->enableButtons();
|
||||
setEnabled(true);
|
||||
progressLabel->hide();
|
||||
progressBar->hide();
|
||||
|
||||
wizard()->next();
|
||||
}
|
||||
|
||||
void LoadSpoilersPage::downloadSpoilersFile(QUrl url)
|
||||
{
|
||||
if (!nam)
|
||||
{
|
||||
nam = new QNetworkAccessManager(this);
|
||||
}
|
||||
QNetworkReply *reply = nam->get(QNetworkRequest(url));
|
||||
|
||||
connect(reply, SIGNAL(finished()), this, SLOT(actDownloadFinishedSpoilersFile()));
|
||||
connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(actDownloadProgressSpoilersFile(qint64, qint64)));
|
||||
}
|
||||
|
||||
bool LoadSpoilersPage::validatePage()
|
||||
{
|
||||
// once the import is finished, we call next(); skip validation
|
||||
if (wizard()->hasTokensData())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
QUrl url = QUrl::fromUserInput(urlLineEdit->text());
|
||||
if (!url.isValid())
|
||||
{
|
||||
QMessageBox::critical(this, tr("Error"), tr("The provided URL is not valid."));
|
||||
return false;
|
||||
}
|
||||
|
||||
progressLabel->setText(tr("Downloading (0MB)"));
|
||||
// show an infinite progressbar
|
||||
progressBar->setMaximum(0);
|
||||
progressBar->setMinimum(0);
|
||||
progressBar->setValue(0);
|
||||
progressLabel->show();
|
||||
progressBar->show();
|
||||
|
||||
wizard()->disableButtons();
|
||||
setEnabled(false);
|
||||
|
||||
downloadSpoilersFile(url);
|
||||
return false;
|
||||
}
|
||||
|
||||
void LoadSpoilersPage::retranslateUi()
|
||||
{
|
||||
setTitle(tr("Spoilers source selection"));
|
||||
setSubTitle(tr("Please specify a spoiler source."));
|
||||
|
||||
urlLabel->setText(tr("Download URL:"));
|
||||
urlButton->setText(tr("Restore default URL"));
|
||||
}
|
||||
|
||||
LoadTokensPage::LoadTokensPage(QWidget *parent) : OracleWizardPage(parent), nam(nullptr)
|
||||
{
|
||||
urlLabel = new QLabel(this);
|
||||
urlLineEdit = new QLineEdit(this);
|
||||
|
||||
progressLabel = new QLabel(this);
|
||||
progressBar = new QProgressBar(this);
|
||||
|
||||
urlButton = new QPushButton(this);
|
||||
connect(urlButton, SIGNAL(clicked()), this, SLOT(actRestoreDefaultUrl()));
|
||||
|
||||
auto *layout = new QGridLayout(this);
|
||||
layout->addWidget(urlLabel, 0, 0);
|
||||
layout->addWidget(urlLineEdit, 0, 1);
|
||||
layout->addWidget(urlButton, 1, 1, Qt::AlignRight);
|
||||
@@ -589,11 +793,13 @@ void LoadTokensPage::actRestoreDefaultUrl()
|
||||
bool LoadTokensPage::validatePage()
|
||||
{
|
||||
// once the import is finished, we call next(); skip validation
|
||||
if(wizard()->hasTokensData())
|
||||
if (wizard()->hasTokensData())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
QUrl url = QUrl::fromUserInput(urlLineEdit->text());
|
||||
if(!url.isValid())
|
||||
if (!url.isValid())
|
||||
{
|
||||
QMessageBox::critical(this, tr("Error"), tr("The provided URL is not valid."));
|
||||
return false;
|
||||
@@ -616,8 +822,10 @@ bool LoadTokensPage::validatePage()
|
||||
|
||||
void LoadTokensPage::downloadTokensFile(QUrl url)
|
||||
{
|
||||
if(!nam)
|
||||
if (!nam)
|
||||
{
|
||||
nam = new QNetworkAccessManager(this);
|
||||
}
|
||||
QNetworkReply *reply = nam->get(QNetworkRequest(url));
|
||||
|
||||
connect(reply, SIGNAL(finished()), this, SLOT(actDownloadFinishedTokensFile()));
|
||||
@@ -626,10 +834,10 @@ void LoadTokensPage::downloadTokensFile(QUrl url)
|
||||
|
||||
void LoadTokensPage::actDownloadProgressTokensFile(qint64 received, qint64 total)
|
||||
{
|
||||
if(total > 0)
|
||||
if (total > 0)
|
||||
{
|
||||
progressBar->setMaximum(total);
|
||||
progressBar->setValue(received);
|
||||
progressBar->setMaximum(static_cast<int>(total));
|
||||
progressBar->setValue(static_cast<int>(received));
|
||||
}
|
||||
progressLabel->setText(tr("Downloading (%1MB)").arg((int) received / (1024 * 1024)));
|
||||
}
|
||||
@@ -637,9 +845,10 @@ void LoadTokensPage::actDownloadProgressTokensFile(qint64 received, qint64 total
|
||||
void LoadTokensPage::actDownloadFinishedTokensFile()
|
||||
{
|
||||
// check for a reply
|
||||
QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
|
||||
auto *reply = dynamic_cast<QNetworkReply *>(sender());
|
||||
QNetworkReply::NetworkError errorCode = reply->error();
|
||||
if (errorCode != QNetworkReply::NoError) {
|
||||
if (errorCode != QNetworkReply::NoError)
|
||||
{
|
||||
QMessageBox::critical(this, tr("Error"), tr("Network error: %1.").arg(reply->errorString()));
|
||||
|
||||
wizard()->enableButtons();
|
||||
@@ -650,7 +859,8 @@ void LoadTokensPage::actDownloadFinishedTokensFile()
|
||||
}
|
||||
|
||||
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
if (statusCode == 301 || statusCode == 302) {
|
||||
if (statusCode == 301 || statusCode == 302)
|
||||
{
|
||||
QUrl redirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
|
||||
qDebug() << "following redirect url:" << redirectUrl.toString();
|
||||
downloadTokensFile(redirectUrl);
|
||||
@@ -662,10 +872,14 @@ void LoadTokensPage::actDownloadFinishedTokensFile()
|
||||
progressBar->hide();
|
||||
|
||||
// save tokens.xml url, but only if the user customized it and download was successfull
|
||||
if(urlLineEdit->text() != QString(TOKENS_URL))
|
||||
if (urlLineEdit->text() != QString(TOKENS_URL))
|
||||
{
|
||||
wizard()->settings->setValue("tokensurl", urlLineEdit->text());
|
||||
}
|
||||
else
|
||||
{
|
||||
wizard()->settings->remove("tokensurl");
|
||||
}
|
||||
|
||||
wizard()->setTokensData(reply->readAll());
|
||||
reply->deleteLater();
|
||||
@@ -678,13 +892,81 @@ void LoadTokensPage::actDownloadFinishedTokensFile()
|
||||
wizard()->next();
|
||||
}
|
||||
|
||||
SaveTokensPage::SaveTokensPage(QWidget *parent)
|
||||
: OracleWizardPage(parent)
|
||||
SaveSpoilersPage::SaveSpoilersPage(QWidget *parent) : OracleWizardPage(parent)
|
||||
{
|
||||
defaultPathCheckBox = new QCheckBox(this);
|
||||
defaultPathCheckBox->setChecked(true);
|
||||
|
||||
QGridLayout *layout = new QGridLayout(this);
|
||||
auto *layout = new QGridLayout(this);
|
||||
layout->addWidget(defaultPathCheckBox, 0, 0);
|
||||
|
||||
setLayout(layout);
|
||||
|
||||
}
|
||||
|
||||
void SaveSpoilersPage::retranslateUi()
|
||||
{
|
||||
setTitle(tr("Spoilers imported"));
|
||||
setSubTitle(tr("The spoilers file has been imported. "
|
||||
"Press \"Save\" to save the imported spoilers to the Cockatrice card database."));
|
||||
|
||||
defaultPathCheckBox->setText(tr("Save to the default path (recommended)"));
|
||||
}
|
||||
|
||||
bool SaveSpoilersPage::validatePage()
|
||||
{
|
||||
bool ok = false;
|
||||
QString defaultPath = settingsCache->getSpoilerCardDatabasePath();
|
||||
QString windowName = tr("Save spoiler database");
|
||||
QString fileType = tr("XML; card database (*.xml)");
|
||||
|
||||
do
|
||||
{
|
||||
QString fileName;
|
||||
if (defaultPathCheckBox->isChecked())
|
||||
{
|
||||
fileName = defaultPath;
|
||||
}
|
||||
else
|
||||
{
|
||||
fileName = QFileDialog::getSaveFileName(this, windowName, defaultPath, fileType);
|
||||
}
|
||||
|
||||
if (fileName.isEmpty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
QFileInfo fi(fileName);
|
||||
QDir fileDir(fi.path());
|
||||
if (!fileDir.exists() && !fileDir.mkpath(fileDir.absolutePath()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (wizard()->saveTokensToFile(fileName))
|
||||
{
|
||||
ok = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
QMessageBox::critical(this, tr("Error"), tr("The file could not be saved to %1").arg(fileName));;
|
||||
if (defaultPathCheckBox->isChecked())
|
||||
{
|
||||
defaultPathCheckBox->setChecked(false);
|
||||
}
|
||||
}
|
||||
} while (!ok);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
SaveTokensPage::SaveTokensPage(QWidget *parent) : OracleWizardPage(parent)
|
||||
{
|
||||
defaultPathCheckBox = new QCheckBox(this);
|
||||
defaultPathCheckBox->setChecked(true);
|
||||
|
||||
auto *layout = new QGridLayout(this);
|
||||
layout->addWidget(defaultPathCheckBox, 0, 0);
|
||||
|
||||
setLayout(layout);
|
||||
@@ -706,33 +988,47 @@ bool SaveTokensPage::validatePage()
|
||||
QString windowName = tr("Save token database");
|
||||
QString fileType = tr("XML; token database (*.xml)");
|
||||
|
||||
do {
|
||||
do
|
||||
{
|
||||
QString fileName;
|
||||
if (defaultPathCheckBox->isChecked())
|
||||
{
|
||||
fileName = defaultPath;
|
||||
}
|
||||
else
|
||||
{
|
||||
fileName = QFileDialog::getSaveFileName(this, windowName, defaultPath, fileType);
|
||||
}
|
||||
|
||||
if (fileName.isEmpty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
QFileInfo fi(fileName);
|
||||
QDir fileDir(fi.path());
|
||||
if (!fileDir.exists() && !fileDir.mkpath(fileDir.absolutePath())) {
|
||||
if (!fileDir.exists() && !fileDir.mkpath(fileDir.absolutePath()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (wizard()->saveTokensToFile(fileName))
|
||||
{
|
||||
ok = true;
|
||||
QMessageBox::information(this,
|
||||
tr("Success"),
|
||||
tr("The token database has been saved successfully to\n%1").arg(fileName));
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
QMessageBox::critical(this, tr("Error"), tr("The file could not be saved to %1").arg(fileName));;
|
||||
if (defaultPathCheckBox->isChecked())
|
||||
{
|
||||
defaultPathCheckBox->setChecked(false);
|
||||
}
|
||||
}
|
||||
} while (!ok);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user