gmyth/gmyth/gmyth_epg.c
author melunko
Thu Mar 13 16:29:38 2008 +0000 (2008-03-13)
branchtrunk
changeset 942 c93bfa74c71f
parent 925 4a8d56080089
permissions -rw-r--r--
[svn r951] gmyth now is 0.8.1. Added methods epg_is_connected() and scheduler_is_connected()
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
melunko@942
   127
gboolean
melunko@942
   128
gmyth_epg_is_connected (GMythEPG *gmyth_epg)
melunko@942
   129
{
melunko@942
   130
    g_return_val_if_fail (gmyth_epg != NULL, FALSE);
melunko@942
   131
melunko@942
   132
    return gmyth_query_is_alive (gmyth_epg->sqlquery);
melunko@942
   133
}
melunko@942
   134
leo_sobral@1
   135
/** Disconnects from the Mysql database in the backend.
morphbr@766
   136
 *
leo_sobral@1
   137
 * @param gmyth_epg the GMythEPG instance to be disconnected
leo_sobral@1
   138
 * @return true if disconnection was success, false if failed.
leo_sobral@1
   139
 */
leo_sobral@1
   140
gboolean
renatofilho@750
   141
gmyth_epg_disconnect(GMythEPG * gmyth_epg)
leo_sobral@1
   142
{
renatofilho@754
   143
    g_return_val_if_fail(gmyth_epg != NULL, FALSE);
leo_sobral@1
   144
renatofilho@754
   145
    if (gmyth_epg->sqlquery != NULL) {
renatofilho@754
   146
        gmyth_query_disconnect(gmyth_epg->sqlquery);
renatofilho@754
   147
        g_object_unref(gmyth_epg->sqlquery);
renatofilho@754
   148
        gmyth_epg->sqlquery = NULL;
renatofilho@754
   149
    }
melunko@583
   150
renatofilho@754
   151
    if (gmyth_epg->backend_info != NULL) {
renatofilho@754
   152
        g_object_unref(gmyth_epg->backend_info);
renatofilho@754
   153
        gmyth_epg->backend_info = NULL;
renatofilho@754
   154
    }
rosfran@698
   155
renatofilho@754
   156
    return TRUE;
leo_sobral@1
   157
}
leo_sobral@1
   158
leo_sobral@1
   159
/** Retrieves the available list of channels from the backend Mysql database.
leo_sobral@1
   160
 * 
leo_sobral@1
   161
 * @param gmyth_epg the GMythEPG instance.
leo_sobral@446
   162
 * @param glist_ptr the GSList pointer to be filled with the loaded list address.
leo_sobral@1
   163
 * @return The amount of channels retrieved from database,  or -1 if error.
leo_sobral@1
   164
 */
leo_sobral@1
   165
gint
renatofilho@750
   166
gmyth_epg_get_channel_list(GMythEPG * gmyth_epg, GList ** glist_ptr)
leo_sobral@1
   167
{
renatofilho@754
   168
    MYSQL_RES      *msql_res;
leo_sobral@1
   169
renatofilho@754
   170
    g_return_val_if_fail(gmyth_epg != NULL, -1);
leo_sobral@1
   171
renatofilho@754
   172
    msql_res = gmyth_query_process_statement(gmyth_epg->sqlquery,
renatofilho@754
   173
                                             "SELECT chanid, channum, name, icon FROM channel;");
leo_sobral@1
   174
renatofilho@754
   175
    (*glist_ptr) = NULL;
rosfran@698
   176
renatofilho@754
   177
    if (msql_res == NULL) {
renatofilho@754
   178
        gmyth_debug("[%s] msql query returned NULL MYSQL_RES",
renatofilho@754
   179
                    __FUNCTION__);
renatofilho@754
   180
        return -1;
renatofilho@754
   181
    } else {
renatofilho@754
   182
        MYSQL_ROW       row;
renatofilho@754
   183
        GMythChannelInfo *channel_info;
leo_sobral@1
   184
renatofilho@754
   185
        while ((row = mysql_fetch_row(msql_res)) != NULL) {
leo_sobral@1
   186
renatofilho@754
   187
            channel_info = g_new0(GMythChannelInfo, 1);
renatofilho@754
   188
            channel_info->channel_ID =
renatofilho@754
   189
                (gint) g_ascii_strtoull(row[0], NULL, 10);
renatofilho@754
   190
            channel_info->channel_num = g_string_new(row[1]);
renatofilho@754
   191
            channel_info->channel_name = g_string_new(row[2]);
renatofilho@754
   192
            channel_info->channel_icon = g_string_new(row[3]);
rosfran@698
   193
#ifdef GMYTH_USE_DEBUG
renatofilho@754
   194
            gmyth_channel_info_print(channel_info);
rosfran@698
   195
#endif
renatofilho@754
   196
            (*glist_ptr) = g_list_append((*glist_ptr), channel_info);
renatofilho@754
   197
        }
renatofilho@754
   198
    }
renatofilho@754
   199
    mysql_free_result(msql_res);
rosfran@698
   200
renatofilho@754
   201
    return (!(*glist_ptr)) ? 0 : g_list_length(*glist_ptr);
leo_sobral@1
   202
}
leo_sobral@1
   203
