[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)");
}
QString LoadTokensPage::getFilePromptName()
{
return tr("tokens");
}
void LoadTokensPage::retranslateUi()
{
setTitle(tr("Tokens import"));
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"));
fileButton->setText(tr("Choose file..."));
pathLabel->setText(tr("The token database will be saved at the following location:") + "<br>" +
SettingsCache::instance().getTokenDatabasePath());
defaultPathCheckBox->setText(tr("Save to a custom path (not recommended)"));
@@ -709,13 +717,21 @@ QString LoadSpoilersPage::getFileType()
return tr("XML; spoiler database (*.xml)");
}
QString LoadSpoilersPage::getFilePromptName()
{
return tr("spoiler");
}
void LoadSpoilersPage::retranslateUi()
{
setTitle(tr("Spoilers import"));
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"));
fileButton->setText(tr("Choose file..."));
pathLabel->setText(tr("The spoiler database will be saved at the following location:") + "<br>" +
SettingsCache::instance().getSpoilerCardDatabasePath());
defaultPathCheckBox->setText(tr("Save to a custom path (not recommended)"));

View File

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

View File

@@ -12,32 +12,43 @@
#include <QNetworkReply>
#include <QProgressBar>
#include <QPushButton>
#include <QRadioButton>
#include <QtGui>
SimpleDownloadFilePage::SimpleDownloadFilePage(QWidget *parent) : OracleWizardPage(parent)
{
urlLabel = new QLabel(this);
urlRadioButton = new QRadioButton(this);
fileRadioButton = new QRadioButton(this);
urlLineEdit = new QLineEdit(this);
fileLineEdit = new QLineEdit(this);
progressLabel = new QLabel(this);
progressBar = new QProgressBar(this);
urlRadioButton->setChecked(true);
urlButton = new QPushButton(this);
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->setTextInteractionFlags(Qt::TextSelectableByMouse);
auto *layout = new QGridLayout(this);
layout->addWidget(urlLabel, 0, 0);
layout->addWidget(urlRadioButton, 0, 0);
layout->addWidget(urlLineEdit, 0, 1);
layout->addWidget(urlButton, 1, 1, Qt::AlignRight);
layout->addWidget(pathLabel, 2, 0, 1, 2);
layout->addWidget(defaultPathCheckBox, 3, 0, 1, 2);
layout->addWidget(progressLabel, 4, 0);
layout->addWidget(progressBar, 4, 1);
layout->addWidget(fileRadioButton, 2, 0);
layout->addWidget(fileLineEdit, 2, 1);
layout->addWidget(fileButton, 3, 1, Qt::AlignRight);
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);
}
@@ -56,6 +67,31 @@ void SimpleDownloadFilePage::actRestoreDefaultUrl()
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()
{
// if data has already been downloaded, pass directly to the "save" step
@@ -68,6 +104,8 @@ bool SimpleDownloadFilePage::validatePage()
}
}
// else, try to import sets
if (urlRadioButton->isChecked()) {
QUrl url = QUrl::fromUserInput(urlLineEdit->text());
if (!url.isValid()) {
QMessageBox::critical(this, tr("Error"), tr("The provided URL is not valid: ") + url.toString());
@@ -84,6 +122,23 @@ bool SimpleDownloadFilePage::validatePage()
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();
}
return false;
}

View File

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