summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--MultiMC.cpp1
-rw-r--r--logic/auth/MojangAccount.cpp30
-rw-r--r--logic/lists/MojangAccountList.cpp81
-rw-r--r--logic/lists/MojangAccountList.h24
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 <QUuid>
+#include <logger/QsLog.h>
+
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<MojangAccountPtr> 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<MojangAccountPtr> 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:
/*!