File indexing completed on 2025-01-05 04:29:52
0001 /** 0002 * SPDX-FileCopyrightText: 2020 Tobias Fella <tobias.fella@kde.org> 0003 * SPDX-FileCopyrightText: 2021 Bart De Vries <bart@mogwai.be> 0004 * 0005 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0006 */ 0007 0008 #include "models/feedsmodel.h" 0009 0010 #include <QDebug> 0011 #include <QModelIndex> 0012 #include <QSqlQuery> 0013 #include <QUrl> 0014 #include <QVariant> 0015 0016 #include "database.h" 0017 #include "datamanager.h" 0018 #include "fetcher.h" 0019 0020 FeedsModel::FeedsModel(QObject *parent) 0021 : QAbstractListModel(parent) 0022 { 0023 connect(&DataManager::instance(), &DataManager::feedAdded, this, [this]() { 0024 beginInsertRows(QModelIndex(), rowCount(QModelIndex()) - 1, rowCount(QModelIndex()) - 1); 0025 endInsertRows(); 0026 }); 0027 connect(&DataManager::instance(), &DataManager::feedRemoved, this, [this](const int &index) { 0028 beginRemoveRows(QModelIndex(), index, index); 0029 endRemoveRows(); 0030 }); 0031 connect(&DataManager::instance(), &DataManager::unreadEntryCountChanged, this, &FeedsModel::triggerFeedUpdate); 0032 connect(&DataManager::instance(), &DataManager::newEntryCountChanged, this, &FeedsModel::triggerFeedUpdate); 0033 connect(&DataManager::instance(), &DataManager::favoriteEntryCountChanged, this, &FeedsModel::triggerFeedUpdate); 0034 connect(&Fetcher::instance(), &Fetcher::feedDetailsUpdated, this, &FeedsModel::triggerFeedUpdate); 0035 } 0036 0037 QHash<int, QByteArray> FeedsModel::roleNames() const 0038 { 0039 return { 0040 {FeedRole, "feed"}, 0041 {UrlRole, "url"}, 0042 {TitleRole, "title"}, 0043 {UnreadCountRole, "unreadCount"}, 0044 {NewCountRole, "newCount"}, 0045 {FavoriteCountRole, "favoriteCount"}, 0046 }; 0047 } 0048 0049 int FeedsModel::rowCount(const QModelIndex &parent) const 0050 { 0051 Q_UNUSED(parent); 0052 return DataManager::instance().feedCount(); 0053 } 0054 0055 QVariant FeedsModel::data(const QModelIndex &index, int role) const 0056 { 0057 switch (role) { 0058 case FeedRole: 0059 return QVariant::fromValue(DataManager::instance().getFeed(index.row())); 0060 case Qt::DisplayRole: 0061 case UrlRole: 0062 return QVariant::fromValue(DataManager::instance().getFeed(index.row())->url()); 0063 case TitleRole: 0064 return QVariant::fromValue(DataManager::instance().getFeed(index.row())->name()); 0065 case UnreadCountRole: 0066 return QVariant::fromValue(DataManager::instance().getFeed(index.row())->unreadEntryCount()); 0067 case NewCountRole: 0068 return QVariant::fromValue(DataManager::instance().getFeed(index.row())->newEntryCount()); 0069 case FavoriteCountRole: 0070 return QVariant::fromValue(DataManager::instance().getFeed(index.row())->favoriteEntryCount()); 0071 default: 0072 return QVariant(); 0073 } 0074 } 0075 0076 void FeedsModel::triggerFeedUpdate(const QString &url) 0077 { 0078 for (int i = 0; i < rowCount(QModelIndex()); i++) { 0079 if (data(index(i, 0), UrlRole).toString() == url) { 0080 Q_EMIT dataChanged(index(i, 0), index(i, 0)); 0081 return; 0082 } 0083 } 0084 }