EQ8/EQ12: High pass and low pass with Q
[calf.git] / src / gui_config.cpp
blob2c6c83428657237e3c050e62f2a9af06d7f2ed72
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;
14 style = "Calf_Default";
17 gui_config::~gui_config()
21 void gui_config::load(config_db_iface *db)
23 rack_float = db->get_int("rack-float", gui_config().rack_float);
24 float_size = db->get_int("float-size", gui_config().float_size);
25 rack_ears = db->get_bool("show-rack-ears", gui_config().rack_ears);
26 vu_meters = db->get_bool("show-vu-meters", gui_config().vu_meters);
27 style = db->get_string("style", gui_config().style);
30 void gui_config::save(config_db_iface *db)
32 db->set_int("rack-float", rack_float);
33 db->set_int("float-size", float_size);
34 db->set_bool("show-rack-ears", rack_ears);
35 db->set_bool("show-vu-meters", vu_meters);
36 db->set_string("style", style);
37 db->save();
40 ////////////////////////////////////////////////////////////////////////////////////////////////////
42 gkeyfile_config_db::notifier::notifier(gkeyfile_config_db *_parent, config_listener_iface *_listener)
44 parent = _parent;
45 listener = _listener;
48 gkeyfile_config_db::notifier::~notifier()
50 parent->remove_notifier(this);
54 ////////////////////////////////////////////////////////////////////////////////////////////////////
56 gkeyfile_config_db::gkeyfile_config_db(GKeyFile *kf, const char *_filename, const char *_section)
58 keyfile = kf;
59 filename = _filename;
60 section = _section;
63 void gkeyfile_config_db::handle_error(GError *error)
65 if (error)
67 string msg = error->message;
68 g_error_free(error);
69 throw config_exception(msg.c_str());
73 void gkeyfile_config_db::remove_notifier(notifier *n)
75 for (size_t i = 0; i < notifiers.size(); i++)
77 if (notifiers[i] == n)
79 notifiers.erase(notifiers.begin() + i);
80 return;
83 assert(0);
86 bool gkeyfile_config_db::has_dir(const char *key)
88 return g_key_file_has_group(keyfile, key);
91 static bool check_not_found_and_delete(GError *error)
93 if (error && error->domain == G_KEY_FILE_ERROR)
95 switch(error->code)
97 case G_KEY_FILE_ERROR_KEY_NOT_FOUND:
98 case G_KEY_FILE_ERROR_GROUP_NOT_FOUND:
99 g_error_free(error);
100 return true;
101 default:
102 return false;
105 return false;
108 bool gkeyfile_config_db::get_bool(const char *key, bool def_value)
110 GError *err = NULL;
111 bool value = (bool)g_key_file_get_boolean(keyfile, section.c_str(), key, &err);
112 if (err)
114 if (check_not_found_and_delete(err))
115 return def_value;
116 handle_error(err);
118 return value;
121 int gkeyfile_config_db::get_int(const char *key, int def_value)
123 GError *err = NULL;
124 int value = g_key_file_get_integer(keyfile, section.c_str(), key, &err);
125 if (err)
127 if (check_not_found_and_delete(err))
128 return def_value;
129 handle_error(err);
131 return value;
134 std::string gkeyfile_config_db::get_string(const char *key, const std::string &def_value)
136 GError *err = NULL;
137 gchar *value = g_key_file_get_string(keyfile, section.c_str(), key, &err);
138 if (err)
140 if (check_not_found_and_delete(err))
141 return def_value;
142 handle_error(err);
144 return value;
147 void gkeyfile_config_db::set_bool(const char *key, bool value)
149 g_key_file_set_boolean(keyfile, section.c_str(), key, (gboolean)value);
152 void gkeyfile_config_db::set_int(const char *key, int value)
154 g_key_file_set_integer(keyfile, section.c_str(), key, value);
157 void gkeyfile_config_db::set_string(const char *key, const std::string &value)
159 g_key_file_set_string(keyfile, section.c_str(), key, value.c_str());
162 void gkeyfile_config_db::save()
164 GError *err = NULL;
165 gsize length = 0;
166 gchar *data = g_key_file_to_data(keyfile, &length, &err);
167 if (err)
168 handle_error(err);
170 if (!g_file_set_contents(filename.c_str(), data, length, &err))
172 g_free(data);
173 handle_error(err);
175 g_free(data);
177 for (size_t i = 0; i < notifiers.size(); i++)
178 notifiers[i]->listener->on_config_change();
181 config_notifier_iface *gkeyfile_config_db::add_listener(config_listener_iface *listener)
183 notifier *n = new notifier(this, listener);
184 notifiers.push_back(n);
185 return n;
188 gkeyfile_config_db::~gkeyfile_config_db()