gmyth/src/gmyth_epg.c
author melunko
Tue Jan 30 20:52:35 2007 +0000 (2007-01-30)
branchtrunk
changeset 313 f8d246310650
parent 307 0a8fe4427e41
child 314 15f6102ef37d
permissions -rw-r--r--
[svn r314] A lot of bug fixed. Test recordings program added.
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>
leo_sobral@1
    11
 *
leo_sobral@1
    12
 *//*
leo_sobral@1
    13
 * 
leo_sobral@1
    14
 * This program is free software; you can redistribute it and/or modify
leo_sobral@1
    15
 * it under the terms of the GNU Lesser General Public License as published by
leo_sobral@1
    16
 * the Free Software Foundation; either version 2 of the License, or
leo_sobral@1
    17
 * (at your option) any later version.
leo_sobral@1
    18
 *
leo_sobral@1
    19
 * This program is distributed in the hope that it will be useful,
leo_sobral@1
    20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
leo_sobral@1
    21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
leo_sobral@1
    22
 * GNU General Public License for more details.
leo_sobral@1
    23
 *
leo_sobral@1
    24
 * You should have received a copy of the GNU Lesser General Public License
leo_sobral@1
    25
 * along with this program; if not, write to the Free Software
leo_sobral@1
    26
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
leo_sobral@1
    27
 */
leo_sobral@213
    28
 
leo_sobral@213
    29
#ifdef HAVE_CONFIG_H
leo_sobral@213
    30
#include "config.h"
leo_sobral@213
    31
#endif
leo_sobral@1
    32
rosfran@240
    33
#include <mysql/mysql.h>
leo_sobral@1
    34
#include <stdlib.h>
leo_sobral@1
    35
#include <string.h>
leo_sobral@1
    36
#include <assert.h>
leo_sobral@1
    37
leo_sobral@1
    38
#include "gmyth_epg.h"
rosfran@291
    39
#include "gmyth_programinfo.h"
leo_sobral@1
    40
#include "gmyth_util.h"
renatofilho@131
    41
#include "gmyth_debug.h"
leo_sobral@1
    42
leo_sobral@1
    43
static void gmyth_epg_class_init          (GMythEPGClass *klass);
leo_sobral@1
    44
static void gmyth_epg_init                (GMythEPG *object);
leo_sobral@1
    45
leo_sobral@1
    46
static void gmyth_epg_dispose  (GObject *object);
leo_sobral@1
    47
static void gmyth_epg_finalize (GObject *object);
leo_sobral@1
    48
leo_sobral@1
    49
G_DEFINE_TYPE(GMythEPG, gmyth_epg, G_TYPE_OBJECT)
leo_sobral@1
    50
    
leo_sobral@1
    51
static void
leo_sobral@1
    52
gmyth_epg_class_init (GMythEPGClass *klass)
leo_sobral@1
    53
{
leo_sobral@161
    54
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
leo_sobral@1
    55
	
leo_sobral@1
    56
    gobject_class->dispose  = gmyth_epg_dispose;
leo_sobral@1
    57
    gobject_class->finalize = gmyth_epg_finalize;	
leo_sobral@1
    58
}
leo_sobral@1
    59
leo_sobral@1
    60
static void
leo_sobral@1
    61
gmyth_epg_init (GMythEPG *gmyth_epg)
leo_sobral@1
    62
{
melunko@117
    63
leo_sobral@1
    64
}
leo_sobral@1
    65
leo_sobral@1
    66
static void
leo_sobral@1
    67
gmyth_epg_dispose  (GObject *object)
leo_sobral@1
    68
{
leo_sobral@1
    69
    //GMythEPG *gmyth_epg = GMYTH_EPG(object);
leo_sobral@161
    70
   
leo_sobral@1
    71
	G_OBJECT_CLASS (gmyth_epg_parent_class)->dispose (object);
leo_sobral@1
    72
}
leo_sobral@1
    73
leo_sobral@1
    74
static void
leo_sobral@1
    75
