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