diff options
author | Sky <git@bunnies.cc> | 2013-10-17 00:02:56 +0100 |
---|---|---|
committer | Sky <git@bunnies.cc> | 2013-10-17 00:02:56 +0100 |
commit | c700b7be2e6e5632f6cea6f0c45744f494a0629f (patch) | |
tree | e434b8773bf11aa3eba84ff2f3886a4e4dbf2004 /logic/net | |
parent | 71d5cf91a86bd268550a12732aebac7570cfae92 (diff) | |
download | MultiMC-c700b7be2e6e5632f6cea6f0c45744f494a0629f.tar MultiMC-c700b7be2e6e5632f6cea6f0c45744f494a0629f.tar.gz MultiMC-c700b7be2e6e5632f6cea6f0c45744f494a0629f.tar.lz MultiMC-c700b7be2e6e5632f6cea6f0c45744f494a0629f.tar.xz MultiMC-c700b7be2e6e5632f6cea6f0c45744f494a0629f.zip |
Un-copy-pasta the login response handler using std::function magic
Diffstat (limited to 'logic/net')
-rw-r--r-- | logic/net/LoginTask.cpp | 145 | ||||
-rw-r--r-- | logic/net/LoginTask.h | 2 |
2 files changed, 63 insertions, 84 deletions
diff --git a/logic/net/LoginTask.cpp b/logic/net/LoginTask.cpp index 62d6331f..70ef2ae6 100644 --- a/logic/net/LoginTask.cpp +++ b/logic/net/LoginTask.cpp @@ -56,45 +56,6 @@ void LoginTask::legacyLogin() netReply = worker->post(netRequest, params.query(QUrl::EncodeSpaces).toUtf8()); } -void LoginTask::processLegacyReply(QNetworkReply *reply) -{ - if (netReply != reply) - return; - // Check for errors. - switch (reply->error()) - { - case QNetworkReply::NoError: - { - // Check the response code. - int responseCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - - if (responseCode == 200) - { - parseLegacyReply(reply->readAll()); - } - else if (responseCode == 503) - { - emitFailed(tr("The login servers are currently unavailable. Check " - "http://help.mojang.com/ for more info.")); - } - else - { - emitFailed(tr("Login failed: Unknown HTTP error %1 occurred.") - .arg(QString::number(responseCode))); - } - break; - } - - case QNetworkReply::OperationCanceledError: - emitFailed(tr("Login canceled.")); - break; - - default: - emitFailed(tr("Login failed: %1").arg(reply->errorString())); - break; - } -} - void LoginTask::parseLegacyReply(QByteArray data) { QString responseStr = QString::fromUtf8(data); @@ -129,6 +90,67 @@ void LoginTask::parseLegacyReply(QByteArray data) } } +void LoginTask::processLegacyReply(QNetworkReply *reply) +{ + processReply(reply, &LoginTask::parseLegacyReply); +} + +void LoginTask::processYggdrasilReply(QNetworkReply *reply) +{ + processReply(reply, &LoginTask::parseYggdrasilReply); +} + +void LoginTask::processReply(QNetworkReply *reply, std::function<void (LoginTask*, QByteArray)> parser) +{ + if (netReply != reply) + return; + // Check for errors. + switch (reply->error()) + { + case QNetworkReply::NoError: + { + // Check the response code. + int responseCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + switch (responseCode) + { + case 200: + parser(this, reply->readAll()); + break; + + default: + emitFailed(tr("Login failed: Unknown HTTP code %1 encountered.").arg(responseCode)); + break; + } + + break; + } + + case QNetworkReply::OperationCanceledError: + emitFailed(tr("Login canceled.")); + break; + + default: + int responseCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + switch (responseCode) + { + case 403: + emitFailed(tr("Invalid username or password.")); + break; + + case 503: + emitFailed(tr("The login servers are currently unavailable. Check " + "http://help.mojang.com/ for more info.")); + break; + + default: + QLOG_DEBUG() << "Login failed with QNetworkReply code:" << reply->error(); + emitFailed(tr("Login failed: %1").arg(reply->errorString())); + break; + } + } +} void LoginTask::yggdrasilLogin() { @@ -168,51 +190,6 @@ void LoginTask::yggdrasilLogin() netReply = worker->post(netRequest, requestConstent.toUtf8()); } -void LoginTask::processYggdrasilReply(QNetworkReply *reply) -{ - if (netReply != reply) - return; - // Check for errors. - switch (reply->error()) - { - case QNetworkReply::NoError: - { - // Check the response code. - int responseCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - - if (responseCode == 200) - { - parseYggdrasilReply(reply->readAll()); - } - else if (responseCode == 503) - { - emitFailed(tr("The login servers are currently unavailable. Check " - "http://help.mojang.com/ for more info.")); - } - else - { - emitFailed(tr("Login failed: Unknown HTTP error %1 occurred.") - .arg(QString::number(responseCode))); - } - break; - } - - case QNetworkReply::OperationCanceledError: - emitFailed(tr("Login canceled.")); - break; - - // Equivalent to an HTTP 403 - case QNetworkReply::ContentOperationNotPermittedError: - emitFailed(tr("Invalid username or password.")); - break; - - default: - QLOG_DEBUG() << "Login failed with QNetworkReply code:" << reply->error(); - emitFailed(tr("Login failed: %1").arg(reply->errorString())); - break; - } -} - /* { "accessToken": "random access token", // hexadecimal diff --git a/logic/net/LoginTask.h b/logic/net/LoginTask.h index daea18af..fa5897cb 100644 --- a/logic/net/LoginTask.h +++ b/logic/net/LoginTask.h @@ -54,6 +54,8 @@ protected slots: void processYggdrasilReply(QNetworkReply *reply); void parseYggdrasilReply(QByteArray data); + void processReply(QNetworkReply *reply, std::function<void(LoginTask*, QByteArray)>); + protected: void executeTask(); |