gmyth_epg_finalize (GObject *object)
leo_sobral@1
    76
{
melunko@117
    77
    g_signal_handlers_destroy (object);
leo_sobral@1
    78
leo_sobral@1
    79
    G_OBJECT_CLASS (gmyth_epg_parent_class)->finalize (object);
leo_sobral@1
    80
}
leo_sobral@1
    81
leo_sobral@1
    82
/**
leo_sobral@1
    83
 * Creates a new instance of GMythEPG.
leo_sobral@1
    84
 * 
leo_sobral@1
    85
 * @return a new instance of GMythEPG.
leo_sobral@1
    86
 */
leo_sobral@1
    87
GMythEPG*
leo_sobral@6
    88
gmyth_epg_new (void)
leo_sobral@1
    89
{
leo_sobral@1
    90
    GMythEPG *epg = GMYTH_EPG (g_object_new(GMYTH_EPG_TYPE, NULL));
leo_sobral@1
    91
leo_sobral@1
    92
    return epg;
leo_sobral@1
    93
}
leo_sobral@1
    94
leo_sobral@1
    95
/** Connects to the Mysql database in the backend. The backend address
leo_sobral@1
    96
 * is loaded from the GMythSettings instance.
leo_sobral@1
    97
 * 
leo_sobral@1
    98
 * @param gmyth_epg the GMythEPG instance to be connected.
leo_sobral@1
    99
 * @return true if connection was success, false if failed.
leo_sobral@1
   100
 */
leo_sobral@1
   101
gboolean
melunko@117
   102
gmyth_epg_connect (GMythEPG *gmyth_epg, GMythBackendInfo *backend_info)
leo_sobral@1
   103
{
melunko@117
   104
    assert(gmyth_epg);
leo_sobral@1
   105
melunko@117
   106
    if (gmyth_epg->sqlquery	== NULL) {
renatofilho@131
   107
	gmyth_debug ("[%s] Creating gmyth_query", __FUNCTION__);
melunko@117
   108
        gmyth_epg->sqlquery = gmyth_query_new ( );
melunko@117
   109
    }
leo_sobral@1
   110
melunko@117
   111
    if (!gmyth_query_connect(gmyth_epg->sqlquery, backend_info)) {
leo_sobral@1
   112
        g_warning ("[%s] Error while connecting to db", __FUNCTION__);
leo_sobral@1
   113
        return FALSE;
leo_sobral@1
   114
    }
leo_sobral@1
   115
leo_sobral@1
   116
    return TRUE;	
leo_sobral@1
   117
}
leo_sobral@1
   118
leo_sobral@1
   119
/** Disconnects from the Mysql database in the backend.
leo_sobral@1
   120
 * 
leo_sobral@1
   121
 * @param gmyth_epg the GMythEPG instance to be disconnected
leo_sobral@1
   122
 * @return true if disconnection was success, false if failed.
leo_sobral@1
   123
 */
leo_sobral@1
   124
gboolean
leo_sobral@1
   125
gmyth_epg_disconnect (GMythEPG *gmyth_epg)
leo_sobral@1
   126
{
leo_sobral@1
   127
	assert(gmyth_epg);
leo_sobral@1
   128
leo_sobral@1
   129
	if (gmyth_epg->sqlquery	!= NULL) {	
leo_sobral@1
   130
		g_object_unref (gmyth_epg->sqlquery);
leo_sobral@32
   131
        gmyth_epg->sqlquery = NULL;
leo_sobral@1
   132
	}
leo_sobral@1
   133
	
leo_sobral@1
   134
	return TRUE;
leo_sobral@1
   135
}
leo_sobral@1
   136
leo_sobral@1
   137
/** Retrieves the available list of channels from the backend Mysql database.
leo_sobral@1
   138
 * 
leo_sobral@1
   139
 * @param gmyth_epg the GMythEPG instance.
leo_sobral@1
   140
 * @param glist_ptr the GList pointer to be filled with the loaded list address.
leo_sobral@1
   141
 * @return The amount of channels retrieved from database,  or -1 if error.
leo_sobral@1
   142
 */
leo_sobral@1
   143
gint
leo_sobral@1
   144
