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