konaclient-gtk/libkonaclient/konaimage.c

136 lines
3.9 KiB
C

#include "konaimage.h"
#include <stdio.h>
#include <string.h>
struct _KonaImage {
GObject parent_instance; // Struct of parent (inherit)
kona_image_meta meta; // Meta info of the picture
GString* preview_file; // Local URL of the preview image
GString* full_file; // Local URL of the full image
};
G_DEFINE_TYPE(KonaImage, kona_image, G_TYPE_OBJECT)
enum {
PROP_META=1,
PROP_FILE_PREVIEW,
PROP_FILE_FULL,
PROP_COUNT
};
static GParamSpec *properties [PROP_COUNT];
static void kona_image_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) {
KonaImage *self = (KonaImage*)object;
switch (prop_id) {
case PROP_FILE_PREVIEW:
g_value_set_string(value, (const gchar*) self->preview_file);
break;
case PROP_FILE_FULL:
g_value_set_string(value, (const gchar*) self->full_file);
break;
case PROP_META:
g_value_set_pointer(value, &self->meta);
break;
default:
break;
}
}
static void kona_image_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) {
KonaImage *self = (KonaImage*)object;
kona_image_meta *meta;
switch (prop_id) {
case PROP_FILE_FULL:
g_free(self->full_file);
self->full_file = (GString*) g_strdup(g_value_get_string(value));
break;
case PROP_FILE_PREVIEW:
g_free(self->preview_file);
self->preview_file = (GString*) g_strdup(g_value_get_string(value));
break;
case PROP_META:
meta = g_value_get_pointer(value);
self->meta.id = meta->id;
self->meta.width= meta->width;
self->meta.height = meta->height;
// Free URLs (strings)
g_free(self->meta.image_url);
self->meta.image_url = g_strdup(meta->image_url);
g_free(self->meta.preview_url);
self->meta.preview_url = g_strdup(meta->preview_url);
// Free tag list (string list)
g_list_free_full(self->meta.tags, g_free);
self->meta.tags = g_list_copy_deep(meta->tags, (GCopyFunc) g_strdup, NULL);
break;
default:
break;
}
}
static void kona_image_class_init(KonaImageClass *klass) {
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->set_property = kona_image_set_property;
object_class->get_property = kona_image_get_property;
object_class->dispose = kona_image_dispose;
object_class->finalize = kona_image_finalize;
properties [PROP_META] =
g_param_spec_pointer("meta", "meta", "Meta info struct", G_PARAM_READWRITE);
properties [PROP_FILE_FULL] =
g_param_spec_string("fullFile", "fullFile", "Local URL to the file", "", G_PARAM_READWRITE);
properties [PROP_FILE_PREVIEW] =
g_param_spec_string("previewFile", "previewFile", "Local URL to the preview file", "", G_PARAM_READWRITE);
g_object_class_install_properties(object_class, PROP_COUNT, properties);
}
static void kona_image_init (KonaImage *self) {
self->meta.tags = NULL;
}
static void kona_image_dispose(GObject *self) {
//KonaImage *ki = (KonaImage*) self;
G_OBJECT_CLASS (kona_image_parent_class)->dispose((GObject*) self);
}
static void kona_image_finalize(GObject *self) {
KonaImage *ki = (KonaImage*) self;
// Free strings
g_free(ki->full_file);
g_free(ki->preview_file);
// Free dynamic meta fields
g_free(ki->meta.image_url);
g_free(ki->meta.preview_url);
g_list_free_full(ki->meta.tags, g_free);
G_OBJECT_CLASS (kona_image_parent_class)->finalize (self);
}
KonaImage *kona_image_new()
{
return g_object_new(KONA_TYPE_IMAGE, 0);
}
kona_image_meta* kona_image_get_meta_from_konaimage(KonaImage *ki) {
kona_image_meta *meta;
GValue val = G_VALUE_INIT;
g_value_init(&val, G_TYPE_POINTER);
g_object_get_property(G_OBJECT(ki), "meta", &val);
meta = g_value_get_pointer(&val);
g_value_reset(&val);
return meta;
}