|
| 1 | +// File to handle the pomelo settings through an glib ini file |
| 2 | +#include <stdlib.h> |
| 3 | +#include "pomelo-settings.h" |
| 4 | +#include <glib/gstdio.h> |
| 5 | + |
| 6 | +using namespace std; |
| 7 | + |
| 8 | +const char *GROUP_NAME = "pomelo"; |
| 9 | + |
| 10 | +static void rec_mkdir(std::string path) |
| 11 | +{ |
| 12 | + size_t pos = 0; |
| 13 | + int mode = 0; |
| 14 | +#ifndef _WIN32 |
| 15 | + mode = S_IRWXU | S_IRWXG | S_IRWXO; |
| 16 | +#endif |
| 17 | + |
| 18 | + if (path[path.size()-1]!= '/') |
| 19 | + path += '/'; |
| 20 | + |
| 21 | + while((pos = path.find_first_of('/', pos+1)) != std::string::npos) |
| 22 | + g_mkdir(path.substr(0, pos).c_str(), mode); |
| 23 | +} |
| 24 | + |
| 25 | +PomeloSettings::PomeloSettings() |
| 26 | +{ |
| 27 | + string config_dir; |
| 28 | + const char *s; |
| 29 | + if ((s = getenv("LOCALAPPDATA"))) // Really only for windows |
| 30 | + config_dir = string(s); |
| 31 | + else |
| 32 | + config_dir = string(getenv("HOME"))+"/.config"; |
| 33 | + |
| 34 | + config_dir += "/pomelo"; |
| 35 | + rec_mkdir(config_dir); |
| 36 | + m_settings_file = config_dir + "/pomelo.conf"; |
| 37 | + try { |
| 38 | + load(); |
| 39 | + } |
| 40 | + catch(...) { |
| 41 | + save(); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +void PomeloSettings::save() |
| 46 | +{ |
| 47 | + save_to_file(m_settings_file); |
| 48 | +} |
| 49 | + |
| 50 | +void PomeloSettings::load() |
| 51 | +{ |
| 52 | + load_from_file(m_settings_file); |
| 53 | +} |
| 54 | + |
| 55 | +// Convenience functions as we have only one group "pomelo" |
| 56 | +std::string PomeloSettings::get_string_default(const gchar *key, const char *default_value) |
| 57 | +{ |
| 58 | + if (!has_key(GROUP_NAME, key)) |
| 59 | + return default_value; |
| 60 | + return get_string(GROUP_NAME, key); |
| 61 | +} |
| 62 | + |
| 63 | +int PomeloSettings::get_int_default(const gchar *key, int default_value) |
| 64 | +{ |
| 65 | + if (!has_key(GROUP_NAME, key)) |
| 66 | + return default_value; |
| 67 | + return get_integer(GROUP_NAME, key); |
| 68 | +} |
| 69 | +int PomeloSettings::get_double_default(const gchar *key, double default_value) |
| 70 | +{ |
| 71 | + if (!has_key(GROUP_NAME, key)) |
| 72 | + return default_value; |
| 73 | + return get_double(GROUP_NAME, key); |
| 74 | +} |
| 75 | + |
| 76 | +void PomeloSettings::set_int(const char *key, int value) |
| 77 | +{ |
| 78 | + set_integer(GROUP_NAME, key, value); |
| 79 | +} |
| 80 | + |
| 81 | +void PomeloSettings::set_double(const char *key, double value) |
| 82 | +{ |
| 83 | + KeyFile::set_double(GROUP_NAME, key, value); |
| 84 | +} |
| 85 | + |
0 commit comments