Use g_build_filename() and G_DIR_SEPARATOR_S.
[geeqie.git] / src / view_dir_list.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13 #include "main.h"
14 #include "view_dir_list.h"
15
16 #include "dnd.h"
17 #include "dupe.h"
18 #include "filedata.h"
19 #include "layout.h"
20 #include "layout_image.h"
21 #include "layout_util.h"
22 #include "utilops.h"
23 #include "ui_bookmark.h"
24 #include "ui_fileops.h"
25 #include "ui_menu.h"
26 #include "ui_tree_edit.h"
27 #include "view_dir.h"
28
29 #include <gdk/gdkkeysyms.h> /* for keyboard values */
30
31
32 #define VDLIST_INFO(_vd_, _part_) (((ViewDirInfoList *)(_vd_->info))->_part_)
33
34
35 /*
36  *-----------------------------------------------------------------------------
37  * misc
38  *-----------------------------------------------------------------------------
39  */
40
41 gint vdlist_find_row(ViewDir *vd, FileData *fd, GtkTreeIter *iter)
42 {
43         GtkTreeModel *store;
44         gint valid;
45         gint row = 0;
46
47         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
48         valid = gtk_tree_model_get_iter_first(store, iter);
49         while (valid)
50                 {
51                 FileData *fd_n;
52                 gtk_tree_model_get(GTK_TREE_MODEL(store), iter, DIR_COLUMN_POINTER, &fd_n, -1);
53                 if (fd_n == fd) return row;
54
55                 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), iter);
56                 row++;
57                 }
58
59         return -1;
60 }
61
62
63 FileData *vdlist_row_by_path(ViewDir *vd, const gchar *path, gint *row)
64 {
65         GList *work;
66         gint n;
67
68         if (!path)
69                 {
70                 if (row) *row = -1;
71                 return NULL;
72                 }
73
74         n = 0;
75         work = VDLIST_INFO(vd, list);
76         while (work)
77                 {
78                 FileData *fd = work->data;
79                 if (strcmp(fd->path, path) == 0)
80                         {
81                         if (row) *row = n;
82                         return fd;
83                         }
84                 work = work->next;
85                 n++;
86                 }
87
88         if (row) *row = -1;
89         return NULL;
90 }
91
92 /*
93  *-----------------------------------------------------------------------------
94  * dnd
95  *-----------------------------------------------------------------------------
96  */
97
98 static void vdlist_scroll_to_row(ViewDir *vd, FileData *fd, gfloat y_align)
99 {
100         GtkTreeIter iter;
101
102         if (GTK_WIDGET_REALIZED(vd->view) && vd_find_row(vd, fd, &iter) >= 0)
103                 {
104                 GtkTreeModel *store;
105                 GtkTreePath *tpath;
106
107                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
108                 tpath = gtk_tree_model_get_path(store, &iter);
109                 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(vd->view), tpath, NULL, TRUE, y_align, 0.0);
110                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(vd->view), tpath, NULL, FALSE);
111                 gtk_tree_path_free(tpath);
112
113                 if (!GTK_WIDGET_HAS_FOCUS(vd->view)) gtk_widget_grab_focus(vd->view);
114                 }
115 }
116
117 /*
118  *-----------------------------------------------------------------------------
119  * main
120  *-----------------------------------------------------------------------------
121  */
122
123 void vdlist_select_row(ViewDir *vd, FileData *fd)
124 {
125         if (fd && vd->select_func)
126                 {
127                 gchar *path;
128
129                 path = g_strdup(fd->path);
130                 vd->select_func(vd, path, vd->select_data);
131                 g_free(path);
132                 }
133 }
134
135 const gchar *vdlist_row_get_path(ViewDir *vd, gint row)
136 {
137         FileData *fd;
138
139         fd = g_list_nth_data(VDLIST_INFO(vd, list), row);
140
141         if (fd) return fd->path;
142
143         return NULL;
144 }
145
146 static void vdlist_populate(ViewDir *vd)
147 {
148         GtkListStore *store;
149         GList *work;
150
151         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view)));
152         gtk_list_store_clear(store);
153
154         work = VDLIST_INFO(vd, list);
155         while (work)
156                 {
157                 FileData *fd;
158                 GtkTreeIter iter;
159                 GdkPixbuf *pixbuf;
160
161                 fd = work->data;
162
163                 if (access_file(fd->path, R_OK | X_OK) && fd->name)
164                         {
165                         if (fd->name[0] == '.' && fd->name[1] == '\0')
166                                 {
167                                 pixbuf = vd->pf->open;
168                                 }
169                         else if (fd->name[0] == '.' && fd->name[1] == '.' && fd->name[2] == '\0')
170                                 {
171                                 pixbuf = vd->pf->parent;
172                                 }
173                         else
174                                 {
175                                 pixbuf = vd->pf->close;
176                                 }
177                         }
178                 else
179                         {
180                         pixbuf = vd->pf->deny;
181                         }
182
183                 gtk_list_store_append(store, &iter);
184                 gtk_list_store_set(store, &iter,
185                                    DIR_COLUMN_POINTER, fd,
186                                    DIR_COLUMN_ICON, pixbuf,
187                                    DIR_COLUMN_NAME, fd->name, -1);
188
189                 work = work->next;
190                 }
191
192         vd->click_fd = NULL;
193         vd->drop_fd = NULL;
194 }
195
196 gint vdlist_set_path(ViewDir *vd, const gchar *path)
197 {
198         gint ret;
199         FileData *fd;
200         gchar *old_path = NULL;
201         gchar *filepath;
202
203         if (!path) return FALSE;
204         if (vd->path && strcmp(path, vd->path) == 0) return TRUE;
205
206         if (vd->path)
207                 {
208                 gchar *base;
209
210                 base = remove_level_from_path(vd->path);
211                 if (strcmp(base, path) == 0)
212                         {
213                         old_path = g_strdup(filename_from_path(vd->path));
214                         }
215                 g_free(base);
216                 }
217
218         g_free(vd->path);
219         vd->path = g_strdup(path);
220
221         filelist_free(VDLIST_INFO(vd, list));
222         VDLIST_INFO(vd, list) = NULL;
223
224         ret = filelist_read(vd->path, NULL, &VDLIST_INFO(vd, list));
225
226         VDLIST_INFO(vd, list) = filelist_sort(VDLIST_INFO(vd, list), SORT_NAME, TRUE);
227
228         /* add . and .. */
229
230         if (strcmp(vd->path, G_DIR_SEPARATOR_S) != 0)
231                 {
232                 filepath = g_build_filename(vd->path, "..", NULL);
233                 fd = file_data_new_simple(filepath);
234                 VDLIST_INFO(vd, list) = g_list_prepend(VDLIST_INFO(vd, list), fd);
235                 g_free(filepath);
236                 }
237
238         if (options->file_filter.show_dot_directory)
239                 {
240                 filepath = g_build_filename(vd->path, ".", NULL);
241                 fd = file_data_new_simple(filepath);
242                 VDLIST_INFO(vd, list) = g_list_prepend(VDLIST_INFO(vd, list), fd);
243                 g_free(filepath);
244         }
245
246         vdlist_populate(vd);
247
248         if (old_path)
249                 {
250                 /* scroll to make last path visible */
251                 FileData *found = NULL;
252                 GList *work;
253
254                 work = VDLIST_INFO(vd, list);
255                 while (work && !found)
256                         {
257                         FileData *fd = work->data;
258                         if (strcmp(old_path, fd->name) == 0) found = fd;
259                         work = work->next;
260                         }
261
262                 if (found) vdlist_scroll_to_row(vd, found, 0.5);
263
264                 g_free(old_path);
265                 return ret;
266                 }
267
268         if (GTK_WIDGET_REALIZED(vd->view))
269                 {
270                 gtk_tree_view_scroll_to_point(GTK_TREE_VIEW(vd->view), 0, 0);
271                 }
272
273         return ret;
274 }
275
276 void vdlist_refresh(ViewDir *vd)
277 {
278         gchar *path;
279
280         path = g_strdup(vd->path);
281         vd->path = NULL;
282         vdlist_set_path(vd, path);
283         g_free(path);
284 }
285
286 gint vdlist_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
287 {
288         ViewDir *vd = data;
289         GtkTreePath *tpath;
290
291         if (event->keyval != GDK_Menu) return FALSE;
292
293         gtk_tree_view_get_cursor(GTK_TREE_VIEW(vd->view), &tpath, NULL);
294         if (tpath)
295                 {
296                 GtkTreeModel *store;
297                 GtkTreeIter iter;
298
299                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
300                 gtk_tree_model_get_iter(store, &iter, tpath);
301                 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &vd->click_fd, -1);
302
303                 gtk_tree_path_free(tpath);
304                 }
305         else
306                 {
307                 vd->click_fd = NULL;
308                 }
309
310         vd_color_set(vd, vd->click_fd, TRUE);
311
312         vd->popup = vd_pop_menu(vd, vd->click_fd);
313
314         gtk_menu_popup(GTK_MENU(vd->popup), NULL, NULL, vd_menu_position_cb, vd, 0, GDK_CURRENT_TIME);
315
316         return TRUE;
317 }
318
319 gint vdlist_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
320 {
321         ViewDir *vd = data;
322         GtkTreePath *tpath;
323         GtkTreeIter iter;
324         FileData *fd = NULL;
325
326         if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y,
327                                           &tpath, NULL, NULL, NULL))
328                 {
329                 GtkTreeModel *store;
330
331                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
332                 gtk_tree_model_get_iter(store, &iter, tpath);
333                 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1);
334                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(widget), tpath, NULL, FALSE);
335                 gtk_tree_path_free(tpath);
336                 }
337
338         vd->click_fd = fd;
339         vd_color_set(vd, vd->click_fd, TRUE);
340
341         if (bevent->button == MOUSE_BUTTON_RIGHT)
342                 {
343                 vd->popup = vd_pop_menu(vd, vd->click_fd);
344                 gtk_menu_popup(GTK_MENU(vd->popup), NULL, NULL, NULL, NULL,
345                                bevent->button, bevent->time);
346                 }
347
348         return TRUE;
349 }
350
351 void vdlist_destroy_cb(GtkWidget *widget, gpointer data)
352 {
353         ViewDir *vd = data;
354
355         vd_dnd_drop_scroll_cancel(vd);
356         widget_auto_scroll_stop(vd->view);
357
358         filelist_free(VDLIST_INFO(vd, list));
359 }
360
361 ViewDir *vdlist_new(ViewDir *vd, const gchar *path)
362 {
363         GtkListStore *store;
364         GtkTreeSelection *selection;
365         GtkTreeViewColumn *column;
366         GtkCellRenderer *renderer;
367
368         vd->info = g_new0(ViewDirInfoList, 1);
369         vd->type = DIRVIEW_LIST;
370
371         VDLIST_INFO(vd, list) = NULL;
372
373         store = gtk_list_store_new(4, G_TYPE_POINTER, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_BOOLEAN);
374         vd->view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
375         g_object_unref(store);
376
377         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(vd->view), FALSE);
378         gtk_tree_view_set_enable_search(GTK_TREE_VIEW(vd->view), FALSE);
379
380         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vd->view));
381         gtk_tree_selection_set_mode(selection, GTK_SELECTION_NONE);
382
383         column = gtk_tree_view_column_new();
384         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
385
386         renderer = gtk_cell_renderer_pixbuf_new();
387         gtk_tree_view_column_pack_start(column, renderer, FALSE);
388         gtk_tree_view_column_add_attribute(column, renderer, "pixbuf", DIR_COLUMN_ICON);
389         gtk_tree_view_column_set_cell_data_func(column, renderer, vd_color_cb, vd, NULL);
390
391         renderer = gtk_cell_renderer_text_new();
392         gtk_tree_view_column_pack_start(column, renderer, TRUE);
393         gtk_tree_view_column_add_attribute(column, renderer, "text", DIR_COLUMN_NAME);
394         gtk_tree_view_column_set_cell_data_func(column, renderer, vd_color_cb, vd, NULL);
395
396         gtk_tree_view_append_column(GTK_TREE_VIEW(vd->view), column);
397
398         return vd;
399 }