From 5b9b03115040afda5c4b4e94af15778590cfa0a0 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Mon, 4 Jan 2016 12:47:59 +0100 Subject: [PATCH] Export to TXT done, import remains. --- .../standard/gui/formstandardimportexport.cpp | 59 +++++++++++++------ .../standard/gui/formstandardimportexport.h | 3 +- .../standardfeedsimportexportmodel.cpp | 14 ++++- .../standard/standardfeedsimportexportmodel.h | 5 ++ 4 files changed, 62 insertions(+), 19 deletions(-) diff --git a/src/services/standard/gui/formstandardimportexport.cpp b/src/services/standard/gui/formstandardimportexport.cpp index 6e7f869cd..a2765d6b0 100755 --- a/src/services/standard/gui/formstandardimportexport.cpp +++ b/src/services/standard/gui/formstandardimportexport.cpp @@ -102,12 +102,15 @@ void FormStandardImportExport::selectFile() { void FormStandardImportExport::selectExportFile() { QString filter_opml20 = tr("OPML 2.0 files (*.opml)"); + QString filter_txt_url_per_line = tr("TXT files (one URL per line) (.txt)"); QString filter; QString selected_filter; // Add more filters here. filter += filter_opml20; + filter += ";;"; + filter += filter_txt_url_per_line; QString selected_file = QFileDialog::getSaveFileName(this, tr("Select file for feeds export"), qApp->homeFolderPath(), filter, &selected_filter); @@ -120,6 +123,13 @@ void FormStandardImportExport::selectExportFile() { selected_file += QL1S(".opml"); } } + else if (selected_filter == filter_txt_url_per_line) { + m_conversionType = TXTUrlPerLine; + + if (!selected_file.endsWith(QL1S(".txt"))) { + selected_file += QL1S(".txt"); + } + } m_ui->m_lblSelectFile->setStatus(WidgetWithStatus::Ok, QDir::toNativeSeparators(selected_file), tr("File is selected.")); } @@ -129,12 +139,15 @@ void FormStandardImportExport::selectExportFile() { void FormStandardImportExport::selectImportFile() { QString filter_opml20 = tr("OPML 2.0 files (*.opml)"); + QString filter_txt_url_per_line = tr("TXT files (one URL per line) (.txt)"); QString filter; QString selected_filter; // Add more filters here. filter += filter_opml20; + filter += ";;"; + filter += filter_txt_url_per_line; QString selected_file = QFileDialog::getOpenFileName(this, tr("Select file for feeds import"), qApp->homeFolderPath(), filter, &selected_filter); @@ -143,6 +156,9 @@ void FormStandardImportExport::selectImportFile() { if (selected_filter == filter_opml20) { m_conversionType = OPML20; } + else if (selected_filter == filter_txt_url_per_line) { + m_conversionType = TXTUrlPerLine; + } m_ui->m_lblSelectFile->setStatus(WidgetWithStatus::Ok, QDir::toNativeSeparators(selected_file), tr("File is selected.")); parseImportFile(selected_file); @@ -170,6 +186,10 @@ void FormStandardImportExport::parseImportFile(const QString &file_name) { parsing_result = m_model->importAsOPML20(input_data); break; + case TXTUrlPerLine: + // TODO: TODO + //parsing_result = m_model->importAsTxtURLPerLine(input_data); + default: return; } @@ -204,29 +224,34 @@ void FormStandardImportExport::performAction() { } void FormStandardImportExport::exportFeeds() { + QByteArray result_data; + bool result_export; + switch (m_conversionType) { - case OPML20: { - QByteArray result_data; - bool result_export = m_model->exportToOMPL20(result_data); + case OPML20: + result_export = m_model->exportToOMPL20(result_data); + break; - if (result_export) { - try { - IOFactory::writeTextFile(m_ui->m_lblSelectFile->label()->text(), result_data); - - m_ui->m_lblResult->setStatus(WidgetWithStatus::Ok, tr("Feeds were exported successfully."), tr("Feeds were exported successfully.")); - } - catch (IOException &ex) { - m_ui->m_lblResult->setStatus(WidgetWithStatus::Error, tr("Cannot write into destination file."), ex.message()); - } - } - else { - m_ui->m_lblResult->setStatus(WidgetWithStatus::Error, tr("Critical error occurred."), tr("Critical error occurred.")); - } - } + case TXTUrlPerLine: + result_export = m_model->exportToTxtURLPerLine(result_data); + break; default: break; } + + if (result_export) { + try { + IOFactory::writeTextFile(m_ui->m_lblSelectFile->label()->text(), result_data); + m_ui->m_lblResult->setStatus(WidgetWithStatus::Ok, tr("Feeds were exported successfully."), tr("Feeds were exported successfully.")); + } + catch (IOException &ex) { + m_ui->m_lblResult->setStatus(WidgetWithStatus::Error, tr("Cannot write into destination file: '%1'."), ex.message()); + } + } + else { + m_ui->m_lblResult->setStatus(WidgetWithStatus::Error, tr("Critical error occurred."), tr("Critical error occurred.")); + } } void FormStandardImportExport::importFeeds() { diff --git a/src/services/standard/gui/formstandardimportexport.h b/src/services/standard/gui/formstandardimportexport.h index 4b36f1ebc..237ff08e1 100755 --- a/src/services/standard/gui/formstandardimportexport.h +++ b/src/services/standard/gui/formstandardimportexport.h @@ -34,7 +34,8 @@ class FormStandardImportExport : public QDialog { public: enum ConversionType { - OPML20 = 0 + OPML20 = 0, + TXTUrlPerLine = 1 }; // Constructors. diff --git a/src/services/standard/standardfeedsimportexportmodel.cpp b/src/services/standard/standardfeedsimportexportmodel.cpp index f5e071e7c..b8e1a38aa 100755 --- a/src/services/standard/standardfeedsimportexportmodel.cpp +++ b/src/services/standard/standardfeedsimportexportmodel.cpp @@ -67,7 +67,7 @@ bool FeedsImportExportModel::exportToOMPL20(QByteArray &result) { QSL("version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"")); opml_document.appendChild(xml_declaration); - // Adde OPML 2.0 metadata. + // Added OPML 2.0 metadata. opml_document.appendChild(opml_document.createElement(QSL("opml"))); opml_document.documentElement().setAttribute(QSL("version"), QSL("version")); @@ -255,6 +255,18 @@ bool FeedsImportExportModel::importAsOPML20(const QByteArray &data) { return true; } +bool FeedsImportExportModel::exportToTxtURLPerLine(QByteArray &result) { + foreach (const Feed * const feed, m_rootItem->getSubTreeFeeds()) { + result += feed->url() + QL1S("\n"); + } + + return true; +} + +bool FeedsImportExportModel::importAsTxtURLPerLine(const QByteArray &data) { + return true; +} + FeedsImportExportModel::Mode FeedsImportExportModel::mode() const { return m_mode; } diff --git a/src/services/standard/standardfeedsimportexportmodel.h b/src/services/standard/standardfeedsimportexportmodel.h index 0a313f7c1..18b1ed01f 100755 --- a/src/services/standard/standardfeedsimportexportmodel.h +++ b/src/services/standard/standardfeedsimportexportmodel.h @@ -62,6 +62,11 @@ class FeedsImportExportModel : public QAbstractItemModel { bool exportToOMPL20(QByteArray &result); bool importAsOPML20(const QByteArray &data); + // Exports to plain text format + // where there is one feed URL per line. + bool exportToTxtURLPerLine(QByteArray &result); + bool importAsTxtURLPerLine(const QByteArray &data); + Mode mode() const; void setMode(const Mode &mode);