gmyth_epg_get_channel_list (GMythEPG *gmyth_epg, GList **glist_ptr)
leo_sobral@1
   145
{
leo_sobral@1
   146
	MYSQL_RES *msql_res;
leo_sobral@1
   147
leo_sobral@1
   148
    assert(gmyth_epg);
leo_sobral@1
   149
leo_sobral@1
   150
    msql_res = gmyth_query_process_statement (gmyth_epg->sqlquery, 
rosfran@307
   151
    		"SELECT chanid, channum, name FROM channel;");
leo_sobral@1
   152
leo_sobral@1
   153
	(*glist_ptr) = NULL;
leo_sobral@1
   154
	
leo_sobral@1
   155
    if (msql_res == NULL) {
leo_sobral@1
   156
        g_warning ("[%s] msql query returned NULL MYSQL_RES", __FUNCTION__);
leo_sobral@1
   157
		return -1;
leo_sobral@1
   158
    } else {
leo_sobral@1
   159
        MYSQL_ROW row;
leo_sobral@1
   160
		GMythChannelInfo *channel_info;        
leo_sobral@1
   161
leo_sobral@1
   162
        while ((row = mysql_fetch_row (msql_res)) != NULL){
leo_sobral@1
   163
leo_sobral@1
   164
        	channel_info = g_new0(GMythChannelInfo, 1);
rosfran@214
   165
            channel_info->channel_ID = g_ascii_strtoull (row[0], NULL, 10);
rosfran@307
   166
            channel_info->channel_num = g_string_new (row[1]);
rosfran@307
   167
            channel_info->channel_name = g_string_new (row[2]);
leo_sobral@49
   168
       
leo_sobral@49
   169
            gmyth_channel_info_print(channel_info);
leo_sobral@49
   170
            
leo_sobral@1
   171
            (*glist_ptr) = g_list_append ((*glist_ptr), channel_info);
leo_sobral@1
   172
    	}
leo_sobral@1
   173
    }
leo_sobral@1
   174
    mysql_free_result (msql_res);
leo_sobral@1
   175
    return (!(*glist_ptr)) ?  0 : g_list_length (*glist_ptr);
leo_sobral@1
   176
}
leo_sobral@1
   177
leo_sobral@1
   178
/** 
leo_sobral@1
   179
 * Retrieves the available list of channels from the backend Mysql database.
leo_sobral@1
   180
 * 
leo_sobral@1
   181
 * @param gmyth_epg the GMythEPG instance.
leo_sobral@1
   182
 * @param proglist the GList pointer to be filled with the loaded list.
leo_sobral@1
   183
 * @param chan_num the channel num on which to search for program.
leo_sobral@1
   184
 * @param starttime the start time to search for programs.
leo_sobral@1
   185
 * @param endtime the end time to search for programs.
leo_sobral@1
   186
 * @return The amount of channels retrieved from database, or -1 if error.
leo_sobral@1
   187
 */
leo_sobral@1
   188
gint
leo_sobral@1
   189
gmyth_epg_get_program_list (GMythEPG *gmyth_epg, GList **proglist,
rosfran@219
   190
		const gint chan_num, GTimeVal *starttime, GTimeVal *endtime)