rosfran@698
   204
GMythChannelInfo *
renatofilho@750
   205
gmyth_epg_get_channel_info(GMythEPG * gmyth_epg, gint channel_id)
melunko@583
   206
{
renatofilho@754
   207
    GMythChannelInfo *channel_info = NULL;
renatofilho@754
   208
    MYSQL_RES      *msql_res;
renatofilho@754
   209
    gchar          *query_str;
melunko@583
   210
renatofilho@754
   211
    g_return_val_if_fail(gmyth_epg != NULL, NULL);
melunko@583
   212
renatofilho@754
   213
    query_str =
renatofilho@754
   214
        g_strdup_printf
renatofilho@754
   215
        ("SELECT channum, name, icon FROM channel WHERE chanid=%d;",
renatofilho@754
   216
         channel_id);
renatofilho@754
   217
    msql_res =
renatofilho@754
   218
        gmyth_query_process_statement(gmyth_epg->sqlquery, query_str);
melunko@583
   219
renatofilho@754
   220
    if (msql_res == NULL) {
renatofilho@754
   221
        gmyth_debug("[%s] msql query returned NULL MYSQL_RES",
renatofilho@754
   222
                    __FUNCTION__);
renatofilho@754
   223
        return NULL;
renatofilho@754
   224
    } else {
renatofilho@754
   225
        MYSQL_ROW       row;
melunko@583
   226
renatofilho@754
   227
        if ((row = mysql_fetch_row(msql_res)) != NULL) {
melunko@583
   228
renatofilho@754
   229
            channel_info = g_new0(GMythChannelInfo, 1);
renatofilho@754
   230
            channel_info->channel_ID = channel_id;
renatofilho@754
   231
            channel_info->channel_num = g_string_new(row[0]);
renatofilho@754
   232
            channel_info->channel_name = g_string_new(row[1]);
renatofilho@754
   233
            channel_info->channel_icon = g_string_new(row[2]);
rosfran@698
   234
#ifdef GMYTH_USE_DEBUG
renatofilho@754
   235
            gmyth_channel_info_print(channel_info);
rosfran@698
   236
#endif
renatofilho@754
   237
        }
renatofilho@754
   238
    }
renatofilho@754
   239
    mysql_free_result(msql_res);
rosfran@698
   240
renatofilho@754
   241
    return channel_info;
melunko@583
   242
}
melunko@583
   243
leo_sobral@1
   244
/** 
leo_sobral@1
   245
 * Retrieves the available list of channels from the backend Mysql database.
leo_sobral@1
   246
 * 
leo_sobral@1
   247
 * @param gmyth_epg the GMythEPG instance.
leo_sobral@446
   248
 * @param proglist the GSList pointer to be filled with the loaded list.
leo_sobral@1
   249
 * @param chan_num the channel num on which to search for program.
leo_sobral@1
   250
 * @param starttime the start time to search for programs.
leo_sobral@1
   251
 * @param endtime the end time to search for programs.
leo_sobral@1
   252
 * @return The amount of channels retrieved from database, or -1 if error.
leo_sobral@1
   253
 */
leo_sobral@1
   254
gint
renatofilho@750
   255
