summaryrefslogtreecommitdiffstats
path: root/logic/lists/MojangAccountList.cpp
diff options
context:
space:
mode:
authorAndrew <forkk@forkk.net>2013-11-19 12:53:30 -0600
committerAndrew <forkk@forkk.net>2013-11-19 12:53:30 -0600
commit928e0d0b151a4690a5849c2ec4a44d97338937c5 (patch)
tree817c32e38fd1492c1f3456be7764e7643a5ef433 /logic/lists/MojangAccountList.cpp
parenta9a0b65358b3799746fa9c8e1aa879e0b59ef526 (diff)
downloadMultiMC-928e0d0b151a4690a5849c2ec4a44d97338937c5.tar
MultiMC-928e0d0b151a4690a5849c2ec4a44d97338937c5.tar.gz
MultiMC-928e0d0b151a4690a5849c2ec4a44d97338937c5.tar.lz
MultiMC-928e0d0b151a4690a5849c2ec4a44d97338937c5.tar.xz
MultiMC-928e0d0b151a4690a5849c2ec4a44d97338937c5.zip
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.
Diffstat (limited to 'logic/lists/MojangAccountList.cpp')
-rw-r--r--logic/lists/MojangAccountList.cpp81
1 files changed, 78 insertions, 3 deletions
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;
+}
+