leo_sobral@1
   191
{
melunko@313
   192
printf ("XXXX %s %d\n", __FUNCTION__, __LINE__);
melunko@313
   193
rosfran@214
   194
    gchar *startts = gmyth_util_time_to_string_from_time_val(starttime);
melunko@313
   195
printf ("XXXX %s %d\n", __FUNCTION__, __LINE__);
rosfran@214
   196
    gchar *endts = gmyth_util_time_to_string_from_time_val(endtime);
melunko@313
   197
printf ("XXXX %s %d\n", __FUNCTION__, __LINE__);
leo_sobral@1
   198
    MYSQL_ROW row;
leo_sobral@1
   199
    GString *querystr;
leo_sobral@1
   200
    
melunko@313
   201
printf ("XXXX %s %d\n", __FUNCTION__, __LINE__);
leo_sobral@1
   202
    assert(gmyth_epg);
leo_sobral@1
   203
    
melunko@313
   204
printf ("XXXX %s %d\n", __FUNCTION__, __LINE__);
leo_sobral@1
   205
    querystr = g_string_new(
leo_sobral@1
   206
        "SELECT DISTINCT program.chanid, program.starttime, program.endtime, "
leo_sobral@1
   207
        "    program.title, program.subtitle, program.description, "
leo_sobral@1
   208
        "    program.category, channel.channum, channel.callsign, "
leo_sobral@1
   209
        "    channel.name, program.previouslyshown, channel.commfree, "
leo_sobral@1
   210
        "    channel.outputfilters, program.seriesid, program.programid, "
leo_sobral@1
   211
        "    program.airdate, program.stars, program.originalairdate, "
leo_sobral@1
   212
        "    program.category_type, oldrecstatus.recordid, "
leo_sobral@1
   213
        "    oldrecstatus.rectype, oldrecstatus.recstatus, "
leo_sobral@1
   214
        "    oldrecstatus.findid "
leo_sobral@1
   215
        "FROM program "
leo_sobral@1
   216
        "LEFT JOIN channel ON program.chanid = channel.chanid "
leo_sobral@1
   217
        "LEFT JOIN oldrecorded AS oldrecstatus ON "
leo_sobral@1
   218
        "    program.title = oldrecstatus.title AND "
leo_sobral@1
   219
        "    channel.callsign = oldrecstatus.station AND "
leo_sobral@1
   220
        "    program.starttime = oldrecstatus.starttime "
leo_sobral@1
   221
        );
leo_sobral@1
   222
        
melunko@313
   223
printf ("XXXX %s %d\n", __FUNCTION__, __LINE__);
leo_sobral@1
   224
    g_string_append_printf (querystr, 
leo_sobral@1
   225
        "WHERE program.chanid = %d "
leo_sobral@1
   226
        "  AND program.endtime >= '%s' "
leo_sobral@1
   227
        "  AND program.starttime <= '%s' "
leo_sobral@1
   228
        "  AND program.manualid = 0 ",
rosfran@214
   229
        chan_num, startts, endts);
leo_sobral@1
   230
melunko@313
   231
printf ("XXXX %s %d\n", __FUNCTION__, __LINE__);
leo_sobral@1
   232
    if (!g_strrstr(querystr->str, " GROUP BY "))
leo_sobral@1
   233
        querystr = g_string_append(querystr,
leo_sobral@1
   234
            " GROUP BY program.starttime, channel.channum, "
leo_sobral@1
   235
            "  channel.callsign, program.title ");
leo_sobral@1
   236
melunko@313
   237
printf ("XXXX %s %d\n", __FUNCTION__, __LINE__);
leo_sobral@1
   238
    if (!g_strrstr(querystr->str, " LIMIT "))
leo_sobral@1
   239
        querystr = g_string_append(querystr, " LIMIT 1000 ");
leo_sobral@1
   240
melunko@313
   241
printf ("XXXX %s %d\n", __FUNCTION__, __LINE__);
leo_sobral@1
   242
    MYSQL_RES *res_set = 
leo_sobral@1
   243
        gmyth_query_process_statement(gmyth_epg->sqlquery, querystr->str);
leo_sobral@1
   244
melunko@313
   245
printf ("XXXX %s %d\n", __FUNCTION__, __LINE__);
leo_sobral@1
   246
    if (res_set == NULL) {
leo_sobral@1
   247
        g_warning ("[%s] msql query returned NULL MYSQL_RES", __FUNCTION__);
leo_sobral@1
   248
		return -1;
leo_sobral@1
   249
    }
leo_sobral@1
   250
melunko@313
   251
printf ("XXXX %s %d\n", __FUNCTION__, __LINE__);
leo_sobral@1
   252
    (*proglist) = NULL;        
leo_sobral@1
   253
    while ((row = mysql_fetch_row (res_set)) != NULL) {
leo_sobral@1
   254
melunko@313
   255
printf ("XXXX %s %d\n", __FUNCTION__, __LINE__);
rosfran@291
   256
        GMythProgramInfo *p = gmyth_program_info_new ();
leo_sobral@1
   257
        p->chanid = g_string_new (row[0]);
melunko@313
   258
printf ("XXXX %s %d\n", __FUNCTION__, __LINE__);
leo_sobral@1
   259
rosfran@214
   260
        p->startts = gmyth_util_string_to_time_val (row[1]);
rosfran@214
   261
        p->endts = gmyth_util_string_to_time_val (row[2]);
melunko@313
   262
printf ("XXXX %s %d\n", __FUNCTION__, __LINE__);
leo_sobral@1
   263
                                                     
melunko@313
   264
        p->recstartts = g_new0 (GTimeVal, 1);
melunko@313
   265
	p->recstartts->tv_sec  = p->startts->tv_sec;
melunko@313
   266
	p->recstartts->tv_usec = p->startts->tv_usec;
melunko@313
   267
melunko@313
   268
        p->recendts = g_new0 (GTimeVal, 1);
melunko@313
   269
	p->recendts->tv_sec  = p->endts->tv_sec;
melunko@313
   270
	p->recendts->tv_usec = p->endts->tv_usec;
melunko@313
   271
melunko@313
   272
        p->lastmodified = g_new0 (GTimeVal, 1);
melunko@313
   273
	p->lastmodified->tv_sec  = p->startts->tv_sec;
melunko@313
   274
	p->lastmodified->tv_usec = p->startts->tv_usec;
melunko@313
   275
printf ("XXXX %s %d\n", __FUNCTION__, __LINE__);
leo_sobral@1
   276
    
leo_sobral@1
   277
        p->title = g_string_new (row[3]);
leo_sobral@1
   278
        p->subtitle = g_string_new (row[4]);
leo_sobral@1
   279
        p->description = g_string_new (row[5]);
leo_sobral@1
   280
        p->category = g_string_new (row[6]);
leo_sobral@1
   281
        p->chanstr = g_string_new (row[7]);
leo_sobral@1
   282
        p->chansign = g_string_new (row[8]);
leo_sobral@1
   283
        p->channame = g_string_new (row[9]);
rosfran@214
   284
        p->repeat = g_ascii_strtoull(row[10], NULL, 10);
rosfran@214
   285
        p->chancommfree = g_ascii_strtoull(row[11], NULL, 10);
leo_sobral@1
   286
        p->chanOutputFilters = g_string_new (row[12]);
leo_sobral@1
   287
        p->seriesid = g_string_new (row[13]);
leo_sobral@1
   288
        p->programid = g_string_new (row[14]);
leo_sobral@1
   289
        p->year = g_string_new (row[15]);
rosfran@214
   290
        p->stars = g_ascii_strtod(row[16], NULL);
leo_sobral@1
   291
melunko@313
   292
printf ("XXXX %s %d\n", __FUNCTION__, __LINE__);
leo_sobral@1
   293
        if (!row[17] || !strcmp(row[17], "")) {
leo_sobral@1
   294
            p->originalAirDate = 0;
leo_sobral@1
   295
            p->hasAirDate = FALSE;
leo_sobral@1
   296
        } else {
rosfran@214
   297
            p->originalAirDate = gmyth_util_string_to_time_val (row[17]);
leo_sobral@1
   298
            p->hasAirDate = TRUE;
leo_sobral@1
   299
        }
leo_sobral@1
   300
        
melunko@313
   301
printf ("XXXX %s %d\n", __FUNCTION__, __LINE__);
leo_sobral@1
   302
        p->catType = g_string_new (row[18]);
leo_sobral@1
   303
leo_sobral@1
   304
        *proglist = g_list_append((*proglist), p);
leo_sobral@49
   305
leo_sobral@49
   306
#if 0        
leo_sobral@49
   307
        gmyth_program_info_print(p);
leo_sobral@49
   308
#endif        
leo_sobral@1
   309
    }
leo_sobral@1
   310
leo_sobral@1
   311
    /* deallocate */
leo_sobral@1
   312
    mysql_free_result (res_set);
leo_sobral@1
   313
    g_string_free(querystr, TRUE);
leo_sobral@1
   314
leo_sobral@1
   315
    return TRUE;
leo_sobral@1
   316
}