2023-08-05 22:14:05 -04:00
|
|
|
// SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
#include "accountmanager.h"
|
2025-03-17 18:27:17 -04:00
|
|
|
#include "accountconfig.h"
|
2023-10-08 18:02:02 -04:00
|
|
|
#include "astra_log.h"
|
2023-07-30 08:49:34 -04:00
|
|
|
|
|
|
|
#include <KSharedConfig>
|
|
|
|
|
2023-12-17 12:01:28 -05:00
|
|
|
using namespace Qt::StringLiterals;
|
|
|
|
|
2024-08-22 18:53:46 -04:00
|
|
|
AccountManager::AccountManager(QObject *parent)
|
2023-07-30 08:49:34 -04:00
|
|
|
: QAbstractListModel(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void AccountManager::load()
|
|
|
|
{
|
|
|
|
auto config = KSharedConfig::openStateConfig();
|
|
|
|
for (const auto &id : config->groupList()) {
|
2023-12-17 12:01:28 -05:00
|
|
|
if (id.contains("account-"_L1)) {
|
|
|
|
const QString uuid = QString(id).remove("account-"_L1);
|
2023-10-08 18:02:02 -04:00
|
|
|
qInfo(ASTRA_LOG) << "Loading account" << uuid;
|
|
|
|
|
2024-08-22 18:53:46 -04:00
|
|
|
const auto account = new Account(uuid, this);
|
2023-07-30 10:11:14 -04:00
|
|
|
m_accounts.append(account);
|
2024-04-01 15:15:30 -04:00
|
|
|
Q_EMIT accountsChanged();
|
2024-11-09 14:56:40 -05:00
|
|
|
Q_EMIT accountAdded(account);
|
2023-07-30 08:49:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int AccountManager::rowCount(const QModelIndex &index) const
|
|
|
|
{
|
2023-12-17 10:27:07 -05:00
|
|
|
Q_UNUSED(index)
|
2023-09-17 09:02:11 -04:00
|
|
|
return static_cast<int>(m_accounts.size());
|
2023-07-30 08:49:34 -04:00
|
|
|
}
|
|
|
|
|
2024-07-04 20:53:06 -04:00
|
|
|
QVariant AccountManager::data(const QModelIndex &index, const int role) const
|
2023-07-30 08:49:34 -04:00
|
|
|
{
|
|
|
|
if (!checkIndex(index)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const int row = index.row();
|
|
|
|
if (role == AccountRole) {
|
|
|
|
return QVariant::fromValue(m_accounts[row]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray> AccountManager::roleNames() const
|
|
|
|
{
|
|
|
|
return {{AccountRole, QByteArrayLiteral("account")}};
|
|
|
|
}
|
|
|
|
|
2024-07-04 20:53:06 -04:00
|
|
|
Account *AccountManager::createSquareEnixAccount(const QString &username, const int licenseType, const bool isFreeTrial)
|
2023-07-30 08:49:34 -04:00
|
|
|
{
|
2024-08-22 18:53:46 -04:00
|
|
|
const auto account = new Account(QUuid::createUuid().toString(), this);
|
2025-03-17 18:27:17 -04:00
|
|
|
account->config()->setLicense(static_cast<Account::GameLicense>(licenseType));
|
|
|
|
account->config()->setIsFreeTrial(isFreeTrial);
|
|
|
|
account->config()->setName(username);
|
2023-07-30 08:49:34 -04:00
|
|
|
|
|
|
|
insertAccount(account);
|
|
|
|
|
|
|
|
return account;
|
|
|
|
}
|
|
|
|
|
|
|
|
Account *AccountManager::getByUuid(const QString &uuid) const
|
|
|
|
{
|
2023-09-16 21:23:58 -04:00
|
|
|
for (const auto &account : m_accounts) {
|
2023-07-30 08:49:34 -04:00
|
|
|
if (account->uuid() == uuid) {
|
|
|
|
return account;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-07-04 20:53:06 -04:00
|
|
|
bool AccountManager::canDelete(const Account *account) const
|
2023-07-30 08:49:34 -04:00
|
|
|
{
|
|
|
|
Q_UNUSED(account)
|
2024-08-22 18:57:25 -04:00
|
|
|
return m_accounts.size() > 1;
|
2023-07-30 08:49:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void AccountManager::deleteAccount(Account *account)
|
|
|
|
{
|
|
|
|
auto config = KSharedConfig::openStateConfig();
|
2023-09-16 21:23:58 -04:00
|
|
|
config->deleteGroup(QStringLiteral("account-%1").arg(account->uuid()));
|
2023-07-30 08:49:34 -04:00
|
|
|
config->sync();
|
|
|
|
|
2023-09-17 09:02:11 -04:00
|
|
|
const int row = static_cast<int>(m_accounts.indexOf(account));
|
2023-07-30 08:49:34 -04:00
|
|
|
beginRemoveRows(QModelIndex(), row, row);
|
|
|
|
m_accounts.removeAll(account);
|
|
|
|
endRemoveRows();
|
2024-04-01 15:15:30 -04:00
|
|
|
Q_EMIT accountsChanged();
|
2023-07-30 08:49:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void AccountManager::insertAccount(Account *account)
|
|
|
|
{
|
2024-08-22 18:46:45 -04:00
|
|
|
connect(account, &Account::avatarUrlChanged, this, [this, account] {
|
|
|
|
Q_EMIT accountLodestoneIdChanged(account);
|
|
|
|
});
|
2023-09-17 09:02:11 -04:00
|
|
|
beginInsertRows(QModelIndex(), static_cast<int>(m_accounts.size()), static_cast<int>(m_accounts.size()));
|
2023-07-30 08:49:34 -04:00
|
|
|
m_accounts.append(account);
|
|
|
|
endInsertRows();
|
2024-04-01 15:15:30 -04:00
|
|
|
Q_EMIT accountsChanged();
|
2024-08-22 18:46:45 -04:00
|
|
|
Q_EMIT accountAdded(account);
|
2023-07-30 08:49:34 -04:00
|
|
|
}
|
2023-10-08 20:01:17 -04:00
|
|
|
|
|
|
|
bool AccountManager::hasAnyAccounts() const
|
|
|
|
{
|
|
|
|
return !m_accounts.empty();
|
|
|
|
}
|
2023-10-08 20:24:35 -04:00
|
|
|
|
|
|
|
int AccountManager::numAccounts() const
|
|
|
|
{
|
2023-12-17 13:38:23 -05:00
|
|
|
return static_cast<int>(m_accounts.count());
|
2023-10-08 20:24:35 -04:00
|
|
|
}
|
2023-12-17 11:12:13 -05:00
|
|
|
|
2024-11-09 14:56:40 -05:00
|
|
|
#include "moc_accountmanager.cpp"
|