gmyth/src/gmyth_epg.c
author renatofilho
Wed Jan 23 20:04:22 2008 +0000 (2008-01-23)
branchtrunk
changeset 890 6b565181036e
parent 886 781db4177ba2
child 895 4bdd0f922b68
permissions -rw-r--r--
[svn r896] fixed channel_id, program_id types
leo_sobral@1
     1
/**
leo_sobral@1
     2
 * GMyth Library
leo_sobral@1
     3
 *
leo_sobral@1
     4
 * @file gmyth/gmyth_epg.c
leo_sobral@1
     5
 * 
leo_sobral@1
     6
 * @brief <p> GMythEPG class provides access to the program and channel data
leo_sobral@1
     7
 * from the Electronic Program Guide (EPG) of the Mythtv backend.
leo_sobral@1
     8
 *
leo_sobral@1
     9
 * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
leo_sobral@1
    10
 * @author Leonardo Sobral Cunha <leonardo.cunha@indt.org.br>
renatofilho@750
    11
 * 
renatofilho@750
    12
 * This program is free software; you can redistribute it and/or modify
renatofilho@750
    13
 * it under the terms of the GNU Lesser General Public License as published by
renatofilho@750
    14
 * the Free Software Foundation; either version 2 of the License, or
renatofilho@750
    15
 * (at your option) any later version.
leo_sobral@1
    16
 *
renatofilho@750
    17
 * This program is distributed in the hope that it will be useful,
renatofilho@750
    18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
renatofilho@750
    19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
renatofilho@750
    20
 * GNU General Public License for more details.
renatofilho@750
    21
 *
renatofilho@750
    22
 * You should have received a copy of the GNU Lesser General Public License
renatofilho@750
    23
 * along with this program; if not, write to the Free Software
renatofilho@750
    24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
renatofilho@750
    25
 */
rosfran@698
    26
leo_sobral@213
    27
#ifdef HAVE_CONFIG_H
leo_sobral@213
    28
#include "config.h"
leo_sobral@213
    29
#endif
leo_sobral@1
    30
rosfran@240
    31
#include <mysql/mysql.h>
leo_sobral@1
    32
#include <stdlib.h>
leo_sobral@1
    33
#include <string.h>
leo_sobral@1
    34
#include <assert.h>
leo_sobral@1
    35
leo_sobral@1
    36
#include "gmyth_epg.h"
rosfran@291
    37
#include "gmyth_programinfo.h"
leo_sobral@1
    38
#include "gmyth_util.h"
melunko@583
    39
#include "gmyth_file_transfer.h"
renatofilho@131
    40
#include "gmyth_debug.h"
leo_sobral@1
    41
morphbr@767
    42
#define CONNECT_TIMEOUT 6
morphbr@766
    43
renatofilho@754
    44
static void     gmyth_epg_class_init(GMythEPGClass * klass);
renatofilho@754
    45
static void     gmyth_epg_init(GMythEPG * object);
leo_sobral@1
    46
renatofilho@754
    47
static void     gmyth_epg_dispose(GObject * object);
renatofilho@754
    48
static void     gmyth_epg_finalize(GObject * object);
leo_sobral@1
    49
renatofilho@750
    50
G_DEFINE_TYPE(GMythEPG, gmyth_epg, G_TYPE_OBJECT)
renatofilho@754
    51
    static void     gmyth_epg_class_init(GMythEPGClass * klass)
leo_sobral@1
    52
{
renatofilho@754
    53
    GObjectClass   *gobject_class = G_OBJECT_CLASS(klass);
rosfran@698
    54
renatofilho@754
    55
    gobject_class->dispose = gmyth_epg_dispose;
renatofilho@754
    56
    gobject_class->finalize = gmyth_epg_finalize;
leo_sobral@1
    57
}
leo_sobral@1
    58
leo_sobral@1
    59
static void
renatofilho@750
    60
gmyth_epg_init(GMythEPG * gmyth_epg)
leo_sobral@1
    61
{
melunko@117
    62
leo_sobral@1
    63
}
leo_sobral@1
    64
leo_sobral@1
    65
static void
renatofilho@750
    66
gmyth_epg_dispose(GObject * object)
leo_sobral@1
    67
{
renatofilho@754
    68
    GMythEPG       *gmyth_epg = GMYTH_EPG(object);
rosfran@698
    69
renatofilho@754
    70
    if (gmyth_epg->sqlquery != NULL) {
renatofilho@754
    71
        g_object_unref(gmyth_epg->sqlquery);
renatofilho@754
    72
        gmyth_epg->sqlquery = NULL;
renatofilho@754
    73
    }
melunko@412
    74
renatofilho@754
    75
    G_OBJECT_CLASS(gmyth_epg_parent_class)->dispose(object);
leo_sobral@1
    76
}
leo_sobral@1
    77
leo_sobral@1
    78
static void
renatofilho@750
    79
gmyth_epg_finalize(GObject * object)
leo_sobral@1
    80
{
renatofilho@754
    81
    g_signal_handlers_destroy(object);
leo_sobral@1
    82
renatofilho@754
    83
    G_OBJECT_CLASS(gmyth_epg_parent_class)->finalize(object);
leo_sobral@1
    84
}
leo_sobral@1
    85
leo_sobral@1
    86
/**
leo_sobral@1
    87
 * Creates a new instance of GMythEPG.
leo_sobral@1
    88
 * 
leo_sobral@1
    89
 * @return a new instance of GMythEPG.
leo_sobral@1
    90
 */
renatofilho@754
    91
GMythEPG       *
renatofilho@750
    92
gmyth_epg_new(void)
leo_sobral@1
    93
{
renatofilho@754
    94
    GMythEPG       *epg = GMYTH_EPG(g_object_new(GMYTH_EPG_TYPE, NULL));
leo_sobral@1
    95
renatofilho@754
    96
    return epg;
leo_sobral@1
    97
}
leo_sobral@1
    98
leo_sobral@1
    99
/** Connects to the Mysql database in the backend. The backend address
leo_sobral@1
   100
 * is loaded from the GMythSettings instance.
morphbr@766
   101
 *
leo_sobral@1
   102
 * @param gmyth_epg the GMythEPG instance to be connected.
leo_sobral@1
   103
 * @return true if connection was success, false if failed.
leo_sobral@1
   104
 */
leo_sobral@1
   105
gboolean
renatofilho@750
   106
gmyth_epg_connect(GMythEPG * gmyth_epg, GMythBackendInfo * backend_info)
leo_sobral@1
   107
{
renatofilho@754
   108
    g_return_val_if_fail(gmyth_epg != NULL, FALSE);
leo_sobral@1
   109
renatofilho@754
   110
    if (gmyth_epg->sqlquery == NULL) {
renatofilho@754
   111
        gmyth_debug("[%s] Creating gmyth_query", __FUNCTION__);
renatofilho@754
   112
        gmyth_epg->sqlquery = gmyth_query_new();
renatofilho@754
   113
    }
leo_sobral@1
   114
morphbr@766
   115
    if (!gmyth_query_connect_with_timeout(gmyth_epg->sqlquery,
morphbr@766
   116
                                          backend_info, CONNECT_TIMEOUT)) {
renatofilho@754
   117
        gmyth_debug("[%s] Error while connecting to db", __FUNCTION__);
renatofilho@754
   118
        return FALSE;
renatofilho@754
   119
    }
leo_sobral@1
   120
renatofilho@754
   121
    gmyth_epg->backend_info = backend_info;
renatofilho@754
   122
    g_object_ref(backend_info);
melunko@583
   123
renatofilho@754
   124
    return TRUE;
leo_sobral@1
   125
}
leo_sobral@1
   126
leo_sobral@1
   127
/** Disconnects from the Mysql database in the backend.
morphbr@766
   128
 *
leo_sobral@1
   129
 * @param gmyth_epg the GMythEPG instance to be disconnected
leo_sobral@1
   130
 * @return true if disconnection was success, false if failed.
leo_sobral@1
   131
 */
