refactor: remove some superfluous signals

Since now we're inheriting from Task, some signals can be reused.
This commit is contained in:
flow 2022-04-26 21:25:42 -03:00
parent 8c8eabf7ac
commit efa3fbff39
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
7 changed files with 33 additions and 40 deletions

View File

@ -72,7 +72,7 @@ void Download::executeTask()
{ {
if (getState() == Task::State::AbortedByUser) { if (getState() == Task::State::AbortedByUser) {
qWarning() << "Attempt to start an aborted Download:" << m_url.toString(); qWarning() << "Attempt to start an aborted Download:" << m_url.toString();
emit aborted(m_index_within_job); emitAborted();
return; return;
} }
@ -80,7 +80,7 @@ void Download::executeTask()
m_state = m_sink->init(request); m_state = m_sink->init(request);
switch (m_state) { switch (m_state) {
case State::Succeeded: case State::Succeeded:
emit succeeded(m_index_within_job); emit succeeded();
qDebug() << "Download cache hit " << m_url.toString(); qDebug() << "Download cache hit " << m_url.toString();
return; return;
case State::Running: case State::Running:
@ -88,7 +88,7 @@ void Download::executeTask()
break; break;
case State::Inactive: case State::Inactive:
case State::Failed: case State::Failed:
emit failed(m_index_within_job); emitFailed();
return; return;
case State::AbortedByUser: case State::AbortedByUser:
return; return;
@ -102,8 +102,8 @@ void Download::executeTask()
QNetworkReply* rep = m_network->get(request); QNetworkReply* rep = m_network->get(request);
m_reply.reset(rep); m_reply.reset(rep);
connect(rep, SIGNAL(downloadProgress(qint64, qint64)), SLOT(downloadProgress(qint64, qint64))); connect(rep, &QNetworkReply::downloadProgress, this, &Download::downloadProgress);
connect(rep, SIGNAL(finished()), SLOT(downloadFinished())); connect(rep, &QNetworkReply::finished, this, &Download::downloadFinished);
connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(downloadError(QNetworkReply::NetworkError))); connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(downloadError(QNetworkReply::NetworkError)));
connect(rep, &QNetworkReply::sslErrors, this, &Download::sslErrors); connect(rep, &QNetworkReply::sslErrors, this, &Download::sslErrors);
connect(rep, &QNetworkReply::readyRead, this, &Download::downloadReadyRead); connect(rep, &QNetworkReply::readyRead, this, &Download::downloadReadyRead);
@ -112,7 +112,6 @@ void Download::executeTask()
void Download::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) void Download::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{ {
setProgress(bytesReceived, bytesTotal); setProgress(bytesReceived, bytesTotal);
emit netActionProgress(m_index_within_job, bytesReceived, bytesTotal);
} }
void Download::downloadError(QNetworkReply::NetworkError error) void Download::downloadError(QNetworkReply::NetworkError error)
@ -212,19 +211,19 @@ void Download::downloadFinished()
qDebug() << "Download failed but we are allowed to proceed:" << m_url.toString(); qDebug() << "Download failed but we are allowed to proceed:" << m_url.toString();
m_sink->abort(); m_sink->abort();
m_reply.reset(); m_reply.reset();
emit succeeded(m_index_within_job); emit succeeded();
return; return;
} else if (m_state == State::Failed) { } else if (m_state == State::Failed) {
qDebug() << "Download failed in previous step:" << m_url.toString(); qDebug() << "Download failed in previous step:" << m_url.toString();
m_sink->abort(); m_sink->abort();
m_reply.reset(); m_reply.reset();
emit failed(m_index_within_job); emitFailed();
return; return;
} else if (m_state == State::AbortedByUser) { } else if (m_state == State::AbortedByUser) {
qDebug() << "Download aborted in previous step:" << m_url.toString(); qDebug() << "Download aborted in previous step:" << m_url.toString();
m_sink->abort(); m_sink->abort();
m_reply.reset(); m_reply.reset();
emit aborted(m_index_within_job); emitAborted();
return; return;
} }
@ -241,12 +240,12 @@ void Download::downloadFinished()
qDebug() << "Download failed to finalize:" << m_url.toString(); qDebug() << "Download failed to finalize:" << m_url.toString();
m_sink->abort(); m_sink->abort();
m_reply.reset(); m_reply.reset();
emit failed(m_index_within_job); emitFailed();
return; return;
} }
m_reply.reset(); m_reply.reset();
qDebug() << "Download succeeded:" << m_url.toString(); qDebug() << "Download succeeded:" << m_url.toString();
emit succeeded(m_index_within_job); emit succeeded();
} }
void Download::downloadReadyRead() void Download::downloadReadyRead()

