-
Notifications
You must be signed in to change notification settings - Fork 12
/
config_storage.h
68 lines (53 loc) · 1.66 KB
/
config_storage.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once
#include <gtkmm.h>
#include "json.hpp"
// for convenience
using json = nlohmann::json;
class ConfigCache {
public:
ConfigCache() {
auto cache_dir_path = Glib::build_filename(Glib::get_user_cache_dir(), Glib::get_prgname());
auto cache_dir = Gio::File::create_for_path(cache_dir_path);
cache_file_path = Glib::build_filename(cache_dir_path, "cache.json");
if (!cache_dir->query_exists()) {
cache_dir->make_directory_with_parents();
}
refresh();
}
void refresh() {
auto cache_file = Gio::File::create_for_path(cache_file_path);
if (!cache_file->query_exists()) {
cache_object = json{};
return;
}
char* contents = nullptr;
gsize length = 0;
if (!cache_file->load_contents(contents, length)) {
throw std::runtime_error("Error loading cache file");
}
cache_object = json::parse(contents);
g_free(contents);
}
void save() {
auto cache_file = Gio::File::create_for_path(cache_file_path);
const auto cache_dump = cache_object.dump();
std::string empty;
cache_file->replace_contents(
cache_dump.c_str(), cache_dump.size(), empty, empty, false, Gio::FILE_CREATE_PRIVATE);
}
json& operator[](std::string key) {
return cache_object[key];
}
json::iterator find(std::string key) {
return cache_object.find(key);
}
json::iterator begin() {
return cache_object.begin();
}
json::iterator end() {
return cache_object.end();
}
private:
json cache_object;
std::string cache_file_path;
};