leo_sobral@1
   132
gboolean
renatofilho@750
   133
gmyth_epg_disconnect(GMythEPG * gmyth_epg)
leo_sobral@1
   134
{
renatofilho@754
   135
    g_return_val_if_fail(gmyth_epg != NULL, FALSE);
leo_sobral@1
   136
renatofilho@754
   137
    if (gmyth_epg->sqlquery != NULL) {
renatofilho@754
   138
        gmyth_query_disconnect(gmyth_epg->sqlquery);
renatofilho@754
   139
        g_object_unref(gmyth_epg->sqlquery);
renatofilho@754
   140
        gmyth_epg->sqlquery = NULL;
renatofilho@754
   141
    }
melunko@583
   142
renatofilho@754
   143
    if (gmyth_epg->backend_info != NULL) {
renatofilho@754
   144
        g_object_unref(gmyth_epg->backend_info);
renatofilho@754
   145
        gmyth_epg->backend_info = NULL;
renatofilho@754
   146
    }
rosfran@698
   147
renatofilho@754
   148
    return TRUE;
leo_sobral@1
   149
}
leo_sobral@1
   150
leo_sobral@1
   151
/** Retrieves the available list of channels from the backend Mysql database.
leo_sobral@1
   152
 * 
leo_sobral@1
   153
 * @param gmyth_epg the GMythEPG instance.
leo_sobral@446
   154
 * @param glist_ptr the GSList pointer to be filled with the loaded list address.
leo_sobral@1
   155
 * @return The amount of channels retrieved from database,  or -1 if error.
leo_sobral@1
   156
 */
leo_sobral@1
   157
gint
renatofilho@750
   158
gmyth_epg_get_channel_list(GMythEPG * gmyth_epg, GList ** glist_ptr)
leo_sobral@1
   159
{
renatofilho@754
   160
    MYSQL_RES      *msql_res;
leo_sobral@1
   161
renatofilho@754
   162
    g_return_val_if_fail(gmyth_epg != NULL, -1);
leo_sobral@1
   163
renatofilho@754
   164
    msql_res = gmyth_query_process_statement(gmyth_epg->sqlquery,
renatofilho@754
   165
                                             "SELECT chanid, channum, name, icon FROM channel;");
leo_sobral@1
   166
renatofilho@754
   167
    (*glist_ptr) = NULL;
rosfran@698
   168
renatofilho@754
   169
    if (msql_res == NULL) {
renatofilho@754
   170
        gmyth_debug("[%s] msql query returned NULL MYSQL_RES",
renatofilho@754
   171
                    __FUNCTION__);
renatofilho@754
   172
        return -1;
renatofilho@754
   173
    } else {
renatofilho@754
   174
        MYSQL_ROW       row;
renatofilho@754
   175
        GMythChannelInfo *channel_info;
leo_sobral@1
   176
renatofilho@754
   177
        while ((row = mysql_fetch_row(msql_res)) != NULL) {
leo_sobral@1
   178
renatofilho@754
   179
            channel_info = g_new0(GMythChannelInfo, 1);
renatofilho@754
   180
            channel_info->channel_ID =
renatofilho@754
   181
                (gint) g_ascii_strtoull(row[0], NULL, 10);
renatofilho@754
   182
            channel_info->channel_num = g_string_new(row[1]);
renatofilho@754
   183
            channel_info->channel_name = g_string_new(row[2]);
renatofilho@754
   184
            channel_info->channel_icon = g_string_new(row[3]);
rosfran@698
   185
#ifdef GMYTH_USE_DEBUG
renatofilho@754
   186
            gmyth_channel_info_print(channel_info);
rosfran@698
   187
#endif
renatofilho@754
   188
            (*glist_ptr) = g_list_append((*glist_ptr), channel_info);
renatofilho@754
   189
        }
renatofilho@754
   190
    }
renatofilho@754
   191
    mysql_free_result(msql_res);
rosfran@698
   192
renatofilho@754
   193
    return (!(*glist_ptr)) ? 0 : g_list_length(*glist_ptr);
leo_sobral@1
   194
}
leo_sobral@1
   195
