Implemented login system.

This commit is contained in:
Andrew 2013-02-05 19:22:19 -06:00
parent a416c58a93
commit e475f5d512
7 changed files with 142 additions and 22 deletions

View File

@ -17,6 +17,7 @@ include_directories(hacks)
#### Find the required Qt parts ####
find_package(Qt5Widgets)
find_package(Qt5Network)
#find_package(Qt5Declarative)
include_directories(${Qt5Widgets_INCLUDE_DIRS})
@ -171,7 +172,7 @@ QT5_WRAP_UI(MULTIMC_UI ${MULTIMC5_UIS})
QT5_ADD_RESOURCES(MULTIMC_QRC multimc.qrc)
add_executable(MultiMC ${MULTIMC_SOURCES} ${MULTIMC_HEADERS} ${MULTIMC_UI} ${MULTIMC_QRC})
qt5_use_modules(MultiMC Widgets)
qt5_use_modules(MultiMC Widgets Network)
target_link_libraries(MultiMC quazip patchlib ${MultiMC_LINK_ADDITIONAL_LIBS})
add_dependencies(MultiMC MultiMCLauncher)
install(TARGETS MultiMC RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX})

View File

@ -15,17 +15,27 @@
#include "loginresponse.h"
LoginResponse::LoginResponse(const QString& username, const QString& sessionID, QObject *parent) :
LoginResponse::LoginResponse(const QString& username, const QString& sessionID,
qint64 latestVersion, QObject *parent) :
QObject(parent)
{
this->username = username;
this->sessionID = sessionID;
this->latestVersion = latestVersion;
}
LoginResponse::LoginResponse()
{
this->username = "";
this->sessionID = "";
this->latestVersion = 0;
}
LoginResponse::LoginResponse(const LoginResponse &other)
{
this->username = other.username;
this->sessionID = other.sessionID;
this->username = other.getUsername();
this->sessionID = other.getSessionID();
this->latestVersion = other.getLatestVersion();
}
QString LoginResponse::getUsername() const
@ -47,3 +57,13 @@ void LoginResponse::setSessionID(const QString& sessionID)
{
this->sessionID = sessionID;
}
qint64 LoginResponse::getLatestVersion() const
{
return latestVersion;
}
void LoginResponse::setLatestVersion(qint64 v)
{
this->latestVersion = v;
}

View File

@ -22,7 +22,9 @@ class LoginResponse : public QObject
{
Q_OBJECT
public:
explicit LoginResponse(const QString &username, const QString &sessionID, QObject *parent = 0);
explicit LoginResponse(const QString &username, const QString &sessionID,
qint64 latestVersion, QObject *parent = 0);
LoginResponse();
LoginResponse(const LoginResponse& other);
QString getUsername() const;
@ -31,9 +33,15 @@ public:
QString getSessionID() const;
void setSessionID(const QString& sessionID);
qint64 getLatestVersion() const;
void setLatestVersion(qint64 v);
private:
QString username;
QString sessionID;
qint64 latestVersion;
};
Q_DECLARE_METATYPE(LoginResponse)
#endif // LOGINRESPONSE_H

View File

@ -142,14 +142,16 @@ void MainWindow::doLogin(const QString &errorMsg)
TaskDialog* tDialog = new TaskDialog(this);
LoginTask* loginTask = new LoginTask(uInfo, tDialog);
connect(loginTask, SIGNAL(loginComplete(LoginResponse)),
SLOT(onLoginComplete(LoginResponse)));
SLOT(onLoginComplete(LoginResponse)), Qt::QueuedConnection);
connect(loginTask, SIGNAL(loginFailed(QString)),
SLOT(doLogin(QString)));
SLOT(doLogin(QString)), Qt::QueuedConnection);
tDialog->exec(loginTask);
}
}
void MainWindow::onLoginComplete(LoginResponse response)
{
QMessageBox::information(this, "Login Successful",
QString("Logged in as %1 with session ID %2.").
arg(response.getUsername(), response.getSessionID()));
}