gmyth_epg_get_program_list(GMythEPG * gmyth_epg, GList ** proglist,
renatofilho@754
   256
                           const gint chan_num, GTimeVal * starttime,
renatofilho@754
   257
                           GTimeVal * endtime)
leo_sobral@1
   258
{
melunko@313
   259
renatofilho@754
   260
    gchar          *startts =
renatofilho@754
   261
        gmyth_util_time_to_string_from_time_val(starttime);
renatofilho@754
   262
    gchar          *endts =
renatofilho@754
   263
        gmyth_util_time_to_string_from_time_val(endtime);
renatofilho@754
   264
    MYSQL_ROW       row;
renatofilho@754
   265
    GString        *querystr;
rosfran@698
   266
renatofilho@754
   267
    assert(gmyth_epg);
rosfran@698
   268
renatofilho@754
   269
    querystr =
renatofilho@754
   270
        g_string_new
renatofilho@754
   271
        ("SELECT DISTINCT program.chanid, program.starttime, program.endtime, "
renatofilho@754
   272
         "    program.title, program.subtitle, program.description, "
renatofilho@754
   273
         "    program.category, channel.channum, channel.callsign, "
renatofilho@754
   274
         "    channel.name, program.previouslyshown, channel.commfree, "
renatofilho@754
   275
         "    channel.outputfilters, program.seriesid, program.programid, "
renatofilho@754
   276
         "    program.airdate, program.stars, program.originalairdate, "
renatofilho@886
   277
         "    program.category_type, record.recordid, "
renatofilho@754
   278
         "    oldrecstatus.rectype, oldrecstatus.recstatus, "
renatofilho@886
   279
         "    oldrecstatus.findid "
renatofilho@886
   280
         "FROM program "
renatofilho@754
   281
         "LEFT JOIN channel ON program.chanid = channel.chanid "
renatofilho@886
   282
         "LEFT JOIN record ON "
renatofilho@886
   283
         "    program.chanid = record.chanid AND "
renatofilho@886
   284
         "    DATE (program.starttime) = record.startdate AND "
renatofilho@886
   285
         "    TIME (program.starttime) = record.starttime AND "
renatofilho@886
   286
         "    DATE (program.endtime) = record.enddate AND "
renatofilho@886
   287
         "    TIME (program.endtime) = record.endtime "
renatofilho@754
   288
         "LEFT JOIN oldrecorded AS oldrecstatus ON "
renatofilho@754
   289
         "    program.title = oldrecstatus.title AND "
renatofilho@754
   290
         "    channel.callsign = oldrecstatus.station AND "
renatofilho@754
   291
         "    program.starttime = oldrecstatus.starttime ");
rosfran@698
   292
renatofilho@754
   293
    g_string_append_printf(querystr,
renatofilho@754
   294
                           "WHERE program.chanid = %d "
renatofilho@754
   295
                           "  AND program.endtime >= '%s' "
renatofilho@754
   296
                           "  AND program.starttime <= '%s' "
renatofilho@754
   297
                           "  AND program.manualid = 0 ", chan_num,
renatofilho@754
   298
                           startts, endts);
leo_sobral@1
   299
renatofilho@754
   300
    if (!g_strrstr(querystr->str, " GROUP BY "))
renatofilho@754
   301
        querystr = g_string_append(querystr,
renatofilho@754
   302
                                   " GROUP BY program.starttime, channel.channum, "
renatofilho@754
   303
                                   "  channel.callsign, program.title ");
leo_sobral@1
   304
renatofilho@754
   305
    if (!g_strrstr(querystr->str, " LIMIT "))
renatofilho@754
   306
        querystr = g_string_append(querystr, " LIMIT 1000 ");
leo_sobral@1
   307
renatofilho@754
   308
    MYSQL_RES      *res_set =
renatofilho@754
   309
        gmyth_query_process_statement(gmyth_epg->sqlquery, querystr->str);
leo_sobral@1
   310
renatofilho@754
   311
    if (res_set == NULL) {
renatofilho@754
   312
        gmyth_debug("[%s] msql query returned NULL MYSQL_RES",
renatofilho@754
   313
                    __FUNCTION__);
renatofilho@754
   314
        return -1;
renatofilho@754
   315
    }
leo_sobral@1
   316
renatofilho@868
   317
    *proglist = NULL;
renatofilho@754
   318
    while ((row = mysql_fetch_row(res_set)) != NULL) {
leo_sobral@1
   319
renatofilho@754
   320
        GMythProgramInfo *p = gmyth_program_info_new();
rosfran@698
   321
renatofilho@895
   322
        p->channel_id = (int) g_ascii_strtoull (row[0], NULL, 10);
leo_sobral@1
   323
renatofilho@754
   324
        p->startts = gmyth_util_string_to_time_val(row[1]);
renatofilho@754
   325
        p->endts = gmyth_util_string_to_time_val(row[2]);
rosfran@698
   326
renatofilho@754
   327
        p->recstartts = g_new0(GTimeVal, 1);
renatofilho@754
   328
        p->recstartts->tv_sec = p->startts->tv_sec;
renatofilho@754
   329
        p->recstartts->tv_usec = p->startts->tv_usec;
melunko@313
   330
renatofilho@754
   331
        p->recendts = g_new0(GTimeVal, 1);
renatofilho@754
   332
        p->recendts->tv_sec = p->endts->tv_sec;
renatofilho@754
   333
        p->recendts->tv_usec = p->endts->tv_usec;
melunko@313
   334
renatofilho@754
   335
        p->lastmodified = g_new0(GTimeVal, 1);
renatofilho@754
   336
        p->lastmodified->tv_sec = p->startts->tv_sec;
renatofilho@754
   337
        p->lastmodified->tv_usec = p->startts->tv_usec;
rosfran@698
   338
renatofilho@886
   339
renatofilho@754
   340
        p->title = g_string_new(row[3]);
renatofilho@754
   341
        p->subtitle = g_string_new(row[4]);
renatofilho@754
   342
        p->description = g_string_new(row[5]);
renatofilho@754
   343
        p->category = g_string_new(row[6]);
renatofilho@754
   344
        p->chanstr = g_string_new(row[7]);
renatofilho@754
   345
        p->chansign = g_string_new(row[8]);
renatofilho@754
   346
        p->channame = g_string_new(row[9]);
renatofilho@754
   347
        p->repeat = g_ascii_strtoull(row[10], NULL, 10);
renatofilho@754
   348
        p->chancommfree = g_ascii_strtoull(row[11], NULL, 10);
renatofilho@754
   349
        p->chanOutputFilters = g_string_new(row[12]);
renatofilho@754
   350
        p->seriesid = g_string_new(row[13]);
renatofilho@895
   351
        p->program_id = g_string_new(row[14]);
renatofilho@754
   352
        p->year = g_string_new(row[15]);
renatofilho@754
   353
        p->stars = g_ascii_strtod(row[16], NULL);
leo_sobral@1
   354
renatofilho@754
   355
        if (!row[17] || !strcmp(row[17], "")) {
renatofilho@754
   356
            p->originalAirDate = 0;
renatofilho@754
   357
            p->hasAirDate = FALSE;
renatofilho@754
   358
        } else {
renatofilho@754
   359
            p->originalAirDate = gmyth_util_string_to_time_val(row[17]);
renatofilho@754
   360
            p->hasAirDate = TRUE;
renatofilho@754
   361
        }
rosfran@698
   362
renatofilho@754
   363
        p->catType = g_string_new(row[18]);
renatofilho@886
   364
        if (row[19] != NULL)
renatofilho@886
   365
            p->recordid =  g_ascii_strtoull(row[19], NULL, 10);
leo_sobral@1
   366
renatofilho@868
   367
        *proglist = g_list_append(*proglist, p);
leo_sobral@49
   368
rosfran@698
   369
#ifdef GMYTH_USE_DEBUG
renatofilho@754
   370
        gmyth_program_info_print(p);
rosfran@698
   371
#endif
renatofilho@754
   372
    }
leo_sobral@1
   373
renatofilho@754
   374
    /*
renatofilho@754
   375
     * deallocate 
renatofilho@754
   376
     */
renatofilho@754
   377
    mysql_free_result(res_set);
renatofilho@754
   378
    g_string_free(querystr, TRUE);
leo_sobral@1
   379
renatofilho@868
   380
    return g_list_length (*proglist);
leo_sobral@1
   381
}
melunko@583
   382
