Cleanup main.h header
[geeqie.git] / src / view-dir-list.cc
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "view-dir-list.h"
23
24 #include <config.h>
25
26 #include "filedata.h"
27 #include "layout.h"
28 #include "ui-fileops.h"
29 #include "ui-tree-edit.h"
30 #include "view-dir.h"
31
32 struct ViewDirInfoList
33 {
34         GList *list;
35 };
36
37 #define VDLIST(_vd_) ((ViewDirInfoList *)((_vd_)->info))
38
39
40 /*
41  *-----------------------------------------------------------------------------
42  * misc
43  *-----------------------------------------------------------------------------
44  */
45
46 gboolean vdlist_find_row(ViewDir *vd, FileData *fd, GtkTreeIter *iter)
47 {
48         GtkTreeModel *store;
49         gboolean valid;
50
51         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
52         valid = gtk_tree_model_get_iter_first(store, iter);
53         while (valid)
54                 {
55                 FileData *fd_n;
56                 gtk_tree_model_get(GTK_TREE_MODEL(store), iter, DIR_COLUMN_POINTER, &fd_n, -1);
57                 if (fd_n == fd) return TRUE;
58
59                 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), iter);
60                 }
61
62         return FALSE;
63 }
64
65
66 FileData *vdlist_row_by_path(ViewDir *vd, const gchar *path, gint *row)
67 {
68         GList *work;
69         gint n;
70
71         if (!path)
72                 {
73                 if (row) *row = -1;
74                 return nullptr;
75                 }
76
77         n = 0;
78         work = VDLIST(vd)->list;
79         while (work)
80                 {
81                 auto fd = static_cast<FileData *>(work->data);
82                 if (strcmp(fd->path, path) == 0)
83                         {
84                         if (row) *row = n;
85                         return fd;
86                         }
87                 work = work->next;
88                 n++;
89                 }
90
91         if (row) *row = -1;
92         return nullptr;
93 }
94
95 /*
96  *-----------------------------------------------------------------------------
97  * dnd
98  *-----------------------------------------------------------------------------
99  */
100
101 static void vdlist_scroll_to_row(ViewDir *vd, FileData *fd, gfloat y_align)
102 {
103         GtkTreeIter iter;
104
105         if (gtk_widget_get_realized(vd->view) && vd_find_row(vd, fd, &iter))
106                 {
107                 GtkTreeModel *store;
108                 GtkTreePath *tpath;
109
110                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
111                 tpath = gtk_tree_model_get_path(store, &iter);
112                 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(vd->view), tpath, nullptr, TRUE, y_align, 0.0);
113                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(vd->view), tpath, nullptr, FALSE);
114                 gtk_tree_path_free(tpath);
115
116                 if (!gtk_widget_has_focus(vd->view)) gtk_widget_grab_focus(vd->view);
117                 }
118 }
119
120 /*
121  *-----------------------------------------------------------------------------
122  * main
123  *-----------------------------------------------------------------------------
124  */
125
126 const gchar *vdlist_row_get_path(ViewDir *vd, gint row)
127 {
128         FileData *fd;
129
130         fd = static_cast<FileData *>(g_list_nth_data(VDLIST(vd)->list, row));
131
132         if (fd) return fd->path;
133
134         return nullptr;
135 }
136
137 static gboolean vdlist_populate(ViewDir *vd, gboolean clear)
138 {
139         GtkListStore *store;
140         GList *work;
141         GtkTreeIter iter;
142         gboolean valid;
143         gchar *filepath;
144         GList *old_list;
145         gboolean ret;
146         FileData *fd;
147         SortType sort_type = SORT_NAME;
148         gboolean sort_ascend = TRUE;
149         gboolean sort_case = TRUE;
150         gchar *link = nullptr;
151
152         if (vd->layout)
153                 {
154                 sort_type = vd->layout->options.dir_view_list_sort.method;
155                 sort_ascend = vd->layout->options.dir_view_list_sort.ascend;
156                 sort_case = vd->layout->options.dir_view_list_sort.case_sensitive;
157                 }
158
159         old_list = VDLIST(vd)->list;
160
161         ret = filelist_read(vd->dir_fd, nullptr, &VDLIST(vd)->list);
162         VDLIST(vd)->list = filelist_sort(VDLIST(vd)->list, sort_type, sort_ascend, sort_case);
163
164         /* add . and .. */
165
166         if (options->file_filter.show_parent_directory && strcmp(vd->dir_fd->path, G_DIR_SEPARATOR_S) != 0)
167                 {
168                 filepath = g_build_filename(vd->dir_fd->path, "..", NULL);
169                 fd = file_data_new_dir(filepath);
170                 VDLIST(vd)->list = g_list_prepend(VDLIST(vd)->list, fd);
171                 g_free(filepath);
172                 }
173
174         if (options->file_filter.show_dot_directory)
175                 {
176                 filepath = g_build_filename(vd->dir_fd->path, ".", NULL);
177                 fd = file_data_new_dir(filepath);
178                 VDLIST(vd)->list = g_list_prepend(VDLIST(vd)->list, fd);
179                 g_free(filepath);
180         }
181
182         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view)));
183         if (clear) gtk_list_store_clear(store);
184
185         valid = gtk_tree_model_iter_children(GTK_TREE_MODEL(store), &iter, nullptr);
186
187         work = VDLIST(vd)->list;
188         while (work)
189                 {
190                 gint match;
191                 GdkPixbuf *pixbuf;
192                 const gchar *date = "";
193                 gboolean done = FALSE;
194
195                 fd = static_cast<FileData *>(work->data);
196
197                 if (access_file(fd->path, R_OK | X_OK) && fd->name)
198                         {
199                         if (islink(fd->path))
200                                 {
201                                 pixbuf = vd->pf->link;
202                                 }
203                         else if (fd->name[0] == '.' && fd->name[1] == '\0')
204                                 {
205                                 pixbuf = vd->pf->open;
206                                 }
207                         else if (fd->name[0] == '.' && fd->name[1] == '.' && fd->name[2] == '\0')
208                                 {
209                                 pixbuf = vd->pf->parent;
210                                 }
211                         else if (!access_file(fd->path, W_OK) )
212                                 {
213                                 pixbuf = vd->pf->read_only;
214                                 }
215                         else
216                                 {
217                                 pixbuf = vd->pf->close;
218                                 if (vd->layout && vd->layout->options.show_directory_date)
219                                         date = text_from_time(fd->date);
220                                 }
221                         }
222                 else
223                         {
224                         pixbuf = vd->pf->deny;
225                         }
226
227                 while (!done)
228                         {
229                         FileData *old_fd = nullptr;
230
231                         if (valid)
232                                 {
233                                 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
234                                                    DIR_COLUMN_POINTER, &old_fd,
235                                                    -1);
236
237                                 if (fd == old_fd)
238                                         {
239                                         match = 0;
240                                         }
241                                 else
242                                         {
243                                         match = filelist_sort_compare_filedata_full(fd, old_fd, sort_type, sort_ascend);
244
245                                         if (match == 0) g_warning("multiple fd for the same path");
246                                         }
247
248                                 }
249                         else
250                                 {
251                                 match = -1;
252                                 }
253
254                         if (islink(fd->path))
255                                 {
256                                 link = realpath(fd->path, nullptr);
257                                 }
258                         else
259                                 {
260                                 link = nullptr;
261                                 }
262
263                         if (match < 0)
264                                 {
265                                 GtkTreeIter new_iter;
266
267                                 if (valid)
268                                         {
269                                         gtk_list_store_insert_before(store, &new_iter, &iter);
270                                         }
271                                 else
272                                         {
273                                         gtk_list_store_append(store, &new_iter);
274                                         }
275
276                                 gtk_list_store_set(store, &new_iter,
277                                                    DIR_COLUMN_POINTER, fd,
278                                                    DIR_COLUMN_ICON, pixbuf,
279                                                    DIR_COLUMN_NAME, fd->name,
280                                                    DIR_COLUMN_LINK, link,
281                                                    DIR_COLUMN_DATE, date,
282                                                    -1);
283
284                                 done = TRUE;
285                                 }
286                         else if (match > 0)
287                                 {
288                                 valid = gtk_list_store_remove(store, &iter);
289                                 }
290                         else
291                                 {
292                                 gtk_list_store_set(store, &iter,
293                                                    DIR_COLUMN_ICON, pixbuf,
294                                                    DIR_COLUMN_NAME, fd->name,
295                                                    DIR_COLUMN_LINK, link,
296                                                    DIR_COLUMN_DATE, date,
297                                                    -1);
298
299                                 if (valid) valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
300
301                                 done = TRUE;
302                                 }
303                         }
304                 work = work->next;
305                 }
306
307         while (valid)
308                 {
309                 FileData *old_fd;
310                 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DIR_COLUMN_POINTER, &old_fd, -1);
311
312                 valid = gtk_list_store_remove(store, &iter);
313                 }
314
315
316         vd->click_fd = nullptr;
317         vd->drop_fd = nullptr;
318
319         filelist_free(old_list);
320         g_free(link);
321         return ret;
322 }
323
324 gboolean vdlist_set_fd(ViewDir *vd, FileData *dir_fd)
325 {
326         gboolean ret;
327         gchar *old_path = nullptr; /* Used to store directory for walking up */
328
329         if (!dir_fd) return FALSE;
330         if (vd->dir_fd == dir_fd) return TRUE;
331
332         if (vd->dir_fd)
333                 {
334                 gchar *base;
335
336                 base = remove_level_from_path(vd->dir_fd->path);
337                 if (strcmp(base, dir_fd->path) == 0)
338                         {
339                         old_path = g_strdup(filename_from_path(vd->dir_fd->path));
340                         }
341                 g_free(base);
342                 }
343
344         file_data_unref(vd->dir_fd);
345         vd->dir_fd = file_data_ref(dir_fd);
346
347         ret = vdlist_populate(vd, TRUE);
348
349         /* scroll to make last path visible */
350         FileData *found = nullptr;
351         GList *work;
352
353         work = VDLIST(vd)->list;
354         while (work && !found)
355                 {
356                 auto fd = static_cast<FileData *>(work->data);
357                 if (!old_path || strcmp(old_path, fd->name) == 0) found = fd;
358                 work = work->next;
359                 }
360
361         if (found) vdlist_scroll_to_row(vd, found, 0.5);
362
363         if (old_path) g_free(old_path);
364
365         return ret;
366 }
367
368 void vdlist_refresh(ViewDir *vd)
369 {
370         vdlist_populate(vd, FALSE);
371 }
372
373 gboolean vdlist_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
374 {
375         auto vd = static_cast<ViewDir *>(data);
376         GtkTreePath *tpath;
377
378         if (event->keyval != GDK_KEY_Menu) return FALSE;
379
380         gtk_tree_view_get_cursor(GTK_TREE_VIEW(vd->view), &tpath, nullptr);
381         if (tpath)
382                 {
383                 GtkTreeModel *store;
384                 GtkTreeIter iter;
385
386                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
387                 gtk_tree_model_get_iter(store, &iter, tpath);
388                 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &vd->click_fd, -1);
389
390                 gtk_tree_path_free(tpath);
391                 }
392         else
393                 {
394                 vd->click_fd = nullptr;
395                 }
396
397         vd_color_set(vd, vd->click_fd, TRUE);
398
399         vd->popup = vd_pop_menu(vd, vd->click_fd);
400
401         gtk_menu_popup_at_pointer(GTK_MENU(vd->popup), nullptr);
402
403         return TRUE;
404 }
405
406 gboolean vdlist_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
407 {
408         auto vd = static_cast<ViewDir *>(data);
409         GtkTreePath *tpath;
410         GtkTreeIter iter;
411         FileData *fd = nullptr;
412
413         if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y,
414                                           &tpath, nullptr, nullptr, nullptr))
415                 {
416                 GtkTreeModel *store;
417
418                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
419                 gtk_tree_model_get_iter(store, &iter, tpath);
420                 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1);
421                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(widget), tpath, nullptr, FALSE);
422                 gtk_tree_path_free(tpath);
423                 }
424
425         vd->click_fd = fd;
426
427         if (options->view_dir_list_single_click_enter)
428                 vd_color_set(vd, vd->click_fd, TRUE);
429
430         if (bevent->button == MOUSE_BUTTON_RIGHT)
431                 {
432                 vd->popup = vd_pop_menu(vd, vd->click_fd);
433                 gtk_menu_popup_at_pointer(GTK_MENU(vd->popup), nullptr);
434                 return TRUE;
435                 }
436
437         return options->view_dir_list_single_click_enter;
438 }
439
440 void vdlist_destroy_cb(GtkWidget *, gpointer data)
441 {
442         auto vd = static_cast<ViewDir *>(data);
443
444         vd_dnd_drop_scroll_cancel(vd);
445         widget_auto_scroll_stop(vd->view);
446
447         filelist_free(VDLIST(vd)->list);
448 }
449
450 ViewDir *vdlist_new(ViewDir *vd, FileData *)
451 {
452         GtkListStore *store;
453         GtkTreeSelection *selection;
454         GtkTreeViewColumn *column;
455         GtkCellRenderer *renderer;
456
457         vd->info = g_new0(ViewDirInfoList, 1);
458
459         vd->type = DIRVIEW_LIST;
460
461         store = gtk_list_store_new(6, G_TYPE_POINTER, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_STRING);
462         vd->view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
463         g_object_unref(store);
464
465         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(vd->view), FALSE);
466         gtk_tree_view_set_enable_search(GTK_TREE_VIEW(vd->view), FALSE);
467
468         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vd->view));
469         gtk_tree_selection_set_mode(selection, GTK_SELECTION_NONE);
470
471         column = gtk_tree_view_column_new();
472         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
473
474         renderer = gtk_cell_renderer_pixbuf_new();
475         gtk_tree_view_column_pack_start(column, renderer, FALSE);
476         gtk_tree_view_column_add_attribute(column, renderer, "pixbuf", DIR_COLUMN_ICON);
477         gtk_tree_view_column_set_cell_data_func(column, renderer, vd_color_cb, vd, nullptr);
478
479         renderer = gtk_cell_renderer_text_new();
480         gtk_tree_view_column_pack_start(column, renderer, TRUE);
481         gtk_tree_view_column_add_attribute(column, renderer, "text", DIR_COLUMN_NAME);
482         gtk_tree_view_column_set_cell_data_func(column, renderer, vd_color_cb, vd, nullptr);
483
484         renderer = gtk_cell_renderer_text_new();
485         gtk_tree_view_column_pack_start(column, renderer, TRUE);
486         gtk_tree_view_column_add_attribute(column, renderer, "text", DIR_COLUMN_DATE);
487         gtk_tree_view_column_set_cell_data_func(column, renderer, vd_color_cb, vd, nullptr);
488
489         gtk_tree_view_append_column(GTK_TREE_VIEW(vd->view), column);
490
491         gtk_tree_view_set_tooltip_column(GTK_TREE_VIEW(vd->view), DIR_COLUMN_LINK);
492
493         return vd;
494 }
495 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */