diff --git a/src/entryListModel.cpp b/src/entryListModel.cpp index 2c79aa83..c6bb5594 100644 --- a/src/entryListModel.cpp +++ b/src/entryListModel.cpp @@ -19,8 +19,8 @@ */ #include -#include #include +#include #include "database.h" #include "entryListModel.h" @@ -34,20 +34,18 @@ EntryListModel::EntryListModel(QObject *parent) setEditStrategy(OnFieldChange); select(); - connect(&Fetcher::instance(), &Fetcher::updated, this, [this] () { - select(); - }); + connect(&Fetcher::instance(), &Fetcher::updated, this, [this]() { select(); }); } QVariant EntryListModel::data(const QModelIndex &index, int role) const { - if(role == Authors) { + if (role == Authors) { QSqlQuery query; query.prepare(QStringLiteral("SELECT name FROM Authors WHERE id=:id")); query.bindValue(QStringLiteral(":id"), data(index, Id)); Database::instance().execute(query); QStringList authors; - while(query.next()) { + while (query.next()) { authors += query.value(0).toString(); } return authors; diff --git a/src/fetcher.cpp b/src/fetcher.cpp index 4782f884..5e474416 100644 --- a/src/fetcher.cpp +++ b/src/fetcher.cpp @@ -51,58 +51,79 @@ void Fetcher::fetch(QUrl url) Syndication::DocumentSource *document = new Syndication::DocumentSource(data, url.toString()); Syndication::FeedPtr feed = Syndication::parserCollection()->parse(*document, QStringLiteral("Atom")); - if (feed.isNull()) - return; - - QSqlQuery query; - - query.prepare(QStringLiteral("UPDATE Feeds SET name=:name, image=:image WHERE url=:url;")); - query.bindValue(QStringLiteral(":name"), feed->title()); - query.bindValue(QStringLiteral(":url"), url.toString()); - if (feed->image()->url().startsWith(QStringLiteral("/"))) { - QString absolute = url.adjusted(QUrl::RemovePath).toString() + feed->image()->url(); - query.bindValue(QStringLiteral(":image"), absolute); - } else - query.bindValue(QStringLiteral(":image"), feed->image()->url()); - Database::instance().execute(query); - qDebug() << "Updated feed title:" << feed->title(); - - for (const auto &entry : feed->items()) { - qDebug() << "Processing" << entry->title(); - query.prepare(QStringLiteral("SELECT COUNT (id) FROM Entries WHERE id=:id;")); - query.bindValue(QStringLiteral(":id"), entry->id()); - Database::instance().execute(query); - query.next(); - if (query.value(0).toInt() != 0) - continue; - query.prepare(QStringLiteral("INSERT INTO Entries VALUES (:feed, :id, :title, :content, :created, :updated, :link);")); - query.bindValue(QStringLiteral(":feed"), url.toString()); - query.bindValue(QStringLiteral(":id"), entry->id()); - query.bindValue(QStringLiteral(":title"), QTextDocumentFragment::fromHtml(entry->title()).toPlainText()); - query.bindValue(QStringLiteral(":created"), static_cast(entry->datePublished())); - query.bindValue(QStringLiteral(":updated"), static_cast(entry->dateUpdated())); - query.bindValue(QStringLiteral(":link"), entry->link()); - if (!entry->content().isEmpty()) - query.bindValue(QStringLiteral(":content"), entry->content()); - else - query.bindValue(QStringLiteral(":content"), entry->description()); - Database::instance().execute(query); - for (const auto &author : entry->authors()) { - query.prepare(QStringLiteral("INSERT INTO Authors VALUES(:feed, :id, :name, :uri, :email);")); - query.bindValue(QStringLiteral(":feed"), url.toString()); - query.bindValue(QStringLiteral(":id"), entry->id()); - query.bindValue(QStringLiteral(":name"), author->name()); - query.bindValue(QStringLiteral(":uri"), author->uri()); - query.bindValue(QStringLiteral(":email"), author->email()); - Database::instance().execute(query); - } - } + processFeed(feed, url); emit updated(); delete reply; }); } +void Fetcher::processFeed(Syndication::FeedPtr feed, QUrl url) +{ + if (feed.isNull()) + return; + + QSqlQuery query; + query.prepare(QStringLiteral("UPDATE Feeds SET name=:name, image=:image WHERE url=:url;")); + query.bindValue(QStringLiteral(":name"), feed->title()); + query.bindValue(QStringLiteral(":url"), url); + if (feed->image()->url().startsWith(QStringLiteral("/"))) { + QString absolute = url.adjusted(QUrl::RemovePath).toString() + feed->image()->url(); + query.bindValue(QStringLiteral(":image"), absolute); + } else + query.bindValue(QStringLiteral(":image"), feed->image()->url()); + Database::instance().execute(query); + qDebug() << "Updated feed title:" << feed->title(); + + for (const auto &entry : feed->items()) { + processEntry(entry, url); + } +} + +void Fetcher::processEntry(Syndication::ItemPtr entry, QUrl url) +{ + qDebug() << "Processing" << entry->title(); + QSqlQuery query; + query.prepare(QStringLiteral("SELECT COUNT (id) FROM Entries WHERE id=:id;")); + query.bindValue(QStringLiteral(":id"), entry->id()); + Database::instance().execute(query); + query.next(); + + if (query.value(0).toInt() != 0) + return; + + query.prepare(QStringLiteral("INSERT INTO Entries VALUES (:feed, :id, :title, :content, :created, :updated, :link);")); + query.bindValue(QStringLiteral(":feed"), url); + query.bindValue(QStringLiteral(":id"), entry->id()); + query.bindValue(QStringLiteral(":title"), QTextDocumentFragment::fromHtml(entry->title()).toPlainText()); + query.bindValue(QStringLiteral(":created"), static_cast(entry->datePublished())); + query.bindValue(QStringLiteral(":updated"), static_cast(entry->dateUpdated())); + query.bindValue(QStringLiteral(":link"), entry->link()); + + if (!entry->content().isEmpty()) + query.bindValue(QStringLiteral(":content"), entry->content()); + else + query.bindValue(QStringLiteral(":content"), entry->description()); + + Database::instance().execute(query); + + for (const auto &author : entry->authors()) { + processAuthor(author, entry, url); + } +} + +void Fetcher::processAuthor(Syndication::PersonPtr author, Syndication::ItemPtr entry, QUrl url) +{ + QSqlQuery query; + query.prepare(QStringLiteral("INSERT INTO Authors VALUES(:feed, :id, :name, :uri, :email);")); + query.bindValue(QStringLiteral(":feed"), url.toString()); + query.bindValue(QStringLiteral(":id"), entry->id()); + query.bindValue(QStringLiteral(":name"), author->name()); + query.bindValue(QStringLiteral(":uri"), author->uri()); + query.bindValue(QStringLiteral(":email"), author->email()); + Database::instance().execute(query); +} + QString Fetcher::image(QString url) { QString path = imagePath(url); diff --git a/src/fetcher.h b/src/fetcher.h index 0e8c3cea..6873ae73 100644 --- a/src/fetcher.h +++ b/src/fetcher.h @@ -23,6 +23,7 @@ #include #include #include +#include class Fetcher : public QObject { @@ -42,6 +43,9 @@ private: Fetcher(const Fetcher &); QString imagePath(QString); + void processFeed(Syndication::FeedPtr feed, QUrl url); + void processEntry(Syndication::ItemPtr entry, QUrl url); + void processAuthor(Syndication::PersonPtr author, Syndication::ItemPtr entry, QUrl url); QNetworkAccessManager *manager; diff --git a/src/main.cpp b/src/main.cpp index dae7a895..419823b1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,10 +24,10 @@ #include #endif +#include #include #include #include -#include #include #include @@ -68,10 +68,10 @@ int main(int argc, char *argv[]) parser.addPositionalArgument(QLatin1String("url"), i18n("Url to add to the subscriptions")); parser.process(app); - if(parser.positionalArguments().size() == 1) { + if (parser.positionalArguments().size() == 1) { QString url = parser.positionalArguments().at(0); qDebug() << url; - //TODO + // TODO } KAboutData about(QStringLiteral("alligator"), i18n("Alligator"), QStringLiteral("0.1"), i18n("Feed Reader"), KAboutLicense::GPL, i18n("© 2020 KDE Community"));