2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
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.
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.
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.
26 #include "collect-dlg.h"
28 #include "collect-io.h"
30 #include "ui-fileops.h"
39 static void collection_save_confirmed(GenericDialog *gdlg, gboolean overwrite, CollectionData *cd);
41 static void collection_confirm_ok_cb(GenericDialog *gdlg, gpointer data)
43 auto cd = static_cast<CollectionData *>(data);
45 collection_save_confirmed(gdlg, TRUE, cd);
48 static void collection_confirm_cancel_cb(GenericDialog *gdlg, gpointer)
50 generic_dialog_close(gdlg);
53 static void collection_save_confirmed(GenericDialog *gdlg, gboolean overwrite, CollectionData *cd)
56 GenericDialog *gdlg_overwrite;
58 generic_dialog_close(gdlg);
60 if (!overwrite && isfile(cd->collection_path))
62 gdlg_overwrite = file_util_gen_dlg(_("Overwrite collection"), "dlg_confirm", nullptr, FALSE, collection_confirm_cancel_cb, cd);
63 generic_dialog_add_message(gdlg_overwrite, GQ_ICON_DIALOG_QUESTION, _("Overwrite existing collection?"), cd->name, TRUE);
64 generic_dialog_add_button(gdlg_overwrite, GQ_ICON_OK, _("Overwrite"), collection_confirm_ok_cb, TRUE);
66 gtk_widget_show(gdlg_overwrite->dialog);
71 if (!collection_save(cd, cd->collection_path))
73 buf = g_strdup_printf(_("Failed to save the collection:\n%s"), cd->collection_path);
74 file_util_warning_dialog(_("Save Failed"), buf, GQ_ICON_DIALOG_ERROR, GENERIC_DIALOG(gdlg)->dialog);
79 collection_window_close_by_collection(cd);
82 static void collection_save_cb(GenericDialog *gd, gpointer data)
84 auto cd = static_cast<CollectionData *>(data);
86 cd->collection_path = g_strconcat(get_collections_dir(), G_DIR_SEPARATOR_S, gtk_entry_get_text(GTK_ENTRY(cd->dialog_name_entry)), GQ_COLLECTION_EXT, nullptr);
88 collection_save_confirmed(gd, FALSE, cd);
91 static void real_collection_button_pressed(GenericDialog *, gpointer data)
93 auto cd = static_cast<CollectionData *>(data);
94 GList *collection_list = nullptr;
96 collect_manager_list(nullptr, nullptr, &collection_list);
98 auto append_collection_path = static_cast<gchar *>(g_list_nth_data(collection_list, cd->collection_append_index));
99 collection_load(cd, append_collection_path, COLLECTION_LOAD_APPEND);
101 collection_unref(cd);
102 string_list_free(collection_list);
105 static void collection_append_cb(GenericDialog *gd, gpointer data)
107 real_collection_button_pressed(gd, data);
110 static void collection_save_or_load_dialog_close_cb(GenericDialog *gdlg, gpointer data)
112 auto cd = static_cast<CollectionData *>(data);
114 if (cd) collection_unref(cd);
115 generic_dialog_close(gdlg);
118 static void collection_append_menu_cb(GtkWidget *collection_append_combo, gpointer data)
120 auto option = static_cast<gint *>(data);
122 *option = gtk_combo_box_get_active(GTK_COMBO_BOX(collection_append_combo));
125 static void collection_save_or_append_dialog(gint type, CollectionData *cd)
128 GtkWidget *parent = nullptr;
131 const gchar *btntext;
133 const gchar *icon_name;
134 GList *collection_list = nullptr;
136 GString *out_string = g_string_new(nullptr);
137 GtkWidget *collection_append_combo;
138 GtkWidget *existing_collections;
139 GtkWidget *save_as_label;
143 if (type == DIALOG_SAVE || type == DIALOG_SAVE_CLOSE)
146 title = _("Save collection");
148 btnfunc = reinterpret_cast<gpointer>(collection_save_cb);
149 icon_name = GQ_ICON_SAVE;
154 title = _("Append collection");
155 btntext = _("_Append");
156 btnfunc = reinterpret_cast<gpointer>(collection_append_cb);
157 icon_name = GQ_ICON_ADD;
160 if (cd) collection_ref(cd);
162 cw = collection_window_find(cd);
163 if (cw) parent = cw->window;
165 if (g_strcmp0(icon_name, GQ_ICON_SAVE) == 0)
167 gdlg = file_util_gen_dlg(title, "dlg_collection_save", NULL, FALSE, collection_save_or_load_dialog_close_cb, cd);
169 generic_dialog_add_message(GENERIC_DIALOG(gdlg), nullptr, title, _("Existing collections:"), FALSE);
170 generic_dialog_add_button(gdlg, icon_name, btntext, reinterpret_cast<void (*)(GenericDialog *, gpointer)>(btnfunc), TRUE);
172 collect_manager_list(&collection_list, nullptr, nullptr);
174 work = collection_list;
177 auto collection_name = static_cast<const gchar *>(work->data);
178 out_string = g_string_append(out_string, g_strdup(collection_name));
179 out_string = g_string_append(out_string, "\n");
184 string_list_free(collection_list);
186 existing_collections = gtk_label_new(out_string->str);
188 scrolled = gtk_scrolled_window_new(nullptr, nullptr);
189 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
190 gtk_widget_show(scrolled);
192 viewport = gtk_viewport_new(nullptr, nullptr);
193 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_NONE);
194 gtk_container_add(GTK_CONTAINER(viewport), existing_collections);
195 gtk_widget_show(viewport);
196 gtk_widget_show(existing_collections);
197 gtk_container_add(GTK_CONTAINER(scrolled), viewport);
199 gtk_box_pack_start(GTK_BOX(gdlg->vbox), scrolled, TRUE,TRUE, 0);
201 save_as_label = gtk_label_new("Save collection as:");
202 gtk_box_pack_start(GTK_BOX(gdlg->vbox), save_as_label, FALSE,FALSE, 0);
203 gtk_label_set_xalign(GTK_LABEL(save_as_label), 0.0);
204 gtk_widget_show(save_as_label);
206 cd->dialog_name_entry = gtk_entry_new();
207 gtk_widget_show(cd->dialog_name_entry);
209 gtk_box_pack_start(GTK_BOX(gdlg->vbox), cd->dialog_name_entry, FALSE, FALSE, 0);
211 gtk_entry_set_text(GTK_ENTRY(cd->dialog_name_entry), cd->name);
212 gtk_widget_grab_focus(cd->dialog_name_entry);
213 gtk_widget_show(GENERIC_DIALOG(gdlg)->dialog);
215 else if (g_strcmp0(icon_name, GQ_ICON_ADD) == 0)
217 gdlg = file_util_gen_dlg(title, "dlg_collection_append", parent, true, nullptr, cd);
219 generic_dialog_add_message(GENERIC_DIALOG(gdlg), nullptr, title, _("Select from existing collections:"), FALSE);
220 generic_dialog_add_button(gdlg, GQ_ICON_CANCEL, _("Cancel"), nullptr, TRUE);
221 generic_dialog_add_button(gdlg, icon_name, btntext, reinterpret_cast<void (*)(GenericDialog *, gpointer)>(btnfunc), TRUE);
223 collect_manager_list(&collection_list, nullptr, nullptr);
225 collection_append_combo = gtk_combo_box_text_new();
227 work = collection_list;
230 auto collection_name = static_cast<const gchar *>(work->data);
231 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(collection_append_combo), collection_name);
234 string_list_free(collection_list);
236 gtk_combo_box_set_active(GTK_COMBO_BOX(collection_append_combo), 0);
238 g_signal_connect(G_OBJECT(collection_append_combo), "changed", G_CALLBACK(collection_append_menu_cb), &cd->collection_append_index);
240 gtk_widget_show(collection_append_combo);
242 gtk_box_pack_start(GTK_BOX(gdlg->vbox), collection_append_combo, TRUE,TRUE, 0);
243 gtk_widget_show(GENERIC_DIALOG(gdlg)->dialog);
247 void collection_dialog_save_as(CollectionData *cd)
249 collection_save_or_append_dialog(DIALOG_SAVE, cd);
252 void collection_dialog_save_close(CollectionData *cd)
254 collection_save_or_append_dialog(DIALOG_SAVE_CLOSE, cd);
257 void collection_dialog_append(CollectionData *cd)
259 collection_save_or_append_dialog(DIALOG_APPEND, cd);
261 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */