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