Parent Directory | Revision Log
Initial import
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 | { NULL, -1, -1 } |
55 | }; |
56 | |
57 | settings_t *settings_load(void) { |
58 | settings_t *settings = g_new0(settings_t,1); |
59 | |
60 | /* ------ set useful defaults ------- */ |
61 | |
62 | #ifdef USE_HILDON |
63 | char *p; |
64 | settings->base_path = strdup(BASE_DIR); |
65 | #else |
66 | char *p = getenv("HOME"); |
67 | g_assert(p); |
68 | |
69 | /* build image path in home directory */ |
70 | settings->base_path = |
71 | malloc(strlen(p)+strlen(BASE_DIR)+2); |
72 | strcpy(settings->base_path, p); |
73 | if(settings->base_path[strlen(settings->base_path)-1] != '/') |
74 | strcat(settings->base_path, "/"); |
75 | strcat(settings->base_path, BASE_DIR); |
76 | #endif |
77 | |
78 | /* ------------- setup download defaults -------------------- */ |
79 | settings->server = strdup("http://api.openstreetmap.org/api/0.5"); |
80 | if((p = getenv("OSM_USER"))) |
81 | settings->username = g_strdup(p); |
82 | else |
83 | settings->username = g_strdup(_("<your osm username>")); |
84 | |
85 | if((p = getenv("OSM_PASS"))) |
86 | settings->password = g_strdup(p); |
87 | else |
88 | settings->password = strdup("<password>"); |
89 | |
90 | settings->wms_server = g_strdup("http://onearth.jpl.nasa.gov"); |
91 | settings->wms_path = g_strdup("/wms.cgi"); |
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 | return settings; |
141 | } |
142 | |
143 | void settings_save(settings_t *settings) { |
144 | |
145 | GConfClient *client = gconf_client_get_default(); |
146 | if(!client) return; |
147 | |
148 | /* store everything listed in the store table */ |
149 | store_t *st = store; |
150 | while(st->key) { |
151 | void **ptr = ((void*)settings) + st->offset; |
152 | char *key = g_strdup_printf("/apps/" PACKAGE "/%s", st->key); |
153 | |
154 | switch(st->type) { |
155 | case STORE_STRING: |
156 | if((char*)(*ptr)) { |
157 | gconf_client_set_string(client, key, (char*)(*ptr), NULL); |
158 | } |
159 | break; |
160 | |
161 | case STORE_BOOL: |
162 | gconf_client_set_bool(client, key, *((int*)ptr), NULL); |
163 | break; |
164 | |
165 | case STORE_INT: |
166 | gconf_client_set_int(client, key, *((int*)ptr), NULL); |
167 | break; |
168 | |
169 | case STORE_FLOAT: |
170 | gconf_client_set_float(client, key, *((float*)ptr), NULL); |
171 | break; |
172 | |
173 | default: |
174 | printf("Unsupported type %d\n", st->type); |
175 | break; |
176 | } |
177 | |
178 | g_free(key); |
179 | st++; |
180 | } |
181 | } |
182 | |
183 | void settings_free(settings_t *settings) { |
184 | store_t *st = store; |
185 | |
186 | /* this is only required because wms_xxx is not yet stored properly */ |
187 | g_free(settings->wms_server); |
188 | g_free(settings->wms_path); |
189 | |
190 | while(st->key) { |
191 | void **ptr = ((void*)settings) + st->offset; |
192 | |
193 | if(st->type == STORE_STRING) |
194 | if((char*)(*ptr)) |
195 | g_free((char*)(*ptr)); |
196 | |
197 | st++; |
198 | } |
199 | |
200 | g_free(settings); |
201 | } |