Finish most of the Queue manipulations

This commit is contained in:
Bart De Vries 2021-04-03 19:29:40 +02:00
parent 4f11060a41
commit e670a65f6d
6 changed files with 86 additions and 13 deletions

View File

@ -1,5 +1,5 @@
/**
* SPDX-FileCopyrightText: 2020 Tobias Fella <fella@posteo.de>
* SPDX-FileCopyrightText: 2021 Bart De Vries <bart@mogwai.be>
*
* 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_queuemap.count(); i++) {
query.prepare(QStringLiteral("UPDATE Queue SET listnr=:i WHERE id=:id;"));
query.bindValue(QStringLiteral(":i"), i);
query.bindValue(QStringLiteral(":id"), m_queuemap[i]);
Database::instance().execute(query);
}
}

View File

@ -37,11 +37,12 @@ public:
//Q_INVOKABLE void removeEntry(const QString &url);
//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 removeQueueItem(const int &index);
Q_INVOKABLE void removeQueueItem(const Entry* entry);
Q_INVOKABLE void importFeeds(const QString &path);
Q_INVOKABLE void exportFeeds(const QString &path);
@ -52,8 +53,8 @@ Q_SIGNALS:
void entryAdded(const QString &feedurl, const QString &id);
//void entryRemoved(const Feed*, const int &index); // TODO: implement this signal
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 queueEntryAdded(const int &index, const QString &id);
void queueEntryRemoved(const int &index, const QString &id);
void queueEntryMoved(const int &from, const int $to);
private:
@ -61,6 +62,7 @@ private:
void loadFeed(QString feedurl) const;
void loadEntry(QString id) const;
bool feedExists(const QString &url);
void updateQueueListnrs() const;
mutable QHash<QString, Feed*> m_feeds; // hash of pointers to all feeds in db, key = url (lazy loading)
mutable QHash<QString, Entry*> m_entries; // hash of pointers to all entries in db, key = id (lazy loading)

View File

@ -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();
}

View File

@ -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) }
}
]
}

View File

@ -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

View File

@ -23,9 +23,4 @@ public:
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QHash<int, QByteArray> 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);
};