diff --git a/src/c-main-window.c b/src/c-main-window.c index 2afc87b..43d4f86 100644 --- a/src/c-main-window.c +++ b/src/c-main-window.c @@ -10,7 +10,8 @@ G_DEFINE_TYPE(CMainWindow, c_main_window, HILDON_TYPE_STACKABLE_WINDOW) enum { - COL_NAME = 0, + COL_ID = 0, + COL_NAME, COL_CURRENT, COL_TOTAL, NUM_COLS @@ -20,6 +21,9 @@ static void c_main_window_add_menu(CMainWindow *window); static void c_main_window_on_new(GtkWidget *widget, GtkWindow *window); static void c_main_window_on_selection_changed(GtkTreeSelection *selection, gpointer user_data); +static void c_main_window_on_add_clicked(GtkWidget *widget, gpointer user_data); +static void c_main_window_on_remove_clicked(GtkWidget *widget, + gpointer user_data); GtkWidget *c_main_window_new(void) { @@ -36,7 +40,8 @@ void c_main_window_load(CMainWindow *self) while (list) { struct collection *col = list->data; - c_main_window_add_line(self, col->name, col->current_qty, col->total_qty); + c_main_window_add_line(self, col->id, col->name, + col->current_qty, col->total_qty); list = g_list_next(list); } @@ -44,12 +49,14 @@ void c_main_window_load(CMainWindow *self) } void c_main_window_add_line(CMainWindow *window, + gint id, const gchar *name, gint current_qty, gint total_qty) { gtk_list_store_append(window->store, &window->iter); gtk_list_store_set(window ->store, &window->iter, + COL_ID, id, COL_NAME, name, COL_CURRENT, current_qty, COL_TOTAL, total_qty, @@ -81,8 +88,8 @@ static void c_main_window_init(CMainWindow *window) GtkWidget *view; GtkWidget *vbox; GtkWidget *hbuttonbox; + GtkWidget *pannablearea; GtkTreeViewColumn *current_column; - GtkTreeSelection *selection; int index; index = -1; @@ -93,17 +100,25 @@ static void c_main_window_init(CMainWindow *window) g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL); window->store = gtk_list_store_new(NUM_COLS, + G_TYPE_INT, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT); vbox = gtk_vbox_new(FALSE, 0); - view = gtk_tree_view_new(); - gtk_box_pack_start(GTK_BOX(vbox), view, TRUE, TRUE, 0); + pannablearea = hildon_pannable_area_new(); + g_object_set(G_OBJECT(pannablearea), + "mov-mode", HILDON_MOVEMENT_MODE_VERT, + NULL); + gtk_box_pack_start(GTK_BOX(vbox), pannablearea, TRUE, TRUE, 0); - selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view)); - g_signal_connect(selection, "changed", + view = gtk_tree_view_new(); + hildon_pannable_area_add_with_viewport(HILDON_PANNABLE_AREA(pannablearea), + view); + + window->selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view)); + g_signal_connect(window->selection, "changed", G_CALLBACK(c_main_window_on_selection_changed), (gpointer)window); @@ -149,11 +164,28 @@ static void c_main_window_init(CMainWindow *window) gtk_button_box_set_layout(GTK_BUTTON_BOX(hbuttonbox), GTK_BUTTONBOX_END); gtk_box_pack_start(GTK_BOX(vbox), hbuttonbox, FALSE, TRUE, 0); - window->add_button = gtk_button_new_from_stock(GTK_STOCK_ADD); + window->add_button = + hildon_button_new_with_text(HILDON_SIZE_AUTO_WIDTH | + HILDON_SIZE_FINGER_HEIGHT, + HILDON_BUTTON_ARRANGEMENT_HORIZONTAL, + "Add", + NULL); + g_signal_connect(window->add_button, "clicked", + G_CALLBACK(c_main_window_on_add_clicked), + (gpointer)window); gtk_box_pack_start(GTK_BOX(hbuttonbox), window->add_button, FALSE, FALSE, 0); - window->remove_button = gtk_button_new_from_stock(GTK_STOCK_REMOVE); - gtk_box_pack_start(GTK_BOX(hbuttonbox), window->remove_button, FALSE, FALSE, 0); + window->remove_button = + hildon_button_new_with_text(HILDON_SIZE_AUTO_WIDTH | + HILDON_SIZE_FINGER_HEIGHT, + HILDON_BUTTON_ARRANGEMENT_HORIZONTAL, + "Remove", + NULL); + g_signal_connect(window->remove_button, "clicked", + G_CALLBACK(c_main_window_on_remove_clicked), + (gpointer)window); + gtk_box_pack_start(GTK_BOX(hbuttonbox), + window->remove_button, FALSE, FALSE, 0); gtk_container_add(GTK_CONTAINER(window), vbox); @@ -223,3 +255,54 @@ static void c_main_window_on_selection_changed(GtkTreeSelection *selection, else c_main_window_set_has_select(self); } + +static void c_main_window_on_add_clicked(GtkWidget *widget, gpointer user_data) +{ + CMainWindow *self; + gint count; + + self = (CMainWindow *)user_data; + count = gtk_tree_selection_count_selected_rows(self->selection); + + if (count > 0) { + GtkTreeModel *model; + + if (gtk_tree_selection_get_selected(self->selection, &model, &self->iter)) { + gint id; + gint current_count; + + gtk_tree_model_get(model, &self->iter, COL_ID, &id, -1); + gtk_tree_model_get(model, &self->iter, COL_CURRENT, ¤t_count, -1); + if (data_add_to_series(id, 1)) + gtk_list_store_set(GTK_LIST_STORE(self->store), &self->iter, + COL_CURRENT, current_count + 1, -1); + + } + } +} + +static void c_main_window_on_remove_clicked(GtkWidget *widget, + gpointer user_data) +{ + CMainWindow *self; + gint count; + + self = (CMainWindow *)user_data; + count = gtk_tree_selection_count_selected_rows(self->selection); + + if (count > 0) { + GtkTreeModel *model; + + if (gtk_tree_selection_get_selected(self->selection, &model, &self->iter)) { + gint id; + gint current_count; + + gtk_tree_model_get(model, &self->iter, COL_ID, &id, -1); + gtk_tree_model_get(model, &self->iter, COL_CURRENT, ¤t_count, -1); + + if (current_count > 0 && data_add_to_series(id, -1)) + gtk_list_store_set(GTK_LIST_STORE(self->store), &self->iter, + COL_CURRENT, current_count - 1, -1); + } + } +} diff --git a/src/c-main-window.h b/src/c-main-window.h index 29af0f5..2b9ea1c 100644 --- a/src/c-main-window.h +++ b/src/c-main-window.h @@ -45,9 +45,11 @@ struct _CMainWindow GtkTreeIter iter; GtkListStore *store; + GtkTreeSelection *selection; GtkWidget *add_button; GtkWidget *remove_button; + GtkWidget *edit_button; }; GType c_main_window_get_type(void); @@ -56,6 +58,7 @@ GtkWidget *c_main_window_new(void); void c_main_window_load(CMainWindow *self); void c_main_window_add_line(CMainWindow *window, + gint id, const gchar *name, gint current_qty, gint total_qty); diff --git a/src/data.c b/src/data.c index 0013ed2..82b5570 100644 --- a/src/data.c +++ b/src/data.c @@ -9,7 +9,7 @@ #include "collections.h" static gboolean data_check_and_create_database(gchar *data_file); -static gint data_create_new_database(const char *filename); +static gint data_create_new_database(const gchar *filename); GList *data_get_series(void) { @@ -24,7 +24,8 @@ GList *data_get_series(void) if (sqlite3_open(data_file, &database) == SQLITE_OK) { int res; const char *sqlStatement = - " SELECT name, " + " SELECT id, " + " name, " " current_qty, " " total_qty " " FROM collection " @@ -38,11 +39,12 @@ GList *data_get_series(void) if (res == SQLITE_OK) { while (sqlite3_step(statement) == SQLITE_ROW) { struct collection *col = - (struct collection *)malloc(sizeof(struct collection *)); + (struct collection *)malloc(sizeof(struct collection)); - col->name = g_strdup(sqlite3_column_text(statement, 0)); - col->current_qty = sqlite3_column_int(statement, 1); - col->total_qty = sqlite3_column_int(statement, 2); + col->id = sqlite3_column_int(statement, 0); + col->name = g_strdup(sqlite3_column_text(statement, 1)); + col->current_qty = sqlite3_column_int(statement, 2); + col->total_qty = sqlite3_column_int(statement, 3); list = g_list_append(list, (gpointer)col); } @@ -51,14 +53,14 @@ GList *data_get_series(void) g_print("error %d: %s\n", res, sqlite3_errmsg(database)); /* Release the compiled statement from memory */ sqlite3_finalize(statement); - } - sqlite3_close(database); + } + sqlite3_close(database); } return list; } -gboolean data_add_series(gchar *name, int total_qty) +gboolean data_add_series(gchar *name, gint total_qty) { sqlite3 *database; sqlite3_stmt *statement; @@ -87,6 +89,38 @@ gboolean data_add_series(gchar *name, int total_qty) return FALSE; } +gboolean data_add_to_series(gint collection_id, gint count) +{ + sqlite3 *database; + sqlite3_stmt *statement; + gchar *data_file; + + g_print("collection_id: %d, count: %d\n", collection_id, count); + + data_file = collections_get_data_file(); + + if (data_check_and_create_database(data_file)) { + if (sqlite3_open(data_file, &database) == SQLITE_OK) { + int res; + const char *sqlStatement = + g_strdup_printf("UPDATE collection " + " SET current_qty = current_qty + %d " + " WHERE id = %d", count, collection_id); + + res = sqlite3_prepare_v2(database, + sqlStatement, + strlen(sqlStatement), + &statement, NULL); + if (res == SQLITE_OK) { + if (sqlite3_step(statement) == SQLITE_DONE) + return TRUE; + } + } + } + + return FALSE; +} + static gboolean data_check_and_create_database(gchar *data_file) { if (!access(data_file, R_OK) == 0) @@ -98,7 +132,7 @@ static gboolean data_check_and_create_database(gchar *data_file) return TRUE; } -static gint data_create_new_database(const char *filename) +static gint data_create_new_database(const gchar *filename) { sqlite3 *db; char *zErrMsg = 0; diff --git a/src/data.h b/src/data.h index 3ebbb2e..875fe68 100644 --- a/src/data.h +++ b/src/data.h @@ -2,10 +2,12 @@ struct collection { - const gchar *name; - gint current_qty; - gint total_qty; + int id; + int current_qty; + int total_qty; + char *name; }; GList *data_get_series(void); -gboolean data_add_series(gchar *name, int total_qty); +gboolean data_add_series(gchar *name, gint total_qty); +gboolean data_add_to_series(gint id, gint count);