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);
select();
connect(&Fetcher::instance(), &Fetcher::updated, this, [this]() {
select();
});
connect(&Fetcher::instance(), &Fetcher::updated, this, [this]() { select(); });
}
QHash<int, QByteArray> FeedListModel::roleNames() const
@ -85,6 +83,7 @@ bool FeedListModel::feedExists(QString url)
void FeedListModel::removeFeed(int index)
{
Fetcher::instance().removeImage(data(createIndex(index, 0), Image).toString());
QSqlQuery query;
query.prepare(QStringLiteral("DELETE FROM Authors WHERE feed=:feed;"));
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/>.
*/
#include <QFile>
#include <QFileInfo>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QFile>
#include <QStandardPaths>
#include <QFileInfo>
#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.bindValue(QStringLiteral(":name"), feed->title());
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();
query.bindValue(QStringLiteral(":image"), absolute);
} else
@ -101,9 +101,8 @@ void Fetcher::fetch(QUrl url)
QString Fetcher::image(QString url)
{
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/") + QString::fromStdString(QCryptographicHash::hash(url.toUtf8(), QCryptographicHash::Md5).toHex().toStdString());
if(QFileInfo(path).exists()) {
QString path = imagePath(url);
if (QFileInfo(path).exists()) {
return path;
}
@ -122,3 +121,14 @@ QString Fetcher::image(QString url)
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
#include <QNetworkAccessManager>
#include <QObject>
#include <QUrl>
#include <QNetworkAccessManager>
class Fetcher : public QObject
{
@ -35,11 +35,14 @@ public:
}
void fetch(QUrl);
QString image(QString);
void removeImage(QString);
private:
Fetcher();
Fetcher(const Fetcher &);
QString imagePath(QString);
QNetworkAccessManager *manager;
Q_SIGNALS: