updated copyright in source files
[geeqie.git] / src / collect-dlg.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13
14 #include "main.h"
15 #include "collect.h"
16 #include "collect-dlg.h"
17
18 #include "collect-io.h"
19 #include "utilops.h"
20 #include "ui_fileops.h"
21 #include "ui_tabcomp.h"
22 #include "ui_utildlg.h"
23
24
25 enum {
26         DIALOG_SAVE,
27         DIALOG_SAVE_CLOSE,
28         DIALOG_LOAD,
29         DIALOG_APPEND
30 };
31
32
33 static gint collection_save_confirmed(FileDialog *fd, gint overwrite, CollectionData *cd);
34
35
36 static void collection_confirm_ok_cb(GenericDialog *gd, gpointer data)
37 {
38         FileDialog *fd = data;
39         CollectionData *cd = GENERIC_DIALOG(fd)->data;
40
41         if (!collection_save_confirmed(fd, TRUE, cd))
42                 {
43                 collection_unref(cd);
44                 file_dialog_close(fd);
45                 }
46 }
47
48 static void collection_confirm_cancel_cb(GenericDialog *gd, gpointer data)
49 {
50         /* this is a no-op, so the cancel button is added */
51 }
52
53 static gint collection_save_confirmed(FileDialog *fd, gint overwrite, CollectionData *cd)
54 {
55         gchar *buf;
56
57         if (isdir(fd->dest_path))
58                 {
59                 buf = g_strdup_printf(_("Specified path:\n%s\nis a folder, collections are files"), fd->dest_path);
60                 file_util_warning_dialog(_("Invalid filename"), buf, GTK_STOCK_DIALOG_INFO, GENERIC_DIALOG(fd)->dialog);
61                 g_free(buf);
62                 return FALSE;
63                 }
64
65         if (!overwrite && isfile(fd->dest_path))
66                 {
67                 GenericDialog *gd;
68
69                 gd = file_util_gen_dlg(_("Overwrite File"), GQ_WMCLASS, "dlg_confirm",
70                                         GENERIC_DIALOG(fd)->dialog, TRUE,
71                                         collection_confirm_cancel_cb, fd);
72
73                 generic_dialog_add_message(gd, GTK_STOCK_DIALOG_QUESTION,
74                                            _("Overwrite existing file?"), fd->dest_path);
75
76                 generic_dialog_add_button(gd, GTK_STOCK_OK, _("_Overwrite"), collection_confirm_ok_cb, TRUE);
77
78                 gtk_widget_show(gd->dialog);
79
80                 return TRUE;
81                 }
82
83         if (!collection_save(cd, fd->dest_path))
84                 {
85                 buf = g_strdup_printf(_("Failed to save the collection:\n%s"), fd->dest_path);
86                 file_util_warning_dialog(_("Save Failed"), buf, GTK_STOCK_DIALOG_ERROR, GENERIC_DIALOG(fd)->dialog);
87                 g_free(buf);
88                 }
89
90         collection_unref(cd);
91         file_dialog_sync_history(fd, TRUE);
92
93         if (fd->type == DIALOG_SAVE_CLOSE) collection_window_close_by_collection(cd);
94         file_dialog_close(fd);
95
96         return TRUE;
97 }
98
99 static void collection_save_cb(FileDialog *fd, gpointer data)
100 {
101         CollectionData *cd = data;
102         const gchar *path;
103
104         path = fd->dest_path;
105
106         if (!(strlen(path) > 7 && strncasecmp(path + (strlen(path) - 4), ".gqv", 4) == 0))
107                 {
108                 gchar *buf;
109                 buf = g_strconcat(path, ".gqv", NULL);
110                 gtk_entry_set_text(GTK_ENTRY(fd->entry), buf);
111                 g_free(buf);
112                 }
113
114         collection_save_confirmed(fd, FALSE, cd);
115 }
116
117 static void real_collection_button_pressed(FileDialog *fd, gpointer data, gint append)
118 {
119         CollectionData *cd = data;
120
121         if (!fd->dest_path || isdir(fd->dest_path)) return;
122
123         if (append)
124                 {
125                 collection_load(cd, fd->dest_path, TRUE);
126                 collection_unref(cd);
127                 }
128         else
129                 {
130                 collection_window_new(fd->dest_path);
131                 }
132
133         file_dialog_sync_history(fd, TRUE);
134         file_dialog_close(fd);
135 }
136
137 static void collection_load_cb(FileDialog *fd, gpointer data)
138 {
139         real_collection_button_pressed(fd, data, FALSE);
140 }
141
142 static void collection_append_cb(FileDialog *fd, gpointer data)
143 {
144         real_collection_button_pressed(fd, data, TRUE);
145 }
146
147 static void collection_save_or_load_dialog_close_cb(FileDialog *fd, gpointer data)
148 {
149         CollectionData *cd = data;
150
151         if (cd) collection_unref(cd);
152         file_dialog_close(fd);
153 }
154
155 static void collection_save_or_load_dialog(const gchar *path,
156                                            gint type, CollectionData *cd)
157 {
158         FileDialog *fd;
159         GtkWidget *parent = NULL;
160         CollectWindow *cw;
161         const gchar *title;
162         const gchar *btntext;
163         void *btnfunc;
164         gchar *base;
165         const gchar *stock_id;
166
167         if (type == DIALOG_SAVE || type == DIALOG_SAVE_CLOSE)
168                 {
169                 if (!cd) return;
170                 title = _("Save collection");
171                 btntext = NULL;
172                 btnfunc = collection_save_cb;
173                 stock_id = GTK_STOCK_SAVE;
174                 }
175         else if (type == DIALOG_LOAD)
176                 {
177                 title = _("Open collection");
178                 btntext = NULL;
179                 btnfunc = collection_load_cb;
180                 stock_id = GTK_STOCK_OPEN;
181                 }
182         else
183                 {
184                 if (!cd) return;
185                 title = _("Append collection");
186                 btntext = _("_Append");
187                 btnfunc = collection_append_cb;
188                 stock_id = GTK_STOCK_ADD;
189                 }
190
191         if (cd) collection_ref(cd);
192
193         cw = collection_window_find(cd);
194         if (cw) parent = cw->window;
195
196         fd = file_util_file_dlg(title, GQ_WMCLASS, "dlg_collection", parent,
197                              collection_save_or_load_dialog_close_cb, cd);
198
199         generic_dialog_add_message(GENERIC_DIALOG(fd), NULL, title, NULL);
200         file_dialog_add_button(fd, stock_id, btntext, btnfunc, TRUE);
201
202         base = g_strconcat(homedir(), "/", GQ_RC_DIR_COLLECTIONS, NULL);
203         file_dialog_add_path_widgets(fd, base, path,
204                                      "collection_load_save", ".gqv", _("Collection Files"));
205         g_free(base);
206
207         fd->type = type;
208
209         gtk_widget_show(GENERIC_DIALOG(fd)->dialog);
210 }
211
212 void collection_dialog_save_as(gchar *path, CollectionData *cd)
213 {
214 #if 0
215         if (!cd->list)
216                 {
217                 GtkWidget *parent = NULL;
218                 CollectWindow *cw;
219
220                 cw = collection_window_find(cd);
221                 if (cw) parent = cw->window;
222                 file_util_warning_dialog(_("Collection empty"),
223                                          _("The current collection is empty, save aborted."),
224                                          GTK_STOCK_DIALOG_INFO, parent);
225                 return;
226                 }
227 #endif
228
229         if (!path) path = cd->path;
230         if (!path) path = cd->name;
231
232         collection_save_or_load_dialog(path, DIALOG_SAVE, cd);
233 }
234
235 void collection_dialog_save_close(gchar *path, CollectionData *cd)
236 {
237         if (!path) path = cd->path;
238         if (!path) path = cd->name;
239
240         collection_save_or_load_dialog(path, DIALOG_SAVE_CLOSE, cd);
241 }
242
243 void collection_dialog_load(gchar *path)
244 {
245         collection_save_or_load_dialog(path, DIALOG_LOAD, NULL);
246 }
247
248 void collection_dialog_append(gchar *path, CollectionData *cd)
249 {
250         collection_save_or_load_dialog(path, DIALOG_APPEND, cd);
251 }