gmyth/src/gmyth_epg.c
author leo_sobral
Thu Nov 30 22:24:05 2006 +0000 (2006-11-30)
branchtrunk
changeset 161 a2308f2e6ed0
parent 131 d1ae310096bd
child 162 27e0944605f2
permissions -rw-r--r--
[svn r162] testing
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@1
    28
leo_sobral@1
    29
#include <mysql.h>
leo_sobral@1
    30
#include <stdlib.h>
leo_sobral@1
    31
#include <string.h>
leo_sobral@1
    32
#include <assert.h>
leo_sobral@1
    33
leo_sobral@1
    34
#include "gmyth_epg.h"
leo_sobral@1
    35
#include "gmyth_util.h"
renatofilho@131
    36
#include "gmyth_debug.h"
leo_sobral@1
    37
leo_sobral@1
    38
static void gmyth_epg_class_init          (GMythEPGClass *klass);
leo_sobral@1
    39
static void gmyth_epg_init                (GMythEPG *object);
leo_sobral@1
    40
leo_sobral@1
    41
static void gmyth_epg_dispose  (GObject *object);
leo_sobral@1
    42
static void gmyth_epg_finalize (GObject *object);
leo_sobral@1
    43
leo_sobral@1
    44
G_DEFINE_TYPE(GMythEPG, gmyth_epg, G_TYPE_OBJECT)
leo_sobral@1
    45
    
leo_sobral@1
    46
static void
leo_sobral@1
    47
gmyth_epg_class_init (GMythEPGClass *klass)
leo_sobral@1
    48
{
leo_sobral@161
    49
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
leo_sobral@1
    50
	
leo_sobral@1
    51
    gobject_class->dispose  = gmyth_epg_dispose;
leo_sobral@1
    52
    gobject_class->finalize = gmyth_epg_finalize;	
leo_sobral@1
    53
}
leo_sobral@1
    54
leo_sobral@1
    55
static void
leo_sobral@1
    56
gmyth_epg_init (GMythEPG *gmyth_epg)
leo_sobral@1
    57
{
melunko@117
    58
leo_sobral@1
    59
}
leo_sobral@1
    60
leo_sobral@1
    61
static void
leo_sobral@1
    62
gmyth_epg_dispose  (GObject *object)
leo_sobral@1
    63
{
leo_sobral@1
    64
    //GMythEPG *gmyth_epg = GMYTH_EPG(object);
leo_sobral@161
    65
   
leo_sobral@1
    66
	G_OBJECT_CLASS (gmyth_epg_parent_class)->dispose (object);
leo_sobral@1
    67
}
leo_sobral@1
    68
leo_sobral@1
    69
static void
leo_sobral@1
    70
gmyth_epg_finalize (GObject *object)
leo_sobral@1
    71
{
melunko@117
    72
    g_signal_handlers_destroy (object);
leo_sobral@1
    73
leo_sobral@161
    74
    g_debug("CHEGOU!!!!");
leo_sobral@1
    75
    G_OBJECT_CLASS (gmyth_epg_parent_class)->finalize (object);
leo_sobral@1
    76
}
leo_sobral@1
    77
leo_sobral@1
    78
/**
leo_sobral@1
    79
 * Creates a new instance of GMythEPG.
leo_sobral@1
    80
 * 
leo_sobral@1
    81
 * @return a new instance of GMythEPG.
leo_sobral@1
    82
 */
leo_sobral@1
    83
GMythEPG*
leo_sobral@6
    84
gmyth_epg_new (void)
leo_sobral@1
    85
{
leo_sobral@1
    86
    GMythEPG *epg = GMYTH_EPG (g_object_new(GMYTH_EPG_TYPE, NULL));
leo_sobral@1
    87
leo_sobral@1
    88
    return epg;
leo_sobral@1
    89
}
leo_sobral@1
    90
leo_sobral@1
    91
/** Connects to the Mysql database in the backend. The backend address
leo_sobral@1
    92
 * is loaded from the GMythSettings instance.
leo_sobral@1
    93
 * 
leo_sobral@1
    94
 * @param gmyth_epg the GMythEPG instance to be connected.
leo_sobral@1
    95
 * @return true if connection was success, false if failed.
leo_sobral@1
    96
 */
leo_sobral@1
    97
gboolean
melunko@117
    98
gmyth_epg_connect (GMythEPG *gmyth_epg, GMythBackendInfo *backend_info)
leo_sobral@1
    99
{
melunko@117
   100
    assert(gmyth_epg);
leo_sobral@1
   101
melunko@117
   102
    if (gmyth_epg->sqlquery	== NULL) {
renatofilho@131
   103
	gmyth_debug ("[%s] Creating gmyth_query", __FUNCTION__);
melunko@117
   104
        gmyth_epg->sqlquery = gmyth_query_new ( );
melunko@117
   105
    }
leo_sobral@1
   106
melunko@117
   107
    if (!gmyth_query_connect(gmyth_epg->sqlquery, backend_info)) {
leo_sobral@1
   108
        g_warning ("[%s] Error while connecting to db", __FUNCTION__);
leo_sobral@1
   109
        return FALSE;
leo_sobral@1
   110
    }
leo_sobral@1
   111
leo_sobral@1
   112
    return TRUE;	
leo_sobral@1
   113
}
leo_sobral@1
   114
leo_sobral@1
   115
/** Disconnects from the Mysql database in the backend.
leo_sobral@1
   116
 * 
leo_sobral@1
   117
 * @param gmyth_epg the GMythEPG instance to be disconnected
leo_sobral@1
   118
 * @return true if disconnection was success, false if failed.
leo_sobral@1
   119
 */
leo_sobral@1
   120
gboolean
leo_sobral@1
   121
gmyth_epg_disconnect (GMythEPG *gmyth_epg)
leo_sobral@1
   122
{
leo_sobral@1
   123
	assert(gmyth_epg);
leo_sobral@1
   124
leo_sobral@1
   125
	if (gmyth_epg->sqlquery	!= NULL) {	
leo_sobral@1
   126
		g_object_unref (gmyth_epg->sqlquery);
leo_sobral@32
   127
        gmyth_epg->sqlquery = NULL;
leo_sobral@1
   128
	}
leo_sobral@1
   129
	
leo_sobral@1
   130
	return TRUE;
leo_sobral@1
   131
}
leo_sobral@1
   132
leo_sobral@1
   133
/** Retrieves the available list of channels from the backend Mysql database.
leo_sobral@1
   134
 * 
leo_sobral@1
   135
 * @param gmyth_epg the GMythEPG instance.
leo_sobral@1
   136
 * @param glist_ptr the GList pointer to be filled with the loaded list address.
leo_sobral@1
   137
 * @return The amount of channels retrieved from database,  or -1 if error.
leo_sobral@1
   138
 */
leo_sobral@1
   139
gint
leo_sobral@1
   140
gmyth_epg_get_channel_list (GMythEPG *gmyth_epg, GList **glist_ptr)
leo_sobral@1
   141
{
leo_sobral@1
   142
	MYSQL_RES *msql_res;
leo_sobral@1
   143
leo_sobral@1
   144
    assert(gmyth_epg);
leo_sobral@1
   145
leo_sobral@1
   146
    msql_res = gmyth_query_process_statement (gmyth_epg->sqlquery, 
leo_sobral@1
   147
    		"SELECT chanid,name FROM channel;");
leo_sobral@1
   148
leo_sobral@1
   149
	(*glist_ptr) = NULL;
leo_sobral@1
   150
	
leo_sobral@1
   151
    if (msql_res == NULL) {
leo_sobral@1
   152
        g_warning ("[%s] msql query returned NULL MYSQL_RES", __FUNCTION__);
leo_sobral@1
   153
		return -1;
leo_sobral@1
   154
    } else {
leo_sobral@1
   155
        MYSQL_ROW row;
leo_sobral@1
   156
		GMythChannelInfo *channel_info;        
leo_sobral@1
   157
leo_sobral@1
   158
        while ((row = mysql_fetch_row (msql_res)) != NULL){
leo_sobral@1
   159
leo_sobral@1
   160
        	channel_info = g_new0(GMythChannelInfo, 1);
leo_sobral@1
   161
            channel_info->channel_ID = atoi (row[0]);
leo_sobral@1
   162
            channel_info->channel_name = g_string_new (row[1]);
leo_sobral@49
   163
       
leo_sobral@49
   164
#if 0
leo_sobral@49
   165
            gmyth_channel_info_print(channel_info);
leo_sobral@49
   166
#endif
leo_sobral@49
   167
            
leo_sobral@1
   168
            (*glist_ptr) = g_list_append ((*glist_ptr), channel_info);
leo_sobral@1
   169
    	}
leo_sobral@1
   170
    }
leo_sobral@1
   171
    mysql_free_result (msql_res);
leo_sobral@1
   172
    return (!(*glist_ptr)) ?  0 : g_list_length (*glist_ptr);
leo_sobral@1
   173
}
leo_sobral@1
   174