rosfran@698
   196
GMythChannelInfo *
renatofilho@750
   197
gmyth_epg_get_channel_info(GMythEPG * gmyth_epg, gint channel_id)
melunko@583
   198
{
renatofilho@754
   199
    GMythChannelInfo *channel_info = NULL;
renatofilho@754
   200
    MYSQL_RES      *msql_res;
renatofilho@754
   201
    gchar          *query_str;
melunko@583
   202
renatofilho@754
   203
    g_return_val_if_fail(gmyth_epg != NULL, NULL);
melunko@583
   204
renatofilho@754
   205
    query_str =
renatofilho@754
   206
        g_strdup_printf
renatofilho@754
   207
        ("SELECT channum, name, icon FROM channel WHERE chanid=%d;",
renatofilho@754
   208
         channel_id);
renatofilho@754
   209
    msql_res =
renatofilho@754
   210
        gmyth_query_process_statement(gmyth_epg->sqlquery, query_str);
melunko@583
   211
renatofilho@754
   212
    if (msql_res == NULL) {
renatofilho@754
   213
        gmyth_debug("[%s] msql query returned NULL MYSQL_RES",
renatofilho@754
   214
                    __FUNCTION__);
renatofilho@754
   215
        return NULL;
renatofilho@754
   216
    } else {
renatofilho@754
   217
        MYSQL_ROW       row;
melunko@583
   218
renatofilho@754
   219
        if ((row = mysql_fetch_row(msql_res)) != NULL) {
melunko@583
   220
renatofilho@754
   221
            channel_info = g_new0(GMythChannelInfo, 1);
renatofilho@754
   222
            channel_info->channel_ID = channel_id;
renatofilho@754
   223
            channel_info->channel_num = g_string_new(row[0]);
renatofilho@754
   224
            channel_info->channel_name = g_string_new(row[1]);
renatofilho@754
   225
            channel_info->channel_icon = g_string_new(row[2]);
rosfran@698
   226
#ifdef GMYTH_USE_DEBUG
renatofilho@754
   227
            gmyth_channel_info_print(channel_info);
rosfran@698
   228
#endif
renatofilho@754
   229
        }
renatofilho@754
   230
    }
renatofilho@754
   231
    mysql_free_result(msql_res);
rosfran@698
   232
renatofilho@754
   233
    return channel_info;
melunko@583
   234
}
melunko@583
   235
leo_sobral@1
   236
/** 
leo_sobral@1
   237
 * Retrieves the available list of channels from the backend Mysql database.
leo_sobral@1
   238
 * 
leo_sobral@1
   239
 * @param gmyth_epg the GMythEPG instance.
leo_sobral@446
   240
 * @param proglist the GSList pointer to be filled with the loaded list.
leo_sobral@1
   241
 * @param chan_num the channel num on which to search for program.
leo_sobral@1
   242
 * @param starttime the start time to search for programs.
leo_sobral@1
   243
 * @param endtime the end time to search for programs.
leo_sobral@1
   244
 * @return The amount of channels retrieved from database, or -1 if error.
leo_sobral@1
   245
 */
leo_sobral@1
   246
gint
renatofilho@750
   247
gmyth_epg_get_program_list(GMythEPG * gmyth_epg, GList ** proglist,
renatofilho@754
   248
                           const gint chan_num, GTimeVal * starttime,
renatofilho@754
   249
                           GTimeVal * endtime)
