From e670a65f6d4ba453c7b1c2b28c8ae4956ad9e977 Mon Sep 17 00:00:00 2001 From: Bart De Vries Date: Sat, 3 Apr 2021 19:29:40 +0200 Subject: [PATCH] Finish most of the Queue manipulations --- src/datamanager.cpp | 70 +++++++++++++++++++++++++++++++++++++-- src/datamanager.h | 8 +++-- src/enclosure.cpp | 4 +-- src/qml/QueueDelegate.qml | 5 +++ src/queuemodel.cpp | 7 ++++ src/queuemodel.h | 5 --- 6 files changed, 86 insertions(+), 13 deletions(-) diff --git a/src/datamanager.cpp b/src/datamanager.cpp index 02710b35..0e52f856 100644 --- a/src/datamanager.cpp +++ b/src/datamanager.cpp @@ -1,5 +1,5 @@ /** - * SPDX-FileCopyrightText: 2020 Tobias Fella + * SPDX-FileCopyrightText: 2021 Bart De Vries * * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL */ @@ -71,6 +71,13 @@ DataManager::DataManager() } } qDebug() << m_entrymap; + + query.prepare(QStringLiteral("SELECT id FROM Queue ORDER BY listnr;")); + Database::instance().execute(query); + while (query.next()) { + m_queuemap += query.value(QStringLiteral("id")).toString(); + } + qDebug() << m_queuemap; } Feed* DataManager::getFeed(int const index) const @@ -221,18 +228,64 @@ int DataManager::queueCount() const void DataManager::addtoQueue(const QString &feedurl, const QString &id) { - // TODO: also add the entry to queue database table + // If item is already in queue, then stop here + if (m_queuemap.contains(id)) return; + + // Add to internal queuemap data structure m_queuemap += id; qDebug() << m_queuemap; - Q_EMIT queueEntryAdded(m_queuemap.length()-1, id); + + // Get index of this entry + const int index = m_queuemap.indexOf(id); // add new entry to end of queue + + // Add to Queue database + QSqlQuery query; + query.prepare(QStringLiteral("INSERT INTO Queue VALUES (:index, :feedurl, :id);")); + query.bindValue(QStringLiteral(":index"), index); + query.bindValue(QStringLiteral(":feedurl"), feedurl); + query.bindValue(QStringLiteral(":id"), id); + Database::instance().execute(query); + + // Make sure that the QueueModel is aware of the changes + Q_EMIT queueEntryAdded(index, id); } void DataManager::moveQueueItem(const int &from, const int &to) { + // First move the items in the internal data structure m_queuemap.move(from, to); + + // Then make sure that the database Queue table reflects these changes + updateQueueListnrs(); + + // Make sure that the QueueModel is aware of the changes so it can update Q_EMIT queueEntryMoved(from, to); } +void DataManager::removeQueueItem(const int &index) +{ + // First remove the item from the internal data structure + const QString id = m_queuemap[index]; + m_queuemap.removeAt(index); + qDebug() << m_queuemap; + + // Then make sure that the database Queue table reflects these changes + QSqlQuery query; + query.prepare(QStringLiteral("DELETE FROM Queue WHERE listnr=:listnr;")); + query.bindValue(QStringLiteral(":listnr"), index); + Database::instance().execute(query); + // ... and update all other listnrs in Queue table + updateQueueListnrs(); + + // Make sure that the QueueModel is aware of the change so it can update + Q_EMIT queueEntryRemoved(index, id); +} + +void DataManager::removeQueueItem(const Entry* entry) +{ + removeQueueItem(m_queuemap.indexOf(entry->id())); +} + void DataManager::importFeeds(const QString &path) { QUrl url(path); @@ -303,3 +356,14 @@ bool DataManager::feedExists(const QString &url) { return m_feeds.contains(url); } + +void DataManager::updateQueueListnrs() const +{ + QSqlQuery query; + for (int i=0; i m_feeds; // hash of pointers to all feeds in db, key = url (lazy loading) mutable QHash m_entries; // hash of pointers to all entries in db, key = id (lazy loading) diff --git a/src/enclosure.cpp b/src/enclosure.cpp index 77de40c1..85e1f8ed 100644 --- a/src/enclosure.cpp +++ b/src/enclosure.cpp @@ -34,7 +34,7 @@ Enclosure::Enclosure(Entry *entry) m_type = query.value(QStringLiteral("type")).toString(); m_url = query.value(QStringLiteral("url")).toString(); - QFile file(Fetcher::instance().filePath(m_url)); + QFile file(path()); if(file.size() == m_size) { m_status = Downloaded; } else { @@ -88,7 +88,7 @@ void Enclosure::download() void Enclosure::deleteFile() { - QFile(Fetcher::instance().filePath(m_url)).remove(); + QFile(path()).remove(); m_status = Downloadable; Q_EMIT statusChanged(); } diff --git a/src/qml/QueueDelegate.qml b/src/qml/QueueDelegate.qml index 8a8f160d..21fc272a 100644 --- a/src/qml/QueueDelegate.qml +++ b/src/qml/QueueDelegate.qml @@ -92,6 +92,11 @@ Kirigami.SwipeListItem { icon.name: "delete" onTriggered: model.entry.enclosure.deleteFile() visible: model.entry.enclosure && model.entry.enclosure.status === Enclosure.Downloaded + }, + Kirigami.Action { + text: i18n("Remove from Queue") + icon.name: "delete-table-row" + onTriggered: { DataManager.removeQueueItem(model.entry) } } ] } diff --git a/src/queuemodel.cpp b/src/queuemodel.cpp index 967d0e8d..5dac93ff 100644 --- a/src/queuemodel.cpp +++ b/src/queuemodel.cpp @@ -25,10 +25,17 @@ QueueModel::QueueModel(QObject *parent) qDebug() << "Moved entry" << from << "to" << to; }); connect(&DataManager::instance(), &DataManager::queueEntryAdded, this, [this](const int &pos, const QString &id) { + Q_UNUSED(id) beginInsertRows(QModelIndex(), pos, pos); endInsertRows(); qDebug() << "Added entry at pos" << pos; }); + connect(&DataManager::instance(), &DataManager::queueEntryRemoved, this, [this](const int &pos, const QString &id) { + Q_UNUSED(id) + beginRemoveRows(QModelIndex(), pos, pos); + endRemoveRows(); + qDebug() << "Removed entry at pos" << pos; + }); } QVariant QueueModel::data(const QModelIndex &index, int role) const diff --git a/src/queuemodel.h b/src/queuemodel.h index 66024d85..f556d31c 100644 --- a/src/queuemodel.h +++ b/src/queuemodel.h @@ -23,9 +23,4 @@ public: QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QHash roleNames() const override; int rowCount(const QModelIndex &parent) const override; - - //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 void addEntry(QString feedurl, QString id); - };