Save settings immediatelly on sqlite and macos.

This commit is contained in:
Christian Speckner 2019-05-01 12:04:09 +02:00
parent 017c2a7c16
commit 0e6e54f15c
8 changed files with 50 additions and 9 deletions

View File

@ -32,6 +32,8 @@ class KeyValueRepository
virtual std::map<string, Variant> load() = 0;
virtual void save(const std::map<string, Variant>& values) = 0;
virtual void save(const string& key, const Variant& value) = 0;
};
#endif // KEY_VALUE_REPOSITORY_HXX

View File

@ -26,9 +26,11 @@ class KeyValueRepositoryConfigfile : public KeyValueRepository
KeyValueRepositoryConfigfile(const string& filename);
virtual std::map<string, Variant> load();
std::map<string, Variant> load() override;
virtual void save(const std::map<string, Variant>& values);
void save(const std::map<string, Variant>& values) override;
void save(const string& key, const Variant& value) override {}
private:

View File

@ -24,11 +24,13 @@ class KeyValueRepositoryNoop : public KeyValueRepository
{
public:
virtual std::map<string, Variant> load() {
virtual std::map<string, Variant> load() override {
return std::map<string, Variant>();
}
virtual void save(const std::map<string, Variant>& values) {}
void save(const std::map<string, Variant>& values) override {}
void save(const string& key, const Variant& value) override {}
};
#endif // KEY_VALUE_REPOSITORY_NOOP_HXX

View File

@ -70,6 +70,24 @@ void KeyValueRepositorySqlite::save(const std::map<string, Variant>& values)
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void KeyValueRepositorySqlite::save(const string& key, const Variant& value)
{
try {
myStmtInsert->reset();
(*myStmtInsert)
.bind(1, key.c_str())
.bind(2, value.toCString())
.step();
myStmtInsert->reset();
}
catch (SqliteError err) {
cout << err.message << endl;
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void KeyValueRepositorySqlite::initialize()
{

View File

@ -29,9 +29,11 @@ class KeyValueRepositorySqlite : public KeyValueRepository
KeyValueRepositorySqlite(SqliteDatabase& db, const string& tableName);
virtual std::map<string, Variant> load();
std::map<string, Variant> load() override;
virtual void save(const std::map<string, Variant>& values);
void save(const std::map<string, Variant>& values) override;
void save(const string& key, const Variant& value) override;
void initialize();

View File

@ -593,8 +593,10 @@ const Variant& Settings::value(const string& key) const
void Settings::setValue(const string& key, const Variant& value)
{
auto it = myPermanentSettings.find(key);
if(it != myPermanentSettings.end())
if(it != myPermanentSettings.end()) {
it->second = value;
myRespository->save(key, value);
}
else
myTemporarySettings[key] = value;
}

View File

@ -23,9 +23,11 @@
class SettingsRepositoryMACOS : public KeyValueRepository
{
public:
virtual std::map<string, Variant> load();
std::map<string, Variant> load() override;
virtual void save(const std::map<string, Variant>& values);
void save(const std::map<string, Variant>& values) override;
void save(const string& key, const Variant& value) override;
};
#endif // SETTINGS_REPOSITORY_MACOS_HXX

View File

@ -51,3 +51,14 @@ void SettingsRepositoryMACOS::save(const std::map<string, Variant>& values)
];
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void SettingsRepositoryMACOS::save(const string& key, const Variant& value)
{
@autoreleasepool {
[[NSUserDefaults standardUserDefaults]
setObject:[NSString stringWithUTF8String:value.toCString()]
forKey:[NSString stringWithUTF8String:key.c_str()]
];
}
}