leo_sobral@1
   250
{
melunko@313
   251
renatofilho@754
   252
    gchar          *startts =
renatofilho@754
   253
        gmyth_util_time_to_string_from_time_val(starttime);
renatofilho@754
   254
    gchar          *endts =
renatofilho@754
   255
        gmyth_util_time_to_string_from_time_val(endtime);
renatofilho@754
   256
    MYSQL_ROW       row;
renatofilho@754
   257
    GString        *querystr;
rosfran@698
   258
renatofilho@754
   259
    assert(gmyth_epg);
rosfran@698
   260
renatofilho@754
   261
    querystr =
renatofilho@754
   262
        g_string_new
renatofilho@754
   263
        ("SELECT DISTINCT program.chanid, program.starttime, program.endtime, "
renatofilho@754
   264
         "    program.title, program.subtitle, program.description, "
renatofilho@754
   265
         "    program.category, channel.channum, channel.callsign, "
renatofilho@754
   266
         "    channel.name, program.previouslyshown, channel.commfree, "
renatofilho@754
   267
         "    channel.outputfilters, program.seriesid, program.programid, "
renatofilho@754
   268
         "    program.airdate, program.stars, program.originalairdate, "
renatofilho@886
   269
         "    program.category_type, record.recordid, "
renatofilho@754
   270
         "    oldrecstatus.rectype, oldrecstatus.recstatus, "
renatofilho@886
   271
         "    oldrecstatus.findid "
renatofilho@886
   272
         "FROM program "
renatofilho@754
   273
         "LEFT JOIN channel ON program.chanid = channel.chanid "
renatofilho@886
   274
         "LEFT JOIN record ON "
renatofilho@886
   275
         "    program.chanid = record.chanid AND "
renatofilho@886
   276
         "    DATE (program.starttime) = record.startdate AND "
renatofilho@886
   277
         "    TIME (program.starttime) = record.starttime AND "
renatofilho@886
   278
         "    DATE (program.endtime) = record.enddate AND "
renatofilho@886
   279
         "    TIME (program.endtime) = record.endtime "
renatofilho@754
   280
         "LEFT JOIN oldrecorded AS oldrecstatus ON "
renatofilho@754
   281
         "    program.title = oldrecstatus.title AND "
renatofilho@754
   282
         "    channel.callsign = oldrecstatus.station AND "
renatofilho@754
   283
         "    program.starttime = oldrecstatus.starttime ");
rosfran@698
   284
renatofilho@754
   285
    g_string_append_printf(querystr,
renatofilho@754
   286
                           "WHERE program.chanid = %d "
renatofilho@754
   287
                           "  AND program.endtime >= '%s' "
renatofilho@754
   288
                           "  AND program.starttime <= '%s' "
renatofilho@754
   289
                           "  AND program.manualid = 0 ", chan_num,
renatofilho@754
   290
                           startts, endts);
leo_sobral@1
   291
renatofilho@754
   292
    if (!g_strrstr(querystr->str, " GROUP BY "))
renatofilho@754
   293
        querystr = g_string_append(querystr,
renatofilho@754
   294
                                   " GROUP BY program.starttime, channel.channum, "
renatofilho@754
   295
                                   "  channel.callsign, program.title ");
leo_sobral@1
   296
renatofilho@754
   297
    if (!g_strrstr(querystr->str, " LIMIT "))
renatofilho@754
   298
        querystr = g_string_append(querystr, " LIMIT 1000 ");
leo_sobral@1
   299
renatofilho@754
   300
    MYSQL_RES      *res_set =
renatofilho@754
   301
        gmyth_query_process_statement(gmyth_epg->sqlquery, querystr->str);
leo_sobral@1
   302
renatofilho@754
   303
    if (res_set == NULL) {
renatofilho@754
   304
        gmyth_debug("[%s] msql query returned NULL MYSQL_RES",
renatofilho@754
   305
                    __FUNCTION__);
renatofilho@754
   306
        return -1;
renatofilho@754
   307
    }
leo_sobral@1
   308
renatofilho@868
   309
    *proglist = NULL;
renatofilho@754
   310
    while ((row = mysql_fetch_row(res_set)) != NULL) {
leo_sobral@1
   311
renatofilho@754
   312
        GMythProgramInfo *p = gmyth_program_info_new();
rosfran@698
   313
renatofilho@890
   314
        p->xx_channel_id = (int) g_ascii_strtoull (row[0], NULL, 10);
leo_sobral@1
   315
renatofilho@754
   316
        p->startts = gmyth_util_string_to_time_val(row[1]);
renatofilho@754
   317
        p->endts = gmyth_util_string_to_time_val(row[2]);
rosfran@698
   318
renatofilho@754
   319
        p->recstartts = g_new0(GTimeVal, 1);
renatofilho@754
   320
        p->recstartts->tv_sec = p->startts->tv_sec;
renatofilho@754
   321
        p->recstartts->tv_usec = p->startts->tv_usec;
melunko@313
   322
renatofilho@754
   323
        p->recendts = g_new0(GTimeVal, 1);
renatofilho@754
   324
        p->recendts->tv_sec = p->endts->tv_sec;
renatofilho@754
   325
        p->recendts->tv_usec = p->endts->tv_usec;
melunko@313
   326
renatofilho@754
   327
        p->lastmodified = g_new0(GTimeVal, 1);
renatofilho@754
   328
        p->lastmodified->tv_sec = p->startts->tv_sec;
renatofilho@754
   329
        p->lastmodified->tv_usec = p->startts->tv_usec;
rosfran@698
   330
renatofilho@886
   331
renatofilho@754
   332
        p->title = g_string_new(row[3]);
renatofilho@754
   333
        p->subtitle = g_string_new(row[4]);
renatofilho@754
   334
        p->description = g_string_new(row[5]);
renatofilho@754
   335
        p->category = g_string_new(row[6]);
renatofilho@754
   336
        p->chanstr = g_string_new(row[7]);
renatofilho@754
   337
        p->chansign = g_string_new(row[8]);
renatofilho@754
   338
        p->channame = g_string_new(row[9]);
renatofilho@754
   339
        p->repeat = g_ascii_strtoull(row[10], NULL, 10);
renatofilho@754
   340
        p->chancommfree = g_ascii_strtoull(row[11], NULL, 10);
renatofilho@754
   341
        p->chanOutputFilters = g_string_new(row[12]);
renatofilho@754
   342
        p->seriesid = g_string_new(row[13]);
renatofilho@890
   343
        p->xx_program_id = g_string_new(row[14]);
renatofilho@754
   344
        p->year = g_string_new(row[15]);
renatofilho@754
   345
        p->stars = g_ascii_strtod(row[16], NULL);
leo_sobral@1
   346
renatofilho@754
   347
        if (!row[17] || !strcmp(row[17], "")) {
renatofilho@754
   348
            p->originalAirDate = 0;
renatofilho@754
   349
            p->hasAirDate = FALSE;
renatofilho@754
   350
        } else {
renatofilho@754
   351
            p->originalAirDate = gmyth_util_string_to_time_val(row[17]);
renatofilho@754
   352
            p->hasAirDate = TRUE;
renatofilho@754
   353
        }
rosfran@698
   354
renatofilho@754
   355
        p->catType = g_string_new(row[18]);
renatofilho@886
   356
        if (row[19] != NULL)
renatofilho@886
   357
            p->recordid =  g_ascii_strtoull(row[19], NULL, 10);
leo_sobral@1
   358
renatofilho@868
   359
        *proglist = g_list_append(*proglist, p);
leo_sobral@49
   360
rosfran@698
   361
#ifdef GMYTH_USE_DEBUG
renatofilho@754
   362
        gmyth_program_info_print(p);
rosfran@698
   363
#endif
renatofilho@754
   364
    }
leo_sobral@1
   365
renatofilho@754
   366
    /*
renatofilho@754
   367
     * deallocate 
renatofilho@754
   368
     */
renatofilho@754
   369
    mysql_free_result(res_set);
renatofilho@754
   370
    g_string_free(querystr, TRUE);
leo_sobral@1
   371
renatofilho@868
   372
    return g_list_length (*proglist);
leo_sobral@1
   373
}
melunko@583
   374
