kasts/src/models/feedsmodel.cpp

72 lines
2.1 KiB
C++
Raw Normal View History

2020-02-28 23:25:08 +01:00
/**
2020-08-14 20:56:04 +02:00
* SPDX-FileCopyrightText: 2020 Tobias Fella <fella@posteo.de>
2021-04-08 13:16:36 +02:00
* SPDX-FileCopyrightText: 2021 Bart De Vries <bart@mogwai.be>
2020-02-28 23:25:08 +01:00
*
2020-08-14 20:56:04 +02:00
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
2020-02-28 23:25:08 +01:00
*/
2021-09-08 11:46:22 +02:00
#include "models/feedsmodel.h"
2020-04-18 21:07:49 +02:00
#include <QDebug>
#include <QModelIndex>
2020-05-26 16:32:07 +02:00
#include <QSqlQuery>
2020-04-22 02:17:57 +02:00
#include <QUrl>
2020-05-26 16:32:07 +02:00
#include <QVariant>
2020-02-28 23:25:08 +01:00
2020-04-22 02:17:57 +02:00
#include "database.h"
#include "datamanager.h"
2020-02-28 23:25:08 +01:00
#include "fetcher.h"
2020-03-16 22:37:04 +01:00
2020-10-09 13:47:35 +02:00
FeedsModel::FeedsModel(QObject *parent)
2020-05-26 16:32:07 +02:00
: QAbstractListModel(parent)
2020-02-28 23:25:08 +01:00
{
connect(&DataManager::instance(), &DataManager::feedAdded, this, [this]() {
2020-05-26 16:32:07 +02:00
beginInsertRows(QModelIndex(), rowCount(QModelIndex()) - 1, rowCount(QModelIndex()) - 1);
endInsertRows();
});
connect(&DataManager::instance(), &DataManager::feedRemoved, this, [this](const int &index) {
beginRemoveRows(QModelIndex(), index, index);
endRemoveRows();
2020-05-26 16:32:07 +02:00
});
connect(&DataManager::instance(), &DataManager::unreadEntryCountChanged, this, [=](const QString &url) {
for (int i = 0; i < rowCount(QModelIndex()); i++) {
if (data(index(i, 0), UrlRole).toString() == url) {
Q_EMIT dataChanged(index(i, 0), index(i, 0));
return;
}
}
});
2020-02-28 23:25:08 +01:00
}
2020-10-09 13:47:35 +02:00
QHash<int, QByteArray> FeedsModel::roleNames() const
2020-02-28 23:25:08 +01:00
{
return {
{FeedRole, "feed"},
{UrlRole, "url"},
{TitleRole, "title"},
{UnreadCountRole, "unreadCount"},
};
2020-02-28 23:25:08 +01:00
}
2020-10-09 13:47:35 +02:00
int FeedsModel::rowCount(const QModelIndex &parent) const
2020-02-28 23:25:08 +01:00
{
Q_UNUSED(parent);
return DataManager::instance().feedCount();
2020-02-28 23:25:08 +01:00
}
2020-04-18 21:07:49 +02:00
2020-10-09 13:47:35 +02:00
QVariant FeedsModel::data(const QModelIndex &index, int role) const
2020-02-28 23:25:08 +01:00
{
switch (role) {
case FeedRole:
return QVariant::fromValue(DataManager::instance().getFeed(index.row()));
case UrlRole:
return QVariant::fromValue(DataManager::instance().getFeed(index.row())->url());
case TitleRole:
return QVariant::fromValue(DataManager::instance().getFeed(index.row())->name());
case UnreadCountRole:
return QVariant::fromValue(DataManager::instance().getFeed(index.row())->unreadEntryCount());
default:
2020-05-26 16:32:07 +02:00
return QVariant();
}
2020-05-26 16:32:07 +02:00
}