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