rosfran@698
   375
gboolean
renatofilho@750
   376
gmyth_epg_channel_has_icon(GMythEPG * gmyth_epg,
renatofilho@754
   377
                           GMythChannelInfo * channel_info)
melunko@583
   378
{
renatofilho@754
   379
    gboolean        res = FALSE;
melunko@583
   380
renatofilho@754
   381
    g_return_val_if_fail(gmyth_epg != NULL, FALSE);
renatofilho@754
   382
    g_return_val_if_fail(channel_info != NULL, FALSE);
melunko@583
   383
renatofilho@754
   384
    if (channel_info->channel_icon != NULL) {
renatofilho@754
   385
        res = gmyth_util_file_exists(gmyth_epg->backend_info,
renatofilho@754
   386
                                     channel_info->channel_icon->str);
renatofilho@754
   387
    }
melunko@583
   388
renatofilho@754
   389
    return res;
melunko@583
   390
melunko@583
   391
}
melunko@583
   392
melunko@583
   393
/**
melunko@583
   394
 * 
melunko@583
   395
 * @param data the data pointer to be filled with icon binary data. It must be freed by the calling function.
melunko@583
   396
 * @return TRUE if success, FALSE if any error happens.
melunko@583
   397
 */
melunko@583
   398
gboolean
renatofilho@750
   399
