Contents of /trunk/src/misc.c

Parent Directory Parent Directory | Revision Log Revision Log


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