Hi,

Having thought about the approach whilst on holiday I've changed the API
and cleaned up the code. This patch makes the bitzi.c code unaware of
the GUI and provides a different query function which passes a callback
for when the data is available. Hopefully this will make it possible for
both the GUI and core to access the data without the bitzi code getting
hairy (for the one day we may separate the two ;-)

Where am I going with all this? My current plan is something like this

* Enumerating the Bitzi data a bit more and fill out the bitzi_data_t
* Make the search lines change colour according to the result
(Green=Good, Red=Bad).
* Cache and age the bitzi results, provide a query_cache function
* Auto check the cache as new results come in
* Hook in the active/queued downloads into the framework

Longer term ideas include reducing the propagation of bad results by
filtering against the cache. I would like to have nicer GUI feedback
(icons?) but that would require help from the GUI chaps.

I have some GTK2 patches to the glade and _cb files which I shall post
in a moment.


-- 
Alex, homepage: http://www.bennee.com/~alex/
How kind of you to be willing to live someone's life for them.
Index: bitzi.c
===================================================================
RCS file: /cvsroot/gtk-gnutella/gtk-gnutella-current/src/bitzi.c,v
retrieving revision 1.2
diff -u -b -r1.2 bitzi.c
--- bitzi.c	5 Aug 2004 14:13:53 -0000	1.2
+++ bitzi.c	13 Aug 2004 13:52:03 -0000
@@ -1,10 +1,23 @@
-/*
+/* -*- mode: cc-mode; tab-width:4; -*-
+ *  
  * $Id: bitzi.c,v 1.2 2004/08/05 14:13:53 cbiere Exp $
  *
  * Copyright (c) 2004, Alex Bennee <[EMAIL PROTECTED]>
  *
  * Bitzi search code
  *
+ * This code makes searches to the Bitzi (bitzi.com) meta-data
+ * service. It is independant of any GUI knowledge (the GUI has to
+ * pass a callback with user data for each request so process the
+ * results if there any)
+ *
+ * The code requires libxml to parse the XML responses
+ *
+ * TODO:
+ *	caching of results
+ *  cache only query for use by core
+ *  filling of data from gnet (requires gnet to pass data about)?
+ *
  *----------------------------------------------------------------------
  * This file is part of gtk-gnutella.
  *
@@ -29,13 +42,9 @@
 
 RCSID("$Id: bitzi.c,v 1.2 2004/08/05 14:13:53 cbiere Exp $");
 
-#ifdef USE_GTK2
-
 #include <libxml/parser.h>
 #include <libxml/tree.h>
 
-#include "gui.h"
-#include "search_gui.h"	/* search_t */
 #include "http.h"		/* http async stuff */
 #include "bitzi.h"		/* bitzi metadata */
 
@@ -50,11 +59,20 @@
 typedef struct {
 	guchar			*bitzi_url;		/* request URL */
 	bitzi_data_t	*bitzi_data;	/* extracted data */
-	record_t		*record;		/* the record */
+
+	/* callback info */
+	bitzi_query_cb_t	callback;		/* callback */
+	gpointer			callback_data;	/* user data for the callback */
+
+	/* xml related bits */
 	xmlParserCtxt	*ctxt;		   	/* libxml parser context */
+	
 } bitzi_request_t;
 
-static GSList *bitzi_request_queue = NULL;
+/*
+ * The request queue, the searches to the Bitzi data service are queued
+ */
+static GSList *bitzi_rq = NULL;
 
 static bitzi_request_t	*current_bitzi_request = NULL;
 static gpointer	 current_bitzi_request_handle;
@@ -63,9 +81,6 @@
 static gboolean do_metadata_query(gpointer ptr);
 static void process_meta_data(bitzi_request_t *request);
 
-#if 0
-static gboolean do_metadata_query(gpointer *ptr);
-#endif /* 0 */
 
 /**
  * Populate callback: more data available. When called with 0 it stops
@@ -235,7 +250,7 @@
 	g_free(request);
 
 	/* set the next query up as a timeout */
