Yet Another Check-Out System
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

228 lines
6.5 KiB

#include "cart_item.h"
#include <json-glib/json-glib.h>
typedef struct _YacosCartItemPriv {
GtkWidget *label_title;
GtkWidget *label_description;
GtkWidget *label_comment;
GtkWidget *label_id;
GtkWidget *button_properties;
GtkWidget *button_delete;
GtkWidget *image;
GtkStack *stack_image_loader;
yacos_tag_id tag_id;
struct api {
gchar *url_base;
gchar *token;
SoupSession *soup_session_meta;
} api;
} YacosCartItemPriv;
struct _YacosCartItem {
/* Inheritance */
GtkListBoxRow parent;
/* Custom Elements */
YacosCartItemPriv priv;
};
G_DEFINE_TYPE(YacosCartItem, yacos_cart_item, GTK_TYPE_LIST_BOX_ROW)
static void yacos_cart_item_dispose(GObject *obj)
{
/* destroy parent container. This destroys all widgets inside */
G_OBJECT_CLASS(yacos_cart_item_parent_class)->dispose(obj);
}
static void yacos_cart_item_constructed(GObject *obj)
{
G_OBJECT_CLASS(yacos_cart_item_parent_class)->constructed(obj);
}
static void yacos_cart_item_finalize(GObject *gobject)
{
YacosCartItem *self = yacos_cart_item_get_instance_private(YACOS_CART_ITEM(gobject));
g_free(self->priv.api.url_base);
g_free(self->priv.api.token);
G_OBJECT_CLASS(yacos_cart_item_parent_class)->constructed(gobject);
}
static void yacos_cart_item_class_init(YacosCartItemClass *klass)
{
GObjectClass *oclass = G_OBJECT_CLASS(klass);
oclass->dispose = yacos_cart_item_dispose;
oclass->constructed = yacos_cart_item_constructed;
oclass->finalize = yacos_cart_item_finalize;
}
static void yacos_cart_item_init(YacosCartItem *self)
{
GtkBuilder *builder;
GtkWidget *box;
builder = gtk_builder_new_from_resource("/net/notsyncing/yacos/glade/cartItem.glade");
box = GTK_WIDGET(gtk_builder_get_object(builder, "cart_item"));
self->priv.label_id = GTK_WIDGET(gtk_builder_get_object(builder, "label_id"));
self->priv.label_title = GTK_WIDGET(gtk_builder_get_object(builder, "label_title"));
self->priv.label_comment = GTK_WIDGET(gtk_builder_get_object(builder, "label_comment"));
self->priv.label_description = GTK_WIDGET(gtk_builder_get_object(builder, "label_description"));
self->priv.button_delete = GTK_WIDGET(gtk_builder_get_object(builder, "button_delete"));
self->priv.button_properties = GTK_WIDGET(gtk_builder_get_object(builder, "button_properties"));
self->priv.image = GTK_WIDGET(gtk_builder_get_object(builder, "image_item"));
self->priv.stack_image_loader = GTK_STACK(gtk_builder_get_object(builder, "stack_image_loader"));
self->priv.api.url_base = g_strdup("http://localhost:8080/api.php");
self->priv.api.token = g_strdup("123456");
self->priv.api.soup_session_meta = soup_session_new();
gtk_container_add(GTK_CONTAINER(self), box);
g_object_unref(builder);
}
GtkWidget *yacos_cart_item_new(void)
{
return GTK_WIDGET(g_object_new(TYPE_YACOS_CART_ITEM, NULL));
}
void yacos_cart_item_set_id(YacosCartItem *self, yacos_tag_id tag_id)
{
gchar *id_str;
gchar *id_str2;
self->priv.tag_id = tag_id;
id_str = yacos_tag_id_to_str(tag_id);
id_str2 = g_strdup_printf("#%s", 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)
{
YacosCartItem *self = YACOS_CART_ITEM(user_data);
const char *temp;
JsonParser *parser;
JsonReader *reader;
if (msg->status_code != 200) {
// TODO: Make this somewhat error tolerant
g_error("Received error code %d from server.", msg->status_code);
return;
}
parser = json_parser_new ();
json_parser_load_from_data (parser,
msg->response_body->data,
msg->response_body->length,
NULL);
reader = json_reader_new (json_parser_get_root (parser));
json_reader_read_member (reader, "title");
temp = json_reader_get_string_value (reader);
gtk_label_set_text(GTK_LABEL(self->priv.label_title), temp);
json_reader_end_member (reader);
json_reader_read_member (reader, "description");
temp = json_reader_get_string_value (reader);
gtk_label_set_text(GTK_LABEL(self->priv.label_description), temp);
json_reader_end_member (reader);
json_reader_read_member (reader, "comment");
temp = json_reader_get_string_value (reader);
gtk_label_set_text(GTK_LABEL(self->priv.label_comment), temp);
json_reader_end_member (reader);
g_object_unref (reader);
g_object_unref (parser);
}
void yacos_cart_item_fetch_meta(YacosCartItem *self)
{
SoupMessage *msg;
gchar *url;
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);
url = g_strdup_printf("%s?meta&id=%s", self->priv.api.url_base, id_str);
msg = soup_message_new("GET", url);
soup_message_headers_append (msg->request_headers, "x-token", self->priv.api.token);
g_free(url);
g_free(id_str);
soup_session_queue_message(self->priv.api.soup_session_meta,
msg,
yacos_cart_item_fetch_meta_cb,
self);
// TODO: The soup 's probably memory-leaking all over the place. Check this some time..
}
static void yacos_cart_item_fetch_image_cb (SoupSession *session, SoupMessage *msg, gpointer user_data)
{
YacosCartItem *self = YACOS_CART_ITEM(user_data);
GdkPixbuf *pixbuf_raw, *pixbuf_scaled;
GMemoryInputStream *s;
if (msg->status_code != 200) {
g_warning("image: Received error code %d from server.", msg->status_code);
} else {
s = G_MEMORY_INPUT_STREAM(g_memory_input_stream_new());
g_memory_input_stream_add_data(s, msg->response_body->data, msg->response_body->length, NULL);
pixbuf_raw = gdk_pixbuf_new_from_stream(G_INPUT_STREAM(s), NULL, NULL);
g_object_unref(s);
pixbuf_scaled = gdk_pixbuf_scale_simple(pixbuf_raw, 100, 100, GDK_INTERP_BILINEAR);
g_object_unref(pixbuf_raw);
gtk_image_set_from_pixbuf(GTK_IMAGE(self->priv.image), pixbuf_scaled);
g_object_unref(pixbuf_scaled);
}
gtk_stack_set_visible_child(self->priv.stack_image_loader, self->priv.image);
}
void yacos_cart_item_fetch_image(YacosCartItem *self)
{
SoupMessage *msg;
gchar *url;
gchar *id_str;
id_str = yacos_tag_id_to_str(self->priv.tag_id);
url = g_strdup_printf("%s?image&id=%s", self->priv.api.url_base, id_str);
g_free(id_str);
msg = soup_message_new("GET", url);
soup_message_headers_append (msg->request_headers, "x-token", self->priv.api.token);
g_free(url);
soup_session_queue_message(self->priv.api.soup_session_meta,
msg,
yacos_cart_item_fetch_image_cb,
self);
/* TODO: More memory leaks? */
}
yacos_tag_id yacos_cart_item_get_id(YacosCartItem *self)
{
return self->priv.tag_id;
}