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>
54 lines
1.6 KiB
C++
54 lines
1.6 KiB
C++
/**
|
|
* @file settings_manager.h
|
|
* @ingroup Settings
|
|
*/
|
|
//! \todo Document this file.
|
|
|
|
#ifndef SETTINGSMANAGER_H
|
|
#define SETTINGSMANAGER_H
|
|
|
|
#include <QObject>
|
|
#include <QSettings>
|
|
#include <QString>
|
|
#include <QVariant>
|
|
#include <functional>
|
|
#include <qtmetamacros.h>
|
|
|
|
class SettingsManager : public QObject
|
|
{
|
|
Q_OBJECT
|
|
|
|
public:
|
|
explicit SettingsManager(const QString &settingPath,
|
|
const QString &defaultGroup = QString(),
|
|
const QString &defaultSubGroup = QString(),
|
|
QObject *parent = nullptr);
|
|
|
|
QVariant getValue(const QString &name) const;
|
|
QVariant getValue(const QString &name, const QString &group, const QString &subGroup = QString()) const;
|
|
QVariant
|
|
getValue(const QString &name, const QString &group, const QString &subGroup, const QVariant &defaultValue) const;
|
|
//! \brief Runs \a batchWriteFunction on a temporary QSettings and syncs once.
|
|
//! \return the status of the underlying file after the final sync.
|
|
QSettings::Status batchWrite(std::function<void(QSettings &)> batchWriteFunction);
|
|
|
|
void sync();
|
|
|
|
protected:
|
|
QString settingPath;
|
|
QString defaultGroup;
|
|
QString defaultSubGroup;
|
|
|
|
QSettings getSettings() const;
|
|
|
|
void setValue(const QVariant &value, const QString &name);
|
|
|
|
void
|
|
setValue(const QVariant &value, const QString &name, const QString &group, const QString &subGroup = QString());
|
|
|
|
void deleteValue(const QString &name);
|
|
|
|
void deleteValue(const QString &name, const QString &group, const QString &subGroup = QString());
|
|
};
|
|
|
|
#endif // SETTINGSMANAGER_H
|