From 395c534fd9aef1f83a72d21f705f6718b8594f5e Mon Sep 17 00:00:00 2001 From: Tom Willemsen Date: Sat, 20 Oct 2012 01:06:39 +0200 Subject: [PATCH] Add Collect filter This filter, in the main window, shows only the manga that still need to be collected, not the completed ones. * src/data.c (data_get_manga): Use `data_get_manga_for_query'. (data_get_incomplete_manga): New function, get all manga that arent' completely collected yet. (data_get_manga_for_query): New function, fetches all the manga as `query' specifies. * src/data.h: Add `data_get_incomplete_manga'. * src/eom-main-window.c (filter_args): New struct. Contains arguments needed by the filter callback. (eom_main_window_load): Accept a list of manga, this list will be used to show the manga, if it's NULL, a list of all manga is used. (eom_main_window_add_menu): Add `All' and `Collect' filter buttons. (eom_main_window_on_filter): Callback for the filter buttons, reloads the list with the correct manga. * src/eom-main-window.h: Add manga parameter to `eom_main_window_load'. * src/interface.c (interface_show_main_window): Pass `NULL' to `eom_main_window_load' to show all manga. --- src/data.c | 45 ++++++++++++++++++++-------- src/data.h | 1 + src/eom-main-window.c | 70 ++++++++++++++++++++++++++++++++++++------- src/eom-main-window.h | 2 +- src/interface.c | 2 +- 5 files changed, 95 insertions(+), 25 deletions(-) diff --git a/src/data.c b/src/data.c index ac1318c..bb5bec8 100644 --- a/src/data.c +++ b/src/data.c @@ -11,8 +11,36 @@ static gboolean data_check_and_create_database(gchar *data_file); static gint data_create_new_database(const gchar *filename); static Manga *data_get_manga_from_statement(sqlite3_stmt *stmt); +static GList *data_get_manga_for_query(const gchar *query); GList *data_get_manga(void) +{ + const char *sql = + " SELECT id, " + " name, " + " current_qty, " + " total_qty " + " FROM manga " + " ORDER BY name " + " COLLATE NOCASE "; + return data_get_manga_for_query(sql); +} + +GList *data_get_incomplete_manga(void) +{ + const char *sql = + " SELECT id, " + " name, " + " current_qty, " + " total_qty " + " FROM manga " + " WHERE current_qty != total_qty " + " ORDER BY name " + " COLLATE NOCASE "; + return data_get_manga_for_query(sql); +} + +GList *data_get_manga_for_query(const gchar *query) { sqlite3 *database; sqlite3_stmt *statement; @@ -23,20 +51,11 @@ GList *data_get_manga(void) if (data_check_and_create_database(data_file)) { if (sqlite3_open(data_file, &database) == SQLITE_OK) { - int res; - const char *sql = - " SELECT id, " - " name, " - " current_qty, " - " total_qty " - " FROM manga " - " ORDER BY name " - " COLLATE NOCASE "; + int res = sqlite3_prepare_v2(database, + query, + strlen(query), + &statement, NULL); - res = sqlite3_prepare_v2(database, - sql, - strlen(sql), - &statement, NULL); if (res == SQLITE_OK) { while (sqlite3_step(statement) == SQLITE_ROW) { Manga *manga = data_get_manga_from_statement(statement); diff --git a/src/data.h b/src/data.h index 1666427..eebb700 100644 --- a/src/data.h +++ b/src/data.h @@ -24,6 +24,7 @@ struct _manga }; GList *data_get_manga(void); +GList *data_get_incomplete_manga(void); Manga *data_get_manga_by_id(gint manga_id); void data_get_volumes_for_manga(Manga *manga); gboolean data_add_manga(gchar *name, gint total_qty); diff --git a/src/eom-main-window.c b/src/eom-main-window.c index c901401..b80e6cb 100644 --- a/src/eom-main-window.c +++ b/src/eom-main-window.c @@ -1,8 +1,11 @@ #include "eom-main-window.h" -#include -#include + #include #include + +#include +#include + #include "eom-new-item-dialog.h" #include "data.h" #include "interface.h" @@ -17,8 +20,14 @@ enum { NUM_COLS }; +struct filter_args { + EomMainWindow *window; + gint state; +}; + static void eom_main_window_add_menu(EomMainWindow *window); static void eom_main_window_on_new(GtkWidget *widget, GtkWindow *window); +static void eom_main_window_on_filter(GtkWidget *widget, struct filter_args *arg); static void eom_main_window_on_add_clicked(GtkWidget *widget, gpointer user_data); static void eom_main_window_on_remove_clicked(GtkWidget *widget, @@ -36,14 +45,17 @@ GtkWidget *eom_main_window_new(void) return g_object_new(EOM_TYPE_MAIN_WINDOW, NULL); } -void eom_main_window_load(EomMainWindow *self) +void eom_main_window_load(EomMainWindow *self, GList *manga) { GList *list; int i; gtk_list_store_clear(self->store); - list = data_get_manga(); + if (manga) + list = manga; + else + list = data_get_manga(); while (list) { Manga *manga = list->data; @@ -157,10 +169,17 @@ static void eom_main_window_init(EomMainWindow *window) static void eom_main_window_add_menu(EomMainWindow *window) { - GtkWidget *appmenu; + HildonAppMenu *appmenu; GtkWidget *new_button; + GtkWidget *all_filter, *collect_filter; + struct filter_args *args0 = malloc(sizeof(struct filter_args)); + struct filter_args *args1 = malloc(sizeof(struct filter_args)); - appmenu = hildon_app_menu_new(); + args0->window = window; + args0->state = 0; + args1->window = window; + args1->state = 1; + appmenu = HILDON_APP_MENU(hildon_app_menu_new()); new_button = hildon_gtk_button_new(HILDON_SIZE_AUTO); gtk_button_set_label(GTK_BUTTON(new_button), "New Manga"); @@ -169,13 +188,44 @@ static void eom_main_window_add_menu(EomMainWindow *window) G_CALLBACK(eom_main_window_on_new), GTK_WINDOW(window)); - hildon_app_menu_append(HILDON_APP_MENU(appmenu), GTK_BUTTON(new_button)); - gtk_widget_show_all(appmenu); + hildon_app_menu_append(appmenu, GTK_BUTTON(new_button)); + + all_filter = hildon_gtk_radio_button_new(HILDON_SIZE_AUTO, NULL); + gtk_button_set_label(GTK_BUTTON(all_filter), "All"); + g_signal_connect_after(all_filter, "clicked", + G_CALLBACK(eom_main_window_on_filter), + args0); + hildon_app_menu_add_filter(appmenu, GTK_BUTTON(all_filter)); + gtk_toggle_button_set_mode(GTK_TOGGLE_BUTTON(all_filter), FALSE); + + collect_filter = + hildon_gtk_radio_button_new_from_widget(HILDON_SIZE_AUTO, + GTK_RADIO_BUTTON(all_filter)); + gtk_button_set_label(GTK_BUTTON(collect_filter), "Collect"); + g_signal_connect_after(collect_filter, "clicked", + G_CALLBACK(eom_main_window_on_filter), + args1); + hildon_app_menu_add_filter(appmenu, GTK_BUTTON(collect_filter)); + gtk_toggle_button_set_mode(GTK_TOGGLE_BUTTON(collect_filter), FALSE); + + gtk_widget_show_all(GTK_WIDGET(appmenu)); hildon_stackable_window_set_main_menu(HILDON_STACKABLE_WINDOW(window), HILDON_APP_MENU(appmenu)); } +static void eom_main_window_on_filter(GtkWidget *widget, struct filter_args *arg) +{ + GList *manga; + + if (arg->state) + manga = data_get_incomplete_manga(); + else + manga = data_get_manga(); + + eom_main_window_load(arg->window, manga); +} + static void eom_main_window_on_new(GtkWidget *widget, GtkWindow *window) { GtkWidget *dialog; @@ -202,7 +252,7 @@ static void eom_main_window_on_new(GtkWidget *widget, GtkWindow *window) if (name != NULL) { if (data_add_manga(name, total_qty)) - eom_main_window_load(EOM_MAIN_WINDOW(window)); + eom_main_window_load(EOM_MAIN_WINDOW(window), NULL); } } @@ -212,7 +262,7 @@ static gboolean eom_main_window_on_edit_closed(GtkWidget *widget, { EomMainWindow *self = user_data; - eom_main_window_load(self); + eom_main_window_load(self, NULL); return FALSE; } diff --git a/src/eom-main-window.h b/src/eom-main-window.h index 93dd050..4cecb50 100644 --- a/src/eom-main-window.h +++ b/src/eom-main-window.h @@ -51,7 +51,7 @@ GType eom_main_window_get_type(void); GtkWidget *eom_main_window_new(void); -void eom_main_window_load(EomMainWindow *self); +void eom_main_window_load(EomMainWindow *self, GList *manga); void eom_main_window_add_line(EomMainWindow *window, gint id, const gchar *name, diff --git a/src/interface.c b/src/interface.c index 02b8ff4..a897f29 100644 --- a/src/interface.c +++ b/src/interface.c @@ -12,7 +12,7 @@ void interface_show_main_window(void) window = eom_main_window_new(); gtk_widget_show_all(window); - eom_main_window_load(EOM_MAIN_WINDOW(window)); + eom_main_window_load(EOM_MAIN_WINDOW(window), NULL); } GtkWidget *interface_show_edit_window(gint manga_id)