mirror of
https://github.com/KDE/kasts.git
synced 2025-02-02 10:26:52 +01:00
store link, description, authors for feed
This commit is contained in:
parent
78255fbdef
commit
dc384c598e
@ -57,7 +57,7 @@ bool Database::migrate()
|
||||
bool Database::migrateTo1()
|
||||
{
|
||||
qDebug() << "Migrating database to version 1";
|
||||
TRUE_OR_RETURN(execute(QStringLiteral("CREATE TABLE IF NOT EXISTS Feeds (name TEXT, url TEXT, image TEXT);")));
|
||||
TRUE_OR_RETURN(execute(QStringLiteral("CREATE TABLE IF NOT EXISTS Feeds (name TEXT, url TEXT, image TEXT, link TEXT, description TEXT);")));
|
||||
TRUE_OR_RETURN(execute(QStringLiteral("CREATE TABLE IF NOT EXISTS Entries (feed TEXT, id TEXT UNIQUE, title TEXT, content TEXT, created INTEGER, updated INTEGER, link TEXT);")));
|
||||
TRUE_OR_RETURN(execute(QStringLiteral("CREATE TABLE IF NOT EXISTS Authors (feed TEXT, id TEXT, name TEXT, uri TEXT, email TEXT);")));
|
||||
TRUE_OR_RETURN(execute(QStringLiteral("CREATE TABLE IF NOT EXISTS Enclosures (feed TEXT, id TEXT, duration INTEGER, size INTEGER, title TEXT, type STRING, url STRING);")));
|
||||
@ -145,10 +145,12 @@ void Database::addFeed(QString url)
|
||||
qDebug() << "Feed does not yet exist";
|
||||
|
||||
QSqlQuery query;
|
||||
query.prepare(QStringLiteral("INSERT INTO Feeds VALUES (:name, :url, :image);"));
|
||||
query.prepare(QStringLiteral("INSERT INTO Feeds VALUES (:name, :url, :image, :link, :description);"));
|
||||
query.bindValue(QStringLiteral(":name"), url);
|
||||
query.bindValue(QStringLiteral(":url"), url);
|
||||
query.bindValue(QStringLiteral(":image"), QLatin1String(""));
|
||||
query.bindValue(QStringLiteral(":link"), QLatin1String(""));
|
||||
query.bindValue(QStringLiteral(":description"), QLatin1String(""));
|
||||
execute(query);
|
||||
|
||||
Q_EMIT feedAdded(url);
|
||||
|
@ -39,12 +39,6 @@ EntryListModel::EntryListModel(QObject *parent)
|
||||
endResetModel();
|
||||
}
|
||||
});
|
||||
connect(&Fetcher::instance(), &Fetcher::feedDetailsUpdated, this, [this](QString url, QString name, QString image) {
|
||||
if (m_feed->url() == url) {
|
||||
m_feed->setName(name);
|
||||
m_feed->setImage(image);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
QVariant EntryListModel::data(const QModelIndex &index, int role) const
|
||||
|
38
src/feed.cpp
38
src/feed.cpp
@ -23,11 +23,14 @@
|
||||
#include "database.h"
|
||||
#include "feed.h"
|
||||
|
||||
Feed::Feed(QString url, QString name, QString image, QObject *parent)
|
||||
Feed::Feed(QString url, QString name, QString image, QString link, QString description, QVector<Author *> authors, QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_url(url)
|
||||
, m_name(name)
|
||||
, m_image(image)
|
||||
, m_link(link)
|
||||
, m_description(description)
|
||||
, m_authors(authors)
|
||||
{
|
||||
}
|
||||
|
||||
@ -50,6 +53,21 @@ QString Feed::image() const
|
||||
return m_image;
|
||||
}
|
||||
|
||||
QString Feed::link() const
|
||||
{
|
||||
return m_link;
|
||||
}
|
||||
|
||||
QString Feed::description() const
|
||||
{
|
||||
return m_description;
|
||||
}
|
||||
|
||||
QVector<Author *> Feed::authors() const
|
||||
{
|
||||
return m_authors;
|
||||
}
|
||||
|
||||
void Feed::setName(QString name)
|
||||
{
|
||||
m_name = name;
|
||||
@ -62,6 +80,24 @@ void Feed::setImage(QString image)
|
||||
emit imageChanged(m_image);
|
||||
}
|
||||
|
||||
void Feed::setLink(QString link)
|
||||
{
|
||||
m_link = link;
|
||||
emit linkChanged(m_link);
|
||||
}
|
||||
|
||||
void Feed::setDescription(QString description)
|
||||
{
|
||||
m_description = description;
|
||||
emit descriptionChanged(m_description);
|
||||
}
|
||||
|
||||
void Feed::setAuthors(QVector<Author *> authors)
|
||||
{
|
||||
m_authors = authors;
|
||||
emit authorsChanged(m_authors);
|
||||
}
|
||||
|
||||
void Feed::remove()
|
||||
{
|
||||
// Delete Authors
|
||||
|
19
src/feed.h
19
src/feed.h
@ -23,6 +23,8 @@
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include "author.h"
|
||||
|
||||
class Feed : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
@ -30,29 +32,44 @@ class Feed : public QObject
|
||||
Q_PROPERTY(QString url READ url CONSTANT)
|
||||
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
|
||||
Q_PROPERTY(QString image READ image WRITE setImage NOTIFY imageChanged)
|
||||
Q_PROPERTY(QString link READ link WRITE setLink NOTIFY linkChanged)
|
||||
Q_PROPERTY(QString description READ description WRITE setDescription NOTIFY descriptionChanged)
|
||||
Q_PROPERTY(QVector<Author *> authors READ authors WRITE setAuthors NOTIFY authorsChanged)
|
||||
|
||||
public:
|
||||
Feed(QString url, QString name, QString image, QObject *parent = nullptr);
|
||||
Feed(QString url, QString name, QString image, QString link, QString description, QVector<Author *> authors, QObject *parent = nullptr);
|
||||
|
||||
~Feed();
|
||||
|
||||
QString url() const;
|
||||
QString name() const;
|
||||
QString image() const;
|
||||
QString link() const;
|
||||
QString description() const;
|
||||
QVector<Author *> authors() const;
|
||||
|
||||
void setName(QString name);
|
||||
void setImage(QString image);
|
||||
void setLink(QString link);
|
||||
void setDescription(QString description);
|
||||
void setAuthors(QVector<Author *> authors);
|
||||
|
||||
void remove();
|
||||
|
||||
Q_SIGNALS:
|
||||
void nameChanged(QString &name);
|
||||
void imageChanged(QString &image);
|
||||
void linkChanged(QString &link);
|
||||
void descriptionChanged(QString &description);
|
||||
void authorsChanged(QVector<Author *> &authors);
|
||||
|
||||
private:
|
||||
QString m_url;
|
||||
QString m_name;
|
||||
QString m_image;
|
||||
QString m_link;
|
||||
QString m_description;
|
||||
QVector<Author *> m_authors;
|
||||
};
|
||||
|
||||
#endif // FEED_H
|
||||
|
@ -35,11 +35,13 @@ FeedListModel::FeedListModel(QObject *parent)
|
||||
beginInsertRows(QModelIndex(), rowCount(QModelIndex()) - 1, rowCount(QModelIndex()) - 1);
|
||||
endInsertRows();
|
||||
});
|
||||
connect(&Fetcher::instance(), &Fetcher::feedDetailsUpdated, this, [this](QString url, QString name, QString image) {
|
||||
connect(&Fetcher::instance(), &Fetcher::feedDetailsUpdated, this, [this](QString url, QString name, QString image, QString link, QString description) {
|
||||
for (int i = rowCount(QModelIndex()) - 1; i >= 0; i--) {
|
||||
if (m_feeds[i]->url() == url) {
|
||||
m_feeds[i]->setName(name);
|
||||
m_feeds[i]->setImage(image);
|
||||
m_feeds[i]->setLink(link);
|
||||
m_feeds[i]->setDescription(description);
|
||||
Q_EMIT dataChanged(createIndex(i, 0), createIndex(i, 0));
|
||||
break;
|
||||
}
|
||||
@ -81,8 +83,18 @@ void FeedListModel::loadFeed(int index) const
|
||||
query.bindValue(QStringLiteral(":index"), index);
|
||||
Database::instance().execute(query);
|
||||
if (!query.next())
|
||||
qWarning() << "Failed to lod feed" << index;
|
||||
Feed *feed = new Feed(query.value(QStringLiteral("url")).toString(), query.value(QStringLiteral("name")).toString(), query.value(QStringLiteral("image")).toString(), nullptr);
|
||||
qWarning() << "Failed to load feed" << index;
|
||||
|
||||
QSqlQuery authorQuery;
|
||||
authorQuery.prepare(QStringLiteral("SELECT * FROM Authors WHERE id='' AND feed=:feed"));
|
||||
authorQuery.bindValue(QStringLiteral(":feed"), query.value(QStringLiteral("url")).toString());
|
||||
Database::instance().execute(authorQuery);
|
||||
QVector<Author *> authors;
|
||||
while (authorQuery.next()) {
|
||||
authors += new Author(authorQuery.value(QStringLiteral("name")).toString(), authorQuery.value(QStringLiteral("email")).toString(), authorQuery.value(QStringLiteral("uri")).toString(), nullptr);
|
||||
}
|
||||
|
||||
Feed *feed = new Feed(query.value(QStringLiteral("url")).toString(), query.value(QStringLiteral("name")).toString(), query.value(QStringLiteral("image")).toString(), query.value(QStringLiteral("link")).toString(), query.value(QStringLiteral("description")).toString(), authors, nullptr);
|
||||
m_feeds[index] = feed;
|
||||
}
|
||||
|
||||
|
@ -64,6 +64,13 @@ void Fetcher::processFeed(Syndication::FeedPtr feed, QString url)
|
||||
query.prepare(QStringLiteral("UPDATE Feeds SET name=:name, image=:image WHERE url=:url;"));
|
||||
query.bindValue(QStringLiteral(":name"), feed->title());
|
||||
query.bindValue(QStringLiteral(":url"), url);
|
||||
query.bindValue(QStringLiteral(":link"), feed->link());
|
||||
query.bindValue(QStringLiteral(":description"), feed->description());
|
||||
|
||||
for(auto &author : feed->authors()) {
|
||||
processAuthor(author, QLatin1String(""), url);
|
||||
}
|
||||
|
||||
QString image;
|
||||
if (feed->image()->url().startsWith(QStringLiteral("/")))
|
||||
image = QUrl(url).adjusted(QUrl::RemovePath).toString() + feed->image()->url();
|
||||
@ -71,9 +78,10 @@ void Fetcher::processFeed(Syndication::FeedPtr feed, QString url)
|
||||
image = feed->image()->url();
|
||||
query.bindValue(QStringLiteral(":image"), image);
|
||||
Database::instance().execute(query);
|
||||
|
||||
qDebug() << "Updated feed title:" << feed->title();
|
||||
|
||||
Q_EMIT feedDetailsUpdated(url, feed->title(), image);
|
||||
Q_EMIT feedDetailsUpdated(url, feed->title(), image, feed->link(), feed->description());
|
||||
|
||||
for (const auto &entry : feed->items()) {
|
||||
processEntry(entry, url);
|
||||
@ -110,7 +118,7 @@ void Fetcher::processEntry(Syndication::ItemPtr entry, QString url)
|
||||
Database::instance().execute(query);
|
||||
|
||||
for (const auto &author : entry->authors()) {
|
||||
processAuthor(author, entry, url);
|
||||
processAuthor(author, entry->id(), url);
|
||||
}
|
||||
|
||||
for (const auto &enclosure : entry->enclosures()) {
|
||||
@ -118,12 +126,12 @@ void Fetcher::processEntry(Syndication::ItemPtr entry, QString url)
|
||||
}
|
||||
}
|
||||
|
||||
void Fetcher::processAuthor(Syndication::PersonPtr author, Syndication::ItemPtr entry, QString url)
|
||||
void Fetcher::processAuthor(Syndication::PersonPtr author, QString entryId, QString url)
|
||||
{
|
||||
QSqlQuery query;
|
||||
query.prepare(QStringLiteral("INSERT INTO Authors VALUES(:feed, :id, :name, :uri, :email);"));
|
||||
query.bindValue(QStringLiteral(":feed"), url);
|
||||
query.bindValue(QStringLiteral(":id"), entry->id());
|
||||
query.bindValue(QStringLiteral(":id"), entryId);
|
||||
query.bindValue(QStringLiteral(":name"), author->name());
|
||||
query.bindValue(QStringLiteral(":uri"), author->uri());
|
||||
query.bindValue(QStringLiteral(":email"), author->email());
|
||||
|
@ -45,12 +45,12 @@ private:
|
||||
QString filePath(QString);
|
||||
void processFeed(Syndication::FeedPtr feed, QString url);
|
||||
void processEntry(Syndication::ItemPtr entry, QString url);
|
||||
void processAuthor(Syndication::PersonPtr author, Syndication::ItemPtr entry, QString url);
|
||||
void processAuthor(Syndication::PersonPtr author, QString entryId, QString url);
|
||||
void processEnclosure(Syndication::EnclosurePtr enclosure, Syndication::ItemPtr entry, QString feedUrl);
|
||||
|
||||
QNetworkAccessManager *manager;
|
||||
|
||||
Q_SIGNALS:
|
||||
void feedUpdated(QString url);
|
||||
void feedDetailsUpdated(QString url, QString name, QString image);
|
||||
void feedDetailsUpdated(QString url, QString name, QString image, QString link, QString description);
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user