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