View File

@ -43,7 +43,7 @@
class NetAction : public Task { class NetAction : public Task {
Q_OBJECT Q_OBJECT
protected: protected:
explicit NetAction() : Task(nullptr) {}; explicit NetAction() : Task() {};
public: public:
using Ptr = shared_qobject_ptr<NetAction>; using Ptr = shared_qobject_ptr<NetAction>;
@ -51,13 +51,7 @@ class NetAction : public Task {
virtual ~NetAction() = default; virtual ~NetAction() = default;
QUrl url() { return m_url; } QUrl url() { return m_url; }
auto index() -> int { return m_index_within_job; }
signals:
void started(int index);
void netActionProgress(int index, qint64 current, qint64 total);
void succeeded(int index);
void failed(int index);
void aborted(int index);
protected slots: protected slots:
virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) = 0; virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) = 0;

View File

@ -45,9 +45,9 @@ auto NetJob::addNetAction(NetAction::Ptr action) -> bool
partProgress(m_parts_progress.count() - 1, action->getProgress(), action->getTotalProgress()); partProgress(m_parts_progress.count() - 1, action->getProgress(), action->getTotalProgress());
if (action->isRunning()) { if (action->isRunning()) {
connect(action.get(), &NetAction::succeeded, this, &NetJob::partSucceeded); connect(action.get(), &NetAction::succeeded, [this, action]{ partSucceeded(action->index()); });
connect(action.get(), &NetAction::failed, this, &NetJob::partFailed); connect(action.get(), &NetAction::failed, [this, action](QString){ partFailed(action->index()); });
connect(action.get(), &NetAction::netActionProgress, this, &NetJob::partProgress); connect(action.get(), &NetAction::progress, [this, action](qint64 done, qint64 total) { partProgress(action->index(), done, total); });
} else { } else {
m_todo.append(m_parts_progress.size() - 1); m_todo.append(m_parts_progress.size() - 1);
} }
@ -218,10 +218,10 @@ void NetJob::startMoreParts()
auto part = m_downloads[doThis]; auto part = m_downloads[doThis];
// connect signals :D // connect signals :D
connect(part.get(), &NetAction::succeeded, this, &NetJob::partSucceeded); connect(part.get(), &NetAction::succeeded, this, [this, part]{ partSucceeded(part->index()); });
connect(part.get(), &NetAction::failed, this, &NetJob::partFailed); connect(part.get(), &NetAction::failed, this, [this, part](QString){ partFailed(part->index()); });
connect(part.get(), &NetAction::aborted, this, &NetJob::partAborted); connect(part.get(), &NetAction::aborted, this, [this, part]{ partAborted(part->index()); });
connect(part.get(), &NetAction::netActionProgress, this, &NetJob::partProgress); connect(part.get(), &NetAction::progress, this, [this, part](qint64 done, qint64 total) { partProgress(part->index(), done, total); });
part->startAction(m_network); part->startAction(m_network);
} }

View File

@ -56,32 +56,32 @@ void ImgurAlbumCreation::downloadFinished()
if (jsonError.error != QJsonParseError::NoError) if (jsonError.error != QJsonParseError::NoError)
{ {
qDebug() << jsonError.errorString(); qDebug() << jsonError.errorString();
emit failed(m_index_within_job); emitFailed();
return; return;
} }
auto object = doc.object(); auto object = doc.object();
if (!object.value("success").toBool()) if (!object.value("success").toBool())
{ {
qDebug() << doc.toJson(); qDebug() << doc.toJson();
emit failed(m_index_within_job); emitFailed();
return; return;
} }
m_deleteHash = object.value("data").toObject().value("deletehash").toString(); m_deleteHash = object.value("data").toObject().value("deletehash").toString();
m_id = object.value("data").toObject().value("id").toString(); m_id = object.value("data").toObject().value("id").toString();
m_state = State::Succeeded; m_state = State::Succeeded;
emit succeeded(m_index_within_job); emit succeeded();
return; return;
} }
else else
{ {
qDebug() << m_reply->readAll(); qDebug() << m_reply->readAll();
m_reply.reset(); m_reply.reset();
emit failed(m_index_within_job); emitFailed();
return; return;
} }
} }
void ImgurAlbumCreation::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) void ImgurAlbumCreation::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{ {
setProgress(bytesReceived, bytesTotal); setProgress(bytesReceived, bytesTotal);
emit netActionProgress(m_index_within_job, bytesReceived, bytesTotal); emit progress(bytesReceived, bytesTotal);
} }

