Contents of /trunk/src/settings.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 24 - (hide annotations)
Sun Dec 21 11:49:35 2008 UTC (15 years, 5 months ago) by harbaum
File MIME type: text/plain
File size: 7677 byte(s)
Option to disable antialiasing
1 harbaum 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 harbaum 24 { "base_path", STORE_STRING, OFFSET(base_path) },
40 harbaum 1
41     /* from project.c */
42 harbaum 24 { "project", STORE_STRING, OFFSET(project) },
43 harbaum 1
44     /* from osm_api.c */
45 harbaum 24 { "server", STORE_STRING, OFFSET(server) },
46     { "username", STORE_STRING, OFFSET(username) },
47     { "password", STORE_STRING, OFFSET(password) },
48 harbaum 1
49     /* wms servers aren't yet saved as a major rewrite is required before */
50    
51     /* style */
52 harbaum 24 { "style", STORE_STRING, OFFSET(style) },
53 harbaum 1
54 harbaum 14 /* map */
55 harbaum 24 { "no_icons", STORE_BOOL, OFFSET(no_icons) },
56     { "no_antialias", STORE_BOOL, OFFSET(no_antialias) },
57 harbaum 14
58 harbaum 1 { NULL, -1, -1 }
59     };
60    
61     settings_t *settings_load(void) {
62     settings_t *settings = g_new0(settings_t,1);
63    
64     /* ------ set useful defaults ------- */
65    
66     #ifdef USE_HILDON
67     char *p;
68     settings->base_path = strdup(BASE_DIR);
69     #else
70     char *p = getenv("HOME");
71     g_assert(p);
72    
73     /* build image path in home directory */
74     settings->base_path =
75     malloc(strlen(p)+strlen(BASE_DIR)+2);
76     strcpy(settings->base_path, p);
77     if(settings->base_path[strlen(settings->base_path)-1] != '/')
78     strcat(settings->base_path, "/");
79     strcat(settings->base_path, BASE_DIR);
80     #endif
81    
82     /* ------------- setup download defaults -------------------- */
83     settings->server = strdup("http://api.openstreetmap.org/api/0.5");
84     if((p = getenv("OSM_USER")))
85     settings->username = g_strdup(p);
86     else
87     settings->username = g_strdup(_("<your osm username>"));
88    
89     if((p = getenv("OSM_PASS")))
90     settings->password = g_strdup(p);
91     else
92     settings->password = strdup("<password>");
93    
94     settings->style = g_strdup(DEFAULT_STYLE);
95    
96 harbaum 16
97 harbaum 1 /* ------ overwrite with settings from gconf if present ------- */
98     GConfClient *client = gconf_client_get_default();
99     if(client) {
100 harbaum 16
101     #ifdef USE_HILDON
102     /* special explanation for the no_icons setting on hildon/maemo */
103     {
104     char *key = g_strdup_printf("/apps/" PACKAGE "/no_icons");
105     GConfValue *value = gconf_client_get(client, key, NULL);
106     g_free(key);
107     if(value)
108     gconf_value_free(value);
109     else {
110     messagef(NULL, _("Icon drawing is disabled"),
111     _("You are running this version of osm2go on a Internet "
112     "Tablet for the first time. Since these currently have "
113     "problems displaying icons on the map, icons have been "
114     "disabled. You might enable them in the menu under "
115     "Map/No Icons at any time."));
116    
117     settings->no_icons = TRUE;
118     }
119     }
120     #endif
121    
122 harbaum 1 /* restore everything listed in the store table */
123     store_t *st = store;
124     while(st->key) {
125     void **ptr = ((void*)settings) + st->offset;
126     char *key = g_strdup_printf("/apps/" PACKAGE "/%s", st->key);
127    
128     /* check if key is present */
129     GConfValue *value = gconf_client_get(client, key, NULL);
130     if(value) {
131     gconf_value_free(value);
132    
133     switch(st->type) {
134     case STORE_STRING: {
135     char **str = (char**)ptr;
136     if(*str) g_free(*str);
137     *str = gconf_client_get_string(client, key, NULL);
138     } break;
139    
140     case STORE_BOOL:
141     *((int*)ptr) = gconf_client_get_bool(client, key, NULL);
142     break;
143    
144     case STORE_INT:
145     *((int*)ptr) = gconf_client_get_int(client, key, NULL);
146     break;
147    
148     case STORE_FLOAT:
149     *((float*)ptr) = gconf_client_get_float(client, key, NULL);
150     break;
151    
152     default:
153     printf("Unsupported type %d\n", st->type);
154     break;
155     }
156     }
157    
158     g_free(key);
159     st++;
160     }
161     }
162    
163 harbaum 14 /* restore wms server list */
164     char *key = g_strdup_printf("/apps/" PACKAGE "/wms/count");
165     GConfValue *value = gconf_client_get(client, key, NULL);
166     if(value) {
167     gconf_value_free(value);
168    
169     int i, count = gconf_client_get_int(client, key, NULL);
170     g_free(key);
171    
172     wms_server_t **cur = &settings->wms_server;
173     for(i=0;i<count;i++) {
174     key = g_strdup_printf("/apps/" PACKAGE "/wms/name%d", i);
175     char *name = gconf_client_get_string(client, key, NULL);
176     g_free(key);
177     key = g_strdup_printf("/apps/" PACKAGE "/wms/server%d", i);
178     char *server = gconf_client_get_string(client, key, NULL);
179     g_free(key);
180     key = g_strdup_printf("/apps/" PACKAGE "/wms/path%d", i);
181     char *path = gconf_client_get_string(client, key, NULL);
182     g_free(key);
183    
184     /* apply valid entry to list */
185     if(name && server && path) {
186     *cur = g_new0(wms_server_t, 1);
187     (*cur)->name = name;
188     (*cur)->server = server;
189     (*cur)->path = path;
190     cur = &(*cur)->next;
191     } else {
192     if(name) g_free(name);
193     if(server) g_free(server);
194     if(path) g_free(path);
195     }
196     }
197     } else {
198     g_free(key);
199    
200     /* add default server(s) */
201     printf("No WMS servers configured, adding default\n");
202     settings->wms_server = wms_server_get_default();
203     }
204    
205 harbaum 1 return settings;
206     }
207    
208     void settings_save(settings_t *settings) {
209    
210     GConfClient *client = gconf_client_get_default();
211     if(!client) return;
212    
213     /* store everything listed in the store table */
214     store_t *st = store;
215     while(st->key) {
216     void **ptr = ((void*)settings) + st->offset;
217     char *key = g_strdup_printf("/apps/" PACKAGE "/%s", st->key);
218    
219     switch(st->type) {
220     case STORE_STRING:
221     if((char*)(*ptr)) {
222     gconf_client_set_string(client, key, (char*)(*ptr), NULL);
223     }
224     break;
225    
226     case STORE_BOOL:
227     gconf_client_set_bool(client, key, *((int*)ptr), NULL);
228     break;
229    
230     case STORE_INT:
231     gconf_client_set_int(client, key, *((int*)ptr), NULL);
232     break;
233    
234     case STORE_FLOAT:
235     gconf_client_set_float(client, key, *((float*)ptr), NULL);
236     break;
237    
238     default:
239     printf("Unsupported type %d\n", st->type);
240     break;
241     }
242    
243     g_free(key);
244     st++;
245     }
246 harbaum 14
247     /* store list of wms servers */
248     wms_server_t *cur = settings->wms_server;
249     int count = 0;
250     while(cur) {
251     char *key = g_strdup_printf("/apps/" PACKAGE "/wms/name%d", count);
252     gconf_client_set_string(client, key, cur->name, NULL);
253     g_free(key);
254     key = g_strdup_printf("/apps/" PACKAGE "/wms/server%d", count);
255     gconf_client_set_string(client, key, cur->server, NULL);
256     g_free(key);
257     key = g_strdup_printf("/apps/" PACKAGE "/wms/path%d", count);
258     gconf_client_set_string(client, key, cur->path, NULL);
259     g_free(key);
260    
261     count++;
262     cur = cur->next;
263     }
264    
265     char *key = g_strdup_printf("/apps/" PACKAGE "/wms/count");
266     gconf_client_set_int(client, key, count, NULL);
267     g_free(key);
268 harbaum 1 }
269    
270     void settings_free(settings_t *settings) {
271     store_t *st = store;
272    
273 harbaum 14 wms_servers_free(settings->wms_server);
274    
275 harbaum 1 while(st->key) {
276     void **ptr = ((void*)settings) + st->offset;
277    
278     if(st->type == STORE_STRING)
279     if((char*)(*ptr))
280     g_free((char*)(*ptr));
281    
282     st++;
283     }
284    
285     g_free(settings);
286     }