Edit which volumes you have

This commit is contained in:
Tom Willemsen 2010-10-22 02:19:47 +02:00
parent bbc8844d49
commit bf85ca8598
9 changed files with 236 additions and 85 deletions

View file

@ -1 +0,0 @@
tom@tom-laptop.14670:1287677918

View file

@ -88,7 +88,7 @@ Manga *data_get_manga_by_id(gint manga_id)
return manga;
}
gint *data_get_volumes_by_manga_id(gint manga_id)
void data_get_volumes_by_manga_id(gint manga_id, gint *size, gint **vols)
{
gint count;
gint *volumes;
@ -103,10 +103,11 @@ gint *data_get_volumes_by_manga_id(gint manga_id)
if (sqlite3_open(data_file, &database) == SQLITE_OK) {
int res;
const char *sql = g_strdup_printf(
" SELECT COUNT(number) "
" SELECT COUNT(id) "
" FROM volume "
" WHERE collection_id = %d ", manga_id);
" WHERE manga_id = %d ", manga_id);
g_print("%s\n", sql);
res = sqlite3_prepare_v2(database, sql, strlen(sql), &statement, NULL);
if (res == SQLITE_OK) {
if (sqlite3_step(statement) == SQLITE_ROW) {
@ -114,18 +115,21 @@ gint *data_get_volumes_by_manga_id(gint manga_id)
}
}
sqlite3_finalize(statement);
g_print("%d: %d\n", manga_id, count);
if (count > 0) {
sql = g_strdup_printf(
" SELECT number "
" FROM volume "
" WHERE collection_id = %d ", manga_id);
" SELECT id "
" FROM volume "
" WHERE manga_id = %d ", manga_id);
g_print("%s\n", sql);
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);
gint volume = sqlite3_column_int(statement, 0);
g_print("volume: %d\n", volume);
volumes[i++] = volume;
}
}
}
@ -133,7 +137,8 @@ gint *data_get_volumes_by_manga_id(gint manga_id)
sqlite3_close(database);
}
return volumes;
*size = count;
*vols = volumes;
}
gboolean data_add_manga(gchar *name, gint total_qty)
@ -203,6 +208,89 @@ gboolean data_add_to_manga(gint manga_id, gint count)
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) ", 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)
{
if (!access(data_file, R_OK) == 0)
@ -251,11 +339,11 @@ static gint data_create_new_database(const gchar *filename)
/* Create items table */
rc = sqlite3_exec(db,
" CREATE TABLE volume( "
" collection_id INTEGER, "
" manga_id INTEGER, "
" id INTEGER, "
" PRIMARY KEY(collection_id, id), "
" FOREIGN KEY(collection_id) "
" REFERENCES collection(id)) ",
" 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);

View file

@ -1,3 +1,6 @@
#ifndef __DATA_H__
#define __DATA_H__
#include <gtk/gtk.h>
typedef struct _manga Manga;
@ -12,6 +15,10 @@ struct _manga
GList *data_get_manga(void);
Manga *data_get_manga_by_id(gint manga_id);
gint *data_get_volumes_by_manga_id(gint manga_id);
void data_get_volumes_by_manga_id(gint manga_id, gint *size, gint **vols);
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_remove_volume_from_manga(gint manga_id, gint volume);
#endif /* __DATA_H__ */

View file

@ -1,7 +1,9 @@
#include "eom-edit-window.h"
#include <hildon/hildon.h>
#include <limits.h>
#include <stdlib.h>
#include "data.h"
#include "eom-main-window.h"
enum {
EOM_EDIT_PROP_0,
@ -9,13 +11,14 @@ enum {
};
static void eom_edit_window_set_manga_id(EomEditWindow *self,
gint manga_id);
gint manga_id);
static void eom_edit_window_on_volume_toggled(GtkToggleButton *togglebutton,
gpointer user_data);
G_DEFINE_TYPE(EomEditWindow, eom_edit_window, HILDON_TYPE_STACKABLE_WINDOW)
GtkWidget *eom_edit_window_new(gint manga_id)
{
g_print("1: %d\n", manga_id);
return g_object_new(EOM_TYPE_EDIT_WINDOW, "manga-id", manga_id, NULL);
}
@ -25,11 +28,11 @@ static void eom_edit_window_set_property(GObject *object,
GParamSpec *pspec)
{
EomEditWindow *self = EOM_EDIT_WINDOW(object);
gint manga_id = g_value_get_int(value);
switch (property_id) {
case EOM_EDIT_PROP_CID:
g_print("2: %d\n", g_value_get_int(value));
self->manga_id = g_value_get_int(value);
eom_edit_window_set_manga_id(self, manga_id);
break;
default:
/* We don't have any other properties */
@ -47,7 +50,7 @@ static void eom_edit_window_get_property(GObject *object,
switch (property_id) {
case EOM_EDIT_PROP_CID:
g_value_set_int(value, self->manga_id);
g_value_set_int(value, self->current_manga->id);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
@ -82,9 +85,6 @@ static void eom_edit_window_init(EomEditWindow *self)
GtkWidget *nameclabel;
GtkWidget *haveclabel;
GtkWidget *totalclabel;
GtkWidget *vbox;
g_print("3: %d\n", self->manga_id);
pannablearea = hildon_pannable_area_new();
g_object_set(G_OBJECT(pannablearea),
@ -101,9 +101,9 @@ static void eom_edit_window_init(EomEditWindow *self)
gtk_table_attach(GTK_TABLE(table), nameclabel, 0, 1, 0, 1,
GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
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,
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);
haveclabel = gtk_label_new("You have:");
@ -121,13 +121,13 @@ static void eom_edit_window_init(EomEditWindow *self)
gtk_table_attach(GTK_TABLE(table), totalclabel, 0, 1, 2, 3,
GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
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,
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);
vbox = gtk_vbox_new(TRUE, 0);
gtk_table_attach(GTK_TABLE(table), vbox, 0, 2, 3, 4,
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);
}
@ -136,15 +136,77 @@ static void eom_edit_window_set_manga_id(EomEditWindow *self,
{
Manga *manga;
gint *volumes;
gint num_vols;
gint i;
gint j = 0;
manga = data_get_manga_by_id(manga_id);
volumes = data_get_volumes_by_manga_id(manga_id);
data_get_volumes_by_manga_id(manga_id, &num_vols, &volumes);
gtk_label_set_text(GTK_LABEL(self->name_label), manga->name);
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_label_set_text(GTK_LABEL(self->total_label),
gtk_entry_set_text(GTK_ENTRY(self->total_entry),
g_strdup_printf("%d", manga->total_qty));
g_free(manga);
for (i = 0; i < manga->total_qty; i++) {
GtkWidget *button;
button = gtk_toggle_button_new_with_label(g_strdup_printf("%d", i + 1));
gtk_box_pack_start(GTK_BOX(self->volumes_box), button, TRUE, TRUE, 0);
g_print("volume found: %d, i=%d\n", volumes[j], i);
if (j < num_vols && volumes[j] == i+1) {
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), TRUE);
j++;
}
g_signal_connect(button, "toggled",
G_CALLBACK(eom_edit_window_on_volume_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)));
g_print("toggled %d: %d\n", self->current_manga->id, volume);
if (active) {
if (!data_add_to_manga(self->current_manga->id, 1)) {
g_print("not added 1 volume to %d\n", self->current_manga->id);
return;
}
if (!data_add_volume_to_manga(self->current_manga->id, volume)) {
g_print("not added volume %d to %d\n", volume, self->current_manga->id);
data_add_to_manga(self->current_manga->id, 1);
return;
}
self->current_manga->current_qty++;
}
else {
if (!data_add_to_manga(self->current_manga->id, -1)) {
g_print("not added -1 volumes to %d\n", self->current_manga->id);
return;
}
if (!data_remove_volume_from_manga(self->current_manga->id, volume)) {
g_print("not added volume %d to %d\n", volume, self->current_manga->id);
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));
}

View file

@ -2,6 +2,7 @@
#define __EOM_EDIT_WINDOW_H__
#include <hildon/hildon-stackable-window.h>
#include "data.h"
G_BEGIN_DECLS
@ -43,11 +44,12 @@ struct _EomEditWindow
{
HildonStackableWindow parent;
GtkWidget *name_label;
GtkWidget *name_entry;
GtkWidget *have_label;
GtkWidget *total_label;
GtkWidget *total_entry;
GtkWidget *volumes_box;
gint manga_id;
Manga *current_manga;
};
GType eom_edit_window_get_type(void);

View file

@ -27,6 +27,9 @@ static void eom_main_window_on_remove_clicked(GtkWidget *widget,
gpointer user_data);
static void eom_main_window_on_edit_clicked(GtkWidget *widget,
gpointer user_data);
static gboolean eom_main_window_on_edit_closed(GtkWidget *widget,
GdkEvent *event,
gpointer user_data);
GtkWidget *eom_main_window_new(void)
{
@ -68,18 +71,14 @@ void eom_main_window_add_line(EomMainWindow *window,
void eom_main_window_set_no_select(EomMainWindow *self)
{
if (GTK_IS_WIDGET(self->add_button))
gtk_widget_set_sensitive(GTK_WIDGET(self->add_button), FALSE);
if (GTK_IS_WIDGET(self->remove_button))
gtk_widget_set_sensitive(GTK_WIDGET(self->remove_button), FALSE);
if (GTK_IS_WIDGET(self->edit_button))
gtk_widget_set_sensitive(GTK_WIDGET(self->edit_button), FALSE);
}
void eom_main_window_set_has_select(EomMainWindow *self)
{
if (GTK_IS_WIDGET(self->add_button))
gtk_widget_set_sensitive(GTK_WIDGET(self->add_button), TRUE);
if (GTK_IS_WIDGET(self->remove_button))
gtk_widget_set_sensitive(GTK_WIDGET(self->remove_button), TRUE);
if (GTK_IS_WIDGET(self->edit_button))
gtk_widget_set_sensitive(GTK_WIDGET(self->edit_button), TRUE);
}
static void eom_main_window_class_init(EomMainWindowClass *class)
@ -167,17 +166,6 @@ static void eom_main_window_init(EomMainWindow *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 =
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(eom_main_window_on_add_clicked),
(gpointer)window);
gtk_box_pack_start(GTK_BOX(hbuttonbox), window->add_button, FALSE, FALSE, 0);
window->edit_button =
hildon_button_new_with_text(HILDON_SIZE_AUTO_WIDTH |
HILDON_SIZE_FINGER_HEIGHT,
@ -189,18 +177,6 @@ static void eom_main_window_init(EomMainWindow *window)
(gpointer)window);
gtk_box_pack_start(GTK_BOX(hbuttonbox), window->edit_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(eom_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);
eom_main_window_set_no_select(window);
@ -270,7 +246,7 @@ static void eom_main_window_on_selection_changed(GtkTreeSelection *selection,
eom_main_window_set_has_select(self);
}
static void eom_main_window_on_add_clicked(GtkWidget *widget, gpointer user_data)
/*static void eom_main_window_on_add_clicked(GtkWidget *widget, gpointer user_data)
{
EomMainWindow *self;
gint count;
@ -293,9 +269,9 @@ static void eom_main_window_on_add_clicked(GtkWidget *widget, gpointer user_data
}
}
}
}*/
static void eom_main_window_on_remove_clicked(GtkWidget *widget,
/*static void eom_main_window_on_remove_clicked(GtkWidget *widget,
gpointer user_data)
{
EomMainWindow *self;
@ -319,15 +295,32 @@ static void eom_main_window_on_remove_clicked(GtkWidget *widget,
COL_CURRENT, current_count - 1, -1);
}
}
}
}*/
static void eom_main_window_on_edit_clicked(GtkWidget *widget,
gpointer user_data)
{
EomMainWindow *self;
EomMainWindow *self = user_data;
GtkTreeModel *model;
gint id;
GtkWidget *window;
self = (EomMainWindow *)user_data;
if (gtk_tree_selection_get_selected(self->selection, &model, &self->iter)) {
gtk_tree_model_get(model, &self->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);
/* TODO: Place more code here */
interface_show_edit_window();
}
}
static gboolean eom_main_window_on_edit_closed(GtkWidget *widget,
GdkEvent *event,
gpointer user_data)
{
EomMainWindow *self = user_data;
eom_main_window_load(self);
return FALSE;
}

View file

@ -47,8 +47,6 @@ struct _EomMainWindow
GtkListStore *store;
GtkTreeSelection *selection;
GtkWidget *add_button;
GtkWidget *remove_button;
GtkWidget *edit_button;
};

View file

@ -15,16 +15,18 @@ void interface_show_main_window(void)
eom_main_window_load(EOM_MAIN_WINDOW(window));
}
void interface_show_edit_window(void)
GtkWidget *interface_show_edit_window(gint manga_id)
{
HildonWindowStack *stack;
/*HildonWindowStack *stack;*/
GtkWidget *window;
stack = hildon_window_stack_get_default();
/*-stack = hildon_window_stack_get_default();*/
window = eom_edit_window_new(1);
hildon_window_stack_push(stack, HILDON_STACKABLE_WINDOW(window), NULL);
window = eom_edit_window_new(manga_id);
/*hildon_window_stack_push(stack, HILDON_STACKABLE_WINDOW(window), NULL);*/
gtk_widget_show_all(window);
return window;
}
GtkWidget *interface_show_new_item_dialog(GtkWindow *window)

View file

@ -1,5 +1,5 @@
#include <gtk/gtkwindow.h>
void interface_show_main_window(void);
void interface_show_edit_window(void);
GtkWidget *interface_show_edit_window(gint manga_id);
GtkWidget *interface_show_new_item_dialog(GtkWindow *window);