From e963880636c53d1ecd11b62f1be8a3e3c0dd9160 Mon Sep 17 00:00:00 2001 From: Tom Willemsen Date: Thu, 21 Oct 2010 19:29:14 +0200 Subject: Edit screen --- src/c-edit-window.c | 125 +++++++++++++++++++++++++++++++++++++------------ src/c-edit-window.h | 10 +++- src/data.c | 131 ++++++++++++++++++++++++++++++++++++++++++++-------- src/data.h | 8 ++-- src/interface.c | 2 +- 5 files changed, 223 insertions(+), 53 deletions(-) diff --git a/src/c-edit-window.c b/src/c-edit-window.c index 488a5f3..999daf7 100644 --- a/src/c-edit-window.c +++ b/src/c-edit-window.c @@ -1,30 +1,90 @@ #include "c-edit-window.h" #include +#include #include "data.h" +enum { + C_EDIT_PROP_0, + C_EDIT_PROP_CID +}; + +static void c_edit_window_set_collection_id(CEditWindow *self, + gint collection_id); + G_DEFINE_TYPE(CEditWindow, c_edit_window, HILDON_TYPE_STACKABLE_WINDOW) -GtkWidget *c_edit_window_new(void) +GtkWidget *c_edit_window_new(gint collection_id) +{ + g_print("1: %d\n", collection_id); + return g_object_new(C_TYPE_EDIT_WINDOW, "collection-id", collection_id, NULL); +} + +static void c_edit_window_set_property(GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + CEditWindow *self = C_EDIT_WINDOW(object); + + switch (property_id) { + case C_EDIT_PROP_CID: + g_print("2: %d\n", g_value_get_int(value)); + self->collection_id = g_value_get_int(value); + break; + default: + /* We don't have any other properties */ + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec); + break; + } +} + +static void c_edit_window_get_property(GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) { - return g_object_new(C_TYPE_EDIT_WINDOW, NULL); + CEditWindow *self = C_EDIT_WINDOW(object); + + switch (property_id) { + case C_EDIT_PROP_CID: + g_value_set_int(value, self->collection_id); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec); + break; + } } -static void c_edit_window_class_init(CEditWindowClass *class) -{} +static void c_edit_window_class_init(CEditWindowClass *klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS(klass); + GParamSpec *pspec; + + gobject_class->set_property = c_edit_window_set_property; + gobject_class->get_property = c_edit_window_get_property; + + pspec = g_param_spec_int("collection-id", + "ID for the collection", + "Set the collection-id", + 0, + INT_MAX, + 0, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE); + g_object_class_install_property(gobject_class, + C_EDIT_PROP_CID, + pspec); +} static void c_edit_window_init(CEditWindow *self) { GtkWidget *pannablearea; GtkWidget *table; GtkWidget *nameclabel; - GtkWidget *namelabel; GtkWidget *haveclabel; - GtkWidget *havelabel; GtkWidget *totalclabel; - GtkWidget *totallabel; - GtkWidget *layout; - int i; - struct collection test = { 0, 3, 10, "Biomega" }; + GtkWidget *vbox; + + g_print("3: %d\n", self->collection_id); pannablearea = hildon_pannable_area_new(); g_object_set(G_OBJECT(pannablearea), @@ -41,9 +101,9 @@ static void c_edit_window_init(CEditWindow *self) gtk_table_attach(GTK_TABLE(table), nameclabel, 0, 1, 0, 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0); - namelabel = gtk_label_new(test.name); - gtk_misc_set_alignment(GTK_MISC(namelabel), 1.0, 0.5); - gtk_table_attach(GTK_TABLE(table), namelabel, 1, 2, 0, 1, + self->name_label = gtk_label_new(""); + gtk_misc_set_alignment(GTK_MISC(self->name_label), 1.0, 0.5); + gtk_table_attach(GTK_TABLE(table), self->name_label, 1, 2, 0, 1, GTK_EXPAND | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0); haveclabel = gtk_label_new("You have:"); @@ -51,9 +111,9 @@ static void c_edit_window_init(CEditWindow *self) gtk_table_attach(GTK_TABLE(table), haveclabel, 0, 1, 1, 2, GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0); - havelabel = gtk_label_new(g_strdup_printf("%d", test.current_qty)); - gtk_misc_set_alignment(GTK_MISC(havelabel), 1.0, 0.5); - gtk_table_attach(GTK_TABLE(table), havelabel, 1, 2, 1, 2, + self->have_label = gtk_label_new(""); + gtk_misc_set_alignment(GTK_MISC(self->have_label), 1.0, 0.5); + gtk_table_attach(GTK_TABLE(table), self->have_label, 1, 2, 1, 2, GTK_EXPAND | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0); totalclabel = gtk_label_new("There are:"); @@ -61,23 +121,30 @@ static void c_edit_window_init(CEditWindow *self) gtk_table_attach(GTK_TABLE(table), totalclabel, 0, 1, 2, 3, GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0); - totallabel = gtk_label_new((test.total_qty == 0) - ? "?" - : g_strdup_printf("%d", test.total_qty)); - gtk_misc_set_alignment(GTK_MISC(totallabel), 1.0, 0.5); - gtk_table_attach(GTK_TABLE(table), totallabel, 1, 2, 2, 3, + self->total_label = gtk_label_new(""); + gtk_misc_set_alignment(GTK_MISC(self->total_label), 1.0, 0.5); + gtk_table_attach(GTK_TABLE(table), self->total_label, 1, 2, 2, 3, GTK_EXPAND | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0); - /*layout = gtk_layout_new(NULL, NULL); - gtk_table_attach(GTK_TABLE(table), layout, 0, 2, 3, 4, + vbox = gtk_vbox_new(TRUE, 0); + gtk_table_attach(GTK_TABLE(table), vbox, 0, 2, 3, 4, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0); +} + +static void c_edit_window_set_collection_id(CEditWindow *self, + gint collection_id) +{ + struct collection *col; + gint *items; - for (i = 0; i < test.total_qty; i++) { - GtkWidget *button; + col = data_get_series_by_id(collection_id); + items = data_get_items_by_collection_id(collection_id); - button = hildon_check_button_new(HILDON_SIZE_FINGER_HEIGHT | - HILDON_SIZE_AUTO_WIDTH); - gtk_button_set_label(GTK_BUTTON(button), g_strdup_printf("%d", i)); + gtk_label_set_text(GTK_LABEL(self->name_label), col->name); + gtk_label_set_text(GTK_LABEL(self->have_label), + g_strdup_printf("%d", col->current_qty)); + gtk_label_set_text(GTK_LABEL(self->total_label), + g_strdup_printf("%d", col->total_qty)); - }*/ + g_free(col); } diff --git a/src/c-edit-window.h b/src/c-edit-window.h index e017841..e63d30e 100644 --- a/src/c-edit-window.h +++ b/src/c-edit-window.h @@ -42,11 +42,17 @@ struct _CEditWindowClass struct _CEditWindow { HildonStackableWindow parent; + + GtkWidget *name_label; + GtkWidget *have_label; + GtkWidget *total_label; + + gint collection_id; }; -GType c_edit_window_get_type(void); +GType c_edit_window_get_type(void); -GtkWidget *c_edit_window_new(void); +GtkWidget *c_edit_window_new(gint collection_id); G_END_DECLS diff --git a/src/data.c b/src/data.c index 82b5570..e3c0703 100644 --- a/src/data.c +++ b/src/data.c @@ -8,8 +8,9 @@ #include #include "collections.h" -static gboolean data_check_and_create_database(gchar *data_file); -static gint data_create_new_database(const gchar *filename); +static gboolean data_check_and_create_database(gchar *data_file); +static gint data_create_new_database(const gchar *filename); +static struct collection *data_get_collection_from_stmt(sqlite3_stmt *stmt); GList *data_get_series(void) { @@ -38,14 +39,7 @@ GList *data_get_series(void) &statement, NULL); if (res == SQLITE_OK) { while (sqlite3_step(statement) == SQLITE_ROW) { - struct collection *col = - (struct collection *)malloc(sizeof(struct collection)); - - 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); - + struct collection *col = data_get_collection_from_stmt(statement); list = g_list_append(list, (gpointer)col); } } @@ -53,19 +47,103 @@ 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; } +struct collection *data_get_series_by_id(gint collection_id) +{ + sqlite3 *database; + sqlite3_stmt *statement; + gchar *data_file; + struct collection *col = NULL; + + 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 *sql = g_strdup_printf( + " SELECT id, " + " name, " + " current_qty, " + " total_qty " + " FROM collection " + " WHERE id = %d ", collection_id); + + res = sqlite3_prepare_v2(database, sql, strlen(sql), &statement, NULL); + if (res == SQLITE_OK) { + if (sqlite3_step(statement) == SQLITE_ROW) { + col = data_get_collection_from_stmt(statement); + } + } + sqlite3_finalize(statement); + } + sqlite3_close(database); + } + + return col; +} + +gint *data_get_items_by_collection_id(gint collection_id) +{ + gint count; + gint *volumes; + sqlite3 *database; + sqlite3_stmt *statement; + gchar *data_file; + + data_file = collections_get_data_file(); + count = 0; + + if (data_check_and_create_database(data_file)) { + if (sqlite3_open(data_file, &database) == SQLITE_OK) { + int res; + const char *sql = g_strdup_printf( + " SELECT COUNT(id) " + " FROM items " + " WHERE collection_id = %d ", collection_id); + + res = sqlite3_prepare_v2(database, sql, strlen(sql), &statement, NULL); + if (res == SQLITE_OK) { + if (sqlite3_step(statement) == SQLITE_ROW) { + count = sqlite3_column_int(statement, 0); + } + } + sqlite3_finalize(statement); + + if (count > 0) { + sql = g_strdup_printf( + " SELECT id " + " FROM items " + " WHERE collection_id = %d ", collection_id); + + res = sqlite3_prepare_v2(database, sql, strlen(sql), &statement, NULL); + if (res == SQLITE_OK) { + gint i = 0; + while (sqlite3_step(statement) == SQLITE_ROW) { + volumes[i++] = sqlite3_column_int(statement, 0); + } + } + } + } + sqlite3_close(database); + } + + return volumes; +} + gboolean data_add_series(gchar *name, gint total_qty) { sqlite3 *database; sqlite3_stmt *statement; gchar *data_file; + gboolean result; + result = FALSE; data_file = collections_get_data_file(); if (data_check_and_create_database(data_file)) { @@ -81,12 +159,14 @@ gboolean data_add_series(gchar *name, gint total_qty) &statement, NULL); if (res == SQLITE_OK) { if (sqlite3_step(statement) == SQLITE_DONE) - return TRUE; + result = TRUE; } + sqlite3_finalize(statement); } + sqlite3_close(database); } - return FALSE; + return result; } gboolean data_add_to_series(gint collection_id, gint count) @@ -94,10 +174,10 @@ 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); + gboolean result; data_file = collections_get_data_file(); + result = FALSE; if (data_check_and_create_database(data_file)) { if (sqlite3_open(data_file, &database) == SQLITE_OK) { @@ -113,12 +193,14 @@ gboolean data_add_to_series(gint collection_id, gint count) &statement, NULL); if (res == SQLITE_OK) { if (sqlite3_step(statement) == SQLITE_DONE) - return TRUE; + result = TRUE; } + sqlite3_finalize(statement); } + sqlite3_close(database); } - return FALSE; + return result; } static gboolean data_check_and_create_database(gchar *data_file) @@ -185,3 +267,16 @@ static gint data_create_new_database(const gchar *filename) sqlite3_close(db); return 0; } + +static struct collection *data_get_collection_from_stmt(sqlite3_stmt *stmt) +{ + struct collection *collection = + (struct collection *)malloc(sizeof(struct collection)); + + collection->id = sqlite3_column_int(stmt, 0); + collection->name = g_strdup(sqlite3_column_text(stmt, 1)); + collection->current_qty = sqlite3_column_int(stmt, 2); + collection->total_qty = sqlite3_column_int(stmt, 3); + + return collection; +} diff --git a/src/data.h b/src/data.h index 875fe68..c97e228 100644 --- a/src/data.h +++ b/src/data.h @@ -8,6 +8,8 @@ struct collection char *name; }; -GList *data_get_series(void); -gboolean data_add_series(gchar *name, gint total_qty); -gboolean data_add_to_series(gint id, gint count); +GList *data_get_series(void); +struct collection *data_get_series_by_id(gint collection_id); +gint *data_get_items_by_collection_id(gint collection_id); +gboolean data_add_series(gchar *name, gint total_qty); +gboolean data_add_to_series(gint id, gint count); diff --git a/src/interface.c b/src/interface.c index b22e65e..9d1b0f1 100644 --- a/src/interface.c +++ b/src/interface.c @@ -22,7 +22,7 @@ void interface_show_edit_window(void) stack = hildon_window_stack_get_default(); - window = c_edit_window_new(); + window = c_edit_window_new(1); hildon_window_stack_push(stack, HILDON_STACKABLE_WINDOW(window), NULL); gtk_widget_show_all(window); } -- cgit v1.2.3-54-g00ecf