# HG changeset patch # User melunko # Date 1177100798 -3600 # Node ID a893a13572cc55ded6833b3fce186fed9b5122d9 # Parent 27872a83abaa51e454de3fde26c8578ccd8526af [svn r588] Added functions to retrieve epg channel icons diff -r 27872a83abaa -r a893a13572cc gmyth/src/gmyth_common.c --- a/gmyth/src/gmyth_common.c Fri Apr 20 20:29:46 2007 +0100 +++ b/gmyth/src/gmyth_common.c Fri Apr 20 21:26:38 2007 +0100 @@ -69,6 +69,23 @@ g_list_free (list); } +void +gmyth_channel_info_free (GMythChannelInfo *channel) +{ + g_return_if_fail (channel != NULL); + + if (channel->channel_num) + g_string_free (channel->channel_num, TRUE); + + if (channel->channel_name) + g_string_free (channel->channel_name, TRUE); + + if (channel->channel_icon) + g_string_free (channel->channel_icon, TRUE); + + g_free (channel); +} + /** * Prints the channel info to the standard output. The gmyth debug must be enabled. * @param channel_info the GMythChannelInfo instance @@ -114,7 +131,9 @@ free_channel_data (gpointer data, gpointer user_data) { /* Frees the GMythChannelInfo structure */ - g_free(data); + GMythChannelInfo *channel = (GMythChannelInfo*) user_data; + + gmyth_channel_info_free (channel); } static void diff -r 27872a83abaa -r a893a13572cc gmyth/src/gmyth_common.h --- a/gmyth/src/gmyth_common.h Fri Apr 20 20:29:46 2007 +0100 +++ b/gmyth/src/gmyth_common.h Fri Apr 20 21:26:38 2007 +0100 @@ -44,16 +44,23 @@ /** The channel ID in backend database */ gint channel_ID; + /** The channel number */ GString* channel_num; /** The channel name in backend database */ GString *channel_name; + /** The channel icon path in the backend database */ + GString *channel_icon; + } GMythChannelInfo; void gmyth_free_channel_list(GList *list); void gmyth_free_program_list(GList *list); +void gmyth_channel_info_free (GMythChannelInfo *channel_info); + + void gmyth_channel_info_print (GMythChannelInfo *channel_info); void gmyth_program_info_print (GMythProgramInfo *program_info); diff -r 27872a83abaa -r a893a13572cc gmyth/src/gmyth_epg.c --- a/gmyth/src/gmyth_epg.c Fri Apr 20 20:29:46 2007 +0100 +++ b/gmyth/src/gmyth_epg.c Fri Apr 20 21:26:38 2007 +0100 @@ -38,6 +38,7 @@ #include "gmyth_epg.h" #include "gmyth_programinfo.h" #include "gmyth_util.h" +#include "gmyth_file_transfer.h" #include "gmyth_debug.h" static void gmyth_epg_class_init (GMythEPGClass *klass); @@ -106,7 +107,7 @@ gboolean gmyth_epg_connect (GMythEPG *gmyth_epg, GMythBackendInfo *backend_info) { - assert(gmyth_epg); + g_return_val_if_fail (gmyth_epg != NULL, FALSE); if (gmyth_epg->sqlquery == NULL) { gmyth_debug ("[%s] Creating gmyth_query", __FUNCTION__); @@ -118,6 +119,9 @@ return FALSE; } + gmyth_epg->backend_info = backend_info; + g_object_ref (backend_info); + return TRUE; } @@ -129,12 +133,17 @@ gboolean gmyth_epg_disconnect (GMythEPG *gmyth_epg) { - assert(gmyth_epg); + g_return_val_if_fail (gmyth_epg != NULL, FALSE); - if (gmyth_epg->sqlquery != NULL) { + if (gmyth_epg->sqlquery != NULL) { g_object_unref (gmyth_epg->sqlquery); gmyth_epg->sqlquery = NULL; } + + if (gmyth_epg->backend_info != NULL) { + g_object_unref (gmyth_epg->backend_info); + gmyth_epg->backend_info = NULL; + } return TRUE; } @@ -150,10 +159,10 @@ { MYSQL_RES *msql_res; - assert(gmyth_epg); + g_return_val_if_fail (gmyth_epg != NULL, -1); msql_res = gmyth_query_process_statement (gmyth_epg->sqlquery, - "SELECT chanid, channum, name FROM channel;"); + "SELECT chanid, channum, name, icon FROM channel;"); (*glist_ptr) = NULL; @@ -170,6 +179,7 @@ channel_info->channel_ID = g_ascii_strtoull (row[0], NULL, 10); channel_info->channel_num = g_string_new (row[1]); channel_info->channel_name = g_string_new (row[2]); + channel_info->channel_icon = g_string_new (row[3]); #ifdef GMYTH_USE_DEBUG gmyth_channel_info_print(channel_info); #endif @@ -181,6 +191,41 @@ return (!(*glist_ptr)) ? 0 : g_list_length (*glist_ptr); } +GMythChannelInfo* +gmyth_epg_get_channel_info (GMythEPG *gmyth_epg, gint channel_id) +{ + GMythChannelInfo *channel_info = NULL; + MYSQL_RES *msql_res; + gchar* query_str; + + g_return_val_if_fail (gmyth_epg != NULL, -1); + + query_str = g_strdup_printf ("SELECT channum, name, icon FROM channel WHERE chanid=%d;", channel_id); + msql_res = gmyth_query_process_statement (gmyth_epg->sqlquery, query_str); + + if (msql_res == NULL) { + gmyth_debug ("[%s] msql query returned NULL MYSQL_RES", __FUNCTION__); + return NULL; + } else { + MYSQL_ROW row; + + if ((row = mysql_fetch_row (msql_res)) != NULL) { + + channel_info = g_new0(GMythChannelInfo, 1); + channel_info->channel_ID = channel_id; + channel_info->channel_num = g_string_new (row[0]); + channel_info->channel_name = g_string_new (row[1]); + channel_info->channel_icon = g_string_new (row[2]); +#ifdef GMYTH_USE_DEBUG + gmyth_channel_info_print(channel_info); +#endif + } + } + mysql_free_result (msql_res); + + return channel_info; +} + /** * Retrieves the available list of channels from the backend Mysql database. * @@ -303,3 +348,68 @@ return TRUE; } + +gboolean +gmyth_epg_channel_has_icon (GMythEPG *gmyth_epg, GMythChannelInfo *channel_info) +{ + gboolean res = FALSE; + + g_return_val_if_fail (gmyth_epg != NULL, FALSE); + g_return_val_if_fail (channel_info != NULL, FALSE); + + if (channel_info->channel_icon != NULL) { + res = gmyth_util_file_exists (gmyth_epg->backend_info, channel_info->channel_icon->str); + } + + return res; + +} + +/** + * + * @param data the data pointer to be filled with icon binary data. It must be freed by the calling function. + * @return TRUE if success, FALSE if any error happens. + */ +gboolean +gmyth_epg_channel_get_icon (GMythEPG *gmyth_epg, GMythChannelInfo *channel_info, guint8 **data, guint *length) +{ + gboolean res = FALSE; + + g_return_val_if_fail (gmyth_epg != NULL, FALSE); + g_return_val_if_fail (channel_info != NULL, FALSE); + + if (gmyth_epg_channel_has_icon (gmyth_epg, channel_info)) { + GMythFileTransfer *transfer = gmyth_file_transfer_new (gmyth_epg->backend_info); + GMythFileReadResult gmyth_res; + GByteArray *icon_data; + guint64 icon_length = 0; + + res = gmyth_file_transfer_open (transfer, channel_info->channel_icon->str); + if (!res) { + gmyth_debug ("Channel icon could not be opened"); + return FALSE; + } + + icon_length = gmyth_file_transfer_get_filesize (transfer); + if (icon_length <= 0) { + gmyth_debug ("Channel icon file size is zero or negative"); + return FALSE; + } + + icon_data = g_byte_array_new (); + gmyth_res = gmyth_file_transfer_read (transfer, icon_data, icon_length, FALSE); + if (gmyth_res == GMYTH_FILE_READ_EOF) { + *length = icon_length; + *data = icon_data->data; + g_byte_array_free (icon_data, FALSE); + res = TRUE; + } else { + *length = 0; + *data = NULL; + g_byte_array_free (icon_data, TRUE); + } + } + + return res; +} + diff -r 27872a83abaa -r a893a13572cc gmyth/src/gmyth_epg.h --- a/gmyth/src/gmyth_epg.h Fri Apr 20 20:29:46 2007 +0100 +++ b/gmyth/src/gmyth_epg.h Fri Apr 20 21:26:38 2007 +0100 @@ -58,7 +58,8 @@ { GObject parent; - GMythQuery *sqlquery; + GMythQuery *sqlquery; + GMythBackendInfo *backend_info; }; GType gmyth_epg_get_type (void); @@ -72,4 +73,11 @@ gint gmyth_epg_get_program_list (GMythEPG *gmyth_epg, GList **proglist, const gint chanNum, GTimeVal *starttime, GTimeVal *endtime); +GMythChannelInfo* gmyth_epg_get_channel_info (GMythEPG *gmyth_epg, gint channel_id); + + +gboolean gmyth_epg_channel_has_icon (GMythEPG *gmyth_epg, GMythChannelInfo *channel); +gboolean gmyth_epg_channel_get_icon (GMythEPG *gmyth_epg, GMythChannelInfo *channel, guint8 **data, guint *length); + + #endif /*GMYTH_EPG_H_*/ diff -r 27872a83abaa -r a893a13572cc gmyth/tests/compile_test_epg --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gmyth/tests/compile_test_epg Fri Apr 20 21:26:38 2007 +0100 @@ -0,0 +1,1 @@ +gcc -o gmyth_test_epg gmyth_test_epg.c `pkg-config --cflags --libs gmyth glib-2.0` diff -r 27872a83abaa -r a893a13572cc gmyth/tests/gmyth_test_epg.c --- a/gmyth/tests/gmyth_test_epg.c Fri Apr 20 20:29:46 2007 +0100 +++ b/gmyth/tests/gmyth_test_epg.c Fri Apr 20 21:26:38 2007 +0100 @@ -41,6 +41,53 @@ g_object_unref (epg); } +static gboolean +test_epg_get_channel_icon (GMythBackendInfo *backend_info) +{ + GMythEPG *epg = gmyth_epg_new (); + GList *clist; + gint i, length; + + if (!gmyth_epg_connect (epg, backend_info)) { + return FALSE; + } + + length = gmyth_epg_get_channel_list (epg, &clist); + g_print ("==== %d channels found in the EPG ====\n", length); + for (i=0; ichannel_name->str); + guint8 *icon_data = NULL; + guint icon_length; + + g_print ("Channel %s has icon %s\n", channel_info->channel_name->str, channel_info->channel_icon->str); + + if (gmyth_epg_channel_get_icon (epg, channel_info, &icon_data, &icon_length)) { + FILE *outfile = fopen (icon_name, "w+"); + if (fwrite (icon_data, icon_length, 1, outfile) == icon_length) + g_print ("\tIcon saved as %s", icon_name); + else + g_print ("\tError while downloading the file or writing it"); + + g_free (icon_data); + } + g_free (icon_name); + + } else { + g_print ("Channel %s does not have icon\n", channel_info->channel_name->str); + } + gmyth_channel_info_print(channel_info); + } + + g_list_free (clist); + gmyth_epg_disconnect (epg); + g_object_unref (epg); + + return TRUE; +} + int main (int args, const char **argv) { @@ -50,10 +97,16 @@ g_type_init (); g_thread_init (NULL); + if (args < 2) { + g_printf ("Type %s myth://hostname:port/?mythconverg\n", argv[0]); + return -1; + } + backend_info = gmyth_backend_info_new_with_uri (argv[1]); test_epg_connection (backend_info); test_epg_get_channels (backend_info); + test_epg_get_channel_icon (backend_info); return 0; }