Browse Source

Now properly copying the properties of KonaImage. Started KonaSearch.

Markus Koch 3 years ago
parent
commit
c3fde6b9fc

+ 2 - 0
konaclient-gtk/main.c

@@ -47,6 +47,7 @@ int buildWindow() {
 
 
     /* Testing callbacks */
+    // NOTE: This test code does not care about freeing memory right now.
     GValue val = G_VALUE_INIT;
     GValue sval = G_VALUE_INIT;
     kona_image_meta meta;
@@ -54,6 +55,7 @@ int buildWindow() {
     meta.width = 200;
     meta.id = 42;
     meta.image_url = "http://meow.cat/fullpic.png";
+    meta.tags = NULL;
 
     g_value_init(&sval, G_TYPE_STRING);
     g_value_set_string(&sval, "/home/markus/Pictures/FFT-an.png");

+ 35 - 2
libkonaclient-test/main.c

@@ -1,16 +1,23 @@
 #include <stdio.h>
 #include "konaimage.h"
+#include "konasearch.h"
+
+#define PRINT_TEST_HEADER() printf("\n========= %s =========\n", __func__)
 
 void cb_meta_changed(KonaImage* ki) {
     kona_image_meta *meta;
     meta = kona_image_get_meta_from_konaimage(ki);
     printf("%s: META.ID = %d\n", __func__, meta->id);
+    for (GList *l = meta->tags; l; l = l->next) {
+        printf ("    meta.tag = %p: %s\n", l->data, l->data);
+    }
 }
 
-int main(int argc, char *argv[])
-{
+void test_kona_image() {
     KonaImage *ki;
 
+    PRINT_TEST_HEADER();
+
     ki = kona_image_new();
     g_signal_connect(ki, "notify::meta", (GCallback)cb_meta_changed, NULL);
 
@@ -18,10 +25,21 @@ int main(int argc, char *argv[])
     GValue val = G_VALUE_INIT;
     GValue sval = G_VALUE_INIT;
     kona_image_meta meta;
+
     meta.height = 100;
     meta.width = 200;
     meta.id = 42;
+    meta.tags = NULL;
     meta.image_url = "http://meow.cat/fullpic.png";
+    meta.preview_url = "http://meow.cat/preview.png";
+    meta.tags = g_list_append(meta.tags, "hello");
+    meta.tags = g_list_append(meta.tags, "world");
+
+    for (GList *l = meta.tags; l; l = l->next) {
+        printf ("ADD meta.tag = %p: %s\n", l->data, l->data);
+    }
+
+    printf("\n");
 
     g_value_init(&sval, G_TYPE_STRING);
     g_value_set_string(&sval, "/path/to/file.png");
@@ -31,5 +49,20 @@ int main(int argc, char *argv[])
     g_value_set_pointer(&val, (gpointer) &meta);
     g_object_set_property((GObject*) ki, "meta", &val);
 
+    g_object_unref(ki);
+}
+
+void test_kona_search() {
+    KonaSearch *ks;
+
+    PRINT_TEST_HEADER();
+    ks = kona_search_new();
+}
+
+int main(int argc, char *argv[])
+{
+    test_kona_image();
+    test_kona_search();
+
     return 0;
 }

+ 25 - 15
libkonaclient/konaimage.c

@@ -2,10 +2,6 @@
 #include <stdio.h>
 #include <string.h>
 
-typedef struct {
-    guint64 test;
-} KonaImagePrivate;
-
 struct _KonaImage {
     GObject parent_instance; // struct of parent (inherit)
     kona_image_meta meta;
@@ -14,7 +10,7 @@ struct _KonaImage {
 };
 
 
-G_DEFINE_TYPE_WITH_PRIVATE(KonaImage, kona_image, G_TYPE_OBJECT)
+G_DEFINE_TYPE(KonaImage, kona_image, G_TYPE_OBJECT)
 
 enum {
     PROP_META=1,
@@ -45,6 +41,7 @@ static void kona_image_get_property(GObject *object, guint prop_id, GValue *valu
 
 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:
@@ -56,7 +53,13 @@ static void kona_image_set_property(GObject *object, guint prop_id, const GValue
         self->preview_url = (GString*) g_strdup(g_value_get_string(value));
         break;
     case PROP_META:
-        memcpy(&self->meta, g_value_get_pointer(value), sizeof(kona_image_meta));
+        meta = g_value_get_pointer(value);
+        self->meta.id = meta->id;
+        self->meta.width= meta->width;
+        self->meta.height = meta->height;
+        self->meta.image_url = g_strdup(meta->image_url);
+        self->meta.preview_url = g_strdup(meta->preview_url);
+        self->meta.tags = g_list_copy_deep(meta->tags, (GCopyFunc) g_strdup, NULL);
         break;
     default:
         break;
@@ -68,6 +71,8 @@ static void kona_image_class_init(KonaImageClass *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);
@@ -82,20 +87,25 @@ static void kona_image_class_init(KonaImageClass *klass) {
 }
 
 static void kona_image_init (KonaImage *self) {
-
+    self->meta.tags = NULL;
 }
 
-static void kona_image_dispose(KonaImage *self) {
-    // Clear privates here
+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(KonaImage *self) {
-    // Clear public vars here
-    g_free(self->meta.image_url);
-    g_free(self->meta.preview_url);
-    g_free(self->meta.tags);
-    G_OBJECT_CLASS (kona_image_parent_class)->finalize ((GObject*) self);
+static void kona_image_finalize(GObject *self) {
+    KonaImage *ki = (KonaImage*) self;
+
+    // Free dynamic meta fields
+    g_free(ki->meta.image_url);
+    g_free(ki->meta.preview_url);
+    g_list_free(ki->meta.tags);
+    //g_list_free_full(ki->meta.tags, g_free);
+
+    G_OBJECT_CLASS (kona_image_parent_class)->finalize (self);
 }
 
 

+ 11 - 2
libkonaclient/konaimage.h

@@ -14,13 +14,22 @@ KonaImage *kona_image_new (void);
 struct kona_image_meta_struct {
     int id;
     GList *tags;
-    char* preview_url;
-    char* image_url;
+    gchar* preview_url;
+    gchar* image_url;
     int width;
     int height;
 };
 typedef struct kona_image_meta_struct kona_image_meta;
 
+/*** GObject functions ***/
+KonaImage *kona_image_new();
+static void kona_image_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec); // WARN: This returns pointers to the actual data. Do not edit.
+static void kona_image_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec); // Every value will be copied (with subelements), so make sure to clear your variables after sending them here.
+static void kona_image_class_init(KonaImageClass *klass);
+static void kona_image_init (KonaImage *self);
+static void kona_image_dispose(GObject *self);
+static void kona_image_finalize(GObject *self);
+
 
 // Access functions for parameters (read only)
 kona_image_meta* kona_image_get_meta_from_konaimage(KonaImage *ki);

+ 39 - 0
libkonaclient/konasearch.c

@@ -0,0 +1,39 @@
+#include "konasearch.h"
+
+#include <stdio.h>
+
+/*** GLIB object start ***/
+
+struct _KonaSearch {
+    GObject parent_instance; // struct of parent (inherit)
+};
+
+G_DEFINE_TYPE(KonaSearch, kona_search, G_TYPE_OBJECT)
+
+static void kona_search_class_init(KonaSearchClass *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_search_dispose;
+    object_class->finalize = kona_search_finalize;
+}
+
+static void kona_search_init (KonaSearch *self) {
+    printf("kona_search_init\n");
+}
+
+static void kona_search_dispose(GObject *self) {
+    G_OBJECT_CLASS (kona_search_parent_class)->dispose((GObject*) self);
+}
+
+static void kona_search_finalize(GObject *self) {
+    G_OBJECT_CLASS (kona_search_parent_class)->finalize ((GObject*) self);
+}
+
+KonaSearch *kona_search_new()
+{
+    return g_object_new(KONA_TYPE_SEARCH, 0);
+}
+
+/*** GLIB object end ***/

+ 22 - 0
libkonaclient/konasearch.h

@@ -0,0 +1,22 @@
+#ifndef KONASEARCH_H
+#define KONASEARCH_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define KONA_TYPE_SEARCH (kona_search_get_type())
+
+G_DECLARE_FINAL_TYPE (KonaSearch, kona_search, KONA, SEARCH, GObject)
+
+KonaSearch *kona_search_new();
+static void kona_search_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
+static void kona_search_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
+static void kona_search_class_init(KonaSearchClass *klass);
+static void kona_search_init (KonaSearch *self);
+static void kona_search_dispose(GObject *self);
+static void kona_search_finalize(GObject *self);
+
+G_END_DECLS
+
+#endif // KONASEARCH_H