Added toggle buttons for switching speak engine, update PO files
[mstardict] / src / conf.cpp
1 /*
2  *  MStarDict - International dictionary for Maemo.
3  *  Copyright (C) 2010 Roman Moravcik
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <list>
25 #include <string>
26
27 #include <gconf/gconf.h>
28 #include <gconf/gconf-client.h>
29
30 #include "conf.hpp"
31
32 Conf::Conf()
33 {
34     /* get the default client */
35     gconf_client = gconf_client_get_default();
36 }
37
38 Conf::~Conf()
39 {
40 }
41
42 bool Conf::GetBool(const gchar *key,
43                    bool *val)
44 {
45     GConfValue *value = NULL;
46
47     if (!gconf_client)
48         return false;
49
50     value = gconf_client_get(gconf_client, key, NULL);
51     if (value) {
52         *val = gconf_value_get_bool(value);
53         gconf_value_free(value);
54     } else {
55         return false;
56     }
57     return true;
58 }
59
60 bool Conf::SetBool(const gchar *key,
61                    bool val)
62 {
63     gboolean ret = false;
64
65     if (!gconf_client)
66         return false;
67
68     ret = gconf_client_set_bool(gconf_client, key, val, NULL);
69     return ret;
70 }
71
72 bool Conf::GetInt(const gchar *key,
73                   int *val)
74 {
75     GConfValue *value = NULL;
76
77     if (!gconf_client)
78         return false;
79
80     value = gconf_client_get(gconf_client, key, NULL);
81     if (value) {
82         *val = gconf_value_get_int(value);
83         gconf_value_free(value);
84     } else {
85         return false;
86     }
87     return true;
88 }
89
90 bool Conf::SetInt(const gchar *key,
91                   int val)
92 {
93     gboolean ret = false;
94
95     if (!gconf_client)
96         return false;
97
98     ret = gconf_client_set_int(gconf_client, key, val, NULL);
99     return ret;
100 }
101
102 bool Conf::GetString(const gchar *key,
103                      gchar **val)
104 {
105     GConfValue *value = NULL;
106     const gchar *sValue = NULL;
107
108     if (!gconf_client)
109         return false;
110
111     value = gconf_client_get(gconf_client, key, NULL);
112     if (value) {
113         sValue = gconf_value_get_string(value);
114
115         if (*val)
116             g_free(*val);
117         *val = g_strdup(sValue);
118
119         gconf_value_free(value);
120     } else {
121         return false;
122     }
123     return true;
124 }
125
126 bool Conf::SetString(const gchar *key,
127                      const gchar *val)
128 {
129     gboolean ret = false;
130
131     if (!gconf_client)
132         return false;
133
134     if (!val)
135         return false;
136
137     ret = gconf_client_set_string(gconf_client, key, val, NULL);
138     return ret;
139 }
140
141 bool Conf::GetStringList(const gchar *key,
142                          std::list < std::string > &list)
143 {
144     GConfValue *value = NULL;
145     GSList *slist = NULL;
146
147     if (!gconf_client)
148         return false;
149
150     value = gconf_client_get(gconf_client, key, NULL);
151     if (value) {
152         slist = gconf_value_get_list(value);
153
154         if (slist) {
155             GSList *entry = slist;
156
157             list.clear();
158             while (entry) {
159                 list.push_back(std::string(gconf_value_get_string((GConfValue *)entry->data)));
160                 entry = entry->next;
161             }
162         }
163         gconf_value_free(value);
164     } else {
165         return false;
166     }
167     return true;
168 }
169
170 bool Conf::SetStringList(const gchar *key,
171                          std::list < std::string > &list)
172 {
173     GSList *slist = NULL;
174     gboolean ret = false;
175
176     if (!gconf_client)
177         return false;
178
179     for (std::list < std::string >::iterator i = list.begin(); i != list.end(); ++i) {
180         slist = g_slist_append(slist, (gpointer) i->c_str());
181     }
182
183     if (slist) {
184         ret = gconf_client_set_list(gconf_client, key, GCONF_VALUE_STRING, slist, NULL);
185         g_slist_free(slist);
186     }
187     return ret;
188 }