leo_sobral@1
   175
/** 
leo_sobral@1
   176
 * Retrieves the available list of channels from the backend Mysql database.
leo_sobral@1
   177
 * 
leo_sobral@1
   178
 * @param gmyth_epg the GMythEPG instance.
leo_sobral@1
   179
 * @param proglist the GList pointer to be filled with the loaded list.
leo_sobral@1
   180
 * @param chan_num the channel num on which to search for program.
leo_sobral@1
   181
 * @param starttime the start time to search for programs.
leo_sobral@1
   182
 * @param endtime the end time to search for programs.
leo_sobral@1
   183
 * @return The amount of channels retrieved from database, or -1 if error.
leo_sobral@1
   184
 */
leo_sobral@1
   185
gint
leo_sobral@1
   186
gmyth_epg_get_program_list (GMythEPG *gmyth_epg, GList **proglist,
leo_sobral@1
   187
		const gint chan_num, time_t starttime, time_t endtime)
leo_sobral@1
   188
{
leo_sobral@1
   189
    GString *startts = gmyth_util_time_to_string(starttime);
leo_sobral@1
   190
    GString *endts = gmyth_util_time_to_string(endtime);
leo_sobral@1
   191
    MYSQL_ROW row;
leo_sobral@1
   192
    GString *querystr;
leo_sobral@1
   193
    
leo_sobral@1
   194
    assert(gmyth_epg);
leo_sobral@1
   195
    
leo_sobral@1
   196
    querystr = g_string_new(
leo_sobral@1
   197
        "SELECT DISTINCT program.chanid, program.starttime, program.endtime, "
leo_sobral@1
   198
        "    program.title, program.subtitle, program.description, "
leo_sobral@1
   199
        "    program.category, channel.channum, channel.callsign, "
leo_sobral@1
   200
        "    channel.name, program.previouslyshown, channel.commfree, "
leo_sobral@1
   201
        "    channel.outputfilters, program.seriesid, program.programid, "
leo_sobral@1
   202
        "    program.airdate, program.stars, program.originalairdate, "
leo_sobral@1
   203
        "    program.category_type, oldrecstatus.recordid, "
leo_sobral@1
   204
        "    oldrecstatus.rectype, oldrecstatus.recstatus, "
leo_sobral@1
   205
        "    oldrecstatus.findid "
leo_sobral@1
   206
        "FROM program "
leo_sobral@1
   207
        "LEFT JOIN channel ON program.chanid = channel.chanid "
leo_sobral@1
   208
        "LEFT JOIN oldrecorded AS oldrecstatus ON "
leo_sobral@1
   209
        "    program.title = oldrecstatus.title AND "
leo_sobral@1
   210
        "    channel.callsign = oldrecstatus.station AND "
leo_sobral@1
   211
        "    program.starttime = oldrecstatus.starttime "
leo_sobral@1
   212
        );
leo_sobral@1
   213
        
leo_sobral@1
   214
    g_string_append_printf (querystr, 
leo_sobral@1
   215
        "WHERE program.chanid = %d "
leo_sobral@1
   216
        "  AND program.endtime >= '%s' "
leo_sobral@1
   217
        "  AND program.starttime <= '%s' "
leo_sobral@1
   218
        "  AND program.manualid = 0 ",
leo_sobral@1
   219
        chan_num, startts->str, endts->str);
leo_sobral@1
   220
leo_sobral@1
   221
    if (!g_strrstr(querystr->str, " GROUP BY "))
leo_sobral@1
   222
        querystr = g_string_append(querystr,
leo_sobral@1
   223
            " GROUP BY program.starttime, channel.channum, "
leo_sobral@1
   224
            "  channel.callsign, program.title ");
leo_sobral@1
   225
leo_sobral@1
   226
    if (!g_strrstr(querystr->str, " LIMIT "))
leo_sobral@1
   227
        querystr = g_string_append(querystr, " LIMIT 1000 ");
leo_sobral@1
   228
leo_sobral@1
   229
    MYSQL_RES *res_set = 
leo_sobral@1
   230
        gmyth_query_process_statement(gmyth_epg->sqlquery, querystr->str);
leo_sobral@1
   231
leo_sobral@1
   232
    if (res_set == NULL) {
leo_sobral@1
   233
        g_warning ("[%s] msql query returned NULL MYSQL_RES", __FUNCTION__);
leo_sobral@1
   234
		return -1;
leo_sobral@1
   235
    }
leo_sobral@1
   236
leo_sobral@1
   237
    (*proglist) = NULL;        
leo_sobral@1
   238
    while ((row = mysql_fetch_row (res_set)) != NULL) {
leo_sobral@1
   239
leo_sobral@1
   240
        GMythProgramInfo *p = g_new0 (GMythProgramInfo, 1);
leo_sobral@1
   241
        p->chanid = g_string_new (row[0]);
leo_sobral@1
   242
leo_sobral@1
   243
        p->startts = gmyth_util_string_to_time (g_string_new (row[1]));
leo_sobral@1
   244
        p->endts = gmyth_util_string_to_time (g_string_new (row[2]));
leo_sobral@1
   245
                                                     
leo_sobral@1
   246
        p->recstartts = p->startts;
leo_sobral@1
   247
        p->recendts = p->endts;
leo_sobral@1
   248
        p->lastmodified = p->startts;
leo_sobral@1
   249
    
leo_sobral@1
   250
        p->title = g_string_new (row[3]);
leo_sobral@1
   251
        p->subtitle = g_string_new (row[4]);
leo_sobral@1
   252
        p->description = g_string_new (row[5]);
leo_sobral@1
   253
        p->category = g_string_new (row[6]);
leo_sobral@1
   254
        p->chanstr = g_string_new (row[7]);
leo_sobral@1
   255
        p->chansign = g_string_new (row[8]);
leo_sobral@1
   256
        p->channame = g_string_new (row[9]);
leo_sobral@1
   257
        p->repeat = atoi(row[10]);
leo_sobral@1
   258
        p->chancommfree = atoi(row[11]);
leo_sobral@1
   259
        p->chanOutputFilters = g_string_new (row[12]);
leo_sobral@1
   260
        p->seriesid = g_string_new (row[13]);
leo_sobral@1
   261
        p->programid = g_string_new (row[14]);
leo_sobral@1
   262
        p->year = g_string_new (row[15]);
leo_sobral@1
   263
        p->stars = atof(row[16]);
leo_sobral@1
   264
leo_sobral@1
   265
        if (!row[17] || !strcmp(row[17], "")) {
leo_sobral@1
   266
            p->originalAirDate = 0;
leo_sobral@1
   267
            p->hasAirDate = FALSE;
leo_sobral@1
   268
        } else {
leo_sobral@1
   269
            p->originalAirDate = gmyth_util_string_to_time (g_string_new (row[17]));
leo_sobral@1
   270
            p->hasAirDate = TRUE;
leo_sobral@1
   271
        }
leo_sobral@1
   272
        
leo_sobral@1
   273
        p->catType = g_string_new (row[18]);
leo_sobral@1
   274
leo_sobral@1
   275
        *proglist = g_list_append((*proglist), p);
leo_sobral@49
   276
leo_sobral@49
   277
#if 0        
leo_sobral@49
   278
        gmyth_program_info_print(p);
leo_sobral@49
   279
#endif        
leo_sobral@1
   280
    }
leo_sobral@1
   281
leo_sobral@1
   282
    /* deallocate */
leo_sobral@1
   283
    mysql_free_result (res_set);
leo_sobral@1
   284
    g_string_free(querystr, TRUE);
leo_sobral@1
   285
leo_sobral@1
   286
    return TRUE;
leo_sobral@1
   287
}