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>
|
2020-04-18 21:07:49 +02:00
|
|
|
#include <QSqlRecord>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QModelIndex>
|
|
|
|
#include <QSqlError>
|
2020-02-28 23:25:08 +01:00
|
|
|
|
|
|
|
#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)
|
2020-04-18 21:07:49 +02:00
|
|
|
: QSqlTableModel(parent)
|
2020-02-28 23:25:08 +01:00
|
|
|
{
|
2020-04-21 23:27:15 +02:00
|
|
|
setTable(QStringLiteral("Feeds"));
|
2020-04-18 21:07:49 +02:00
|
|
|
setSort(0, Qt::AscendingOrder);
|
|
|
|
setEditStrategy(OnFieldChange);
|
|
|
|
select();
|
2020-02-28 23:25:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray> FeedListModel::roleNames() const
|
|
|
|
{
|
|
|
|
QHash<int, QByteArray> roleNames;
|
2020-04-18 21:07:49 +02:00
|
|
|
roleNames[Name] = "name";
|
|
|
|
roleNames[Url] = "url";
|
|
|
|
roleNames[Image] = "image";
|
2020-02-28 23:25:08 +01:00
|
|
|
return roleNames;
|
|
|
|
}
|
|
|
|
|
2020-04-18 21:07:49 +02:00
|
|
|
void FeedListModel::addFeed(QString url)
|
2020-02-28 23:25:08 +01:00
|
|
|
{
|
2020-04-18 21:07:49 +02:00
|
|
|
qDebug() << "Adding feed";
|
|
|
|
if(feedExists(url)) {
|
|
|
|
qDebug() << "Feed already exists";
|
|
|
|
return;
|
2020-03-29 18:07:28 +02:00
|
|
|
}
|
2020-04-18 21:07:49 +02:00
|
|
|
qDebug() << "Feed does not yet exist";
|
|
|
|
|
|
|
|
QSqlRecord rec = record();
|
|
|
|
rec.setValue(0, url);
|
|
|
|
rec.setValue(1, url);
|
2020-04-21 23:27:15 +02:00
|
|
|
rec.setValue(2, QStringLiteral(""));
|
2020-04-18 21:07:49 +02:00
|
|
|
|
|
|
|
insertRecord(-1, rec);
|
|
|
|
|
|
|
|
connect(&Fetcher::instance(), &Fetcher::updated, this, [this]() {
|
|
|
|
select();
|
|
|
|
|
|
|
|
disconnect(&Fetcher::instance(), &Fetcher::updated, nullptr, nullptr);
|
|
|
|
});
|
|
|
|
Fetcher::instance().fetch(QUrl(url));
|
2020-02-28 23:25:08 +01:00
|
|
|
}
|
2020-04-18 21:07:49 +02:00
|
|
|
|
|
|
|
QVariant FeedListModel::data(const QModelIndex &index, int role) const
|
2020-02-28 23:25:08 +01:00
|
|
|
{
|
2020-04-18 21:07:49 +02:00
|
|
|
return QSqlTableModel::data(createIndex(index.row(), role), 0);
|
2020-02-28 23:25:08 +01:00
|
|
|
}
|
|
|
|
|
2020-04-18 21:07:49 +02:00
|
|
|
bool FeedListModel::feedExists(QString url) {
|
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();
|
2020-04-18 21:07:49 +02:00
|
|
|
return query.value(0).toInt() != 0;
|
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
|
|
|
{
|
2020-03-16 22:37:04 +01:00
|
|
|
QSqlQuery query;
|
2020-04-20 02:06:21 +02:00
|
|
|
query.prepare(QStringLiteral("DELETE FROM Authors WHERE feed=:feed;"));
|
|
|
|
query.bindValue(QStringLiteral(":feed"), data(createIndex(index, 0), 1).toString());
|
|
|
|
Database::instance().execute(query);
|
|
|
|
|
2020-04-20 02:02:31 +02:00
|
|
|
query.prepare(QStringLiteral("DELETE FROM Entries WHERE feed=:feed;"));
|
|
|
|
query.bindValue(QStringLiteral(":feed"), data(createIndex(index, 0), 1).toString());
|
|
|
|
Database::instance().execute(query);
|
|
|
|
|
|
|
|
//Workaround...
|
|
|
|
query.prepare(QStringLiteral("DELETE FROM Feeds WHERE url=:url;"));
|
2020-04-20 01:50:57 +02:00
|
|
|
query.bindValue(QStringLiteral(":url"), data(createIndex(index, 0), 1).toString());
|
2020-03-16 22:37:04 +01:00
|
|
|
Database::instance().execute(query);
|
2020-04-18 21:07:49 +02:00
|
|
|
select();
|
2020-02-28 23:25:08 +01:00
|
|
|
}
|