169 lines
5.3 KiB
C
169 lines
5.3 KiB
C
|
|
#ifndef CONFIGURATION_MANAGER_H
|
||
|
|
#define CONFIGURATION_MANAGER_H
|
||
|
|
|
||
|
|
#include <QObject>
|
||
|
|
#include <QSettings>
|
||
|
|
#include <QFile>
|
||
|
|
#include <QDir>
|
||
|
|
#include <QMutex>
|
||
|
|
#include <QTimer>
|
||
|
|
#include <QJsonObject>
|
||
|
|
#include <QJsonDocument>
|
||
|
|
#include <QJsonArray>
|
||
|
|
#include "../common_structures/TradingStructures.h"
|
||
|
|
|
||
|
|
// Configuration scope
|
||
|
|
enum class ConfigScope {
|
||
|
|
System, // System-wide configuration
|
||
|
|
User, // User-specific configuration
|
||
|
|
Session, // Current session only
|
||
|
|
Temporary // Temporary configuration
|
||
|
|
};
|
||
|
|
|
||
|
|
// Configuration change type
|
||
|
|
enum class ConfigChangeType {
|
||
|
|
Added,
|
||
|
|
Modified,
|
||
|
|
Removed,
|
||
|
|
Reset
|
||
|
|
};
|
||
|
|
|
||
|
|
// Configuration entry
|
||
|
|
struct ConfigEntry {
|
||
|
|
QString key;
|
||
|
|
QVariant value;
|
||
|
|
ConfigScope scope;
|
||
|
|
QDateTime lastModified;
|
||
|
|
QString description;
|
||
|
|
QVariant defaultValue;
|
||
|
|
bool isModified = false;
|
||
|
|
|
||
|
|
ConfigEntry() = default;
|
||
|
|
ConfigEntry(const QString& k, const QVariant& v, ConfigScope s = ConfigScope::System)
|
||
|
|
: key(k), value(v), scope(s), lastModified(QDateTime::currentDateTime()) {}
|
||
|
|
};
|
||
|
|
|
||
|
|
// Configuration change event
|
||
|
|
struct ConfigChangeEvent {
|
||
|
|
QString key;
|
||
|
|
QVariant oldValue;
|
||
|
|
QVariant newValue;
|
||
|
|
ConfigChangeType changeType;
|
||
|
|
ConfigScope scope;
|
||
|
|
QDateTime timestamp;
|
||
|
|
QString source;
|
||
|
|
|
||
|
|
ConfigChangeEvent(const QString& k, const QVariant& oldVal, const QVariant& newVal,
|
||
|
|
ConfigChangeType type, ConfigScope s, const QString& src = "")
|
||
|
|
: key(k), oldValue(oldVal), newValue(newVal), changeType(type), scope(s),
|
||
|
|
timestamp(QDateTime::currentDateTime()), source(src) {}
|
||
|
|
};
|
||
|
|
|
||
|
|
class ConfigurationManager : public QObject
|
||
|
|
{
|
||
|
|
Q_OBJECT
|
||
|
|
public:
|
||
|
|
static ConfigurationManager* instance();
|
||
|
|
|
||
|
|
// Initialization
|
||
|
|
bool initialize(const QString& configPath = "");
|
||
|
|
void shutdown();
|
||
|
|
|
||
|
|
// Basic configuration operations
|
||
|
|
void setValue(const QString& key, const QVariant& value, ConfigScope scope = ConfigScope::System);
|
||
|
|
QVariant getValue(const QString& key, const QVariant& defaultValue = QVariant()) const;
|
||
|
|
bool contains(const QString& key) const;
|
||
|
|
void remove(const QString& key);
|
||
|
|
void clearScope(ConfigScope scope);
|
||
|
|
|
||
|
|
// Typed accessors
|
||
|
|
QString getString(const QString& key, const QString& defaultValue = "") const;
|
||
|
|
int getInt(const QString& key, int defaultValue = 0) const;
|
||
|
|
double getDouble(const QString& key, double defaultValue = 0.0) const;
|
||
|
|
bool getBool(const QString& key, bool defaultValue = false) const;
|
||
|
|
QJsonObject getJsonObject(const QString& key) const;
|
||
|
|
QJsonArray getJsonArray(const QString& key) const;
|
||
|
|
|
||
|
|
// Configuration management
|
||
|
|
void setDefaultValue(const QString& key, const QVariant& defaultValue);
|
||
|
|
void resetToDefault(const QString& key);
|
||
|
|
void resetAllToDefaults();
|
||
|
|
|
||
|
|
// Bulk operations
|
||
|
|
void setValues(const QMap<QString, QVariant>& values, ConfigScope scope = ConfigScope::System);
|
||
|
|
QMap<QString, QVariant> getValues(const QString& prefix = "") const;
|
||
|
|
QMap<QString, QVariant> getScopeValues(ConfigScope scope) const;
|
||
|
|
|
||
|
|
// Persistence
|
||
|
|
bool saveToFile(const QString& filePath = "");
|
||
|
|
bool loadFromFile(const QString& filePath = "");
|
||
|
|
bool exportConfig(const QString& filePath) const;
|
||
|
|
bool importConfig(const QString& filePath);
|
||
|
|
|
||
|
|
// Validation
|
||
|
|
bool validateConfig() const;
|
||
|
|
QStringList validateKey(const QString& key) const;
|
||
|
|
QList<ConfigChangeEvent> getChangeHistory() const;
|
||
|
|
|
||
|
|
// System configuration
|
||
|
|
Trading::SystemConfig getSystemConfig() const;
|
||
|
|
void setSystemConfig(const Trading::SystemConfig& config);
|
||
|
|
|
||
|
|
// Monitoring
|
||
|
|
void enableAutoSave(bool enabled, int intervalMs = 5000);
|
||
|
|
void enableChangeTracking(bool enabled);
|
||
|
|
void setChangeTrackingLimit(int limit);
|
||
|
|
|
||
|
|
// Status
|
||
|
|
bool isInitialized() const { return m_initialized; }
|
||
|
|
QString getConfigPath() const { return m_configPath; }
|
||
|
|
int getTotalEntries() const;
|
||
|
|
int getModifiedEntries() const;
|
||
|
|
|
||
|
|
signals:
|
||
|
|
void configValueChanged(const QString& key, const QVariant& oldValue, const QVariant& newValue);
|
||
|
|
void configScopeChanged(ConfigScope scope);
|
||
|
|
void configLoaded();
|
||
|
|
void configSaved();
|
||
|
|
void configError(const QString& error);
|
||
|
|
void configValidationFailed(const QStringList& errors);
|
||
|
|
|
||
|
|
private:
|
||
|
|
explicit ConfigurationManager(QObject *parent = nullptr);
|
||
|
|
~ConfigurationManager();
|
||
|
|
|
||
|
|
void initializeDefaults();
|
||
|
|
void loadSystemDefaults();
|
||
|
|
void loadUserDefaults();
|
||
|
|
QString getScopeName(ConfigScope scope) const;
|
||
|
|
QString getScopeFilePath(ConfigScope scope) const;
|
||
|
|
void trackChange(const QString& key, const QVariant& oldValue,
|
||
|
|
const QVariant& newValue, ConfigChangeType type, const QString& source = "");
|
||
|
|
void validateAndFixConfig();
|
||
|
|
|
||
|
|
// Configuration storage
|
||
|
|
QMap<QString, ConfigEntry> m_configEntries;
|
||
|
|
QMap<QString, QVariant> m_defaultValues;
|
||
|
|
|
||
|
|
// Change tracking
|
||
|
|
QList<ConfigChangeEvent> m_changeHistory;
|
||
|
|
int m_changeTrackingLimit = 1000;
|
||
|
|
bool m_changeTrackingEnabled = true;
|
||
|
|
|
||
|
|
// File management
|
||
|
|
QString m_configPath;
|
||
|
|
QString m_configFileName = "trading_config.ini";
|
||
|
|
|
||
|
|
// Auto-save
|
||
|
|
QTimer* m_autoSaveTimer;
|
||
|
|
bool m_autoSaveEnabled = false;
|
||
|
|
|
||
|
|
// State
|
||
|
|
bool m_initialized = false;
|
||
|
|
|
||
|
|
// Synchronization
|
||
|
|
mutable QMutex m_mutex;
|
||
|
|
};
|
||
|
|
|
||
|
|
#endif // CONFIGURATION_MANAGER_H
|