From b8ea078fcf8897fae6e74cfacddcf72a0c1392a4 Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Fri, 4 Sep 2020 16:52:32 +0200 Subject: [PATCH] Implement feed export to opml --- src/database.cpp | 35 ++++++++++++++++++++++++++++++----- src/database.h | 3 ++- src/qml/FeedListPage.qml | 18 ++++++++++++++++-- 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/database.cpp b/src/database.cpp index 605e2d4d..8ab3ff05 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include "alligatorsettings.h" #include "database.h" @@ -156,13 +157,12 @@ void Database::addFeed(QString url) Fetcher::instance().fetch(urlFromInput.toString()); } -void Database::importFeedsFromUrl(QString url) +void Database::importFeeds(QString path) { - QFile *file = new QFile(QUrl(url).toLocalFile()); - file->open(QIODevice::ReadOnly); + QFile file(QUrl(path).toLocalFile()); + file.open(QIODevice::ReadOnly); - QXmlStreamReader xmlReader; - xmlReader.setDevice(file); + QXmlStreamReader xmlReader(&file); while(!xmlReader.atEnd()) { xmlReader.readNext(); if(xmlReader.tokenType() == 4 && xmlReader.attributes().hasAttribute(QStringLiteral("xmlUrl"))) { @@ -171,3 +171,28 @@ void Database::importFeedsFromUrl(QString url) } Fetcher::instance().fetchAll(); } + +void Database::exportFeeds(QString path) +{ + QFile file(QUrl(path).toLocalFile()); + file.open(QIODevice::WriteOnly); + QXmlStreamWriter xmlWriter(&file); + xmlWriter.setAutoFormatting(true); + xmlWriter.writeStartDocument(QStringLiteral("1.0")); + xmlWriter.writeStartElement(QStringLiteral("opml")); + xmlWriter.writeEmptyElement(QStringLiteral("head")); + xmlWriter.writeStartElement(QStringLiteral("body")); + xmlWriter.writeAttribute(QStringLiteral("version"), QStringLiteral("1.0")); + QSqlQuery query; + query.prepare(QStringLiteral("SELECT url, name FROM Feeds;")); + execute(query); + while(query.next()) { + xmlWriter.writeEmptyElement(QStringLiteral("outline")); + xmlWriter.writeAttribute(QStringLiteral("xmlUrl"), query.value(0).toString()); + xmlWriter.writeAttribute(QStringLiteral("title"), query.value(1).toString()); + } + xmlWriter.writeEndElement(); + xmlWriter.writeEndElement(); + xmlWriter.writeEndDocument(); + +} diff --git a/src/database.h b/src/database.h index 047a0de2..7c0cfd7b 100644 --- a/src/database.h +++ b/src/database.h @@ -21,7 +21,8 @@ public: bool execute(QSqlQuery &query); bool execute(QString query); Q_INVOKABLE void addFeed(QString url); - Q_INVOKABLE void importFeedsFromUrl(QString url); + Q_INVOKABLE void importFeeds(QString path); + Q_INVOKABLE void exportFeeds(QString path); Q_SIGNALS: void feedAdded(QString url); diff --git a/src/qml/FeedListPage.qml b/src/qml/FeedListPage.qml index cf5e27c2..d80f7dae 100644 --- a/src/qml/FeedListPage.qml +++ b/src/qml/FeedListPage.qml @@ -36,6 +36,11 @@ Kirigami.ScrollablePage { text: i18n("Import Feeds...") iconName: "document-import" onTriggered: importDialog.open() + }, + Kirigami.Action { + text: i18n("Export Feeds...") + iconName: "document-export" + onTriggered: exportDialog.open() } ] @@ -75,7 +80,16 @@ Kirigami.ScrollablePage { id: importDialog title: i18n("Import Feeds") folder: StandardPaths.writableLocation(StandardPaths.HomeLocation) - nameFilters: [i18n("All Files (*.*)"), i18n("XML Files (*.xml)"), i18n("OPML Files (*.opml)")] - onAccepted: Database.importFeedsFromUrl(file) + nameFilters: [i18n("All Files (*)"), i18n("XML Files (*.xml)"), i18n("OPML Files (*.opml)")] + onAccepted: Database.importFeeds(file) + } + + FileDialog { + id: exportDialog + title: i18n("Export Feeds") + folder: StandardPaths.writableLocation(StandardPaths.HomeLocation) + nameFilters: [i18n("All Files")] + onAccepted: Database.exportFeeds(file) + fileMode: FileDialog.SaveFile } }