Remove unused RendererTiles::tile_cols
[geeqie.git] / src / collect-dlg.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 "collect-dlg.h"
23
24 #include <glib-object.h>
25 #include <glib.h>
26 #include <gtk/gtk.h>
27
28 #include "collect-io.h"
29 #include "collect.h"
30 #include "compat.h"
31 #include "intl.h"
32 #include "main-defines.h"
33 #include "misc.h"
34 #include "ui-fileops.h"
35 #include "ui-utildlg.h"
36 #include "utilops.h"
37
38 enum {
39         DIALOG_SAVE,
40         DIALOG_SAVE_CLOSE,
41         DIALOG_LOAD,
42         DIALOG_APPEND
43 };
44
45 static void collection_save_confirmed(GenericDialog *gdlg, gboolean overwrite, CollectionData *cd);
46
47 static void collection_confirm_ok_cb(GenericDialog *gdlg, gpointer data)
48 {
49         auto cd = static_cast<CollectionData *>(data);
50
51         collection_save_confirmed(gdlg, TRUE, cd);
52 }
53
54 static void collection_confirm_cancel_cb(GenericDialog *gdlg, gpointer)
55 {
56         generic_dialog_close(gdlg);
57 }
58
59 static void collection_save_confirmed(GenericDialog *gdlg, gboolean overwrite, CollectionData *cd)
60 {
61         gchar *buf;
62         GenericDialog *gdlg_overwrite;
63
64         generic_dialog_close(gdlg);
65
66         if (!overwrite && isfile(cd->collection_path))
67                 {
68                 gdlg_overwrite = file_util_gen_dlg(_("Overwrite collection"), "dlg_confirm", nullptr, FALSE, collection_confirm_cancel_cb, cd);
69                 generic_dialog_add_message(gdlg_overwrite, GQ_ICON_DIALOG_QUESTION, _("Overwrite existing collection?"), cd->name, TRUE);
70                 generic_dialog_add_button(gdlg_overwrite, GQ_ICON_OK, _("Overwrite"), collection_confirm_ok_cb, TRUE);
71
72                 gtk_widget_show(gdlg_overwrite->dialog);
73
74                 return;
75                 }
76
77         if (!collection_save(cd, cd->collection_path))
78                 {
79                 buf = g_strdup_printf(_("Failed to save the collection:\n%s"), cd->collection_path);
80                 file_util_warning_dialog(_("Save Failed"), buf, GQ_ICON_DIALOG_ERROR, GENERIC_DIALOG(gdlg)->dialog);
81                 g_free(buf);
82                 }
83
84         collection_unref(cd);
85         collection_window_close_by_collection(cd);
86 }
87
88 static void collection_save_cb(GenericDialog *gd, gpointer data)
89 {
90         auto cd = static_cast<CollectionData *>(data);
91
92         cd->collection_path = g_strconcat(get_collections_dir(), G_DIR_SEPARATOR_S, gq_gtk_entry_get_text(GTK_ENTRY(cd->dialog_name_entry)), GQ_COLLECTION_EXT, nullptr);
93
94         collection_save_confirmed(gd, FALSE, cd);
95 }
96
97 static void real_collection_button_pressed(GenericDialog *, gpointer data)
98 {
99         auto cd = static_cast<CollectionData *>(data);
100         GList *collection_list = nullptr;
101
102         collect_manager_list(nullptr, nullptr, &collection_list);
103
104         auto append_collection_path = static_cast<gchar *>(g_list_nth_data(collection_list, cd->collection_append_index));
105         collection_load(cd, append_collection_path, COLLECTION_LOAD_APPEND);
106
107         collection_unref(cd);
108         string_list_free(collection_list);
109 }
110
111 static void collection_append_cb(GenericDialog *gd, gpointer data)
112 {
113         real_collection_button_pressed(gd, data);
114 }
115
116 static void collection_save_or_load_dialog_close_cb(GenericDialog *gdlg, gpointer data)
117 {
118         auto cd = static_cast<CollectionData *>(data);
119
120         if (cd) collection_unref(cd);
121         generic_dialog_close(gdlg);
122 }
123
124 static void collection_append_menu_cb(GtkWidget *collection_append_combo, gpointer data)
125 {
126         auto option = static_cast<gint *>(data);
127
128         *option = gtk_combo_box_get_active(GTK_COMBO_BOX(collection_append_combo));
129 }
130
131 static void collection_save_or_append_dialog(gint type, CollectionData *cd)
132 {
133         GenericDialog *gdlg;
134         const gchar *title;
135         GList *collection_list = nullptr;
136
137         if (!cd) return;
138
139         collection_ref(cd);
140
141         if (type == DIALOG_SAVE || type == DIALOG_SAVE_CLOSE)
142                 {
143                 GtkWidget *existing_collections;
144                 GtkWidget *save_as_label;
145                 GtkWidget *scrolled;
146                 GtkWidget *viewport;
147
148                 title = _("Save collection");
149
150                 gdlg = file_util_gen_dlg(title, "dlg_collection_save", nullptr, FALSE, collection_save_or_load_dialog_close_cb, cd);
151
152                 generic_dialog_add_message(GENERIC_DIALOG(gdlg), nullptr, title, _("Existing collections:"), FALSE);
153                 generic_dialog_add_button(gdlg, GQ_ICON_SAVE, _("Save"), collection_save_cb, TRUE);
154
155                 collect_manager_list(&collection_list, nullptr, nullptr);
156
157                 GString *out_string = g_string_new(nullptr);
158
159                 for (GList *work = collection_list; work != nullptr; work = work->next)
160                         {
161                         auto collection_name = static_cast<const gchar *>(work->data);
162                         out_string = g_string_append(out_string, collection_name);
163                         out_string = g_string_append(out_string, "\n");
164                         }
165                 string_list_free(collection_list);
166
167                 existing_collections = gtk_label_new(out_string->str);
168                 g_string_free(out_string, TRUE);
169
170                 scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
171                 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
172                 gtk_widget_show(scrolled);
173
174                 viewport = gtk_viewport_new(nullptr, nullptr);
175                 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_NONE);
176                 gq_gtk_container_add(GTK_WIDGET(viewport), existing_collections);
177                 gtk_widget_show(viewport);
178                 gtk_widget_show(existing_collections);
179                 gq_gtk_container_add(GTK_WIDGET(scrolled), viewport);
180
181                 gq_gtk_box_pack_start(GTK_BOX(gdlg->vbox), scrolled, TRUE,TRUE, 0);
182
183                 save_as_label = gtk_label_new(_("Save collection as:"));
184                 gq_gtk_box_pack_start(GTK_BOX(gdlg->vbox), save_as_label, FALSE,FALSE, 0);
185                 gtk_label_set_xalign(GTK_LABEL(save_as_label), 0.0);
186                 gtk_widget_show(save_as_label);
187
188                 cd->dialog_name_entry = gtk_entry_new();
189                 gtk_widget_show(cd->dialog_name_entry);
190
191                 gq_gtk_box_pack_start(GTK_BOX(gdlg->vbox), cd->dialog_name_entry, FALSE, FALSE, 0);
192
193                 gq_gtk_entry_set_text(GTK_ENTRY(cd->dialog_name_entry), cd->name);
194                 gtk_widget_grab_focus(cd->dialog_name_entry);
195                 gtk_widget_show(GENERIC_DIALOG(gdlg)->dialog);
196                 }
197         else
198                 {
199                 CollectWindow *cw;
200                 GtkWidget *parent = nullptr;
201                 GtkWidget *collection_append_combo;
202
203                 title = _("Append collection");
204
205                 cw = collection_window_find(cd);
206                 if (cw) parent = cw->window;
207
208                 gdlg = file_util_gen_dlg(title, "dlg_collection_append", parent, true, nullptr, cd);
209
210                 generic_dialog_add_message(GENERIC_DIALOG(gdlg), nullptr, title, _("Select from existing collections:"), FALSE);
211                 generic_dialog_add_button(gdlg, GQ_ICON_CANCEL, _("Cancel"), nullptr, TRUE);
212                 generic_dialog_add_button(gdlg, GQ_ICON_ADD, _("_Append"), collection_append_cb, TRUE);
213
214                 collect_manager_list(&collection_list, nullptr, nullptr);
215
216                 collection_append_combo = gtk_combo_box_text_new();
217
218                 for (GList *work = collection_list; work != nullptr; work = work->next)
219                         {
220                         auto collection_name = static_cast<const gchar *>(work->data);
221                         gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(collection_append_combo), collection_name);
222                         }
223                 string_list_free(collection_list);
224
225                 gtk_combo_box_set_active(GTK_COMBO_BOX(collection_append_combo), 0);
226
227                 g_signal_connect(G_OBJECT(collection_append_combo), "changed", G_CALLBACK(collection_append_menu_cb), &cd->collection_append_index);
228
229                 gtk_widget_show(collection_append_combo);
230
231                 gq_gtk_box_pack_start(GTK_BOX(gdlg->vbox), collection_append_combo, TRUE,TRUE, 0);
232                 gtk_widget_show(GENERIC_DIALOG(gdlg)->dialog);
233                 }
234 }
235
236 void collection_dialog_save_as(CollectionData *cd)
237 {
238         collection_save_or_append_dialog(DIALOG_SAVE, cd);
239 }
240
241 void collection_dialog_save_close(CollectionData *cd)
242 {
243         collection_save_or_append_dialog(DIALOG_SAVE_CLOSE, cd);
244 }
245
246 void collection_dialog_append(CollectionData *cd)
247 {
248         collection_save_or_append_dialog(DIALOG_APPEND, cd);
249 }
250 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */