295 lines
6.3 KiB
C++
295 lines
6.3 KiB
C++
#include "storage/SecureStorage.hpp"
|
|
#include "storage/SettingsManager.hpp"
|
|
#include <QDataStream>
|
|
|
|
namespace Echoion::Desktop
|
|
{
|
|
|
|
SettingsManager::SettingsManager(QObject* parent) : QObject(parent) {}
|
|
|
|
bool SettingsManager::load()
|
|
{
|
|
bool settingsOk = loadSettings();
|
|
bool tokensOk = loadTokens();
|
|
|
|
if (settingsOk || tokensOk) {
|
|
emit settingsLoaded();
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool SettingsManager::save()
|
|
{
|
|
bool settingsOk = saveSettings();
|
|
bool tokensOk = saveTokens();
|
|
|
|
if (settingsOk && tokensOk) {
|
|
emit settingsSaved();
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool SettingsManager::loadSettings()
|
|
{
|
|
SecureStorage storage;
|
|
QString path = SecureStorage::getSettingsPath();
|
|
|
|
if (!QFile::exists(path)) {
|
|
// No settings file yet, use defaults
|
|
return true;
|
|
}
|
|
|
|
QByteArray encrypted = storage.loadFromFile(path);
|
|
if (encrypted.isEmpty()) {
|
|
return false;
|
|
}
|
|
|
|
QByteArray decrypted = storage.decrypt(encrypted);
|
|
if (decrypted.isEmpty()) {
|
|
return false;
|
|
}
|
|
|
|
QDataStream stream(decrypted);
|
|
stream >> settings_;
|
|
|
|
return stream.status() == QDataStream::Ok;
|
|
}
|
|
|
|
bool SettingsManager::saveSettings()
|
|
{
|
|
SecureStorage storage;
|
|
|
|
QByteArray data;
|
|
QDataStream stream(&data, QIODevice::WriteOnly);
|
|
stream << settings_;
|
|
|
|
if (stream.status() != QDataStream::Ok) {
|
|
return false;
|
|
}
|
|
|
|
QByteArray encrypted = storage.encrypt(data);
|
|
return storage.saveToFile(SecureStorage::getSettingsPath(), encrypted);
|
|
}
|
|
|
|
bool SettingsManager::loadTokens()
|
|
{
|
|
SecureStorage storage;
|
|
QString path = SecureStorage::getTokensPath();
|
|
|
|
if (!QFile::exists(path)) {
|
|
// No tokens file yet
|
|
return true;
|
|
}
|
|
|
|
QByteArray encrypted = storage.loadFromFile(path);
|
|
if (encrypted.isEmpty()) {
|
|
return false;
|
|
}
|
|
|
|
QByteArray decrypted = storage.decrypt(encrypted);
|
|
if (decrypted.isEmpty()) {
|
|
return false;
|
|
}
|
|
|
|
QDataStream stream(decrypted);
|
|
stream >> tokens_;
|
|
|
|
return stream.status() == QDataStream::Ok;
|
|
}
|
|
|
|
bool SettingsManager::saveTokens()
|
|
{
|
|
SecureStorage storage;
|
|
|
|
QByteArray data;
|
|
QDataStream stream(&data, QIODevice::WriteOnly);
|
|
stream << tokens_;
|
|
|
|
if (stream.status() != QDataStream::Ok) {
|
|
return false;
|
|
}
|
|
|
|
QByteArray encrypted = storage.encrypt(data);
|
|
return storage.saveToFile(SecureStorage::getTokensPath(), encrypted);
|
|
}
|
|
|
|
void SettingsManager::addProfile(const ConnectionProfile& profile)
|
|
{
|
|
// Remove existing profile with same name
|
|
removeProfile(profile.name);
|
|
|
|
settings_.profiles.append(profile);
|
|
|
|
// If this is the first profile or marked as default, update
|
|
// lastProfileName
|
|
if (settings_.profiles.size() == 1 || profile.isDefault) {
|
|
if (profile.isDefault) {
|
|
// Unset default on other profiles
|
|
for (auto& p : settings_.profiles) {
|
|
if (p.name != profile.name) {
|
|
p.isDefault = false;
|
|
}
|
|
}
|
|
}
|
|
settings_.lastProfileName = profile.name;
|
|
}
|
|
|
|
save();
|
|
emit profileAdded(profile.name);
|
|
}
|
|
|
|
void SettingsManager::removeProfile(const QString& name)
|
|
{
|
|
auto it = std::remove_if(
|
|
settings_.profiles.begin(),
|
|
settings_.profiles.end(),
|
|
[&name](const ConnectionProfile& p) { return p.name == name; });
|
|
|
|
if (it != settings_.profiles.end()) {
|
|
settings_.profiles.erase(it, settings_.profiles.end());
|
|
|
|
// Update lastProfileName if needed
|
|
if (settings_.lastProfileName == name) {
|
|
if (!settings_.profiles.isEmpty()) {
|
|
settings_.lastProfileName = settings_.profiles.first().name;
|
|
} else {
|
|
settings_.lastProfileName.clear();
|
|
}
|
|
}
|
|
|
|
save();
|
|
emit profileRemoved(name);
|
|
}
|
|
}
|
|
|
|
void SettingsManager::updateProfile(const QString& name,
|
|
const ConnectionProfile& profile)
|
|
{
|
|
removeProfile(name);
|
|
addProfile(profile);
|
|
emit profileChanged(profile.name);
|
|
}
|
|
|
|
QVector<ConnectionProfile> SettingsManager::profiles() const
|
|
{
|
|
return settings_.profiles;
|
|
}
|
|
|
|
std::optional<ConnectionProfile>
|
|
SettingsManager::getProfile(const QString& name) const
|
|
{
|
|
for (const auto& profile : settings_.profiles) {
|
|
if (profile.name == name) {
|
|
return profile;
|
|
}
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
std::optional<ConnectionProfile> SettingsManager::getDefaultProfile() const
|
|
{
|
|
// First try to find profile marked as default
|
|
for (const auto& profile : settings_.profiles) {
|
|
if (profile.isDefault) {
|
|
return profile;
|
|
}
|
|
}
|
|
|
|
// Then try last used profile
|
|
if (!settings_.lastProfileName.isEmpty()) {
|
|
auto profile = getProfile(settings_.lastProfileName);
|
|
if (profile) {
|
|
return profile;
|
|
}
|
|
}
|
|
|
|
// Finally return first profile
|
|
if (!settings_.profiles.isEmpty()) {
|
|
return settings_.profiles.first();
|
|
}
|
|
|
|
return std::nullopt;
|
|
}
|
|
|
|
void SettingsManager::setDefaultProfile(const QString& name)
|
|
{
|
|
for (auto& profile : settings_.profiles) {
|
|
profile.isDefault = (profile.name == name);
|
|
}
|
|
|
|
settings_.lastProfileName = name;
|
|
save();
|
|
}
|
|
|
|
void SettingsManager::saveToken(const QString& host, int port,
|
|
const QString& nickname,
|
|
const QString& token)
|
|
{
|
|
// Remove existing token for this host:port
|
|
deleteToken(host, port);
|
|
|
|
CachedToken ct;
|
|
ct.host = host;
|
|
ct.port = port;
|
|
ct.nickname = nickname;
|
|
ct.token = token;
|
|
ct.savedAt = QDateTime::currentDateTime();
|
|
|
|
tokens_.append(ct);
|
|
saveTokens();
|
|
|
|
emit tokenSaved(host, port);
|
|
}
|
|
|
|
std::optional<QString> SettingsManager::loadToken(
|
|
const QString& host, int port, QString& outNickname) const
|
|
{
|
|
for (const auto& ct : tokens_) {
|
|
if (ct.host == host && ct.port == port) {
|
|
outNickname = ct.nickname;
|
|
return ct.token;
|
|
}
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
void SettingsManager::deleteToken(const QString& host, int port)
|
|
{
|
|
auto it = std::remove_if(tokens_.begin(), tokens_.end(),
|
|
[&host, port](const CachedToken& ct) {
|
|
return ct.host == host && ct.port == port;
|
|
});
|
|
|
|
if (it != tokens_.end()) {
|
|
tokens_.erase(it, tokens_.end());
|
|
saveTokens();
|
|
emit tokenDeleted(host, port);
|
|
}
|
|
}
|
|
|
|
void SettingsManager::clearAllTokens()
|
|
{
|
|
tokens_.clear();
|
|
saveTokens();
|
|
}
|
|
|
|
bool SettingsManager::hasToken(const QString& host, int port) const
|
|
{
|
|
for (const auto& ct : tokens_) {
|
|
if (ct.host == host && ct.port == port) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
AppSettings& SettingsManager::settings() { return settings_; }
|
|
|
|
const AppSettings& SettingsManager::settings() const { return settings_; }
|
|
|
|
} // namespace Echoion::Desktop
|