Implement Import/Export RSS rules in JSON format

This commit is contained in:
Vladimir Golovnev (Glassez) 2017-11-23 10:25:43 +03:00
parent b8fc415870
commit 916cfcdb03
No known key found for this signature in database
GPG key ID: 52A2C7DEE2DFA6F7
4 changed files with 150 additions and 47 deletions

View file

@ -54,8 +54,13 @@
#include "autoexpandabledialog.h"
#include "ui_automatedrssdownloader.h"
const QString EXT_JSON {QStringLiteral(".json")};
const QString EXT_LEGACY {QStringLiteral(".rssrules")};
AutomatedRssDownloader::AutomatedRssDownloader(QWidget *parent)
: QDialog(parent)
, m_formatFilterJSON(QString("%1 (*%2)").arg(tr("Rules")).arg(EXT_JSON))
, m_formatFilterLegacy(QString("%1 (*%2)").arg(tr("Rules (legacy)")).arg(EXT_LEGACY))
, m_ui(new Ui::AutomatedRssDownloader)
, m_currentRuleItem(nullptr)
{
@ -390,17 +395,30 @@ void AutomatedRssDownloader::on_exportBtn_clicked()
return;
}
QString selectedFilter {m_formatFilterJSON};
QString path = QFileDialog::getSaveFileName(
this, tr("Where would you like to save the list?")
, QDir::homePath(), tr("Rules list (legacy)") + QString(" (*.rssrules)"));
this, tr("Export RSS rules"), QDir::homePath()
, QString("%1;;%2").arg(m_formatFilterJSON).arg(m_formatFilterLegacy), &selectedFilter);
if (path.isEmpty()) return;
if (!path.endsWith(".rssrules", Qt::CaseInsensitive))
path += ".rssrules";
const RSS::AutoDownloader::RulesFileFormat format {
(selectedFilter == m_formatFilterJSON)
? RSS::AutoDownloader::RulesFileFormat::JSON
: RSS::AutoDownloader::RulesFileFormat::Legacy
};
QFile file(path);
if (format == RSS::AutoDownloader::RulesFileFormat::JSON) {
if (!path.endsWith(EXT_JSON, Qt::CaseInsensitive))
path += EXT_JSON;
}
else {
if (!path.endsWith(EXT_LEGACY, Qt::CaseInsensitive))
path += EXT_LEGACY;
}
QFile file {path};
if (!file.open(QFile::WriteOnly)
|| (file.write(RSS::AutoDownloader::instance()->exportRulesToLegacyFormat()) == -1)) {
|| (file.write(RSS::AutoDownloader::instance()->exportRules(format)) == -1)) {
QMessageBox::critical(
this, tr("I/O Error")
, tr("Failed to create the destination file. Reason: %1").arg(file.errorString()));
@ -409,13 +427,14 @@ void AutomatedRssDownloader::on_exportBtn_clicked()
void AutomatedRssDownloader::on_importBtn_clicked()
{
QString selectedFilter {m_formatFilterJSON};
QString path = QFileDialog::getOpenFileName(
this, tr("Please point to the RSS download rules file")
, QDir::homePath(), tr("Rules list (legacy)") + QString(" (*.rssrules)"));
this, tr("Import RSS rules"), QDir::homePath()
, QString("%1;;%2").arg(m_formatFilterJSON).arg(m_formatFilterLegacy), &selectedFilter);
if (path.isEmpty() || !QFile::exists(path))
return;
QFile file(path);
QFile file {path};
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::critical(
this, tr("I/O Error")
@ -423,10 +442,19 @@ void AutomatedRssDownloader::on_importBtn_clicked()
return;
}
if (!RSS::AutoDownloader::instance()->importRulesFromLegacyFormat(file.readAll())) {
const RSS::AutoDownloader::RulesFileFormat format {
(selectedFilter == m_formatFilterJSON)
? RSS::AutoDownloader::RulesFileFormat::JSON
: RSS::AutoDownloader::RulesFileFormat::Legacy
};
try {
RSS::AutoDownloader::instance()->importRules(file.readAll(),format);
}
catch (const RSS::ParsingError &error) {
QMessageBox::critical(
this, tr("Import Error")
, tr("Failed to import the selected rules file."));
, tr("Failed to import the selected rules file. Reason: %1").arg(error.message()));
}
}