From 52e927a6fdfc013424ef976caa44476008aee6a2 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 11 Nov 2013 12:59:23 -0600 Subject: Added vim *.swp files to .gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 0af63a46..7fb7efab 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ build resources/CMakeFiles resources/MultiMCLauncher.jar *~ +*.swp + -- cgit v1.2.3 From 962639aa6d58d88fae32159ed4e2c70ec120991e Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 11 Nov 2013 12:59:59 -0600 Subject: Added data structures for Mojang Account. --- CMakeLists.txt | 4 +++ logic/auth/MojangAccount.cpp | 55 ++++++++++++++++++++++++++++++++++ logic/auth/MojangAccount.h | 70 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 logic/auth/MojangAccount.cpp create mode 100644 logic/auth/MojangAccount.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e150c459..97ceba1b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -267,6 +267,10 @@ logic/net/LoginTask.cpp logic/net/S3ListBucket.h logic/net/S3ListBucket.cpp +# Yggdrasil login stuff +logic/auth/MojangAccount.h +logic/auth/MojangAccount.cpp + # legacy instances logic/LegacyInstance.h diff --git a/logic/auth/MojangAccount.cpp b/logic/auth/MojangAccount.cpp new file mode 100644 index 00000000..f796794e --- /dev/null +++ b/logic/auth/MojangAccount.cpp @@ -0,0 +1,55 @@ +/* Copyright 2013 MultiMC Contributors + * + * Authors: Orochimarufan + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "MojangAccount.h" + +#include + +MojangAccount::MojangAccount(const QString& username, QObject* parent) : + QObject(parent) +{ + // Generate a client token. + m_clientToken = QUuid::createUuid().toString(); + + m_username = username; +} + +MojangAccount::MojangAccount(const QString& username, const QString& clientToken, + const QString& accessToken, QObject* parent) : + QObject(parent) +{ + m_username = username; + m_clientToken = clientToken; + m_accessToken = accessToken; +} + + +QString MojangAccount::username() const +{ + return m_username; +} + +QString MojangAccount::clientToken() const +{ + return m_clientToken; +} + +QString MojangAccount::accessToken() const +{ + return m_accessToken; +} + diff --git a/logic/auth/MojangAccount.h b/logic/auth/MojangAccount.h new file mode 100644 index 00000000..b7f90659 --- /dev/null +++ b/logic/auth/MojangAccount.h @@ -0,0 +1,70 @@ +/* Copyright 2013 Andrew Okin + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include + +/** + * Object that stores information about a certain Mojang account. + * + * Said information may include things such as that account's username, client token, and access + * token if the user chose to stay logged in. + */ +class MojangAccount : public QObject +{ +Q_OBJECT +public: + /** + * Constructs a new MojangAccount with the given username. + * The client token will be generated automatically and the access token will be blank. + */ + explicit MojangAccount(const QString& username, QObject* parent = 0); + + /** + * Constructs a new MojangAccount with the given username, client token, and access token. + */ + explicit MojangAccount(const QString& username, const QString& clientToken, const QString& accessToken, QObject* parent = 0); + + + /** + * This MojangAccount's username. May be an email address if the account is migrated. + */ + QString username() const; + + /** + * This MojangAccount's client token. This is a UUID used by Mojang's auth servers to identify this client. + * This is unique for each MojangAccount. + */ + QString clientToken() const; + + /** + * This MojangAccount's access token. + * If the user has not chosen to stay logged in, this will be an empty string. + */ + QString accessToken() const; + + /** + * Changes this MojangAccount's access token to the given value. + */ + QString setAccessToken(const QString& token); + +protected: + QString m_username; + QString m_clientToken; + QString m_accessToken; // Blank if not logged in. +}; + -- cgit v1.2.3 From f6a652f3ae3e7d6109aabdb2039b5efa309e0d16 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 13 Nov 2013 12:37:54 -0600 Subject: Added YouCompleteMe config to .gitignore --- .gitignore | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.gitignore b/.gitignore index 7fb7efab..597bbbec 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,9 @@ resources/MultiMCLauncher.jar *~ *.swp +# Ctags File +tags + +# YouCompleteMe config stuff. +.ycm_extra_conf.* + -- cgit v1.2.3 From d84b1a91f494eff3c96cd961ceba9690f31e741e Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 13 Nov 2013 12:38:28 -0600 Subject: Add abstract base class for Yggdrasil tasks. --- CMakeLists.txt | 2 + logic/auth/MojangAccount.h | 2 +- logic/auth/YggdrasilTask.cpp | 170 +++++++++++++++++++++++++++++++++++++++++++ logic/auth/YggdrasilTask.h | 124 +++++++++++++++++++++++++++++++ 4 files changed, 297 insertions(+), 1 deletion(-) create mode 100644 logic/auth/YggdrasilTask.cpp create mode 100644 logic/auth/YggdrasilTask.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 97ceba1b..69c89876 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -270,6 +270,8 @@ logic/net/S3ListBucket.cpp # Yggdrasil login stuff logic/auth/MojangAccount.h logic/auth/MojangAccount.cpp +logic/auth/YggdrasilTask.h +logic/auth/YggdrasilTask.cpp # legacy instances diff --git a/logic/auth/MojangAccount.h b/logic/auth/MojangAccount.h index b7f90659..d4b8dfb1 100644 --- a/logic/auth/MojangAccount.h +++ b/logic/auth/MojangAccount.h @@ -1,4 +1,4 @@ -/* Copyright 2013 Andrew Okin +/* Copyright 2013 MultiMC Contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/logic/auth/YggdrasilTask.cpp b/logic/auth/YggdrasilTask.cpp new file mode 100644 index 00000000..0029581a --- /dev/null +++ b/logic/auth/YggdrasilTask.cpp @@ -0,0 +1,170 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include +#include +#include +#include + +#include +#include + +YggdrasilTask::YggdrasilTask(MojangAccount* account, QObject* parent) : Task(parent) +{ + m_account = account; +} + + +YggdrasilTask::~YggdrasilTask() +{ + if (m_error) + delete m_error; +} + +void YggdrasilTask::executeTask() +{ + setStatus(getStateMessage(STATE_SENDING_REQUEST)); + + // Get the content of the request we're going to send to the server. + QJsonDocument doc(getRequestContent()); + + auto worker = MMC->qnam(); + connect(worker.get(), SIGNAL(finished(QNetworkReply*)), this, + SLOT(processResponse(QNetworkReply*))); + + QUrl reqUrl("https://authserver.mojang.com/" + getEndpoint()); + QNetworkRequest netRequest(reqUrl); + netRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); + + m_netReply = worker->post(netRequest, doc.toJson()); +} + +void YggdrasilTask::processReply(QNetworkReply* reply) +{ + setStatus(getStateMessage(STATE_PROCESSING_RESPONSE)); + + if (m_netReply != reply) + // Wrong reply for some reason... + return; + + // Check for errors. + switch (reply->error()) + { + case QNetworkReply::NoError: + { + // Try to parse the response regardless of the response code. + // Sometimes the auth server will give more information and an error code. + QJsonParseError jsonError; + QJsonDocument doc = QJsonDocument::fromJson(reply->readAll(), &jsonError); + + // Check the response code. + int responseCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + switch (responseCode) + { + case 200: + { + // If the response code was 200, then there shouldn't be an error. Make sure anyways. + switch (jsonError.error) + { + case QJsonParseError::NoError: + if (!processResponse(doc.object())) + { + YggdrasilTask::Error* err = getError(); + if (err) + emitFailed(err->getErrorMessage()); + else + emitFailed(tr("An unknown error occurred when processing the response from the authentication server.")); + } + break; + + default: + emitFailed(tr("Failed to parse Yggdrasil JSON response: \"%1\".").arg(jsonError.errorString())); + break; + } + break; + } + + default: + // If the response code was something else, then Yggdrasil may have given us information about the error. + // If we can parse the response, then get information from it. Otherwise just say there was an unknown error. + switch (jsonError.error) + { + case QJsonParseError::NoError: + // We were able to parse the server's response. Woo! + // Call processError. If a subclass has overridden it then they'll handle their stuff there. + processError(doc.object()); + break; + + default: + // The server didn't say anything regarding the error. Give the user an unknown error. + emitFailed(tr("Login failed: Unknown HTTP code %1 encountered.").arg(responseCode)); + break; + } + break; + } + + break; + } + + case QNetworkReply::OperationCanceledError: + emitFailed(tr("Login canceled.")); + break; + + default: + emitFailed(tr("An unknown error occurred when trying to communicate with the authentication server.")); + break; + } +} + +QString YggdrasilTask::processError(QJsonObject responseData) +{ + QJsonValue errorVal = responseData.value("error"); + QJsonValue msgVal = responseData.value("errorMessage"); + QJsonValue causeVal = responseData.value("cause"); + + if (errorVal.isString() && msgVal.isString() && causeVal.isString()) + { + m_error = new Error(errorVal.toString(""), msgVal.toString(""), causeVal.toString("")); + return m_error->getDisplayMessage(); + } + else + { + // Error is not in standard format. Don't set m_error and return unknown error. + return tr("An unknown Yggdrasil error occurred."); + } +} + +QString YggdrasilTask::getStateMessage(const YggdrasilTask::State state) +{ + switch (state) + { + case STATE_SENDING_REQUEST: + return tr("Sending request to auth servers."); + case STATE_PROCESSING_RESPONSE: + return tr("Processing response from servers."); + default: + return tr("Processing. Please wait."); + } +} + +YggdrasilTask::Error *YggdrasilTask::getError() const +{ + return this->m_error; +} + diff --git a/logic/auth/YggdrasilTask.h b/logic/auth/YggdrasilTask.h new file mode 100644 index 00000000..05018213 --- /dev/null +++ b/logic/auth/YggdrasilTask.h @@ -0,0 +1,124 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +#include +#include + + +class MojangAccount; + +class QNetworkReply; + + +/** + * A Yggdrasil task is a task that performs an operation on a given mojang account. + */ +class YggdrasilTask : public Task +{ +Q_OBJECT +public: + explicit YggdrasilTask(MojangAccount* account, QObject* parent=0); + ~YggdrasilTask(); + +protected: + /** + * Class describing a Yggdrasil error response. + */ + class Error + { + public: + Error(const QString& shortError, const QString& errorMessage, const QString& cause) : + m_shortError(shortError), m_errorMessage(errorMessage), m_cause(cause) {} + + QString getShortError() const { return m_shortError; } + QString getErrorMessage() const { return m_errorMessage; } + QString getCause() const { return m_cause; } + + /// Gets the string to display in the GUI for describing this error. + QString getDisplayMessage() { return getErrorMessage(); } + + protected: + QString m_shortError; + QString m_errorMessage; + QString m_cause; + }; + + /** + * Enum for describing the state of the current task. + * Used by the getStateMessage function to determine what the status message should be. + */ + enum State + { + STATE_SENDING_REQUEST, + STATE_PROCESSING_RESPONSE, + STATE_OTHER, + }; + + virtual void executeTask(); + + /** + * Gets the JSON object that will be sent to the authentication server. + * Should be overridden by subclasses. + */ + virtual QJsonObject getRequestContent() const = 0; + + /** + * Gets the endpoint to POST to. + * No leading slash. + */ + virtual QString getEndpoint() const = 0; + + /** + * Processes the response received from the server. + * If an error occurred, this should emit a failed signal and return false. + * If Yggdrasil gave an error response, it should call setError() first, and then return false. + * Otherwise, it should return true. + */ + virtual bool processResponse(QJsonObject responseData) = 0; + + /** + * Processes an error response received from the server. + * The default implementation will read data from Yggdrasil's standard error response format and set it as this task's Error. + * \returns a QString error message that will be passed to emitFailed. + */ + virtual QString processError(QJsonObject responseData); + + /** + * Returns the state message for the given state. + * Used to set the status message for the task. + * Should be overridden by subclasses that want to change messages for a given state. + */ + virtual QString getStateMessage(const State state); + + /** + * Returns a pointer to a YggdrasilTask::Error object if an error has occurred. + * If no error has occurred, returns a null pointer. + */ + virtual Error* getError() const; + + MojangAccount* m_account; + + QNetworkReply* m_netReply; + + Error* m_error; + +protected slots: + void processReply(QNetworkReply* reply); +}; + -- cgit v1.2.3 From 9cfd5ae4654a128af5c0ed26d2b1fb0d3a6d96fc Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 13 Nov 2013 16:59:50 -0600 Subject: Add test authentication task. It doesn't actually do anything with the server's reply yet. --- CMakeLists.txt | 2 ++ logic/auth/AuthenticateTask.cpp | 71 +++++++++++++++++++++++++++++++++++++++++ logic/auth/AuthenticateTask.h | 44 +++++++++++++++++++++++++ logic/auth/YggdrasilTask.cpp | 5 +++ logic/auth/YggdrasilTask.h | 19 +++++++---- 5 files changed, 134 insertions(+), 7 deletions(-) create mode 100644 logic/auth/AuthenticateTask.cpp create mode 100644 logic/auth/AuthenticateTask.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 69c89876..8ff3aafe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -272,6 +272,8 @@ logic/auth/MojangAccount.h logic/auth/MojangAccount.cpp logic/auth/YggdrasilTask.h logic/auth/YggdrasilTask.cpp +logic/auth/AuthenticateTask.h +logic/auth/AuthenticateTask.cpp # legacy instances diff --git a/logic/auth/AuthenticateTask.cpp b/logic/auth/AuthenticateTask.cpp new file mode 100644 index 00000000..edfdafdb --- /dev/null +++ b/logic/auth/AuthenticateTask.cpp @@ -0,0 +1,71 @@ + +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include +#include +#include + +AuthenticateTask::AuthenticateTask(MojangAccount* account, const QString& password, QObject* parent) : + YggdrasilTask(account, parent), m_password(password) +{ +} + +QJsonObject AuthenticateTask::getRequestContent() const +{ + /* + * { + * "agent": { // optional + * "name": "Minecraft", // So far this is the only encountered value + * "version": 1 // This number might be increased + * // by the vanilla client in the future + * }, + * "username": "mojang account name", // Can be an email address or player name for + // unmigrated accounts + * "password": "mojang account password", + * "clientToken": "client identifier" // optional + * } + */ + QJsonObject req; + + { + QJsonObject agent; + // C++ makes string literals void* for some stupid reason, so we have to tell it QString... Thanks Obama. + agent.insert("name", QString("Minecraft")); + agent.insert("version", 1); + req.insert("agent", agent); + } + + req.insert("username", getMojangAccount()->username()); + req.insert("password", m_password); + req.insert("clientToken", getMojangAccount()->clientToken()); + + return req; +} + +bool AuthenticateTask::processResponse(QJsonObject responseData) +{ + qDebug() << QJsonDocument(responseData).toVariant().toString(); + return false; +} + +QString AuthenticateTask::getEndpoint() const +{ + return "authenticate"; +} + diff --git a/logic/auth/AuthenticateTask.h b/logic/auth/AuthenticateTask.h new file mode 100644 index 00000000..8c142535 --- /dev/null +++ b/logic/auth/AuthenticateTask.h @@ -0,0 +1,44 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +#include +#include +#include + +/** + * The authenticate task takes a MojangAccount with no access token and password and attempts to authenticate with Mojang's servers. + * If successful, it will set the MojangAccount's access token. + */ +class AuthenticateTask : public YggdrasilTask +{ +Q_OBJECT +public: + AuthenticateTask(MojangAccount* account, const QString& password, QObject* parent=0); + +protected: + virtual QJsonObject getRequestContent() const; + + virtual QString getEndpoint() const; + + virtual bool processResponse(QJsonObject responseData); + +private: + QString m_password; +}; + diff --git a/logic/auth/YggdrasilTask.cpp b/logic/auth/YggdrasilTask.cpp index 0029581a..efc6ec96 100644 --- a/logic/auth/YggdrasilTask.cpp +++ b/logic/auth/YggdrasilTask.cpp @@ -168,3 +168,8 @@ YggdrasilTask::Error *YggdrasilTask::getError() const return this->m_error; } +MojangAccount* YggdrasilTask::getMojangAccount() const +{ + return this->m_account; +} + diff --git a/logic/auth/YggdrasilTask.h b/logic/auth/YggdrasilTask.h index 05018213..57bbede9 100644 --- a/logic/auth/YggdrasilTask.h +++ b/logic/auth/YggdrasilTask.h @@ -36,7 +36,6 @@ public: explicit YggdrasilTask(MojangAccount* account, QObject* parent=0); ~YggdrasilTask(); -protected: /** * Class describing a Yggdrasil error response. */ @@ -59,6 +58,18 @@ protected: QString m_cause; }; + /** + * Gets the Mojang account that this task is operating on. + */ + virtual MojangAccount* getMojangAccount() const; + + /** + * Returns a pointer to a YggdrasilTask::Error object if an error has occurred. + * If no error has occurred, returns a null pointer. + */ + virtual Error* getError() const; + +protected: /** * Enum for describing the state of the current task. * Used by the getStateMessage function to determine what the status message should be. @@ -106,12 +117,6 @@ protected: */ virtual QString getStateMessage(const State state); - /** - * Returns a pointer to a YggdrasilTask::Error object if an error has occurred. - * If no error has occurred, returns a null pointer. - */ - virtual Error* getError() const; - MojangAccount* m_account; QNetworkReply* m_netReply; -- cgit v1.2.3 From ad8aeb0b2bdfd7586beab0be31bc36c64da31092 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 14 Nov 2013 14:32:43 -0600 Subject: Implement auth task's response processing. The authenticate task can now successfully log a user in. --- logic/auth/AuthenticateTask.cpp | 115 ++++++++++++++++++++++++++++++++++++++-- logic/auth/AuthenticateTask.h | 2 + logic/auth/MojangAccount.cpp | 74 ++++++++++++++++++++++++++ logic/auth/MojangAccount.h | 58 +++++++++++++++++++- logic/auth/YggdrasilTask.cpp | 13 +++-- logic/auth/YggdrasilTask.h | 2 +- 6 files changed, 256 insertions(+), 8 deletions(-) diff --git a/logic/auth/AuthenticateTask.cpp b/logic/auth/AuthenticateTask.cpp index edfdafdb..db4b0d92 100644 --- a/logic/auth/AuthenticateTask.cpp +++ b/logic/auth/AuthenticateTask.cpp @@ -17,10 +17,15 @@ #include #include + #include +#include +#include #include #include +#include "logger/QsLog.h" + AuthenticateTask::AuthenticateTask(MojangAccount* account, const QString& password, QObject* parent) : YggdrasilTask(account, parent), m_password(password) { @@ -53,15 +58,106 @@ QJsonObject AuthenticateTask::getRequestContent() const req.insert("username", getMojangAccount()->username()); req.insert("password", m_password); - req.insert("clientToken", getMojangAccount()->clientToken()); + + // If we already have a client token, give it to the server. + // Otherwise, let the server give us one. + if (!getMojangAccount()->clientToken().isEmpty()) + req.insert("clientToken", getMojangAccount()->clientToken()); return req; } bool AuthenticateTask::processResponse(QJsonObject responseData) { - qDebug() << QJsonDocument(responseData).toVariant().toString(); - return false; + // Read the response data. We need to get the client token, access token, and the selected profile. + QLOG_DEBUG() << "Processing authentication response."; + + // If we already have a client token, make sure the one the server gave us matches our existing one. + QLOG_DEBUG() << "Getting client token."; + QString clientToken = responseData.value("clientToken").toString(""); + if (clientToken.isEmpty()) + { + // Fail if the server gave us an empty client token + // TODO: Set an error properly to display to the user. + QLOG_ERROR() << "Server didn't send a client token."; + return false; + } + if (!getMojangAccount()->clientToken().isEmpty() && clientToken != getMojangAccount()->clientToken()) + { + // The server changed our client token! Obey its wishes, but complain. That's what I do for my parents, so... + QLOG_WARN() << "Server changed our client token to '" << clientToken + << "'. This shouldn't happen, but it isn't really a big deal."; + } + // Set the client token. + getMojangAccount()->setClientToken(clientToken); + + + // Now, we set the access token. + QLOG_DEBUG() << "Getting access token."; + QString accessToken = responseData.value("accessToken").toString(""); + if (accessToken.isEmpty()) + { + // Fail if the server didn't give us an access token. + // TODO: Set an error properly to display to the user. + QLOG_ERROR() << "Server didn't send an access token."; + } + // Set the access token. + getMojangAccount()->setAccessToken(accessToken); + + + // Now we load the list of available profiles. + // Mojang hasn't yet implemented the profile system, + // but we might as well support what's there so we + // don't have trouble implementing it later. + QLOG_DEBUG() << "Loading profile list."; + QJsonArray availableProfiles = responseData.value("availableProfiles").toArray(); + ProfileList loadedProfiles; + for (auto iter : availableProfiles) + { + QJsonObject profile = iter.toObject(); + // Profiles are easy, we just need their ID and name. + QString id = profile.value("id").toString(""); + QString name = profile.value("name").toString(""); + + if (id.isEmpty() || name.isEmpty()) + { + // This should never happen, but we might as well + // warn about it if it does so we can debug it easily. + // You never know when Mojang might do something truly derpy. + QLOG_WARN() << "Found entry in available profiles list with missing ID or name field. Ignoring it."; + } + + // Now, add a new AccountProfile entry to the list. + loadedProfiles.append(AccountProfile(id, name)); + } + // Put the list of profiles we loaded into the MojangAccount object. + getMojangAccount()->loadProfiles(loadedProfiles); + + + // Finally, we set the current profile to the correct value. This is pretty simple. + // We do need to make sure that the current profile that the server gave us + // is actually in the available profiles list. + // If it isn't, we'll just fail horribly (*shouldn't* ever happen, but you never know). + QLOG_DEBUG() << "Setting current profile."; + QJsonObject currentProfile = responseData.value("selectedProfile").toObject(); + QString currentProfileId = currentProfile.value("id").toString(""); + if (currentProfileId.isEmpty()) + { + // TODO: Set an error to display to the user. + QLOG_ERROR() << "Server didn't specify a currently selected profile."; + return false; + } + if (!getMojangAccount()->setProfile(currentProfileId)) + { + // TODO: Set an error to display to the user. + QLOG_ERROR() << "Server specified a selected profile that wasn't in the available profiles list."; + return false; + } + + + // We've made it through the minefield of possible errors. Return true to indicate that we've succeeded. + QLOG_DEBUG() << "Finished reading authentication response."; + return true; } QString AuthenticateTask::getEndpoint() const @@ -69,3 +165,16 @@ QString AuthenticateTask::getEndpoint() const return "authenticate"; } +QString AuthenticateTask::getStateMessage(const YggdrasilTask::State state) const +{ + switch (state) + { + case STATE_SENDING_REQUEST: + return tr("Authenticating: Sending request."); + case STATE_PROCESSING_RESPONSE: + return tr("Authenticating: Processing response."); + default: + return YggdrasilTask::getStateMessage(state); + } +} + diff --git a/logic/auth/AuthenticateTask.h b/logic/auth/AuthenticateTask.h index 8c142535..4169ad5f 100644 --- a/logic/auth/AuthenticateTask.h +++ b/logic/auth/AuthenticateTask.h @@ -37,6 +37,8 @@ protected: virtual QString getEndpoint() const; virtual bool processResponse(QJsonObject responseData); + + QString getStateMessage(const YggdrasilTask::State state) const; private: QString m_password; diff --git a/logic/auth/MojangAccount.cpp b/logic/auth/MojangAccount.cpp index f796794e..8856047e 100644 --- a/logic/auth/MojangAccount.cpp +++ b/logic/auth/MojangAccount.cpp @@ -26,6 +26,8 @@ MojangAccount::MojangAccount(const QString& username, QObject* parent) : m_clientToken = QUuid::createUuid().toString(); m_username = username; + + m_currentProfile = -1; } MojangAccount::MojangAccount(const QString& username, const QString& clientToken, @@ -35,6 +37,8 @@ MojangAccount::MojangAccount(const QString& username, const QString& clientToken m_username = username; m_clientToken = clientToken; m_accessToken = accessToken; + + m_currentProfile = -1; } @@ -48,8 +52,78 @@ QString MojangAccount::clientToken() const return m_clientToken; } +void MojangAccount::setClientToken(const QString& clientToken) +{ + m_clientToken = clientToken; +} + + QString MojangAccount::accessToken() const { return m_accessToken; } +void MojangAccount::setAccessToken(const QString& accessToken) +{ + m_accessToken = accessToken; +} + + +const QList MojangAccount::profiles() const +{ + return m_profiles; +} + +const AccountProfile* MojangAccount::currentProfile() const +{ + if (m_currentProfile < 0) + return nullptr; + else + return &m_profiles.at(m_currentProfile); +} + +bool MojangAccount::setProfile(const QString& profileId) +{ + const QList& profiles = this->profiles(); + for (int i = 0; i < profiles.length(); i++) + { + if (profiles.at(i).id() == profileId) + { + m_currentProfile = i; + return true; + } + } + return false; +} + +void MojangAccount::loadProfiles(const ProfileList& profiles) +{ + m_profiles.clear(); + for (auto profile : profiles) + m_profiles.append(profile); +} + + +AccountProfile::AccountProfile(const QString& id, const QString& name) +{ + m_id = id; + m_name = name; +} + +AccountProfile::AccountProfile(const AccountProfile& other) +{ + m_id = other.m_id; + m_name = other.m_name; +} + +QString AccountProfile::id() const +{ + return m_id; +} + +QString AccountProfile::name() const +{ + return m_name; +} + + diff --git a/logic/auth/MojangAccount.h b/logic/auth/MojangAccount.h index d4b8dfb1..c5a26736 100644 --- a/logic/auth/MojangAccount.h +++ b/logic/auth/MojangAccount.h @@ -17,6 +17,32 @@ #include #include +#include + + +/** + * Class that represents a profile within someone's Mojang account. + * + * Currently, the profile system has not been implemented by Mojang yet, + * but we might as well add some things for it in MultiMC right now so + * we don't have to rip the code to pieces to add it later. + */ +class AccountProfile +{ +public: + AccountProfile(const QString& id, const QString& name); + AccountProfile(const AccountProfile& other); + + QString id() const; + QString name() const; +protected: + QString m_id; + QString m_name; +}; + + +typedef QList ProfileList; + /** * Object that stores information about a certain Mojang account. @@ -51,6 +77,11 @@ public: */ QString clientToken() const; + /** + * Sets the MojangAccount's client token to the given value. + */ + void setClientToken(const QString& token); + /** * This MojangAccount's access token. * If the user has not chosen to stay logged in, this will be an empty string. @@ -60,11 +91,36 @@ public: /** * Changes this MojangAccount's access token to the given value. */ - QString setAccessToken(const QString& token); + void setAccessToken(const QString& token); + + /** + * Returns a list of the available account profiles. + */ + const ProfileList profiles() const; + + /** + * Returns a pointer to the currently selected profile. + * If no profile is selected, returns nullptr. + */ + const AccountProfile* currentProfile() const; + + /** + * Sets the currently selected profile to the profile with the given ID string. + * If profileId is not in the list of available profiles, the function will simply return false. + */ + bool setProfile(const QString& profileId); + + /** + * Clears the current account profile list and replaces it with the given profile list. + */ + void loadProfiles(const ProfileList& profiles); + protected: QString m_username; QString m_clientToken; QString m_accessToken; // Blank if not logged in. + int m_currentProfile; // Index of the selected profile within the list of available profiles. -1 if nothing is selected. + ProfileList m_profiles; // List of available profiles. }; diff --git a/logic/auth/YggdrasilTask.cpp b/logic/auth/YggdrasilTask.cpp index efc6ec96..a023b07e 100644 --- a/logic/auth/YggdrasilTask.cpp +++ b/logic/auth/YggdrasilTask.cpp @@ -20,12 +20,14 @@ #include #include #include +#include #include #include YggdrasilTask::YggdrasilTask(MojangAccount* account, QObject* parent) : Task(parent) { + m_error = nullptr; m_account = account; } @@ -45,7 +47,7 @@ void YggdrasilTask::executeTask() auto worker = MMC->qnam(); connect(worker.get(), SIGNAL(finished(QNetworkReply*)), this, - SLOT(processResponse(QNetworkReply*))); + SLOT(processReply(QNetworkReply*))); QUrl reqUrl("https://authserver.mojang.com/" + getEndpoint()); QNetworkRequest netRequest(reqUrl); @@ -70,7 +72,8 @@ void YggdrasilTask::processReply(QNetworkReply* reply) // Try to parse the response regardless of the response code. // Sometimes the auth server will give more information and an error code. QJsonParseError jsonError; - QJsonDocument doc = QJsonDocument::fromJson(reply->readAll(), &jsonError); + QByteArray replyData = reply->readAll(); + QJsonDocument doc = QJsonDocument::fromJson(replyData, &jsonError); // Check the response code. int responseCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); @@ -91,6 +94,10 @@ void YggdrasilTask::processReply(QNetworkReply* reply) else emitFailed(tr("An unknown error occurred when processing the response from the authentication server.")); } + else + { + emitSucceeded(); + } break; default: @@ -150,7 +157,7 @@ QString YggdrasilTask::processError(QJsonObject responseData) } } -QString YggdrasilTask::getStateMessage(const YggdrasilTask::State state) +QString YggdrasilTask::getStateMessage(const YggdrasilTask::State state) const { switch (state) { diff --git a/logic/auth/YggdrasilTask.h b/logic/auth/YggdrasilTask.h index 57bbede9..64632d44 100644 --- a/logic/auth/YggdrasilTask.h +++ b/logic/auth/YggdrasilTask.h @@ -115,7 +115,7 @@ protected: * Used to set the status message for the task. * Should be overridden by subclasses that want to change messages for a given state. */ - virtual QString getStateMessage(const State state); + virtual QString getStateMessage(const State state) const; MojangAccount* m_account; -- cgit v1.2.3 From cdca53013990ac85967394529476712e6695bbf9 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 18 Nov 2013 12:05:35 -0600 Subject: Implement account list and account list dialog --- CMakeLists.txt | 5 ++ gui/dialogs/AccountListDialog.cpp | 90 +++++++++++++++++++++++ gui/dialogs/AccountListDialog.h | 60 ++++++++++++++++ gui/dialogs/AccountListDialog.ui | 83 ++++++++++++++++++++++ logic/auth/AuthenticateTask.cpp | 2 +- logic/auth/AuthenticateTask.h | 2 +- logic/auth/MojangAccount.cpp | 10 +++ logic/auth/MojangAccount.h | 9 +++ logic/auth/YggdrasilTask.cpp | 4 +- logic/auth/YggdrasilTask.h | 9 ++- logic/lists/MojangAccountList.cpp | 146 ++++++++++++++++++++++++++++++++++++++ logic/lists/MojangAccountList.h | 109 ++++++++++++++++++++++++++++ 12 files changed, 520 insertions(+), 9 deletions(-) create mode 100644 gui/dialogs/AccountListDialog.cpp create mode 100644 gui/dialogs/AccountListDialog.h create mode 100644 gui/dialogs/AccountListDialog.ui create mode 100644 logic/lists/MojangAccountList.cpp create mode 100644 logic/lists/MojangAccountList.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 8ff3aafe..343f6ac8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -215,6 +215,8 @@ gui/dialogs/EditNotesDialog.h gui/dialogs/EditNotesDialog.cpp gui/dialogs/CustomMessageBox.h gui/dialogs/CustomMessageBox.cpp +gui/dialogs/AccountListDialog.h +gui/dialogs/AccountListDialog.cpp # GUI - widgets gui/widgets/InstanceDelegate.h @@ -323,6 +325,8 @@ logic/lists/ForgeVersionList.h logic/lists/ForgeVersionList.cpp logic/lists/JavaVersionList.h logic/lists/JavaVersionList.cpp +logic/lists/MojangAccountList.h +logic/lists/MojangAccountList.cpp # misc model/view logic/EnabledItemFilter.h @@ -363,6 +367,7 @@ gui/dialogs/IconPickerDialog.ui gui/dialogs/LegacyModEditDialog.ui gui/dialogs/OneSixModEditDialog.ui gui/dialogs/EditNotesDialog.ui +gui/dialogs/AccountListDialog.ui # Widgets/other gui/widgets/MCModInfoFrame.ui diff --git a/gui/dialogs/AccountListDialog.cpp b/gui/dialogs/AccountListDialog.cpp new file mode 100644 index 00000000..86d34de8 --- /dev/null +++ b/gui/dialogs/AccountListDialog.cpp @@ -0,0 +1,90 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "AccountListDialog.h" +#include "ui_AccountListDialog.h" + +#include + +#include + +#include +#include + +AccountListDialog::AccountListDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::AccountListDialog) +{ + ui->setupUi(this); + + ui->listView->setModel(&m_accounts); +} + +AccountListDialog::~AccountListDialog() +{ + delete ui; +} + + +void AccountListDialog::on_addAccountBtn_clicked() +{ + doLogin("Please log in to add your account."); +} + +void AccountListDialog::on_rmAccountBtn_clicked() +{ + // TODO +} + +void AccountListDialog::on_editAccountBtn_clicked() +{ + // TODO +} + +void AccountListDialog::on_closedBtnBox_rejected() +{ + close(); +} + +void AccountListDialog::doLogin(const QString& errMsg) +{ + // TODO: We can use the login dialog for this for now, but we'll have to make something better for it eventually. + LoginDialog loginDialog(this); + loginDialog.exec(); + + if (loginDialog.result() == QDialog::Accepted) + { + QString username(loginDialog.getUsername()); + QString password(loginDialog.getPassword()); + + MojangAccountPtr account = MojangAccountPtr(new MojangAccount(username)); + + ProgressDialog* progDialog = new ProgressDialog(this); + m_authTask = new AuthenticateTask(account, password, progDialog); + connect(m_authTask, SIGNAL(succeeded()), SLOT(onLoginComplete()), Qt::QueuedConnection); + connect(m_authTask, SIGNAL(failed(QString)), SLOT(doLogin(QString)), Qt::QueuedConnection); + progDialog->exec(m_authTask); + //delete m_authTask; + } +} + +void AccountListDialog::onLoginComplete() +{ + // Add the authenticated account to the accounts list. + MojangAccountPtr account = m_authTask->getMojangAccount(); + m_accounts.addAccount(account); + //ui->listView->update(); +} + diff --git a/gui/dialogs/AccountListDialog.h b/gui/dialogs/AccountListDialog.h new file mode 100644 index 00000000..442834ef --- /dev/null +++ b/gui/dialogs/AccountListDialog.h @@ -0,0 +1,60 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +#include "logic/lists/MojangAccountList.h" + +namespace Ui { +class AccountListDialog; +} + +class AuthenticateTask; + +class AccountListDialog : public QDialog +{ +Q_OBJECT +public: + explicit AccountListDialog(QWidget *parent = 0); + ~AccountListDialog(); + +public +slots: + void on_addAccountBtn_clicked(); + + void on_rmAccountBtn_clicked(); + + void on_editAccountBtn_clicked(); + + // This will be sent when the "close" button is clicked. + void on_closedBtnBox_rejected(); + +protected: + // Temporarily putting this here... + MojangAccountList m_accounts; + + AuthenticateTask* m_authTask; + +protected +slots: + void doLogin(const QString& errMsg=""); + void onLoginComplete(); + +private: + Ui::AccountListDialog *ui; +}; + diff --git a/gui/dialogs/AccountListDialog.ui b/gui/dialogs/AccountListDialog.ui new file mode 100644 index 00000000..034985a9 --- /dev/null +++ b/gui/dialogs/AccountListDialog.ui @@ -0,0 +1,83 @@ + + + AccountListDialog + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + + + <html><head/><body><p>Welcome! If you're new here, you can click the &quot;Add&quot; button to add your Mojang or Minecraft account.</p></body></html> + + + true + + + + + + + + + + + + + + &Add + + + + + + + &Edit + + + + + + + &Remove + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + QDialogButtonBox::Close + + + + + + + + diff --git a/logic/auth/AuthenticateTask.cpp b/logic/auth/AuthenticateTask.cpp index db4b0d92..a9c2c03f 100644 --- a/logic/auth/AuthenticateTask.cpp +++ b/logic/auth/AuthenticateTask.cpp @@ -26,7 +26,7 @@ #include "logger/QsLog.h" -AuthenticateTask::AuthenticateTask(MojangAccount* account, const QString& password, QObject* parent) : +AuthenticateTask::AuthenticateTask(MojangAccountPtr account, const QString& password, QObject* parent) : YggdrasilTask(account, parent), m_password(password) { } diff --git a/logic/auth/AuthenticateTask.h b/logic/auth/AuthenticateTask.h index 4169ad5f..54a6b79a 100644 --- a/logic/auth/AuthenticateTask.h +++ b/logic/auth/AuthenticateTask.h @@ -29,7 +29,7 @@ class AuthenticateTask : public YggdrasilTask { Q_OBJECT public: - AuthenticateTask(MojangAccount* account, const QString& password, QObject* parent=0); + AuthenticateTask(MojangAccountPtr account, const QString& password, QObject* parent=0); protected: virtual QJsonObject getRequestContent() const; diff --git a/logic/auth/MojangAccount.cpp b/logic/auth/MojangAccount.cpp index 8856047e..936046fb 100644 --- a/logic/auth/MojangAccount.cpp +++ b/logic/auth/MojangAccount.cpp @@ -41,6 +41,16 @@ MojangAccount::MojangAccount(const QString& username, const QString& clientToken m_currentProfile = -1; } +MojangAccount::MojangAccount(const MojangAccount& other, QObject* parent) +{ + m_username = other.username(); + m_clientToken = other.clientToken(); + m_accessToken = other.accessToken(); + + m_profiles = other.m_profiles; + m_currentProfile = other.m_currentProfile; +} + QString MojangAccount::username() const { diff --git a/logic/auth/MojangAccount.h b/logic/auth/MojangAccount.h index c5a26736..a38cb8f7 100644 --- a/logic/auth/MojangAccount.h +++ b/logic/auth/MojangAccount.h @@ -19,6 +19,7 @@ #include #include +#include /** * Class that represents a profile within someone's Mojang account. @@ -65,6 +66,11 @@ public: */ explicit MojangAccount(const QString& username, const QString& clientToken, const QString& accessToken, QObject* parent = 0); + /** + * Constructs a new MojangAccount matching the given account. + */ + MojangAccount(const MojangAccount& other, QObject* parent); + /** * This MojangAccount's username. May be an email address if the account is migrated. @@ -124,3 +130,6 @@ protected: ProfileList m_profiles; // List of available profiles. }; +typedef std::shared_ptr MojangAccountPtr; +Q_DECLARE_METATYPE(MojangAccountPtr) + diff --git a/logic/auth/YggdrasilTask.cpp b/logic/auth/YggdrasilTask.cpp index a023b07e..39dfb749 100644 --- a/logic/auth/YggdrasilTask.cpp +++ b/logic/auth/YggdrasilTask.cpp @@ -25,7 +25,7 @@ #include #include -YggdrasilTask::YggdrasilTask(MojangAccount* account, QObject* parent) : Task(parent) +YggdrasilTask::YggdrasilTask(MojangAccountPtr account, QObject* parent) : Task(parent) { m_error = nullptr; m_account = account; @@ -175,7 +175,7 @@ YggdrasilTask::Error *YggdrasilTask::getError() const return this->m_error; } -MojangAccount* YggdrasilTask::getMojangAccount() const +MojangAccountPtr YggdrasilTask::getMojangAccount() const { return this->m_account; } diff --git a/logic/auth/YggdrasilTask.h b/logic/auth/YggdrasilTask.h index 64632d44..6aebae16 100644 --- a/logic/auth/YggdrasilTask.h +++ b/logic/auth/YggdrasilTask.h @@ -20,8 +20,7 @@ #include #include - -class MojangAccount; +#include "logic/auth/MojangAccount.h" class QNetworkReply; @@ -33,7 +32,7 @@ class YggdrasilTask : public Task { Q_OBJECT public: - explicit YggdrasilTask(MojangAccount* account, QObject* parent=0); + explicit YggdrasilTask(MojangAccountPtr account, QObject* parent=0); ~YggdrasilTask(); /** @@ -61,7 +60,7 @@ public: /** * Gets the Mojang account that this task is operating on. */ - virtual MojangAccount* getMojangAccount() const; + virtual MojangAccountPtr getMojangAccount() const; /** * Returns a pointer to a YggdrasilTask::Error object if an error has occurred. @@ -117,7 +116,7 @@ protected: */ virtual QString getStateMessage(const State state) const; - MojangAccount* m_account; + MojangAccountPtr m_account; QNetworkReply* m_netReply; diff --git a/logic/lists/MojangAccountList.cpp b/logic/lists/MojangAccountList.cpp new file mode 100644 index 00000000..d309d63a --- /dev/null +++ b/logic/lists/MojangAccountList.cpp @@ -0,0 +1,146 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "logic/lists/MojangAccountList.h" +#include "logic/auth/MojangAccount.h" + +MojangAccountList::MojangAccountList(QObject *parent) : QAbstractListModel(parent) +{ +} + +MojangAccountPtr MojangAccountList::findAccount(const QString &username) +{ + for (int i = 0; i < count(); i++) + { + MojangAccountPtr account = at(i); + if (account->username() == username) + return account; + } + return MojangAccountPtr(); +} + + +const MojangAccountPtr MojangAccountList::at(int i) const +{ + return MojangAccountPtr(m_accounts.at(i)); +} + +void MojangAccountList::addAccount(const MojangAccountPtr account) +{ + beginResetModel(); + m_accounts.append(account); + endResetModel(); +} + +void MojangAccountList::removeAccount(const QString& username) +{ + beginResetModel(); + for (auto account : m_accounts) + { + if (account->username() == username) + { + m_accounts.removeOne(account); + return; + } + } + endResetModel(); +} + + +int MojangAccountList::count() const +{ + return m_accounts.count(); +} + + +QVariant MojangAccountList::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + if (index.row() > count()) + return QVariant(); + + MojangAccountPtr account = at(index.row()); + + switch (role) + { + case Qt::DisplayRole: + switch (index.column()) + { + case NameColumn: + return account->username(); + + default: + return QVariant(); + } + + case Qt::ToolTipRole: + return account->username(); + + case PointerRole: + return qVariantFromValue(account); + + default: + return QVariant(); + } +} + +QVariant MojangAccountList::headerData(int section, Qt::Orientation orientation, int role) const +{ + switch (role) + { + case Qt::DisplayRole: + switch (section) + { + case NameColumn: + return "Name"; + + default: + return QVariant(); + } + + case Qt::ToolTipRole: + switch (section) + { + case NameColumn: + return "The name of the version."; + + default: + return QVariant(); + } + + default: + return QVariant(); + } +} + +int MojangAccountList::rowCount(const QModelIndex &parent) const +{ + // Return count + return count(); +} + +int MojangAccountList::columnCount(const QModelIndex &parent) const +{ + return 1; +} + +void MojangAccountList::updateListData(QList versions) +{ + beginResetModel(); + m_accounts = versions; + endResetModel(); +} diff --git a/logic/lists/MojangAccountList.h b/logic/lists/MojangAccountList.h new file mode 100644 index 00000000..ce16d70d --- /dev/null +++ b/logic/lists/MojangAccountList.h @@ -0,0 +1,109 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include +#include +#include + +#include "logic/auth/MojangAccount.h" + + +/*! + * \brief List of available Mojang accounts. + * This should be loaded in the background by MultiMC on startup. + * + * This class also inherits from QAbstractListModel. Methods from that + * class determine how this list shows up in a list view. Said methods + * all have a default implementation, but they can be overridden by subclasses to + * change the behavior of the list. + */ +class MojangAccountList : public QAbstractListModel +{ + Q_OBJECT +public: + enum ModelRoles + { + PointerRole = 0x34B1CB48 + }; + + enum VListColumns + { + // TODO: Add icon column. + // First column - Name + NameColumn = 0, + }; + + explicit MojangAccountList(QObject *parent = 0); + + //! Gets the account at the given index. + virtual const MojangAccountPtr at(int i) const; + + //! Returns the number of accounts in the list. + virtual int count() const; + + //////// List Model Functions //////// + virtual QVariant data(const QModelIndex &index, int role) const; + virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const; + virtual int rowCount(const QModelIndex &parent) const; + virtual int columnCount(const QModelIndex &parent) const; + + /*! + * Adds a the given Mojang account to the account list. + */ + virtual void addAccount(const MojangAccountPtr account); + + /*! + * Removes the mojang account with the given username from the account list. + */ + virtual void removeAccount(const QString& username); + + /*! + * \brief Finds an account by its username. + * \param The username of the account to find. + * \return A const pointer to the account with the given username. NULL if + * one doesn't exist. + */ + virtual MojangAccountPtr findAccount(const QString &username); + +signals: + /*! + * Signal emitted to indicate that the account list has changed. + * This will also fire if the value of an element in the list changes (will be implemented later). + */ + void listChanged(); + +protected: + QList m_accounts; + +protected +slots: + /*! + * Updates this list with the given list of accounts. + * This is done by copying each account in the given list and inserting it + * into this one. + * We need to do this so that we can set the parents of the accounts are set to this + * account list. This can't be done in the load task, because the accounts the load + * task creates are on the load task's thread and Qt won't allow their parents + * to be set to something created on another thread. + * To get around that problem, we invoke this method on the GUI thread, which + * then copies the accounts and sets their parents correctly. + * \param accounts List of accounts whose parents should be set. + */ + virtual void updateListData(QList versions); +}; + -- cgit v1.2.3 From a9a0b65358b3799746fa9c8e1aa879e0b59ef526 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 18 Nov 2013 12:58:03 -0600 Subject: Implement loading accounts from list. --- MultiMC.cpp | 6 +++ MultiMC.h | 7 +++ gui/dialogs/AccountListDialog.cpp | 7 ++- gui/dialogs/AccountListDialog.h | 5 ++- logic/auth/MojangAccount.h | 20 +++++++-- logic/lists/MojangAccountList.cpp | 89 +++++++++++++++++++++++++++++++++++++++ logic/lists/MojangAccountList.h | 14 ++++++ 7 files changed, 141 insertions(+), 7 deletions(-) diff --git a/MultiMC.cpp b/MultiMC.cpp index 1c70fb54..ae1401a6 100644 --- a/MultiMC.cpp +++ b/MultiMC.cpp @@ -10,6 +10,7 @@ #include "gui/MainWindow.h" #include "gui/dialogs/VersionSelectDialog.h" #include "logic/lists/InstanceList.h" +#include "logic/lists/MojangAccountList.h" #include "logic/lists/IconList.h" #include "logic/lists/LwjglVersionList.h" #include "logic/lists/MinecraftVersionList.h" @@ -146,6 +147,11 @@ MultiMC::MultiMC(int &argc, char **argv) : QApplication(argc, argv) connect(InstDirSetting, SIGNAL(settingChanged(const Setting &, QVariant)), m_instances.get(), SLOT(on_InstFolderChanged(const Setting &, QVariant))); + // and accounts + m_accounts.reset(new MojangAccountList(this)); + QLOG_INFO() << "Loading accounts..."; + m_accounts->loadList(); + // init the http meta cache initHttpMetaCache(); diff --git a/MultiMC.h b/MultiMC.h index cd4a5f7d..dba923b1 100644 --- a/MultiMC.h +++ b/MultiMC.h @@ -13,6 +13,7 @@ class LWJGLVersionList; class HttpMetaCache; class SettingsObject; class InstanceList; +class MojangAccountList; class IconList; class QNetworkAccessManager; class ForgeVersionList; @@ -57,6 +58,11 @@ public: return m_instances; } + std::shared_ptr accounts() + { + return m_accounts; + } + std::shared_ptr icons(); Status status() @@ -101,6 +107,7 @@ private: std::shared_ptr m_mmc_translator; std::shared_ptr m_settings; std::shared_ptr m_instances; + std::shared_ptr m_accounts; std::shared_ptr m_icons; std::shared_ptr m_qnam; std::shared_ptr m_metacache; diff --git a/gui/dialogs/AccountListDialog.cpp b/gui/dialogs/AccountListDialog.cpp index 86d34de8..5a73cb18 100644 --- a/gui/dialogs/AccountListDialog.cpp +++ b/gui/dialogs/AccountListDialog.cpp @@ -23,13 +23,16 @@ #include #include +#include + AccountListDialog::AccountListDialog(QWidget *parent) : QDialog(parent), ui(new Ui::AccountListDialog) { ui->setupUi(this); - ui->listView->setModel(&m_accounts); + m_accounts = MMC->accounts(); + ui->listView->setModel(m_accounts.get()); } AccountListDialog::~AccountListDialog() @@ -84,7 +87,7 @@ void AccountListDialog::onLoginComplete() { // Add the authenticated account to the accounts list. MojangAccountPtr account = m_authTask->getMojangAccount(); - m_accounts.addAccount(account); + m_accounts->addAccount(account); //ui->listView->update(); } diff --git a/gui/dialogs/AccountListDialog.h b/gui/dialogs/AccountListDialog.h index 442834ef..57c8b460 100644 --- a/gui/dialogs/AccountListDialog.h +++ b/gui/dialogs/AccountListDialog.h @@ -17,6 +17,8 @@ #include +#include + #include "logic/lists/MojangAccountList.h" namespace Ui { @@ -44,8 +46,7 @@ slots: void on_closedBtnBox_rejected(); protected: - // Temporarily putting this here... - MojangAccountList m_accounts; + std::shared_ptr m_accounts; AuthenticateTask* m_authTask; diff --git a/logic/auth/MojangAccount.h b/logic/auth/MojangAccount.h index a38cb8f7..35261d65 100644 --- a/logic/auth/MojangAccount.h +++ b/logic/auth/MojangAccount.h @@ -18,9 +18,16 @@ #include #include #include +#include #include +class MojangAccount; + +typedef std::shared_ptr MojangAccountPtr; +Q_DECLARE_METATYPE(MojangAccountPtr) + + /** * Class that represents a profile within someone's Mojang account. * @@ -71,6 +78,16 @@ public: */ MojangAccount(const MojangAccount& other, QObject* parent); + /** + * Loads a MojangAccount from the given JSON object. + */ + static MojangAccountPtr loadFromJson(const QJsonObject& json); + + /** + * Saves a MojangAccount to a JSON object and returns it. + */ + QJsonObject saveToJson(); + /** * This MojangAccount's username. May be an email address if the account is migrated. @@ -130,6 +147,3 @@ protected: ProfileList m_profiles; // List of available profiles. }; -typedef std::shared_ptr MojangAccountPtr; -Q_DECLARE_METATYPE(MojangAccountPtr) - diff --git a/logic/lists/MojangAccountList.cpp b/logic/lists/MojangAccountList.cpp index d309d63a..40f0ea26 100644 --- a/logic/lists/MojangAccountList.cpp +++ b/logic/lists/MojangAccountList.cpp @@ -14,8 +14,23 @@ */ #include "logic/lists/MojangAccountList.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "logger/QsLog.h" + #include "logic/auth/MojangAccount.h" +#define DEFAULT_ACCOUNT_LIST_FILE "accounts.json" + +#define ACCOUNT_LIST_FORMAT_VERSION 1 + MojangAccountList::MojangAccountList(QObject *parent) : QAbstractListModel(parent) { } @@ -144,3 +159,77 @@ void MojangAccountList::updateListData(QList versions) m_accounts = versions; endResetModel(); } + +bool MojangAccountList::loadList(const QString& filePath) +{ + QString path = filePath; + if (path.isEmpty()) path = DEFAULT_ACCOUNT_LIST_FILE; + + QFile file(path); + + // Try to open the file and fail if we can't. + // TODO: We should probably report this error to the user. + if (!file.open(QIODevice::ReadOnly)) + { + QLOG_ERROR() << "Failed to read the account list file (" << path << ")."; + return false; + } + + // Read the file and close it. + QByteArray jsonData = file.readAll(); + file.close(); + + QJsonParseError parseError; + QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &parseError); + + // Fail if the JSON is invalid. + if (parseError.error != QJsonParseError::NoError) + { + QLOG_ERROR() << QString("Failed to parse account list file: %1 at offset %2") + .arg(parseError.errorString(), QString::number(parseError.offset)) + .toUtf8(); + return false; + } + + // Make sure the root is an object. + if (!jsonDoc.isObject()) + { + QLOG_ERROR() << "Invalid account list JSON: Root should be an array."; + return false; + } + + QJsonObject root = jsonDoc.object(); + + // Make sure the format version matches. + if (root.value("formatVersion").toVariant().toInt() != ACCOUNT_LIST_FORMAT_VERSION) + { + QString newName = "accountlist-old.json"; + QLOG_WARN() << "Format version mismatch when loading account list. Existing one will be renamed to \"" + << newName << "\"."; + + // Attempt to rename the old version. + file.rename(newName); + return false; + } + + // Now, load the accounts array. + beginResetModel(); + QJsonArray accounts = root.value("accounts").toArray(); + for (QJsonValue accountVal : accounts) + { + QJsonObject accountObj = accountVal.toObject(); + MojangAccountPtr account = MojangAccount::loadFromJson(accountObj); + if (account.get() != nullptr) + { + m_accounts.append(account); + } + else + { + QLOG_WARN() << "Failed to load an account."; + } + } + endResetModel(); + + return true; +} + diff --git a/logic/lists/MojangAccountList.h b/logic/lists/MojangAccountList.h index ce16d70d..37c928de 100644 --- a/logic/lists/MojangAccountList.h +++ b/logic/lists/MojangAccountList.h @@ -80,6 +80,20 @@ public: */ virtual MojangAccountPtr findAccount(const QString &username); + /*! + * \brief Loads the account list from the given file path. + * If the given file is an empty string (default), will load from the default account list file. + * \return True if successful, otherwise false. + */ + virtual bool loadList(const QString& file=""); + + /*! + * \brief Saves the account list to the given file. + * If the given file is an empty string (default), will save from the default account list file. + * \return True if successful, otherwise false. + */ + virtual bool saveList(const QString& file=""); + signals: /*! * Signal emitted to indicate that the account list has changed. -- cgit v1.2.3 From 928e0d0b151a4690a5849c2ec4a44d97338937c5 Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 19 Nov 2013 12:53:30 -0600 Subject: Implement saving account list to file Currently it only saves when accounts are added or removed. We'll have to fix this, but we need signals for when account objects change first. --- MultiMC.cpp | 1 + logic/auth/MojangAccount.cpp | 30 +++++++++++++++ logic/lists/MojangAccountList.cpp | 81 +++++++++++++++++++++++++++++++++++++-- logic/lists/MojangAccountList.h | 24 ++++++++++++ 4 files changed, 133 insertions(+), 3 deletions(-) diff --git a/MultiMC.cpp b/MultiMC.cpp index ae1401a6..9179f0e0 100644 --- a/MultiMC.cpp +++ b/MultiMC.cpp @@ -150,6 +150,7 @@ MultiMC::MultiMC(int &argc, char **argv) : QApplication(argc, argv) // and accounts m_accounts.reset(new MojangAccountList(this)); QLOG_INFO() << "Loading accounts..."; + m_accounts->setListFilePath("accounts.json", true); m_accounts->loadList(); // init the http meta cache diff --git a/logic/auth/MojangAccount.cpp b/logic/auth/MojangAccount.cpp index 936046fb..80b88082 100644 --- a/logic/auth/MojangAccount.cpp +++ b/logic/auth/MojangAccount.cpp @@ -19,6 +19,8 @@ #include +#include + MojangAccount::MojangAccount(const QString& username, QObject* parent) : QObject(parent) { @@ -113,6 +115,34 @@ void MojangAccount::loadProfiles(const ProfileList& profiles) m_profiles.append(profile); } +MojangAccountPtr MojangAccount::loadFromJson(const QJsonObject& object) +{ + // The JSON object must at least have a username for it to be valid. + if (!object.value("username").isString()) + { + QLOG_ERROR() << "Can't load Mojang account info from JSON object. Username field is missing or of the wrong type."; + return nullptr; + } + + QString username = object.value("username").toString(""); + QString clientToken = object.value("clientToken").toString(""); + QString accessToken = object.value("accessToken").toString(""); + + // TODO: Load profiles? + + return MojangAccountPtr(new MojangAccount(username, clientToken, accessToken)); +} + +QJsonObject MojangAccount::saveToJson() +{ + QJsonObject json; + json.insert("username", username()); + json.insert("clientToken", clientToken()); + json.insert("accessToken", accessToken()); + // TODO: Save profiles? + return json; +} + AccountProfile::AccountProfile(const QString& id, const QString& name) { diff --git a/logic/lists/MojangAccountList.cpp b/logic/lists/MojangAccountList.cpp index 40f0ea26..68a4da18 100644 --- a/logic/lists/MojangAccountList.cpp +++ b/logic/lists/MojangAccountList.cpp @@ -27,8 +27,6 @@ #include "logic/auth/MojangAccount.h" -#define DEFAULT_ACCOUNT_LIST_FILE "accounts.json" - #define ACCOUNT_LIST_FORMAT_VERSION 1 MojangAccountList::MojangAccountList(QObject *parent) : QAbstractListModel(parent) @@ -57,6 +55,7 @@ void MojangAccountList::addAccount(const MojangAccountPtr account) beginResetModel(); m_accounts.append(account); endResetModel(); + onListChanged(); } void MojangAccountList::removeAccount(const QString& username) @@ -71,6 +70,16 @@ void MojangAccountList::removeAccount(const QString& username) } } endResetModel(); + onListChanged(); +} + + +void MojangAccountList::onListChanged() +{ + if (m_autosave) + // TODO: Alert the user if this fails. + saveList(); + emit listChanged(); } @@ -163,7 +172,12 @@ void MojangAccountList::updateListData(QList versions) bool MojangAccountList::loadList(const QString& filePath) { QString path = filePath; - if (path.isEmpty()) path = DEFAULT_ACCOUNT_LIST_FILE; + if (path.isEmpty()) path = m_listFilePath; + if (path.isEmpty()) + { + QLOG_ERROR() << "Can't load Mojang account list. No file path given and no default set."; + return false; + } QFile file(path); @@ -233,3 +247,64 @@ bool MojangAccountList::loadList(const QString& filePath) return true; } +bool MojangAccountList::saveList(const QString& filePath) +{ + QString path(filePath); + if (path.isEmpty()) path = m_listFilePath; + if (path.isEmpty()) + { + QLOG_ERROR() << "Can't save Mojang account list. No file path given and no default set."; + return false; + } + + QLOG_INFO() << "Writing account list to \"" << path << "\"..."; + + QLOG_DEBUG() << "Building JSON data structure."; + // Build the JSON document to write to the list file. + QJsonObject root; + + root.insert("formatVersion", ACCOUNT_LIST_FORMAT_VERSION); + + // Build a list of accounts. + QLOG_DEBUG() << "Building account array."; + QJsonArray accounts; + for (MojangAccountPtr account : m_accounts) + { + QJsonObject accountObj = account->saveToJson(); + accounts.append(accountObj); + } + + // Insert the account list into the root object. + root.insert("accounts", accounts); + + // Create a JSON document object to convert our JSON to bytes. + QJsonDocument doc(root); + + + // Now that we're done building the JSON object, we can write it to the file. + QLOG_DEBUG() << "Writing account list to file."; + QFile file(path); + + // Try to open the file and fail if we can't. + // TODO: We should probably report this error to the user. + if (!file.open(QIODevice::WriteOnly)) + { + QLOG_ERROR() << "Failed to read the account list file (" << path << ")."; + return false; + } + + // Write the JSON to the file. + file.write(doc.toJson()); + file.close(); + + QLOG_INFO() << "Saved account list to \"" << path << "\"."; + + return true; +} + +void MojangAccountList::setListFilePath(QString path, bool autosave) +{ + m_listFilePath = path; + autosave = autosave; +} + diff --git a/logic/lists/MojangAccountList.h b/logic/lists/MojangAccountList.h index 37c928de..491abf6d 100644 --- a/logic/lists/MojangAccountList.h +++ b/logic/lists/MojangAccountList.h @@ -79,6 +79,15 @@ public: * one doesn't exist. */ virtual MojangAccountPtr findAccount(const QString &username); + + /*! + * Sets the default path to save the list file to. + * If autosave is true, this list will automatically save to the given path whenever it changes. + * THIS FUNCTION DOES NOT LOAD THE LIST. If you set autosave, be sure to call loadList() immediately + * after calling this function to ensure an autosaved change doesn't overwrite the list you intended + * to load. + */ + virtual void setListFilePath(QString path, bool autosave=false); /*! * \brief Loads the account list from the given file path. @@ -102,8 +111,23 @@ signals: void listChanged(); protected: + /*! + * Called whenever the list changes. + * This emits the listChanged() signal and autosaves the list (if autosave is enabled). + */ + void onListChanged(); + QList m_accounts; + //! Path to the account list file. Empty string if there isn't one. + QString m_listFilePath; + + /*! + * If true, the account list will automatically save to the account list path when it changes. + * Ignored if m_listFilePath is blank. + */ + bool m_autosave; + protected slots: /*! -- cgit v1.2.3 From 03652b01d2ec8a7c54fb39dd8ed660f0bbc2fa2a Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 20 Nov 2013 12:20:35 -0600 Subject: Add a button to open the account list. Also fix the account list dialog's close button. --- gui/MainWindow.cpp | 7 +++++++ gui/MainWindow.h | 2 ++ gui/MainWindow.ui | 9 +++++++++ gui/dialogs/AccountListDialog.cpp | 2 +- gui/dialogs/AccountListDialog.h | 2 +- 5 files changed, 20 insertions(+), 2 deletions(-) diff --git a/gui/MainWindow.cpp b/gui/MainWindow.cpp index 62ae195b..3bf248f9 100644 --- a/gui/MainWindow.cpp +++ b/gui/MainWindow.cpp @@ -57,6 +57,7 @@ #include "gui/dialogs/IconPickerDialog.h" #include "gui/dialogs/EditNotesDialog.h" #include "gui/dialogs/CopyInstanceDialog.h" +#include "gui/dialogs/AccountListDialog.h" #include "gui/ConsoleWindow.h" @@ -427,6 +428,12 @@ void MainWindow::on_actionSettings_triggered() proxymodel->sort(0); } +void MainWindow::on_actionManageAccounts_triggered() +{ + AccountListDialog dialog(this); + dialog.exec(); +} + void MainWindow::on_actionReportBug_triggered() { openWebPage(QUrl("http://multimc.myjetbrains.com/youtrack/dashboard#newissue=yes")); diff --git a/gui/MainWindow.h b/gui/MainWindow.h index 97aa0d9f..b89aab7c 100644 --- a/gui/MainWindow.h +++ b/gui/MainWindow.h @@ -79,6 +79,8 @@ slots: void on_actionSettings_triggered(); + void on_actionManageAccounts_triggered(); + void on_actionReportBug_triggered(); void on_actionNews_triggered(); diff --git a/gui/MainWindow.ui b/gui/MainWindow.ui index 6f70fc98..f76d4d4e 100644 --- a/gui/MainWindow.ui +++ b/gui/MainWindow.ui @@ -70,6 +70,7 @@ + @@ -465,6 +466,14 @@ Add a new instance. + + + Manage Accounts + + + Manage your Mojang or Minecraft accounts. + + diff --git a/gui/dialogs/AccountListDialog.cpp b/gui/dialogs/AccountListDialog.cpp index 5a73cb18..af074514 100644 --- a/gui/dialogs/AccountListDialog.cpp +++ b/gui/dialogs/AccountListDialog.cpp @@ -56,7 +56,7 @@ void AccountListDialog::on_editAccountBtn_clicked() // TODO } -void AccountListDialog::on_closedBtnBox_rejected() +void AccountListDialog::on_closeBtnBox_rejected() { close(); } diff --git a/gui/dialogs/AccountListDialog.h b/gui/dialogs/AccountListDialog.h index 57c8b460..99dee639 100644 --- a/gui/dialogs/AccountListDialog.h +++ b/gui/dialogs/AccountListDialog.h @@ -43,7 +43,7 @@ slots: void on_editAccountBtn_clicked(); // This will be sent when the "close" button is clicked. - void on_closedBtnBox_rejected(); + void on_closeBtnBox_rejected(); protected: std::shared_ptr m_accounts; -- cgit v1.2.3 From abf8408911c057d8aafe90790f5d2f5de0e1d97c Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 20 Nov 2013 18:31:15 -0600 Subject: Nuke and pave the old login system Also, account list now saves profile lists. --- gui/MainWindow.cpp | 134 ++++++++++++++++---------------------- gui/MainWindow.h | 12 ++-- logic/BaseInstance.h | 6 +- logic/InstanceLauncher.cpp | 3 + logic/LegacyInstance.cpp | 6 +- logic/LegacyInstance.h | 4 +- logic/OneSixInstance.cpp | 16 ++--- logic/OneSixInstance.h | 6 +- logic/auth/MojangAccount.cpp | 55 ++++++++++++++-- logic/auth/MojangAccount.h | 2 +- logic/lists/MojangAccountList.cpp | 14 ++-- 11 files changed, 144 insertions(+), 114 deletions(-) diff --git a/gui/MainWindow.cpp b/gui/MainWindow.cpp index 3bf248f9..734ed3b4 100644 --- a/gui/MainWindow.cpp +++ b/gui/MainWindow.cpp @@ -544,11 +544,7 @@ void MainWindow::instanceActivated(QModelIndex index) NagUtils::checkJVMArgs(inst->settings().get("JvmArgs").toString(), this); - bool autoLogin = inst->settings().get("AutoLogin").toBool(); - if (autoLogin) - doAutoLogin(); - else - doLogin(); + doLogin(); } void MainWindow::on_actionLaunchInstance_triggered() @@ -560,56 +556,34 @@ void MainWindow::on_actionLaunchInstance_triggered() } } -void MainWindow::doAutoLogin() +void MainWindow::doLogin(const QString &errorMsg) { if (!m_selectedInstance) return; - Keyring *k = Keyring::instance(); - QStringList accounts = k->getStoredAccounts("minecraft"); - - if (!accounts.isEmpty()) + // Find an account to use. + std::shared_ptr accounts = MMC->accounts(); + MojangAccountPtr account; + if (accounts->count() <= 0) { - QString username = accounts[0]; - QString password = k->getPassword("minecraft", username); - - if (!password.isEmpty()) - { - QLOG_INFO() << "Automatically logging in with stored account: " << username; - m_activeInst = m_selectedInstance; - doLogin(username, password); - } - else - { - QLOG_ERROR() << "Auto login set for account, but no password was found: " - << username; - doLogin(tr("Auto login attempted, but no password is stored.")); - } + // Tell the user they need to log in at least one account in order to play. + CustomMessageBox::selectable(this, tr("No Accounts"), + tr("In order to play Minecraft, you must have at least one Mojang or Minecraft account logged in to MultiMC. Please add an account."), + QMessageBox::Warning)->show(); } else { - QLOG_ERROR() << "Auto login set but no accounts were stored."; - doLogin(tr("Auto login attempted, but no accounts are stored.")); + // TODO: Allow user to select different accounts. + // For now, we'll just use the first one in the list until I get arround to implementing that. + account = accounts->at(0); } -} - -void MainWindow::doLogin(QString username, QString password) -{ - UserInfo uInfo{username, password}; - - ProgressDialog *tDialog = new ProgressDialog(this); - LoginTask *loginTask = new LoginTask(uInfo, tDialog); - connect(loginTask, SIGNAL(succeeded()), SLOT(onLoginComplete()), Qt::QueuedConnection); - connect(loginTask, SIGNAL(failed(QString)), SLOT(doLogin(QString)), Qt::QueuedConnection); - - tDialog->exec(loginTask); -} -void MainWindow::doLogin(const QString &errorMsg) -{ - if (!m_selectedInstance) - return; + // We'll need to validate the access token to make sure the account is still logged in. + // TODO: Do that ^ + + launchInstance(m_selectedInstance, account); + /* LoginDialog *loginDlg = new LoginDialog(this, errorMsg); if (!m_selectedInstance->lastLaunch()) loginDlg->forceOnline(); @@ -632,6 +606,41 @@ void MainWindow::doLogin(const QString &errorMsg) launchInstance(m_activeInst, m_activeLogin); } } + */ +} + +void MainWindow::launchInstance(BaseInstance *instance, MojangAccountPtr account) +{ + Q_ASSERT_X(instance != NULL, "launchInstance", "instance is NULL"); + + proc = instance->prepareForLaunch(account); + if (!proc) + return; + + // Prepare GUI: If it shall stay open disable the required parts + if (MMC->settings()->get("NoHide").toBool()) + { + ui->actionLaunchInstance->setEnabled(false); + } + else + { + this->hide(); + } + + console = new ConsoleWindow(proc); + + connect(proc, SIGNAL(log(QString, MessageLevel::Enum)), console, + SLOT(write(QString, MessageLevel::Enum))); + connect(proc, SIGNAL(ended(BaseInstance *)), this, SLOT(instanceEnded(BaseInstance *))); + + if (instance->settings().get("ShowConsole").toBool()) + { + console->show(); + } + + // I think this will work... + proc->setLogin(account->username(), account->accessToken()); + proc->launch(); } void MainWindow::onLoginComplete() @@ -644,7 +653,7 @@ void MainWindow::onLoginComplete() BaseUpdate *updateTask = m_activeInst->doUpdate(); if (!updateTask) { - launchInstance(m_activeInst, m_activeLogin); + //launchInstance(m_activeInst, m_activeLogin); } else { @@ -701,7 +710,7 @@ void MainWindow::onLoginComplete() void MainWindow::onGameUpdateComplete() { - launchInstance(m_activeInst, m_activeLogin); + //launchInstance(m_activeInst, m_activeLogin); } void MainWindow::onGameUpdateError(QString error) @@ -710,39 +719,6 @@ void MainWindow::onGameUpdateError(QString error) QMessageBox::Warning)->show(); } -void MainWindow::launchInstance(BaseInstance *instance, LoginResponse response) -{ - Q_ASSERT_X(instance != NULL, "launchInstance", "instance is NULL"); - - proc = instance->prepareForLaunch(response); - if (!proc) - return; - - // Prepare GUI: If it shall stay open disable the required parts - if (MMC->settings()->get("NoHide").toBool()) - { - ui->actionLaunchInstance->setEnabled(false); - } - else - { - this->hide(); - } - - console = new ConsoleWindow(proc); - - connect(proc, SIGNAL(log(QString, MessageLevel::Enum)), console, - SLOT(write(QString, MessageLevel::Enum))); - connect(proc, SIGNAL(ended(BaseInstance *)), this, SLOT(instanceEnded(BaseInstance *))); - - if (instance->settings().get("ShowConsole").toBool()) - { - console->show(); - } - - proc->setLogin(response.username, response.session_id); - proc->launch(); -} - void MainWindow::taskStart() { // Nothing to do here yet. diff --git a/gui/MainWindow.h b/gui/MainWindow.h index b89aab7c..c0fcc385 100644 --- a/gui/MainWindow.h +++ b/gui/MainWindow.h @@ -21,6 +21,8 @@ #include "logic/net/LoginTask.h" #include "logic/BaseInstance.h" +#include "logic/auth/MojangAccount.h" + class QToolButton; class LabeledToolButton; class QLabel; @@ -104,8 +106,12 @@ slots: void on_actionEditInstNotes_triggered(); void doLogin(const QString &errorMsg = ""); - void doLogin(QString username, QString password); - void doAutoLogin(); + + /*! + * Launches the given instance with the given account. + * This function assumes that the given account has a valid, usable access token. + */ + void launchInstance(BaseInstance* instance, MojangAccountPtr account); void onLoginComplete(); @@ -137,8 +143,6 @@ slots: void startTask(Task *task); - void launchInstance(BaseInstance *inst, LoginResponse response); - protected: bool eventFilter(QObject *obj, QEvent *ev); void setCatBackground(bool enabled); diff --git a/logic/BaseInstance.h b/logic/BaseInstance.h index b083c24a..b92d50cc 100644 --- a/logic/BaseInstance.h +++ b/logic/BaseInstance.h @@ -22,7 +22,7 @@ #include "inifile.h" #include "lists/BaseVersionList.h" -#include "net/LoginTask.h" +#include "logic/auth/MojangAccount.h" class QDialog; class BaseUpdate; @@ -153,8 +153,8 @@ public: /// returns a valid update task if update is needed, NULL otherwise virtual BaseUpdate *doUpdate() = 0; - /// returns a valid minecraft process, ready for launch - virtual MinecraftProcess *prepareForLaunch(LoginResponse response) = 0; + /// returns a valid minecraft process, ready for launch with the given account. + virtual MinecraftProcess *prepareForLaunch(MojangAccountPtr account) = 0; /// do any necessary cleanups after the instance finishes. also runs before /// 'prepareForLaunch' diff --git a/logic/InstanceLauncher.cpp b/logic/InstanceLauncher.cpp index 9f78e55b..c4df8220 100644 --- a/logic/InstanceLauncher.cpp +++ b/logic/InstanceLauncher.cpp @@ -38,6 +38,8 @@ void InstanceLauncher::onTerminated() void InstanceLauncher::onLoginComplete() { + // TODO: Fix this. + /* LoginTask *task = (LoginTask *)QObject::sender(); auto result = task->getResult(); auto instance = MMC->instances()->getInstanceById(instId); @@ -55,6 +57,7 @@ void InstanceLauncher::onLoginComplete() SLOT(write(QString, MessageLevel::Enum))); proc->launch(); + */ } void InstanceLauncher::doLogin(const QString &errorMsg) diff --git a/logic/LegacyInstance.cpp b/logic/LegacyInstance.cpp index 9a91b839..3a337140 100644 --- a/logic/LegacyInstance.cpp +++ b/logic/LegacyInstance.cpp @@ -50,7 +50,7 @@ BaseUpdate *LegacyInstance::doUpdate() return new LegacyUpdate(this, this); } -MinecraftProcess *LegacyInstance::prepareForLaunch(LoginResponse response) +MinecraftProcess *LegacyInstance::prepareForLaunch(MojangAccountPtr account) { MinecraftProcess *proc = new MinecraftProcess(this); @@ -103,8 +103,8 @@ MinecraftProcess *LegacyInstance::prepareForLaunch(LoginResponse response) #endif args << "-jar" << LAUNCHER_FILE; - args << response.player_name; - args << response.session_id; + args << account->currentProfile()->name(); + args << account->accessToken(); args << windowTitle; args << windowSize; args << lwjgl; diff --git a/logic/LegacyInstance.h b/logic/LegacyInstance.h index 8a8d4b91..e78bfd73 100644 --- a/logic/LegacyInstance.h +++ b/logic/LegacyInstance.h @@ -80,7 +80,7 @@ public: virtual void setShouldUpdate(bool val); virtual BaseUpdate *doUpdate(); - virtual MinecraftProcess *prepareForLaunch(LoginResponse response); + virtual MinecraftProcess *prepareForLaunch(MojangAccountPtr account); virtual void cleanupAfterRun(); virtual QDialog *createModEditDialog(QWidget *parent); @@ -93,4 +93,4 @@ public: protected slots: virtual void jarModsChanged(); -}; \ No newline at end of file +}; diff --git a/logic/OneSixInstance.cpp b/logic/OneSixInstance.cpp index 5c93236b..5a83bafc 100644 --- a/logic/OneSixInstance.cpp +++ b/logic/OneSixInstance.cpp @@ -65,7 +65,7 @@ QString replaceTokensIn(QString text, QMap with) return result; } -QStringList OneSixInstance::processMinecraftArgs(LoginResponse response) +QStringList OneSixInstance::processMinecraftArgs(MojangAccountPtr account) { I_D(OneSixInstance); auto version = d->version; @@ -73,11 +73,11 @@ QStringList OneSixInstance::processMinecraftArgs(LoginResponse response) QMap token_mapping; // yggdrasil! - token_mapping["auth_username"] = response.username; - token_mapping["auth_session"] = response.session_id; - token_mapping["auth_access_token"] = response.access_token; - token_mapping["auth_player_name"] = response.player_name; - token_mapping["auth_uuid"] = response.player_id; + token_mapping["auth_username"] = account->username(); + //token_mapping["auth_session"] = response.session_id; + token_mapping["auth_access_token"] = account->accessToken(); + token_mapping["auth_player_name"] = account->currentProfile()->name(); + token_mapping["auth_uuid"] = account->currentProfile()->id(); // this is for offline?: /* @@ -102,7 +102,7 @@ QStringList OneSixInstance::processMinecraftArgs(LoginResponse response) return parts; } -MinecraftProcess *OneSixInstance::prepareForLaunch(LoginResponse response) +MinecraftProcess *OneSixInstance::prepareForLaunch(MojangAccountPtr account) { I_D(OneSixInstance); cleanupAfterRun(); @@ -169,7 +169,7 @@ MinecraftProcess *OneSixInstance::prepareForLaunch(LoginResponse response) args << classPath; } args << version->mainClass; - args.append(processMinecraftArgs(response)); + args.append(processMinecraftArgs(account)); // Set the width and height for 1.6 instances bool maximize = settings().get("LaunchMaximized").toBool(); diff --git a/logic/OneSixInstance.h b/logic/OneSixInstance.h index 2d02b605..c1c742a8 100644 --- a/logic/OneSixInstance.h +++ b/logic/OneSixInstance.h @@ -40,7 +40,7 @@ public: virtual QString instanceConfigFolder() const; virtual BaseUpdate *doUpdate(); - virtual MinecraftProcess *prepareForLaunch(LoginResponse response); + virtual MinecraftProcess *prepareForLaunch(MojangAccountPtr account); virtual void cleanupAfterRun(); virtual QString intendedVersionId() const; @@ -72,5 +72,5 @@ public: virtual QString getStatusbarDescription(); private: - QStringList processMinecraftArgs(LoginResponse response); -}; \ No newline at end of file + QStringList processMinecraftArgs(MojangAccountPtr account); +}; diff --git a/logic/auth/MojangAccount.cpp b/logic/auth/MojangAccount.cpp index 80b88082..4875e5f7 100644 --- a/logic/auth/MojangAccount.cpp +++ b/logic/auth/MojangAccount.cpp @@ -18,6 +18,8 @@ #include "MojangAccount.h" #include +#include +#include #include @@ -89,7 +91,12 @@ const QList MojangAccount::profiles() const const AccountProfile* MojangAccount::currentProfile() const { if (m_currentProfile < 0) - return nullptr; + { + if (m_profiles.length() > 0) + return &m_profiles.at(0); + else + return nullptr; + } else return &m_profiles.at(m_currentProfile); } @@ -128,9 +135,36 @@ MojangAccountPtr MojangAccount::loadFromJson(const QJsonObject& object) QString clientToken = object.value("clientToken").toString(""); QString accessToken = object.value("accessToken").toString(""); - // TODO: Load profiles? + QJsonArray profileArray = object.value("profiles").toArray(); + if (profileArray.size() < 1) + { + QLOG_ERROR() << "Can't load Mojang account with username \"" << username << "\". No profiles found."; + return nullptr; + } + + ProfileList profiles; + for (QJsonValue profileVal : profileArray) + { + QJsonObject profileObject = profileVal.toObject(); + QString id = profileObject.value("id").toString(""); + QString name = profileObject.value("name").toString(""); + if (id.isEmpty() || name.isEmpty()) + { + QLOG_WARN() << "Unable to load a profile because it was missing an ID or a name."; + continue; + } + profiles.append(AccountProfile(id, name)); + } + + MojangAccountPtr account(new MojangAccount(username, clientToken, accessToken)); + account->loadProfiles(profiles); - return MojangAccountPtr(new MojangAccount(username, clientToken, accessToken)); + // Get the currently selected profile. + QString currentProfile = object.value("activeProfile").toString(""); + if (!currentProfile.isEmpty()) + account->setProfile(currentProfile); + + return account; } QJsonObject MojangAccount::saveToJson() @@ -139,7 +173,20 @@ QJsonObject MojangAccount::saveToJson() json.insert("username", username()); json.insert("clientToken", clientToken()); json.insert("accessToken", accessToken()); - // TODO: Save profiles? + + QJsonArray profileArray; + for (AccountProfile profile : m_profiles) + { + QJsonObject profileObj; + profileObj.insert("id", profile.id()); + profileObj.insert("name", profile.name()); + profileArray.append(profileObj); + } + json.insert("profiles", profileArray); + + if (currentProfile() != nullptr) + json.insert("activeProfile", currentProfile()->id()); + return json; } diff --git a/logic/auth/MojangAccount.h b/logic/auth/MojangAccount.h index 35261d65..e5684b77 100644 --- a/logic/auth/MojangAccount.h +++ b/logic/auth/MojangAccount.h @@ -123,7 +123,7 @@ public: /** * Returns a pointer to the currently selected profile. - * If no profile is selected, returns nullptr. + * If no profile is selected, returns the first profile in the profile list or nullptr if there are none. */ const AccountProfile* currentProfile() const; diff --git a/logic/lists/MojangAccountList.cpp b/logic/lists/MojangAccountList.cpp index 68a4da18..32317f84 100644 --- a/logic/lists/MojangAccountList.cpp +++ b/logic/lists/MojangAccountList.cpp @@ -185,7 +185,7 @@ bool MojangAccountList::loadList(const QString& filePath) // TODO: We should probably report this error to the user. if (!file.open(QIODevice::ReadOnly)) { - QLOG_ERROR() << "Failed to read the account list file (" << path << ")."; + QLOG_ERROR() << QString("Failed to read the account list file (%1).").arg(path).toUtf8(); return false; } @@ -217,9 +217,9 @@ bool MojangAccountList::loadList(const QString& filePath) // Make sure the format version matches. if (root.value("formatVersion").toVariant().toInt() != ACCOUNT_LIST_FORMAT_VERSION) { - QString newName = "accountlist-old.json"; - QLOG_WARN() << "Format version mismatch when loading account list. Existing one will be renamed to \"" - << newName << "\"."; + QString newName = "accounts-old.json"; + QLOG_WARN() << "Format version mismatch when loading account list. Existing one will be renamed to" + << newName; // Attempt to rename the old version. file.rename(newName); @@ -257,7 +257,7 @@ bool MojangAccountList::saveList(const QString& filePath) return false; } - QLOG_INFO() << "Writing account list to \"" << path << "\"..."; + QLOG_INFO() << "Writing account list to" << path; QLOG_DEBUG() << "Building JSON data structure."; // Build the JSON document to write to the list file. @@ -289,7 +289,7 @@ bool MojangAccountList::saveList(const QString& filePath) // TODO: We should probably report this error to the user. if (!file.open(QIODevice::WriteOnly)) { - QLOG_ERROR() << "Failed to read the account list file (" << path << ")."; + QLOG_ERROR() << QString("Failed to read the account list file (%1).").arg(path).toUtf8(); return false; } @@ -297,7 +297,7 @@ bool MojangAccountList::saveList(const QString& filePath) file.write(doc.toJson()); file.close(); - QLOG_INFO() << "Saved account list to \"" << path << "\"."; + QLOG_INFO() << "Saved account list to" << path; return true; } -- cgit v1.2.3 From a332e0d7b15a05d7a611624ef59c0604e74b5ae4 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 22 Nov 2013 09:53:27 -0600 Subject: Fix crashing when there are no accounts added The warning message actually displays now when there are no Mojang accounts to log in with, rather than simply crashing MultiMC. --- gui/MainWindow.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gui/MainWindow.cpp b/gui/MainWindow.cpp index 734ed3b4..3279661f 100644 --- a/gui/MainWindow.cpp +++ b/gui/MainWindow.cpp @@ -569,7 +569,8 @@ void MainWindow::doLogin(const QString &errorMsg) // Tell the user they need to log in at least one account in order to play. CustomMessageBox::selectable(this, tr("No Accounts"), tr("In order to play Minecraft, you must have at least one Mojang or Minecraft account logged in to MultiMC. Please add an account."), - QMessageBox::Warning)->show(); + QMessageBox::Information)->exec(); + return; } else { @@ -612,6 +613,7 @@ void MainWindow::doLogin(const QString &errorMsg) void MainWindow::launchInstance(BaseInstance *instance, MojangAccountPtr account) { Q_ASSERT_X(instance != NULL, "launchInstance", "instance is NULL"); + Q_ASSERT_X(account.get() != nullptr, "launchInstance", "account is NULL"); proc = instance->prepareForLaunch(account); if (!proc) -- cgit v1.2.3 From 69ac3e5a86d2a5602abc6e74904f29475b99a63c Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 22 Nov 2013 10:12:16 -0600 Subject: Tweak the "no accounts" warning a bit. It now asks users if they want to open the manage accounts dialog to add their accounts. --- gui/MainWindow.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/gui/MainWindow.cpp b/gui/MainWindow.cpp index 3279661f..9824d52f 100644 --- a/gui/MainWindow.cpp +++ b/gui/MainWindow.cpp @@ -567,9 +567,16 @@ void MainWindow::doLogin(const QString &errorMsg) if (accounts->count() <= 0) { // Tell the user they need to log in at least one account in order to play. - CustomMessageBox::selectable(this, tr("No Accounts"), - tr("In order to play Minecraft, you must have at least one Mojang or Minecraft account logged in to MultiMC. Please add an account."), - QMessageBox::Information)->exec(); + auto reply = CustomMessageBox::selectable(this, tr("No Accounts"), + tr("In order to play Minecraft, you must have at least one Mojang or Minecraft account logged in to MultiMC." + "Would you like to open the account manager to add an account now?"), + QMessageBox::Information, QMessageBox::Yes | QMessageBox::No)->exec(); + + if (reply == QMessageBox::Yes) + { + // Open the account manager. + on_actionManageAccounts_triggered(); + } return; } else -- cgit v1.2.3 From 23bc195b3c8558cb997789ca8772342612716993 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 22 Nov 2013 10:54:52 -0600 Subject: Implement removing accounts. --- gui/dialogs/AccountListDialog.cpp | 9 ++++++++- logic/lists/MojangAccountList.cpp | 8 ++++++++ logic/lists/MojangAccountList.h | 5 +++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/gui/dialogs/AccountListDialog.cpp b/gui/dialogs/AccountListDialog.cpp index af074514..838bef7f 100644 --- a/gui/dialogs/AccountListDialog.cpp +++ b/gui/dialogs/AccountListDialog.cpp @@ -16,6 +16,8 @@ #include "AccountListDialog.h" #include "ui_AccountListDialog.h" +#include + #include #include @@ -48,7 +50,12 @@ void AccountListDialog::on_addAccountBtn_clicked() void AccountListDialog::on_rmAccountBtn_clicked() { - // TODO + QModelIndexList selection = ui->listView->selectionModel()->selectedIndexes(); + if (selection.size() > 0) + { + QModelIndex selected = selection.first(); + m_accounts->removeAccount(selected); + } } void AccountListDialog::on_editAccountBtn_clicked() diff --git a/logic/lists/MojangAccountList.cpp b/logic/lists/MojangAccountList.cpp index 32317f84..442ef3af 100644 --- a/logic/lists/MojangAccountList.cpp +++ b/logic/lists/MojangAccountList.cpp @@ -73,6 +73,14 @@ void MojangAccountList::removeAccount(const QString& username) onListChanged(); } +void MojangAccountList::removeAccount(QModelIndex index) +{ + beginResetModel(); + m_accounts.removeAt(index.row()); + endResetModel(); + onListChanged(); +} + void MojangAccountList::onListChanged() { diff --git a/logic/lists/MojangAccountList.h b/logic/lists/MojangAccountList.h index 491abf6d..bccc2f9a 100644 --- a/logic/lists/MojangAccountList.h +++ b/logic/lists/MojangAccountList.h @@ -72,6 +72,11 @@ public: */ virtual void removeAccount(const QString& username); + /*! + * Removes the account at the given QModelIndex. + */ + virtual void removeAccount(QModelIndex index); + /*! * \brief Finds an account by its username. * \param The username of the account to find. -- cgit v1.2.3 From 75e7932607bdd84d2867765eb6f07dcec95ee193 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 22 Nov 2013 12:47:39 -0600 Subject: Properly implement launching and downloading Also added a system to select an active account to log in with. --- gui/MainWindow.cpp | 147 +++++++++++++++----------------------- gui/MainWindow.h | 12 ++-- gui/dialogs/AccountListDialog.cpp | 12 ++++ gui/dialogs/AccountListDialog.h | 2 + gui/dialogs/AccountListDialog.ui | 12 +++- logic/lists/MojangAccountList.cpp | 37 +++++++++- logic/lists/MojangAccountList.h | 29 +++++++- 7 files changed, 148 insertions(+), 103 deletions(-) diff --git a/gui/MainWindow.cpp b/gui/MainWindow.cpp index 9824d52f..7ea67764 100644 --- a/gui/MainWindow.cpp +++ b/gui/MainWindow.cpp @@ -85,11 +85,6 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi ui->setupUi(this); setWindowTitle(QString("MultiMC %1").arg(MMC->version().toString())); - // Set the selected instance to null - m_selectedInstance = nullptr; - // Set active instance to null. - m_activeInst = nullptr; - // OSX magic. setUnifiedTitleAndToolBarOnMac(true); @@ -563,7 +558,7 @@ void MainWindow::doLogin(const QString &errorMsg) // Find an account to use. std::shared_ptr accounts = MMC->accounts(); - MojangAccountPtr account; + MojangAccountPtr account = accounts->activeAccount(); if (accounts->count() <= 0) { // Tell the user they need to log in at least one account in order to play. @@ -577,107 +572,53 @@ void MainWindow::doLogin(const QString &errorMsg) // Open the account manager. on_actionManageAccounts_triggered(); } - return; } - else + else if (account.get() == nullptr) { - // TODO: Allow user to select different accounts. - // For now, we'll just use the first one in the list until I get arround to implementing that. - account = accounts->at(0); - } - - // We'll need to validate the access token to make sure the account is still logged in. - // TODO: Do that ^ - - launchInstance(m_selectedInstance, account); - - /* - LoginDialog *loginDlg = new LoginDialog(this, errorMsg); - if (!m_selectedInstance->lastLaunch()) - loginDlg->forceOnline(); + // Tell the user they need to log in at least one account in order to play. + auto reply = CustomMessageBox::selectable(this, tr("No Account Selected"), + tr("You don't have an account selected as an active account." + "Would you like to open the account manager to select one now?"), + QMessageBox::Information, QMessageBox::Yes | QMessageBox::No)->exec(); - loginDlg->exec(); - if (loginDlg->result() == QDialog::Accepted) - { - if (loginDlg->isOnline()) - { - m_activeInst = m_selectedInstance; - doLogin(loginDlg->getUsername(), loginDlg->getPassword()); - } - else + if (reply == QMessageBox::Yes) { - QString user = loginDlg->getUsername(); - if (user.length() == 0) - user = QString("Player"); - m_activeLogin = {user, QString("Offline"), user, QString()}; - m_activeInst = m_selectedInstance; - launchInstance(m_activeInst, m_activeLogin); + // Open the account manager. + on_actionManageAccounts_triggered(); } } - */ -} - -void MainWindow::launchInstance(BaseInstance *instance, MojangAccountPtr account) -{ - Q_ASSERT_X(instance != NULL, "launchInstance", "instance is NULL"); - Q_ASSERT_X(account.get() != nullptr, "launchInstance", "account is NULL"); - - proc = instance->prepareForLaunch(account); - if (!proc) - return; - - // Prepare GUI: If it shall stay open disable the required parts - if (MMC->settings()->get("NoHide").toBool()) - { - ui->actionLaunchInstance->setEnabled(false); - } else { - this->hide(); + // We'll need to validate the access token to make sure the account is still logged in. + // TODO: Do that ^ + + prepareLaunch(m_selectedInstance, account); } - - console = new ConsoleWindow(proc); - - connect(proc, SIGNAL(log(QString, MessageLevel::Enum)), console, - SLOT(write(QString, MessageLevel::Enum))); - connect(proc, SIGNAL(ended(BaseInstance *)), this, SLOT(instanceEnded(BaseInstance *))); - - if (instance->settings().get("ShowConsole").toBool()) - { - console->show(); - } - - // I think this will work... - proc->setLogin(account->username(), account->accessToken()); - proc->launch(); } -void MainWindow::onLoginComplete() +void MainWindow::prepareLaunch(BaseInstance* instance, MojangAccountPtr account) { - if (!m_activeInst) - return; - LoginTask *task = (LoginTask *)QObject::sender(); - m_activeLogin = task->getResult(); - - BaseUpdate *updateTask = m_activeInst->doUpdate(); + BaseUpdate *updateTask = instance->doUpdate(); if (!updateTask) { - //launchInstance(m_activeInst, m_activeLogin); + launchInstance(instance, account); } else { ProgressDialog tDialog(this); - connect(updateTask, SIGNAL(succeeded()), SLOT(onGameUpdateComplete())); + connect(updateTask, &BaseUpdate::succeeded, [this, instance, account] { launchInstance(instance, account); }); connect(updateTask, SIGNAL(failed(QString)), SLOT(onGameUpdateError(QString))); tDialog.exec(updateTask); delete updateTask; } - auto job = new NetJob("Player skin: " + m_activeLogin.player_name); + QString playerName = account->currentProfile()->name(); + + auto job = new NetJob("Player skin: " + playerName); - auto meta = MMC->metacache()->resolveEntry("skins", m_activeLogin.player_name + ".png"); + auto meta = MMC->metacache()->resolveEntry("skins", playerName + ".png"); auto action = CacheDownload::make( - QUrl("http://skins.minecraft.net/MinecraftSkins/" + m_activeLogin.player_name + ".png"), + QUrl("http://skins.minecraft.net/MinecraftSkins/" + playerName + ".png"), meta); job->addNetAction(action); meta->stale = true; @@ -702,12 +643,12 @@ void MainWindow::onLoginComplete() QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError); QJsonObject root = jsonDoc.object(); QJsonObject mappings = root.value("mappings").toObject(); - QJsonArray usernames = mappings.value(m_activeLogin.username).toArray(); + QJsonArray usernames = mappings.value(account->username()).toArray(); - if (!usernames.contains(m_activeLogin.player_name)) + if (!usernames.contains(playerName)) { - usernames.prepend(m_activeLogin.player_name); - mappings[m_activeLogin.username] = usernames; + usernames.prepend(playerName); + mappings[account->username()] = usernames; root["mappings"] = mappings; jsonDoc.setObject(root); @@ -717,9 +658,39 @@ void MainWindow::onLoginComplete() } } -void MainWindow::onGameUpdateComplete() +void MainWindow::launchInstance(BaseInstance *instance, MojangAccountPtr account) { - //launchInstance(m_activeInst, m_activeLogin); + Q_ASSERT_X(instance != NULL, "launchInstance", "instance is NULL"); + Q_ASSERT_X(account.get() != nullptr, "launchInstance", "account is NULL"); + + proc = instance->prepareForLaunch(account); + if (!proc) + return; + + // Prepare GUI: If it shall stay open disable the required parts + if (MMC->settings()->get("NoHide").toBool()) + { + ui->actionLaunchInstance->setEnabled(false); + } + else + { + this->hide(); + } + + console = new ConsoleWindow(proc); + + connect(proc, SIGNAL(log(QString, MessageLevel::Enum)), console, + SLOT(write(QString, MessageLevel::Enum))); + connect(proc, SIGNAL(ended(BaseInstance *)), this, SLOT(instanceEnded(BaseInstance *))); + + if (instance->settings().get("ShowConsole").toBool()) + { + console->show(); + } + + // I think this will work... + proc->setLogin(account->username(), account->accessToken()); + proc->launch(); } void MainWindow::onGameUpdateError(QString error) diff --git a/gui/MainWindow.h b/gui/MainWindow.h index c0fcc385..f88905bf 100644 --- a/gui/MainWindow.h +++ b/gui/MainWindow.h @@ -113,9 +113,11 @@ slots: */ void launchInstance(BaseInstance* instance, MojangAccountPtr account); - void onLoginComplete(); + /*! + * Prepares the given instance for launch with the given account. + */ + void prepareLaunch(BaseInstance* instance, MojangAccountPtr account); - void onGameUpdateComplete(); void onGameUpdateError(QString error); void taskStart(); @@ -160,12 +162,6 @@ private: BaseInstance *m_selectedInstance; - // A pointer to the instance we are actively doing stuff with. - // This is set when the user launches an instance and is used to refer to that - // instance throughout the launching process. - BaseInstance *m_activeInst; - LoginResponse m_activeLogin; - Task *m_versionLoadTask; QLabel *m_statusLeft; diff --git a/gui/dialogs/AccountListDialog.cpp b/gui/dialogs/AccountListDialog.cpp index 838bef7f..c685c164 100644 --- a/gui/dialogs/AccountListDialog.cpp +++ b/gui/dialogs/AccountListDialog.cpp @@ -34,6 +34,7 @@ AccountListDialog::AccountListDialog(QWidget *parent) : ui->setupUi(this); m_accounts = MMC->accounts(); + // TODO: Make the "Active?" column show checkboxes or radio buttons. ui->listView->setModel(m_accounts.get()); } @@ -63,6 +64,17 @@ void AccountListDialog::on_editAccountBtn_clicked() // TODO } +void AccountListDialog::on_setActiveBtn_clicked() +{ + QModelIndexList selection = ui->listView->selectionModel()->selectedIndexes(); + if (selection.size() > 0) + { + QModelIndex selected = selection.first(); + MojangAccountPtr account = selected.data(MojangAccountList::PointerRole).value(); + m_accounts->setActiveAccount(account->username()); + } +} + void AccountListDialog::on_closeBtnBox_rejected() { close(); diff --git a/gui/dialogs/AccountListDialog.h b/gui/dialogs/AccountListDialog.h index 99dee639..17a50bec 100644 --- a/gui/dialogs/AccountListDialog.h +++ b/gui/dialogs/AccountListDialog.h @@ -42,6 +42,8 @@ slots: void on_editAccountBtn_clicked(); + void on_setActiveBtn_clicked(); + // This will be sent when the "close" button is clicked. void on_closeBtnBox_rejected(); diff --git a/gui/dialogs/AccountListDialog.ui b/gui/dialogs/AccountListDialog.ui index 034985a9..2872b368 100644 --- a/gui/dialogs/AccountListDialog.ui +++ b/gui/dialogs/AccountListDialog.ui @@ -27,7 +27,7 @@ - + @@ -65,6 +65,16 @@ + + + + <html><head/><body><p>Set the currently selected account as the active account. The active account is the account that is used to log in (unless it is overridden in an instance-specific setting).</p></body></html> + + + &Set Active + + + diff --git a/logic/lists/MojangAccountList.cpp b/logic/lists/MojangAccountList.cpp index 442ef3af..1d67c70f 100644 --- a/logic/lists/MojangAccountList.cpp +++ b/logic/lists/MojangAccountList.cpp @@ -33,7 +33,7 @@ MojangAccountList::MojangAccountList(QObject *parent) : QAbstractListModel(paren { } -MojangAccountPtr MojangAccountList::findAccount(const QString &username) +MojangAccountPtr MojangAccountList::findAccount(const QString &username) const { for (int i = 0; i < count(); i++) { @@ -41,7 +41,7 @@ MojangAccountPtr MojangAccountList::findAccount(const QString &username) if (account->username() == username) return account; } - return MojangAccountPtr(); + return nullptr; } @@ -82,6 +82,25 @@ void MojangAccountList::removeAccount(QModelIndex index) } +MojangAccountPtr MojangAccountList::activeAccount() const +{ + if (m_activeAccount.isEmpty()) + return nullptr; + else + return findAccount(m_activeAccount); +} + +void MojangAccountList::setActiveAccount(const QString& username) +{ + beginResetModel(); + for (MojangAccountPtr account : m_accounts) + if (account->username() == username) + m_activeAccount = username; + endResetModel(); + onListChanged(); +} + + void MojangAccountList::onListChanged() { if (m_autosave) @@ -112,6 +131,9 @@ QVariant MojangAccountList::data(const QModelIndex &index, int role) const case Qt::DisplayRole: switch (index.column()) { + case ActiveColumn: + return account->username() == m_activeAccount; + case NameColumn: return account->username(); @@ -137,6 +159,9 @@ QVariant MojangAccountList::headerData(int section, Qt::Orientation orientation, case Qt::DisplayRole: switch (section) { + case ActiveColumn: + return "Active?"; + case NameColumn: return "Name"; @@ -167,7 +192,7 @@ int MojangAccountList::rowCount(const QModelIndex &parent) const int MojangAccountList::columnCount(const QModelIndex &parent) const { - return 1; + return 2; } void MojangAccountList::updateListData(QList versions) @@ -251,6 +276,9 @@ bool MojangAccountList::loadList(const QString& filePath) } } endResetModel(); + + // Load the active account. + m_activeAccount = root.value("activeAccount").toString(""); return true; } @@ -285,6 +313,9 @@ bool MojangAccountList::saveList(const QString& filePath) // Insert the account list into the root object. root.insert("accounts", accounts); + // Save the active account. + root.insert("activeAccount", m_activeAccount); + // Create a JSON document object to convert our JSON to bytes. QJsonDocument doc(root); diff --git a/logic/lists/MojangAccountList.h b/logic/lists/MojangAccountList.h index bccc2f9a..71f472f7 100644 --- a/logic/lists/MojangAccountList.h +++ b/logic/lists/MojangAccountList.h @@ -44,8 +44,12 @@ public: enum VListColumns { // TODO: Add icon column. - // First column - Name - NameColumn = 0, + + // First column - Active? + ActiveColumn = 0, + + // Second column - Name + NameColumn, }; explicit MojangAccountList(QObject *parent = 0); @@ -83,7 +87,7 @@ public: * \return A const pointer to the account with the given username. NULL if * one doesn't exist. */ - virtual MojangAccountPtr findAccount(const QString &username); + virtual MojangAccountPtr findAccount(const QString &username) const; /*! * Sets the default path to save the list file to. @@ -108,6 +112,19 @@ public: */ virtual bool saveList(const QString& file=""); + /*! + * \brief Gets a pointer to the account that the user has selected as their "active" account. + * Which account is active can be overridden on a per-instance basis, but this will return the one that + * is set as active globally. + * \return The currently active MojangAccount. If there isn't an active account, returns a null pointer. + */ + virtual MojangAccountPtr activeAccount() const; + + /*! + * Sets the given account as the current active account. + */ + virtual void setActiveAccount(const QString& username); + signals: /*! * Signal emitted to indicate that the account list has changed. @@ -124,6 +141,12 @@ protected: QList m_accounts; + /*! + * Username of the account that is currently active. + * Empty string if no account is active. + */ + QString m_activeAccount; + //! Path to the account list file. Empty string if there isn't one. QString m_listFilePath; -- cgit v1.2.3