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 <QUrl>
|
|
|
|
|
|
|
|
#include "feedListModel.h"
|
|
|
|
#include "fetcher.h"
|
2020-03-16 22:37:04 +01:00
|
|
|
#include "database.h"
|
|
|
|
|
2020-02-28 23:25:08 +01:00
|
|
|
FeedListModel::FeedListModel(QObject *parent)
|
|
|
|
: QAbstractListModel(parent)
|
|
|
|
{
|
2020-03-16 22:37:04 +01:00
|
|
|
QSqlQuery query;
|
2020-03-29 18:07:28 +02:00
|
|
|
query.prepare(QStringLiteral("SELECT name, url, image FROM Feeds"));
|
2020-03-16 22:37:04 +01:00
|
|
|
Database::instance().execute(query);
|
2020-02-28 23:25:08 +01:00
|
|
|
beginInsertRows(QModelIndex(), 0, query.size());
|
|
|
|
while (query.next()) {
|
2020-03-29 18:07:28 +02:00
|
|
|
feeds += Feed(query.value(1).toString(), query.value(0).toString(), query.value(2).toString());
|
2020-02-28 23:25:08 +01:00
|
|
|
}
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray> FeedListModel::roleNames() const
|
|
|
|
{
|
|
|
|
QHash<int, QByteArray> roleNames;
|
2020-03-29 18:07:28 +02:00
|
|
|
roleNames[FeedRole] = "feed";
|
2020-02-28 23:25:08 +01:00
|
|
|
return roleNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant FeedListModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2020-03-29 18:07:28 +02:00
|
|
|
if (role == FeedRole) {
|
|
|
|
return QVariant::fromValue(feeds[index.row()]);
|
|
|
|
}
|
2020-02-29 15:26:40 +01:00
|
|
|
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
|
|
|
{
|
2020-03-16 22:37:04 +01:00
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare(QStringLiteral("SELECT COUNT (url) FROM Feeds WHERE url=:url;"));
|
|
|
|
query.bindValue(QStringLiteral(":url"), url);
|
|
|
|
Database::instance().execute(query);
|
|
|
|
query.next();
|
|
|
|
if(query.value(0).toInt() != 0) return;
|
2020-03-06 22:45:17 +01:00
|
|
|
connect(&Fetcher::instance(), &Fetcher::finished, this, [this, url]() {
|
2020-03-16 22:37:04 +01:00
|
|
|
QSqlQuery query;
|
2020-03-29 18:07:28 +02:00
|
|
|
query.prepare(QStringLiteral("SELECT name, image FROM Feeds WHERE url=:url;"));
|
2020-03-06 22:45:17 +01:00
|
|
|
query.bindValue(QStringLiteral(":url"), url);
|
2020-03-16 22:37:04 +01:00
|
|
|
Database::instance().execute(query);
|
2020-03-08 21:56:36 +01:00
|
|
|
query.next();
|
2020-03-06 22:45:17 +01:00
|
|
|
for(int i = 0; i < feeds.length(); i++) {
|
|
|
|
if(feeds[i].url() == url) {
|
|
|
|
feeds.removeAt(i);
|
2020-03-30 21:09:15 +02:00
|
|
|
feeds.insert(i, Feed(url, query.value(0).toString(), query.value(1).toString()));
|
2020-03-06 22:45:17 +01:00
|
|
|
emit dataChanged(index(i), index(i));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
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-03-16 22:37:04 +01:00
|
|
|
|
2020-03-29 18:07:28 +02:00
|
|
|
query.prepare(QStringLiteral("INSERT INTO Feeds VALUES (:url, :name, '');"));
|
2020-02-28 23:25:08 +01:00
|
|
|
query.bindValue(QStringLiteral(":url"), url);
|
|
|
|
query.bindValue(QStringLiteral(":name"), url);
|
2020-03-16 22:37:04 +01:00
|
|
|
Database::instance().execute(query);
|
2020-02-28 23:25:08 +01:00
|
|
|
}
|
|
|
|
|
2020-04-15 21:36:59 +02:00
|
|
|
void FeedListModel::removeFeed(int index)
|
2020-02-28 23:25:08 +01:00
|
|
|
{
|
|
|
|
Feed toRemove = feeds[index];
|
2020-03-16 22:37:04 +01:00
|
|
|
QSqlQuery query;
|
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());
|
2020-03-16 22:37:04 +01:00
|
|
|
Database::instance().execute(query);
|
2020-02-28 23:25:08 +01:00
|
|
|
beginRemoveRows(QModelIndex(), index, index);
|
|
|
|
feeds.remove(index);
|
|
|
|
endRemoveRows();
|
|
|
|
}
|