From aa77d2a2f5039f0397f358958b2e5d702bdf9e16 Mon Sep 17 00:00:00 2001 From: Tom Willemsen Date: Sun, 21 Oct 2012 16:24:45 +0200 Subject: [PATCH] Restyle, reindent, refactor (a little) --- src/data.c | 683 +++++++++++++++++--------------------- src/data.h | 46 ++- src/eom-edit-window.c | 477 +++++++++++++------------- src/eom-edit-window.h | 69 ++-- src/eom-main-window.c | 427 ++++++++++++------------ src/eom-main-window.h | 72 ++-- src/eom-new-item-dialog.c | 62 ++-- src/eom-new-item-dialog.h | 22 +- src/eom.c | 14 +- src/eom.h | 5 + 10 files changed, 894 insertions(+), 983 deletions(-) diff --git a/src/data.c b/src/data.c index bb5bec8..e357f2e 100644 --- a/src/data.c +++ b/src/data.c @@ -1,442 +1,359 @@ #include "data.h" -#include -#include -#include -#include -#include -#include + #include +#include +#include +#include +#include +#include +#include + #include "eom.h" -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); +static gboolean check_and_create_database(gchar*); +static gint create_new_database(const gchar*); +static gboolean execute_non_query(const char *sql); +static GList *get_manga_for_query(const gchar*); +static Manga *get_manga_from_statement(sqlite3_stmt*); -GList *data_get_manga(void) +gboolean +data_add_manga(gchar *name, gint total_qty) { - const char *sql = - " SELECT id, " - " name, " - " current_qty, " - " total_qty " - " FROM manga " - " ORDER BY name " - " COLLATE NOCASE "; - return data_get_manga_for_query(sql); + gchar *sql = + g_strdup_printf(" INSERT INTO manga (name, current_qty, " + "total_qty) VALUES ('%s', 0, %d)", + name, total_qty); + gboolean ret = execute_non_query(sql); + + g_free(sql); + + return ret; } -GList *data_get_incomplete_manga(void) +gboolean +data_add_to_manga(gint manga_id, gint count) { - 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); + gchar *sql = + g_strdup_printf(" UPDATE manga " + " SET current_qty = current_qty + %d " + " WHERE id = %d", count, manga_id); + gboolean ret = execute_non_query(sql); + + g_free(sql); + + return ret; } -GList *data_get_manga_for_query(const gchar *query) +gboolean +data_add_volume_to_manga(gint manga_id, gint volume) { - sqlite3 *database; - sqlite3_stmt *statement; - gchar *data_file; - GList *list = NULL; + char *sql = g_strdup_printf(" INSERT INTO volume " + " VALUES (%d, %d, 0) ", + manga_id, volume); + gboolean ret = execute_non_query(sql); - data_file = eom_get_data_file(); + g_free(sql); - if (data_check_and_create_database(data_file)) { - if (sqlite3_open(data_file, &database) == SQLITE_OK) { - int res = sqlite3_prepare_v2(database, - query, - strlen(query), - &statement, NULL); + return ret; +} - if (res == SQLITE_OK) { - while (sqlite3_step(statement) == SQLITE_ROW) { - Manga *manga = data_get_manga_from_statement(statement); - list = g_list_append(list, (gpointer)manga); +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 get_manga_for_query(sql); +} + +GList * +data_get_manga(void) +{ + const char *sql = + " SELECT id, " + " name, " + " current_qty, " + " total_qty " + " FROM manga " + " ORDER BY name " + " COLLATE NOCASE "; + + return get_manga_for_query(sql); +} + +Manga * +data_get_manga_by_id(gint manga_id) +{ + sqlite3 *db; + sqlite3_stmt *stmt; + gchar *data_file; + Manga *manga = NULL; + + data_file = eom_get_data_file(); + + if (check_and_create_database(data_file)) { + if (sqlite3_open(data_file, &db) == SQLITE_OK) { + int res; + char *sql = + g_strdup_printf(" SELECT id, " + " name, " + " current_qty, " + " total_qty " + " FROM manga " + " WHERE id = %d ", manga_id); + + res = sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, NULL); + g_free(sql); + + if (res == SQLITE_OK) { + if (sqlite3_step(stmt) == SQLITE_ROW) { + manga = get_manga_from_statement(stmt); + } + } + + sqlite3_finalize(stmt); } - } - else - g_print("error %d: %s\n", res, sqlite3_errmsg(database)); - - /* Release the compiled statement from memory */ - sqlite3_finalize(statement); + sqlite3_close(db); } - sqlite3_close(database); - } - return list; + return manga; } -Manga *data_get_manga_by_id(gint manga_id) +void +data_get_volumes_for_manga(Manga *manga) { - sqlite3 *database; - sqlite3_stmt *statement; - gchar *data_file; - Manga *manga = NULL; + gint count; + Volume *volumes; + sqlite3 *db; + sqlite3_stmt *stmt; + gchar *data_file; - data_file = eom_get_data_file(); + data_file = eom_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 id, " - " name, " - " current_qty, " - " total_qty " - " FROM manga " - " WHERE id = %d ", manga_id); + if (check_and_create_database(data_file)) { + if (sqlite3_open(data_file, &db) == SQLITE_OK) { + int res; + char *sql = g_strdup_printf(" SELECT COUNT(id) " + " FROM volume " + " WHERE manga_id = %d ", + manga->id); - res = sqlite3_prepare_v2(database, sql, strlen(sql), &statement, NULL); - if (res == SQLITE_OK) { - if (sqlite3_step(statement) == SQLITE_ROW) { - manga = data_get_manga_from_statement(statement); + res = sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, NULL); + g_free(sql); + + if (res == SQLITE_OK) { + if (sqlite3_step(stmt) == SQLITE_ROW) { + count = sqlite3_column_int(stmt, 0); + } + } + + sqlite3_finalize(stmt); + volumes = calloc(sizeof(Volume), count); + + if (count > 0) { + sql = g_strdup_printf(" SELECT id, " + " read " + " FROM volume " + " WHERE manga_id = %d ", + manga->id); + + res = sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, + NULL); + g_free(sql); + + if (res == SQLITE_OK) { + gint i = 0; + while (sqlite3_step(stmt) == SQLITE_ROW) { + volumes[i].number = sqlite3_column_int(stmt, 0); + volumes[i].read = sqlite3_column_int(stmt, 1); + + i++; + }; + } + + sqlite3_finalize(stmt); + } } - } - - sqlite3_finalize(statement); + sqlite3_close(db); } - sqlite3_close(database); - } - return manga; + manga->vol_count = count; + manga->volumes = volumes; } -void data_get_volumes_for_manga(Manga *manga) +gboolean +data_mark_volume_read(int read, gint manga_id, gint volume) { - gint count; - Volume *volumes; - sqlite3 *database; - sqlite3_stmt *statement; - gchar *data_file; + gchar *sql = g_strdup_printf(" UPDATE volume " + " SET read = %d " + " WHERE manga_id = %d " + " AND id = %d ", + read, manga_id, volume); + gboolean ret = execute_non_query(sql); - data_file = eom_get_data_file(); - count = 0; + g_free(sql); - 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 volume " - " WHERE manga_id = %d ", manga->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); - volumes = calloc(sizeof(Volume), count); - - if (count > 0) { - sql = g_strdup_printf( - " SELECT id, " - " read " - " FROM volume " - " WHERE manga_id = %d ", manga->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].number = sqlite3_column_int(statement, 0); - volumes[i].read = sqlite3_column_int(statement, 1); - - i++; - }; - } - - sqlite3_finalize(statement); - } - } - sqlite3_close(database); - } - - manga->vol_count = count; - manga->volumes = volumes; + return ret; } -gboolean data_add_manga(gchar *name, gint total_qty) +gboolean +data_remove_volume_from_manga(gint manga_id, gint volume) { - sqlite3 *database; - sqlite3_stmt *statement; - gchar *data_file; - gboolean result; + char *sql = g_strdup_printf(" DELETE FROM volume " + " WHERE manga_id = %d " + " AND id = %d ", manga_id, volume); + gboolean ret = execute_non_query(sql); - result = FALSE; - data_file = eom_get_data_file(); + g_free(sql); - if (data_check_and_create_database(data_file)) { - if (sqlite3_open(data_file, &database) == SQLITE_OK) { - int res; - const char *sql = g_strdup_printf( - " INSERT INTO manga (name, current_qty, total_qty) " - " VALUES ('%s', 0, %d)", name, total_qty); - - res = sqlite3_prepare_v2(database, - sql, - strlen(sql), - &statement, NULL); - if (res == SQLITE_OK) { - if (sqlite3_step(statement) == SQLITE_DONE) - result = TRUE; - } - - sqlite3_finalize(statement); - } - sqlite3_close(database); - } - - return result; + return ret; } -gboolean data_add_to_manga(gint manga_id, gint count) -{ - sqlite3 *database; - sqlite3_stmt *statement; - gchar *data_file; - gboolean result; - - data_file = eom_get_data_file(); - result = FALSE; - - if (data_check_and_create_database(data_file)) { - if (sqlite3_open(data_file, &database) == SQLITE_OK) { - int res; - const char *sql = g_strdup_printf( - " UPDATE manga " - " SET current_qty = current_qty + %d " - " WHERE id = %d", count, manga_id); - - res = sqlite3_prepare_v2(database, - sql, - strlen(sql), - &statement, NULL); - if (res == SQLITE_OK) { - if (sqlite3_step(statement) == SQLITE_DONE) - result = TRUE; - } - - sqlite3_finalize(statement); - } - sqlite3_close(database); - } - - return result; -} - -gboolean data_add_volume_to_manga(gint manga_id, gint volume) -{ - sqlite3 *database; - sqlite3_stmt *statement; - gchar *data_file; - gboolean result; - - data_file = eom_get_data_file(); - result = FALSE; - - if (data_check_and_create_database(data_file)) { - if (sqlite3_open(data_file, &database) == SQLITE_OK) { - int res; - const char *sql = g_strdup_printf( - " INSERT INTO volume " - " VALUES (%d, %d, 0) ", - manga_id, volume); - - res = sqlite3_prepare_v2(database, - sql, - strlen(sql), - &statement, NULL); - if (res == SQLITE_OK) { - res = sqlite3_step(statement); - if (res == SQLITE_DONE) - result = TRUE; - else - g_print("step did not return DONE, it DID return %d\n", res); - } - else - g_print("res was not OK\n"); - - sqlite3_finalize(statement); - } - else - g_print("database wasn't opened\n"); - sqlite3_close(database); - } - else - g_print("couldn't check or create database\n"); - - return result; -} - -gboolean data_mark_volume_read(int read, gint manga_id, gint volume) -{ - sqlite3 *database; - sqlite3_stmt *statement; - gchar *data_file; - gboolean result; - - data_file = eom_get_data_file(); - result = FALSE; - - if (data_check_and_create_database(data_file)) { - if (sqlite3_open(data_file, &database) == SQLITE_OK) { - int res; - const char *sql = g_strdup_printf( - " UPDATE volume " - " SET read = %d " - " WHERE manga_id = %d " - " AND id = %d ", - read, manga_id, volume); - - res = sqlite3_prepare_v2(database, - sql, - strlen(sql), - &statement, NULL); - if (res == SQLITE_OK) { - res = sqlite3_step(statement); - if (res == SQLITE_DONE) - result = TRUE; - else - g_print("step did not return DONE, it DID return %d\n", res); - } - else - g_print("res was not OK\n"); - - sqlite3_finalize(statement); - } - else - g_print("database wasn't opened\n"); - sqlite3_close(database); - } - else - g_print("couldn't check or create database\n"); - - return result; -} - -gboolean data_remove_volume_from_manga(gint manga_id, gint volume) -{ - sqlite3 *database; - sqlite3_stmt *statement; - gchar *data_file; - gboolean result; - - data_file = eom_get_data_file(); - result = FALSE; - - if (data_check_and_create_database(data_file)) { - if (sqlite3_open(data_file, &database) == SQLITE_OK) { - int res; - const char *sql = g_strdup_printf( - " DELETE FROM volume " - " WHERE manga_id = %d " - " AND id = %d ", manga_id, volume); - - res = sqlite3_prepare_v2(database, - sql, - strlen(sql), - &statement, NULL); - if (res == SQLITE_OK) - if (sqlite3_step(statement) == SQLITE_DONE) - result = TRUE; - else - g_print("step did not return DONE\n"); - else - g_print("result did not return OK\n"); - - sqlite3_finalize(statement); - } - else - g_print("couldn't open database\n"); - sqlite3_close(database); - } - else - g_print("couldn't check or create database\n"); - - return result; -} - -static gboolean data_check_and_create_database(gchar *data_file) +static gboolean +check_and_create_database(gchar *data_file) { if (!access(data_file, R_OK) == 0) - if (data_create_new_database(data_file)) { + if (create_new_database(data_file)) { g_printerr("Couldn't create a new database\n"); + return FALSE; /* Couldn't create database, can't continue */ } return TRUE; } -static gint data_create_new_database(const gchar *filename) +static gint +create_new_database(const gchar *filename) { - sqlite3 *db; - char *zErrMsg = 0; - int rc; + sqlite3 *db; + char *zErrMsg = 0; + int rc; - if (access(eom_get_config_dir(), R_OK) == -1) { - if (mkdir(eom_get_config_dir(), S_IRWXU) == -1) - g_printerr("Can't create directory: %s\n", strerror(errno)); - } + if (access(eom_get_config_dir(), R_OK) == -1) { + if (mkdir(eom_get_config_dir(), S_IRWXU) == -1) + g_printerr("Can't create directory: %s\n", strerror(errno)); + } - /* Open database connection, create file */ - rc = sqlite3_open(filename, &db); - if (rc) { - g_printerr("Can't open database: %s\n", sqlite3_errmsg(db)); - if (db) - sqlite3_close(db); - return -1; - } + /* Open database connection, create file */ + rc = sqlite3_open(filename, &db); + + if (rc) { + g_printerr("Can't open database: %s\n", sqlite3_errmsg(db)); + if (db) + sqlite3_close(db); + return -1; + } + + /* Create collections table */ + rc = sqlite3_exec(db, + "CREATE TABLE manga(" + "id INTEGER PRIMARY KEY," + "name VARCHAR(100)," + "current_qty INTEGER," + "total_qty INTEGER" + ")", NULL, NULL, &zErrMsg); + if (rc != SQLITE_OK) { + g_printerr("Can't create manga table: %s\n", zErrMsg); + sqlite3_free(zErrMsg); + sqlite3_close(db); + return -1; + } + + /* Create items table */ + rc = sqlite3_exec(db, + " CREATE TABLE volume( " + " manga_id INTEGER, " + " id INTEGER, " + " read INTEGER, " + " PRIMARY KEY(manga_id, id), " + " FOREIGN KEY(manga_id) " + " REFERENCES manga(id)) ", + NULL, NULL, &zErrMsg); + if (rc != SQLITE_OK) { + g_printerr("Can't create volume table: %s\n", zErrMsg); + sqlite3_free(zErrMsg); + sqlite3_close(db); + return -1; + } - /* Create collections table */ - rc = sqlite3_exec(db, "CREATE TABLE manga(" - "id INTEGER PRIMARY KEY," - "name VARCHAR(100)," - "current_qty INTEGER," - "total_qty INTEGER" - ")", NULL, NULL, &zErrMsg); - if (rc != SQLITE_OK) { - g_printerr("Can't create manga table: %s\n", zErrMsg); - sqlite3_free(zErrMsg); sqlite3_close(db); - return -1; - } - - /* Create items table */ - rc = sqlite3_exec(db, - " CREATE TABLE volume( " - " manga_id INTEGER, " - " id INTEGER, " - " read INTEGER, " - " PRIMARY KEY(manga_id, id), " - " FOREIGN KEY(manga_id) " - " REFERENCES manga(id)) ", - NULL, NULL, &zErrMsg); - if (rc != SQLITE_OK) { - g_printerr("Can't create volume table: %s\n", zErrMsg); - sqlite3_free(zErrMsg); - sqlite3_close(db); - return -1; - } - - sqlite3_close(db); - return 0; + return 0; } -static Manga *data_get_manga_from_statement(sqlite3_stmt *stmt) +static gboolean +execute_non_query(const gchar *sql) { - Manga *manga = - (Manga *)malloc(sizeof(Manga)); + sqlite3 *db; + sqlite3_stmt *stmt; + gchar *data_file; + gboolean result; + + result = FALSE; + data_file = eom_get_data_file(); + + if (check_and_create_database(data_file)) { + if (sqlite3_open(data_file, &db) == SQLITE_OK) { + int res = + sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, NULL); + + if (res == SQLITE_OK) { + if (sqlite3_step(stmt) == SQLITE_DONE) + result = TRUE; + } + + sqlite3_finalize(stmt); + } + sqlite3_close(db); + } + + return result; +} + +static GList * +get_manga_for_query(const gchar *query) +{ + sqlite3 *database; + sqlite3_stmt *statement; + gchar *data_file; + GList *list = NULL; + + data_file = eom_get_data_file(); + + if (check_and_create_database(data_file)) { + if (sqlite3_open(data_file, &database) == SQLITE_OK) { + int res = sqlite3_prepare_v2(database, query, strlen(query), + &statement, NULL); + + if (res == SQLITE_OK) { + while (sqlite3_step(statement) == SQLITE_ROW) { + Manga *manga = get_manga_from_statement(statement); + list = g_list_append(list, (gpointer)manga); + } + } + else + g_print("error %d: %s\n", res, sqlite3_errmsg(database)); + + /* Release the compiled statement from memory */ + sqlite3_finalize(statement); + } + sqlite3_close(database); + } + + return list; +} + +static Manga * +get_manga_from_statement(sqlite3_stmt *stmt) +{ + Manga *manga = (Manga *)malloc(sizeof(Manga)); manga->id = sqlite3_column_int(stmt, 0); manga->name = g_strdup(sqlite3_column_text(stmt, 1)); diff --git a/src/data.h b/src/data.h index eebb700..b34e2aa 100644 --- a/src/data.h +++ b/src/data.h @@ -3,34 +3,30 @@ #include -typedef struct _volume Volume; - -struct _volume +typedef struct { - int number; - int read; -}; + int number; + int read; +} Volume; -typedef struct _manga Manga; - -struct _manga +typedef struct { - int id; - int current_qty; - int total_qty; - char *name; - int vol_count; - Volume *volumes; -}; + int id; + int current_qty; + int total_qty; + char *name; + int vol_count; + Volume *volumes; +} 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); -gboolean data_add_to_manga(gint id, gint count); -gboolean data_add_volume_to_manga(gint manga_id, gint volume); -gboolean data_mark_volume_read(gint read, gint manga_id, gint volume); -gboolean data_remove_volume_from_manga(gint manga_id, gint volume); +gboolean data_add_manga(gchar*, gint); +gboolean data_add_to_manga(gint, gint); +gboolean data_add_volume_to_manga(gint, gint); +GList *data_get_incomplete_manga(void); +GList *data_get_manga(void); +Manga *data_get_manga_by_id(gint); +void data_get_volumes_for_manga(Manga*); +gboolean data_mark_volume_read(gint, gint, gint); +gboolean data_remove_volume_from_manga(gint, gint); #endif /* __DATA_H__ */ diff --git a/src/eom-edit-window.c b/src/eom-edit-window.c index 3ae7a00..3381e16 100644 --- a/src/eom-edit-window.c +++ b/src/eom-edit-window.c @@ -1,7 +1,9 @@ #include "eom-edit-window.h" + #include #include #include + #include "data.h" #include "eom-main-window.h" @@ -10,250 +12,253 @@ enum { EOM_EDIT_PROP_CID }; -static void eom_edit_window_set_manga_id(EomEditWindow *self, - gint manga_id); -static void eom_edit_window_on_volume_toggled(GtkToggleButton *togglebutton, - gpointer user_data); -static void -eom_edit_window_on_volume_read_toggled(GtkToggleButton *togglebutton, - gpointer user_data); +static void eom_edit_window_class_init(EomEditWindowClass*); +static void eom_edit_window_init(EomEditWindow *self); +static void finalize(GObject*); +static void get_property(GObject*, guint, GValue*, GParamSpec*); +static void on_volume_read_toggled(GtkToggleButton*, gpointer); +static void on_volume_toggled(GtkToggleButton*, gpointer); +static void set_manga_id(EomEditWindow*, gint); +static void set_property(GObject*, guint, const GValue*, GParamSpec*); G_DEFINE_TYPE(EomEditWindow, eom_edit_window, HILDON_TYPE_STACKABLE_WINDOW) -GtkWidget *eom_edit_window_new(gint manga_id) +GtkWidget * +eom_edit_window_new(gint manga_id) { - return g_object_new(EOM_TYPE_EDIT_WINDOW, "manga-id", manga_id, NULL); -} - -static void eom_edit_window_set_property(GObject *object, - guint property_id, - const GValue *value, - GParamSpec *pspec) -{ - EomEditWindow *self = EOM_EDIT_WINDOW(object); - gint manga_id = g_value_get_int(value); - - switch (property_id) { - case EOM_EDIT_PROP_CID: - eom_edit_window_set_manga_id(self, manga_id); - break; - default: - /* We don't have any other properties */ - G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec); - break; - } -} - -static void eom_edit_window_get_property(GObject *object, - guint property_id, - GValue *value, - GParamSpec *pspec) -{ - EomEditWindow *self = EOM_EDIT_WINDOW(object); - - switch (property_id) { - case EOM_EDIT_PROP_CID: - g_value_set_int(value, self->current_manga->id); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec); - break; - } -} - -static void eom_edit_window_finalize(GObject *object) -{ - EomEditWindow *self = EOM_EDIT_WINDOW(object); - - g_free(self->current_manga); - - G_OBJECT_CLASS(eom_edit_window_parent_class)->finalize(object); -} - -static void eom_edit_window_class_init(EomEditWindowClass *klass) -{ - GObjectClass *gobject_class = G_OBJECT_CLASS(klass); - GParamSpec *pspec; - - gobject_class->set_property = eom_edit_window_set_property; - gobject_class->get_property = eom_edit_window_get_property; - gobject_class->finalize = eom_edit_window_finalize; - - pspec = g_param_spec_int("manga-id", - "ID of the manga", - "Set the manga-id", - 0, - INT_MAX, - 0, - G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE); - g_object_class_install_property(gobject_class, - EOM_EDIT_PROP_CID, - pspec); -} - -static void eom_edit_window_init(EomEditWindow *self) -{ - GtkWidget *pannablearea; - GtkWidget *table; - GtkWidget *nameclabel; - GtkWidget *haveclabel; - GtkWidget *totalclabel; - - pannablearea = hildon_pannable_area_new(); - g_object_set(G_OBJECT(pannablearea), - "mov-mode", HILDON_MOVEMENT_MODE_VERT, - NULL); - gtk_container_add(GTK_CONTAINER(self), pannablearea); - - table = gtk_table_new(4, 2, FALSE); - hildon_pannable_area_add_with_viewport(HILDON_PANNABLE_AREA(pannablearea), - table); - - /* Label for the name field */ - nameclabel = gtk_label_new("Name:"); - gtk_misc_set_alignment(GTK_MISC(nameclabel), 0.0, 0.5); - gtk_table_attach(GTK_TABLE(table), nameclabel, 0, 1, 0, 1, - GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0); - - /* The name field */ - self->name_entry = hildon_entry_new(HILDON_SIZE_AUTO); - gtk_entry_set_alignment(GTK_ENTRY(self->name_entry), 1.0); - gtk_table_attach(GTK_TABLE(table), self->name_entry, 1, 2, 0, 1, - GTK_EXPAND | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0); - - /* Label for the collected field */ - haveclabel = gtk_label_new("You have:"); - gtk_misc_set_alignment(GTK_MISC(haveclabel), 0.0, 0.5); - gtk_table_attach(GTK_TABLE(table), haveclabel, 0, 1, 1, 2, - GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0); - - /* The collected field */ - 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); - - /* Label for the total field */ - totalclabel = gtk_label_new("There are:"); - gtk_misc_set_alignment(GTK_MISC(totalclabel), 0.0, 0.5); - gtk_table_attach(GTK_TABLE(table), totalclabel, 0, 1, 2, 3, - GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0); - - /* The total field */ - self->total_entry = hildon_entry_new(HILDON_SIZE_AUTO); - gtk_entry_set_alignment(GTK_ENTRY(self->total_entry), 1.0); - gtk_table_attach(GTK_TABLE(table), self->total_entry, 1, 2, 2, 3, - GTK_EXPAND | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0); - - self->volumes_box = gtk_vbox_new(TRUE, 0); - gtk_table_attach(GTK_TABLE(table), self->volumes_box, 0, 2, 3, 4, - GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0); -} - -static void eom_edit_window_set_manga_id(EomEditWindow *self, gint manga_id) -{ - Manga *manga; - gint i; - gint j = 0; - GtkWidget *clabel; - GtkWidget *tlabel; - GtkWidget *buttonbox; - - manga = data_get_manga_by_id(manga_id); - data_get_volumes_for_manga(manga); - - self->current_manga = manga; - - gtk_entry_set_text(GTK_ENTRY(self->name_entry), manga->name); - gtk_label_set_text(GTK_LABEL(self->have_label), - g_strdup_printf("%d", manga->current_qty)); - gtk_entry_set_text(GTK_ENTRY(self->total_entry), - g_strdup_printf("%d", manga->total_qty)); - - /* TODO: Create labels for collected and read lists */ - - for (i = 0; i < manga->total_qty; i++) { - GtkWidget *button; - GtkWidget *read_button; - - buttonbox = gtk_hbox_new(TRUE, 2); - gtk_box_pack_start(GTK_BOX(self->volumes_box), buttonbox, TRUE, TRUE, 0); - - /* Button indicating collected state */ - button = gtk_toggle_button_new_with_label(g_strdup_printf("%d", i + 1)); - gtk_box_pack_start(GTK_BOX(buttonbox), button, TRUE, TRUE, 0); - - /* Button indicating read state */ - read_button = - gtk_toggle_button_new_with_label(g_strdup_printf("%d", i + 1)); - gtk_box_pack_start(GTK_BOX(buttonbox), read_button, TRUE, TRUE, 0); - - if (j < manga->vol_count && manga->volumes[j].number == i+1) { - gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), TRUE); - if (manga->volumes[j].read) - gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(read_button), TRUE); - j++; - } - g_signal_connect(button, "toggled", - G_CALLBACK(eom_edit_window_on_volume_toggled), - (gpointer)self); - g_signal_connect(read_button, "toggled", - G_CALLBACK(eom_edit_window_on_volume_read_toggled), - (gpointer)self); - } -} - -static void eom_edit_window_on_volume_toggled(GtkToggleButton *togglebutton, - gpointer user_data) -{ - EomEditWindow *self; - gboolean active; - gint volume; - - - self = (EomEditWindow *)user_data; - active = gtk_toggle_button_get_active(togglebutton); - volume = atoi(gtk_button_get_label(GTK_BUTTON(togglebutton))); - - if (active) { - /* Add 1 to mangas collected */ - if (!data_add_to_manga(self->current_manga->id, 1)) { - return; - } - if (!data_add_volume_to_manga(self->current_manga->id, volume)) { - data_add_to_manga(self->current_manga->id, 1); - return; - } - self->current_manga->current_qty++; - } - else { - /* Remove 1 from mangas collected */ - if (!data_add_to_manga(self->current_manga->id, -1)) { - return; - } - if (!data_remove_volume_from_manga(self->current_manga->id, volume)) { - data_add_to_manga(self->current_manga->id, 1); /* Undo */ - return; - } - self->current_manga->current_qty--; - } - - gtk_label_set_text(GTK_LABEL(self->have_label), - g_strdup_printf("%d", self->current_manga->current_qty)); + return g_object_new(EOM_TYPE_EDIT_WINDOW, + "manga-id", manga_id, + NULL); } static void -eom_edit_window_on_volume_read_toggled(GtkToggleButton *togglebutton, - gpointer user_data) +eom_edit_window_class_init(EomEditWindowClass *klass) { - EomEditWindow *self; - gboolean active; - gint volume; + GObjectClass *gobject_class = G_OBJECT_CLASS(klass); + GParamSpec *pspec; - self = (EomEditWindow *)user_data; - active = gtk_toggle_button_get_active(togglebutton); - volume = atoi(gtk_button_get_label(GTK_BUTTON(togglebutton))); + gobject_class->set_property = set_property; + gobject_class->get_property = get_property; + gobject_class->finalize = finalize; + pspec = g_param_spec_int("manga-id", "ID of the manga", + "Set the manga-id", 0, INT_MAX, 0, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE); - if (!data_mark_volume_read(active, self->current_manga->id, volume)) - g_print("coulnd't mark volume as read\n"); + g_object_class_install_property(gobject_class, EOM_EDIT_PROP_CID, + pspec); +} + +static void +eom_edit_window_init(EomEditWindow *self) +{ + GtkWidget *panarea; + GtkWidget *table; + GtkWidget *nameclabel; + GtkWidget *haveclabel; + GtkWidget *totalclabel; + + panarea = hildon_pannable_area_new(); + g_object_set(G_OBJECT(panarea), + "mov-mode", HILDON_MOVEMENT_MODE_VERT, + NULL); + gtk_container_add(GTK_CONTAINER(self), panarea); + + table = gtk_table_new(4, 2, FALSE); + hildon_pannable_area_add_with_viewport(HILDON_PANNABLE_AREA(panarea), + table); + + /* Label for the name field */ + nameclabel = gtk_label_new("Name:"); + gtk_misc_set_alignment(GTK_MISC(nameclabel), 0.0, 0.5); + gtk_table_attach(GTK_TABLE(table), nameclabel, 0, 1, 0, 1, + GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0); + + /* The name field */ + self->name_entry = hildon_entry_new(HILDON_SIZE_AUTO); + gtk_entry_set_alignment(GTK_ENTRY(self->name_entry), 1.0); + gtk_table_attach(GTK_TABLE(table), self->name_entry, 1, 2, 0, 1, + GTK_EXPAND | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0); + + /* Label for the collected field */ + haveclabel = gtk_label_new("You have:"); + gtk_misc_set_alignment(GTK_MISC(haveclabel), 0.0, 0.5); + gtk_table_attach(GTK_TABLE(table), haveclabel, 0, 1, 1, 2, + GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0); + + /* The collected field */ + 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); + + /* Label for the total field */ + totalclabel = gtk_label_new("There are:"); + gtk_misc_set_alignment(GTK_MISC(totalclabel), 0.0, 0.5); + gtk_table_attach(GTK_TABLE(table), totalclabel, 0, 1, 2, 3, + GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0); + + /* The total field */ + self->total_entry = hildon_entry_new(HILDON_SIZE_AUTO); + gtk_entry_set_alignment(GTK_ENTRY(self->total_entry), 1.0); + gtk_table_attach(GTK_TABLE(table), self->total_entry, 1, 2, 2, 3, + GTK_EXPAND | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0); + + self->volsbox = gtk_vbox_new(TRUE, 0); + gtk_table_attach(GTK_TABLE(table), self->volsbox, 0, 2, 3, 4, + GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0); +} + +static void +finalize(GObject *object) +{ + EomEditWindow *self = EOM_EDIT_WINDOW(object); + + g_free(self->manga); + + G_OBJECT_CLASS(eom_edit_window_parent_class)->finalize(object); +} + +static void +get_property(GObject *object, guint property_id, GValue *value, + GParamSpec *pspec) +{ + EomEditWindow *self = EOM_EDIT_WINDOW(object); + + switch (property_id) { + case EOM_EDIT_PROP_CID: + g_value_set_int(value, self->manga->id); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec); + break; + } +} + +static void +on_volume_read_toggled(GtkToggleButton *togglebutton, gpointer user_data) +{ + EomEditWindow *self = (EomEditWindow *)user_data; + gboolean active = gtk_toggle_button_get_active(togglebutton); + gint volume = atoi(gtk_button_get_label(GTK_BUTTON(togglebutton))); + + if (!data_mark_volume_read(active, self->manga->id, volume)) + g_print("coulnd't mark volume as read\n"); +} + +static void +on_volume_toggled(GtkToggleButton *togglebutton, gpointer user_data) +{ + EomEditWindow *self = (EomEditWindow *)user_data; + gboolean active = gtk_toggle_button_get_active(togglebutton); + gint volume = atoi(gtk_button_get_label(GTK_BUTTON(togglebutton))); + gchar *txt; + + if (active) { + /* Add 1 to mangas collected */ + if (!data_add_to_manga(self->manga->id, 1)) { + return; + } + if (!data_add_volume_to_manga(self->manga->id, volume)) { + data_add_to_manga(self->manga->id, 1); + return; + } + self->manga->current_qty++; + } + else { + /* Remove 1 from mangas collected */ + if (!data_add_to_manga(self->manga->id, -1)) { + return; + } + if (!data_remove_volume_from_manga(self->manga->id, + volume)) { + data_add_to_manga(self->manga->id, 1); /* Undo */ + return; + } + self->manga->current_qty--; + } + + txt = g_strdup_printf("%d", self->manga->current_qty); + gtk_label_set_text(GTK_LABEL(self->have_label), txt); + g_free(txt); +} + +static void +set_manga_id(EomEditWindow *self, gint manga_id) +{ + GtkWidget *bbox; + GtkWidget *clabel; + GtkWidget *tlabel; + Manga *manga; + gchar *txt; + gint i; + gint j = 0; + + manga = data_get_manga_by_id(manga_id); + data_get_volumes_for_manga(manga); + + self->manga = manga; + + gtk_entry_set_text(GTK_ENTRY(self->name_entry), manga->name); + + txt = g_strdup_printf("%d", manga->current_qty); + gtk_label_set_text(GTK_LABEL(self->have_label), txt); + g_free(txt); + + txt = g_strdup_printf("%d", manga->total_qty); + gtk_entry_set_text(GTK_ENTRY(self->total_entry), txt); + g_free(txt); + + /* TODO: Create labels for collected and read lists */ + + for (i = 0; i < manga->total_qty; i++) { + GtkWidget *btn; + GtkWidget *rbtn; + + bbox = gtk_hbox_new(TRUE, 2); + gtk_box_pack_start(GTK_BOX(self->volsbox), bbox, TRUE, TRUE, 0); + + /* Button indicating collected state */ + txt = g_strdup_printf("%d", i + 1); + btn = gtk_toggle_button_new_with_label(txt); + gtk_box_pack_start(GTK_BOX(bbox), btn, TRUE, TRUE, 0); + + /* Button indicating read state */ + rbtn = gtk_toggle_button_new_with_label(txt); + gtk_box_pack_start(GTK_BOX(bbox), rbtn, TRUE, TRUE, 0); + g_free(txt); + + if (j < manga->vol_count && manga->volumes[j].number == i + 1) { + gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(btn), TRUE); + + if (manga->volumes[j].read) + gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(rbtn), + TRUE); + + j++; + } + + g_signal_connect(btn, "toggled", G_CALLBACK(on_volume_toggled), + (gpointer)self); + g_signal_connect(rbtn, "toggled", + G_CALLBACK(on_volume_read_toggled), + (gpointer)self); + } +} + +static void +set_property(GObject *object, guint property_id, const GValue *value, + GParamSpec *pspec) +{ + EomEditWindow *self = EOM_EDIT_WINDOW(object); + gint manga_id = g_value_get_int(value); + + switch (property_id) { + case EOM_EDIT_PROP_CID: + set_manga_id(self, manga_id); + break; + default: + /* We don't have any other properties */ + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec); + break; + } } diff --git a/src/eom-edit-window.h b/src/eom-edit-window.h index c0bef79..52cf886 100644 --- a/src/eom-edit-window.h +++ b/src/eom-edit-window.h @@ -2,59 +2,56 @@ #define __EOM_EDIT_WINDOW_H__ #include + #include "data.h" G_BEGIN_DECLS #define EOM_TYPE_EDIT_WINDOW \ - (eom_edit_window_get_type()) + (eom_edit_window_get_type()) #define EOM_EDIT_WINDOW(obj) \ - (G_TYPE_CHECK_INSTANCE_CAST ((obj), \ - EOM_TYPE_EDIT_WINDOW, \ - EomEditWindow)) + (G_TYPE_CHECK_INSTANCE_CAST ((obj), \ + EOM_TYPE_EDIT_WINDOW, \ + EomEditWindow)) -#define EOM_EDIT_WINDOW_CLASS(klass) \ - (G_TYPE_CHECK_CLASS_CAST ((klass), \ - EOM_TYPE_EDIT_WINDOW, \ - EomEditWindowClass)) - -#define EOM_IS_EDIT_WINDOW(obj) \ - (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \ - EOM_TYPE_EDIT_WINDOW)) - -#define EOM_IS_EDIT_WINDOW_CLASS(klass) \ - (G_TYPE_CHECK_CLASS_TYPE ((klass), \ - EOM_TYPE_EDIT_WINDOW)) - -#define EOM_EDIT_WINDOW_GET_CLASS(obj) \ - (G_TYPE_INSTANCE_GET_CLASS ((obj), \ - EOM_TYPE_EDIT_WINDOW, \ +#define EOM_EDIT_WINDOW_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST ((klass), \ + EOM_TYPE_EDIT_WINDOW, \ EomEditWindowClass)) -typedef struct _EomEditWindow EomEditWindow; -typedef struct _EomEditWindowClass EomEditWindowClass; +#define EOM_IS_EDIT_WINDOW(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \ + EOM_TYPE_EDIT_WINDOW)) -struct _EomEditWindowClass +#define EOM_IS_EDIT_WINDOW_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE ((klass), \ + EOM_TYPE_EDIT_WINDOW)) + +#define EOM_EDIT_WINDOW_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS ((obj), \ + EOM_TYPE_EDIT_WINDOW, \ + EomEditWindowClass)) + +typedef struct { - HildonStackableWindowClass parent_class; -}; + HildonStackableWindowClass parent_class; +} EomEditWindowClass; -struct _EomEditWindow +typedef struct { - HildonStackableWindow parent; + HildonStackableWindow parent; - GtkWidget *name_entry; - GtkWidget *have_label; - GtkWidget *total_entry; - GtkWidget *volumes_box; + GtkWidget *name_entry; + GtkWidget *have_label; + GtkWidget *total_entry; + GtkWidget *volsbox; - Manga *current_manga; -}; + Manga *manga; +} EomEditWindow; -GType eom_edit_window_get_type(void); - -GtkWidget *eom_edit_window_new(gint manga_id); +GType eom_edit_window_get_type(void); +GtkWidget *eom_edit_window_new(gint); G_END_DECLS diff --git a/src/eom-main-window.c b/src/eom-main-window.c index fa1253e..1183972 100644 --- a/src/eom-main-window.c +++ b/src/eom-main-window.c @@ -10,276 +10,271 @@ #include "data.h" #include "interface.h" -G_DEFINE_TYPE(EomMainWindow, eom_main_window, HILDON_TYPE_STACKABLE_WINDOW) +G_DEFINE_TYPE(EomMainWindow, eom_main_window, + HILDON_TYPE_STACKABLE_WINDOW) enum { - COL_ID = 0, - COL_NAME, - COL_CURRENT, - COL_TOTAL, - NUM_COLS + COL_ID = 0, + COL_NAME, + COL_CURRENT, + COL_TOTAL, + NUM_COLS }; struct filter_args { - EomMainWindow *window; - gint state; + 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, - gpointer user_data); -static gboolean eom_main_window_on_edit_closed(GtkWidget *widget, - GdkEvent *event, - gpointer user_data); -static void eom_main_window_on_row_activated(GtkWidget *treeview, - GtkTreePath *path, - GtkTreeViewColumn *column, - gpointer user_data); +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_filter(GtkWidget*, struct filter_args*); +static void on_new(GtkWidget*, GtkWindow*); +static void on_row_activated(GtkWidget*, GtkTreePath*, GtkTreeViewColumn*, gpointer); -GtkWidget *eom_main_window_new(void) +void +eom_main_window_add_line(EomMainWindow *window, gint id, + const gchar *name, gint current_qty, + gint total_qty) { - return g_object_new(EOM_TYPE_MAIN_WINDOW, - "state", 0, - NULL); + 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, + -1); } -void eom_main_window_load(EomMainWindow *self) +void +eom_main_window_load(EomMainWindow *self) { - GList *list; - int i; + GList *list; + int i; - gtk_list_store_clear(self->store); + gtk_list_store_clear(self->store); - if (self->state) - list = data_get_incomplete_manga(); - else - list = data_get_manga(); + if (self->state) + list = data_get_incomplete_manga(); + else + list = data_get_manga(); - while (list) { - Manga *manga = list->data; - eom_main_window_add_line(self, manga->id, manga->name, - manga->current_qty, manga->total_qty); + while (list) { + Manga *manga = list->data; - free(manga->name); - if (manga->volumes != NULL) { - for (i = 0; i < manga->vol_count; i++) - free(manga->volumes + i); + eom_main_window_add_line(self, manga->id, manga->name, + manga->current_qty, manga->total_qty); + free(manga->name); + + if (manga->volumes != NULL) { + for (i = 0; i < manga->vol_count; i++) + free(manga->volumes + i); + } + + free(manga); + list = g_list_next(list); } - free(manga); - list = g_list_next(list); - } - g_list_free_1(list); + g_list_free_1(list); } -void eom_main_window_add_line(EomMainWindow *window, - gint id, - const gchar *name, - gint current_qty, - gint total_qty) +GtkWidget * +eom_main_window_new(void) { - 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, - -1); + return g_object_new(EOM_TYPE_MAIN_WINDOW, NULL); } -static void eom_main_window_class_init(EomMainWindowClass *class) +static void +add_menu(EomMainWindow *window) +{ + 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)); + + 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"); + g_signal_connect_after(new_button, "clicked", G_CALLBACK(on_new), + GTK_WINDOW(window)); + 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(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(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_class_init(EomMainWindowClass *class) {} -static void eom_main_window_init(EomMainWindow *window) +static void +eom_main_window_init(EomMainWindow *window) { - GtkCellRenderer *renderer; - GtkWidget *view; - GtkWidget *pannablearea; - GtkTreeViewColumn *current_column; - GtkTreeSelection *selection; - int index; + GtkCellRenderer *renderer; + GtkWidget *view; + GtkWidget *pannablearea; + GtkTreeViewColumn *current_column; + GtkTreeSelection *selection; + int index; - index = -1; + index = -1; - eom_main_window_add_menu(window); + add_menu(window); - g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL); - g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL); + g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL); + 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); + window->store = gtk_list_store_new(NUM_COLS, + G_TYPE_INT, + G_TYPE_STRING, + G_TYPE_INT, + G_TYPE_INT); - pannablearea = hildon_pannable_area_new(); - g_object_set(G_OBJECT(pannablearea), - "mov-mode", HILDON_MOVEMENT_MODE_VERT, - NULL); - gtk_container_add(GTK_CONTAINER(window), pannablearea); - - view = hildon_gtk_tree_view_new(HILDON_UI_MODE_NORMAL); - g_signal_connect(view, "row-activated", - G_CALLBACK(eom_main_window_on_row_activated), window); - gtk_container_add(GTK_CONTAINER(pannablearea), view); - - selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view)); - gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE); - - renderer = gtk_cell_renderer_text_new(); - gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), - ++index, - "Naam", - renderer, - "text", COL_NAME, - NULL); - current_column = gtk_tree_view_get_column(GTK_TREE_VIEW(view), index); - gtk_tree_view_column_set_expand(current_column, TRUE); - - renderer = gtk_cell_renderer_text_new(); - gtk_object_set(GTK_OBJECT(renderer), - "xalign", 1.0, - "width", 100, + pannablearea = hildon_pannable_area_new(); + g_object_set(G_OBJECT(pannablearea), + "mov-mode", HILDON_MOVEMENT_MODE_VERT, NULL); - gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), - ++index, - "", - renderer, - "text", COL_CURRENT, - NULL); + gtk_container_add(GTK_CONTAINER(window), pannablearea); - renderer = gtk_cell_renderer_text_new(); - gtk_object_set(GTK_OBJECT(renderer), - "xalign", 1.0, - "width", 100, - NULL); - gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), - ++index, - "", - renderer, - "text", COL_TOTAL, - NULL); + view = hildon_gtk_tree_view_new(HILDON_UI_MODE_NORMAL); + g_signal_connect(view, "row-activated", G_CALLBACK(on_row_activated), + window); + gtk_container_add(GTK_CONTAINER(pannablearea), view); - gtk_tree_view_set_model(GTK_TREE_VIEW(view), - GTK_TREE_MODEL(window->store)); - g_object_unref(window->store); + selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view)); + gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE); + + renderer = gtk_cell_renderer_text_new(); + gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), + ++index, + "Naam", + renderer, + "text", COL_NAME, + NULL); + current_column = + gtk_tree_view_get_column(GTK_TREE_VIEW(view), index); + gtk_tree_view_column_set_expand(current_column, TRUE); + + renderer = gtk_cell_renderer_text_new(); + gtk_object_set(GTK_OBJECT(renderer), + "xalign", 1.0, + "width", 100, + NULL); + gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), + ++index, + "", + renderer, + "text", COL_CURRENT, + NULL); + + renderer = gtk_cell_renderer_text_new(); + gtk_object_set(GTK_OBJECT(renderer), + "xalign", 1.0, + "width", 100, + NULL); + gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), + ++index, + "", + renderer, + "text", COL_TOTAL, + NULL); + + gtk_tree_view_set_model(GTK_TREE_VIEW(view), + GTK_TREE_MODEL(window->store)); + g_object_unref(window->store); } -static void eom_main_window_add_menu(EomMainWindow *window) +static gboolean +on_edit_closed(GtkWidget *widget, GdkEvent *event, gpointer user_data) { - 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)); + EomMainWindow *self = user_data; - args0->window = window; - args0->state = 0; - args1->window = window; - args1->state = 1; - appmenu = HILDON_APP_MENU(hildon_app_menu_new()); + eom_main_window_load(self); - new_button = hildon_gtk_button_new(HILDON_SIZE_AUTO); - gtk_button_set_label(GTK_BUTTON(new_button), "New Manga"); - g_signal_connect_after(new_button, - "clicked", - G_CALLBACK(eom_main_window_on_new), - GTK_WINDOW(window)); - - 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)); + return FALSE; } -static void eom_main_window_on_filter(GtkWidget *widget, struct filter_args *arg) +static void +on_filter(GtkWidget *widget, struct filter_args *arg) { - arg->window->state = arg->state; - eom_main_window_load(arg->window); + arg->window->state = arg->state; + eom_main_window_load(arg->window); } -static void eom_main_window_on_new(GtkWidget *widget, GtkWindow *window) +static void +on_new(GtkWidget *widget, GtkWindow *window) { - GtkWidget *dialog; - gint result; - gchar *name = NULL; - gint total_qty; + GtkWidget *dialog; + gint result; + gchar *name = NULL; + gint total_qty; - dialog = interface_show_new_item_dialog(window); + dialog = interface_show_new_item_dialog(window); + result = gtk_dialog_run(GTK_DIALOG(dialog)); - result = gtk_dialog_run(GTK_DIALOG(dialog)); + if (result == GTK_RESPONSE_OK) { + const gchar *tmp; + tmp = eom_new_item_dialog_get_name(EOM_NEW_ITEM_DIALOG(dialog)); + name = (gchar *)malloc(strlen(tmp) + 1); - if (result == GTK_RESPONSE_OK) { - const gchar *tmp; - tmp = eom_new_item_dialog_get_name(EOM_NEW_ITEM_DIALOG(dialog)); - name = (gchar *)malloc(strlen(tmp) + 1); + strcpy(name, tmp); + strcat(name, "\0"); - strcpy(name, tmp); - strcat(name, "\0"); + total_qty = eom_new_item_dialog_get_total_qty(EOM_NEW_ITEM_DIALOG(dialog)); - total_qty = eom_new_item_dialog_get_total_qty(EOM_NEW_ITEM_DIALOG(dialog)); + gtk_widget_destroy(dialog); + } - gtk_widget_destroy(dialog); - } - - if (name != NULL) { - if (data_add_manga(name, total_qty)) - eom_main_window_load(EOM_MAIN_WINDOW(window)); - } + if (name != NULL) { + if (data_add_manga(name, total_qty)) + eom_main_window_load(EOM_MAIN_WINDOW(window)); + } } -static gboolean eom_main_window_on_edit_closed(GtkWidget *widget, - GdkEvent *event, - gpointer user_data) +static void +on_row_activated(GtkWidget *treeview, GtkTreePath *path, + GtkTreeViewColumn *column, gpointer user_data) { - EomMainWindow *self = user_data; + EomMainWindow *self = user_data; + GtkTreeIter iter; + GtkTreeModel *model; + gint id; + GtkWidget *window; - eom_main_window_load(self); + model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview)); - return FALSE; -} - -static void eom_main_window_on_row_activated(GtkWidget *treeview, - GtkTreePath *path, - GtkTreeViewColumn *column, - gpointer user_data) -{ - EomMainWindow *self = user_data; - GtkTreeIter iter; - GtkTreeModel *model; - gint id; - GtkWidget *window; - - model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview)); - if (!gtk_tree_model_get_iter(model, &iter, path)) - return; - - gtk_tree_model_get(model, &iter, COL_ID, &id, -1); - window = interface_show_edit_window(id); - g_signal_connect(window, "delete-event", - G_CALLBACK(eom_main_window_on_edit_closed), self); + if (!gtk_tree_model_get_iter(model, &iter, path)) + return; + + gtk_tree_model_get(model, &iter, COL_ID, &id, -1); + window = interface_show_edit_window(id); + g_signal_connect(window, "delete-event", G_CALLBACK(on_edit_closed), + self); } diff --git a/src/eom-main-window.h b/src/eom-main-window.h index d4ff74e..fb3524f 100644 --- a/src/eom-main-window.h +++ b/src/eom-main-window.h @@ -6,60 +6,52 @@ G_BEGIN_DECLS #define EOM_TYPE_MAIN_WINDOW \ - (eom_main_window_get_type()) + (eom_main_window_get_type()) #define EOM_MAIN_WINDOW(obj) \ - (G_TYPE_CHECK_INSTANCE_CAST ((obj), \ - EOM_TYPE_MAIN_WINDOW, \ - EomMainWindow)) + (G_TYPE_CHECK_INSTANCE_CAST ((obj), \ + EOM_TYPE_MAIN_WINDOW, \ + EomMainWindow)) -#define EOM_MAIN_WINDOW_CLASS(klass) \ - (G_TYPE_CHECK_CLASS_CAST ((klass), \ - EOM_TYPE_MAIN_WINDOW, \ - EomMainWindowClass)) - -#define EOM_IS_MAIN_WINDOW(obj) \ - (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \ - EOM_TYPE_MAIN_WINDOW)) - -#define EOM_IS_MAIN_WINDOW_CLASS(klass) \ - (G_TYPE_CHECK_CLASS_TYPE ((klass), \ - EOM_TYPE_MAIN_WINDOW)) - -#define EOM_MAIN_WINDOW_GET_CLASS(obj) \ - (G_TYPE_INSTANCE_GET_CLASS ((obj), \ - EOM_TYPE_MAIN_WINDOW, \ +#define EOM_MAIN_WINDOW_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST ((klass), \ + EOM_TYPE_MAIN_WINDOW, \ EomMainWindowClass)) -typedef struct _EomMainWindow EomMainWindow; -typedef struct _EomMainWindowClass EomMainWindowClass; +#define EOM_IS_MAIN_WINDOW(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \ + EOM_TYPE_MAIN_WINDOW)) -struct _EomMainWindowClass +#define EOM_IS_MAIN_WINDOW_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE ((klass), \ + EOM_TYPE_MAIN_WINDOW)) + +#define EOM_MAIN_WINDOW_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS ((obj), \ + EOM_TYPE_MAIN_WINDOW, \ + EomMainWindowClass)) + +typedef struct { - HildonStackableWindowClass parent_class; -}; + HildonStackableWindowClass parent_class; +} EomMainWindowClass; -struct _EomMainWindow +typedef struct { - HildonStackableWindow parent; + HildonStackableWindow parent; - GtkTreeIter iter; - GtkListStore *store; + GtkTreeIter iter; + GtkListStore *store; - int state; -}; - -GType eom_main_window_get_type(void); + int state; +} EomMainWindow; +void eom_main_window_add_line(EomMainWindow*, gint, const gchar*, gint, + gint); +GType eom_main_window_get_type(void); +void eom_main_window_load(EomMainWindow*); GtkWidget *eom_main_window_new(void); -void eom_main_window_load(EomMainWindow *self); -void eom_main_window_add_line(EomMainWindow *window, - gint id, - const gchar *name, - gint current_qty, - gint total_qty); - G_END_DECLS #endif /* __EOM_MAIN_WINDOW_H__ */ diff --git a/src/eom-new-item-dialog.c b/src/eom-new-item-dialog.c index aa8c169..e7d5052 100644 --- a/src/eom-new-item-dialog.c +++ b/src/eom-new-item-dialog.c @@ -1,50 +1,60 @@ #include "eom-new-item-dialog.h" -#include + #include +#include #include +static void eom_new_item_dialog_class_init(EomNewItemDialogClass*); +static void eom_new_item_dialog_init(EomNewItemDialog*); + G_DEFINE_TYPE(EomNewItemDialog, eom_new_item_dialog, GTK_TYPE_DIALOG) -GtkWidget *eom_new_item_dialog_new(void) +const gchar * +eom_new_item_dialog_get_name(EomNewItemDialog *dialog) { - return g_object_new(EOM_TYPE_NEW_ITEM_DIALOG, NULL); + return hildon_entry_get_text(HILDON_ENTRY(dialog->name_entry)); } -const gchar *eom_new_item_dialog_get_name(EomNewItemDialog *dialog) +gint +eom_new_item_dialog_get_total_qty(EomNewItemDialog *dialog) { - return hildon_entry_get_text(HILDON_ENTRY(dialog->name_entry)); + return atoi(hildon_entry_get_text(HILDON_ENTRY(dialog->qty_entry))); } -gint eom_new_item_dialog_get_total_qty(EomNewItemDialog *dialog) +GtkWidget * +eom_new_item_dialog_new(void) { - return atoi(hildon_entry_get_text(HILDON_ENTRY(dialog->qty_entry))); + return g_object_new(EOM_TYPE_NEW_ITEM_DIALOG, NULL); } -static void eom_new_item_dialog_class_init(EomNewItemDialogClass *class) +static void +eom_new_item_dialog_class_init(EomNewItemDialogClass *class) {} -static void eom_new_item_dialog_init(EomNewItemDialog *dialog) +static void +eom_new_item_dialog_init(EomNewItemDialog *dialog) { - GtkWidget *content_area; - GtkWidget *hbox; + GtkWidget *content_area; + GtkWidget *hbox; - content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); + content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); - hbox = gtk_hbox_new(FALSE, 0); - gtk_container_add(GTK_CONTAINER(content_area), hbox); + hbox = gtk_hbox_new(FALSE, 0); + gtk_container_add(GTK_CONTAINER(content_area), hbox); - dialog->name_entry = hildon_entry_new(HILDON_SIZE_AUTO); - hildon_entry_set_placeholder(HILDON_ENTRY(dialog->name_entry), "Name..."); - gtk_box_pack_start(GTK_BOX(hbox), dialog->name_entry, TRUE, TRUE, 0); + dialog->name_entry = hildon_entry_new(HILDON_SIZE_AUTO); + hildon_entry_set_placeholder(HILDON_ENTRY(dialog->name_entry), + "Name..."); + gtk_box_pack_start(GTK_BOX(hbox), dialog->name_entry, TRUE, TRUE, 0); - dialog->qty_entry = hildon_entry_new(HILDON_SIZE_AUTO); - g_object_set(G_OBJECT(dialog->qty_entry), "width-chars", 5, NULL); - hildon_entry_set_text(HILDON_ENTRY(dialog->qty_entry), "0"); - gtk_box_pack_start(GTK_BOX(hbox), dialog->qty_entry, TRUE, TRUE, 0); + dialog->qty_entry = hildon_entry_new(HILDON_SIZE_AUTO); + g_object_set(G_OBJECT(dialog->qty_entry), "width-chars", 5, NULL); + hildon_entry_set_text(HILDON_ENTRY(dialog->qty_entry), "0"); + gtk_box_pack_start(GTK_BOX(hbox), dialog->qty_entry, TRUE, TRUE, 0); - gtk_window_set_title(GTK_WINDOW(dialog), "New item"); - gtk_dialog_add_buttons(GTK_DIALOG(dialog), - GTK_STOCK_OK, - GTK_RESPONSE_OK, - NULL); + gtk_window_set_title(GTK_WINDOW(dialog), "New item"); + gtk_dialog_add_buttons(GTK_DIALOG(dialog), + GTK_STOCK_OK, + GTK_RESPONSE_OK, + NULL); } diff --git a/src/eom-new-item-dialog.h b/src/eom-new-item-dialog.h index b77a93f..5e74204 100644 --- a/src/eom-new-item-dialog.h +++ b/src/eom-new-item-dialog.h @@ -31,29 +31,23 @@ G_BEGIN_DECLS EOM_TYPE_NEW_ITEM_DIALOG, \ EomNewItemDialogClass)) -typedef struct _EomNewItemDialog EomNewItemDialog; -typedef struct _EomNewItemDialogClass EomNewItemDialogClass; - -struct _EomNewItemDialogClass +typedef struct { GtkDialogClass parent_class; -}; +} EomNewItemDialogClass; -struct _EomNewItemDialog +typedef struct { GtkDialog parent; GtkWidget *name_entry; GtkWidget *qty_entry; -}; +} EomNewItemDialog; -GType eom_new_item_dialog_get_type(void); - -GtkWidget *eom_new_item_dialog_new(void); - -const gchar *eom_new_item_dialog_get_name(EomNewItemDialog *dialog); - -gint eom_new_item_dialog_get_total_qty(EomNewItemDialog *dialog); +const gchar *eom_new_item_dialog_get_name(EomNewItemDialog*); +gint eom_new_item_dialog_get_total_qty(EomNewItemDialog*); +GType eom_new_item_dialog_get_type(void); +GtkWidget *eom_new_item_dialog_new(void); G_END_DECLS diff --git a/src/eom.c b/src/eom.c index 1b4a12e..e4d8cf3 100644 --- a/src/eom.c +++ b/src/eom.c @@ -1,9 +1,11 @@ #include #include -#include "interface.h" -#include "eom.h" -gchar *eom_get_data_file(void) +#include "eom.h" +#include "interface.h" + +gchar * +eom_get_data_file(void) { static gchar *filedir = NULL; @@ -15,7 +17,8 @@ gchar *eom_get_data_file(void) return filedir; } -gchar *eom_get_config_dir(void) +gchar * +eom_get_config_dir(void) { static gchar *filedir = NULL; @@ -29,11 +32,8 @@ gchar *eom_get_config_dir(void) int main(int argc, char *argv[]) { hildon_gtk_init(&argc, &argv); - g_set_application_name("Eye on Manga"); - interface_show_main_window(); - gtk_main(); return 0; diff --git a/src/eom.h b/src/eom.h index 155cd60..5d0b8ed 100644 --- a/src/eom.h +++ b/src/eom.h @@ -1,2 +1,7 @@ +#ifndef __EOM_H__ +#define __EOM_H__ + gchar *eom_get_data_file(void); gchar *eom_get_config_dir(void); + +#endif /* __EOM_H__ */