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.
This commit is contained in:
Tom Willemsen 2012-10-26 00:25:51 +02:00
parent 7ffc87d99d
commit 33b73cac14
4 changed files with 79 additions and 17 deletions

View file

@ -57,6 +57,25 @@ data_add_volume_to_manga(gint manga_id, gint volume)
return ret; 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 * GList *
data_get_incomplete_manga(void) data_get_incomplete_manga(void)
{ {
@ -255,6 +274,7 @@ data_update_manga(gint manga_id, const gchar *name, gint total_qty)
"AND id > %d", "AND id > %d",
manga_id, total_qty); manga_id, total_qty);
ret = execute_non_query(sql); ret = execute_non_query(sql);
g_free(sql);
} }
return ret; return ret;

View file

@ -22,6 +22,7 @@ typedef struct
gboolean data_add_manga(const gchar*, gint); gboolean data_add_manga(const gchar*, gint);
gboolean data_add_to_manga(gint, gint); gboolean data_add_to_manga(gint, gint);
gboolean data_add_volume_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_incomplete_manga(void);
GList *data_get_manga(void); GList *data_get_manga(void);
Manga *data_get_manga_by_id(gint); Manga *data_get_manga_by_id(gint);

View file

@ -24,6 +24,7 @@ static void eom_detail_window_class_init(EomDetailWindowClass*);
static void eom_detail_window_init(EomDetailWindow *self); static void eom_detail_window_init(EomDetailWindow *self);
static void finalize(GObject*); static void finalize(GObject*);
static void get_property(GObject*, guint, GValue*, GParamSpec*); static void get_property(GObject*, guint, GValue*, GParamSpec*);
static void on_delete(GtkWidget*, gpointer);
static void on_edit(GtkWidget*, EomDetailWindow*); static void on_edit(GtkWidget*, EomDetailWindow*);
static void on_volume_read_toggled(GtkToggleButton*, gpointer); static void on_volume_read_toggled(GtkToggleButton*, gpointer);
static void on_volume_toggled(GtkToggleButton*, gpointer); static void on_volume_toggled(GtkToggleButton*, gpointer);
@ -102,18 +103,26 @@ static void
add_menu(EomDetailWindow *window) add_menu(EomDetailWindow *window)
{ {
HildonAppMenu *appmenu; HildonAppMenu *appmenu;
GtkWidget *edit_button; GtkWidget *button;
appmenu = HILDON_APP_MENU(hildon_app_menu_new()); appmenu = HILDON_APP_MENU(hildon_app_menu_new());
edit_button = hildon_gtk_button_new(HILDON_SIZE_AUTO); button = hildon_gtk_button_new(HILDON_SIZE_AUTO);
gtk_button_set_label(GTK_BUTTON(edit_button), "Edit..."); gtk_button_set_label(GTK_BUTTON(button), "Edit...");
g_signal_connect_after(edit_button, "clicked", G_CALLBACK(on_edit), g_signal_connect_after(button, "clicked", G_CALLBACK(on_edit),
window); 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)); gtk_widget_show_all(GTK_WIDGET(appmenu));
hildon_stackable_window_set_main_menu(HILDON_STACKABLE_WINDOW(window), hildon_stackable_window_set_main_menu(
HILDON_APP_MENU(appmenu)); HILDON_STACKABLE_WINDOW(window), appmenu
);
} }
static void 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 static void
on_edit(GtkWidget *widget, EomDetailWindow *self) on_edit(GtkWidget *widget, EomDetailWindow *self)
{ {

View file

@ -6,8 +6,8 @@
#include <hildon/hildon.h> #include <hildon/hildon.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include "eom-new-item-dialog.h"
#include "data.h" #include "data.h"
#include "eom-new-item-dialog.h"
#include "interface.h" #include "interface.h"
G_DEFINE_TYPE(EomMainWindow, eom_main_window, G_DEFINE_TYPE(EomMainWindow, eom_main_window,
@ -35,7 +35,7 @@ struct filter_args {
static void add_menu(EomMainWindow*); static void add_menu(EomMainWindow*);
static void eom_main_window_class_init(EomMainWindowClass*); static void eom_main_window_class_init(EomMainWindowClass*);
static void eom_main_window_init(EomMainWindow*); 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_filter(GtkWidget*, struct filter_args*);
static void on_new(GtkWidget*, GtkWindow*); static void on_new(GtkWidget*, GtkWindow*);
static void on_row_activated(GtkWidget*, GtkTreePath*, GtkTreeViewColumn*, gpointer); static void on_row_activated(GtkWidget*, GtkTreePath*, GtkTreeViewColumn*, gpointer);
@ -236,14 +236,11 @@ eom_main_window_init(EomMainWindow *window)
g_object_unref(window->store); g_object_unref(window->store);
} }
static gboolean static void
on_edit_closed(GtkWidget *widget, GdkEvent *event, gpointer user_data) on_detail_hidden(GtkWidget *widget, gpointer user_data)
{ {
EomMainWindow *self = user_data; EomMainWindow *self = EOM_MAIN_WINDOW(user_data);
eom_main_window_load(self); eom_main_window_load(self);
return FALSE;
} }
static void static void
@ -294,6 +291,5 @@ on_row_activated(GtkWidget *treeview, GtkTreePath *path,
gtk_tree_model_get(model, &iter, COL_ID, &id, -1); gtk_tree_model_get(model, &iter, COL_ID, &id, -1);
window = interface_show_detail_window(id); window = interface_show_detail_window(id);
g_signal_connect(window, "delete-event", G_CALLBACK(on_edit_closed), g_signal_connect(window, "hide", G_CALLBACK(on_detail_hidden), self);
self);
} }