[svn r588] Added functions to retrieve epg channel icons trunk
authormelunko
Fri Apr 20 21:26:38 2007 +0100 (2007-04-20)
branchtrunk
changeset 583a893a13572cc
parent 582 27872a83abaa
child 584 170e18a7ca71
[svn r588] Added functions to retrieve epg channel icons
gmyth/src/gmyth_common.c
gmyth/src/gmyth_common.h
gmyth/src/gmyth_epg.c
gmyth/src/gmyth_epg.h
gmyth/tests/compile_test_epg
gmyth/tests/gmyth_test_epg.c
     1.1 --- a/gmyth/src/gmyth_common.c	Fri Apr 20 20:29:46 2007 +0100
     1.2 +++ b/gmyth/src/gmyth_common.c	Fri Apr 20 21:26:38 2007 +0100
     1.3 @@ -69,6 +69,23 @@
     1.4      g_list_free (list);
     1.5  }
     1.6  
     1.7 +void
     1.8 +gmyth_channel_info_free (GMythChannelInfo *channel)
     1.9 +{
    1.10 +    g_return_if_fail (channel != NULL);
    1.11 +
    1.12 +    if (channel->channel_num)
    1.13 +        g_string_free (channel->channel_num, TRUE);
    1.14 +
    1.15 +    if (channel->channel_name)
    1.16 +        g_string_free (channel->channel_name, TRUE);
    1.17 +
    1.18 +    if (channel->channel_icon)
    1.19 +        g_string_free (channel->channel_icon, TRUE);
    1.20 +
    1.21 +    g_free (channel);
    1.22 +}
    1.23 +
    1.24  /**
    1.25   * Prints the channel info to the standard output. The gmyth debug must be enabled.
    1.26   * @param channel_info the GMythChannelInfo instance
    1.27 @@ -114,7 +131,9 @@
    1.28  free_channel_data (gpointer data, gpointer user_data)
    1.29  {
    1.30      /* Frees the GMythChannelInfo structure */
    1.31 -    g_free(data);
    1.32 +    GMythChannelInfo *channel = (GMythChannelInfo*) user_data;
    1.33 +
    1.34 +    gmyth_channel_info_free (channel);
    1.35  }
    1.36  
    1.37  static void
     2.1 --- a/gmyth/src/gmyth_common.h	Fri Apr 20 20:29:46 2007 +0100
     2.2 +++ b/gmyth/src/gmyth_common.h	Fri Apr 20 21:26:38 2007 +0100
     2.3 @@ -44,16 +44,23 @@
     2.4  	/** The channel ID in backend database */
     2.5  	gint channel_ID;
     2.6  	
     2.7 +        /** The channel number */
     2.8  	GString* channel_num; 
     2.9  	
    2.10  	/** The channel name in backend database */
    2.11  	GString *channel_name;
    2.12  
    2.13 +        /** The channel icon path in the backend database */
    2.14 +        GString *channel_icon;
    2.15 +
    2.16  } GMythChannelInfo;
    2.17  
    2.18  void gmyth_free_channel_list(GList *list);
    2.19  void gmyth_free_program_list(GList *list);
    2.20  
    2.21 +void gmyth_channel_info_free (GMythChannelInfo *channel_info);
    2.22 +
    2.23 +
    2.24  void gmyth_channel_info_print (GMythChannelInfo *channel_info);
    2.25  void gmyth_program_info_print (GMythProgramInfo *program_info);
    2.26  
     3.1 --- a/gmyth/src/gmyth_epg.c	Fri Apr 20 20:29:46 2007 +0100
     3.2 +++ b/gmyth/src/gmyth_epg.c	Fri Apr 20 21:26:38 2007 +0100
     3.3 @@ -38,6 +38,7 @@
     3.4  #include "gmyth_epg.h"
     3.5  #include "gmyth_programinfo.h"
     3.6  #include "gmyth_util.h"
     3.7 +#include "gmyth_file_transfer.h"
     3.8  #include "gmyth_debug.h"
     3.9  
    3.10  static void gmyth_epg_class_init          (GMythEPGClass *klass);
    3.11 @@ -106,7 +107,7 @@
    3.12  gboolean
    3.13  gmyth_epg_connect (GMythEPG *gmyth_epg, GMythBackendInfo *backend_info)
    3.14  {
    3.15 -    assert(gmyth_epg);
    3.16 +    g_return_val_if_fail (gmyth_epg != NULL, FALSE);
    3.17  
    3.18      if (gmyth_epg->sqlquery == NULL) {
    3.19  	gmyth_debug ("[%s] Creating gmyth_query", __FUNCTION__);
    3.20 @@ -118,6 +119,9 @@
    3.21          return FALSE;
    3.22      }
    3.23  
    3.24 +    gmyth_epg->backend_info = backend_info;
    3.25 +    g_object_ref (backend_info);
    3.26 +
    3.27      return TRUE;	
    3.28  }
    3.29  
    3.30 @@ -129,12 +133,17 @@
    3.31  gboolean
    3.32  gmyth_epg_disconnect (GMythEPG *gmyth_epg)
    3.33  {
    3.34 -    assert(gmyth_epg);
    3.35 +    g_return_val_if_fail (gmyth_epg != NULL, FALSE);
    3.36  
    3.37 -    if (gmyth_epg->sqlquery	!= NULL) {	
    3.38 +    if (gmyth_epg->sqlquery != NULL) {	
    3.39          g_object_unref (gmyth_epg->sqlquery);
    3.40          gmyth_epg->sqlquery = NULL;
    3.41      }
    3.42 +
    3.43 +    if (gmyth_epg->backend_info != NULL) {
    3.44 +        g_object_unref (gmyth_epg->backend_info);
    3.45 +        gmyth_epg->backend_info = NULL;
    3.46 +    }
    3.47  	
    3.48      return TRUE;
    3.49  }
    3.50 @@ -150,10 +159,10 @@
    3.51  {
    3.52      MYSQL_RES *msql_res;
    3.53  
    3.54 -    assert(gmyth_epg);
    3.55 +    g_return_val_if_fail (gmyth_epg != NULL, -1);
    3.56  
    3.57      msql_res = gmyth_query_process_statement (gmyth_epg->sqlquery, 
    3.58 -    		"SELECT chanid, channum, name FROM channel;");
    3.59 +    		"SELECT chanid, channum, name, icon FROM channel;");
    3.60  
    3.61      (*glist_ptr) = NULL;
    3.62  	
    3.63 @@ -170,6 +179,7 @@
    3.64              channel_info->channel_ID = g_ascii_strtoull (row[0], NULL, 10);
    3.65              channel_info->channel_num = g_string_new (row[1]);
    3.66              channel_info->channel_name = g_string_new (row[2]);
    3.67 +            channel_info->channel_icon = g_string_new (row[3]);
    3.68  #ifdef GMYTH_USE_DEBUG       
    3.69              gmyth_channel_info_print(channel_info);
    3.70  #endif            
    3.71 @@ -181,6 +191,41 @@
    3.72      return (!(*glist_ptr)) ?  0 : g_list_length (*glist_ptr);
    3.73  }
    3.74  
    3.75 +GMythChannelInfo*
    3.76 +gmyth_epg_get_channel_info (GMythEPG *gmyth_epg, gint channel_id)
    3.77 +{
    3.78 +    GMythChannelInfo *channel_info = NULL;
    3.79 +    MYSQL_RES *msql_res;
    3.80 +    gchar* query_str;
    3.81 +
    3.82 +    g_return_val_if_fail (gmyth_epg != NULL, -1);
    3.83 +
    3.84 +    query_str = g_strdup_printf ("SELECT channum, name, icon FROM channel WHERE chanid=%d;", channel_id);
    3.85 +    msql_res = gmyth_query_process_statement (gmyth_epg->sqlquery, query_str);
    3.86 +
    3.87 +    if (msql_res == NULL) {
    3.88 +        gmyth_debug ("[%s] msql query returned NULL MYSQL_RES", __FUNCTION__);
    3.89 +	return NULL;
    3.90 +    } else {
    3.91 +        MYSQL_ROW row;
    3.92 +
    3.93 +        if ((row = mysql_fetch_row (msql_res)) != NULL) {
    3.94 +
    3.95 +            channel_info = g_new0(GMythChannelInfo, 1);
    3.96 +            channel_info->channel_ID = channel_id;
    3.97 +            channel_info->channel_num = g_string_new (row[0]);
    3.98 +            channel_info->channel_name = g_string_new (row[1]);
    3.99 +            channel_info->channel_icon = g_string_new (row[2]);
   3.100 +#ifdef GMYTH_USE_DEBUG       
   3.101 +            gmyth_channel_info_print(channel_info);
   3.102 +#endif            
   3.103 +    	}
   3.104 +    }
   3.105 +    mysql_free_result (msql_res);
   3.106 +    
   3.107 +    return channel_info;
   3.108 +}
   3.109 +
   3.110  /** 
   3.111   * Retrieves the available list of channels from the backend Mysql database.
   3.112   * 
   3.113 @@ -303,3 +348,68 @@
   3.114  
   3.115      return TRUE;
   3.116  }
   3.117 +
   3.118 +gboolean 
   3.119 +gmyth_epg_channel_has_icon (GMythEPG *gmyth_epg, GMythChannelInfo *channel_info)
   3.120 +{
   3.121 +    gboolean res = FALSE;
   3.122 +
   3.123 +    g_return_val_if_fail (gmyth_epg != NULL, FALSE);
   3.124 +    g_return_val_if_fail (channel_info != NULL, FALSE);
   3.125 +
   3.126 +    if (channel_info->channel_icon != NULL) {
   3.127 +        res = gmyth_util_file_exists (gmyth_epg->backend_info, channel_info->channel_icon->str);
   3.128 +    }
   3.129 +
   3.130 +    return res;
   3.131 +
   3.132 +}
   3.133 +
   3.134 +/**
   3.135 + * 
   3.136 + * @param data the data pointer to be filled with icon binary data. It must be freed by the calling function.
   3.137 + * @return TRUE if success, FALSE if any error happens.
   3.138 + */
   3.139 +gboolean
   3.140 +gmyth_epg_channel_get_icon (GMythEPG *gmyth_epg, GMythChannelInfo *channel_info, guint8 **data, guint *length)
   3.141 +{
   3.142 +    gboolean res = FALSE;
   3.143 +
   3.144 +    g_return_val_if_fail (gmyth_epg != NULL, FALSE);
   3.145 +    g_return_val_if_fail (channel_info != NULL, FALSE);
   3.146 +
   3.147 +    if (gmyth_epg_channel_has_icon (gmyth_epg, channel_info)) {
   3.148 +        GMythFileTransfer *transfer = gmyth_file_transfer_new (gmyth_epg->backend_info);
   3.149 +        GMythFileReadResult gmyth_res;
   3.150 +        GByteArray *icon_data;
   3.151 +        guint64 icon_length = 0;
   3.152 +
   3.153 +        res = gmyth_file_transfer_open (transfer, channel_info->channel_icon->str);
   3.154 +        if (!res) {
   3.155 +            gmyth_debug ("Channel icon could not be opened");
   3.156 +            return FALSE;
   3.157 +        }
   3.158 +
   3.159 +        icon_length = gmyth_file_transfer_get_filesize (transfer);
   3.160 +        if (icon_length <= 0) {
   3.161 +            gmyth_debug ("Channel icon file size is zero or negative");
   3.162 +            return FALSE;
   3.163 +        }
   3.164 +
   3.165 +        icon_data = g_byte_array_new ();
   3.166 +        gmyth_res = gmyth_file_transfer_read (transfer, icon_data, icon_length, FALSE);
   3.167 +        if (gmyth_res == GMYTH_FILE_READ_EOF) {
   3.168 +            *length = icon_length;
   3.169 +            *data = icon_data->data;
   3.170 +            g_byte_array_free (icon_data, FALSE);
   3.171 +            res = TRUE;
   3.172 +        } else {
   3.173 +            *length = 0;
   3.174 +            *data = NULL;
   3.175 +            g_byte_array_free (icon_data, TRUE);
   3.176 +        }
   3.177 +    }
   3.178 +
   3.179 +    return res;
   3.180 +}
   3.181 +
     4.1 --- a/gmyth/src/gmyth_epg.h	Fri Apr 20 20:29:46 2007 +0100
     4.2 +++ b/gmyth/src/gmyth_epg.h	Fri Apr 20 21:26:38 2007 +0100
     4.3 @@ -58,7 +58,8 @@
     4.4  {
     4.5      GObject parent;
     4.6  
     4.7 -	GMythQuery *sqlquery;
     4.8 +    GMythQuery *sqlquery;
     4.9 +    GMythBackendInfo *backend_info;
    4.10  };
    4.11  
    4.12  GType          gmyth_epg_get_type (void);
    4.13 @@ -72,4 +73,11 @@
    4.14  gint gmyth_epg_get_program_list (GMythEPG *gmyth_epg, GList **proglist,
    4.15  		const gint chanNum, GTimeVal *starttime, GTimeVal *endtime);
    4.16  
    4.17 +GMythChannelInfo* gmyth_epg_get_channel_info (GMythEPG *gmyth_epg, gint channel_id);
    4.18 +
    4.19 +
    4.20 +gboolean gmyth_epg_channel_has_icon (GMythEPG *gmyth_epg, GMythChannelInfo *channel);
    4.21 +gboolean gmyth_epg_channel_get_icon (GMythEPG *gmyth_epg, GMythChannelInfo *channel, guint8 **data, guint *length);
    4.22 +
    4.23 +
    4.24  #endif /*GMYTH_EPG_H_*/
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/gmyth/tests/compile_test_epg	Fri Apr 20 21:26:38 2007 +0100
     5.3 @@ -0,0 +1,1 @@
     5.4 +gcc -o gmyth_test_epg gmyth_test_epg.c `pkg-config --cflags --libs gmyth glib-2.0`
     6.1 --- a/gmyth/tests/gmyth_test_epg.c	Fri Apr 20 20:29:46 2007 +0100
     6.2 +++ b/gmyth/tests/gmyth_test_epg.c	Fri Apr 20 21:26:38 2007 +0100
     6.3 @@ -41,6 +41,53 @@
     6.4      g_object_unref (epg);
     6.5  }
     6.6  
     6.7 +static gboolean
     6.8 +test_epg_get_channel_icon (GMythBackendInfo *backend_info)
     6.9 +{
    6.10 +    GMythEPG *epg = gmyth_epg_new ();
    6.11 +    GList *clist;
    6.12 +    gint i, length;
    6.13 +
    6.14 +    if (!gmyth_epg_connect (epg, backend_info)) {
    6.15 +        return FALSE;
    6.16 +    }
    6.17 +
    6.18 +    length = gmyth_epg_get_channel_list (epg, &clist);
    6.19 +    g_print ("==== %d channels found in the EPG ====\n", length);
    6.20 +    for (i=0; i<length; i++) {
    6.21 +        GMythChannelInfo *channel_info = (GMythChannelInfo*) g_list_nth_data (clist, i);
    6.22 +        
    6.23 +        if (gmyth_epg_channel_has_icon (epg, channel_info)) {
    6.24 +            gchar *icon_name = g_strdup_printf ("%s.jpg", channel_info->channel_name->str);
    6.25 +            guint8 *icon_data = NULL;
    6.26 +            guint icon_length;
    6.27 +
    6.28 +            g_print ("Channel %s has icon %s\n", channel_info->channel_name->str, channel_info->channel_icon->str);
    6.29 +
    6.30 +            if (gmyth_epg_channel_get_icon (epg, channel_info, &icon_data, &icon_length)) {
    6.31 +                FILE *outfile = fopen (icon_name, "w+");
    6.32 +                if (fwrite (icon_data, icon_length, 1, outfile) == icon_length)
    6.33 +                    g_print ("\tIcon saved as %s", icon_name);
    6.34 +                else
    6.35 +                    g_print ("\tError while downloading the file or writing it");
    6.36 +
    6.37 +                g_free (icon_data);
    6.38 +            }
    6.39 +            g_free (icon_name);
    6.40 +            
    6.41 +        } else {
    6.42 +            g_print ("Channel %s does not have icon\n", channel_info->channel_name->str);
    6.43 +        }
    6.44 +        gmyth_channel_info_print(channel_info);
    6.45 +    }
    6.46 +
    6.47 +    g_list_free (clist);
    6.48 +    gmyth_epg_disconnect (epg);
    6.49 +    g_object_unref (epg);
    6.50 +
    6.51 +    return TRUE;
    6.52 +}
    6.53 +
    6.54  int
    6.55  main (int args, const char **argv)
    6.56  {
    6.57 @@ -50,10 +97,16 @@
    6.58      g_type_init ();
    6.59      g_thread_init (NULL);
    6.60  
    6.61 +    if (args < 2) {
    6.62 +        g_printf ("Type %s myth://hostname:port/?mythconverg\n", argv[0]);
    6.63 +        return -1;
    6.64 +    }
    6.65 +
    6.66      backend_info = gmyth_backend_info_new_with_uri (argv[1]);
    6.67  
    6.68      test_epg_connection (backend_info);
    6.69      test_epg_get_channels (backend_info);
    6.70 +    test_epg_get_channel_icon (backend_info);
    6.71  
    6.72      return 0;
    6.73  }