Mercurial > pidgin
comparison finch/libgnt/gntfilesel.c @ 15846:a2ab257116ce
File selector dialog. Still in an experimental state. When properly complete, this can be used for file/folder-request etc.
author | Sadrul Habib Chowdhury <imadil@gmail.com> |
---|---|
date | Tue, 20 Mar 2007 01:16:35 +0000 |
parents | |
children | 3da9d5da9054 |
comparison
equal
deleted
inserted
replaced
15840:c4f348500fa7 | 15846:a2ab257116ce |
---|---|
1 #include "gntbutton.h" | |
2 #include "gntentry.h" | |
3 #include "gntfilesel.h" | |
4 #include "gntlabel.h" | |
5 #include "gntmarshal.h" | |
6 #include "gntstyle.h" | |
7 #include "gnttree.h" | |
8 | |
9 #include <string.h> | |
10 #include <sys/types.h> | |
11 #include <sys/stat.h> | |
12 #include <unistd.h> | |
13 | |
14 #include <glob.h> | |
15 | |
16 enum | |
17 { | |
18 SIG_FILE_SELECTED, | |
19 SIGS | |
20 }; | |
21 | |
22 static GntWindowClass *parent_class = NULL; | |
23 static guint signals[SIGS] = { 0 }; | |
24 static void (*orig_map)(GntWidget *widget); | |
25 | |
26 static void | |
27 gnt_file_sel_destroy(GntWidget *widget) | |
28 { | |
29 GntFileSel *sel = GNT_FILE_SEL(widget); | |
30 g_free(sel->current); | |
31 } | |
32 | |
33 static char * | |
34 process_path(const char *path) | |
35 { | |
36 char **splits = NULL; | |
37 int i, j; | |
38 char *str, *ret; | |
39 | |
40 splits = g_strsplit(path, G_DIR_SEPARATOR_S, -1); | |
41 for (i = 0, j = 0; splits[i]; i++) { | |
42 if (strcmp(splits[i], ".") == 0) { | |
43 } else if (strcmp(splits[i], "..") == 0) { | |
44 if (j) | |
45 j--; | |
46 } else { | |
47 if (i != j) { | |
48 g_free(splits[j]); | |
49 splits[j] = splits[i]; | |
50 splits[i] = NULL; | |
51 } | |
52 j++; | |
53 } | |
54 } | |
55 g_free(splits[j]); | |
56 splits[j] = NULL; | |
57 str = g_build_pathv(G_DIR_SEPARATOR_S, splits); | |
58 ret = g_strdup_printf(G_DIR_SEPARATOR_S "%s", str); | |
59 g_free(str); | |
60 g_strfreev(splits); | |
61 return ret; | |
62 } | |
63 | |
64 static void | |
65 update_location(GntFileSel *sel) | |
66 { | |
67 char *old; | |
68 const char *tmp; | |
69 tmp = (const char*)gnt_tree_get_selection_data(GNT_TREE(sel->files)); | |
70 old = g_strdup_printf("%s%s%s", sel->current, sel->current[1] ? G_DIR_SEPARATOR_S : "", tmp ? tmp : ""); | |
71 gnt_entry_set_text(GNT_ENTRY(sel->location), old); | |
72 g_free(old); | |
73 } | |
74 | |
75 static gboolean | |
76 location_changed(GntFileSel *sel, GError **err) | |
77 { | |
78 GDir *dir; | |
79 const char *str; | |
80 | |
81 gnt_tree_remove_all(GNT_TREE(sel->dirs)); | |
82 gnt_tree_remove_all(GNT_TREE(sel->files)); | |
83 gnt_entry_set_text(GNT_ENTRY(sel->location), NULL); | |
84 if (sel->current == NULL) { | |
85 if (GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(sel), GNT_WIDGET_MAPPED)) | |
86 gnt_widget_draw(GNT_WIDGET(sel)); | |
87 return TRUE; | |
88 } | |
89 | |
90 dir = g_dir_open(sel->current, 0, err); | |
91 if (dir == NULL || *err) { | |
92 g_printerr("GntFileSel: error opening location %s (%s)\n", | |
93 sel->current, *err ? (*err)->message : "reason unknown"); | |
94 return FALSE; | |
95 } | |
96 | |
97 if (*sel->current != '\0' && strcmp(sel->current, G_DIR_SEPARATOR_S)) | |
98 gnt_tree_add_row_after(GNT_TREE(sel->dirs), g_strdup(".."), | |
99 gnt_tree_create_row(GNT_TREE(sel->dirs), ".."), NULL, NULL); | |
100 | |
101 while ((str = g_dir_read_name(dir)) != NULL) { | |
102 char *fp = g_build_filename(sel->current, str, NULL); | |
103 struct stat st; | |
104 | |
105 if (stat(fp, &st)) { | |
106 g_printerr("Error stating location %s\n", fp); | |
107 } else { | |
108 if (S_ISDIR(st.st_mode)) | |
109 gnt_tree_add_row_after(GNT_TREE(sel->dirs), g_strdup(str), | |
110 gnt_tree_create_row(GNT_TREE(sel->dirs), str), NULL, NULL); | |
111 else { | |
112 char size[128]; | |
113 snprintf(size, sizeof(size), "%ld", (long)st.st_size); | |
114 | |
115 gnt_tree_add_row_after(GNT_TREE(sel->files), g_strdup(str), | |
116 gnt_tree_create_row(GNT_TREE(sel->files), str, size, ""), NULL, NULL); | |
117 } | |
118 } | |
119 g_free(fp); | |
120 } | |
121 if (GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(sel), GNT_WIDGET_MAPPED)) | |
122 gnt_widget_draw(GNT_WIDGET(sel)); | |
123 return TRUE; | |
124 } | |
125 | |
126 static gboolean | |
127 dir_key_pressed(GntTree *tree, const char *key, GntFileSel *sel) | |
128 { | |
129 if (strcmp(key, "\r") == 0) { | |
130 /* XXX: if we are moving up the tree, make sure the current node is selected after the redraw */ | |
131 char *str = g_strdup(gnt_tree_get_selection_data(tree)); | |
132 char *path = g_build_filename(sel->current, str, NULL); | |
133 char *dir = g_path_get_basename(sel->current); | |
134 if (!gnt_file_sel_set_current_location(sel, path)) { | |
135 gnt_tree_set_selected(tree, str); | |
136 } else if (strcmp(str, "..") == 0) { | |
137 gnt_tree_set_selected(tree, dir); | |
138 } | |
139 g_free(dir); | |
140 g_free(str); | |
141 g_free(path); | |
142 return TRUE; | |
143 } | |
144 return FALSE; | |
145 } | |
146 | |
147 static gboolean | |
148 location_key_pressed(GntTree *tree, const char *key, GntFileSel *sel) | |
149 { | |
150 if (strcmp(key, "\r") == 0) { | |
151 int count; | |
152 glob_t gl; | |
153 char *path; | |
154 char *str; | |
155 struct stat st; | |
156 int glob_ret; | |
157 | |
158 str = (char*)gnt_entry_get_text(GNT_ENTRY(sel->location)); | |
159 if (*str == G_DIR_SEPARATOR) | |
160 path = g_strdup(str); | |
161 else | |
162 path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", sel->current, str); | |
163 str = process_path(path); | |
164 g_free(path); | |
165 path = str; | |
166 | |
167 if (!stat(path, &st)) { | |
168 if (S_ISDIR(st.st_mode)) { | |
169 gnt_file_sel_set_current_location(sel, path); | |
170 goto success; | |
171 } | |
172 } | |
173 | |
174 glob_ret = glob(path, GLOB_MARK, NULL, &gl); | |
175 if (!glob_ret) { /* XXX: do something with the return value */ | |
176 char *loc = g_path_get_dirname(gl.gl_pathv[0]); | |
177 | |
178 stat(gl.gl_pathv[0], &st); | |
179 gnt_file_sel_set_current_location(sel, loc); /* XXX: check the return value */ | |
180 g_free(loc); | |
181 if (!S_ISDIR(st.st_mode)) { | |
182 gnt_tree_remove_all(GNT_TREE(sel->files)); | |
183 for (count = 0; count < gl.gl_pathc; count++) { | |
184 char *tmp = process_path(gl.gl_pathv[count]); | |
185 loc = g_path_get_dirname(tmp); | |
186 if (g_utf8_collate(sel->current, loc) == 0) { | |
187 char *base = g_path_get_basename(tmp); | |
188 char size[128]; | |
189 snprintf(size, sizeof(size), "%ld", (long)st.st_size); | |
190 gnt_tree_add_row_after(GNT_TREE(sel->files), base, | |
191 gnt_tree_create_row(GNT_TREE(sel->files), base, size, ""), NULL, NULL); | |
192 } | |
193 g_free(loc); | |
194 g_free(tmp); | |
195 } | |
196 gnt_widget_draw(sel->files); | |
197 } | |
198 } else { | |
199 gnt_tree_remove_all(GNT_TREE(sel->files)); | |
200 gnt_widget_draw(sel->files); | |
201 } | |
202 globfree(&gl); | |
203 success: | |
204 g_free(path); | |
205 return TRUE; | |
206 } | |
207 return FALSE; | |
208 } | |
209 | |
210 static void | |
211 file_sel_changed(GntWidget *widget, gpointer old, gpointer current, GntFileSel *sel) | |
212 { | |
213 update_location(sel); | |
214 } | |
215 | |
216 static void | |
217 gnt_file_sel_map(GntWidget *widget) | |
218 { | |
219 orig_map(widget); | |
220 update_location(GNT_FILE_SEL(widget)); | |
221 } | |
222 | |
223 static void | |
224 gnt_file_sel_class_init(GntFileSelClass *klass) | |
225 { | |
226 GntWidgetClass *kl = GNT_WIDGET_CLASS(klass); | |
227 parent_class = GNT_WINDOW_CLASS(klass); | |
228 kl->destroy = gnt_file_sel_destroy; | |
229 orig_map = kl->map; | |
230 kl->map = gnt_file_sel_map; | |
231 | |
232 signals[SIG_FILE_SELECTED] = | |
233 g_signal_new("file_selected", | |
234 G_TYPE_FROM_CLASS(klass), | |
235 G_SIGNAL_RUN_LAST, | |
236 G_STRUCT_OFFSET(GntFileSelClass, file_selected), | |
237 NULL, NULL, | |
238 gnt_closure_marshal_VOID__STRING_STRING, | |
239 G_TYPE_NONE, 0); | |
240 gnt_style_read_actions(G_OBJECT_CLASS_TYPE(klass), GNT_BINDABLE_CLASS(klass)); | |
241 | |
242 GNTDEBUG; | |
243 } | |
244 | |
245 static void | |
246 gnt_file_sel_init(GTypeInstance *instance, gpointer class) | |
247 { | |
248 GNTDEBUG; | |
249 } | |
250 | |
251 /****************************************************************************** | |
252 * GntFileSel API | |
253 *****************************************************************************/ | |
254 GType | |
255 gnt_file_sel_get_gtype(void) | |
256 { | |
257 static GType type = 0; | |
258 | |
259 if(type == 0) | |
260 { | |
261 static const GTypeInfo info = { | |
262 sizeof(GntFileSelClass), | |
263 NULL, /* base_init */ | |
264 NULL, /* base_finalize */ | |
265 (GClassInitFunc)gnt_file_sel_class_init, | |
266 NULL, /* class_finalize */ | |
267 NULL, /* class_data */ | |
268 sizeof(GntFileSel), | |
269 0, /* n_preallocs */ | |
270 gnt_file_sel_init, /* instance_init */ | |
271 NULL | |
272 }; | |
273 | |
274 type = g_type_register_static(GNT_TYPE_WINDOW, | |
275 "GntFileSel", | |
276 &info, 0); | |
277 } | |
278 | |
279 return type; | |
280 } | |
281 | |
282 GntWidget *gnt_file_sel_new() | |
283 { | |
284 GntWidget *widget = g_object_new(GNT_TYPE_FILE_SEL, NULL); | |
285 GntFileSel *sel = GNT_FILE_SEL(widget); | |
286 GntWidget *hbox, *vbox; | |
287 | |
288 vbox = gnt_vbox_new(FALSE); | |
289 gnt_box_set_pad(GNT_BOX(vbox), 0); | |
290 gnt_box_set_alignment(GNT_BOX(vbox), GNT_ALIGN_LEFT); | |
291 | |
292 /* The dir. and files list */ | |
293 hbox = gnt_hbox_new(FALSE); | |
294 gnt_box_set_pad(GNT_BOX(hbox), 0); | |
295 | |
296 sel->dirs = gnt_tree_new(); | |
297 gnt_tree_set_compare_func(GNT_TREE(sel->dirs), (GCompareFunc)g_utf8_collate); | |
298 gnt_tree_set_hash_fns(GNT_TREE(sel->dirs), g_str_hash, g_str_equal, g_free); | |
299 gnt_tree_set_column_titles(GNT_TREE(sel->dirs), "Directories"); | |
300 gnt_tree_set_show_title(GNT_TREE(sel->dirs), TRUE); | |
301 gnt_tree_set_col_width(GNT_TREE(sel->dirs), 0, 20); | |
302 g_signal_connect(G_OBJECT(sel->dirs), "key_pressed", G_CALLBACK(dir_key_pressed), sel); | |
303 | |
304 sel->files = gnt_tree_new_with_columns(2); /* Name, Size, Modified */ | |
305 gnt_tree_set_compare_func(GNT_TREE(sel->files), (GCompareFunc)g_utf8_collate); | |
306 /*gnt_tree_set_column_titles(GNT_TREE(sel->files), "Filename", "Size", "Modified");*/ | |
307 gnt_tree_set_column_titles(GNT_TREE(sel->files), "Filename", "Size"); | |
308 gnt_tree_set_show_title(GNT_TREE(sel->files), TRUE); | |
309 gnt_tree_set_col_width(GNT_TREE(sel->files), 0, 25); | |
310 gnt_tree_set_col_width(GNT_TREE(sel->files), 1, 10); | |
311 /*gnt_tree_set_col_width(GNT_TREE(sel->files), 2, 10);*/ | |
312 g_signal_connect(G_OBJECT(sel->files), "selection_changed", G_CALLBACK(file_sel_changed), sel); | |
313 | |
314 gnt_box_add_widget(GNT_BOX(hbox), sel->dirs); | |
315 gnt_box_add_widget(GNT_BOX(hbox), sel->files); | |
316 gnt_box_add_widget(GNT_BOX(vbox), hbox); | |
317 | |
318 /* The location entry */ | |
319 sel->location = gnt_entry_new(NULL); | |
320 gnt_box_add_widget(GNT_BOX(vbox), sel->location); | |
321 g_signal_connect(G_OBJECT(sel->location), "key_pressed", G_CALLBACK(location_key_pressed), sel); | |
322 | |
323 /* The buttons */ | |
324 hbox = gnt_hbox_new(FALSE); | |
325 sel->cancel = gnt_button_new("Cancel"); | |
326 sel->select = gnt_button_new("Select"); | |
327 gnt_box_add_widget(GNT_BOX(hbox), sel->cancel); | |
328 gnt_box_add_widget(GNT_BOX(hbox), sel->select); | |
329 gnt_box_add_widget(GNT_BOX(vbox), hbox); | |
330 | |
331 gnt_box_add_widget(GNT_BOX(sel), vbox); | |
332 return widget; | |
333 } | |
334 | |
335 gboolean gnt_file_sel_set_current_location(GntFileSel *sel, const char *path) | |
336 { | |
337 char *old; | |
338 GError *error = NULL; | |
339 gboolean ret = TRUE; | |
340 | |
341 old = sel->current; | |
342 sel->current = process_path(path); | |
343 if (!location_changed(sel, &error)) { | |
344 g_error_free(error); | |
345 error = NULL; | |
346 g_free(sel->current); | |
347 sel->current = old; | |
348 location_changed(sel, &error); | |
349 ret = FALSE; | |
350 } else | |
351 g_free(old); | |
352 | |
353 update_location(sel); | |
354 return ret; | |
355 } | |
356 |