clang=tidy: readability-uppercase-literal-suffix
[geeqie.git] / src / ui-bookmark.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 <cstring>
23 #include <memory>
24
25 #include "main.h"
26 #include "ui-bookmark.h"
27
28 #include "history-list.h"
29 #include "misc.h"
30 #include "pixbuf-util.h"
31 #include "ui-fileops.h"
32 #include "ui-menu.h"
33 #include "ui-misc.h"
34 #include "ui-tabcomp.h"
35 #include "ui-utildlg.h"
36 #include "uri-utils.h"
37
38 /*
39  *-----------------------------------------------------------------------------
40  * bookmarks
41  *-----------------------------------------------------------------------------
42  */
43
44 #define BOOKMARK_DATA_KEY "bookmarkdata"
45 #define MARKER_PATH "[path]"
46 #define MARKER_ICON "[icon]"
47
48 struct BookButtonData;
49
50 struct BookMarkData
51 {
52         GtkWidget *widget;
53         GtkWidget *box;
54         const gchar *key;
55
56         void (*select_func)(const gchar *path, gpointer data);
57         gpointer select_data;
58
59         gboolean no_defaults;
60         gboolean editable;
61         gboolean only_directories;
62
63         BookButtonData *active_button;
64 };
65
66 struct BookButtonData
67 {
68         GtkWidget *button;
69         GtkWidget *image;
70         GtkWidget *label;
71
72         gchar *key;
73         gchar *name;
74         gchar *path;
75         gchar *icon;
76         gchar *parent;
77 };
78
79 struct BookPropData
80 {
81         GtkWidget *name_entry;
82         GtkWidget *path_entry;
83         GtkWidget *icon_entry;
84
85         BookButtonData *bb;
86 };
87
88 enum {
89         TARGET_URI_LIST,
90         TARGET_X_URL,
91         TARGET_TEXT_PLAIN
92 };
93
94 static GtkTargetEntry bookmark_drop_types[] = {
95         { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST },
96         { const_cast<gchar *>("x-url/http"),    0, TARGET_X_URL },
97         { const_cast<gchar *>("_NETSCAPE_URL"), 0, TARGET_X_URL }
98 };
99 #define bookmark_drop_types_n 3
100
101 static GtkTargetEntry bookmark_drag_types[] = {
102         { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST },
103         { const_cast<gchar *>("text/plain"),    0, TARGET_TEXT_PLAIN }
104 };
105 #define bookmark_drag_types_n 2
106
107
108 static GList *bookmark_widget_list = nullptr;
109 static GList *bookmark_default_list = nullptr;
110
111
112 static void bookmark_populate_all(const gchar *key);
113
114
115 static BookButtonData *bookmark_from_string(const gchar *text)
116 {
117         BookButtonData *b;
118         const gchar *path_ptr;
119         const gchar *icon_ptr;
120
121         b = g_new0(BookButtonData, 1);
122
123         if (!text)
124                 {
125                 b->name = g_strdup(_("New Bookmark"));
126                 b->path = g_strdup(homedir());
127                 b->key = nullptr;
128                 return b;
129                 }
130
131         b->key = g_strdup(text);
132
133         path_ptr = strstr(text, MARKER_PATH);
134         icon_ptr = strstr(text, MARKER_ICON);
135
136         if (path_ptr && icon_ptr && icon_ptr < path_ptr)
137                 {
138                 log_printf("warning, bookmark icon must be after path\n");
139                 return nullptr;
140                 }
141
142         if (path_ptr)
143                 {
144                 gint l;
145
146                 l = path_ptr - text;
147                 b->name = g_strndup(text, l);
148                 path_ptr += strlen(MARKER_PATH);
149                 if (icon_ptr)
150                         {
151                         l = icon_ptr - path_ptr;
152                         b->path = g_strndup(path_ptr, l);
153                         }
154                 else
155                         {
156                         b->path = g_strdup(path_ptr);
157                         }
158                 }
159         else
160                 {
161                 b->name = g_strdup(text);
162                 b->path = g_strdup("");
163                 }
164
165         if (icon_ptr)
166                 {
167                 icon_ptr += strlen(MARKER_ICON);
168                 b->icon = g_strdup(icon_ptr);
169                 }
170
171         return b;
172 }
173
174 static void bookmark_free(BookButtonData *b)
175 {
176         if (!b) return;
177
178         g_free(b->name);
179         g_free(b->path);
180         g_free(b->icon);
181         g_free(b->key);
182         g_free(b->parent);
183         g_free(b);
184 }
185
186 static gchar *bookmark_string(const gchar *name, const gchar *path, const gchar *icon)
187 {
188         if (!name) name = _("New Bookmark");
189
190         if (icon)
191                 {
192                 return g_strdup_printf("%s" MARKER_PATH "%s" MARKER_ICON "%s", name, path, icon);
193                 }
194
195         return g_strdup_printf("%s" MARKER_PATH "%s", name, path);
196 }
197
198 static void bookmark_select_cb(GtkWidget *button, gpointer data)
199 {
200         auto bm = static_cast<BookMarkData *>(data);
201         BookButtonData *b;
202
203         b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
204         if (!b) return;
205
206         if (bm->select_func) bm->select_func(b->path, bm->select_data);
207 }
208
209 static void bookmark_edit_destroy_cb(GtkWidget *, gpointer data)
210 {
211         auto p = static_cast<BookPropData *>(data);
212
213         bookmark_free(p->bb);
214         g_free(p);
215 }
216
217 static void bookmark_edit_cancel_cb(GenericDialog *, gpointer)
218 {
219 }
220
221 static void bookmark_edit_ok_cb(GenericDialog *, gpointer data)
222 {
223         auto p = static_cast<BookPropData *>(data);
224         const gchar *name;
225         gchar *path;
226         const gchar *icon;
227         gchar *new_string;
228
229         name = gq_gtk_entry_get_text(GTK_ENTRY(p->name_entry));
230         path = remove_trailing_slash(gq_gtk_entry_get_text(GTK_ENTRY(p->path_entry)));
231         icon = gq_gtk_entry_get_text(GTK_ENTRY(p->icon_entry));
232
233         new_string = bookmark_string(name, path, icon);
234
235         if (p->bb->key)
236                 {
237                 history_list_item_change(p->bb->parent, p->bb->key, new_string);
238                 }
239         else
240                 {
241                 history_list_add_to_key(p->bb->parent, new_string, 0);
242                 }
243
244         if (path && strlen(path) > 0) tab_completion_append_to_history(p->path_entry, path);
245         if (icon && strlen(icon) > 0) tab_completion_append_to_history(p->icon_entry, icon);
246
247         g_free(path);
248         g_free(new_string);
249
250         bookmark_populate_all(p->bb->parent);
251 }
252
253 /* simply pass NULL for text to turn this into a 'new bookmark' dialog */
254
255 static void bookmark_edit(const gchar *key, const gchar *text, GtkWidget *parent)
256 {
257         BookPropData *p;
258         GenericDialog *gd;
259         GtkWidget *table;
260         GtkWidget *label;
261         const gchar *icon;
262
263         if (!key) key = "bookmarks";
264
265         p = g_new0(BookPropData, 1);
266
267         p->bb = bookmark_from_string(text);
268         p->bb->parent = g_strdup(key);
269
270         gd = generic_dialog_new(_("Edit Bookmark"), "bookmark_edit",
271                                 parent, TRUE,
272                                 bookmark_edit_cancel_cb, p);
273         g_signal_connect(G_OBJECT(gd->dialog), "destroy",
274                          G_CALLBACK(bookmark_edit_destroy_cb), p);
275
276         generic_dialog_add_message(gd, nullptr, _("Edit Bookmark"), nullptr, FALSE);
277
278         generic_dialog_add_button(gd, GQ_ICON_OK, "OK",
279                                   bookmark_edit_ok_cb, TRUE);
280
281         table = pref_table_new(gd->vbox, 3, 2, FALSE, TRUE);
282         pref_table_label(table, 0, 0, _("Name:"), GTK_ALIGN_END);
283
284         p->name_entry = gtk_entry_new();
285         gtk_widget_set_size_request(p->name_entry, 300, -1);
286         if (p->bb->name) gq_gtk_entry_set_text(GTK_ENTRY(p->name_entry), p->bb->name);
287         gq_gtk_grid_attach_default(GTK_GRID(table), p->name_entry, 1, 2, 0, 1);
288         generic_dialog_attach_default(gd, p->name_entry);
289         gtk_widget_show(p->name_entry);
290
291         pref_table_label(table, 0, 1, _("Path:"), GTK_ALIGN_END);
292
293         label = tab_completion_new_with_history(&p->path_entry, p->bb->path,
294                                                 "bookmark_path", -1, nullptr, nullptr);
295         tab_completion_add_select_button(p->path_entry, nullptr, TRUE);
296         gq_gtk_grid_attach_default(GTK_GRID(table), label, 1, 2, 1, 2);
297         generic_dialog_attach_default(gd, p->path_entry);
298         gtk_widget_show(label);
299
300         pref_table_label(table, 0, 2, _("Icon:"), GTK_ALIGN_END);
301
302         icon = p->bb->icon;
303         if (!icon) icon = "";
304         label = tab_completion_new_with_history(&p->icon_entry, icon,
305                                                 "bookmark_icons", -1, nullptr, nullptr);
306         tab_completion_add_select_button(p->icon_entry, _("Select icon"), FALSE);
307         gq_gtk_grid_attach_default(GTK_GRID(table), label, 1, 2, 2, 3);
308         generic_dialog_attach_default(gd, p->icon_entry);
309         gtk_widget_show(label);
310
311         gtk_widget_show(gd->dialog);
312 }
313
314 static void bookmark_move(BookMarkData *bm, GtkWidget *button, gint direction)
315 {
316         BookButtonData *b;
317         gint p;
318         GList *list;
319         const gchar *key_holder;
320
321         if (!bm->editable) return;
322
323         b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
324         if (!b) return;
325
326         list = gtk_container_get_children(GTK_CONTAINER(bm->box));
327         p = g_list_index(list, button);
328         g_list_free(list);
329
330         if (p < 0 || p + direction < 0) return;
331
332         key_holder = bm->key;
333         bm->key = "_TEMPHOLDER";
334         history_list_item_move(key_holder, b->key, -direction);
335         bookmark_populate_all(key_holder);
336         bm->key = key_holder;
337
338         gtk_box_reorder_child(GTK_BOX(bm->box), button, p + direction);
339 }
340
341 static void bookmark_menu_prop_cb(GtkWidget *widget, gpointer data)
342 {
343         auto bm = static_cast<BookMarkData *>(data);
344
345         if (!bm->active_button) return;
346
347         bookmark_edit(bm->key, bm->active_button->key, widget);
348 }
349
350 static void bookmark_menu_move(BookMarkData *bm, gint direction)
351 {
352         if (!bm->active_button) return;
353
354         bookmark_move(bm, bm->active_button->button, direction);
355 }
356
357 static void bookmark_menu_up_cb(GtkWidget *, gpointer data)
358 {
359         bookmark_menu_move(static_cast<BookMarkData *>(data), -1);
360 }
361
362 static void bookmark_menu_down_cb(GtkWidget *, gpointer data)
363 {
364         bookmark_menu_move(static_cast<BookMarkData *>(data), 1);
365 }
366
367 static void bookmark_menu_remove_cb(GtkWidget *, gpointer data)
368 {
369         auto bm = static_cast<BookMarkData *>(data);
370
371         if (!bm->active_button) return;
372
373         history_list_item_remove(bm->key, bm->active_button->key);
374         bookmark_populate_all(bm->key);
375 }
376
377 static void bookmark_menu_popup(BookMarkData *bm, GtkWidget *button, gint, guint32, gboolean local)
378 {
379         GtkWidget *menu;
380         BookButtonData *b;
381
382         b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
383         if (!b) return;
384
385         bm->active_button = b;
386
387         menu = popup_menu_short_lived();
388         menu_item_add_icon_sensitive(menu, _("_Properties..."), PIXBUF_INLINE_ICON_PROPERTIES, bm->editable,
389                       G_CALLBACK(bookmark_menu_prop_cb), bm);
390         menu_item_add_icon_sensitive(menu, _("Move _up"), GQ_ICON_GO_UP, bm->editable,
391                       G_CALLBACK(bookmark_menu_up_cb), bm);
392         menu_item_add_icon_sensitive(menu, _("Move _down"), GQ_ICON_GO_DOWN, bm->editable,
393                       G_CALLBACK(bookmark_menu_down_cb), bm);
394         menu_item_add_icon_sensitive(menu, _("_Remove"), GQ_ICON_REMOVE, bm->editable,
395                       G_CALLBACK(bookmark_menu_remove_cb), bm);
396
397         if (local)
398                 {
399                 gtk_menu_popup_at_widget(GTK_MENU(menu), button, GDK_GRAVITY_NORTH_EAST, GDK_GRAVITY_CENTER, nullptr);
400                 }
401         else
402                 {
403                 gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
404                 }
405 }
406
407 static gboolean bookmark_press_cb(GtkWidget *button, GdkEventButton *event, gpointer data)
408 {
409         auto bm = static_cast<BookMarkData *>(data);
410
411         if (event->button != MOUSE_BUTTON_RIGHT) return FALSE;
412
413         bookmark_menu_popup(bm, button, event->button, event->time, FALSE);
414
415         return TRUE;
416 }
417
418 static gboolean bookmark_keypress_cb(GtkWidget *button, GdkEventKey *event, gpointer data)
419 {
420         auto bm = static_cast<BookMarkData *>(data);
421
422         switch (event->keyval)
423                 {
424                 case GDK_KEY_F10:
425                         if (!(event->state & GDK_CONTROL_MASK)) return FALSE;
426                         /* fall through */
427                 case GDK_KEY_Menu:
428                         bookmark_menu_popup(bm, button, 0, event->time, TRUE);
429                         return TRUE;
430                         break;
431                 case GDK_KEY_Up:
432                         if (event->state & GDK_SHIFT_MASK)
433                                 {
434                                 bookmark_move(bm, button, -1);
435                                 return TRUE;
436                                 }
437                         break;
438                 case GDK_KEY_Down:
439                         if (event->state & GDK_SHIFT_MASK)
440                                 {
441                                 bookmark_move(bm, button, 1);
442                                 return TRUE;
443                                 }
444                         break;
445                 }
446
447         return FALSE;
448 }
449
450 static void bookmark_drag_set_data(GtkWidget *button,
451                                    GdkDragContext *context, GtkSelectionData *selection_data,
452                                    guint, guint, gpointer data)
453 {
454         auto bm = static_cast<BookMarkData *>(data);
455         BookButtonData *b;
456         GList *list = nullptr;
457
458         return;
459         if (gdk_drag_context_get_dest_window(context) == gtk_widget_get_window(bm->widget)) return;
460
461         b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
462         if (!b) return;
463
464         list = g_list_append(list, b->path);
465
466         gchar **uris = uris_from_pathlist(list);
467         gboolean ret = gtk_selection_data_set_uris(selection_data, uris);
468         if (!ret)
469                 {
470                 char *str = g_strjoinv("\r\n", uris);
471                 ret = gtk_selection_data_set_text(selection_data, str, -1);
472                 g_free(str);
473                 }
474
475         g_strfreev(uris);
476         g_list_free(list);
477 }
478
479 static void bookmark_drag_begin(GtkWidget *button, GdkDragContext *context, gpointer)
480 {
481         GdkPixbuf *pixbuf;
482         GdkModifierType mask;
483         gint x, y;
484         GtkAllocation allocation;
485         GdkSeat *seat;
486         GdkDevice *device;
487
488         gtk_widget_get_allocation(button, &allocation);
489
490         pixbuf = gdk_pixbuf_get_from_window(gtk_widget_get_window(button),
491                                             allocation.x, allocation.y,
492                                             allocation.width, allocation.height);
493         seat = gdk_display_get_default_seat(gdk_window_get_display(gtk_widget_get_window(button)));
494         device = gdk_seat_get_pointer(seat);
495         gdk_window_get_device_position(gtk_widget_get_window(button), device, &x, &y, &mask);
496
497         gtk_drag_set_icon_pixbuf(context, pixbuf,
498                                  x - allocation.x, y - allocation.y);
499         g_object_unref(pixbuf);
500 }
501
502 static gboolean bookmark_path_tooltip_cb(GtkWidget *button, gpointer)
503 {
504         BookButtonData *b;
505
506         b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
507         gtk_widget_set_tooltip_text(GTK_WIDGET(button), b->path);
508
509         return FALSE;
510 }
511
512 static void bookmark_populate(BookMarkData *bm)
513 {
514         GtkBox *box;
515         GList *work;
516         GList *children;
517
518         box = GTK_BOX(bm->box);
519         children = gtk_container_get_children(GTK_CONTAINER(box));
520         work = children;
521         while (work)
522                 {
523                 GtkWidget *widget = GTK_WIDGET(work->data);
524                 work = work->next;
525                 gq_gtk_widget_destroy(widget);
526                 }
527
528         if (!bm->no_defaults && !history_list_get_by_key(bm->key))
529                 {
530                 gchar *buf;
531                 gchar *path;
532
533                 if (!bookmark_default_list)
534                         {
535                         buf = bookmark_string(_("Home"), homedir(), nullptr);
536                         history_list_add_to_key(bm->key, buf, 0);
537                         g_free(buf);
538
539                         if (g_strcmp0(bm->key, "shortcuts") != 0)
540                                 {
541                                 buf = bookmark_string(".", g_strdup(history_list_find_last_path_by_key("path_list")), nullptr);
542                                 history_list_add_to_key(bm->key, buf, 0);
543                                 g_free(buf);
544                                 }
545
546                         path = g_build_filename(homedir(), "Desktop", NULL);
547                         if (isname(path))
548                                 {
549                                 buf = bookmark_string(_("Desktop"), path, nullptr);
550                                 history_list_add_to_key(bm->key, buf, 0);
551                                 g_free(buf);
552                                 }
553                         g_free(path);
554                         }
555
556                 work = bookmark_default_list;
557                 while (work && work->next)
558                         {
559                         gchar *name;
560
561                         name = static_cast<gchar *>(work->data);
562                         work = work->next;
563                         path = static_cast<gchar *>(work->data);
564                         work = work->next;
565
566                         if (strcmp(name, ".") == 0)
567                                 {
568                                 if (g_strcmp0(bm->key, "shortcuts") != 0)
569                                         {
570                                         buf = bookmark_string(name, g_strdup(history_list_find_last_path_by_key("path_list")), nullptr);
571                                         }
572                                 else
573                                         {
574                                         continue;
575                                         }
576                                 }
577                         else
578                                 {
579                                 buf = bookmark_string(name, path, nullptr);
580                                 }
581                         history_list_add_to_key(bm->key, buf, 0);
582                         g_free(buf);
583                         }
584                 }
585
586         work = history_list_get_by_key(bm->key);
587         work = g_list_last(work);
588         while (work)
589                 {
590                 BookButtonData *b;
591
592                 b = bookmark_from_string(static_cast<const gchar *>(work->data));
593                 if (b)
594                         {
595                         if (strcmp(b->name, ".") == 0)
596                                 {
597                                 gchar *buf;
598
599                                 b->path = g_strdup(history_list_find_last_path_by_key("path_list"));
600                                 buf = bookmark_string(".", b->path, b->icon);
601                                 history_list_item_change("bookmarks", b->key, buf);
602                                 b->key = g_strdup(buf);
603                                 g_free(buf);
604                                 }
605                         GtkWidget *box;
606
607                         b->button = gtk_button_new();
608                         gtk_button_set_relief(GTK_BUTTON(b->button), GTK_RELIEF_NONE);
609                         gq_gtk_box_pack_start(GTK_BOX(bm->box), b->button, FALSE, FALSE, 0);
610                         gtk_widget_show(b->button);
611
612                         g_object_set_data_full(G_OBJECT(b->button), "bookbuttondata",
613                                                b, reinterpret_cast<GDestroyNotify>(bookmark_free));
614
615                         box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_BUTTON_GAP);
616                         gq_gtk_container_add(GTK_WIDGET(b->button), box);
617                         gtk_widget_show(box);
618
619                         if (b->icon)
620                                 {
621                                 GdkPixbuf *pixbuf = nullptr;
622                                 gchar *iconl;
623
624                                 iconl = path_from_utf8(b->icon);
625                                 pixbuf = gdk_pixbuf_new_from_file(iconl, nullptr);
626
627                                 if (isfile(b->icon))
628                                         {
629                                         pixbuf = gdk_pixbuf_new_from_file(iconl, nullptr);
630                                         }
631                                 else
632                                         {
633                                         gint w, h;
634
635                                         w = h = 16;
636                                         gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
637
638                                         pixbuf = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(), b->icon, w, GTK_ICON_LOOKUP_NO_SVG, nullptr);
639                                         }
640
641                                 g_free(iconl);
642                                 if (pixbuf)
643                                         {
644                                         GdkPixbuf *scaled;
645                                         gint w, h;
646
647                                         w = h = 16;
648                                         gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
649
650                                         scaled = gdk_pixbuf_scale_simple(pixbuf, w, h,
651                                                                          GDK_INTERP_BILINEAR);
652                                         b->image = gtk_image_new_from_pixbuf(scaled);
653                                         g_object_unref(scaled);
654                                         g_object_unref(pixbuf);
655                                         }
656                                 else
657                                         {
658                                         b->image = gtk_image_new_from_icon_name(GQ_ICON_DIRECTORY, GTK_ICON_SIZE_BUTTON);
659                                         }
660                                 }
661                         else
662                                 {
663                                 b->image = gtk_image_new_from_icon_name(GQ_ICON_DIRECTORY, GTK_ICON_SIZE_BUTTON);
664                                 }
665                         gq_gtk_box_pack_start(GTK_BOX(box), b->image, FALSE, FALSE, 0);
666                         gtk_widget_show(b->image);
667
668                         b->label = gtk_label_new(b->name);
669                         gq_gtk_box_pack_start(GTK_BOX(box), b->label, FALSE, FALSE, 0);
670                         gtk_widget_show(b->label);
671
672                         g_signal_connect(G_OBJECT(b->button), "clicked",
673                                          G_CALLBACK(bookmark_select_cb), bm);
674                         g_signal_connect(G_OBJECT(b->button), "button_press_event",
675                                          G_CALLBACK(bookmark_press_cb), bm);
676                         g_signal_connect(G_OBJECT(b->button), "key_press_event",
677                                          G_CALLBACK(bookmark_keypress_cb), bm);
678
679                         gtk_drag_source_set(b->button, GDK_BUTTON1_MASK,
680                                             bookmark_drag_types, bookmark_drag_types_n,
681                                             static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
682                         g_signal_connect(G_OBJECT(b->button), "drag_data_get",
683                                          G_CALLBACK(bookmark_drag_set_data), bm);
684                         g_signal_connect(G_OBJECT(b->button), "drag_begin",
685                                          G_CALLBACK(bookmark_drag_begin), bm);
686
687                         gtk_widget_set_has_tooltip(GTK_WIDGET(b->button), TRUE);
688                         g_signal_connect(G_OBJECT(b->button), "query_tooltip", G_CALLBACK(bookmark_path_tooltip_cb), bm);
689                         }
690
691                 work = work->prev;
692                 }
693 }
694
695 static void bookmark_populate_all(const gchar *key)
696 {
697         GList *work;
698
699         if (!key) return;
700
701         work = bookmark_widget_list;
702         while (work)
703                 {
704                 BookMarkData *bm;
705
706                 bm = static_cast<BookMarkData *>(work->data);
707                 work = work->next;
708
709                 if (strcmp(bm->key, key) == 0)
710                         {
711                         bookmark_populate(bm);
712                         }
713                 }
714 }
715
716 static void bookmark_dnd_get_data(GtkWidget *, GdkDragContext *,
717                                   gint, gint,
718                                   GtkSelectionData *selection_data, guint,
719                                   guint, gpointer data)
720 {
721         auto bm = static_cast<BookMarkData *>(data);
722         GList *list = nullptr;
723         GList *errors = nullptr;
724         GList *work;
725         gchar *real_path;
726         gchar **uris;
727
728         if (!bm->editable) return;
729
730         uris = gtk_selection_data_get_uris(selection_data);
731         if (uris)
732                 {
733                 list = uri_pathlist_from_uris(uris, &errors);
734                 if(errors)
735                         {
736                         warning_dialog_dnd_uri_error(errors);
737                         g_list_free_full(errors, g_free);
738                         }
739                 g_strfreev(uris);
740
741                 work = list;
742                 while (work)
743                         {
744                         auto path = static_cast<gchar *>(work->data);
745                         gchar *buf;
746
747                         work = work->next;
748
749                         if (bm->only_directories && !isdir(path)) continue;
750
751                         real_path = realpath(path, nullptr);
752
753                         if (strstr(real_path, get_collections_dir()) && isfile(path))
754                                 {
755                                 buf = bookmark_string(filename_from_path(path), path, "gq-icon-collection");
756                                 }
757                         else if (isfile(path))
758                                 {
759                                 buf = bookmark_string(filename_from_path(path), path, GQ_ICON_FILE);
760                                 }
761                         else
762                                 {
763                                 buf = bookmark_string(filename_from_path(path), path, nullptr);
764                                 }
765                         history_list_add_to_key(bm->key, buf, 0);
766                         g_free(buf);
767                         g_free(real_path);
768                         }
769
770                 g_list_free_full(list, g_free);
771
772                 bookmark_populate_all(bm->key);
773                 }
774 }
775
776 static void bookmark_list_destroy(GtkWidget *, gpointer data)
777 {
778         auto bm = static_cast<BookMarkData *>(data);
779
780         bookmark_widget_list = g_list_remove(bookmark_widget_list, bm);
781
782         g_free(const_cast<gchar *>(bm->key));
783         g_free(bm);
784 }
785
786 GtkWidget *bookmark_list_new(const gchar *key,
787                              void (*select_func)(const gchar *path, gpointer data), gpointer select_data)
788 {
789         GtkWidget *scrolled;
790         BookMarkData *bm;
791
792         if (!key) key = "bookmarks";
793
794         bm = g_new0(BookMarkData, 1);
795         bm->key = g_strdup(key);
796
797         bm->select_func = select_func;
798         bm->select_data = select_data;
799
800         bm->no_defaults = FALSE;
801         bm->editable = TRUE;
802         bm->only_directories = FALSE;
803
804         scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
805
806         PangoLayout *layout;
807         gint width, height;
808
809         layout = gtk_widget_create_pango_layout(GTK_WIDGET(scrolled), "reasonable width");
810         pango_layout_get_pixel_size(layout, &width, &height);
811         gtk_scrolled_window_set_min_content_width(GTK_SCROLLED_WINDOW(scrolled), width);
812         g_object_unref(layout);
813
814         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
815
816         bm->box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
817         gq_gtk_container_add(GTK_WIDGET(scrolled), bm->box);
818         gtk_widget_show(bm->box);
819
820         bookmark_populate(bm);
821
822         g_signal_connect(G_OBJECT(bm->box), "destroy",
823                          G_CALLBACK(bookmark_list_destroy), bm);
824         g_object_set_data(G_OBJECT(bm->box), BOOKMARK_DATA_KEY, bm);
825         g_object_set_data(G_OBJECT(scrolled), BOOKMARK_DATA_KEY, bm);
826         bm->widget = scrolled;
827
828         gtk_drag_dest_set(scrolled,
829                           static_cast<GtkDestDefaults>(GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP),
830                           bookmark_drop_types, bookmark_drop_types_n,
831                           static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
832         g_signal_connect(G_OBJECT(scrolled), "drag_data_received",
833                          G_CALLBACK(bookmark_dnd_get_data), bm);
834
835         bookmark_widget_list = g_list_append(bookmark_widget_list, bm);
836
837         return scrolled;
838 }
839
840 void bookmark_list_set_key(GtkWidget *list, const gchar *key)
841 {
842         BookMarkData *bm;
843
844         if (!list || !key) return;
845
846         bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
847         if (!bm) return;
848
849         if (bm->key && strcmp(bm->key, key) == 0) return;
850
851         g_free(const_cast<gchar *>(bm->key));
852         bm->key = g_strdup(key);
853
854         bookmark_populate(bm);
855 }
856
857 void bookmark_list_set_no_defaults(GtkWidget *list, gboolean no_defaults)
858 {
859         BookMarkData *bm;
860
861         bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
862         if (!bm) return;
863
864         bm->no_defaults = no_defaults;
865 }
866
867 void bookmark_list_set_editable(GtkWidget *list, gboolean editable)
868 {
869         BookMarkData *bm;
870
871         bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
872         if (!bm) return;
873
874         bm->editable = editable;
875 }
876
877 void bookmark_list_set_only_directories(GtkWidget *list, gboolean only_directories)
878 {
879         BookMarkData *bm;
880
881         bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
882         if (!bm) return;
883
884         bm->only_directories = only_directories;
885 }
886
887 void bookmark_list_add(GtkWidget *list, const gchar *name, const gchar *path)
888 {
889         BookMarkData *bm;
890         gchar *real_path;
891
892         bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
893         if (!bm) return;
894
895         std::unique_ptr<gchar, decltype(&g_free)> buf(bookmark_string(name, path, nullptr), g_free);
896         real_path = realpath(path, nullptr);
897
898         if (strstr(real_path, get_collections_dir()) && isfile(path))
899                 {
900                 buf.reset(bookmark_string(name, path, "gq-icon-collection"));
901                 }
902         else
903                 {
904                 if (isfile(path))
905                         {
906                         buf.reset(bookmark_string(name, path, GQ_ICON_FILE));
907                         }
908                 else
909                         {
910                         buf.reset(bookmark_string(name, path, nullptr));
911                         }
912                 }
913
914         history_list_add_to_key(bm->key, buf.get(), 0);
915         g_free(real_path);
916
917         bookmark_populate_all(bm->key);
918 }
919
920 void bookmark_add_default(const gchar *name, const gchar *path)
921 {
922         if (!name || !path) return;
923         bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(name));
924         bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(path));
925 }
926
927 /*
928  *-----------------------------------------------------------------------------
929  * combo with history key
930  *-----------------------------------------------------------------------------
931  */
932
933 struct HistoryComboData
934 {
935         GtkWidget *combo;
936         GtkWidget *entry;
937         gchar *history_key;
938         gint history_levels;
939 };
940
941 static void history_combo_destroy(GtkWidget *, gpointer data)
942 {
943         auto hc = static_cast<HistoryComboData *>(data);
944
945         g_free(hc->history_key);
946         g_free(data);
947 }
948
949 /* if text is NULL, entry is set to the most recent item */
950 GtkWidget *history_combo_new(GtkWidget **entry, const gchar *text,
951                              const gchar *history_key, gint max_levels)
952 {
953         HistoryComboData *hc;
954         GList *work;
955         gint n = 0;
956
957         hc = g_new0(HistoryComboData, 1);
958         hc->history_key = g_strdup(history_key);
959         hc->history_levels = max_levels;
960
961         hc->combo = gtk_combo_box_text_new_with_entry();
962
963         hc->entry = gtk_bin_get_child(GTK_BIN(hc->combo));
964
965         g_object_set_data(G_OBJECT(hc->combo), "history_combo_data", hc);
966         g_object_set_data(G_OBJECT(hc->entry), "history_combo_data", hc);
967         g_signal_connect(G_OBJECT(hc->combo), "destroy",
968                          G_CALLBACK(history_combo_destroy), hc);
969
970         work = history_list_get_by_key(hc->history_key);
971         while (work)
972                 {
973                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(hc->combo), static_cast<gchar *>(work->data));
974                 work = work->next;
975                 n++;
976                 }
977
978         if (text)
979                 {
980                 gq_gtk_entry_set_text(GTK_ENTRY(hc->entry), text);
981                 }
982         else if (n > 0)
983                 {
984                 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), 0);
985                 }
986
987         if (entry) *entry = hc->entry;
988         return hc->combo;
989 }
990
991 /* if text is NULL, current entry text is used
992  * widget can be the combo or entry widget
993  */
994 void history_combo_append_history(GtkWidget *widget, const gchar *text)
995 {
996         HistoryComboData *hc;
997         gchar *new_text;
998
999         hc = static_cast<HistoryComboData *>(g_object_get_data(G_OBJECT(widget), "history_combo_data"));
1000         if (!hc)
1001                 {
1002                 log_printf("widget is not a history combo\n");
1003                 return;
1004                 }
1005
1006         if (text)
1007                 {
1008                 new_text = g_strdup(text);
1009                 }
1010         else
1011                 {
1012                 new_text = g_strdup(gq_gtk_entry_get_text(GTK_ENTRY(hc->entry)));
1013                 }
1014
1015         if (new_text && strlen(new_text) > 0)
1016                 {
1017                 GtkTreeModel *store;
1018                 GList *work;
1019
1020                 history_list_add_to_key(hc->history_key, new_text, hc->history_levels);
1021
1022                 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), -1);
1023
1024                 store = gtk_combo_box_get_model(GTK_COMBO_BOX(hc->combo));
1025                 gtk_list_store_clear(GTK_LIST_STORE(store));
1026
1027                 work = history_list_get_by_key(hc->history_key);
1028                 while (work)
1029                         {
1030                         gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(hc->combo), static_cast<gchar *>(work->data));
1031                         work = work->next;
1032                         }
1033                 }
1034
1035         g_free(new_text);
1036 }
1037 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */