File indexing completed on 2025-01-05 04:29:52

0001 /**
0002  * SPDX-FileCopyrightText: 2021 Bart De Vries <bart@mogwai.be>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "models/downloadmodel.h"
0008 #include "models/downloadmodellogging.h"
0009 
0010 #include <QSqlQuery>
0011 
0012 #include "database.h"
0013 #include "datamanager.h"
0014 #include "models/episodemodel.h"
0015 
0016 DownloadModel::DownloadModel()
0017     : QAbstractListModel(nullptr)
0018 {
0019     updateInternalState();
0020 }
0021 
0022 QVariant DownloadModel::data(const QModelIndex &index, int role) const
0023 {
0024     switch (role) {
0025     case EpisodeModel::Roles::EntryRole:
0026         return QVariant::fromValue(DataManager::instance().getEntry(m_entryIds[index.row()]));
0027     case EpisodeModel::Roles::IdRole:
0028         return QVariant::fromValue(m_entryIds[index.row()]);
0029     default:
0030         return QVariant();
0031     }
0032 }
0033 
0034 QHash<int, QByteArray> DownloadModel::roleNames() const
0035 {
0036     return {
0037         {EpisodeModel::Roles::EntryRole, "entry"},
0038         {EpisodeModel::Roles::IdRole, "id"},
0039         {EpisodeModel::Roles::ReadRole, "read"},
0040         {EpisodeModel::Roles::NewRole, "new"},
0041         {EpisodeModel::Roles::FavoriteRole, "favorite"},
0042     };
0043 }
0044 
0045 int DownloadModel::rowCount(const QModelIndex &parent) const
0046 {
0047     Q_UNUSED(parent)
0048     return m_entryIds.count();
0049 }
0050 
0051 void DownloadModel::monitorDownloadStatus()
0052 {
0053     beginResetModel();
0054     updateInternalState();
0055     endResetModel();
0056 }
0057 
0058 void DownloadModel::updateInternalState()
0059 {
0060     m_entryIds.clear();
0061 
0062     QSqlQuery query;
0063     query.prepare(
0064         QStringLiteral("SELECT * FROM Enclosures INNER JOIN Entries ON Enclosures.id = Entries.id WHERE downloaded=:downloaded ORDER BY updated DESC;"));
0065 
0066     query.bindValue(QStringLiteral(":downloaded"), Enclosure::statusToDb(Enclosure::Downloading));
0067     Database::instance().execute(query);
0068     while (query.next()) {
0069         m_entryIds += query.value(QStringLiteral("id")).toString();
0070     }
0071 
0072     query.bindValue(QStringLiteral(":downloaded"), Enclosure::statusToDb(Enclosure::PartiallyDownloaded));
0073     Database::instance().execute(query);
0074     while (query.next()) {
0075         m_entryIds += query.value(QStringLiteral("id")).toString();
0076     }
0077 
0078     query.bindValue(QStringLiteral(":downloaded"), Enclosure::statusToDb(Enclosure::Downloaded));
0079     Database::instance().execute(query);
0080     while (query.next()) {
0081         m_entryIds += query.value(QStringLiteral("id")).toString();
0082     }
0083 }
0084 
0085 // Hack to get a QItemSelection in QML
0086 QItemSelection DownloadModel::createSelection(int rowa, int rowb)
0087 {
0088     return QItemSelection(index(rowa, 0), index(rowb, 0));
0089 }