gmyth_epg_channel_get_icon(GMythEPG * gmyth_epg,
renatofilho@754
   400
                           GMythChannelInfo * channel_info, guint8 ** data,
renatofilho@754
   401
                           guint * length)
melunko@583
   402
{
morphbr@766
   403
    gboolean res = FALSE;
melunko@583
   404
renatofilho@754
   405
    g_return_val_if_fail(gmyth_epg != NULL, FALSE);
renatofilho@754
   406
    g_return_val_if_fail(channel_info != NULL, FALSE);
melunko@583
   407
renatofilho@754
   408
    if (gmyth_epg_channel_has_icon(gmyth_epg, channel_info)) {
renatofilho@754
   409
        GMythFileTransfer *transfer =
renatofilho@754
   410
            gmyth_file_transfer_new(gmyth_epg->backend_info);
morphbr@766
   411
renatofilho@754
   412
        GMythFileReadResult gmyth_res;
morphbr@766
   413
        GByteArray *icon_data;
morphbr@766
   414
        guint64 icon_length = 0;
melunko@583
   415
renatofilho@754
   416
        res = gmyth_file_transfer_open(transfer,
renatofilho@754
   417
                                       channel_info->channel_icon->str);
renatofilho@754
   418
        if (!res) {
renatofilho@754
   419
            gmyth_debug("Channel icon could not be opened");
renatofilho@754
   420
            return FALSE;
renatofilho@754
   421
        }
melunko@583
   422
renatofilho@754
   423
        icon_length = gmyth_file_transfer_get_filesize(transfer);
renatofilho@754
   424
        if (icon_length <= 0) {
renatofilho@754
   425
            gmyth_debug("Channel icon file size is zero or negative");
renatofilho@754
   426
            return FALSE;
renatofilho@754
   427
        }
melunko@583
   428
renatofilho@754
   429
        icon_data = g_byte_array_new();
renatofilho@754
   430
        gmyth_res = gmyth_file_transfer_read(transfer,
renatofilho@754
   431
                                             icon_data,
renatofilho@754
   432
                                             icon_length, FALSE);
renatofilho@754
   433
        if (gmyth_res == GMYTH_FILE_READ_EOF) {
renatofilho@754
   434
            *length = icon_length;
renatofilho@754
   435
            *data = icon_data->data;
renatofilho@754
   436
            g_byte_array_free(icon_data, FALSE);
renatofilho@754
   437
            res = TRUE;
renatofilho@754
   438
        } else {
renatofilho@754
   439
            *length = 0;
renatofilho@754
   440
            *data = NULL;
renatofilho@754
   441
            g_byte_array_free(icon_data, TRUE);
renatofilho@754
   442
        }
renatofilho@754
   443
    }
melunko@583
   444
renatofilho@754
   445
    return res;
melunko@583
   446
}