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,43 +60,51 @@ 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)
{ {
QByteArray data = m_reply->readAll(); qCritical() << "Double finished ImgurUpload!";
m_reply.reset();
QJsonParseError jsonError;
QJsonDocument doc = QJsonDocument::fromJson(data, &jsonError);
if (jsonError.error != QJsonParseError::NoError)
{
qDebug() << jsonError.errorString();
emit failed(m_index_within_job);
return;
}
auto object = doc.object();
if (!object.value("success").toBool())
{
qDebug() << doc.toJson();
emit failed(m_index_within_job);
return;
}
m_shot->m_imgurId = object.value("data").toObject().value("id").toString();
m_shot->m_url = object.value("data").toObject().value("link").toString();
m_status = Job_Finished;
emit succeeded(m_index_within_job);
return; return;
} }
else QByteArray data = m_reply->readAll();
m_reply.reset();
QJsonParseError jsonError;
QJsonDocument doc = QJsonDocument::fromJson(data, &jsonError);
if (jsonError.error != QJsonParseError::NoError)
{ {
qDebug() << m_reply->readAll(); qDebug() << "imgur server did not reply with JSON" << jsonError.errorString();
finished = true;
m_reply.reset(); m_reply.reset();
emit failed(m_index_within_job); emit failed(m_index_within_job);
return; return;
} }
auto object = doc.object();
if (!object.value("success").toBool())
{
qDebug() << "Screenshot upload not successful:" << doc.toJson();
finished = true;
m_reply.reset();
emit failed(m_index_within_job);
return;
}
m_shot->m_imgurId = object.value("data").toObject().value("id").toString();
m_shot->m_url = object.value("data").toObject().value("link").toString();
m_status = Job_Finished;
finished = true;
emit succeeded(m_index_within_job);
return;
} }
void ImgurUpload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) void ImgurUpload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{ {

View File

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