More work on queuemodel refactoring

This commit is contained in:
Bart De Vries 2021-04-03 13:49:33 +02:00
parent 0808d3b5fe
commit 5e0772eb63
6 changed files with 64 additions and 123 deletions

View File

@ -209,6 +209,30 @@ void DataManager::addFeed(const QString &url)
Fetcher::instance().fetch(urlFromInput.toString()); Fetcher::instance().fetch(urlFromInput.toString());
} }
Entry* DataManager::getQueueEntry(int const &index) const
{
return getEntry(m_queuemap[index]);
}
int DataManager::queueCount() const
{
return m_queuemap.count();
}
void DataManager::addtoQueue(const QString &feedurl, const QString &id)
{
// TODO: also add the entry to queue database table
m_queuemap += id;
qDebug() << m_queuemap;
Q_EMIT queueEntryAdded(m_queuemap.length()-1, id);
}
void DataManager::moveQueueItem(const int &from, const int &to)
{
m_queuemap.move(from, to);
Q_EMIT queueEntryMoved(from, to);
}
void DataManager::importFeeds(const QString &path) void DataManager::importFeeds(const QString &path)
{ {
QUrl url(path); QUrl url(path);

View File

@ -33,10 +33,16 @@ public:
Q_INVOKABLE void removeFeed(const Feed* feed); Q_INVOKABLE void removeFeed(const Feed* feed);
Q_INVOKABLE void removeFeed(const int &index); Q_INVOKABLE void removeFeed(const int &index);
//Q_INVOKABLE void addEntry(const QString &url); //Q_INVOKABLE void addEntry(const QString &url); // TODO: implement these methods
//Q_INVOKABLE void removeEntry(const QString &url); //Q_INVOKABLE void removeEntry(const QString &url);
//Q_INVOKABLE void removeEntry(const Feed* feed, const int &index); //Q_INVOKABLE void removeEntry(const Feed* feed, const int &index);
// TODO: add getQueue, queueCount etc.
Entry* getQueueEntry(int const &index) const;
int queueCount() const;
Q_INVOKABLE void addtoQueue(const QString &feedurl, const QString &id);
Q_INVOKABLE void moveQueueItem(const int &from, const int &to);
Q_INVOKABLE void importFeeds(const QString &path); Q_INVOKABLE void importFeeds(const QString &path);
Q_INVOKABLE void exportFeeds(const QString &path); Q_INVOKABLE void exportFeeds(const QString &path);
@ -44,8 +50,11 @@ Q_SIGNALS:
void feedAdded(const QString &url); void feedAdded(const QString &url);
void feedRemoved(const int &index); void feedRemoved(const int &index);
void entryAdded(const QString &feedurl, const QString &id); void entryAdded(const QString &feedurl, const QString &id);
void entryRemoved(const Feed*, const int &index); // TODO: implement this method //void entryRemoved(const Feed*, const int &index); // TODO: implement this signal
void feedEntriesUpdated(const QString &url); void feedEntriesUpdated(const QString &url);
void queueEntryAdded(const int &index, const QString &id); // TODO: implement
void queueEntryRemoved(const int &index, const QString &id); // TODO: implement
void queueEntryMoved(const int &from, const int $to);
private: private:
DataManager(); DataManager();
@ -53,8 +62,10 @@ private:
void loadEntry(QString id) const; void loadEntry(QString id) const;
bool feedExists(const QString &url); bool feedExists(const QString &url);
QStringList m_feedmap; mutable QHash<QString, Feed*> m_feeds; // hash of pointers to all feeds in db, key = url (lazy loading)
mutable QHash<QString, Feed*> m_feeds; mutable QHash<QString, Entry*> m_entries; // hash of pointers to all entries in db, key = id (lazy loading)
QHash<QString, QStringList> m_entrymap;
mutable QHash<QString, Entry*> m_entries; QStringList m_feedmap; // list of feedurls in the order that they should appear in feedlist
QHash<QString, QStringList> m_entrymap; // list of entries (per feed; key = url) in the order that they should appear in entrylist
QStringList m_queuemap; // list of entries/enclosures in the order that they should show up in queuelist
}; };