-	if (bitzi_request_queue != NULL) {
+	if (bitzi_rq != NULL) {
 	  g_timeout_add(1 * 10000, (GSourceFunc) do_metadata_query, NULL);
 	}
 
@@ -251,10 +266,10 @@
 {
 	g_message("do_metadata_query");
 
-	if (current_bitzi_request == NULL && bitzi_request_queue != NULL) {
-		current_bitzi_request = bitzi_request_queue->data;
-		bitzi_request_queue = g_slist_remove(bitzi_request_queue,
-				current_bitzi_request);
+	if (current_bitzi_request == NULL && bitzi_rq != NULL) {
+
+		current_bitzi_request = bitzi_rq->data;
+		bitzi_rq = g_slist_remove(bitzi_rq,current_bitzi_request);
 
 		/*
 		 * Create the XML Parser
@@ -293,66 +308,37 @@
 	return FALSE;
 }
 
-
-
 /**
- * Queue on entry on the metadata search queue
+ * A GUI/Bitzi API passes a pointer to the search type (currently only
+ * urn:sha1), a pointer to a callback function and a user data
+ * pointer.
+ *
+ * If no query succeds then the call back is never made, however we
+ * should always get some sort of data back from the service.
+ *
+ * TODO:
+ *     Implement caching of results so we can return straight away
  */
-static void
-queue_metadata_search(record_t *rec, gpointer null_data)
-{
-	bitzi_request_t	*request;
 
-	g_assert(rec != NULL);
+bitzi_data_t * bitzi_queryby_urnsha1(gchar *urnsha1, bitzi_query_cb_t callback, gpointer userdata)
+{
+	if (urnsha1!=NULL)
+	{
+		bitzi_request_t	*request = g_malloc(sizeof *request);
+
+		/* setup the callback */
+		request->callback = callback;
+		request->callback_data = userdata;
 
-	if (rec->sha1 != NULL) {
-		request = g_malloc(sizeof *request);
-		request->record = rec;
+		/* build the bitzi url */
 		request->bitzi_url = g_strdup_printf(
 								"http://ticket.bitzi.com/rdf/urn:sha1:%s";,
-								sha1_base32(rec->sha1));
-
-		g_message("queue_metadat_search request=%s (%p)",
-			request->bitzi_url, request->bitzi_url);
-
-		bitzi_request_queue = g_slist_append(bitzi_request_queue, request);
-	}
-}
-
-/*
- ** GUI Actions
- */
-
-void
-on_search_meta_data_activate(GtkMenuItem *menuitem, gpointer user_data)
-{
-	search_t *search;
-	GtkTreeSelection *selection;
-	GSList *sl;
-
-	g_message("on_search_meta_data_active: called");
-
-	/* collect the list of files selected */
-
-	search = search_gui_get_current_search();
-	g_assert(search != NULL);
-
-	selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(search->tree_view));
+								sha1_base32(urnsha1));
 
-	sl = tree_selection_collect_data(selection, gui_record_sha1_eq);
+		bitzi_rq = g_slist_append(bitzi_rq, request);
 
-	/* Queue up our requests */
-	g_message("on_search_meta_data: %d items",
-		g_slist_position(sl, g_slist_last(sl)) + 1);
-
-	g_slist_foreach(sl, (GFunc) queue_metadata_search, NULL);
-	g_slist_free(sl);
-
-	/* Kick off requests if nothing happening */
-	if (current_bitzi_request == NULL)
 		do_metadata_query(NULL);
-
+	}
 }
 
 /* vi: set ts=4 sw=4 cindent: */
-#endif /* USE_GTK2 */
Index: bitzi.h
===================================================================
RCS file: /cvsroot/gtk-gnutella/gtk-gnutella-current/src/bitzi.h,v
retrieving revision 1.1
diff -u -b -r1.1 bitzi.h
--- bitzi.h	4 Aug 2004 03:38:42 -0000	1.1
+++ bitzi.h	13 Aug 2004 13:52:03 -0000
@@ -1,4 +1,5 @@
-/*
+/* -*- mode: cc-mode; tab-width:4; -*-
+ *
  * $Id: bitzi.h,v 1.1 2004/08/04 03:38:42 cbiere Exp $
  *
  * Copyright (c) 2004, Alex Bennee <[EMAIL PROTECTED]>
@@ -38,4 +39,15 @@
     xmlChar	*judgement;
 } bitzi_data_t;
 
+
+/*
+** Bitzi API declartions
+*/
+
+typedef void (* bitzi_query_cb_t) (bitzi_data_t *bitzi_data, gpointer user_data);
+
+bitzi_data_t * bitzi_queryby_urnsha1(gchar *urnsha1, bitzi_query_cb_t callback, gpointer userdata);
+
+
+
 #endif /* _bitzi_h_ */

Reply via email to