Contents of /trunk/src/settings.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 178 - (show annotations)
Tue Jun 16 15:27:37 2009 UTC (14 years, 11 months ago) by harbaum
File MIME type: text/plain
File size: 9619 byte(s)
First work on demo project auto-setup
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 #define PROXY_KEY "/system/http_proxy/"
26
27 enum {
28 STORE_STRING, STORE_FLOAT, STORE_INT, STORE_BOOL,
29 };
30
31 typedef struct {
32 char *key;
33 int type;
34 int offset;
35 } store_t;
36
37 #define OFFSET(a) offsetof(settings_t, a)
38
39 static store_t store[] = {
40 /* not user configurable */
41 { "base_path", STORE_STRING, OFFSET(base_path) },
42
43 /* from project.c */
44 { "project", STORE_STRING, OFFSET(project) },
45
46 /* from osm_api.c */
47 { "server", STORE_STRING, OFFSET(server) },
48 { "username", STORE_STRING, OFFSET(username) },
49 { "password", STORE_STRING, OFFSET(password) },
50
51 /* wms servers aren't yet saved as a major rewrite is required before */
52
53 /* style */
54 { "style", STORE_STRING, OFFSET(style) },
55
56 /* main */
57 { "no_icons", STORE_BOOL, OFFSET(no_icons) },
58 { "no_antialias", STORE_BOOL, OFFSET(no_antialias) },
59 { "track_path", STORE_STRING, OFFSET(track_path) },
60 { "enable_gps", STORE_BOOL, OFFSET(enable_gps) },
61 { "follow_gps", STORE_BOOL, OFFSET(follow_gps) },
62
63 { NULL, -1, -1 }
64 };
65
66 settings_t *settings_load(void) {
67 settings_t *settings = g_new0(settings_t,1);
68
69 /* ------ set useful defaults ------- */
70
71 #ifdef USE_HILDON
72 char *p;
73 settings->base_path = strdup(BASE_DIR);
74 #else
75 char *p = getenv("HOME");
76 g_assert(p);
77
78 /* build image path in home directory */
79 settings->base_path =
80 malloc(strlen(p)+strlen(BASE_DIR)+2);
81 strcpy(settings->base_path, p);
82 if(settings->base_path[strlen(settings->base_path)-1] != '/')
83 strcat(settings->base_path, "/");
84 strcat(settings->base_path, BASE_DIR);
85 #endif
86
87 /* ------------- setup download defaults -------------------- */
88 settings->server = strdup("http://api.openstreetmap.org/api/0.6");
89 if((p = getenv("OSM_USER")))
90 settings->username = g_strdup(p);
91 else
92 settings->username = g_strdup(_("<your osm username>"));
93
94 if((p = getenv("OSM_PASS")))
95 settings->password = g_strdup(p);
96 else
97 settings->password = strdup("<password>");
98
99 settings->style = g_strdup(DEFAULT_STYLE);
100
101
102 /* ------ overwrite with settings from gconf if present ------- */
103 GConfClient *client = gconf_client_get_default();
104 if(client) {
105
106 #ifdef USE_HILDON
107 /* special explanation for the no_icons setting on hildon/maemo */
108 {
109 char *key = g_strdup_printf("/apps/" PACKAGE "/no_icons");
110 GConfValue *value = gconf_client_get(client, key, NULL);
111 g_free(key);
112 if(value)
113 gconf_value_free(value);
114 else {
115 messagef(NULL, _("Icon drawing is disabled"),
116 _("You are running this version of osm2go on a Internet "
117 "Tablet for the first time. Since these currently have "
118 "problems displaying icons on the map, icons have been "
119 "disabled. You might enable them in the menu under "
120 "Map/No Icons at any time."));
121
122 settings->no_icons = TRUE;
123 }
124 }
125 #endif
126
127 /* restore everything listed in the store table */
128 store_t *st = store;
129 while(st->key) {
130 void **ptr = ((void*)settings) + st->offset;
131 char *key = g_strdup_printf("/apps/" PACKAGE "/%s", st->key);
132
133 /* check if key is present */
134 GConfValue *value = gconf_client_get(client, key, NULL);
135 if(value) {
136 gconf_value_free(value);
137
138 switch(st->type) {
139 case STORE_STRING: {
140 char **str = (char**)ptr;
141 if(*str) g_free(*str);
142 *str = gconf_client_get_string(client, key, NULL);
143 } break;
144
145 case STORE_BOOL:
146 *((int*)ptr) = gconf_client_get_bool(client, key, NULL);
147 break;
148
149 case STORE_INT:
150 *((int*)ptr) = gconf_client_get_int(client, key, NULL);
151 break;
152
153 case STORE_FLOAT:
154 *((float*)ptr) = gconf_client_get_float(client, key, NULL);
155 break;
156
157 default:
158 printf("Unsupported type %d\n", st->type);
159 break;
160 }
161 }
162
163 g_free(key);
164 st++;
165 }
166
167 /* restore wms server list */
168 char *key = g_strdup_printf("/apps/" PACKAGE "/wms/count");
169 GConfValue *value = gconf_client_get(client, key, NULL);
170 if(value) {
171 gconf_value_free(value);
172
173 int i, count = gconf_client_get_int(client, key, NULL);
174 g_free(key);
175
176 wms_server_t **cur = &settings->wms_server;
177 for(i=0;i<count;i++) {
178 key = g_strdup_printf("/apps/" PACKAGE "/wms/name%d", i);
179 char *name = gconf_client_get_string(client, key, NULL);
180 g_free(key);
181 key = g_strdup_printf("/apps/" PACKAGE "/wms/server%d", i);
182 char *server = gconf_client_get_string(client, key, NULL);
183 g_free(key);
184 key = g_strdup_printf("/apps/" PACKAGE "/wms/path%d", i);
185 char *path = gconf_client_get_string(client, key, NULL);
186 g_free(key);
187
188 /* apply valid entry to list */
189 if(name && server && path) {
190 *cur = g_new0(wms_server_t, 1);
191 (*cur)->name = name;
192 (*cur)->server = server;
193 (*cur)->path = path;
194 cur = &(*cur)->next;
195 } else {
196 if(name) g_free(name);
197 if(server) g_free(server);
198 if(path) g_free(path);
199 }
200 }
201 } else {
202 g_free(key);
203
204 /* add default server(s) */
205 printf("No WMS servers configured, adding default\n");
206 settings->wms_server = wms_server_get_default();
207 }
208
209 /* ------------- get proxy settings -------------------- */
210 if(gconf_client_get_bool(client, PROXY_KEY "use_http_proxy", NULL)) {
211 proxy_t *proxy = settings->proxy = g_new0(proxy_t, 1);
212
213 /* get basic settings */
214 proxy->host = gconf_client_get_string(client, PROXY_KEY "host", NULL);
215 proxy->port = gconf_client_get_int(client, PROXY_KEY "port", NULL);
216 proxy->ignore_hosts =
217 gconf_client_get_string(client, PROXY_KEY "ignore_hosts", NULL);
218
219 /* check for authentication */
220 proxy->use_authentication =
221 gconf_client_get_bool(client, PROXY_KEY "use_authentication", NULL);
222
223 if(proxy->use_authentication) {
224 proxy->authentication_user =
225 gconf_client_get_string(client, PROXY_KEY "authentication_user", NULL);
226 proxy->authentication_password =
227 gconf_client_get_string(client, PROXY_KEY "authentication_password",
228 NULL);
229 }
230 }
231
232 #if 1 // def USE_HILDON
233 /* demo setup for maemo/hildon */
234 {
235 char *key = g_strdup_printf("/apps/" PACKAGE "/base_path");
236 GConfValue *value = gconf_client_get(client, key, NULL);
237 if(value)
238 gconf_value_free(value);
239 else {
240 printf("base_path not set, assuming first time boot\n");
241
242 /* check for presence of demo project */
243
244 }
245 }
246 #endif
247
248 }
249
250
251 return settings;
252 }
253
254 void settings_save(settings_t *settings) {
255
256 GConfClient *client = gconf_client_get_default();
257 if(!client) return;
258
259 /* store everything listed in the store table */
260 store_t *st = store;
261 while(st->key) {
262 void **ptr = ((void*)settings) + st->offset;
263 char *key = g_strdup_printf("/apps/" PACKAGE "/%s", st->key);
264
265 switch(st->type) {
266 case STORE_STRING:
267 if((char*)(*ptr)) {
268 gconf_client_set_string(client, key, (char*)(*ptr), NULL);
269 }
270 break;
271
272 case STORE_BOOL:
273 gconf_client_set_bool(client, key, *((int*)ptr), NULL);
274 break;
275
276 case STORE_INT:
277 gconf_client_set_int(client, key, *((int*)ptr), NULL);
278 break;
279
280 case STORE_FLOAT:
281 gconf_client_set_float(client, key, *((float*)ptr), NULL);
282 break;
283
284 default:
285 printf("Unsupported type %d\n", st->type);
286 break;
287 }
288
289 g_free(key);
290 st++;
291 }
292
293 /* store list of wms servers */
294 wms_server_t *cur = settings->wms_server;
295 int count = 0;
296 while(cur) {
297 char *key = g_strdup_printf("/apps/" PACKAGE "/wms/name%d", count);
298 gconf_client_set_string(client, key, cur->name, NULL);
299 g_free(key);
300 key = g_strdup_printf("/apps/" PACKAGE "/wms/server%d", count);
301 gconf_client_set_string(client, key, cur->server, NULL);
302 g_free(key);
303 key = g_strdup_printf("/apps/" PACKAGE "/wms/path%d", count);
304 gconf_client_set_string(client, key, cur->path, NULL);
305 g_free(key);
306
307 count++;
308 cur = cur->next;
309 }
310
311 char *key = g_strdup_printf("/apps/" PACKAGE "/wms/count");
312 gconf_client_set_int(client, key, count, NULL);
313 g_free(key);
314 }
315
316 void settings_free(settings_t *settings) {
317 store_t *st = store;
318
319 wms_servers_free(settings->wms_server);
320
321 while(st->key) {
322 void **ptr = ((void*)settings) + st->offset;
323
324 if(st->type == STORE_STRING)
325 if((char*)(*ptr))
326 g_free((char*)(*ptr));
327
328 st++;
329 }
330
331 /* free proxy settings if present */
332 if(settings->proxy) {
333 proxy_t *proxy = settings->proxy;
334
335 if(proxy->host) g_free(proxy->host);
336 if(proxy->ignore_hosts) g_free(proxy->ignore_hosts);
337 if(proxy->authentication_user) g_free(proxy->authentication_user);
338 if(proxy->authentication_password) g_free(proxy->authentication_password);
339
340 g_free(proxy);
341 }
342
343 g_free(settings);
344 }