View File

@ -60,6 +60,6 @@ Kirigami.ScrollablePage {
text: "Add to queue" text: "Add to queue"
icon.name: "media-playlist-append" icon.name: "media-playlist-append"
visible: entry.enclosure visible: entry.enclosure
onTriggered: { queueModel.addEntry(entry.feed.url, entry.id) } onTriggered: { DataManager.addtoQueue(entry.feed.url, entry.id) }
} }
} }

View File

@ -23,7 +23,7 @@ Kirigami.ScrollablePage {
Kirigami.ListItemDragHandle { Kirigami.ListItemDragHandle {
listItem: listItem listItem: listItem
listView: mainList listView: mainList
onMoveRequested: queueModel.move(oldIndex, newIndex) onMoveRequested: DataManager.moveQueueItem(oldIndex, newIndex)
} }
Controls.Label { Controls.Label {

View File

@ -11,101 +11,32 @@
#include <QFile> #include <QFile>
#include "database.h" #include "database.h"
#include "datamanager.h"
#include "queuemodel.h" #include "queuemodel.h"
#include "fetcher.h" #include "fetcher.h"
QueueModel::QueueModel(QObject *parent) QueueModel::QueueModel(QObject *parent)
: QAbstractListModel(parent) : QAbstractListModel(parent)
{ {
connect(&Fetcher::instance(), &Fetcher::downloadFinished, this, [this](const QString &url) { connect(&DataManager::instance(), &DataManager::queueEntryMoved, this, [this](const int &from, const int &to_orig) {
beginResetModel(); int to = (from < to_orig) ? to_orig + 1 : to_orig;
for (auto &entry : m_entries) { beginMoveRows(QModelIndex(), from, from, QModelIndex(), to);
delete entry; endMoveRows();
} qDebug() << "Moved entry" << from << "to" << to;
m_entries.clear(); });
updateQueue(); connect(&DataManager::instance(), &DataManager::queueEntryAdded, this, [this](const int &pos, const QString &id) {
endResetModel(); beginInsertRows(QModelIndex(), pos, pos);
endInsertRows();
qDebug() << "Added entry at pos" << pos;
}); });
updateQueue();
}
void QueueModel::updateQueue()
{
/*
QSqlQuery query;
query.prepare(QStringLiteral("SELECT * FROM Enclosures"));
Database::instance().execute(query);
while (query.next()) {
int feed_index = -1;
int entry_index = -1;
QString feedurl = query.value(QStringLiteral("feed")).toString();
QString id = query.value(QStringLiteral("id")).toString();
int duration = query.value(QStringLiteral("duration")).toInt();
int size = query.value(QStringLiteral("size")).toInt();
QString title = query.value(QStringLiteral("title")).toString();
QString type = query.value(QStringLiteral("type")).toString();
QString url = query.value(QStringLiteral("url")).toString();
QFile file(Fetcher::instance().filePath(url));
if(file.size() == size) {
// Enclosure is in database and file has been downloaded !
// Let's find the feed and entry index value so we can create
// an entry object
QSqlQuery feedQuery;
feedQuery.prepare(QStringLiteral("SELECT rowid FROM Feeds WHERE url=:feedurl"));
feedQuery.bindValue(QStringLiteral(":feedurl"), feedurl);
Database::instance().execute(feedQuery);
if (!feedQuery.next()) {
qWarning() << "Feed not found:" << feedurl;
// TODO: remove enclosures belonging to non-existent feed
}
feed_index = feedQuery.value(QStringLiteral("rowid")).toInt() - 1;
qDebug() << feed_index << feedurl;
// Find query index
QSqlQuery entryQuery;
entryQuery.prepare(QStringLiteral("SELECT id FROM Entries WHERE feed=:feedurl;"));
entryQuery.bindValue(QStringLiteral(":feedurl"), feedurl);
entryQuery.bindValue(QStringLiteral(":id"), id);
Database::instance().execute(entryQuery);
int counter = -1;
while (entryQuery.next()) {
counter++;
QString idquery = entryQuery.value(QStringLiteral("id")).toString();
if (idquery == id) entry_index = counter;
}
qDebug() << entry_index << id;
} else {
// TODO: there is some problem with the already downloaded file
// should probably delete the file (and reset status probably not needed)
}
if ((feed_index > -1) && (entry_index > -1)) {
Feed *feed = new Feed(feed_index);
Entry *entry = new Entry(feed, entry_index);
m_entries.append(entry);
}
}
*/
}
QueueModel::~QueueModel()
{
qDeleteAll(m_entries);
} }
QVariant QueueModel::data(const QModelIndex &index, int role) const QVariant QueueModel::data(const QModelIndex &index, int role) const
{ {
if (role != 0) if (role != 0)
return QVariant(); return QVariant();
if (m_entries[index.row()] == nullptr) qDebug() << "return entry" << DataManager::instance().getQueueEntry(index.row());
return QVariant(); return QVariant::fromValue(DataManager::instance().getQueueEntry(index.row()));
return QVariant::fromValue(m_entries[index.row()]);
} }
QHash<int, QByteArray> QueueModel::roleNames() const QHash<int, QByteArray> QueueModel::roleNames() const
@ -118,33 +49,11 @@ QHash<int, QByteArray> QueueModel::roleNames() const
int QueueModel::rowCount(const QModelIndex &parent) const int QueueModel::rowCount(const QModelIndex &parent) const
{ {
Q_UNUSED(parent) Q_UNUSED(parent)
return m_entries.size(); qDebug() << "queueCount is" << DataManager::instance().queueCount();
return DataManager::instance().queueCount();
} }
bool QueueModel::move(int from, int to) /*void QueueModel::addEntry(QString feedurl, QString id) {
{
return moveRows(QModelIndex(), from, 1, QModelIndex(), to);
}
bool QueueModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
{
Q_ASSERT(count == 1); // Only implemented the case of moving one row at a time
Q_ASSERT(sourceParent == destinationParent); // No moving between lists
int to = (sourceRow < destinationChild) ? destinationChild + 1 : destinationChild;
if (!beginMoveRows(sourceParent, sourceRow, sourceRow, destinationParent, to)) {
return false;
}
m_entries.move(sourceRow, destinationChild);
endMoveRows();
return true;
}
void QueueModel::addEntry(QString feedurl, QString id) {
qDebug() << feedurl << id; qDebug() << feedurl << id;
QSqlQuery feedQuery; QSqlQuery feedQuery;
@ -179,3 +88,4 @@ void QueueModel::addEntry(QString feedurl, QString id) {
endInsertRows(); endInsertRows();
return; return;
} }
*/

View File

@ -19,17 +19,13 @@ class QueueModel : public QAbstractListModel
public: public:
explicit QueueModel(QObject* = nullptr); explicit QueueModel(QObject* = nullptr);
~QueueModel() override; //~QueueModel() override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QHash<int, QByteArray> roleNames() const override; QHash<int, QByteArray> roleNames() const override;
int rowCount(const QModelIndex &parent) const override; int rowCount(const QModelIndex &parent) const override;
Q_INVOKABLE bool move(int from, int to); //Q_INVOKABLE bool move(int from, int to);
Q_INVOKABLE bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override; //Q_INVOKABLE bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override;
Q_INVOKABLE void addEntry(QString feedurl, QString id); //Q_INVOKABLE void addEntry(QString feedurl, QString id);
private:
void updateQueue();
mutable QVector<Entry *> m_entries;
}; };