2021-02-21 19:54:10 +01:00
|
|
|
/**
|
|
|
|
* SPDX-FileCopyrightText: 2020 Tobias Fella <fella@posteo.de>
|
2021-04-11 20:30:58 +02:00
|
|
|
* SPDX-FileCopyrightText: 2021 Bart De Vries <bart@mogwai.be>
|
2021-02-21 19:54:10 +01:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "enclosure.h"
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QSqlQuery>
|
|
|
|
|
|
|
|
#include "database.h"
|
2021-04-18 21:56:33 +02:00
|
|
|
#include "datamanager.h"
|
2021-02-21 19:54:10 +01:00
|
|
|
#include "enclosuredownloadjob.h"
|
|
|
|
#include "entry.h"
|
|
|
|
#include "fetcher.h"
|
|
|
|
|
|
|
|
Enclosure::Enclosure(Entry *entry)
|
|
|
|
: QObject(entry)
|
|
|
|
, m_entry(entry)
|
|
|
|
{
|
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare(QStringLiteral("SELECT * FROM Enclosures WHERE id=:id"));
|
|
|
|
query.bindValue(QStringLiteral(":id"), entry->id());
|
|
|
|
Database::instance().execute(query);
|
|
|
|
|
|
|
|
if (!query.next()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_duration = query.value(QStringLiteral("duration")).toInt();
|
|
|
|
m_size = query.value(QStringLiteral("size")).toInt();
|
|
|
|
m_title = query.value(QStringLiteral("title")).toString();
|
|
|
|
m_type = query.value(QStringLiteral("type")).toString();
|
|
|
|
m_url = query.value(QStringLiteral("url")).toString();
|
2021-04-10 22:28:36 +02:00
|
|
|
m_playposition = query.value(QStringLiteral("playposition")).toLongLong();
|
2021-04-18 21:56:33 +02:00
|
|
|
m_status = query.value(QStringLiteral("downloaded")).toBool() ? Downloaded : Downloadable;
|
2021-04-10 22:28:36 +02:00
|
|
|
m_playposition_dbsave = m_playposition;
|
2021-02-21 19:54:10 +01:00
|
|
|
|
2021-04-18 21:56:33 +02:00
|
|
|
// In principle the database contains this status, we check anyway in case
|
|
|
|
// something changed on disk
|
2021-04-03 19:29:40 +02:00
|
|
|
QFile file(path());
|
2021-04-14 11:22:35 +02:00
|
|
|
if (file.exists()) {
|
|
|
|
if(file.size() == m_size) {
|
2021-04-18 21:56:33 +02:00
|
|
|
if (m_status == Downloadable) {
|
|
|
|
// file is on disk, but was not expected, write to database
|
|
|
|
// this should never happen
|
|
|
|
m_status = Downloaded;
|
|
|
|
query.prepare(QStringLiteral("UPDATE Enclosures SET downloaded=:downloaded WHERE id=:id;"));
|
|
|
|
query.bindValue(QStringLiteral(":id"), entry->id());
|
|
|
|
query.bindValue(QStringLiteral(":downloaded"), true);
|
|
|
|
Database::instance().execute(query);
|
|
|
|
}
|
2021-04-14 11:22:35 +02:00
|
|
|
} else {
|
2021-04-18 21:56:33 +02:00
|
|
|
if (m_status == Downloaded) {
|
|
|
|
// file was downloaded, but there is a size mismatch
|
|
|
|
// delete file and update status in database
|
|
|
|
file.remove();
|
|
|
|
m_status = Downloadable;
|
|
|
|
query.prepare(QStringLiteral("UPDATE Enclosures SET downloaded=:downloaded WHERE id=:id;"));
|
|
|
|
query.bindValue(QStringLiteral(":id"), entry->id());
|
|
|
|
query.bindValue(QStringLiteral(":downloaded"), false);
|
|
|
|
Database::instance().execute(query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (m_status == Downloaded) {
|
|
|
|
// file was supposed to be on disk, but isn't there
|
|
|
|
// update status and write to the database
|
2021-02-21 19:54:10 +01:00
|
|
|
file.remove();
|
2021-04-14 11:22:35 +02:00
|
|
|
m_status = Downloadable;
|
2021-04-18 21:56:33 +02:00
|
|
|
query.prepare(QStringLiteral("UPDATE Enclosures SET downloaded=:downloaded WHERE id=:id;"));
|
|
|
|
query.bindValue(QStringLiteral(":id"), entry->id());
|
|
|
|
query.bindValue(QStringLiteral(":downloaded"), false);
|
|
|
|
Database::instance().execute(query);
|
2021-02-21 19:54:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Enclosure::download()
|
|
|
|
{
|
2021-04-07 12:47:46 +02:00
|
|
|
EnclosureDownloadJob *downloadJob = new EnclosureDownloadJob(m_url, path(), m_entry->title());
|
2021-02-21 19:54:10 +01:00
|
|
|
downloadJob->start();
|
|
|
|
|
|
|
|
m_downloadProgress = 0;
|
|
|
|
Q_EMIT downloadProgressChanged();
|
|
|
|
|
|
|
|
m_entry->feed()->setErrorId(0);
|
|
|
|
m_entry->feed()->setErrorString(QString());
|
|
|
|
|
|
|
|
connect(downloadJob, &KJob::result, this, [this, downloadJob]() {
|
|
|
|
if(downloadJob->error() == 0) {
|
2021-04-09 22:32:13 +02:00
|
|
|
processDownloadedFile();
|
2021-02-21 19:54:10 +01:00
|
|
|
} else {
|
|
|
|
m_status = Downloadable;
|
|
|
|
if(downloadJob->error() != QNetworkReply::OperationCanceledError) {
|
|
|
|
m_entry->feed()->setErrorId(downloadJob->error());
|
|
|
|
m_entry->feed()->setErrorString(downloadJob->errorString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
disconnect(this, &Enclosure::cancelDownload, this, nullptr);
|
|
|
|
Q_EMIT statusChanged();
|
|
|
|
});
|
|
|
|
|
|
|
|
connect(this, &Enclosure::cancelDownload, this, [this, downloadJob]() {
|
|
|
|
downloadJob->doKill();
|
|
|
|
m_status = Downloadable;
|
|
|
|
Q_EMIT statusChanged();
|
2021-04-18 21:56:33 +02:00
|
|
|
Q_EMIT DataManager::instance().downloadCountChanged(m_entry->feed()->url());
|
2021-02-21 19:54:10 +01:00
|
|
|
disconnect(this, &Enclosure::cancelDownload, this, nullptr);
|
|
|
|
});
|
|
|
|
|
|
|
|
connect(downloadJob, &KJob::percentChanged, this, [=](KJob*, unsigned long percent) {
|
|
|
|
m_downloadProgress = percent;
|
|
|
|
Q_EMIT downloadProgressChanged();
|
|
|
|
});
|
|
|
|
|
|
|
|
m_status = Downloading;
|
|
|
|
Q_EMIT statusChanged();
|
|
|
|
}
|
|
|
|
|
2021-04-09 22:32:13 +02:00
|
|
|
void Enclosure::processDownloadedFile() {
|
|
|
|
// This will be run if the enclosure has been downloaded successfully
|
2021-04-18 21:56:33 +02:00
|
|
|
m_status = Downloaded;
|
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare(QStringLiteral("UPDATE Enclosures SET downloaded=:downloaded WHERE id=:id;"));
|
|
|
|
query.bindValue(QStringLiteral(":id"), m_entry->id());
|
|
|
|
query.bindValue(QStringLiteral(":downloaded"), true);
|
|
|
|
Database::instance().execute(query);
|
2021-04-09 22:32:13 +02:00
|
|
|
// Unset "new" status of item
|
|
|
|
if (m_entry->getNew()) m_entry->setNew(false);
|
|
|
|
|
|
|
|
// Check if reported filesize in rss feed corresponds to real file size
|
|
|
|
// if not, correct the filesize in the database
|
|
|
|
// otherwise the file will get deleted because of mismatch in signature
|
|
|
|
QFile file(path());
|
|
|
|
if(file.size() != m_size) {
|
|
|
|
qDebug() << "enclosure file size mismatch" << m_entry->title();
|
|
|
|
m_size = file.size();
|
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare(QStringLiteral("UPDATE Enclosures SET size=:size WHERE id=:id AND feed=:feed"));
|
|
|
|
query.bindValue(QStringLiteral(":id"), m_entry->id());
|
|
|
|
query.bindValue(QStringLiteral(":feed"), m_entry->feed()->url());
|
|
|
|
query.bindValue(QStringLiteral(":size"), m_size);
|
|
|
|
Database::instance().execute(query);
|
|
|
|
}
|
2021-04-18 21:56:33 +02:00
|
|
|
Q_EMIT DataManager::instance().downloadCountChanged(m_entry->feed()->url());
|
|
|
|
|
2021-04-09 22:32:13 +02:00
|
|
|
}
|
|
|
|
|
2021-02-21 19:54:10 +01:00
|
|
|
void Enclosure::deleteFile()
|
|
|
|
{
|
2021-04-03 21:39:20 +02:00
|
|
|
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
|
2021-02-21 19:54:10 +01:00
|
|
|
m_status = Downloadable;
|
2021-04-18 21:56:33 +02:00
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare(QStringLiteral("UPDATE Enclosures SET downloaded=:downloaded WHERE id=:id;"));
|
|
|
|
query.bindValue(QStringLiteral(":id"), m_entry->id());
|
|
|
|
query.bindValue(QStringLiteral(":downloaded"), false);
|
|
|
|
Database::instance().execute(query);
|
2021-02-21 19:54:10 +01:00
|
|
|
Q_EMIT statusChanged();
|
2021-04-18 21:56:33 +02:00
|
|
|
Q_EMIT DataManager::instance().downloadCountChanged(m_entry->feed()->url());
|
2021-02-21 19:54:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Enclosure::path() const
|
|
|
|
{
|
2021-04-07 12:47:46 +02:00
|
|
|
return Fetcher::instance().enclosurePath(m_url);
|
2021-02-21 19:54:10 +01:00
|
|
|
}
|
2021-04-10 22:28:36 +02:00
|
|
|
|
2021-04-13 20:51:00 +02:00
|
|
|
Enclosure::Status Enclosure::status() const
|
|
|
|
{
|
|
|
|
return m_status;
|
|
|
|
}
|
2021-04-18 22:21:59 +02:00
|
|
|
|
2021-04-10 22:28:36 +02:00
|
|
|
qint64 Enclosure::playPosition() const{
|
|
|
|
return m_playposition;
|
|
|
|
}
|
|
|
|
|
2021-04-18 22:21:59 +02:00
|
|
|
qint64 Enclosure::duration() const {
|
|
|
|
return m_duration;
|
|
|
|
}
|
|
|
|
|
2021-04-10 22:28:36 +02:00
|
|
|
void Enclosure::setPlayPosition(const qint64 &position)
|
|
|
|
{
|
|
|
|
m_playposition = position;
|
2021-04-11 20:31:17 +02:00
|
|
|
qDebug() << "save playPosition" << position << m_entry->title();
|
2021-04-15 21:52:32 +02:00
|
|
|
Q_EMIT playPositionChanged();
|
2021-04-10 22:28:36 +02:00
|
|
|
|
|
|
|
// let's only save the play position to the database every 15 seconds
|
2021-04-13 22:11:12 +02:00
|
|
|
if ((abs(m_playposition - m_playposition_dbsave) > 15000) || position == 0) {
|
2021-04-11 20:31:17 +02:00
|
|
|
qDebug() << "save playPosition to database" << position << m_entry->title();
|
2021-04-10 22:28:36 +02:00
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare(QStringLiteral("UPDATE Enclosures SET playposition=:playposition WHERE id=:id AND feed=:feed"));
|
|
|
|
query.bindValue(QStringLiteral(":id"), m_entry->id());
|
|
|
|
query.bindValue(QStringLiteral(":feed"), m_entry->feed()->url());
|
|
|
|
query.bindValue(QStringLiteral(":playposition"), m_playposition);
|
|
|
|
Database::instance().execute(query);
|
|
|
|
m_playposition_dbsave = m_playposition;
|
|
|
|
}
|
|
|
|
}
|
2021-04-18 22:21:59 +02:00
|
|
|
|
|
|
|
void Enclosure::setDuration(const qint64 &duration)
|
|
|
|
{
|
|
|
|
m_duration = duration;
|
|
|
|
Q_EMIT durationChanged();
|
|
|
|
|
|
|
|
// also save to database
|
|
|
|
qDebug() << "updating entry duration" << duration << m_entry->title();
|
|
|
|
QSqlQuery query;
|
|
|
|
query.prepare(QStringLiteral("UPDATE Enclosures SET duration=:duration WHERE id=:id AND feed=:feed"));
|
|
|
|
query.bindValue(QStringLiteral(":id"), m_entry->id());
|
|
|
|
query.bindValue(QStringLiteral(":feed"), m_entry->feed()->url());
|
|
|
|
query.bindValue(QStringLiteral(":duration"), m_duration);
|
|
|
|
Database::instance().execute(query);
|
|
|
|
}
|