Back to index

gcompris  8.2.2
gc_net.c
Go to the documentation of this file.
00001 /* gcompris - gameutil_net.c
00002  *
00003  * Time-stamp: <2006/07/10 01:24:04 bruno>
00004  *
00005  * Copyright (C) 2006 Bruno Coudoin
00006  *
00007  *   This program is free software; you can redistribute it and/or modify
00008  *   it under the terms of the GNU General Public License as published by
00009  *   the Free Software Foundation; either version 2 of the License, or
00010  *   (at your option) any later version.
00011  *
00012  *   This program is distributed in the hope that it will be useful,
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *   GNU General Public License for more details.
00016  *
00017  *   You should have received a copy of the GNU General Public License
00018  *   along with this program; if not, write to the Free Software
00019  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  */
00021 
00022 #include "gc_net.h"
00023 #include "gc_core.h"
00024 
00025 #ifdef USE_GNET
00026 #include <gnet.h>
00027 #endif
00028 
00029 /* FIXME: Should not be needed, a bug in gnet header ? */
00030 gboolean         gnet_http_get                     (const gchar      *url,
00031                                                     gchar           **buffer,
00032                                                     gsize            *length,
00033                                                     guint            *response);
00034 
00035 #include <string.h>
00036 
00037 #ifdef USE_GNET
00038 static GSList *server_content_list = NULL;
00039 #define       SUPPORT_OR_RETURN(rv)       {if(!gc_prop_get()->server) return rv;}
00040 #else
00041 #define       SUPPORT_OR_RETURN(rv)       { return rv; }
00042 #endif
00043 
00044 
00045 static inline int my_strcmp(gchar *a, gchar *b) { return strcmp( a, b); }
00046 
00050 void gc_net_init()
00051 {
00052   SUPPORT_OR_RETURN();
00053 
00054 #ifdef USE_GNET
00055   GcomprisProperties *properties = gc_prop_get();
00056   gchar *url;
00057   gchar *buf = NULL;
00058   gsize  buflen;
00059   guint  response;
00060 
00061   gnet_init();
00062 
00063   /*
00064    * Get the content.txt file at the root of the http server and store it in a glist
00065    * we then now exactly which files we have there
00066    * warning, do not use gc_net_get_url_from_file() since we are in fact buildind the list of file
00067    * for it.
00068    */
00069   url = g_strdup_printf("%s/%s", properties->server, "/content.txt");
00070 
00071   if(gnet_http_get(url, &buf, &buflen, &response) && response == 200)
00072     {
00073       char line[200];
00074       int i = 0;
00075       /* Parse each line of the buffer and save it in 'server_content_list' */
00076       while( i < buflen)
00077        {
00078          sscanf(buf+i, "%s", (char *)&line);
00079          server_content_list = g_slist_prepend(server_content_list, g_strdup(line));
00080          i+=strlen(line)+1;
00081        }
00082     }
00083   else
00084     {
00085       /* We did not get the content list, disable network now */
00086       g_free(properties->server);
00087       properties->server = NULL;
00088       g_warning("Failed to initialize networked GCompris because '%s' is not found", url);
00089     }
00090 
00091   g_free(buf);
00092   g_free(url);
00093 #endif
00094 }
00095 
00102 GdkPixbuf *gc_net_load_pixmap(const char *url)
00103 {
00104   if(!gc_net_is_url(url))
00105     return(gdk_pixbuf_new_from_file (url, NULL));
00106 
00107   SUPPORT_OR_RETURN(NULL);
00108 
00109 #ifdef USE_GNET
00110   gchar *buf = NULL;
00111   gsize  buflen;
00112   guint  response;
00113 
00114   g_warning("Loading image from url '%s'", url);
00115 
00116   if(gnet_http_get(url, &buf, &buflen, &response) && response == 200)
00117     {
00118       GdkPixbuf *pixmap=NULL;
00119       GdkPixbufLoader* loader;
00120       loader = gdk_pixbuf_loader_new();
00121       gdk_pixbuf_loader_write(loader, (guchar *)buf, buflen, NULL);
00122       g_free(buf);
00123       gdk_pixbuf_loader_close(loader, NULL);
00124       pixmap = gdk_pixbuf_loader_get_pixbuf(loader);
00125       if(!pixmap)
00126        g_warning("Loading image from url '%s' returned a null pointer", url);
00127 
00128       return(pixmap);
00129     }
00130 
00131   g_free(buf);
00132   return(NULL);
00133 
00134 #endif
00135 }
00136 
00143 xmlDocPtr gc_net_load_xml(const char *url)
00144 {
00145   if(!gc_net_is_url(url))
00146     return(xmlParseFile(url));
00147 
00148   SUPPORT_OR_RETURN(NULL);
00149 
00150 #ifdef USE_GNET
00151   gchar *buf = NULL;
00152   gsize  buflen;
00153   guint  response;
00154 
00155   g_warning("Loading xml file from url '%s'", url);
00156 
00157   if(gnet_http_get(url, &buf, &buflen, &response) && response == 200)
00158     {
00159       xmlDocPtr      doc = xmlParseMemory((const char *)buf, buflen);
00160       g_free(buf);
00161       if(!buf)
00162        g_warning("Loading xml file from url '%s' returned a null pointer", url);
00163 
00164       return(doc);
00165     }
00166 
00167   g_free(buf);
00168   return(NULL);
00169 
00170 #endif
00171 }
00172 
00178 gchar *
00179 gc_net_get_url_from_file(const gchar *format, ...)
00180 {
00181   SUPPORT_OR_RETURN(NULL);
00182 
00183 #ifdef USE_GNET
00184   GcomprisProperties *properties = gc_prop_get();
00185   gchar *file, *url;
00186   va_list args;
00187 
00188   va_start (args, format);
00189   file = g_strdup_vprintf (format, args);
00190   va_end (args);
00191 
00192   /* FIXME: In case the file does not starts with boards/, preprend it */
00193   {
00194     if(strncmp(file, "boards/", 7))
00195       {
00196        gchar *file2 = g_strconcat("boards/", file, NULL);
00197        g_free(file);
00198        file = file2;
00199       }
00200   }
00201 
00202   g_warning("gc_net_get_url_from_file '%s'", file);
00203   if(!g_slist_find_custom(server_content_list,(gconstpointer) file, (GCompareFunc) my_strcmp))
00204     {
00205       g_free(file);
00206       return NULL;
00207     }
00208   url = g_strconcat(properties->server, "/", file, NULL);
00209   g_free(file);
00210 
00211   g_warning("gc_net_get_url_from_file returns url '%s'", url);
00212   return url;
00213 #endif
00214 }
00215 
00221 gboolean
00222 gc_net_is_url(const gchar *url)
00223 {
00224   if( !url || strncmp(url, "http://", 7) )
00225     return FALSE;
00226 
00227   return TRUE;
00228 }
00229 
00238 GSList *gc_net_dir_read_name(const gchar* dir, const gchar *ext)
00239 {
00240   SUPPORT_OR_RETURN(NULL);
00241 
00242 #ifdef USE_GNET
00243   GSList *filelist = NULL;
00244   GSList *i = NULL;
00245 
00246   g_return_val_if_fail(dir!=NULL, NULL);
00247 
00248   for (i = server_content_list; i != NULL; i = g_slist_next (i))
00249     {
00250       if(strncmp(dir, (gchar *)i->data, strlen(dir)) == 0)
00251        if(ext == NULL ||
00252           g_str_has_suffix ((gchar *)i->data, ext))
00253          filelist = g_slist_prepend(filelist, i->data);
00254     }
00255 
00256   return(filelist);
00257 #endif
00258 }