mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-05 09:33:25 -08:00
The server list used hand-numbered saveName%1/server%1 keys and an O(n) shift loop to delete entries. Store the profiles with beginReadArray and beginWriteArray instead, and migrate the legacy keys once in the constructor so existing profiles and saved passwords survive. SettingsManager::batchWrite no longer disables atomic sync, so a batch is still written through QSettings' atomic save. Add tests for add, update, remove, password updates and the legacy migration. Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
116 lines
4.2 KiB
C++
116 lines
4.2 KiB
C++
/**
|
|
* @file servers_settings.h
|
|
* @ingroup NetworkSettings
|
|
*/
|
|
//! \todo Document this file.
|
|
|
|
#ifndef SERVERSSETTINGS_H
|
|
#define SERVERSSETTINGS_H
|
|
|
|
#include "settings_manager.h"
|
|
|
|
#include <QList>
|
|
#include <QLoggingCategory>
|
|
#include <QString>
|
|
#include <QStringList>
|
|
#include <optional>
|
|
#include <qtmetamacros.h>
|
|
|
|
class QObject;
|
|
|
|
#define SERVERSETTINGS_DEFAULT_HOST "server.cockatrice.us"
|
|
#define SERVERSETTINGS_DEFAULT_PORT "4748"
|
|
|
|
inline Q_LOGGING_CATEGORY(ServersSettingsLog, "servers_settings");
|
|
|
|
class ServersSettings : public SettingsManager
|
|
{
|
|
Q_OBJECT
|
|
friend class SettingsCache;
|
|
|
|
public:
|
|
struct ServerEntry
|
|
{
|
|
QString saveName;
|
|
QString host;
|
|
QString port;
|
|
QString username;
|
|
QString password;
|
|
bool savePassword = false;
|
|
QString site;
|
|
};
|
|
|
|
explicit ServersSettings(const QString &settingPath, QObject *parent = nullptr);
|
|
|
|
QList<ServerEntry> getServers() const;
|
|
std::optional<ServerEntry> getServer(int index) const;
|
|
|
|
int getPreviousHostLogin() const;
|
|
int getPrevioushostindex(const QString &) const;
|
|
QStringList getPreviousHostList() const;
|
|
QString getPrevioushostName() const;
|
|
QString getHostname(QString defaultHost = SERVERSETTINGS_DEFAULT_HOST) const;
|
|
QString getPort(QString defaultPort = SERVERSETTINGS_DEFAULT_PORT) const;
|
|
QString getPlayerName(QString defaultName = "") const;
|
|
QString getFPHostname(QString defaultHost = SERVERSETTINGS_DEFAULT_HOST) const;
|
|
QString getFPPort(QString defaultPort = SERVERSETTINGS_DEFAULT_PORT) const;
|
|
QString getFPPlayerName(QString defaultName = "") const;
|
|
QString getPassword();
|
|
QString getSaveName(QString defaultname = "");
|
|
QString getSite(QString defaultName = "");
|
|
bool getSavePassword() const;
|
|
int getAutoConnect() const;
|
|
|
|
void setPreviousHostLogin(int previous);
|
|
void setPrevioushostName(const QString &);
|
|
void setPreviousHostList(QStringList list);
|
|
void setAutoConnect(int autoconnect);
|
|
void setSite(QString site);
|
|
void setFPHostName(QString hostname);
|
|
void setFPPort(QString port);
|
|
void setFPPlayerName(QString playerName);
|
|
//! \brief Store a password (or a "$scrypt$..." verifier) for the given saved server.
|
|
void setServerPassword(const QString &saveName, const QString &password);
|
|
void addNewServer(const QString &saveName,
|
|
const QString &serv,
|
|
const QString &port,
|
|
const QString &username,
|
|
const QString &password,
|
|
bool savePassword,
|
|
const QString &site = QString());
|
|
void removeServer(QString servAddr);
|
|
bool updateExistingServer(QString saveName,
|
|
QString serv,
|
|
QString port,
|
|
QString username,
|
|
QString password,
|
|
bool savePassword,
|
|
QString site = QString());
|
|
void updateServerFields(int index,
|
|
const QString &saveName,
|
|
const QString &username,
|
|
const QString &password,
|
|
bool savePassword,
|
|
const QString &site);
|
|
int findServerIndex(const QString &host, const QString &port) const;
|
|
int findHostIndex(const QString &host) const;
|
|
bool hasUsername(const QString &host, const QString &port) const;
|
|
bool hasCredentials(const QString &host, const QString &port) const;
|
|
bool hasLoginData(const QString &host, const QString &port) const;
|
|
|
|
bool updateExistingServerWithoutLoss(QString saveName,
|
|
QString serv = QString(),
|
|
QString port = QString(),
|
|
QString site = QString());
|
|
void setClearDebugLogStatus(bool abIsChecked);
|
|
bool getClearDebugLogStatus(bool abDefaultValue) const;
|
|
|
|
private:
|
|
ServersSettings(const ServersSettings & /*other*/);
|
|
|
|
QList<ServerEntry> loadServers() const;
|
|
void storeServers(const QList<ServerEntry> &servers);
|
|
void migrateLegacyServers();
|
|
};
|
|
|
|
#endif // SERVERSSETTINGS_H
|