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/>.
|
|
|
|
*/
|
|
|
|
|
2020-04-18 21:07:49 +02:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QModelIndex>
|
2020-04-22 02:17:57 +02:00
|
|
|
#include <QSqlRecord>
|
|
|
|
#include <QUrl>
|
2020-02-28 23:25:08 +01:00
|
|
|
|
2020-04-22 02:17:57 +02:00
|
|
|
#include "database.h"
|
2020-02-28 23:25:08 +01:00
|
|
|
#include "feedListModel.h"
|
|
|
|
#include "fetcher.h"
|
2020-03-16 22:37:04 +01:00
|
|
|
|
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-04-25 22:16:19 +02:00
|
|
|
|
2020-04-26 23:40:09 +02:00
|
|
|
connect(&Fetcher::instance(), &Fetcher::updated, this, [this]() { 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-05-11 21:13:27 +02:00
|
|
|
Database::instance().addFeed(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-15 21:36:59 +02:00
|
|
|
void FeedListModel::removeFeed(int index)
|
2020-02-28 23:25:08 +01:00
|
|
|
{
|
2020-04-26 23:40:09 +02:00
|
|
|
Fetcher::instance().removeImage(data(createIndex(index, 0), Image).toString());
|
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);
|
|
|
|
|
2020-05-18 17:02:46 +02:00
|
|
|
query.prepare(QStringLiteral("DELETE FROM Enclosures WHERE feed=:feed;"));
|
|
|
|
query.bindValue(QStringLiteral(":feed"), data(createIndex(index, 0), 1).toString());
|
|
|
|
Database::instance().execute(query);
|
|
|
|
|
2020-04-22 02:17:57 +02:00
|
|
|
// Workaround...
|
2020-04-20 02:02:31 +02:00
|
|
|
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
|
|
|
}
|