Contents of /trunk/src/settings.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 16 - (show annotations)
Tue Dec 16 19:50:43 2008 UTC (15 years, 5 months ago) by harbaum
File MIME type: text/plain
File size: 7594 byte(s)
Added icon display warning under maemo/hildon
1 /*
2 * Copyright (C) 2008 Till Harbaum <till@harbaum.org>.
3 *
4 * This file is part of OSM2Go.
5 *
6 * OSM2Go is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * OSM2Go is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with OSM2Go. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "appdata.h"
21
22 #include <gconf/gconf.h>
23 #include <gconf/gconf-client.h>
24
25 enum {
26 STORE_STRING, STORE_FLOAT, STORE_INT, STORE_BOOL,
27 };
28
29 typedef struct {
30 char *key;
31 int type;
32 int offset;
33 } store_t;
34
35 #define OFFSET(a) offsetof(settings_t, a)
36
37 static store_t store[] = {
38 /* not user configurable */
39 { "base_path", STORE_STRING, OFFSET(base_path) },
40
41 /* from project.c */
42 { "project", STORE_STRING, OFFSET(project) },
43
44 /* from osm_api.c */
45 { "server", STORE_STRING, OFFSET(server) },
46 { "username", STORE_STRING, OFFSET(username) },
47 { "password", STORE_STRING, OFFSET(password) },
48
49 /* wms servers aren't yet saved as a major rewrite is required before */
50
51 /* style */
52 { "style", STORE_STRING, OFFSET(style) },
53
54 /* map */
55 { "no_icons", STORE_BOOL, OFFSET(no_icons) },
56
57 { NULL, -1, -1 }
58 };
59
60 settings_t *settings_load(void) {
61 settings_t *settings = g_new0(settings_t,1);
62
63 /* ------ set useful defaults ------- */
64
65 #ifdef USE_HILDON
66 char *p;
67 settings->base_path = strdup(BASE_DIR);
68 #else
69 char *p = getenv("HOME");
70 g_assert(p);
71
72 /* build image path in home directory */
73 settings->base_path =
74 malloc(strlen(p)+strlen(BASE_DIR)+2);
75 strcpy(settings->base_path, p);
76 if(settings->base_path[strlen(settings->base_path)-1] != '/')
77 strcat(settings->base_path, "/");
78 strcat(settings->base_path, BASE_DIR);
79 #endif
80
81 /* ------------- setup download defaults -------------------- */
82 settings->server = strdup("http://api.openstreetmap.org/api/0.5");
83 if((p = getenv("OSM_USER")))
84 settings->username = g_strdup(p);
85 else
86 settings->username = g_strdup(_("<your osm username>"));
87
88 if((p = getenv("OSM_PASS")))
89 settings->password = g_strdup(p);
90 else
91 settings->password = strdup("<password>");
92
93 settings->style = g_strdup(DEFAULT_STYLE);
94
95
96 /* ------ overwrite with settings from gconf if present ------- */
97 GConfClient *client = gconf_client_get_default();
98 if(client) {
99
100 #ifdef USE_HILDON
101 /* special explanation for the no_icons setting on hildon/maemo */
102 {
103 char *key = g_strdup_printf("/apps/" PACKAGE "/no_icons");
104 GConfValue *value = gconf_client_get(client, key, NULL);
105 g_free(key);
106 if(value)
107 gconf_value_free(value);
108 else {
109 messagef(NULL, _("Icon drawing is disabled"),
110 _("You are running this version of osm2go on a Internet "
111 "Tablet for the first time. Since these currently have "
112 "problems displaying icons on the map, icons have been "
113 "disabled. You might enable them in the menu under "
114 "Map/No Icons at any time."));
115
116 settings->no_icons = TRUE;
117 }
118 }
119 #endif
120
121 /* restore everything listed in the store table */
122 store_t *st = store;
123 while(st->key) {
124 void **ptr = ((void*)settings) + st->offset;
125 char *key = g_strdup_printf("/apps/" PACKAGE "/%s", st->key);
126
127 /* check if key is present */
128 GConfValue *value = gconf_client_get(client, key, NULL);
129 if(value) {
130 gconf_value_free(value);
131
132 switch(st->type) {
133 case STORE_STRING: {
134 char **str = (char**)ptr;
135 if(*str) g_free(*str);
136 *str = gconf_client_get_string(client, key, NULL);
137 } break;
138
139 case STORE_BOOL:
140 *((int*)ptr) = gconf_client_get_bool(client, key, NULL);
141 break;
142
143 case STORE_INT:
144 *((int*)ptr) = gconf_client_get_int(client, key, NULL);
145 break;
146
147 case STORE_FLOAT:
148 *((float*)ptr) = gconf_client_get_float(client, key, NULL);
149 break;
150
151 default:
152 printf("Unsupported type %d\n", st->type);
153 break;
154 }
155 }
156
157 g_free(key);
158 st++;
159 }
160 }
161
162 /* restore wms server list */
163 char *key = g_strdup_printf("/apps/" PACKAGE "/wms/count");
164 GConfValue *value = gconf_client_get(client, key, NULL);
165 if(value) {
166 gconf_value_free(value);
167
168 int i, count = gconf_client_get_int(client, key, NULL);
169 g_free(key);
170
171 wms_server_t **cur = &settings->wms_server;
172 for(i=0;i<count;i++) {
173 key = g_strdup_printf("/apps/" PACKAGE "/wms/name%d", i);
174 char *name = gconf_client_get_string(client, key, NULL);
175 g_free(key);
176 key = g_strdup_printf("/apps/" PACKAGE "/wms/server%d", i);
177 char *server = gconf_client_get_string(client, key, NULL);
178 g_free(key);
179 key = g_strdup_printf("/apps/" PACKAGE "/wms/path%d", i);
180 char *path = gconf_client_get_string(client, key, NULL);
181 g_free(key);
182
183 /* apply valid entry to list */
184 if(name && server && path) {
185 *cur = g_new0(wms_server_t, 1);
186 (*cur)->name = name;
187 (*cur)->server = server;
188 (*cur)->path = path;
189 cur = &(*cur)->next;
190 } else {
191 if(name) g_free(name);
192 if(server) g_free(server);
193 if(path) g_free(path);
194 }
195 }
196 } else {
197 g_free(key);
198
199 /* add default server(s) */
200 printf("No WMS servers configured, adding default\n");
201 settings->wms_server = wms_server_get_default();
202 }
203
204 return settings;
205 }
206
207 void settings_save(settings_t *settings) {
208
209 GConfClient *client = gconf_client_get_default();
210 if(!client) return;
211
212 /* store everything listed in the store table */
213 store_t *st = store;
214 while(st->key) {
215 void **ptr = ((void*)settings) + st->offset;
216 char *key = g_strdup_printf("/apps/" PACKAGE "/%s", st->key);
217
218 switch(st->type) {
219 case STORE_STRING:
220 if((char*)(*ptr)) {
221 gconf_client_set_string(client, key, (char*)(*ptr), NULL);
222 }
223 break;
224
225 case STORE_BOOL:
226 gconf_client_set_bool(client, key, *((int*)ptr), NULL);
227 break;
228
229 case STORE_INT:
230 gconf_client_set_int(client, key, *((int*)ptr), NULL);
231 break;
232
233 case STORE_FLOAT:
234 gconf_client_set_float(client, key, *((float*)ptr), NULL);
235 break;
236
237 default:
238 printf("Unsupported type %d\n", st->type);
239 break;
240 }
241
242 g_free(key);
243 st++;
244 }
245
246 /* store list of wms servers */
247 wms_server_t *cur = settings->wms_server;
248 int count = 0;
249 while(cur) {
250 char *key = g_strdup_printf("/apps/" PACKAGE "/wms/name%d", count);
251 gconf_client_set_string(client, key, cur->name, NULL);
252 g_free(key);
253 key = g_strdup_printf("/apps/" PACKAGE "/wms/server%d", count);
254 gconf_client_set_string(client, key, cur->server, NULL);
255 g_free(key);
256 key = g_strdup_printf("/apps/" PACKAGE "/wms/path%d", count);
257 gconf_client_set_string(client, key, cur->path, NULL);
258 g_free(key);
259
260 count++;
261 cur = cur->next;
262 }
263
264 char *key = g_strdup_printf("/apps/" PACKAGE "/wms/count");
265 gconf_client_set_int(client, key, count, NULL);
266 g_free(key);
267 }
268
269 void settings_free(settings_t *settings) {
270 store_t *st = store;
271
272 wms_servers_free(settings->wms_server);
273
274 while(st->key) {
275 void **ptr = ((void*)settings) + st->offset;
276
277 if(st->type == STORE_STRING)
278 if((char*)(*ptr))
279 g_free((char*)(*ptr));
280
281 st++;
282 }
283
284 g_free(settings);
285 }