7 * This software is released under the GNU General Public License (GNU GPL).
8 * Please read the included file COPYING for more information.
9 * This software comes with no warranty of any kind, use at your own risk!
15 #include "collect-dlg.h"
17 #include "collect-io.h"
19 #include "ui_fileops.h"
20 #include "ui_tabcomp.h"
21 #include "ui_utildlg.h"
32 static gint collection_save_confirmed(FileDialog *fd, gint overwrite, CollectionData *cd);
35 static void collection_confirm_ok_cb(GenericDialog *gd, gpointer data)
37 FileDialog *fd = data;
38 CollectionData *cd = GENERIC_DIALOG(fd)->data;
40 if (!collection_save_confirmed(fd, TRUE, cd))
43 file_dialog_close(fd);
47 static void collection_confirm_cancel_cb(GenericDialog *gd, gpointer data)
49 /* this is a no-op, so the cancel button is added */
52 static gint collection_save_confirmed(FileDialog *fd, gint overwrite, CollectionData *cd)
56 if (isdir(fd->dest_path))
58 buf = g_strdup_printf(_("Specified path:\n%s\nis a folder, collections are files"), fd->dest_path);
59 file_util_warning_dialog(_("Invalid filename"), buf, GTK_STOCK_DIALOG_INFO, GENERIC_DIALOG(fd)->dialog);
64 if (!overwrite && isfile(fd->dest_path))
68 gd = file_util_gen_dlg(_("Overwrite File"), GQ_WMCLASS, "dlg_confirm",
69 GENERIC_DIALOG(fd)->dialog, TRUE,
70 collection_confirm_cancel_cb, fd);
72 generic_dialog_add_message(gd, GTK_STOCK_DIALOG_QUESTION,
73 _("Overwrite existing file?"), fd->dest_path);
75 generic_dialog_add_button(gd, GTK_STOCK_OK, _("_Overwrite"), collection_confirm_ok_cb, TRUE);
77 gtk_widget_show(gd->dialog);
82 if (!collection_save(cd, fd->dest_path))
84 buf = g_strdup_printf(_("Failed to save the collection:\n%s"), fd->dest_path);
85 file_util_warning_dialog(_("Save Failed"), buf, GTK_STOCK_DIALOG_ERROR, GENERIC_DIALOG(fd)->dialog);
90 file_dialog_sync_history(fd, TRUE);
92 if (fd->type == DIALOG_SAVE_CLOSE) collection_window_close_by_collection(cd);
93 file_dialog_close(fd);
98 static void collection_save_cb(FileDialog *fd, gpointer data)
100 CollectionData *cd = data;
103 path = fd->dest_path;
105 if (!(strlen(path) > 7 && strncasecmp(path + (strlen(path) - 4), ".gqv", 4) == 0))
108 buf = g_strconcat(path, ".gqv", NULL);
109 gtk_entry_set_text(GTK_ENTRY(fd->entry), buf);
113 collection_save_confirmed(fd, FALSE, cd);
116 static void real_collection_button_pressed(FileDialog *fd, gpointer data, gint append)
118 CollectionData *cd = data;
120 if (!fd->dest_path || isdir(fd->dest_path)) return;
124 collection_load(cd, fd->dest_path, TRUE);
125 collection_unref(cd);
129 collection_window_new(fd->dest_path);
132 file_dialog_sync_history(fd, TRUE);
133 file_dialog_close(fd);
136 static void collection_load_cb(FileDialog *fd, gpointer data)
138 real_collection_button_pressed(fd, data, FALSE);
141 static void collection_append_cb(FileDialog *fd, gpointer data)
143 real_collection_button_pressed(fd, data, TRUE);
146 static void collection_save_or_load_dialog_close_cb(FileDialog *fd, gpointer data)
148 CollectionData *cd = data;
150 if (cd) collection_unref(cd);
151 file_dialog_close(fd);
154 static void collection_save_or_load_dialog(const gchar *path,
155 gint type, CollectionData *cd)
158 GtkWidget *parent = NULL;
161 const gchar *btntext;
164 const gchar *stock_id;
166 if (type == DIALOG_SAVE || type == DIALOG_SAVE_CLOSE)
169 title = _("Save collection");
171 btnfunc = collection_save_cb;
172 stock_id = GTK_STOCK_SAVE;
174 else if (type == DIALOG_LOAD)
176 title = _("Open collection");
178 btnfunc = collection_load_cb;
179 stock_id = GTK_STOCK_OPEN;
184 title = _("Append collection");
185 btntext = _("_Append");
186 btnfunc = collection_append_cb;
187 stock_id = GTK_STOCK_ADD;
190 if (cd) collection_ref(cd);
192 cw = collection_window_find(cd);
193 if (cw) parent = cw->window;
195 fd = file_util_file_dlg(title, GQ_WMCLASS, "dlg_collection", parent,
196 collection_save_or_load_dialog_close_cb, cd);
198 generic_dialog_add_message(GENERIC_DIALOG(fd), NULL, title, NULL);
199 file_dialog_add_button(fd, stock_id, btntext, btnfunc, TRUE);
201 base = g_strconcat(homedir(), "/", GQ_RC_DIR_COLLECTIONS, NULL);
202 file_dialog_add_path_widgets(fd, base, path,
203 "collection_load_save", ".gqv", _("Collection Files"));
208 gtk_widget_show(GENERIC_DIALOG(fd)->dialog);
211 void collection_dialog_save_as(gchar *path, CollectionData *cd)
216 GtkWidget *parent = NULL;
219 cw = collection_window_find(cd);
220 if (cw) parent = cw->window;
221 file_util_warning_dialog(_("Collection empty"),
222 _("The current collection is empty, save aborted."),
223 GTK_STOCK_DIALOG_INFO, parent);
228 if (!path) path = cd->path;
229 if (!path) path = cd->name;
231 collection_save_or_load_dialog(path, DIALOG_SAVE, cd);
234 void collection_dialog_save_close(gchar *path, CollectionData *cd)
236 if (!path) path = cd->path;
237 if (!path) path = cd->name;
239 collection_save_or_load_dialog(path, DIALOG_SAVE_CLOSE, cd);
242 void collection_dialog_load(gchar *path)
244 collection_save_or_load_dialog(path, DIALOG_LOAD, NULL);
247 void collection_dialog_append(gchar *path, CollectionData *cd)
249 collection_save_or_load_dialog(path, DIALOG_APPEND, cd);