View File

@ -28,7 +28,7 @@ void ImgurUpload::executeTask()
QFile f(m_shot->m_file.absoluteFilePath()); QFile f(m_shot->m_file.absoluteFilePath());
if (!f.open(QFile::ReadOnly)) if (!f.open(QFile::ReadOnly))
{ {
emit failed(m_index_within_job); emitFailed();
return; return;
} }
@ -66,7 +66,7 @@ void ImgurUpload::downloadError(QNetworkReply::NetworkError error)
m_state = Task::State::Failed; m_state = Task::State::Failed;
finished = true; finished = true;
m_reply.reset(); m_reply.reset();
emit failed(m_index_within_job); emitFailed();
} }
void ImgurUpload::downloadFinished() void ImgurUpload::downloadFinished()
{ {
@ -84,7 +84,7 @@ void ImgurUpload::downloadFinished()
qDebug() << "imgur server did not reply with JSON" << jsonError.errorString(); qDebug() << "imgur server did not reply with JSON" << jsonError.errorString();
finished = true; finished = true;
m_reply.reset(); m_reply.reset();
emit failed(m_index_within_job); emitFailed();
return; return;
} }
auto object = doc.object(); auto object = doc.object();
@ -93,7 +93,7 @@ void ImgurUpload::downloadFinished()
qDebug() << "Screenshot upload not successful:" << doc.toJson(); qDebug() << "Screenshot upload not successful:" << doc.toJson();
finished = true; finished = true;
m_reply.reset(); m_reply.reset();
emit failed(m_index_within_job); emitFailed();
return; return;
} }
m_shot->m_imgurId = object.value("data").toObject().value("id").toString(); m_shot->m_imgurId = object.value("data").toObject().value("id").toString();
@ -101,11 +101,11 @@ void ImgurUpload::downloadFinished()
m_shot->m_imgurDeleteHash = object.value("data").toObject().value("deletehash").toString(); m_shot->m_imgurDeleteHash = object.value("data").toObject().value("deletehash").toString();
m_state = Task::State::Succeeded; m_state = Task::State::Succeeded;
finished = true; finished = true;
emit succeeded(m_index_within_job); emit succeeded();
return; return;
} }
void ImgurUpload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) void ImgurUpload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{ {
setProgress(bytesReceived, bytesTotal); setProgress(bytesReceived, bytesTotal);
emit netActionProgress(m_index_within_job, bytesReceived, bytesTotal); emit progress(bytesReceived, bytesTotal);
} }

View File

@ -99,8 +99,7 @@ void Task::emitAborted()
m_state = State::AbortedByUser; m_state = State::AbortedByUser;
m_failReason = "Aborted."; m_failReason = "Aborted.";
qDebug() << "Task" << describe() << "aborted."; qDebug() << "Task" << describe() << "aborted.";
emit failed(m_failReason); emit aborted();
emit finished();
} }
void Task::emitSucceeded() void Task::emitSucceeded()

View File

@ -73,6 +73,7 @@ class Task : public QObject {
virtual void progress(qint64 current, qint64 total); virtual void progress(qint64 current, qint64 total);
void finished(); void finished();
void succeeded(); void succeeded();
void aborted();
void failed(QString reason); void failed(QString reason);
void status(QString status); void status(QString status);
@ -86,7 +87,7 @@ class Task : public QObject {
protected slots: protected slots:
virtual void emitSucceeded(); virtual void emitSucceeded();
virtual void emitAborted(); virtual void emitAborted();
virtual void emitFailed(QString reason); virtual void emitFailed(QString reason = "");
public slots: public slots:
void setStatus(const QString& status); void setStatus(const QString& status);