From 33b73cac14f28e91cd90e0fb98579bf7690f637f Mon Sep 17 00:00:00 2001 From: Tom Willemsen Date: Fri, 26 Oct 2012 00:25:51 +0200 Subject: [PATCH] Add "Delete" button to detail window * src/data.c (data_delete_manga): New function, deletes a manga and its associated volumes. (data_update_manga): Extra free. * src/eom-detail-window.c (add_menu): Add a "Delete" button to the menu. (on_delete): New function. Shows a confirmation dialog and if the user pressed "Ok" tries to delete the manga and close the window. * src/eom-main-window.c (on_row_activated): Connect to the `hide' signal instead of the `delete-event' signal. I couldn't find how te emit my own `delete-event' signal, then I found that hide works both when I close a window and when the window manager does it. --- src/data.c | 20 ++++++++++++++ src/data.h | 1 + src/eom-detail-window.c | 59 ++++++++++++++++++++++++++++++++++++----- src/eom-main-window.c | 16 +++++------ 4 files changed, 79 insertions(+), 17 deletions(-) diff --git a/src/data.c b/src/data.c index f2a7996..970ae1a 100644 --- a/src/data.c +++ b/src/data.c @@ -57,6 +57,25 @@ data_add_volume_to_manga(gint manga_id, gint volume) return ret; } +gboolean +data_delete_manga(gint manga_id) +{ + char *sql = g_strdup_printf("DELETE FROM volume " + "WHERE manga_id = %d", manga_id); + gboolean ret = execute_non_query(sql); + + g_free(sql); + + if (ret) { + sql = g_strdup_printf("DELETE FROM manga " + "WHERE id = %d", manga_id); + ret = execute_non_query(sql); + g_free(sql); + } + + return ret; +} + GList * data_get_incomplete_manga(void) { @@ -255,6 +274,7 @@ data_update_manga(gint manga_id, const gchar *name, gint total_qty) "AND id > %d", manga_id, total_qty); ret = execute_non_query(sql); + g_free(sql); } return ret; diff --git a/src/data.h b/src/data.h index eb2cd39..b48b09f 100644 --- a/src/data.h +++ b/src/data.h @@ -22,6 +22,7 @@ typedef struct gboolean data_add_manga(const gchar*, gint); gboolean data_add_to_manga(gint, gint); gboolean data_add_volume_to_manga(gint, gint); +gboolean data_delete_manga(gint); GList *data_get_incomplete_manga(void); GList *data_get_manga(void); Manga *data_get_manga_by_id(gint); diff --git a/src/eom-detail-window.c b/src/eom-detail-window.c index 7ce638e..edd8954 100644 --- a/src/eom-detail-window.c +++ b/src/eom-detail-window.c @@ -24,6 +24,7 @@ static void eom_detail_window_class_init(EomDetailWindowClass*); static void eom_detail_window_init(EomDetailWindow *self); static void finalize(GObject*); static void get_property(GObject*, guint, GValue*, GParamSpec*); +static void on_delete(GtkWidget*, gpointer); static void on_edit(GtkWidget*, EomDetailWindow*); static void on_volume_read_toggled(GtkToggleButton*, gpointer); static void on_volume_toggled(GtkToggleButton*, gpointer); @@ -102,18 +103,26 @@ static void add_menu(EomDetailWindow *window) { HildonAppMenu *appmenu; - GtkWidget *edit_button; + GtkWidget *button; appmenu = HILDON_APP_MENU(hildon_app_menu_new()); - edit_button = hildon_gtk_button_new(HILDON_SIZE_AUTO); - gtk_button_set_label(GTK_BUTTON(edit_button), "Edit..."); - g_signal_connect_after(edit_button, "clicked", G_CALLBACK(on_edit), + button = hildon_gtk_button_new(HILDON_SIZE_AUTO); + gtk_button_set_label(GTK_BUTTON(button), "Edit..."); + g_signal_connect_after(button, "clicked", G_CALLBACK(on_edit), window); - hildon_app_menu_append(appmenu, GTK_BUTTON(edit_button)); + hildon_app_menu_append(appmenu, GTK_BUTTON(button)); + + button = hildon_gtk_button_new(HILDON_SIZE_AUTO); + gtk_button_set_label(GTK_BUTTON(button), "Delete..."); + g_signal_connect_after(button, "clicked", G_CALLBACK(on_delete), + (gpointer)window); + hildon_app_menu_append(appmenu, GTK_BUTTON(button)); + gtk_widget_show_all(GTK_WIDGET(appmenu)); - hildon_stackable_window_set_main_menu(HILDON_STACKABLE_WINDOW(window), - HILDON_APP_MENU(appmenu)); + hildon_stackable_window_set_main_menu( + HILDON_STACKABLE_WINDOW(window), appmenu + ); } static void @@ -223,6 +232,42 @@ get_property(GObject *object, guint property_id, GValue *value, } } +static void +on_delete(GtkWidget *widget, gpointer user_data) +{ + GtkWidget *dialog, *label, *content_area; + EomDetailWindow *self = EOM_DETAIL_WINDOW(user_data); + gint result; + gchar *ttxt, *ctxt; + + ttxt = g_strdup_printf("Delete %s", self->manga->name); + ctxt = g_strdup_printf("Really delete \"%s\"?", self->manga->name); + dialog = gtk_dialog_new_with_buttons( + ttxt, GTK_WINDOW(self), GTK_DIALOG_MODAL, + GTK_STOCK_OK, GTK_RESPONSE_OK, + GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, + NULL + ); + content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); + label = gtk_label_new(ctxt); + + gtk_container_add(GTK_CONTAINER(content_area), label); + gtk_widget_show(label); + + result = gtk_dialog_run(GTK_DIALOG(dialog)); + + gtk_widget_destroy(dialog); + g_free(ttxt); + g_free(ctxt); + + if (result == GTK_RESPONSE_OK) { + if (data_delete_manga(self->manga->id)) { + gtk_widget_hide(GTK_WIDGET(self)); + gtk_widget_destroy(GTK_WIDGET(self)); + } + } +} + static void on_edit(GtkWidget *widget, EomDetailWindow *self) { diff --git a/src/eom-main-window.c b/src/eom-main-window.c index ea3a1df..9dd2a5c 100644 --- a/src/eom-main-window.c +++ b/src/eom-main-window.c @@ -6,8 +6,8 @@ #include #include -#include "eom-new-item-dialog.h" #include "data.h" +#include "eom-new-item-dialog.h" #include "interface.h" G_DEFINE_TYPE(EomMainWindow, eom_main_window, @@ -35,7 +35,7 @@ struct filter_args { static void add_menu(EomMainWindow*); static void eom_main_window_class_init(EomMainWindowClass*); static void eom_main_window_init(EomMainWindow*); -static gboolean on_edit_closed(GtkWidget*, GdkEvent*, gpointer); +static void on_detail_hidden(GtkWidget*, gpointer); static void on_filter(GtkWidget*, struct filter_args*); static void on_new(GtkWidget*, GtkWindow*); static void on_row_activated(GtkWidget*, GtkTreePath*, GtkTreeViewColumn*, gpointer); @@ -236,14 +236,11 @@ eom_main_window_init(EomMainWindow *window) g_object_unref(window->store); } -static gboolean -on_edit_closed(GtkWidget *widget, GdkEvent *event, gpointer user_data) +static void +on_detail_hidden(GtkWidget *widget, gpointer user_data) { - EomMainWindow *self = user_data; - + EomMainWindow *self = EOM_MAIN_WINDOW(user_data); eom_main_window_load(self); - - return FALSE; } static void @@ -294,6 +291,5 @@ on_row_activated(GtkWidget *treeview, GtkTreePath *path, gtk_tree_model_get(model, &iter, COL_ID, &id, -1); window = interface_show_detail_window(id); - g_signal_connect(window, "delete-event", G_CALLBACK(on_edit_closed), - self); + g_signal_connect(window, "hide", G_CALLBACK(on_detail_hidden), self); }