Line Graph: fix cairo radial gradient LED sim in portrait orientation
[calf.git] / src / gui_config.cpp
blobfc51ab85dc42f42e30464b7826625cec5306b10d
1 #include <calf/gui_config.h>
2 #include <assert.h>
3 #include <stdio.h>
5 using namespace std;
6 using namespace calf_utils;
8 gui_config::gui_config()
10 rack_float = 0;
11 float_size = 1;
12 rack_ears = true;
13 vu_meters = true;
16 gui_config::~gui_config()
20 void gui_config::load(config_db_iface *db)
22 rack_float = db->get_int("rack-float", gui_config().rack_float);
23 float_size = db->get_int("float-size", gui_config().float_size);
24 rack_ears = db->get_bool("show-rack-ears", gui_config().rack_ears);
25 vu_meters = db->get_bool("show-vu-meters", gui_config().vu_meters);
28 void gui_config::save(config_db_iface *db)
30 db->set_int("rack-float", rack_float);
31 db->set_int("float-size", float_size);
32 db->set_bool("show-rack-ears", rack_ears);
33 db->set_bool("show-vu-meters", vu_meters);
34 db->save();
37 ////////////////////////////////////////////////////////////////////////////////////////////////////
39 gkeyfile_config_db::notifier::notifier(gkeyfile_config_db *_parent, config_listener_iface *_listener)
41 parent = _parent;
42 listener = _listener;
45 gkeyfile_config_db::notifier::~notifier()
47 parent->remove_notifier(this);
51 ////////////////////////////////////////////////////////////////////////////////////////////////////
53 gkeyfile_config_db::gkeyfile_config_db(GKeyFile *kf, const char *_filename, const char *_section)
55 keyfile = kf;
56 filename = _filename;
57 section = _section;
60 void gkeyfile_config_db::handle_error(GError *error)
62 if (error)
64 string msg = error->message;
65 g_error_free(error);
66 throw config_exception(msg.c_str());
70 void gkeyfile_config_db::remove_notifier(notifier *n)
72 for (size_t i = 0; i < notifiers.size(); i++)
74 if (notifiers[i] == n)
76 notifiers.erase(notifiers.begin() + i);
77 return;
80 assert(0);
83 bool gkeyfile_config_db::has_dir(const char *key)
85 return g_key_file_has_group(keyfile, key);
88 static bool check_not_found_and_delete(GError *error)
90 if (error && error->domain == G_KEY_FILE_ERROR)
92 switch(error->code)
94 case G_KEY_FILE_ERROR_KEY_NOT_FOUND:
95 case G_KEY_FILE_ERROR_GROUP_NOT_FOUND:
96 g_error_free(error);
97 return true;
98 default:
99 return false;
102 return false;
105 bool gkeyfile_config_db::get_bool(const char *key, bool def_value)
107 GError *err = NULL;
108 bool value = (bool)g_key_file_get_boolean(keyfile, section.c_str(), key, &err);
109 if (err)
111 if (check_not_found_and_delete(err))
112 return def_value;
113 handle_error(err);
115 return value;
118 int gkeyfile_config_db::get_int(const char *key, int def_value)
120 GError *err = NULL;
121 int value = g_key_file_get_integer(keyfile, section.c_str(), key, &err);
122 if (err)
124 if (check_not_found_and_delete(err))
125 return def_value;
126 handle_error(err);
128 return value;
131 std::string gkeyfile_config_db::get_string(const char *key, const std::string &def_value)
133 GError *err = NULL;
134 gchar *value = g_key_file_get_string(keyfile, section.c_str(), key, &err);
135 if (err)
137 if (check_not_found_and_delete(err))
138 return def_value;
139 handle_error(err);
141 return value;
144 void gkeyfile_config_db::set_bool(const char *key, bool value)
146 g_key_file_set_boolean(keyfile, section.c_str(), key, (gboolean)value);
149 void gkeyfile_config_db::set_int(const char *key, int value)
151 g_key_file_set_integer(keyfile, section.c_str(), key, value);
154 void gkeyfile_config_db::set_string(const char *key, const std::string &value)
156 g_key_file_set_string(keyfile, section.c_str(), key, value.c_str());
159 void gkeyfile_config_db::save()
161 GError *err = NULL;
162 gsize length = 0;
163 gchar *data = g_key_file_to_data(keyfile, &length, &err);
164 if (err)
165 handle_error(err);
167 if (!g_file_set_contents(filename.c_str(), data, length, &err))
169 g_free(data);
170 handle_error(err);
172 g_free(data);
174 for (size_t i = 0; i < notifiers.size(); i++)
175 notifiers[i]->listener->on_config_change();
178 config_notifier_iface *gkeyfile_config_db::add_listener(config_listener_iface *listener)
180 notifier *n = new notifier(this, listener);
181 notifiers.push_back(n);
182 return n;
185 gkeyfile_config_db::~gkeyfile_config_db()