2020-02-28 23:25:08 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2020 Tobias Fella <fella@posteo.de>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License or (at your option) version 3 or any later version
|
|
|
|
* accepted by the membership of KDE e.V. (or its successor approved
|
|
|
|
* by the membership of KDE e.V.), which shall act as a proxy
|
|
|
|
* defined in Section 14 of version 3 of the license.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QSqlDatabase>
|
|
|
|
#include <QSqlQuery>
|
|
|
|
#include <QUrl>
|
|
|
|
|
2020-02-29 15:34:12 +01:00
|
|
|
#include <Syndication/Syndication>
|
2020-02-28 23:25:08 +01:00
|
|
|
|
|
|
|
#include "feedListModel.h"
|
|
|
|
#include "fetcher.h"
|
|
|
|
|
|
|
|
FeedListModel::FeedListModel(QObject *parent)
|
|
|
|
: QAbstractListModel(parent)
|
|
|
|
{
|
2020-02-29 00:55:16 +01:00
|
|
|
QSqlQuery query(QSqlDatabase::database());
|
2020-02-28 23:25:08 +01:00
|
|
|
query.exec(QStringLiteral("SELECT name, url FROM Feeds"));
|
|
|
|
beginInsertRows(QModelIndex(), 0, query.size());
|
|
|
|
while (query.next()) {
|
|
|
|
feeds += Feed(query.value(1).toString(), query.value(0).toString());
|
|
|
|
}
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray> FeedListModel::roleNames() const
|
|
|
|
{
|
|
|
|
QHash<int, QByteArray> roleNames;
|
|
|
|
roleNames[Qt::DisplayRole] = "display";
|
|
|
|
roleNames[Url] = "url";
|
|
|
|
return roleNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant FeedListModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (role == Url)
|
2020-02-29 15:26:40 +01:00
|
|
|
return feeds[index.row()].url();
|
2020-02-28 23:25:08 +01:00
|
|
|
if (role == Qt::DisplayRole)
|
2020-02-29 15:26:40 +01:00
|
|
|
return feeds[index.row()].name();
|
|
|
|
return QStringLiteral("DEADBEEF");
|
2020-02-28 23:25:08 +01:00
|
|
|
}
|
|
|
|
int FeedListModel::rowCount(const QModelIndex &index) const
|
|
|
|
{
|
2020-02-29 15:26:40 +01:00
|
|
|
return feeds.size();
|
2020-02-28 23:25:08 +01:00
|
|
|
}
|
|
|
|
|
2020-02-29 15:47:35 +01:00
|
|
|
void FeedListModel::addFeed(QString url)
|
2020-02-28 23:25:08 +01:00
|
|
|
{
|
|
|
|
Fetcher::instance().fetch(QUrl(url));
|
|
|
|
beginInsertRows(QModelIndex(), feeds.size(), feeds.size());
|
|
|
|
feeds.append(Feed(url));
|
|
|
|
endInsertRows();
|
2020-02-29 00:55:16 +01:00
|
|
|
QSqlQuery query(QSqlDatabase::database());
|
2020-02-28 23:25:08 +01:00
|
|
|
query.prepare(QStringLiteral("INSERT INTO Feeds VALUES (:url, :name);"));
|
|
|
|
query.bindValue(QStringLiteral(":url"), url);
|
|
|
|
query.bindValue(QStringLiteral(":name"), url);
|
|
|
|
query.exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FeedListModel::remove_feed(int index)
|
|
|
|
{
|
|
|
|
Feed toRemove = feeds[index];
|
2020-02-29 00:55:16 +01:00
|
|
|
QSqlQuery query(QSqlDatabase::database());
|
2020-02-28 23:25:08 +01:00
|
|
|
query.prepare(QStringLiteral("DELETE FROM Feeds WHERE name=:name AND url=url;"));
|
|
|
|
query.bindValue(QStringLiteral(":url"), toRemove.url());
|
|
|
|
query.bindValue(QStringLiteral(":name"), toRemove.name());
|
|
|
|
query.exec();
|
|
|
|
beginRemoveRows(QModelIndex(), index, index);
|
|
|
|
feeds.remove(index);
|
|
|
|
endRemoveRows();
|
|
|
|
}
|