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