Contents of /trunk/src/misc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 232 - (hide annotations)
Fri Jul 17 12:32:57 2009 UTC (14 years, 10 months ago) by harbaum
File MIME type: text/plain
File size: 8850 byte(s)
Bug fix in proxy evaluation
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 209
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 harbaum 1 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 harbaum 41 "~/." PACKAGE, // in home directory
133     PREFIX "/share/" PACKAGE , // final installation path
134 harbaum 1 #ifdef USE_HILDON
135 harbaum 41 "/media/mmc1/" PACKAGE, // path to external memory card
136     "/media/mmc2/" PACKAGE, // path to internal memory card
137 harbaum 1 #endif
138 harbaum 41 "./data", "../data", // local paths for testing
139 harbaum 1 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 harbaum 167
212    
213     #ifdef USE_HILDON
214     static const gint dialog_sizes[][2] = {
215     { 400, 100 }, // SMALL
216     { 450, 300 }, // MEDIUM
217 harbaum 172 #if MAEMO_VERSION_MAJOR < 5
218 harbaum 167 { 800, 480 }, // LARGE
219 harbaum 172 #else
220     { 800, 380 }, // LARGE
221     #endif
222 harbaum 167 { 640, 100 }, // WIDE
223 harbaum 200 { 450, 480 }, // HIGH
224 harbaum 167 };
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 harbaum 200 { 200, 350 }, // HIGH
232 harbaum 167 };
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 harbaum 195
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 harbaum 200 const char *misc_get_proxy_uri(settings_t *settings) {
294     static char proxy_buffer[64];
295 harbaum 195
296 harbaum 200 /* use environment settings if preset */
297     const char *proxy = g_getenv("http_proxy");
298 harbaum 232 if(proxy) {
299     printf("http_proxy: %s\n", proxy);
300     return proxy;
301     }
302 harbaum 200
303     /* otherwise try settings */
304     if(!settings || !settings->proxy ||
305     !settings->proxy->host) return NULL;
306    
307 harbaum 232 snprintf(proxy_buffer, sizeof(proxy_buffer), "%s%s:%u",
308     strncmp(settings->proxy->host, "http://", 7)?"http://":"",
309     settings->proxy->host, settings->proxy->port);
310    
311 harbaum 200 proxy_buffer[sizeof(proxy_buffer)-1] = 0;
312 harbaum 232 printf("gconf_proxy: %s\n", proxy_buffer);
313 harbaum 200 return proxy_buffer;
314     }
315 harbaum 203
316     void misc_table_attach(GtkWidget *table, GtkWidget *widget, int x, int y) {
317     gtk_table_attach_defaults(GTK_TABLE(table), widget, x, x+1, y, y+1);
318     }