Parent Directory | Revision Log
Version increase
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 | static void vmessagef(GtkWidget *parent, int type, int buttons, |
23 | char *title, const char *fmt, |
24 | va_list args) { |
25 | |
26 | char *buf = g_strdup_vprintf(fmt, args); |
27 | |
28 | GtkWidget *dialog = gtk_message_dialog_new( |
29 | GTK_WINDOW(parent), |
30 | GTK_DIALOG_DESTROY_WITH_PARENT, |
31 | type, buttons, buf); |
32 | |
33 | gtk_window_set_title(GTK_WINDOW(dialog), title); |
34 | |
35 | gtk_dialog_run(GTK_DIALOG(dialog)); |
36 | gtk_widget_destroy(dialog); |
37 | |
38 | g_free(buf); |
39 | } |
40 | |
41 | |
42 | void messagef(GtkWidget *parent, char *title, const char *fmt, ...) { |
43 | va_list args; |
44 | va_start( args, fmt ); |
45 | vmessagef(parent, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, title, fmt, args); |
46 | va_end( args ); |
47 | } |
48 | |
49 | void errorf(GtkWidget *parent, const char *fmt, ...) { |
50 | va_list args; |
51 | va_start( args, fmt ); |
52 | vmessagef(parent, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, |
53 | _("Error"), fmt, args); |
54 | va_end( args ); |
55 | } |
56 | |
57 | void warningf(GtkWidget *parent, const char *fmt, ...) { |
58 | va_list args; |
59 | va_start( args, fmt ); |
60 | vmessagef(parent, GTK_MESSAGE_WARNING, GTK_BUTTONS_CLOSE, |
61 | _("Warning"), fmt, args); |
62 | va_end( args ); |
63 | } |
64 | |
65 | static void on_toggled(GtkWidget *button, gpointer data) { |
66 | gboolean active = |
67 | gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)); |
68 | |
69 | GtkWidget *dialog = gtk_widget_get_toplevel(button); |
70 | |
71 | if(*(gint*)data & MISC_AGAIN_FLAG_DONT_SAVE_NO) |
72 | gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), |
73 | GTK_RESPONSE_NO, !active); |
74 | |
75 | if(*(gint*)data & MISC_AGAIN_FLAG_DONT_SAVE_YES) |
76 | gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), |
77 | GTK_RESPONSE_YES, !active); |
78 | } |
79 | |
80 | gboolean yes_no_f(GtkWidget *parent, appdata_t *appdata, gulong again_bit, |
81 | gint flags, char *title, const char *fmt, ...) { |
82 | |
83 | if(appdata && again_bit && (appdata->dialog_again.not & again_bit)) |
84 | return((appdata->dialog_again.reply & again_bit) != 0); |
85 | |
86 | va_list args; |
87 | va_start( args, fmt ); |
88 | char *buf = g_strdup_vprintf(fmt, args); |
89 | va_end( args ); |
90 | |
91 | printf("%s: \"%s\"\n", title, buf); |
92 | |
93 | GtkWidget *dialog = gtk_message_dialog_new( |
94 | GTK_WINDOW(parent), |
95 | GTK_DIALOG_DESTROY_WITH_PARENT, |
96 | GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, |
97 | buf); |
98 | |
99 | gtk_window_set_title(GTK_WINDOW(dialog), title); |
100 | |
101 | GtkWidget *cbut = NULL; |
102 | if(appdata && again_bit) { |
103 | GtkWidget *alignment = gtk_alignment_new(0.5, 0, 0, 0); |
104 | |
105 | cbut = gtk_check_button_new_with_label( |
106 | _("Don't ask this question again this session")); |
107 | g_signal_connect(cbut, "toggled", G_CALLBACK(on_toggled), &flags); |
108 | |
109 | gtk_container_add(GTK_CONTAINER(alignment), cbut); |
110 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), alignment); |
111 | |
112 | gtk_widget_show_all(dialog); |
113 | } |
114 | |
115 | gboolean yes = (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_YES); |
116 | |
117 | if(cbut && gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cbut))) { |
118 | /* the user doesn't want to see this dialog again */ |
119 | |
120 | appdata->dialog_again.not |= again_bit; |
121 | if(yes) appdata->dialog_again.reply |= again_bit; |
122 | else appdata->dialog_again.reply &= ~again_bit; |
123 | } |
124 | |
125 | gtk_widget_destroy(dialog); |
126 | |
127 | g_free(buf); |
128 | return yes; |
129 | } |
130 | |
131 | static const char *data_paths[] = { |
132 | "~/." PACKAGE, // in home directory |
133 | PREFIX "/share/" PACKAGE , // final installation path |
134 | #ifdef USE_HILDON |
135 | "/media/mmc1/" PACKAGE, // path to external memory card |
136 | "/media/mmc2/" PACKAGE, // path to internal memory card |
137 | #endif |
138 | "./data", "../data", // local paths for testing |
139 | NULL |
140 | }; |
141 | |
142 | char *find_file(char *name) { |
143 | const char **path = data_paths; |
144 | char *p = getenv("HOME"); |
145 | |
146 | while(*path) { |
147 | char *full_path = NULL; |
148 | |
149 | if(*path[0] == '~') |
150 | full_path = g_strdup_printf("%s/%s/%s", p, *path+2, name); |
151 | else |
152 | full_path = g_strdup_printf("%s/%s", *path, name); |
153 | |
154 | if(g_file_test(full_path, G_FILE_TEST_IS_REGULAR)) |
155 | return full_path; |
156 | |
157 | g_free(full_path); |
158 | path++; |
159 | } |
160 | |
161 | return NULL; |
162 | } |
163 | |
164 | /* scan all data directories for the given file pattern and */ |
165 | /* return a list of files matching this pattern */ |
166 | file_chain_t *file_scan(char *pattern) { |
167 | file_chain_t *chain = NULL, **chainP = &chain; |
168 | |
169 | const char **path = data_paths; |
170 | char *p = getenv("HOME"); |
171 | |
172 | while(*path) { |
173 | GDir *dir = NULL; |
174 | |
175 | /* scan for projects */ |
176 | const char *dirname = *path; |
177 | |
178 | if(*path[0] == '~') |
179 | dirname = g_strdup_printf("%s/%s", p, *path+2); |
180 | |
181 | if((dir = g_dir_open(dirname, 0, NULL))) { |
182 | const char *name = NULL; |
183 | do { |
184 | name = g_dir_read_name(dir); |
185 | |
186 | if(name) { |
187 | char *fullname = g_strdup_printf("%s/%s", dirname, name); |
188 | if(g_file_test(fullname, G_FILE_TEST_IS_REGULAR)) { |
189 | if(g_pattern_match_simple(pattern, name)) { |
190 | *chainP = g_new0(file_chain_t, 1); |
191 | (*chainP)->name = fullname; |
192 | chainP = &(*chainP)->next; |
193 | } else |
194 | g_free(fullname); |
195 | } else |
196 | g_free(fullname); |
197 | } |
198 | } while(name); |
199 | |
200 | g_dir_close(dir); |
201 | |
202 | if(*path[0] == '~') |
203 | g_free((char*)dirname); |
204 | } |
205 | |
206 | path++; |
207 | } |
208 | |
209 | return chain; |
210 | } |
211 | |
212 | |
213 | #ifdef USE_HILDON |
214 | static const gint dialog_sizes[][2] = { |
215 | { 400, 100 }, // SMALL |
216 | { 450, 300 }, // MEDIUM |
217 | #if MAEMO_VERSION_MAJOR < 5 |
218 | { 800, 480 }, // LARGE |
219 | #else |
220 | { 800, 380 }, // LARGE |
221 | #endif |
222 | { 640, 100 }, // WIDE |
223 | { 450, 480 }, // HIGH |
224 | }; |
225 | #else |
226 | static const gint dialog_sizes[][2] = { |
227 | { 300, 100 }, // SMALL |
228 | { 400, 300 }, // MEDIUM |
229 | { 500, 350 }, // LARGE |
230 | { 450, 100 }, // WIDE |
231 | { 200, 350 }, // HIGH |
232 | }; |
233 | #endif |
234 | |
235 | /* create a modal dialog using one of the predefined size hints */ |
236 | GtkWidget *misc_dialog_new(guint hint, const char *title, |
237 | GtkWindow *parent, ...) { |
238 | va_list args; |
239 | va_start( args, parent ); |
240 | |
241 | /* create dialog itself */ |
242 | GtkWidget *dialog = gtk_dialog_new(); |
243 | |
244 | gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); |
245 | if(title) gtk_window_set_title(GTK_WINDOW(dialog), title); |
246 | if(parent) gtk_window_set_transient_for(GTK_WINDOW(dialog), parent); |
247 | |
248 | const gchar *button_text = va_arg(args, const gchar *); |
249 | while(button_text) { |
250 | gtk_dialog_add_button(GTK_DIALOG(dialog), button_text, va_arg(args, gint)); |
251 | button_text = va_arg(args, const gchar *); |
252 | } |
253 | |
254 | va_end( args ); |
255 | |
256 | if(hint != MISC_DIALOG_NOSIZE) |
257 | gtk_window_set_default_size(GTK_WINDOW(dialog), |
258 | dialog_sizes[hint][0], dialog_sizes[hint][1]); |
259 | |
260 | return dialog; |
261 | } |
262 | |
263 | #if defined(USE_HILDON) && (MAEMO_VERSION_MAJOR == 5) |
264 | #include <hildon/hildon-pannable-area.h> |
265 | /* create a pannable area */ |
266 | GtkWidget *misc_scrolled_window_new(gboolean etched_in) { |
267 | return hildon_pannable_area_new(); |
268 | } |
269 | |
270 | void misc_scrolled_window_add_with_viewport(GtkWidget *win, GtkWidget *child) { |
271 | hildon_pannable_area_add_with_viewport(HILDON_PANNABLE_AREA(win), child); |
272 | } |
273 | |
274 | #else |
275 | /* create a scrolled window */ |
276 | GtkWidget *misc_scrolled_window_new(gboolean etched_in) { |
277 | GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL); |
278 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), |
279 | GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); |
280 | if(etched_in) |
281 | gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), |
282 | GTK_SHADOW_ETCHED_IN); |
283 | return scrolled_window; |
284 | } |
285 | |
286 | void misc_scrolled_window_add_with_viewport(GtkWidget *win, GtkWidget *child) { |
287 | gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(win), child); |
288 | } |
289 | |
290 | |
291 | #endif |
292 | |
293 | const char *misc_get_proxy_uri(settings_t *settings) { |
294 | static char proxy_buffer[64]; |
295 | |
296 | /* use environment settings if preset */ |
297 | const char *proxy = g_getenv("http_proxy"); |
298 | if(proxy) return proxy; |
299 | |
300 | /* otherwise try settings */ |
301 | if(!settings || !settings->proxy || |
302 | !settings->proxy->host) return NULL; |
303 | |
304 | snprintf(proxy_buffer, sizeof(proxy_buffer), |
305 | "http://%s:%u", settings->proxy->host, |
306 | settings->proxy->port); |
307 | proxy_buffer[sizeof(proxy_buffer)-1] = 0; |
308 | return proxy_buffer; |
309 | } |
310 | |
311 | void misc_table_attach(GtkWidget *table, GtkWidget *widget, int x, int y) { |
312 | gtk_table_attach_defaults(GTK_TABLE(table), widget, x, x+1, y, y+1); |
313 | } |