Cleanup main.h header
[geeqie.git] / src / bar-sort.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 "bar-sort.h"
23
24 #include <config.h>
25
26 #include "collect.h"
27 #include "collect-io.h"
28 #include "compat.h"
29 #include "debug.h"
30 #include "editors.h"
31 #include "filedata.h"
32 #include "history-list.h"
33 #include "intl.h"
34 #include "layout.h"
35 #include "layout-image.h"
36 #include "main-defines.h"
37 #include "misc.h"
38 #include "rcfile.h"
39 #include "ui-bookmark.h"
40 #include "ui-fileops.h"
41 #include "ui-misc.h"
42 #include "utilops.h"
43 #include "window.h"
44
45
46 /*
47   *-------------------------------------------------------------------
48   * sort bar
49   *-------------------------------------------------------------------
50   */
51
52 struct SortData
53 {
54         GtkWidget *vbox;
55         GtkWidget *bookmarks;
56         LayoutWindow *lw;
57
58         FileDialog *dialog;
59         GtkWidget *dialog_name_entry;
60
61         SortModeType mode;
62         SortActionType action;
63         gchar *filter_key;
64
65         SortSelectionType selection;
66
67         GtkWidget *folder_group;
68         GtkWidget *collection_group;
69
70         GtkWidget *add_button;
71         GtkWidget *help_button;
72         GtkWidget *undo_button;
73         SortActionType undo_action;
74         GList *undo_src_list;
75         GList *undo_dest_list;
76         gchar *undo_collection;
77 };
78
79
80 #define SORT_KEY_FOLDERS     "sort_manager"
81 #define SORT_KEY_COLLECTIONS "sort_manager_collections"
82
83
84 static void bar_sort_undo_set(SortData *sd, GList *src_list, const gchar *dest);
85 static void bar_sort_add_close(SortData *sd);
86
87
88 static void bar_sort_collection_list_build(GtkWidget *bookmarks)
89 {
90         FileData *dir_fd;
91         GList *list;
92         GList *work;
93
94         history_list_free_key(SORT_KEY_COLLECTIONS);
95         bookmark_list_set_key(bookmarks, SORT_KEY_COLLECTIONS);
96
97         dir_fd = file_data_new_dir(get_collections_dir());
98         filelist_read(dir_fd, &list, nullptr);
99         file_data_unref(dir_fd);
100
101         list = filelist_sort_path(list);
102
103         work = list;
104         while (work)
105                 {
106                 FileData *fd;
107                 gchar *name;
108
109                 fd = static_cast<FileData *>(work->data);
110                 work = work->next;
111
112                 if (file_extension_match(fd->path, GQ_COLLECTION_EXT))
113                         {
114                         name = remove_extension_from_path(fd->name);
115                         }
116                 else
117                         {
118                         name = g_strdup(fd->name);
119                         }
120                 bookmark_list_add(bookmarks, name, fd->path);
121                 g_free(name);
122                 }
123
124         filelist_free(list);
125 }
126
127 static void bar_sort_mode_sync(SortData *sd, SortModeType mode)
128 {
129         gboolean folder_mode;
130
131         if (sd->mode == mode) return;
132         sd->mode = mode;
133
134         folder_mode = (sd->mode == BAR_SORT_MODE_FOLDER);
135
136         bookmark_list_set_no_defaults(sd->bookmarks, !folder_mode);
137         bookmark_list_set_editable(sd->bookmarks, folder_mode);
138         bookmark_list_set_only_directories(sd->bookmarks, folder_mode);
139
140         if (folder_mode)
141                 {
142                 gtk_widget_hide(sd->collection_group);
143                 gtk_widget_show(sd->folder_group);
144                 bookmark_list_set_key(sd->bookmarks, SORT_KEY_FOLDERS);
145                 }
146         else
147                 {
148                 gtk_widget_hide(sd->folder_group);
149                 gtk_widget_show(sd->collection_group);
150                 bar_sort_collection_list_build(sd->bookmarks);
151                 }
152
153         bar_sort_add_close(sd);
154
155         bar_sort_undo_set(sd, nullptr, nullptr);
156 }
157
158 static void bar_sort_mode_cb(GtkWidget *combo, gpointer data)
159 {
160         auto sd = static_cast<SortData *>(data);
161
162         if (gtk_combo_box_get_active(GTK_COMBO_BOX(combo)) == BAR_SORT_MODE_FOLDER)
163                 {
164                 bar_sort_mode_sync(sd, BAR_SORT_MODE_FOLDER);
165                 }
166         else
167                 {
168                 bar_sort_mode_sync(sd, BAR_SORT_MODE_COLLECTION);
169                 }
170 }
171
172 /* this takes control of src_list */
173 static void bar_sort_undo_set(SortData *sd, GList *src_list, const gchar *dest)
174 {
175         g_list_free_full(sd->undo_src_list, g_free);
176         sd->undo_src_list = filelist_to_path_list(src_list);
177
178         if (src_list)
179                 {
180                 /* we should create the undo_dest_list to use it later... */
181                 g_list_free_full(sd->undo_dest_list, g_free);
182                 sd->undo_dest_list=nullptr;
183
184                 GList *work = sd->undo_src_list;
185                 while(work)
186                         {
187                         gchar *filename = g_strdup(filename_from_path(static_cast<const gchar *>(work->data)));
188                         gchar *dest_path = g_build_filename(g_strdup(dest), filename, NULL);
189                         sd->undo_dest_list = g_list_prepend(sd->undo_dest_list, g_strdup(dest_path));
190                         work = work->next;
191                         }
192                 sd->undo_dest_list = g_list_reverse(sd->undo_dest_list);
193                 }
194
195         sd->undo_action = sd->action;
196
197         if (sd->undo_button)
198                 {
199                 gtk_widget_set_sensitive(sd->undo_button,
200                                         ((sd->undo_src_list ) && sd->undo_dest_list));
201                 }
202 }
203
204 static void bar_sort_undo_folder(SortData *sd, GtkWidget *button)
205 {
206         gchar *origin;
207
208         if (!(sd->undo_src_list && sd->undo_dest_list)) return;
209
210         switch (sd->undo_action)
211                 {
212                 case BAR_SORT_MOVE:
213                         {
214                         GList *list;
215                         gchar *src_dir;
216                         gchar *src_path;
217
218                         if (sd->undo_src_list)
219                                 {
220                                 GList *work = nullptr;
221
222                                 src_path = g_strdup(static_cast<const gchar *>(sd->undo_src_list->data));
223                                 src_dir = remove_level_from_path(src_path);
224                                 list = sd->undo_dest_list;
225                                 while (list)
226                                         {
227                                         work = g_list_prepend(work, file_data_new_group(static_cast<const gchar *>(list->data)));
228                                         list=list->next;
229                                         }
230                                 file_util_move_simple(work, src_dir, sd->lw->window);
231                                 g_free(src_dir);
232                                 g_free(src_path);
233                                 }
234                         }
235                         break;
236
237                 case BAR_SORT_COPY:
238                 case BAR_SORT_FILTER:
239                         if (sd->undo_src_list)
240                                 {
241                                 GList *delete_list;
242                                 GList *work = nullptr;
243
244                                 delete_list = sd->undo_dest_list;
245                                 while (delete_list)
246                                         {
247                                         work = g_list_append(work, file_data_new_group(static_cast<const gchar *>(delete_list->data)));
248                                         delete_list = delete_list->next;
249                                         }
250                                 options->file_ops.safe_delete_enable = TRUE;
251                                 file_util_delete(nullptr, work, button);
252                                 }
253                         break;
254
255                 default:
256                         break;
257                 }
258
259         layout_refresh(sd->lw);
260         origin = static_cast<gchar *>((sd->undo_src_list)->data);
261
262         if (isfile(origin))
263                 {
264                 layout_image_set_fd(sd->lw, file_data_new_group(origin));
265                 }
266
267         bar_sort_undo_set(sd, nullptr, nullptr);
268 }
269
270 static void bar_sort_undo_collection(SortData *sd)
271 {
272         GList *work;
273
274         work = sd->undo_src_list;
275         while (work)
276                 {
277                 gchar *source;
278                 source = static_cast<gchar *>(work->data);
279                 work = work->next;
280                 collect_manager_remove(file_data_new_group(source), sd->undo_collection);
281                 }
282
283         bar_sort_undo_set(sd, nullptr,  nullptr);
284 }
285
286 static void bar_sort_undo_cb(GtkWidget *button, gpointer data)
287 {
288         auto sd = static_cast<SortData *>(data);
289
290         if (sd->mode == BAR_SORT_MODE_FOLDER)
291                 {
292                 bar_sort_undo_folder(sd, button);
293                 }
294         else
295                 {
296                 bar_sort_undo_collection(sd);
297                 }
298 }
299
300 static void bar_sort_bookmark_select_folder(SortData *sd, FileData *, const gchar *path)
301 {
302         GList *orig_list;
303         GList *action_list;
304         GList *undo_src_list;
305
306         if (!isdir(path)) return;
307
308         orig_list = layout_selection_list(sd->lw);
309         action_list = orig_list;
310         undo_src_list = orig_list;
311         orig_list = nullptr;
312
313         bar_sort_undo_set(sd, undo_src_list, path);
314
315         switch (sd->action)
316                 {
317                 case BAR_SORT_COPY:
318                         file_util_copy_simple(action_list, path, sd->lw->window);
319                         action_list = nullptr;
320                         layout_image_next(sd->lw);
321                         break;
322
323                 case BAR_SORT_MOVE:
324                         file_util_move_simple(action_list, path, sd->lw->window);
325                         action_list = nullptr;
326                         break;
327
328                 case BAR_SORT_FILTER:
329                         file_util_start_filter_from_filelist(sd->filter_key, action_list, path, sd->lw->window);
330                         layout_image_next(sd->lw);
331                         break;
332
333                 default:
334                         break;
335                 }
336 }
337
338 static void bar_sort_bookmark_select_collection(SortData *sd, FileData *source, const gchar *path)
339 {
340         GList *list = nullptr;
341
342         switch (sd->selection)
343                 {
344                 case BAR_SORT_SELECTION_IMAGE:
345                         list = g_list_append(nullptr, file_data_ref(source));
346                         break;
347                 case BAR_SORT_SELECTION_SELECTED:
348                         list = layout_selection_list(sd->lw);
349                         break;
350                 default:
351                         break;
352                 }
353
354         if (!list)
355                 {
356                 bar_sort_undo_set(sd, nullptr, nullptr);
357                 return;
358                 }
359
360         bar_sort_undo_set(sd, list, path);
361         sd->undo_collection = g_strdup(path);
362
363         while (list)
364                 {
365                 FileData *image_fd;
366
367                 image_fd = static_cast<FileData *>(list->data);
368                 list = list->next;
369                 collect_manager_add(image_fd, path);
370                 }
371 }
372
373 static void bar_sort_bookmark_select(const gchar *path, gpointer data)
374 {
375         auto sd = static_cast<SortData *>(data);
376         FileData *source;
377
378         source = layout_image_get_fd(sd->lw);
379         if (!path || !source) return;
380
381         if (sd->mode == BAR_SORT_MODE_FOLDER)
382                 {
383                 bar_sort_bookmark_select_folder(sd, source, path);
384                 }
385         else
386                 {
387                 bar_sort_bookmark_select_collection(sd, source, path);
388                 }
389 }
390
391 static void bar_sort_set_action(SortData *sd, SortActionType action, const gchar *filter_key)
392 {
393         sd->action = action;
394         if (action == BAR_SORT_FILTER)
395                 {
396                 if (!filter_key) filter_key = "";
397                 sd->filter_key = g_strdup(filter_key);
398                 }
399         else
400                 {
401                 sd->filter_key = nullptr;
402                 }
403 }
404
405 static void bar_sort_set_copy_cb(GtkWidget *button, gpointer data)
406 {
407         auto sd = static_cast<SortData *>(data);
408         if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) return;
409         bar_sort_set_action(sd, BAR_SORT_COPY, nullptr);
410 }
411
412 static void bar_sort_set_move_cb(GtkWidget *button, gpointer data)
413 {
414         auto sd = static_cast<SortData *>(data);
415         if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) return;
416         bar_sort_set_action(sd, BAR_SORT_MOVE, nullptr);
417 }
418
419 static void bar_sort_set_filter_cb(GtkWidget *button, gpointer data)
420 {
421         auto sd = static_cast<SortData *>(data);
422         const gchar *key;
423
424         if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) return;
425         key = static_cast<const gchar *>(g_object_get_data(G_OBJECT(button), "filter_key"));
426         bar_sort_set_action(sd, BAR_SORT_FILTER, key);
427 }
428
429 static void bar_filter_help_cb(GenericDialog *, gpointer)
430 {
431         help_window_show("GuidePluginsConfig.html#Geeqieextensions");
432 }
433
434 static void bar_filter_help_dialog()
435 {
436         GenericDialog *gd;
437
438         gd = generic_dialog_new(_("Sort Manager Operations"),
439                                 "sort_manager_operations", nullptr, TRUE, nullptr, nullptr);
440         generic_dialog_add_message(gd, GQ_ICON_DIALOG_INFO,
441                                 "Sort Manager Operations", _("Additional operations utilising plugins\nmay be included by setting:\n\nX-Geeqie-Filter=true\n\nin the plugin file."), TRUE);
442         generic_dialog_add_button(gd, GQ_ICON_HELP, _("Help"), bar_filter_help_cb, TRUE);
443         generic_dialog_add_button(gd, GQ_ICON_OK, "OK", nullptr, TRUE);
444
445         gtk_widget_show(gd->dialog);
446 }
447
448 static gboolean bar_filter_message_cb(GtkWidget *, GdkEventButton *event, gpointer)
449 {
450         if (event->button != MOUSE_BUTTON_RIGHT) return FALSE;
451
452         bar_filter_help_dialog();
453
454         return TRUE;
455 }
456
457 static void bar_sort_help_cb(gpointer)
458 {
459         bar_filter_help_dialog();
460 }
461
462 static void bar_sort_set_selection(SortData *sd, SortSelectionType selection)
463 {
464         sd->selection = selection;
465 }
466
467 static void bar_sort_set_selection_image_cb(GtkWidget *button, gpointer data)
468 {
469         auto sd = static_cast<SortData *>(data);
470         if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) return;
471         bar_sort_set_selection(sd, BAR_SORT_SELECTION_IMAGE);
472 }
473
474 static void bar_sort_set_selection_selected_cb(GtkWidget *button, gpointer data)
475 {
476         auto sd = static_cast<SortData *>(data);
477         if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) return;
478         bar_sort_set_selection(sd, BAR_SORT_SELECTION_SELECTED);
479 }
480
481 static void bar_sort_add_close(SortData *sd)
482 {
483         if (sd->dialog) file_dialog_close(sd->dialog);
484         sd->dialog_name_entry = nullptr;
485         sd->dialog = nullptr;
486 }
487
488 static void bar_sort_add_ok_cb(FileDialog *fd, gpointer data)
489 {
490         auto sd = static_cast<SortData *>(data);
491         const gchar *name = gq_gtk_entry_get_text(GTK_ENTRY(sd->dialog_name_entry));
492         gboolean empty_name = (name[0] == '\0');
493
494         name = gq_gtk_entry_get_text(GTK_ENTRY(sd->dialog_name_entry));
495         if (sd->mode == BAR_SORT_MODE_FOLDER)
496                 {
497                 if (empty_name)
498                         {
499                         name = filename_from_path(fd->dest_path);
500                         }
501
502                 bookmark_list_add(sd->bookmarks, name, fd->dest_path);
503                 }
504         else
505                 {
506                 gchar *path;
507                 gboolean has_extension;
508                 auto filename = const_cast<gchar *>(name);
509
510                 if (empty_name) return;
511
512                 has_extension = file_extension_match(name, GQ_COLLECTION_EXT);
513                 if (!has_extension)
514                         {
515                         filename = g_strconcat(name, GQ_COLLECTION_EXT, NULL);
516                         }
517
518                 path = g_build_filename(get_collections_dir(), filename, NULL);
519                 if (isfile(path))
520                         {
521                         gchar *text = g_strdup_printf(_("The collection:\n%s\nalready exists."), filename);
522                         file_util_warning_dialog(_("Collection exists"), text, GQ_ICON_DIALOG_INFO, nullptr);
523                         g_free(text);
524                         }
525                 else
526                         {
527                         CollectionData *cd;
528
529                         cd = collection_new(path);
530                         if (collection_save(cd, path))
531                                 {
532                                 bar_sort_collection_list_build(sd->bookmarks);
533                                 }
534                         else
535                                 {
536                                 gchar *text = g_strdup_printf(_("Failed to save the collection:\n%s"), path);
537                                 file_util_warning_dialog(_("Save Failed"), text,
538                                                          GQ_ICON_DIALOG_ERROR, GENERIC_DIALOG(fd)->dialog);
539                                 g_free(text);
540                                 }
541                         collection_unref(cd);
542                         }
543
544                 if (!has_extension) g_free(filename);
545                 g_free(path);
546                 }
547
548         bar_sort_add_close(sd);
549 }
550
551 static void bar_sort_add_cancel_cb(FileDialog *, gpointer data)
552 {
553         auto sd = static_cast<SortData *>(data);
554
555         bar_sort_add_close(sd);
556 }
557
558 static void bar_sort_add_cb(GtkWidget *button, gpointer data)
559 {
560         auto sd = static_cast<SortData *>(data);
561         GtkWidget *hbox;
562         const gchar *title;
563
564         if (sd->dialog)
565                 {
566                 gtk_window_present(GTK_WINDOW(GENERIC_DIALOG(sd->dialog)->dialog));
567                 return;
568                 }
569
570         if (sd->mode == BAR_SORT_MODE_FOLDER)
571                 {
572                 title = _("Add Bookmark");
573                 }
574         else
575                 {
576                 title = _("Add Collection");
577                 }
578
579         sd->dialog = file_util_file_dlg(title,
580                                        "add_bookmark", button,
581                                        bar_sort_add_cancel_cb, sd);
582         file_dialog_add_button(sd->dialog, GQ_ICON_OK, "OK", bar_sort_add_ok_cb, TRUE);
583
584         generic_dialog_add_message(GENERIC_DIALOG(sd->dialog), nullptr, title, nullptr, FALSE);
585
586         if (sd->mode == BAR_SORT_MODE_FOLDER)
587                 {
588                 file_dialog_add_path_widgets(sd->dialog, nullptr, nullptr, "add_bookmark", nullptr, nullptr);
589                 }
590
591         hbox = pref_box_new(GENERIC_DIALOG(sd->dialog)->vbox, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
592
593         pref_label_new(hbox, _("Name:"));
594
595         sd->dialog_name_entry = gtk_entry_new();
596         gq_gtk_box_pack_start(GTK_BOX(hbox), sd->dialog_name_entry, TRUE, TRUE, 0);
597         generic_dialog_attach_default(GENERIC_DIALOG(sd->dialog), sd->dialog_name_entry);
598         gtk_widget_show(sd->dialog_name_entry);
599
600         if (sd->mode == BAR_SORT_MODE_COLLECTION)
601                 {
602                 gtk_widget_grab_focus(sd->dialog_name_entry);
603                 }
604
605         gtk_widget_show(GENERIC_DIALOG(sd->dialog)->dialog);
606 }
607
608 void bar_sort_close(GtkWidget *bar)
609 {
610         SortData *sd;
611
612         sd = static_cast<SortData *>(g_object_get_data(G_OBJECT(bar), "bar_sort_data"));
613         if (!sd) return;
614
615         gq_gtk_widget_destroy(sd->vbox);
616 }
617
618 static void bar_sort_destroy(GtkWidget *, gpointer data)
619 {
620         auto sd = static_cast<SortData *>(data);
621
622         bar_sort_add_close(sd);
623
624         g_free(sd->filter_key);
625         g_list_free_full(sd->undo_src_list, g_free);
626         g_list_free_full(sd->undo_dest_list, g_free);
627         g_free(sd->undo_collection);
628         g_free(sd);
629 }
630
631 static void bar_sort_edit_button_free(gpointer data)
632 {
633         g_free(data);
634 }
635
636 static GtkWidget *bar_sort_new(LayoutWindow *lw, SortActionType action,
637                                SortModeType mode, SortSelectionType selection,
638                                const gchar *filter_key)
639 {
640         SortData *sd;
641         GtkWidget *buttongrp;
642         GtkWidget *label;
643         GtkWidget *tbar;
644         GtkWidget *combo;
645         GList *editors_list;
646         GList *work;
647         gboolean have_filter;
648         GtkWidget *button;
649
650         if (!lw) return nullptr;
651
652         sd = g_new0(SortData, 1);
653
654         sd->lw = lw;
655
656         sd->action = action;
657
658         if (sd->action == BAR_SORT_FILTER && (!filter_key || !filter_key[0]))
659                 {
660                 sd->action = BAR_SORT_COPY;
661                 }
662
663         sd->selection = selection;
664         sd->undo_src_list = nullptr;
665         sd->undo_dest_list = nullptr;
666         sd->undo_collection = nullptr;
667
668         sd->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
669         DEBUG_NAME(sd->vbox);
670         g_object_set_data(G_OBJECT(sd->vbox), "bar_sort_data", sd);
671         g_signal_connect(G_OBJECT(sd->vbox), "destroy",
672                          G_CALLBACK(bar_sort_destroy), sd);
673
674         label = gtk_label_new(_("Sort Manager"));
675         pref_label_bold(label, TRUE, FALSE);
676         gq_gtk_box_pack_start(GTK_BOX(sd->vbox), label, FALSE, FALSE, 0);
677         gtk_widget_show(label);
678
679         combo = gtk_combo_box_text_new();
680         gq_gtk_box_pack_start(GTK_BOX(sd->vbox), combo, FALSE, FALSE, 0);
681         gtk_widget_show(combo);
682
683         gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), _("Folders"));
684         gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), _("Collections"));
685
686         g_signal_connect(G_OBJECT(combo), "changed",
687                          G_CALLBACK(bar_sort_mode_cb), sd);
688
689         sd->folder_group = pref_box_new(sd->vbox, FALSE, GTK_ORIENTATION_VERTICAL, 0);
690         DEBUG_NAME(sd->folder_group);
691         gtk_widget_set_tooltip_text(sd->folder_group, _("See the Help file for additional functions"));
692
693         buttongrp = pref_radiobutton_new(sd->folder_group, nullptr,
694                                          _("Copy"), (sd->action == BAR_SORT_COPY),
695                                          G_CALLBACK(bar_sort_set_copy_cb), sd);
696         g_signal_connect(G_OBJECT(buttongrp), "button_press_event", G_CALLBACK(bar_filter_message_cb), NULL);
697         button = pref_radiobutton_new(sd->folder_group, buttongrp,
698                              _("Move"), (sd->action == BAR_SORT_MOVE),
699                              G_CALLBACK(bar_sort_set_move_cb), sd);
700         g_signal_connect(G_OBJECT(button), "button_press_event", G_CALLBACK(bar_filter_message_cb), NULL);
701
702
703         have_filter = FALSE;
704         editors_list = editor_list_get();
705         work = editors_list;
706         while (work)
707                 {
708                 GtkWidget *button;
709                 auto editor = static_cast<EditorDescription *>(work->data);
710                 gchar *key;
711                 gboolean select = FALSE;
712
713                 work = work->next;
714
715                 if (!editor_is_filter(editor->key)) continue;
716
717                 key = g_strdup(editor->key);
718                 if (sd->action == BAR_SORT_FILTER && strcmp(key, filter_key) == 0)
719                         {
720                         bar_sort_set_action(sd, sd->action, key);
721                         select = TRUE;
722                         have_filter = TRUE;
723                         }
724
725                 button = pref_radiobutton_new(sd->folder_group, buttongrp,
726                                               editor->name, select,
727                                               G_CALLBACK(bar_sort_set_filter_cb), sd);
728                 g_signal_connect(G_OBJECT(button), "button_press_event", G_CALLBACK(bar_filter_message_cb), NULL);
729
730                 g_object_set_data_full(G_OBJECT(button), "filter_key", key, bar_sort_edit_button_free);
731                 }
732         g_list_free(editors_list);
733
734         if (sd->action == BAR_SORT_FILTER && !have_filter) sd->action = BAR_SORT_COPY;
735
736         sd->collection_group = pref_box_new(sd->vbox, FALSE, GTK_ORIENTATION_VERTICAL, 0);
737
738         buttongrp = pref_radiobutton_new(sd->collection_group, nullptr,
739                                          _("Add image"), (sd->selection == BAR_SORT_SELECTION_IMAGE),
740                                          G_CALLBACK(bar_sort_set_selection_image_cb), sd);
741         pref_radiobutton_new(sd->collection_group, buttongrp,
742                              _("Add selection"), (sd->selection == BAR_SORT_SELECTION_SELECTED),
743                              G_CALLBACK(bar_sort_set_selection_selected_cb), sd);
744
745         sd->bookmarks = bookmark_list_new(SORT_KEY_FOLDERS, bar_sort_bookmark_select, sd);
746         DEBUG_NAME(sd->bookmarks);
747         gq_gtk_box_pack_start(GTK_BOX(sd->vbox), sd->bookmarks, TRUE, TRUE, 0);
748         gtk_widget_show(sd->bookmarks);
749
750         tbar = pref_toolbar_new(sd->vbox);
751         DEBUG_NAME(tbar);
752
753         sd->add_button = pref_toolbar_button(tbar, GQ_ICON_ADD, _("Add"), FALSE,
754                                              _("Add Bookmark"),
755                                              G_CALLBACK(bar_sort_add_cb), sd);
756         sd->undo_button = pref_toolbar_button(tbar, GQ_ICON_UNDO, _("Undo"), FALSE,
757                                               _("Undo last image"),
758                                               G_CALLBACK(bar_sort_undo_cb), sd);
759         sd->help_button = pref_toolbar_button(tbar, GQ_ICON_HELP, _("Help"), FALSE,
760                                               _("Functions additional to Copy and Move"),
761                                               G_CALLBACK(bar_sort_help_cb), sd);
762
763         sd->mode = static_cast<SortModeType>(-1);
764         bar_sort_mode_sync(sd, mode);
765         gtk_combo_box_set_active(GTK_COMBO_BOX(combo), static_cast<gint>(sd->mode));
766
767         return sd->vbox;
768 }
769
770 GtkWidget *bar_sort_new_from_config(LayoutWindow *lw, const gchar **, const gchar **)
771 {
772         GtkWidget *bar;
773
774         bar = bar_sort_new(lw, lw->options.action, lw->options.mode, lw->options.selection, lw->options.filter_key);
775
776         if (lw->bar_sort_enabled) gtk_widget_show(bar);
777         return bar;
778 }
779
780 /**
781  * @brief Sets the bar_sort_enabled flag
782  * @param lw
783  * @param attribute_names
784  * @param attribute_values
785  *
786  * Called from rcfile when processing geeqierc.xml on start-up.
787  * It is necessary to set the bar_sort_enabled flag because
788  * the sort manager and desktop files are set up in the idle loop, and
789  * setup is not yet completed during initialisation.
790  * The flag is checked in layout_editors_reload_idle_cb.
791  */
792 void bar_sort_cold_start(LayoutWindow *lw, const gchar **attribute_names, const gchar **attribute_values)
793 {
794         gboolean enabled = TRUE;
795         gint action = BAR_SORT_COPY;
796         gint mode = BAR_SORT_MODE_FOLDER;
797         gint selection = BAR_SORT_SELECTION_IMAGE;
798         gchar *filter_key = nullptr;
799
800         while (attribute_names && *attribute_names)
801                 {
802                 const gchar *option = *attribute_names++;
803                 const gchar *value = *attribute_values++;
804
805                 if (READ_BOOL_FULL("enabled", enabled)) continue;
806                 if (READ_INT_CLAMP_FULL("action", action, 0, BAR_SORT_ACTION_COUNT - 1)) continue;
807                 if (READ_INT_CLAMP_FULL("mode", mode, 0, BAR_SORT_MODE_COUNT - 1)) continue;
808                 if (READ_INT_CLAMP_FULL("selection", selection, 0, BAR_SORT_SELECTION_COUNT - 1)) continue;
809                 if (READ_CHAR_FULL("filter_key", filter_key)) continue;
810
811                 log_printf("unknown attribute %s = %s\n", option, value);
812                 }
813
814         lw->options.action = static_cast<SortActionType>(action);
815         lw->options.mode = static_cast<SortModeType>(mode);
816         lw->options.selection = static_cast<SortSelectionType>(selection);
817         lw->options.filter_key = g_strdup(filter_key);
818         lw->bar_sort_enabled = enabled;
819
820         g_free(filter_key);
821 }
822
823 GtkWidget *bar_sort_new_default(LayoutWindow *lw)
824 {
825         return bar_sort_new_from_config(lw, nullptr, nullptr);
826 }
827
828 void bar_sort_write_config(GtkWidget *bar, GString *outstr, gint indent)
829 {
830         SortData *sd;
831
832         if (!bar) return;
833         sd = static_cast<SortData *>(g_object_get_data(G_OBJECT(bar), "bar_sort_data"));
834         if (!sd) return;
835
836         WRITE_NL(); WRITE_STRING("<bar_sort ");
837         write_bool_option(outstr, indent, "enabled", gtk_widget_get_visible(bar));
838         WRITE_INT(*sd, mode);
839         WRITE_INT(*sd, action);
840         WRITE_INT(*sd, selection);
841         WRITE_CHAR(*sd, filter_key);
842         WRITE_STRING("/>");
843 }
844
845
846 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */