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. --- logic/auth/MojangAccount.cpp | 55 ++++++++++++++++++++++++++++++++++++++++---- logic/auth/MojangAccount.h | 2 +- 2 files changed, 52 insertions(+), 5 deletions(-) (limited to 'logic/auth') 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; -- cgit v1.2.3