# HG changeset patch
# User morphbr
# Date 1169675543 0
# Node ID 01d60f80fa524566f5f725d46f4f3cc09ba52eef
# Parent  a84d2982ff016256574c45f475aa5b0aff8a573c
[svn r301] Get ProgramGuide info from XML+Http at gmyth_http

diff -r a84d2982ff01 -r 01d60f80fa52 gmyth/src/gmyth_http.c
--- a/gmyth/src/gmyth_http.c	Wed Jan 24 21:03:05 2007 +0000
+++ b/gmyth/src/gmyth_http.c	Wed Jan 24 21:52:23 2007 +0000
@@ -34,6 +34,7 @@
 #include <assert.h>
 #include <libxml/parser.h>
 #include <libxml/tree.h>
+#include <libxml/xpath.h>
 
 #include "gmyth_http.h"
 #include "gmyth_debug.h"
@@ -45,6 +46,102 @@
   size_t size;
 };
 
+
+xmlXPathObjectPtr
+getnodeset(xmlDocPtr doc, xmlChar *xpath)
+{
+
+    xmlXPathContextPtr context;
+    xmlXPathObjectPtr result;
+
+    context = xmlXPathNewContext(doc);
+    result = xmlXPathEvalExpression(xpath, context);
+
+    if(xmlXPathNodeSetIsEmpty(result->nodesetval))
+    {
+        g_printf("No result\n");
+        return NULL;
+    }
+
+    xmlXPathFreeContext(context);
+    return result;
+}
+
+
+xmlDocPtr XMLParse (const char *content, int length) 
+{
+    xmlDocPtr doc; /* the resulting document tree */
+
+    doc = xmlReadMemory(content, length, NULL, NULL, 0);
+    if (doc == NULL) 
+    {
+        g_fprintf(stderr, "Failed to parse document\n");
+        return NULL;
+    }
+
+    return doc;
+}
+
+xmlXPathObjectPtr getXPath (xmlChar *xpath, xmlDocPtr doc)
+{
+    xmlXPathObjectPtr result;
+    result = getnodeset(doc, xpath);
+    return result;
+}
+
+void get_ProgramGuide_Properties(xmlNodePtr nodeTab, GMythEpg* epg)
+{
+	sscanf ((char *)xmlGetProp(nodeTab, (xmlChar *)"startChanId"), "%d", &(epg->startChanId));
+	sscanf ((char *)xmlGetProp(nodeTab, (xmlChar *)"endChanId"), "%d", &(epg->endChanId));
+
+	epg->version = g_string_new((char *)xmlGetProp(nodeTab, (xmlChar *)"version"));
+
+	sscanf ((char *)xmlGetProp(nodeTab, (xmlChar *)"protoVer"), "%d", &(epg->protoVer));
+	sscanf ((char *)xmlGetProp(nodeTab, (xmlChar *)"totalCount"), "%d", &(epg->totalCount));
+	sscanf ((char *)xmlGetProp(nodeTab, (xmlChar *)"numOfChannels"), "%d", &(epg->numOfChannels));
+
+	epg->asOf = gmyth_util_string_to_time_val ((char *)xmlGetProp(nodeTab, (xmlChar *)"asOf"));
+	epg->startTime = gmyth_util_string_to_time_val ((char *)xmlGetProp(nodeTab, (xmlChar *)"startTime"));
+	epg->endTime = gmyth_util_string_to_time_val ((char *)xmlGetProp(nodeTab, (xmlChar *)"endTime"));
+
+	sscanf ((char *)xmlGetProp(nodeTab, (xmlChar *)"details"), "%d", &(epg->details));
+
+}
+
+void getEpg (xmlDocPtr doc)
+{
+    xmlXPathObjectPtr result;
+    xmlNodeSetPtr nodeset;
+    xmlChar *keyword;
+	GMythEpg epg;
+
+    int i = 0;
+    result = getXPath((xmlChar *)"/*", doc);
+
+
+    if (result) 
+    {
+        nodeset = result->nodesetval;
+        for (i=0; i < nodeset->nodeNr; i++) 
+        {
+            keyword = nodeset->nodeTab[i]->name;
+
+			if (g_ascii_strcasecmp(keyword, "ProgramGuide") == 0)
+				get_ProgramGuide_Properties(nodeset->nodeTab[i], &epg);
+// 
+// 			else if g_ascii_strcasecmp(keyword, "Channel")
+// 				get_Channel_Properties(nodeTab[i]);
+// 
+// 			else if g_ascii_strcasecmp(keyword, "Program")
+// 				get_Program_Properties(nodeTab[i]);
+
+            g_printf("keyword: %s\n", keyword);
+        }
+
+        xmlXPathFreeObject (result);
+    }	
+}
+
 /* Aux functions got from libcurl */
 void *myrealloc(void *ptr, size_t size)
 {
@@ -82,16 +179,16 @@
 {
     va_list args;
     va_start(args, command);
-    char* s = NULL;
+    gchar* s = NULL;
 
     g_string_append(command, "?");
 
     /* Sintax: var, value */
-    while ( (s = va_arg(args, char *)) != NULL ) 
+    while ( (s = va_arg(args, gchar *)) != NULL ) 
     {
         g_string_append(command, s);
         g_string_append(command, "=");
-        s = va_arg(args, char *);
+        s = va_arg(args, gchar *);
         g_string_append(command, s);
         g_string_append(command, "&");
     }
@@ -110,12 +207,12 @@
 GString 
 *gmyth_http_request (GMythBackendInfo *backend_info, GString *command)
 {
-
+	
     LIBXML_TEST_VERSION
 
     size_t size = strlen(backend_info->hostname) + strlen(command->str) + 13;
-    char *URL = malloc(sizeof(char)*size);
-    snprintf(URL, size+1, "http://%s:6544/%s", backend_info->hostname, command->str);     
+    gchar *URL = malloc(sizeof(gchar)*size);
+    g_snprintf(URL, size+1, "http://%s:6544/%s", backend_info->hostname, command->str);     
 
     CURL *curl_handle;
     
@@ -152,6 +249,8 @@
 
     if(chunk.memory)
     {
+        xmlDocPtr doc = XMLParse(chunk.memory, strlen(chunk.memory));
+        getEpg(doc);
         response = g_string_new(chunk.memory);
         free(chunk.memory);
     }
diff -r a84d2982ff01 -r 01d60f80fa52 gmyth/src/gmyth_http.h
--- a/gmyth/src/gmyth_http.h	Wed Jan 24 21:03:05 2007 +0000
+++ b/gmyth/src/gmyth_http.h	Wed Jan 24 21:52:23 2007 +0000
@@ -35,8 +35,11 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdarg.h>
+#include <glib.h>
+#include <glib/gprintf.h>
 
 #include "gmyth_backendinfo.h"
+#include "gmyth_util.h"
 
 #include <curl/curl.h>
 #include <curl/types.h>
@@ -49,7 +52,10 @@
 #define TYPE_TEXT_XML 0
 #define TYPE_FORM_URLENCODED 1
 
-typedef struct _GMythPacket     GMythPacket;       
+typedef struct _GMythPacket     GMythPacket;
+typedef struct _GMythProgram    GMythProgram;
+typedef struct _GMythChannel	GMythChannel;
+typedef struct _GMythEpg		GMythEpg;
 
 struct _GMythPacket
 {
@@ -57,6 +63,43 @@
     int    type;
 };
 
+struct _GMythProgram
+{
+    GString* title;
+	GString* subtitle;
+	GString* catType;
+    GString* category;
+	gint	repeat;
+	GTimeVal* startTime;
+	GTimeVal* endTime;
+};
+
+//gmyth_util_string_to_time_val ( const gchar* time_str )
+
+struct _GMythChannel
+{
+	GString* channelName;
+	GString* chanNum;
+	gint	chanId;
+	gint	callSign;
+	GSList	programList;
+};
+
+struct _GMythEpg
+{
+	gint startChanId;
+	gint endChanId;
+	GString* version;
+	gint protoVer;
+	gint totalCount;
+	gint numOfChannels;
+	GTimeVal* asOf;
+	GTimeVal* startTime;
+	GTimeVal* endTime;
+	gint details;
+	GSList channelList;
+};
+
 GString* gmyth_http_create_command(GString *command, ...);
 GString *gmyth_http_request (GMythBackendInfo *backend_info, GString *command);
 
diff -r a84d2982ff01 -r 01d60f80fa52 gmyth/tests/http.c
--- a/gmyth/tests/http.c	Wed Jan 24 21:03:05 2007 +0000
+++ b/gmyth/tests/http.c	Wed Jan 24 21:52:23 2007 +0000
@@ -1,12 +1,13 @@
 #include <glib-object.h>
 #include "gmyth.h"
+#include <glib.h>
 
 int
 main (int args, const char **argv)
 {
     GMythBackendInfo *backend_info;
-    g_type_init ();
-    g_thread_init (NULL);
+    g_type_init();
+    //g_thread_init(NULL);
 
     backend_info = gmyth_backend_info_new ();