* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+#include <memory>
+
#include "main.h"
#include "collect.h"
#include "collect-dlg.h"
#include "collect-io.h"
-#include "utilops.h"
+#include "misc.h"
#include "ui-fileops.h"
+#include "utilops.h"
enum {
DIALOG_SAVE,
DIALOG_APPEND
};
+static void collection_save_confirmed(GenericDialog *gdlg, gboolean overwrite, CollectionData *cd);
-static gboolean collection_save_confirmed(FileDialog *fd, gboolean overwrite, CollectionData *cd);
-
-
-static void collection_confirm_ok_cb(GenericDialog *UNUSED(gd), gpointer data)
+static void collection_confirm_ok_cb(GenericDialog *gdlg, gpointer data)
{
- FileDialog *fd = static_cast<FileDialog *>(data);
- CollectionData *cd = GENERIC_DIALOG(fd)->data;
+ auto cd = static_cast<CollectionData *>(data);
- if (!collection_save_confirmed(fd, TRUE, cd))
- {
- collection_unref(cd);
- file_dialog_close(fd);
- }
+ collection_save_confirmed(gdlg, TRUE, cd);
}
-static void collection_confirm_cancel_cb(GenericDialog *UNUSED(gd), gpointer UNUSED(data))
+static void collection_confirm_cancel_cb(GenericDialog *gdlg, gpointer)
{
- /* this is a no-op, so the cancel button is added */
+ generic_dialog_close(gdlg);
}
-static gboolean collection_save_confirmed(FileDialog *fd, gboolean overwrite, CollectionData *cd)
+static void collection_save_confirmed(GenericDialog *gdlg, gboolean overwrite, CollectionData *cd)
{
gchar *buf;
+ GenericDialog *gdlg_overwrite;
- if (isdir(fd->dest_path))
- {
- buf = g_strdup_printf(_("Specified path:\n%s\nis a folder, collections are files"), fd->dest_path);
- file_util_warning_dialog(_("Invalid filename"), buf, GTK_STOCK_DIALOG_INFO, GENERIC_DIALOG(fd)->dialog);
- g_free(buf);
- return FALSE;
- }
+ generic_dialog_close(gdlg);
- if (!overwrite && isfile(fd->dest_path))
+ if (!overwrite && isfile(cd->collection_path))
{
- GenericDialog *gd;
-
- gd = file_util_gen_dlg(_("Overwrite File"), "dlg_confirm",
- GENERIC_DIALOG(fd)->dialog, TRUE,
- collection_confirm_cancel_cb, fd);
-
- generic_dialog_add_message(gd, GTK_STOCK_DIALOG_QUESTION,
- _("Overwrite existing file?"), fd->dest_path, TRUE);
-
- generic_dialog_add_button(gd, GTK_STOCK_OK, _("_Overwrite"), collection_confirm_ok_cb, TRUE);
+ gdlg_overwrite = file_util_gen_dlg(_("Overwrite collection"), "dlg_confirm", nullptr, FALSE, collection_confirm_cancel_cb, cd);
+ generic_dialog_add_message(gdlg_overwrite, GQ_ICON_DIALOG_QUESTION, _("Overwrite existing collection?"), cd->name, TRUE);
+ generic_dialog_add_button(gdlg_overwrite, GQ_ICON_OK, _("Overwrite"), collection_confirm_ok_cb, TRUE);
- gtk_widget_show(gd->dialog);
+ gtk_widget_show(gdlg_overwrite->dialog);
- return TRUE;
+ return;
}
- if (!collection_save(cd, fd->dest_path))
+ if (!collection_save(cd, cd->collection_path))
{
- buf = g_strdup_printf(_("Failed to save the collection:\n%s"), fd->dest_path);
- file_util_warning_dialog(_("Save Failed"), buf, GTK_STOCK_DIALOG_ERROR, GENERIC_DIALOG(fd)->dialog);
+ buf = g_strdup_printf(_("Failed to save the collection:\n%s"), cd->collection_path);
+ file_util_warning_dialog(_("Save Failed"), buf, GQ_ICON_DIALOG_ERROR, GENERIC_DIALOG(gdlg)->dialog);
g_free(buf);
}
collection_unref(cd);
- file_dialog_sync_history(fd, TRUE);
-
- if (fd->type == DIALOG_SAVE_CLOSE) collection_window_close_by_collection(cd);
- file_dialog_close(fd);
-
- return TRUE;
+ collection_window_close_by_collection(cd);
}
-static void collection_save_cb(FileDialog *fd, gpointer data)
+static void collection_save_cb(GenericDialog *gd, gpointer data)
{
- CollectionData *cd = static_cast<CollectionData *>(data);
- const gchar *path;
+ auto cd = static_cast<CollectionData *>(data);
- path = fd->dest_path;
+ 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);
- /** @FIXME utf8 */
- if (!file_extension_match(path, GQ_COLLECTION_EXT))
- {
- gchar *buf;
- buf = g_strconcat(path, GQ_COLLECTION_EXT, NULL);
- gtk_entry_set_text(GTK_ENTRY(fd->entry), buf);
- g_free(buf);
- }
-
- collection_save_confirmed(fd, FALSE, cd);
+ collection_save_confirmed(gd, FALSE, cd);
}
-static void real_collection_button_pressed(FileDialog *fd, gpointer data, gint append)
+static void real_collection_button_pressed(GenericDialog *, gpointer data)
{
- CollectionData *cd = static_cast<CollectionData *>(data);
- gboolean err = FALSE;
- gchar *text = NULL;
-
- if (!isname(fd->dest_path))
- {
- err = TRUE;
- text = g_strdup_printf(_("No such file '%s'."), fd->dest_path);
- }
- if (!err && isdir(fd->dest_path))
- {
- err = TRUE;
- text = g_strdup_printf(_("'%s' is a directory, not a collection file."), fd->dest_path);
- }
- if (!err && !access_file(fd->dest_path, R_OK))
- {
- err = TRUE;
- text = g_strdup_printf(_("You do not have read permissions on the file '%s'."), fd->dest_path);
- }
+ auto cd = static_cast<CollectionData *>(data);
+ GList *collection_list = nullptr;
- if (err) {
- if (text)
- {
- file_util_warning_dialog(_("Can not open collection file"), text, GTK_STOCK_DIALOG_ERROR, NULL);
- g_free(text);
- }
- return;
- }
+ collect_manager_list(nullptr, nullptr, &collection_list);
- if (append)
- {
- collection_load(cd, fd->dest_path, COLLECTION_LOAD_APPEND);
- collection_unref(cd);
- }
- else
- {
- collection_window_new(fd->dest_path);
- }
+ auto append_collection_path = static_cast<gchar *>(g_list_nth_data(collection_list, cd->collection_append_index));
+ collection_load(cd, append_collection_path, COLLECTION_LOAD_APPEND);
- file_dialog_sync_history(fd, TRUE);
- file_dialog_close(fd);
+ collection_unref(cd);
+ string_list_free(collection_list);
}
-static void collection_load_cb(FileDialog *fd, gpointer data)
+static void collection_append_cb(GenericDialog *gd, gpointer data)
{
- real_collection_button_pressed(fd, data, FALSE);
+ real_collection_button_pressed(gd, data);
}
-static void collection_append_cb(FileDialog *fd, gpointer data)
+static void collection_save_or_load_dialog_close_cb(GenericDialog *gdlg, gpointer data)
{
- real_collection_button_pressed(fd, data, TRUE);
+ auto cd = static_cast<CollectionData *>(data);
+
+ if (cd) collection_unref(cd);
+ generic_dialog_close(gdlg);
}
-static void collection_save_or_load_dialog_close_cb(FileDialog *fd, gpointer data)
+static void collection_append_menu_cb(GtkWidget *collection_append_combo, gpointer data)
{
- CollectionData *cd = static_cast<CollectionData *>(data);
+ auto option = static_cast<gint *>(data);
- if (cd) collection_unref(cd);
- file_dialog_close(fd);
+ *option = gtk_combo_box_get_active(GTK_COMBO_BOX(collection_append_combo));
}
-static void collection_save_or_load_dialog(const gchar *path,
- gint type, CollectionData *cd)
+static void collection_save_or_append_dialog(gint type, CollectionData *cd)
{
- FileDialog *fd;
- GtkWidget *parent = NULL;
- CollectWindow *cw;
+ GenericDialog *gdlg;
const gchar *title;
- const gchar *btntext;
- gpointer btnfunc;
- const gchar *stock_id;
+ GList *collection_list = nullptr;
+
+ if (!cd) return;
+
+ collection_ref(cd);
if (type == DIALOG_SAVE || type == DIALOG_SAVE_CLOSE)
{
- if (!cd) return;
+ GtkWidget *existing_collections;
+ GtkWidget *save_as_label;
+ GtkWidget *scrolled;
+ GtkWidget *viewport;
+
title = _("Save collection");
- btntext = NULL;
- btnfunc = (gpointer)collection_save_cb;
- stock_id = GTK_STOCK_SAVE;
- }
- else if (type == DIALOG_LOAD)
- {
- title = _("Open collection");
- btntext = NULL;
- btnfunc = (gpointer)collection_load_cb;
- stock_id = GTK_STOCK_OPEN;
+
+ gdlg = file_util_gen_dlg(title, "dlg_collection_save", nullptr, FALSE, collection_save_or_load_dialog_close_cb, cd);
+
+ generic_dialog_add_message(GENERIC_DIALOG(gdlg), nullptr, title, _("Existing collections:"), FALSE);
+ generic_dialog_add_button(gdlg, GQ_ICON_SAVE, _("Save"), collection_save_cb, TRUE);
+
+ collect_manager_list(&collection_list, nullptr, nullptr);
+
+ GString *out_string = g_string_new(nullptr);
+
+ for (GList *work = collection_list; work != nullptr; work = work->next)
+ {
+ auto collection_name = static_cast<const gchar *>(work->data);
+ out_string = g_string_append(out_string, collection_name);
+ out_string = g_string_append(out_string, "\n");
+ }
+ string_list_free(collection_list);
+
+ existing_collections = gtk_label_new(out_string->str);
+ g_string_free(out_string, TRUE);
+
+ scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
+ gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
+ gtk_widget_show(scrolled);
+
+ viewport = gtk_viewport_new(nullptr, nullptr);
+ gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_NONE);
+ gq_gtk_container_add(GTK_WIDGET(viewport), existing_collections);
+ gtk_widget_show(viewport);
+ gtk_widget_show(existing_collections);
+ gq_gtk_container_add(GTK_WIDGET(scrolled), viewport);
+
+ gq_gtk_box_pack_start(GTK_BOX(gdlg->vbox), scrolled, TRUE,TRUE, 0);
+
+ save_as_label = gtk_label_new("Save collection as:");
+ gq_gtk_box_pack_start(GTK_BOX(gdlg->vbox), save_as_label, FALSE,FALSE, 0);
+ gtk_label_set_xalign(GTK_LABEL(save_as_label), 0.0);
+ gtk_widget_show(save_as_label);
+
+ cd->dialog_name_entry = gtk_entry_new();
+ gtk_widget_show(cd->dialog_name_entry);
+
+ gq_gtk_box_pack_start(GTK_BOX(gdlg->vbox), cd->dialog_name_entry, FALSE, FALSE, 0);
+
+ gq_gtk_entry_set_text(GTK_ENTRY(cd->dialog_name_entry), cd->name);
+ gtk_widget_grab_focus(cd->dialog_name_entry);
+ gtk_widget_show(GENERIC_DIALOG(gdlg)->dialog);
}
else
{
- if (!cd) return;
+ CollectWindow *cw;
+ GtkWidget *parent = nullptr;
+ GtkWidget *collection_append_combo;
+
title = _("Append collection");
- btntext = _("_Append");
- btnfunc = (gpointer)collection_append_cb;
- stock_id = GTK_STOCK_ADD;
- }
- if (cd) collection_ref(cd);
+ cw = collection_window_find(cd);
+ if (cw) parent = cw->window;
- cw = collection_window_find(cd);
- if (cw) parent = cw->window;
+ gdlg = file_util_gen_dlg(title, "dlg_collection_append", parent, true, nullptr, cd);
- fd = file_util_file_dlg(title, "dlg_collection", parent,
- collection_save_or_load_dialog_close_cb, cd);
+ generic_dialog_add_message(GENERIC_DIALOG(gdlg), nullptr, title, _("Select from existing collections:"), FALSE);
+ generic_dialog_add_button(gdlg, GQ_ICON_CANCEL, _("Cancel"), nullptr, TRUE);
+ generic_dialog_add_button(gdlg, GQ_ICON_ADD, _("_Append"), collection_append_cb, TRUE);
- generic_dialog_add_message(GENERIC_DIALOG(fd), NULL, title, NULL, FALSE);
- file_dialog_add_button(fd, stock_id, btntext, (void (*)(FileDialog *, gpointer))btnfunc, TRUE);
+ collect_manager_list(&collection_list, nullptr, nullptr);
- file_dialog_add_path_widgets(fd, get_collections_dir(), path,
- "collection_load_save", GQ_COLLECTION_EXT, _("Collection Files"));
+ collection_append_combo = gtk_combo_box_text_new();
- fd->type = type;
+ for (GList *work = collection_list; work != nullptr; work = work->next)
+ {
+ auto collection_name = static_cast<const gchar *>(work->data);
+ gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(collection_append_combo), collection_name);
+ }
+ string_list_free(collection_list);
- gtk_widget_show(GENERIC_DIALOG(fd)->dialog);
-}
+ gtk_combo_box_set_active(GTK_COMBO_BOX(collection_append_combo), 0);
-void collection_dialog_save_as(gchar *path, CollectionData *cd)
-{
- if (!path) path = cd->path;
- if (!path) path = cd->name;
+ g_signal_connect(G_OBJECT(collection_append_combo), "changed", G_CALLBACK(collection_append_menu_cb), &cd->collection_append_index);
+
+ gtk_widget_show(collection_append_combo);
- collection_save_or_load_dialog(path, DIALOG_SAVE, cd);
+ gq_gtk_box_pack_start(GTK_BOX(gdlg->vbox), collection_append_combo, TRUE,TRUE, 0);
+ gtk_widget_show(GENERIC_DIALOG(gdlg)->dialog);
+ }
}
-void collection_dialog_save_close(gchar *path, CollectionData *cd)
+void collection_dialog_save_as(CollectionData *cd)
{
- if (!path) path = cd->path;
- if (!path) path = cd->name;
-
- collection_save_or_load_dialog(path, DIALOG_SAVE_CLOSE, cd);
+ collection_save_or_append_dialog(DIALOG_SAVE, cd);
}
-void collection_dialog_load(gchar *path)
+void collection_dialog_save_close(CollectionData *cd)
{
- collection_save_or_load_dialog(path, DIALOG_LOAD, NULL);
+ collection_save_or_append_dialog(DIALOG_SAVE_CLOSE, cd);
}
-void collection_dialog_append(gchar *path, CollectionData *cd)
+void collection_dialog_append(CollectionData *cd)
{
- collection_save_or_load_dialog(path, DIALOG_APPEND, cd);
+ collection_save_or_append_dialog(DIALOG_APPEND, cd);
}
/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */