Simplify vflist_get_formatted()
[geeqie.git] / src / collect-dlg.c
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 "main.h"
23 #include "collect.h"
24 #include "collect-dlg.h"
25
26 #include "collect-io.h"
27 #include "utilops.h"
28 #include "ui_fileops.h"
29 #include "ui_tabcomp.h"
30 #include "ui_utildlg.h"
31
32
33 enum {
34         DIALOG_SAVE,
35         DIALOG_SAVE_CLOSE,
36         DIALOG_LOAD,
37         DIALOG_APPEND
38 };
39
40
41 static gboolean collection_save_confirmed(FileDialog *fd, gboolean overwrite, CollectionData *cd);
42
43
44 static void collection_confirm_ok_cb(GenericDialog *gd, gpointer data)
45 {
46         FileDialog *fd = data;
47         CollectionData *cd = GENERIC_DIALOG(fd)->data;
48
49         if (!collection_save_confirmed(fd, TRUE, cd))
50                 {
51                 collection_unref(cd);
52                 file_dialog_close(fd);
53                 }
54 }
55
56 static void collection_confirm_cancel_cb(GenericDialog *gd, gpointer data)
57 {
58         /* this is a no-op, so the cancel button is added */
59 }
60
61 static gboolean collection_save_confirmed(FileDialog *fd, gboolean overwrite, CollectionData *cd)
62 {
63         gchar *buf;
64
65         if (isdir(fd->dest_path))
66                 {
67                 buf = g_strdup_printf(_("Specified path:\n%s\nis a folder, collections are files"), fd->dest_path);
68                 file_util_warning_dialog(_("Invalid filename"), buf, GTK_STOCK_DIALOG_INFO, GENERIC_DIALOG(fd)->dialog);
69                 g_free(buf);
70                 return FALSE;
71                 }
72
73         if (!overwrite && isfile(fd->dest_path))
74                 {
75                 GenericDialog *gd;
76
77                 gd = file_util_gen_dlg(_("Overwrite File"), "dlg_confirm",
78                                         GENERIC_DIALOG(fd)->dialog, TRUE,
79                                         collection_confirm_cancel_cb, fd);
80
81                 generic_dialog_add_message(gd, GTK_STOCK_DIALOG_QUESTION,
82                                            _("Overwrite existing file?"), fd->dest_path, TRUE);
83
84                 generic_dialog_add_button(gd, GTK_STOCK_OK, _("_Overwrite"), collection_confirm_ok_cb, TRUE);
85
86                 gtk_widget_show(gd->dialog);
87
88                 return TRUE;
89                 }
90
91         if (!collection_save(cd, fd->dest_path))
92                 {
93                 buf = g_strdup_printf(_("Failed to save the collection:\n%s"), fd->dest_path);
94                 file_util_warning_dialog(_("Save Failed"), buf, GTK_STOCK_DIALOG_ERROR, GENERIC_DIALOG(fd)->dialog);
95                 g_free(buf);
96                 }
97
98         collection_unref(cd);
99         file_dialog_sync_history(fd, TRUE);
100
101         if (fd->type == DIALOG_SAVE_CLOSE) collection_window_close_by_collection(cd);
102         file_dialog_close(fd);
103
104         return TRUE;
105 }
106
107 static void collection_save_cb(FileDialog *fd, gpointer data)
108 {
109         CollectionData *cd = data;
110         const gchar *path;
111
112         path = fd->dest_path;
113
114         /** @FIXME utf8 */
115         if (!file_extension_match(path, GQ_COLLECTION_EXT))
116                 {
117                 gchar *buf;
118                 buf = g_strconcat(path, GQ_COLLECTION_EXT, NULL);
119                 gtk_entry_set_text(GTK_ENTRY(fd->entry), buf);
120                 g_free(buf);
121                 }
122
123         collection_save_confirmed(fd, FALSE, cd);
124 }
125
126 static void real_collection_button_pressed(FileDialog *fd, gpointer data, gint append)
127 {
128         CollectionData *cd = data;
129         gboolean err = FALSE;
130         gchar *text = NULL;
131
132         if (!isname(fd->dest_path))
133                 {
134                 err = TRUE;
135                 text = g_strdup_printf(_("No such file '%s'."), fd->dest_path);
136                 }
137         if (!err && isdir(fd->dest_path))
138                 {
139                 err = TRUE;
140                 text = g_strdup_printf(_("'%s' is a directory, not a collection file."), fd->dest_path);
141                 }
142         if (!err && !access_file(fd->dest_path, R_OK))
143                 {
144                 err = TRUE;
145                 text = g_strdup_printf(_("You do not have read permissions on the file '%s'."), fd->dest_path);
146                 }
147
148         if (err) {
149                 if  (text)
150                         {
151                         file_util_warning_dialog(_("Can not open collection file"), text, GTK_STOCK_DIALOG_ERROR, NULL);
152                         g_free(text);
153                 }
154                 return;
155         }
156
157         if (append)
158                 {
159                 collection_load(cd, fd->dest_path, TRUE);
160                 collection_unref(cd);
161                 }
162         else
163                 {
164                 collection_window_new(fd->dest_path);
165                 }
166
167         file_dialog_sync_history(fd, TRUE);
168         file_dialog_close(fd);
169 }
170
171 static void collection_load_cb(FileDialog *fd, gpointer data)
172 {
173         real_collection_button_pressed(fd, data, FALSE);
174 }
175
176 static void collection_append_cb(FileDialog *fd, gpointer data)
177 {
178         real_collection_button_pressed(fd, data, TRUE);
179 }
180
181 static void collection_save_or_load_dialog_close_cb(FileDialog *fd, gpointer data)
182 {
183         CollectionData *cd = data;
184
185         if (cd) collection_unref(cd);
186         file_dialog_close(fd);
187 }
188
189 static void collection_save_or_load_dialog(const gchar *path,
190                                            gint type, CollectionData *cd)
191 {
192         FileDialog *fd;
193         GtkWidget *parent = NULL;
194         CollectWindow *cw;
195         const gchar *title;
196         const gchar *btntext;
197         gpointer btnfunc;
198         const gchar *stock_id;
199
200         if (type == DIALOG_SAVE || type == DIALOG_SAVE_CLOSE)
201                 {
202                 if (!cd) return;
203                 title = _("Save collection");
204                 btntext = NULL;
205                 btnfunc = collection_save_cb;
206                 stock_id = GTK_STOCK_SAVE;
207                 }
208         else if (type == DIALOG_LOAD)
209                 {
210                 title = _("Open collection");
211                 btntext = NULL;
212                 btnfunc = collection_load_cb;
213                 stock_id = GTK_STOCK_OPEN;
214                 }
215         else
216                 {
217                 if (!cd) return;
218                 title = _("Append collection");
219                 btntext = _("_Append");
220                 btnfunc = collection_append_cb;
221                 stock_id = GTK_STOCK_ADD;
222                 }
223
224         if (cd) collection_ref(cd);
225
226         cw = collection_window_find(cd);
227         if (cw) parent = cw->window;
228
229         fd = file_util_file_dlg(title, "dlg_collection", parent,
230                              collection_save_or_load_dialog_close_cb, cd);
231
232         generic_dialog_add_message(GENERIC_DIALOG(fd), NULL, title, NULL, FALSE);
233         file_dialog_add_button(fd, stock_id, btntext, btnfunc, TRUE);
234
235         file_dialog_add_path_widgets(fd, get_collections_dir(), path,
236                                      "collection_load_save", GQ_COLLECTION_EXT, _("Collection Files"));
237
238         fd->type = type;
239
240         gtk_widget_show(GENERIC_DIALOG(fd)->dialog);
241 }
242
243 void collection_dialog_save_as(gchar *path, CollectionData *cd)
244 {
245         if (!path) path = cd->path;
246         if (!path) path = cd->name;
247
248         collection_save_or_load_dialog(path, DIALOG_SAVE, cd);
249 }
250
251 void collection_dialog_save_close(gchar *path, CollectionData *cd)
252 {
253         if (!path) path = cd->path;
254         if (!path) path = cd->name;
255
256         collection_save_or_load_dialog(path, DIALOG_SAVE_CLOSE, cd);
257 }
258
259 void collection_dialog_load(gchar *path)
260 {
261         collection_save_or_load_dialog(path, DIALOG_LOAD, NULL);
262 }
263
264 void collection_dialog_append(gchar *path, CollectionData *cd)
265 {
266         collection_save_or_load_dialog(path, DIALOG_APPEND, cd);
267 }
268 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */