[Oracle] Support importing tokens and spoilers from local file (#6387)

This commit is contained in:
RickyRister
2025-12-02 21:19:56 -08:00
committed by GitHub
parent 658ae83157
commit b4e3f2cba9
4 changed files with 105 additions and 25 deletions

View File

@@ -672,13 +672,21 @@ QString LoadTokensPage::getFileType()
return tr("XML; token database (*.xml)"); return tr("XML; token database (*.xml)");
} }
QString LoadTokensPage::getFilePromptName()
{
return tr("tokens");
}
void LoadTokensPage::retranslateUi() void LoadTokensPage::retranslateUi()
{ {
setTitle(tr("Tokens import")); setTitle(tr("Tokens import"));
setSubTitle(tr("Please specify a compatible source for token data.")); setSubTitle(tr("Please specify a compatible source for token data."));
urlLabel->setText(tr("Download URL:")); urlRadioButton->setText(tr("Download URL:"));
fileRadioButton->setText(tr("Local file:"));
urlButton->setText(tr("Restore default URL")); urlButton->setText(tr("Restore default URL"));
fileButton->setText(tr("Choose file..."));
pathLabel->setText(tr("The token database will be saved at the following location:") + "<br>" + pathLabel->setText(tr("The token database will be saved at the following location:") + "<br>" +
SettingsCache::instance().getTokenDatabasePath()); SettingsCache::instance().getTokenDatabasePath());
defaultPathCheckBox->setText(tr("Save to a custom path (not recommended)")); defaultPathCheckBox->setText(tr("Save to a custom path (not recommended)"));
@@ -709,13 +717,21 @@ QString LoadSpoilersPage::getFileType()
return tr("XML; spoiler database (*.xml)"); return tr("XML; spoiler database (*.xml)");
} }
QString LoadSpoilersPage::getFilePromptName()
{
return tr("spoiler");
}
void LoadSpoilersPage::retranslateUi() void LoadSpoilersPage::retranslateUi()
{ {
setTitle(tr("Spoilers import")); setTitle(tr("Spoilers import"));
setSubTitle(tr("Please specify a compatible source for spoiler data.")); setSubTitle(tr("Please specify a compatible source for spoiler data."));
urlLabel->setText(tr("Download URL:")); urlRadioButton->setText(tr("Download URL:"));
fileRadioButton->setText(tr("Local file:"));
urlButton->setText(tr("Restore default URL")); urlButton->setText(tr("Restore default URL"));
fileButton->setText(tr("Choose file..."));
pathLabel->setText(tr("The spoiler database will be saved at the following location:") + "<br>" + pathLabel->setText(tr("The spoiler database will be saved at the following location:") + "<br>" +
SettingsCache::instance().getSpoilerCardDatabasePath()); SettingsCache::instance().getSpoilerCardDatabasePath());
defaultPathCheckBox->setText(tr("Save to a custom path (not recommended)")); defaultPathCheckBox->setText(tr("Save to a custom path (not recommended)"));

View File

@@ -131,6 +131,7 @@ protected:
QString getDefaultSavePath() override; QString getDefaultSavePath() override;
QString getWindowTitle() override; QString getWindowTitle() override;
QString getFileType() override; QString getFileType() override;
QString getFilePromptName() override;
}; };
class LoadTokensPage : public SimpleDownloadFilePage class LoadTokensPage : public SimpleDownloadFilePage
@@ -148,6 +149,7 @@ protected:
QString getDefaultSavePath() override; QString getDefaultSavePath() override;
QString getWindowTitle() override; QString getWindowTitle() override;
QString getFileType() override; QString getFileType() override;
QString getFilePromptName() override;
void initializePage() override; void initializePage() override;
}; };

View File

@@ -12,32 +12,43 @@
#include <QNetworkReply> #include <QNetworkReply>
#include <QProgressBar> #include <QProgressBar>
#include <QPushButton> #include <QPushButton>
#include <QRadioButton>
#include <QtGui> #include <QtGui>
SimpleDownloadFilePage::SimpleDownloadFilePage(QWidget *parent) : OracleWizardPage(parent) SimpleDownloadFilePage::SimpleDownloadFilePage(QWidget *parent) : OracleWizardPage(parent)
{ {
urlLabel = new QLabel(this); urlRadioButton = new QRadioButton(this);
fileRadioButton = new QRadioButton(this);
urlLineEdit = new QLineEdit(this); urlLineEdit = new QLineEdit(this);
fileLineEdit = new QLineEdit(this);
progressLabel = new QLabel(this); progressLabel = new QLabel(this);
progressBar = new QProgressBar(this); progressBar = new QProgressBar(this);
urlRadioButton->setChecked(true);
urlButton = new QPushButton(this); urlButton = new QPushButton(this);
connect(urlButton, &QPushButton::clicked, this, &SimpleDownloadFilePage::actRestoreDefaultUrl); connect(urlButton, &QPushButton::clicked, this, &SimpleDownloadFilePage::actRestoreDefaultUrl);
defaultPathCheckBox = new QCheckBox(this); fileButton = new QPushButton(this);
connect(fileButton, &QPushButton::clicked, this, &SimpleDownloadFilePage::actLoadCardFile);
defaultPathCheckBox = new QCheckBox(this);
pathLabel = new QLabel(this); pathLabel = new QLabel(this);
pathLabel->setTextInteractionFlags(Qt::TextSelectableByMouse); pathLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
auto *layout = new QGridLayout(this); auto *layout = new QGridLayout(this);
layout->addWidget(urlLabel, 0, 0); layout->addWidget(urlRadioButton, 0, 0);
layout->addWidget(urlLineEdit, 0, 1); layout->addWidget(urlLineEdit, 0, 1);
layout->addWidget(urlButton, 1, 1, Qt::AlignRight); layout->addWidget(urlButton, 1, 1, Qt::AlignRight);
layout->addWidget(pathLabel, 2, 0, 1, 2); layout->addWidget(fileRadioButton, 2, 0);
layout->addWidget(defaultPathCheckBox, 3, 0, 1, 2); layout->addWidget(fileLineEdit, 2, 1);
layout->addWidget(progressLabel, 4, 0); layout->addWidget(fileButton, 3, 1, Qt::AlignRight);
layout->addWidget(progressBar, 4, 1); layout->addWidget(pathLabel, 4, 0, 1, 2);
layout->addWidget(defaultPathCheckBox, 5, 0, 1, 2);
layout->addWidget(progressLabel, 6, 0);
layout->addWidget(progressBar, 6, 1);
setLayout(layout); setLayout(layout);
} }
@@ -56,6 +67,31 @@ void SimpleDownloadFilePage::actRestoreDefaultUrl()
urlLineEdit->setText(getDefaultUrl()); urlLineEdit->setText(getDefaultUrl());
} }
void SimpleDownloadFilePage::actLoadCardFile()
{
QFileDialog dialog(this, tr("Load %1 file").arg(getFilePromptName()));
dialog.setFileMode(QFileDialog::ExistingFile);
QString extensions = "*.json *.xml";
#ifdef HAS_ZLIB
extensions += " *.zip";
#endif
#ifdef HAS_LZMA
extensions += " *.xz";
#endif
dialog.setNameFilter(tr("%1 file (%1)").arg(getFilePromptName(), extensions));
if (!fileLineEdit->text().isEmpty() && QFile::exists(fileLineEdit->text())) {
dialog.selectFile(fileLineEdit->text());
}
if (!dialog.exec()) {
return;
}
fileLineEdit->setText(dialog.selectedFiles().at(0));
}
bool SimpleDownloadFilePage::validatePage() bool SimpleDownloadFilePage::validatePage()
{ {
// if data has already been downloaded, pass directly to the "save" step // if data has already been downloaded, pass directly to the "save" step
@@ -68,22 +104,41 @@ bool SimpleDownloadFilePage::validatePage()
} }
} }
QUrl url = QUrl::fromUserInput(urlLineEdit->text()); // else, try to import sets
if (!url.isValid()) { if (urlRadioButton->isChecked()) {
QMessageBox::critical(this, tr("Error"), tr("The provided URL is not valid: ") + url.toString()); QUrl url = QUrl::fromUserInput(urlLineEdit->text());
return false; if (!url.isValid()) {
QMessageBox::critical(this, tr("Error"), tr("The provided URL is not valid: ") + url.toString());
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();
downloadFile(url);
} else if (fileRadioButton->isChecked()) {
QFile cardFile(fileLineEdit->text());
if (!cardFile.exists()) {
QMessageBox::critical(this, tr("Error"), tr("Please choose a file."));
return false;
}
if (!cardFile.open(QIODevice::ReadOnly)) {
QMessageBox::critical(nullptr, tr("Error"), tr("Cannot open file '%1'.").arg(fileLineEdit->text()));
return false;
}
downloadData = cardFile.readAll();
wizard()->next();
} }
progressLabel->setText(tr("Downloading (0MB)"));
// show an infinite progressbar
progressBar->setMaximum(0);
progressBar->setMinimum(0);
progressBar->setValue(0);
progressLabel->show();
progressBar->show();
wizard()->disableButtons();
downloadFile(url);
return false; return false;
} }

View File

@@ -3,6 +3,8 @@
#include <QWizardPage> #include <QWizardPage>
class QFile;
class QRadioButton;
class OracleWizard; class OracleWizard;
class QCheckBox; class QCheckBox;
class QLabel; class QLabel;
@@ -43,15 +45,19 @@ protected:
virtual QString getDefaultSavePath() = 0; virtual QString getDefaultSavePath() = 0;
virtual QString getWindowTitle() = 0; virtual QString getWindowTitle() = 0;
virtual QString getFileType() = 0; virtual QString getFileType() = 0;
virtual QString getFilePromptName() = 0;
bool saveToFile(); bool saveToFile();
bool internalSaveToFile(const QString &fileName); bool internalSaveToFile(const QString &fileName);
protected: protected:
QByteArray downloadData; QByteArray downloadData;
QLabel *urlLabel; QRadioButton *urlRadioButton;
QLabel *pathLabel; QRadioButton *fileRadioButton;
QLineEdit *urlLineEdit; QLineEdit *urlLineEdit;
QLineEdit *fileLineEdit;
QPushButton *urlButton; QPushButton *urlButton;
QPushButton *fileButton;
QLabel *pathLabel;
QLabel *progressLabel; QLabel *progressLabel;
QProgressBar *progressBar; QProgressBar *progressBar;
QCheckBox *defaultPathCheckBox; QCheckBox *defaultPathCheckBox;
@@ -60,6 +66,7 @@ signals:
void parsedDataReady(); void parsedDataReady();
private slots: private slots:
void actRestoreDefaultUrl(); void actRestoreDefaultUrl();
void actLoadCardFile();
void actDownloadProgress(qint64 received, qint64 total); void actDownloadProgress(qint64 received, qint64 total);
void actDownloadFinished(); void actDownloadFinished();
}; };