Correctly show changes when total goes down

* src/data.c (data_update_manga): When updating, set `current_qty' to
  the lesser of `current_qty' and `total_qty' so that current will
  never exceed total.  If the update went well remove any volumes that
  have numbers greater than the `total_qty'.

* src/eom-detail-window.c (eom_detail_window_load): Get to upper bound
  of rows, if I need 1.5 rows I actually need 2 rows, what was I
  thinking?  Clear both tables of children before filling them up
  again.
  (cb_destroy): New function.  Just calls destroy in `widget', but
  `gtk_widget_destroy' doesn't seem like a valid `GtkCallback'.
  (on_volume_toggled): When adding a volume fails, revert one from
  `current_qty'.  Don't mess with `self->manga->current_qty', just
  reset the manga id, otherwise we will know that there should be one
  more or less volume, but won't show it in the manga's volumes
  collection.
This commit is contained in:
Tom Willemsen 2012-10-25 21:15:50 +02:00
parent 337a0ba2d2
commit 7ffc87d99d
2 changed files with 27 additions and 10 deletions

View file

@ -239,12 +239,24 @@ gboolean
data_update_manga(gint manga_id, const gchar *name, gint total_qty) data_update_manga(gint manga_id, const gchar *name, gint total_qty)
{ {
gchar *sql = gchar *sql =
g_strdup_printf("UPDATE manga SET name = '%s', total_qty = %d " g_strdup_printf("UPDATE manga SET "
"WHERE id = %d", name, total_qty, manga_id); " name = '%s', "
" total_qty = %d, "
" current_qty = MIN(current_qty, %d) "
"WHERE id = %d", name, total_qty, total_qty,
manga_id);
gboolean ret = execute_non_query(sql); gboolean ret = execute_non_query(sql);
g_free(sql); g_free(sql);
if (ret) {
sql = g_strdup_printf("DELETE FROM volume "
"WHERE manga_id = %d "
"AND id > %d",
manga_id, total_qty);
ret = execute_non_query(sql);
}
return ret; return ret;
} }

View file

@ -18,6 +18,7 @@ enum {
}; };
static void add_menu(EomDetailWindow*); static void add_menu(EomDetailWindow*);
static void cb_destroy(GtkWidget*, gpointer);
static GtkWidget *create_volume_button(gchar*, GtkTable*, int, int); static GtkWidget *create_volume_button(gchar*, GtkTable*, int, int);
static void eom_detail_window_class_init(EomDetailWindowClass*); static void eom_detail_window_class_init(EomDetailWindowClass*);
static void eom_detail_window_init(EomDetailWindow *self); static void eom_detail_window_init(EomDetailWindow *self);
@ -42,10 +43,12 @@ eom_detail_window_load(EomDetailWindow *self)
if (!total_qty) if (!total_qty)
total_qty = self->manga->current_qty + 1; total_qty = self->manga->current_qty + 1;
rows = (int)floor(total_qty / COLUMNS); rows = (int)ceil(total_qty / COLUMNS);
gtk_window_set_title(GTK_WINDOW(self), self->manga->name); gtk_window_set_title(GTK_WINDOW(self), self->manga->name);
gtk_container_foreach(GTK_CONTAINER(self->ctable), cb_destroy, NULL);
gtk_table_resize(GTK_TABLE(self->ctable), rows, COLUMNS); gtk_table_resize(GTK_TABLE(self->ctable), rows, COLUMNS);
gtk_container_foreach(GTK_CONTAINER(self->rtable), cb_destroy, NULL);
gtk_table_resize(GTK_TABLE(self->rtable), rows, COLUMNS); gtk_table_resize(GTK_TABLE(self->rtable), rows, COLUMNS);
for (i = 0; i < total_qty; i++) { for (i = 0; i < total_qty; i++) {
@ -113,6 +116,12 @@ add_menu(EomDetailWindow *window)
HILDON_APP_MENU(appmenu)); HILDON_APP_MENU(appmenu));
} }
static void
cb_destroy(GtkWidget *widget, gpointer user_data)
{
gtk_widget_destroy(widget);
}
static GtkWidget * static GtkWidget *
create_volume_button(gchar* text, GtkTable* table, int column, int row) create_volume_button(gchar* text, GtkTable* table, int column, int row)
{ {
@ -264,26 +273,22 @@ on_volume_toggled(GtkToggleButton *togglebutton, gpointer user_data)
return; return;
} }
if (!data_add_volume_to_manga(self->manga->id, volume)) { if (!data_add_volume_to_manga(self->manga->id, volume)) {
data_add_to_manga(self->manga->id, 1); data_add_to_manga(self->manga->id, -1);
return; return;
} }
self->manga->current_qty++;
} }
else { else {
/* Remove 1 from mangas collected */ /* Remove 1 from mangas collected */
if (!data_add_to_manga(self->manga->id, -1)) { if (!data_add_to_manga(self->manga->id, -1)) {
return; return;
} }
if (!data_remove_volume_from_manga(self->manga->id, if (!data_remove_volume_from_manga(self->manga->id, volume)) {
volume)) {
data_add_to_manga(self->manga->id, 1); /* Undo */ data_add_to_manga(self->manga->id, 1); /* Undo */
return; return;
} }
self->manga->current_qty--;
} }
if (!self->manga->total_qty) set_manga_id(self, self->manga->id);
eom_detail_window_load(self);
} }
static void static void