GH-849 Fix failure signals not getting delivered from NetJob properly

This commit is contained in:
Petr Mrázek 2015-04-05 16:06:15 +02:00
parent f623dc54ef
commit d5c79db12c
4 changed files with 38 additions and 34 deletions

View File

@ -85,13 +85,13 @@ void NetJob::startMoreParts()
{ {
if(!m_failed.size()) if(!m_failed.size())
{ {
qDebug() << m_job_name.toLocal8Bit() << "succeeded."; qDebug() << m_job_name << "succeeded.";
emit succeeded(); emit succeeded();
} }
else else
{ {
qCritical() << m_job_name.toLocal8Bit() << "failed."; qCritical() << m_job_name << "failed.";
emit failed(); emit failed(tr("%1 failed").arg(m_job_name));
} }
} }
return; return;

View File

@ -86,12 +86,6 @@ public:
private slots: private slots:
void startMoreParts(); void startMoreParts();
signals:
void started();
void progress(qint64 current, qint64 total);
void succeeded();
void failed();
public slots: public slots:
virtual void start(); virtual void start();
// FIXME: implement // FIXME: implement

View File

@ -20,6 +20,7 @@ ImgurUpload::ImgurUpload(ScreenshotPtr shot) : NetAction(), m_shot(shot)
void ImgurUpload::start() void ImgurUpload::start()
{ {
finished = false;
m_status = Job_InProgress; m_status = Job_InProgress;
QNetworkRequest request(m_url); QNetworkRequest request(m_url);
request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)"); request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)");
@ -59,44 +60,52 @@ void ImgurUpload::start()
} }
void ImgurUpload::downloadError(QNetworkReply::NetworkError error) void ImgurUpload::downloadError(QNetworkReply::NetworkError error)
{ {
qDebug() << m_reply->errorString(); qCritical() << "ImgurUpload failed with error" << m_reply->errorString() << "Server reply:\n" << m_reply->readAll();
if(finished)
{
qCritical() << "Double finished ImgurUpload!";
return;
}
m_status = Job_Failed; m_status = Job_Failed;
finished = true;
m_reply.reset();
emit failed(m_index_within_job);
} }
void ImgurUpload::downloadFinished() void ImgurUpload::downloadFinished()
{ {
if (m_status != Job_Failed) if(finished)
{ {
qCritical() << "Double finished ImgurUpload!";
return;
}
QByteArray data = m_reply->readAll(); QByteArray data = m_reply->readAll();
m_reply.reset(); m_reply.reset();
QJsonParseError jsonError; QJsonParseError jsonError;
QJsonDocument doc = QJsonDocument::fromJson(data, &jsonError); QJsonDocument doc = QJsonDocument::fromJson(data, &jsonError);
if (jsonError.error != QJsonParseError::NoError) if (jsonError.error != QJsonParseError::NoError)
{ {
qDebug() << jsonError.errorString(); qDebug() << "imgur server did not reply with JSON" << jsonError.errorString();
finished = true;
m_reply.reset();
emit failed(m_index_within_job); emit failed(m_index_within_job);
return; return;
} }
auto object = doc.object(); auto object = doc.object();
if (!object.value("success").toBool()) if (!object.value("success").toBool())
{ {
qDebug() << doc.toJson(); qDebug() << "Screenshot upload not successful:" << doc.toJson();
finished = true;
m_reply.reset();
emit failed(m_index_within_job); emit failed(m_index_within_job);
return; return;
} }
m_shot->m_imgurId = object.value("data").toObject().value("id").toString(); m_shot->m_imgurId = object.value("data").toObject().value("id").toString();
m_shot->m_url = object.value("data").toObject().value("link").toString(); m_shot->m_url = object.value("data").toObject().value("link").toString();
m_status = Job_Finished; m_status = Job_Finished;
finished = true;
emit succeeded(m_index_within_job); emit succeeded(m_index_within_job);
return; return;
} }
else
{
qDebug() << m_reply->readAll();
m_reply.reset();
emit failed(m_index_within_job);
return;
}
}
void ImgurUpload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) void ImgurUpload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{ {
m_total_progress = bytesTotal; m_total_progress = bytesTotal;

View File

@ -27,4 +27,5 @@ slots:
private: private:
ScreenshotPtr m_shot; ScreenshotPtr m_shot;
bool finished = true;
}; };