rosfran@698
   383
gboolean
renatofilho@750
   384
gmyth_epg_channel_has_icon(GMythEPG * gmyth_epg,
renatofilho@754
   385
                           GMythChannelInfo * channel_info)
melunko@583
   386
{
renatofilho@754
   387
    gboolean        res = FALSE;
melunko@583
   388
renatofilho@754
   389
    g_return_val_if_fail(gmyth_epg != NULL, FALSE);
renatofilho@754
   390
    g_return_val_if_fail(channel_info != NULL, FALSE);
melunko@583
   391
renatofilho@754
   392
    if (channel_info->channel_icon != NULL) {
renatofilho@754
   393
        res = gmyth_util_file_exists(gmyth_epg->backend_info,
renatofilho@754
   394
                                     channel_info->channel_icon->str);
renatofilho@754
   395
    }
melunko@583
   396
renatofilho@754
   397
    return res;
melunko@583
   398
melunko@583
   399
}
melunko@583
   400
melunko@583
   401
/**
melunko@583
   402
 * 
melunko@583
   403
 * @param data the data pointer to be filled with icon binary data. It must be freed by the calling function.
melunko@583
   404
 * @return TRUE if success, FALSE if any error happens.
melunko@583
   405
 */
melunko@583
   406
gboolean
renatofilho@750
   407