View File

@ -19,6 +19,7 @@
#include "data/appsettings.h"
#include "data/loginresponse.h"
int main(int argc, char *argv[])
{
@ -28,6 +29,9 @@ int main(int argc, char *argv[])
settings = new AppSettings(&app);
// Register meta types.
qRegisterMetaType<LoginResponse>("LoginResponse");
MainWindow mainWin;
mainWin.show();

View File

@ -15,6 +15,15 @@
#include "logintask.h"
#include <QStringList>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>
#include <QUrl>
#include <QUrlQuery>
LoginTask::LoginTask(const UserInfo &uInfo, QObject *parent) :
Task(parent), uInfo(uInfo)
{
@ -25,20 +34,88 @@ void LoginTask::executeTask()
{
setStatus("Logging in...");
// TODO: PLACEHOLDER
for (int p = 0; p < 100; p++)
{
msleep(25);
setProgress(p);
QNetworkAccessManager netMgr;
connect(&netMgr, SIGNAL(finished(QNetworkReply*)),
SLOT(processNetReply(QNetworkReply*)));
QUrl loginURL("https://login.minecraft.net/");
QNetworkRequest netRequest(loginURL);
netRequest.setHeader(QNetworkRequest::ContentTypeHeader,
"application/x-www-form-urlencoded");
QUrlQuery params;
params.addQueryItem("user", uInfo.getUsername());
params.addQueryItem("password", uInfo.getPassword());
params.addQueryItem("version", "13");
netReply = netMgr.post(netRequest, params.query(QUrl::EncodeSpaces).toUtf8());
exec();
}
if (uInfo.getUsername() == "test")
void LoginTask::processNetReply(QNetworkReply *reply)
{
LoginResponse response("test", "Fake Session ID");
// Check for errors.
switch (reply->error())
{
case QNetworkReply::NoError:
{
// Check the response code.
int responseCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (responseCode == 200)
{
QString responseStr(reply->readAll());
QStringList strings = responseStr.split(":");
if (strings.count() >= 4)
{
bool parseSuccess;
qint64 latestVersion = strings[0].toLongLong(&parseSuccess);
if (parseSuccess)
{
// strings[1] is the download ticket. It isn't used anymore.
QString username = strings[2];
QString sessionID = strings[3];
LoginResponse response(username, sessionID, latestVersion);
emit loginComplete(response);
}
else
{
emit loginFailed("Testing");
emit loginFailed("Failed to parse Minecraft version string.");
}
}
else
{
if (responseStr.toLower() == "bad login")
emit loginFailed("Invalid username or password.");
else if (responseStr.toLower() == "old version")
emit loginFailed("Launcher outdated, please update.");
else
emit loginFailed("Login failed: " + responseStr);
}
}
else if (responseCode == 503)
{
emit loginFailed("The login servers are currently unavailable. "
"Check http://help.mojang.com/ for more info.");
}
else
{
emit loginFailed(QString("Login failed: Unknown HTTP error %1 occurred.").
arg(QString::number(responseCode)));
}
break;
}
case QNetworkReply::OperationCanceledError:
emit loginFailed("Login canceled.");
break;
default:
emit loginFailed("Login failed: " + reply->errorString());
break;
}
quit();
}

View File

@ -21,19 +21,27 @@
#include "data/userinfo.h"
#include "data/loginresponse.h"
//class QNetworkAccessManager;
class QNetworkReply;
class LoginTask : public Task
{
Q_OBJECT
public:
explicit LoginTask(const UserInfo& uInfo, QObject *parent = 0);
public slots:
void processNetReply(QNetworkReply* reply);
signals:
void loginComplete(const LoginResponse& loginResponse);
void loginComplete(LoginResponse loginResponse);
void loginFailed(const QString& errorMsg);
protected:
void executeTask();
QNetworkReply* netReply;
UserInfo uInfo;
};