Browse Source

KonaSearch: Added code to clear the search again

Markus Koch 3 years ago
parent
commit
555edeb10c
3 changed files with 33 additions and 7 deletions
  1. 1 1
      libkonaclient-test/main.c
  2. 27 6
      libkonaclient/konasearch.c
  3. 5 0
      libkonaclient/konasearch.h

+ 1 - 1
libkonaclient-test/main.c

@@ -2,7 +2,7 @@
 #include "konaimage.h"
 #include "konasearch.h"
 
-#define PRINT_TEST_HEADER() printf("\n========= %s =========\n", __func__)
+#define PRINT_TEST_HEADER() printf("\n========= %s =========\n", __func__); fflush(stdout);
 
 void cb_meta_changed(KonaImage* ki) {
     kona_image_meta *meta;

+ 27 - 6
libkonaclient/konasearch.c

@@ -1,8 +1,9 @@
 #include "konasearch.h"
+#include "konaimage.h"
+
 #include <stdio.h>
 #include <libsoup/soup.h>
 #include <libxml2/libxml/parser.h>
-#include "konaimage.h"
 
 // Function prototypes for local functions
 static void kona_search_posts_soup_cb (SoupSession *session, SoupMessage *msg, gpointer user_data);
@@ -49,8 +50,8 @@ static void kona_search_dispose(GObject *self)
 static void kona_search_finalize(GObject *self)
 {
     KonaSearch *ks = (KonaSearch*) self;
-    //g_free(ks->postsSession);
     g_free(ks->searchString);
+    // TODO: Free SoupSession ks->postsSession
     G_OBJECT_CLASS (kona_search_parent_class)->finalize ((GObject*) self);
 }
 
@@ -116,21 +117,24 @@ static void kona_search_posts_soup_cb (SoupSession *session, SoupMessage *msg, g
                     meta.tags        = NULL;
                     tagsplit = g_strsplit(xmlGetProp(xNodePtr, "tags"), " ", 100);
                     for (tagsplitc = tagsplit; *tagsplitc; tagsplitc++) {
-                        meta.tags = g_list_append(meta.tags, g_strdup(*tagsplitc));
+                        meta.tags = g_list_append(meta.tags, *tagsplitc);
                     }
-                    g_strfreev(tagsplit);
 
                     // Create new KonaImage
                     ki = kona_image_new();
                     g_value_set_pointer(&val, (gpointer) &meta);
                     g_object_set_property((GObject*) ki, "meta", &val);
 
+                    // Free temp string (copied by g_object_set_property)
+                    g_strfreev(tagsplit);
+
                     // Add to list
                     ks->searchResult.images = g_list_append(ks->searchResult.images, ki);
                 }
             } while (xNodePtr = xNodePtr->next);
         }
-        else {
+        else
+        {
             fprintf(stderr, "Error: Invalid XML root node!\n");
         }
     }
@@ -138,5 +142,22 @@ static void kona_search_posts_soup_cb (SoupSession *session, SoupMessage *msg, g
     {
         fprintf(stderr, "Error parsing XML result!\n");
     }
-    fflush(stdout);
+
+    xmlFreeDoc(xDocPtr);
+}
+
+void kona_search_clear_full(KonaSearch *ks)
+{
+    ks->searchResult.count = -1;
+
+    g_list_free_full(ks->searchResult.images, g_object_unref);
+    ks->searchResult.images = NULL;
+}
+
+void kona_search_clear(KonaSearch *ks)
+{
+    ks->searchResult.count = -1;
+
+    g_list_free(ks->searchResult.images);
+    ks->searchResult.images = NULL;
 }

+ 5 - 0
libkonaclient/konasearch.h

@@ -18,7 +18,12 @@ static void kona_search_init (KonaSearch *self);
 static void kona_search_dispose(GObject *self);
 static void kona_search_finalize(GObject *self);
 
+// Starts a search.
 void kona_search_start(KonaSearch *self);
+// Clears the search results and deallocates ALL buffer - including all created KonaImages
+void kona_search_clear_full(KonaSearch *ks);
+// Clears the search, but leaves the KonaImages intact
+void kona_search_clear(KonaSearch *ks);
 
 G_END_DECLS