Delete image when deleting feed

This commit is contained in:
Tobias Fella 2020-04-26 23:40:09 +02:00
parent cfb18072fc
commit 14d8819fb5
3 changed files with 22 additions and 10 deletions

View File

@ -35,9 +35,7 @@ FeedListModel::FeedListModel(QObject *parent)
setEditStrategy(OnFieldChange); setEditStrategy(OnFieldChange);
select(); select();
connect(&Fetcher::instance(), &Fetcher::updated, this, [this]() { connect(&Fetcher::instance(), &Fetcher::updated, this, [this]() { select(); });
select();
});
} }
QHash<int, QByteArray> FeedListModel::roleNames() const QHash<int, QByteArray> FeedListModel::roleNames() const
@ -85,6 +83,7 @@ bool FeedListModel::feedExists(QString url)
void FeedListModel::removeFeed(int index) void FeedListModel::removeFeed(int index)
{ {
Fetcher::instance().removeImage(data(createIndex(index, 0), Image).toString());
QSqlQuery query; QSqlQuery query;
query.prepare(QStringLiteral("DELETE FROM Authors WHERE feed=:feed;")); query.prepare(QStringLiteral("DELETE FROM Authors WHERE feed=:feed;"));
query.bindValue(QStringLiteral(":feed"), data(createIndex(index, 0), 1).toString()); query.bindValue(QStringLiteral(":feed"), data(createIndex(index, 0), 1).toString());

View File

@ -18,11 +18,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <QFile>
#include <QFileInfo>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
#include <QNetworkReply> #include <QNetworkReply>
#include <QFile>
#include <QStandardPaths> #include <QStandardPaths>
#include <QFileInfo>
#include <Syndication/Syndication> #include <Syndication/Syndication>
@ -56,7 +56,7 @@ void Fetcher::fetch(QUrl url)
query.prepare(QStringLiteral("UPDATE Feeds SET name=:name, image=:image WHERE url=:url;")); query.prepare(QStringLiteral("UPDATE Feeds SET name=:name, image=:image WHERE url=:url;"));
query.bindValue(QStringLiteral(":name"), feed->title()); query.bindValue(QStringLiteral(":name"), feed->title());
query.bindValue(QStringLiteral(":url"), url.toString()); query.bindValue(QStringLiteral(":url"), url.toString());
if(feed->image()->url().startsWith(QStringLiteral("/"))) { if (feed->image()->url().startsWith(QStringLiteral("/"))) {
QString absolute = url.adjusted(QUrl::RemovePath).toString() + feed->image()->url(); QString absolute = url.adjusted(QUrl::RemovePath).toString() + feed->image()->url();
query.bindValue(QStringLiteral(":image"), absolute); query.bindValue(QStringLiteral(":image"), absolute);
} else } else
@ -101,9 +101,8 @@ void Fetcher::fetch(QUrl url)
QString Fetcher::image(QString url) QString Fetcher::image(QString url)
{ {
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/") + QString::fromStdString(QCryptographicHash::hash(url.toUtf8(), QCryptographicHash::Md5).toHex().toStdString()); QString path = imagePath(url);
if (QFileInfo(path).exists()) {
if(QFileInfo(path).exists()) {
return path; return path;
} }
@ -122,3 +121,14 @@ QString Fetcher::image(QString url)
return QStringLiteral(""); return QStringLiteral("");
} }
void Fetcher::removeImage(QString url)
{
qDebug() << imagePath(url);
QFile(imagePath(url)).remove();
}
QString Fetcher::imagePath(QString url)
{
return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/") + QString::fromStdString(QCryptographicHash::hash(url.toUtf8(), QCryptographicHash::Md5).toHex().toStdString());
}

View File

@ -20,9 +20,9 @@
#pragma once #pragma once
#include <QNetworkAccessManager>
#include <QObject> #include <QObject>
#include <QUrl> #include <QUrl>
#include <QNetworkAccessManager>
class Fetcher : public QObject class Fetcher : public QObject
{ {
@ -35,11 +35,14 @@ public:
} }
void fetch(QUrl); void fetch(QUrl);
QString image(QString); QString image(QString);
void removeImage(QString);
private: private:
Fetcher(); Fetcher();
Fetcher(const Fetcher &); Fetcher(const Fetcher &);
QString imagePath(QString);
QNetworkAccessManager *manager; QNetworkAccessManager *manager;
Q_SIGNALS: Q_SIGNALS: