Prevent items from being added twice

This commit is contained in:
Markus Koch 2019-07-24 21:19:26 +02:00
parent f545d3d21e
commit 304c75c4db
6 changed files with 57 additions and 13 deletions

View File

@ -96,13 +96,16 @@ GtkWidget *yacos_cart_item_new(void)
void yacos_cart_item_set_id(YacosCartItem *self, yacos_tag_id tag_id) void yacos_cart_item_set_id(YacosCartItem *self, yacos_tag_id tag_id)
{ {
gchar *id_str; gchar *id_str;
gchar *id_str2;
self->priv.tag_id = tag_id; self->priv.tag_id = tag_id;
id_str = yacos_tag_id_to_str(tag_id); id_str = yacos_tag_id_to_str(tag_id);
gtk_label_set_text(GTK_LABEL(self->priv.label_id), id_str); id_str2 = g_strdup_printf("#%s", id_str);
g_free(id_str); g_free(id_str);
gtk_label_set_text(GTK_LABEL(self->priv.label_id), id_str2);
g_free(id_str2);
} }
static void yacos_cart_item_fetch_meta_cb (SoupSession *session, SoupMessage *msg, gpointer user_data) static void yacos_cart_item_fetch_meta_cb (SoupSession *session, SoupMessage *msg, gpointer user_data)
@ -151,6 +154,11 @@ void yacos_cart_item_fetch_meta(YacosCartItem *self)
gchar *url; gchar *url;
gchar *id_str; gchar *id_str;
gtk_label_set_text(GTK_LABEL(self->priv.label_title), "Loading ...");
gtk_label_set_text(GTK_LABEL(self->priv.label_description), "");
gtk_label_set_text(GTK_LABEL(self->priv.label_comment), "");
id_str = yacos_tag_id_to_str(self->priv.tag_id); id_str = yacos_tag_id_to_str(self->priv.tag_id);
url = g_strdup_printf("%s?meta&id=%s", self->priv.api.url_base, id_str); url = g_strdup_printf("%s?meta&id=%s", self->priv.api.url_base, id_str);
@ -163,3 +171,13 @@ void yacos_cart_item_fetch_meta(YacosCartItem *self)
yacos_cart_item_fetch_meta_cb, yacos_cart_item_fetch_meta_cb,
self); self);
} }
void yacos_cart_item_fetch_image(YacosCartItem *self)
{
return;
}
yacos_tag_id yacos_cart_item_get_id(YacosCartItem *self)
{
return self->priv.tag_id;
}

View File

@ -16,6 +16,7 @@ GtkWidget *yacos_cart_item_new(void);
void yacos_cart_item_set_id(YacosCartItem *self, yacos_tag_id tag_id); void yacos_cart_item_set_id(YacosCartItem *self, yacos_tag_id tag_id);
void yacos_cart_item_fetch_meta(YacosCartItem *self); void yacos_cart_item_fetch_meta(YacosCartItem *self);
void yacos_cart_item_fetch_image(YacosCartItem *self); void yacos_cart_item_fetch_image(YacosCartItem *self);
yacos_tag_id yacos_cart_item_get_id(YacosCartItem *self);
G_END_DECLS G_END_DECLS

View File

@ -30,7 +30,7 @@ static void activate(GtkApplication* app, gpointer user_data)
g_signal_connect(scanner, "newtag", g_signal_connect(scanner, "newtag",
(GCallback) mainWindow_scan_tags, (*mainWindow)); (GCallback) mainWindow_scan_tags, (*mainWindow));
mainWindow_scan_tags(scanner, *mainWindow); // TODO: For testing only //mainWindow_scan_tags(scanner, *mainWindow); // TODO: For testing only
} }

View File

@ -29,8 +29,28 @@ void mainWindow_scan_tags(YacosScanner *scanner, struct mainWindow *mainWindow)
// TODO: For testing only // TODO: For testing only
gtk_stack_set_visible_child(mainWindow->stack_main, mainWindow->page_cart.box); gtk_stack_set_visible_child(mainWindow->stack_main, mainWindow->page_cart.box);
GtkWidget *le = yacos_cart_item_new(); GtkWidget *le = yacos_cart_item_new();
yacos_cart_item_set_id(YACOS_CART_ITEM(le), 123); GList *l;
yacos_tag_id tag_id;
GList *stack_children;
GList *ll;
gboolean found = 0;
for (l = yacos_scanner_get_present_tags(scanner); l != NULL; l = l->next) {
tag_id = ((struct yacos_scanner_tag*) l->data)->tag_id;
stack_children = gtk_container_get_children(mainWindow->page_cart.list);
for (ll = stack_children; ll != NULL; ll = ll->next) {
if (yacos_cart_item_get_id(((YacosCartItem *) ll->data)) == tag_id) {
found = 1;
break;
}
}
if (found)
continue;
yacos_cart_item_set_id(YACOS_CART_ITEM(le), tag_id);
yacos_cart_item_fetch_meta(YACOS_CART_ITEM(le)); yacos_cart_item_fetch_meta(YACOS_CART_ITEM(le));
yacos_cart_item_fetch_image(YACOS_CART_ITEM(le));
gtk_list_box_insert(mainWindow->page_cart.list, le, -1); gtk_list_box_insert(mainWindow->page_cart.list, le, -1);
gtk_widget_show(le); gtk_widget_show(le);
}
} }

View File

@ -100,7 +100,7 @@ static int yacos_scanner_list(YacosScanner *self)
if (new_tags) { if (new_tags) {
for (l = self->detected_tags; l != NULL; l = l->next) { for (l = self->detected_tags; l != NULL; l = l->next) {
g_warning("Found %x (%d)", g_info("Found %X (%d)",
((struct yacos_scanner_tag*)l->data)->tag_id, ((struct yacos_scanner_tag*)l->data)->tag_id,
((struct yacos_scanner_tag*)l->data)->tag_type); ((struct yacos_scanner_tag*)l->data)->tag_type);
} }
@ -152,8 +152,6 @@ static void yacos_scanner_init(YacosScanner *self)
{ {
self->detected_tags = NULL; self->detected_tags = NULL;
return; // TODO: Disabled during development
g_debug("Using libnfc %s", nfc_version()); g_debug("Using libnfc %s", nfc_version());
nfc_init(&self->nfc_ctx); nfc_init(&self->nfc_ctx);
if (!self->nfc_ctx) { if (!self->nfc_ctx) {
@ -177,5 +175,11 @@ YacosScanner *yacos_scanner_new(void)
// Free afterwards using g_free // Free afterwards using g_free
gchar *yacos_tag_id_to_str(yacos_tag_id tag_id) gchar *yacos_tag_id_to_str(yacos_tag_id tag_id)
{ {
return g_strdup_printf("%d", tag_id); return g_strdup_printf("%X", tag_id);
}
GList *yacos_scanner_get_present_tags(YacosScanner *scanner)
{
return scanner->detected_tags;
} }

View File

@ -25,6 +25,7 @@ struct yacos_scanner_tag {
}; };
gchar *yacos_tag_id_to_str(yacos_tag_id tag_id); gchar *yacos_tag_id_to_str(yacos_tag_id tag_id);
GList *yacos_scanner_get_present_tags(YacosScanner *scanner);
G_END_DECLS G_END_DECLS