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

View File

@ -43,7 +43,7 @@
class NetAction : public Task {
Q_OBJECT
protected:
explicit NetAction() : Task(nullptr) {};
explicit NetAction() : Task() {};
public:
using Ptr = shared_qobject_ptr<NetAction>;
@ -51,13 +51,7 @@ class NetAction : public Task {
virtual ~NetAction() = default;
QUrl url() { return m_url; }
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);
auto index() -> int { return m_index_within_job; }
protected slots:
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());
if (action->isRunning()) {
connect(action.get(), &NetAction::succeeded, this, &NetJob::partSucceeded);
connect(action.get(), &NetAction::failed, this, &NetJob::partFailed);
connect(action.get(), &NetAction::netActionProgress, this, &NetJob::partProgress);
connect(action.get(), &NetAction::succeeded, [this, action]{ partSucceeded(action->index()); });
connect(action.get(), &NetAction::failed, [this, action](QString){ partFailed(action->index()); });
connect(action.get(), &NetAction::progress, [this, action](qint64 done, qint64 total) { partProgress(action->index(), done, total); });
} else {
m_todo.append(m_parts_progress.size() - 1);
}
@ -218,10 +218,10 @@ void NetJob::startMoreParts()
auto part = m_downloads[doThis];
// connect signals :D
connect(part.get(), &NetAction::succeeded, this, &NetJob::partSucceeded);
connect(part.get(), &NetAction::failed, this, &NetJob::partFailed);
connect(part.get(), &NetAction::aborted, this, &NetJob::partAborted);
connect(part.get(), &NetAction::netActionProgress, this, &NetJob::partProgress);
connect(part.get(), &NetAction::succeeded, this, [this, part]{ partSucceeded(part->index()); });
connect(part.get(), &NetAction::failed, this, [this, part](QString){ partFailed(part->index()); });
connect(part.get(), &NetAction::aborted, this, [this, part]{ partAborted(part->index()); });
connect(part.get(), &NetAction::progress, this, [this, part](qint64 done, qint64 total) { partProgress(part->index(), done, total); });
part->startAction(m_network);
}

View File

@ -56,32 +56,32 @@ void ImgurAlbumCreation::downloadFinished()
if (jsonError.error != QJsonParseError::NoError)
{
qDebug() << jsonError.errorString();
emit failed(m_index_within_job);
emitFailed();
return;
}
auto object = doc.object();
if (!object.value("success").toBool())
{
qDebug() << doc.toJson();
emit failed(m_index_within_job);
emitFailed();
return;
}
m_deleteHash = object.value("data").toObject().value("deletehash").toString();
m_id = object.value("data").toObject().value("id").toString();
m_state = State::Succeeded;
emit succeeded(m_index_within_job);
emit succeeded();
return;
}
else
{
qDebug() << m_reply->readAll();
m_reply.reset();
emit failed(m_index_within_job);
emitFailed();
return;
}
}
void ImgurAlbumCreation::downloadProgress(qint64 bytesReceived, qint64 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());
if (!f.open(QFile::ReadOnly))
{
emit failed(m_index_within_job);
emitFailed();
return;
}
@ -66,7 +66,7 @@ void ImgurUpload::downloadError(QNetworkReply::NetworkError error)
m_state = Task::State::Failed;
finished = true;
m_reply.reset();
emit failed(m_index_within_job);
emitFailed();
}
void ImgurUpload::downloadFinished()
{
@ -84,7 +84,7 @@ void ImgurUpload::downloadFinished()
qDebug() << "imgur server did not reply with JSON" << jsonError.errorString();
finished = true;
m_reply.reset();
emit failed(m_index_within_job);
emitFailed();
return;
}
auto object = doc.object();
@ -93,7 +93,7 @@ void ImgurUpload::downloadFinished()
qDebug() << "Screenshot upload not successful:" << doc.toJson();
finished = true;
m_reply.reset();
emit failed(m_index_within_job);
emitFailed();
return;
}
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_state = Task::State::Succeeded;
finished = true;
emit succeeded(m_index_within_job);
emit succeeded();
return;
}
void ImgurUpload::downloadProgress(qint64 bytesReceived, qint64 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_failReason = "Aborted.";
qDebug() << "Task" << describe() << "aborted.";
emit failed(m_failReason);
emit finished();
emit aborted();
}
void Task::emitSucceeded()

View File

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