Make sure that everything is cleaned up when removing a feed
This commit is contained in:
parent
e670a65f6d
commit
7e05231b63
@ -145,42 +145,56 @@ void DataManager::removeFeed(const int &index)
|
||||
{
|
||||
// Get feed pointer
|
||||
Feed* feed = m_feeds[m_feedmap[index]];
|
||||
const QString feedurl = feed->url();
|
||||
|
||||
// First delete everything from the database
|
||||
// Delete the object instances and mappings
|
||||
// First delete entries in Queue
|
||||
qDebug() << "delete queueentries of" << feedurl;
|
||||
for (auto& id : m_queuemap) {
|
||||
if (getEntry(id)->feed()->url() == feedurl) {
|
||||
removeQueueItem(id);
|
||||
}
|
||||
}
|
||||
|
||||
// Delete entries themselves
|
||||
qDebug() << "delete entries of" << feedurl;
|
||||
for (auto& id : m_entrymap[feedurl]) {
|
||||
if (getEntry(id)->hasEnclosure()) getEntry(id)->enclosure()->deleteFile(); // delete enclosure (if it exists)
|
||||
delete m_entries[id]; // delete pointer
|
||||
m_entries.remove(id); // delete the hash key
|
||||
}
|
||||
m_entrymap.remove(feedurl); // remove all the entry mappings belonging to the feed
|
||||
|
||||
qDebug() << "Remove image" << feed->image() << "for feed" << feedurl;
|
||||
if (!feed->image().isEmpty()) Fetcher::instance().removeImage(feed->image());
|
||||
delete feed; // remove the pointer
|
||||
m_feeds.remove(m_feedmap[index]); // remove from m_feeds
|
||||
m_feedmap.removeAt(index); // remove from m_feedmap
|
||||
|
||||
// Then delete everything from the database
|
||||
qDebug() << "delete database part of" << feedurl;
|
||||
|
||||
// Delete Authors
|
||||
QSqlQuery query;
|
||||
query.prepare(QStringLiteral("DELETE FROM Authors WHERE feed=:feed;"));
|
||||
query.bindValue(QStringLiteral(":feed"), feed->url());
|
||||
query.bindValue(QStringLiteral(":feed"), feedurl);
|
||||
Database::instance().execute(query);
|
||||
|
||||
// Delete Entries
|
||||
query.prepare(QStringLiteral("DELETE FROM Entries WHERE feed=:feed;"));
|
||||
query.bindValue(QStringLiteral(":feed"), feed->url());
|
||||
query.bindValue(QStringLiteral(":feed"), feedurl);
|
||||
Database::instance().execute(query);
|
||||
|
||||
// Delete Enclosures
|
||||
query.prepare(QStringLiteral("DELETE FROM Enclosures WHERE feed=:feed;"));
|
||||
query.bindValue(QStringLiteral(":feed"), feed->url());
|
||||
query.bindValue(QStringLiteral(":feed"), feedurl);
|
||||
Database::instance().execute(query);
|
||||
|
||||
// Delete Feed
|
||||
query.prepare(QStringLiteral("DELETE FROM Feeds WHERE url=:url;"));
|
||||
query.bindValue(QStringLiteral(":url"), feed->url());
|
||||
query.bindValue(QStringLiteral(":url"), feedurl);
|
||||
Database::instance().execute(query);
|
||||
|
||||
// TODO: delete files: enclosure files and images
|
||||
|
||||
// Then delete the object instances and mappings
|
||||
for (auto& id : m_entrymap[feed->url()]) {
|
||||
delete m_entries[id]; // delete pointer
|
||||
m_entries.remove(id); // delete the hash key
|
||||
}
|
||||
m_entrymap.remove(feed->url()); // remove all the entry mappings belonging to the feed
|
||||
|
||||
delete feed; // remove the pointer
|
||||
m_feeds.remove(m_feedmap[index]); // remove from m_feeds
|
||||
m_feedmap.removeAt(index); // remove from m_feedmap
|
||||
Q_EMIT(feedRemoved(index));
|
||||
}
|
||||
|
||||
@ -281,9 +295,9 @@ void DataManager::removeQueueItem(const int &index)
|
||||
Q_EMIT queueEntryRemoved(index, id);
|
||||
}
|
||||
|
||||
void DataManager::removeQueueItem(const Entry* entry)
|
||||
void DataManager::removeQueueItem(const QString id)
|
||||
{
|
||||
removeQueueItem(m_queuemap.indexOf(entry->id()));
|
||||
removeQueueItem(m_queuemap.indexOf(id));
|
||||
}
|
||||
|
||||
void DataManager::importFeeds(const QString &path)
|
||||
|
@ -42,7 +42,7 @@ public:
|
||||
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 removeQueueItem(const QString id);
|
||||
|
||||
Q_INVOKABLE void importFeeds(const QString &path);
|
||||
Q_INVOKABLE void exportFeeds(const QString &path);
|
||||
|
@ -88,9 +88,14 @@ void Enclosure::download()
|
||||
|
||||
void Enclosure::deleteFile()
|
||||
{
|
||||
QFile(path()).remove();
|
||||
qDebug() << "Trying to delete enclosure file" << path();
|
||||
// First check if file still exists; you never know what has happened
|
||||
if (QFile(path()).exists())
|
||||
QFile(path()).remove();
|
||||
// If file disappeared unexpectedly, then still change status to downloadable
|
||||
m_status = Downloadable;
|
||||
Q_EMIT statusChanged();
|
||||
|
||||
}
|
||||
|
||||
QString Enclosure::path() const
|
||||
|
@ -12,41 +12,6 @@
|
||||
|
||||
#include "database.h"
|
||||
|
||||
Entry::Entry(Feed *feed, int index)
|
||||
: QObject(nullptr)
|
||||
, m_feed(feed)
|
||||
{
|
||||
QSqlQuery entryQuery;
|
||||
entryQuery.prepare(QStringLiteral("SELECT * FROM Entries WHERE feed=:feed ORDER BY updated DESC LIMIT 1 OFFSET :index;"));
|
||||
entryQuery.bindValue(QStringLiteral(":feed"), m_feed->url());
|
||||
entryQuery.bindValue(QStringLiteral(":index"), index);
|
||||
Database::instance().execute(entryQuery);
|
||||
if (!entryQuery.next())
|
||||
qWarning() << "No element with index" << index << "found in feed" << m_feed->url();
|
||||
|
||||
QSqlQuery authorQuery;
|
||||
authorQuery.prepare(QStringLiteral("SELECT * FROM Authors WHERE id=:id"));
|
||||
authorQuery.bindValue(QStringLiteral(":id"), entryQuery.value(QStringLiteral("id")).toString());
|
||||
Database::instance().execute(authorQuery);
|
||||
|
||||
while (authorQuery.next()) {
|
||||
m_authors += new Author(authorQuery.value(QStringLiteral("name")).toString(), authorQuery.value(QStringLiteral("email")).toString(), authorQuery.value(QStringLiteral("uri")).toString(), nullptr);
|
||||
}
|
||||
|
||||
m_created.setSecsSinceEpoch(entryQuery.value(QStringLiteral("created")).toInt());
|
||||
m_updated.setSecsSinceEpoch(entryQuery.value(QStringLiteral("updated")).toInt());
|
||||
|
||||
m_id = entryQuery.value(QStringLiteral("id")).toString();
|
||||
m_title = entryQuery.value(QStringLiteral("title")).toString();
|
||||
m_content = entryQuery.value(QStringLiteral("content")).toString();
|
||||
m_link = entryQuery.value(QStringLiteral("link")).toString();
|
||||
m_read = entryQuery.value(QStringLiteral("read")).toBool();
|
||||
|
||||
if (entryQuery.value(QStringLiteral("hasEnclosure")).toBool()) {
|
||||
m_enclosure = new Enclosure(this);
|
||||
}
|
||||
}
|
||||
|
||||
Entry::Entry(Feed *feed, QString id)
|
||||
: QObject(nullptr)
|
||||
, m_feed(feed)
|
||||
@ -78,6 +43,7 @@ Entry::Entry(Feed *feed, QString id)
|
||||
m_read = entryQuery.value(QStringLiteral("read")).toBool();
|
||||
|
||||
if (entryQuery.value(QStringLiteral("hasEnclosure")).toBool()) {
|
||||
m_hasenclosure = true;
|
||||
m_enclosure = new Enclosure(this);
|
||||
}
|
||||
}
|
||||
@ -184,6 +150,11 @@ Enclosure *Entry::enclosure() const
|
||||
return m_enclosure;
|
||||
}
|
||||
|
||||
bool Entry::hasEnclosure() const
|
||||
{
|
||||
return m_hasenclosure;
|
||||
}
|
||||
|
||||
Feed *Entry::feed() const
|
||||
{
|
||||
return m_feed;
|
||||
|
@ -32,9 +32,9 @@ class Entry : public QObject
|
||||
Q_PROPERTY(QString baseUrl READ baseUrl CONSTANT)
|
||||
Q_PROPERTY(bool read READ read WRITE setRead NOTIFY readChanged);
|
||||
Q_PROPERTY(Enclosure *enclosure READ enclosure CONSTANT);
|
||||
Q_PROPERTY(bool hasEnclosure READ hasEnclosure CONSTANT);
|
||||
|
||||
public:
|
||||
Entry(Feed *feed, int index);
|
||||
Entry(Feed *feed, QString id);
|
||||
~Entry();
|
||||
|
||||
@ -47,6 +47,7 @@ public:
|
||||
QString link() const;
|
||||
bool read() const;
|
||||
Enclosure *enclosure() const;
|
||||
bool hasEnclosure() const;
|
||||
Feed *feed() const;
|
||||
|
||||
QString baseUrl() const;
|
||||
@ -69,6 +70,7 @@ private:
|
||||
QString m_link;
|
||||
bool m_read;
|
||||
Enclosure *m_enclosure = nullptr;
|
||||
bool m_hasenclosure = false;
|
||||
};
|
||||
|
||||
#endif // ENTRY_H
|
||||
|
@ -59,40 +59,3 @@ int QueueModel::rowCount(const QModelIndex &parent) const
|
||||
qDebug() << "queueCount is" << DataManager::instance().queueCount();
|
||||
return DataManager::instance().queueCount();
|
||||
}
|
||||
|
||||
/*void QueueModel::addEntry(QString feedurl, QString id) {
|
||||
qDebug() << feedurl << id;
|
||||
|
||||
QSqlQuery feedQuery;
|
||||
feedQuery.prepare(QStringLiteral("SELECT * 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
|
||||
return;
|
||||
}
|
||||
int feed_index = feedQuery.value(QStringLiteral("rowid")).toInt() - 1;
|
||||
qDebug() << feed_index << feedurl;
|
||||
Feed* feed = new Feed(feed_index);
|
||||
|
||||
// Find query index
|
||||
QSqlQuery entryQuery;
|
||||
entryQuery.prepare(QStringLiteral("SELECT rowid FROM Entries WHERE feed=:feedurl ORDER BY updated;"));
|
||||
entryQuery.bindValue(QStringLiteral(":feedurl"), feedurl);
|
||||
Database::instance().execute(entryQuery);
|
||||
int counter = -1;
|
||||
int entry_index = -1;
|
||||
while (entryQuery.next()) {
|
||||
counter++;
|
||||
QString idquery = entryQuery.value(QStringLiteral("id")).toString();
|
||||
if (idquery == id) entry_index = counter;
|
||||
}
|
||||
if (entry_index == -1) return;
|
||||
|
||||
beginInsertRows(QModelIndex(), rowCount(QModelIndex()) - 1, rowCount(QModelIndex()) - 1);
|
||||
m_entries.append(new Entry(feed, entry_index));
|
||||
endInsertRows();
|
||||
return;
|
||||
}
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user