Implement loading accounts from list.

This commit is contained in:
Andrew 2013-11-18 12:58:03 -06:00
parent cdca530139
commit a9a0b65358
7 changed files with 141 additions and 7 deletions

View File

@ -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();

View File

@ -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<MojangAccountList> accounts()
{
return m_accounts;
}
std::shared_ptr<IconList> icons();
Status status()
@ -101,6 +107,7 @@ private:
std::shared_ptr<QTranslator> m_mmc_translator;
std::shared_ptr<SettingsObject> m_settings;
std::shared_ptr<InstanceList> m_instances;
std::shared_ptr<MojangAccountList> m_accounts;
std::shared_ptr<IconList> m_icons;
std::shared_ptr<QNetworkAccessManager> m_qnam;
std::shared_ptr<HttpMetaCache> m_metacache;

View File

@ -23,13 +23,16 @@
#include <gui/dialogs/LoginDialog.h>
#include <gui/dialogs/ProgressDialog.h>
#include <MultiMC.h>
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();
}

View File

@ -17,6 +17,8 @@
#include <QDialog>
#include <memory>
#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<MojangAccountList> m_accounts;
AuthenticateTask* m_authTask;

View File

@ -18,9 +18,16 @@
#include <QObject>
#include <QString>
#include <QList>
#include <QJsonObject>
#include <memory>
class MojangAccount;
typedef std::shared_ptr<MojangAccount> 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<MojangAccount> MojangAccountPtr;
Q_DECLARE_METATYPE(MojangAccountPtr)

View File

@ -14,8 +14,23 @@
*/
#include "logic/lists/MojangAccountList.h"
#include <QIODevice>
#include <QFile>
#include <QTextStream>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonParseError>
#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<MojangAccountPtr> 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;
}

View File

@ -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.