Contents of /trunk/src/misc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations)
Tue Dec 9 20:06:06 2008 UTC (15 years, 5 months ago) by harbaum
Original Path: src/misc.c
File MIME type: text/plain
File size: 5720 byte(s)
Initial import
1 /*
2 * This file is part of OSM2Go.
3 *
4 * OSM2Go is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * OSM2Go is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with OSM2Go. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "appdata.h"
19
20 void messagef(GtkWidget *parent, char *title, const char *fmt, ...) {
21 va_list args;
22 va_start( args, fmt );
23 char *buf = g_strdup_vprintf(fmt, args);
24 va_end( args );
25
26 printf("%s: \"%s\"\n", title, buf);
27
28 GtkWidget *dialog = gtk_message_dialog_new(
29 GTK_WINDOW(parent),
30 GTK_DIALOG_DESTROY_WITH_PARENT,
31 GTK_MESSAGE_INFO, GTK_BUTTONS_OK,
32 buf);
33
34 gtk_window_set_title(GTK_WINDOW(dialog), title);
35
36 gtk_dialog_run(GTK_DIALOG(dialog));
37 gtk_widget_destroy(dialog);
38
39 g_free(buf);
40 }
41
42 static void on_toggled(GtkWidget *button, gpointer data) {
43 gboolean active =
44 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
45
46 GtkWidget *dialog = gtk_widget_get_toplevel(button);
47
48 if(*(gint*)data & MISC_AGAIN_FLAG_DONT_SAVE_NO)
49 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog),
50 GTK_RESPONSE_NO, !active);
51
52 if(*(gint*)data & MISC_AGAIN_FLAG_DONT_SAVE_YES)
53 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog),
54 GTK_RESPONSE_YES, !active);
55 }
56
57 gboolean yes_no_f(GtkWidget *parent, appdata_t *appdata, gulong again_bit,
58 gint flags, char *title, const char *fmt, ...) {
59
60 if(appdata && again_bit && (appdata->dialog_again.not & again_bit))
61 return((appdata->dialog_again.reply & again_bit) != 0);
62
63 va_list args;
64 va_start( args, fmt );
65 char *buf = g_strdup_vprintf(fmt, args);
66 va_end( args );
67
68 printf("%s: \"%s\"\n", title, buf);
69
70 GtkWidget *dialog = gtk_message_dialog_new(
71 GTK_WINDOW(parent),
72 GTK_DIALOG_DESTROY_WITH_PARENT,
73 GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
74 buf);
75
76 gtk_window_set_title(GTK_WINDOW(dialog), title);
77
78 GtkWidget *cbut = NULL;
79 if(appdata && again_bit) {
80 GtkWidget *alignment = gtk_alignment_new(0.5, 0, 0, 0);
81
82 cbut = gtk_check_button_new_with_label(
83 _("Don't ask this question again this session"));
84 g_signal_connect(cbut, "toggled", G_CALLBACK(on_toggled), &flags);
85
86 gtk_container_add(GTK_CONTAINER(alignment), cbut);
87 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog)->vbox), alignment);
88
89 gtk_widget_show_all(dialog);
90 }
91
92 gboolean yes = (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_YES);
93
94 if(cbut && gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cbut))) {
95 /* the user doesn't want to see this dialog again */
96
97 appdata->dialog_again.not |= again_bit;
98 if(yes) appdata->dialog_again.reply |= again_bit;
99 else appdata->dialog_again.reply &= ~again_bit;
100 }
101
102 gtk_widget_destroy(dialog);
103
104 g_free(buf);
105 return yes;
106 }
107
108 void errorf(GtkWidget *parent, const char *fmt, ...) {
109 va_list args;
110 va_start( args, fmt );
111 char *buf = g_strdup_vprintf(fmt, args);
112 va_end( args );
113
114 printf("errorf(\"%s\")\n", buf);
115
116 GtkWidget *dialog = gtk_message_dialog_new(
117 GTK_WINDOW(parent),
118 GTK_DIALOG_DESTROY_WITH_PARENT,
119 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
120 buf);
121
122 gtk_window_set_title(GTK_WINDOW(dialog), _("ERROR"));
123
124 gtk_dialog_run(GTK_DIALOG(dialog));
125 gtk_widget_destroy(dialog);
126
127 g_free(buf);
128 }
129
130 static const char *data_paths[] = {
131 "/usr/share/" PACKAGE , // final installation path
132 #ifdef USE_HILDON
133 "/media/mmc1/" PACKAGE, // path to external memory card
134 "/media/mmc2/" PACKAGE, // path to internal memory card
135 #endif
136 "./data", "../data", // local paths for testing
137 "~/." PACKAGE, // in home directory
138 NULL
139 };
140
141 char *find_file(char *name) {
142 const char **path = data_paths;
143 char *p = getenv("HOME");
144
145 while(*path) {
146 char *full_path = NULL;
147
148 if(*path[0] == '~')
149 full_path = g_strdup_printf("%s/%s/%s", p, *path+2, name);
150 else
151 full_path = g_strdup_printf("%s/%s", *path, name);
152
153 if(g_file_test(full_path, G_FILE_TEST_IS_REGULAR))
154 return full_path;
155
156 g_free(full_path);
157 path++;
158 }
159
160 return NULL;
161 }
162
163 /* scan all data directories for the given file pattern and */
164 /* return a list of files matching this pattern */
165 file_chain_t *file_scan(char *pattern) {
166 file_chain_t *chain = NULL, **chainP = &chain;
167
168 const char **path = data_paths;
169 char *p = getenv("HOME");
170
171 while(*path) {
172 GDir *dir = NULL;
173
174 /* scan for projects */
175 const char *dirname = *path;
176
177 if(*path[0] == '~')
178 dirname = g_strdup_printf("%s/%s", p, *path+2);
179
180 if((dir = g_dir_open(dirname, 0, NULL))) {
181 const char *name = NULL;
182 do {
183 name = g_dir_read_name(dir);
184
185 if(name) {
186 char *fullname = g_strdup_printf("%s/%s", dirname, name);
187 if(g_file_test(fullname, G_FILE_TEST_IS_REGULAR)) {
188 if(g_pattern_match_simple(pattern, name)) {
189 *chainP = g_new0(file_chain_t, 1);
190 (*chainP)->name = fullname;
191 chainP = &(*chainP)->next;
192 } else
193 g_free(fullname);
194 } else
195 g_free(fullname);
196 }
197 } while(name);
198
199 g_dir_close(dir);
200
201 if(*path[0] == '~')
202 g_free((char*)dirname);
203 }
204
205 path++;
206 }
207
208 return chain;
209 }