diff --git a/oracle/src/pages.cpp b/oracle/src/pages.cpp index 9b2f5ce7c..46edf1cf8 100644 --- a/oracle/src/pages.cpp +++ b/oracle/src/pages.cpp @@ -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:") + "
" + 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:") + "
" + SettingsCache::instance().getSpoilerCardDatabasePath()); defaultPathCheckBox->setText(tr("Save to a custom path (not recommended)")); diff --git a/oracle/src/pages.h b/oracle/src/pages.h index 6a88b6099..066cc2e1b 100644 --- a/oracle/src/pages.h +++ b/oracle/src/pages.h @@ -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; }; diff --git a/oracle/src/pagetemplates.cpp b/oracle/src/pagetemplates.cpp index d9ce3300f..4a27fef30 100644 --- a/oracle/src/pagetemplates.cpp +++ b/oracle/src/pagetemplates.cpp @@ -12,32 +12,43 @@ #include #include #include +#include #include 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,22 +104,41 @@ bool SimpleDownloadFilePage::validatePage() } } - QUrl url = QUrl::fromUserInput(urlLineEdit->text()); - if (!url.isValid()) { - QMessageBox::critical(this, tr("Error"), tr("The provided URL is not valid: ") + url.toString()); - return false; + // 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()); + 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; } diff --git a/oracle/src/pagetemplates.h b/oracle/src/pagetemplates.h index 372aa2fef..79dcdd632 100644 --- a/oracle/src/pagetemplates.h +++ b/oracle/src/pagetemplates.h @@ -3,6 +3,8 @@ #include +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(); };