File indexing completed on 2024-05-12 16:23:39

0001 /**
0002  * SPDX-FileCopyrightText: 2020 Tobias Fella <fella@posteo.de>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include <QDebug>
0008 #include <QModelIndex>
0009 #include <QSqlQuery>
0010 #include <QVariant>
0011 
0012 #include "database.h"
0013 #include "feedsmodel.h"
0014 #include "fetcher.h"
0015 
0016 FeedsModel::FeedsModel(QObject *parent)
0017     : QAbstractListModel(parent)
0018 {
0019     connect(&Database::instance(), &Database::feedAdded, this, [this]() {
0020         beginInsertRows(QModelIndex(), rowCount(QModelIndex()) - 1, rowCount(QModelIndex()) - 1);
0021         endInsertRows();
0022     });
0023 
0024     connect(
0025         &Fetcher::instance(),
0026         &Fetcher::feedDetailsUpdated,
0027         this,
0028         [this](const QString &url, const QString &name, const QString &image, const QString &link, const QString &description, const QDateTime &lastUpdated) {
0029             for (int i = 0; i < m_feeds.length(); i++) {
0030                 if (m_feeds[i]->url() == url) {
0031                     m_feeds[i]->setName(name);
0032                     m_feeds[i]->setImage(image);
0033                     m_feeds[i]->setLink(link);
0034                     m_feeds[i]->setDescription(description);
0035                     m_feeds[i]->setLastUpdated(lastUpdated);
0036                     Q_EMIT dataChanged(createIndex(i, 0), createIndex(i, 0));
0037                     break;
0038                 }
0039             }
0040         });
0041 
0042     connect(&Database::instance(), &Database::feedDetailsUpdated, [this](const QString &url, const QString &displayName, const QString &groupName) {
0043         for (int i = 0; i < m_feeds.length(); i++) {
0044             if (m_feeds[i]->url() == url) {
0045                 m_feeds[i]->setDisplayName(displayName);
0046                 m_feeds[i]->setGroupName(groupName);
0047                 Q_EMIT dataChanged(createIndex(i, 0), createIndex(i, 0));
0048                 break;
0049             }
0050         }
0051     });
0052 
0053     connect(&Database::instance(), &Database::feedGroupRemoved, [this](const QString &groupName) {
0054         for (int i = 0; i < m_feeds.length(); i++) {
0055             if (m_feeds[i]->groupName() == groupName) {
0056                 m_feeds[i]->setGroupName(QString());
0057                 Q_EMIT dataChanged(createIndex(i, 0), createIndex(i, 0));
0058                 break;
0059             }
0060         }
0061     });
0062 }
0063 
0064 QHash<int, QByteArray> FeedsModel::roleNames() const
0065 {
0066     QHash<int, QByteArray> roleNames;
0067     roleNames[0] = "feed";
0068     return roleNames;
0069 }
0070 
0071 int FeedsModel::rowCount(const QModelIndex &parent) const
0072 {
0073     Q_UNUSED(parent)
0074     QSqlQuery query;
0075     query.prepare(QStringLiteral("SELECT COUNT() FROM Feeds;"));
0076     Database::instance().execute(query);
0077     if (!query.next()) {
0078         qWarning() << "Failed to query feed count";
0079     }
0080     return query.value(0).toInt();
0081 }
0082 
0083 QVariant FeedsModel::data(const QModelIndex &index, int role) const
0084 {
0085     if (role != 0) {
0086         return QVariant();
0087     }
0088 
0089     while (m_feeds.length() <= index.row()) {
0090         loadFeed(m_feeds.length());
0091     }
0092     return QVariant::fromValue(m_feeds[index.row()]);
0093 }
0094 
0095 void FeedsModel::loadFeed(int index) const
0096 {
0097     m_feeds += new Feed(index);
0098 }
0099 
0100 void FeedsModel::removeFeed(const QString &url)
0101 {
0102     for (int i = 0; i < m_feeds.length(); i++) {
0103         if (m_feeds[i]->url() == url) {
0104             m_feeds[i]->remove();
0105             delete m_feeds[i];
0106             beginRemoveRows(QModelIndex(), i, i);
0107             m_feeds.removeAt(i);
0108             endRemoveRows();
0109         }
0110     }
0111 }
0112 
0113 void FeedsModel::refreshAll()
0114 {
0115     for (auto &feed : m_feeds) {
0116         feed->refresh();
0117     }
0118 }