gmyth_epg_channel_get_icon(GMythEPG * gmyth_epg,
renatofilho@754
   408
                           GMythChannelInfo * channel_info, guint8 ** data,
renatofilho@754
   409
                           guint * length)
melunko@583
   410
{
morphbr@766
   411
    gboolean res = FALSE;
melunko@583
   412
renatofilho@754
   413
    g_return_val_if_fail(gmyth_epg != NULL, FALSE);
renatofilho@754
   414
    g_return_val_if_fail(channel_info != NULL, FALSE);
melunko@583
   415
renatofilho@754
   416
    if (gmyth_epg_channel_has_icon(gmyth_epg, channel_info)) {
renatofilho@754
   417
        GMythFileTransfer *transfer =
renatofilho@754
   418
            gmyth_file_transfer_new(gmyth_epg->backend_info);
morphbr@766
   419
renatofilho@754
   420
        GMythFileReadResult gmyth_res;
morphbr@766
   421
        GByteArray *icon_data;
morphbr@766
   422
        guint64 icon_length = 0;
melunko@583
   423
renatofilho@754
   424
        res = gmyth_file_transfer_open(transfer,
renatofilho@754
   425
                                       channel_info->channel_icon->str);
renatofilho@754
   426
        if (!res) {
renatofilho@754
   427
            gmyth_debug("Channel icon could not be opened");
renatofilho@754
   428
            return FALSE;
renatofilho@754
   429
        }
melunko@583
   430
renatofilho@754
   431
        icon_length = gmyth_file_transfer_get_filesize(transfer);
renatofilho@754
   432
        if (icon_length <= 0) {
renatofilho@754
   433
            gmyth_debug("Channel icon file size is zero or negative");
renatofilho@754
   434
            return FALSE;
renatofilho@754
   435
        }
melunko@583
   436
renatofilho@754
   437
        icon_data = g_byte_array_new();
renatofilho@754
   438
        gmyth_res = gmyth_file_transfer_read(transfer,
renatofilho@754
   439
                                             icon_data,
renatofilho@754
   440
                                             icon_length, FALSE);
renatofilho@754
   441
        if (gmyth_res == GMYTH_FILE_READ_EOF) {
renatofilho@754
   442
            *length = icon_length;
renatofilho@754
   443
            *data = icon_data->data;
renatofilho@754
   444
            g_byte_array_free(icon_data, FALSE);
renatofilho@754
   445
            res = TRUE;
renatofilho@754
   446
        } else {
renatofilho@754
   447
            *length = 0;
renatofilho@754
   448
            *data = NULL;
renatofilho@754
   449
            g_byte_array_free(icon_data, TRUE);
renatofilho@754
   450
        }
renatofilho@754
   451
    }
melunko@583
   452
renatofilho@754
   453
    return res;
melunko@583
   454
}