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