Parent Directory | Revision Log
WMS server selection redone, other small changes and bugfixes
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 | /* ------ overwrite with settings from gconf if present ------- */ |
96 | |
97 | GConfClient *client = gconf_client_get_default(); |
98 | if(client) { |
99 | /* restore everything listed in the store table */ |
100 | store_t *st = store; |
101 | while(st->key) { |
102 | void **ptr = ((void*)settings) + st->offset; |
103 | char *key = g_strdup_printf("/apps/" PACKAGE "/%s", st->key); |
104 | |
105 | /* check if key is present */ |
106 | GConfValue *value = gconf_client_get(client, key, NULL); |
107 | if(value) { |
108 | gconf_value_free(value); |
109 | |
110 | switch(st->type) { |
111 | case STORE_STRING: { |
112 | char **str = (char**)ptr; |
113 | if(*str) g_free(*str); |
114 | *str = gconf_client_get_string(client, key, NULL); |
115 | } break; |
116 | |
117 | case STORE_BOOL: |
118 | *((int*)ptr) = gconf_client_get_bool(client, key, NULL); |
119 | break; |
120 | |
121 | case STORE_INT: |
122 | *((int*)ptr) = gconf_client_get_int(client, key, NULL); |
123 | break; |
124 | |
125 | case STORE_FLOAT: |
126 | *((float*)ptr) = gconf_client_get_float(client, key, NULL); |
127 | break; |
128 | |
129 | default: |
130 | printf("Unsupported type %d\n", st->type); |
131 | break; |
132 | } |
133 | } |
134 | |
135 | g_free(key); |
136 | st++; |
137 | } |
138 | } |
139 | |
140 | /* restore wms server list */ |
141 | char *key = g_strdup_printf("/apps/" PACKAGE "/wms/count"); |
142 | GConfValue *value = gconf_client_get(client, key, NULL); |
143 | if(value) { |
144 | gconf_value_free(value); |
145 | |
146 | int i, count = gconf_client_get_int(client, key, NULL); |
147 | g_free(key); |
148 | |
149 | wms_server_t **cur = &settings->wms_server; |
150 | for(i=0;i<count;i++) { |
151 | key = g_strdup_printf("/apps/" PACKAGE "/wms/name%d", i); |
152 | char *name = gconf_client_get_string(client, key, NULL); |
153 | g_free(key); |
154 | key = g_strdup_printf("/apps/" PACKAGE "/wms/server%d", i); |
155 | char *server = gconf_client_get_string(client, key, NULL); |
156 | g_free(key); |
157 | key = g_strdup_printf("/apps/" PACKAGE "/wms/path%d", i); |
158 | char *path = gconf_client_get_string(client, key, NULL); |
159 | g_free(key); |
160 | |
161 | /* apply valid entry to list */ |
162 | if(name && server && path) { |
163 | *cur = g_new0(wms_server_t, 1); |
164 | (*cur)->name = name; |
165 | (*cur)->server = server; |
166 | (*cur)->path = path; |
167 | cur = &(*cur)->next; |
168 | } else { |
169 | if(name) g_free(name); |
170 | if(server) g_free(server); |
171 | if(path) g_free(path); |
172 | } |
173 | } |
174 | } else { |
175 | g_free(key); |
176 | |
177 | /* add default server(s) */ |
178 | printf("No WMS servers configured, adding default\n"); |
179 | settings->wms_server = wms_server_get_default(); |
180 | } |
181 | |
182 | return settings; |
183 | } |
184 | |
185 | void settings_save(settings_t *settings) { |
186 | |
187 | GConfClient *client = gconf_client_get_default(); |
188 | if(!client) return; |
189 | |
190 | /* store everything listed in the store table */ |
191 | store_t *st = store; |
192 | while(st->key) { |
193 | void **ptr = ((void*)settings) + st->offset; |
194 | char *key = g_strdup_printf("/apps/" PACKAGE "/%s", st->key); |
195 | |
196 | switch(st->type) { |
197 | case STORE_STRING: |
198 | if((char*)(*ptr)) { |
199 | gconf_client_set_string(client, key, (char*)(*ptr), NULL); |
200 | } |
201 | break; |
202 | |
203 | case STORE_BOOL: |
204 | gconf_client_set_bool(client, key, *((int*)ptr), NULL); |
205 | break; |
206 | |
207 | case STORE_INT: |
208 | gconf_client_set_int(client, key, *((int*)ptr), NULL); |
209 | break; |
210 | |
211 | case STORE_FLOAT: |
212 | gconf_client_set_float(client, key, *((float*)ptr), NULL); |
213 | break; |
214 | |
215 | default: |
216 | printf("Unsupported type %d\n", st->type); |
217 | break; |
218 | } |
219 | |
220 | g_free(key); |
221 | st++; |
222 | } |
223 | |
224 | /* store list of wms servers */ |
225 | wms_server_t *cur = settings->wms_server; |
226 | int count = 0; |
227 | while(cur) { |
228 | char *key = g_strdup_printf("/apps/" PACKAGE "/wms/name%d", count); |
229 | gconf_client_set_string(client, key, cur->name, NULL); |
230 | g_free(key); |
231 | key = g_strdup_printf("/apps/" PACKAGE "/wms/server%d", count); |
232 | gconf_client_set_string(client, key, cur->server, NULL); |
233 | g_free(key); |
234 | key = g_strdup_printf("/apps/" PACKAGE "/wms/path%d", count); |
235 | gconf_client_set_string(client, key, cur->path, NULL); |
236 | g_free(key); |
237 | |
238 | count++; |
239 | cur = cur->next; |
240 | } |
241 | |
242 | char *key = g_strdup_printf("/apps/" PACKAGE "/wms/count"); |
243 | gconf_client_set_int(client, key, count, NULL); |
244 | g_free(key); |
245 | } |
246 | |
247 | void settings_free(settings_t *settings) { |
248 | store_t *st = store; |
249 | |
250 | wms_servers_free(settings->wms_server); |
251 | |
252 | while(st->key) { |
253 | void **ptr = ((void*)settings) + st->offset; |
254 | |
255 | if(st->type == STORE_STRING) |
256 | if((char*)(*ptr)) |
257 | g_free((char*)(*ptr)); |
258 | |
259 | st++; |
260 | } |
261 | |
262 | g_free(settings); |
263 | } |