branches/gmyth-0.1b/src/gmyth_scheduler.c
branchtrunk
changeset 329 818deb9ae65d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/branches/gmyth-0.1b/src/gmyth_scheduler.c	Wed Feb 07 00:03:05 2007 +0000
     1.3 @@ -0,0 +1,667 @@
     1.4 +/**
     1.5 + * GMyth Library
     1.6 + *
     1.7 + * @file gmyth/gmyth_scheduler.c
     1.8 + * 
     1.9 + * @brief <p> The scheduler encapsulates all functions for browsing, scheduling
    1.10 + * and modifying the recorded content.
    1.11 + *
    1.12 + * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
    1.13 + * @author Alexsandro Jose Virginio dos Santos <alexsandro.santos@indt.org.br>
    1.14 + *
    1.15 + *//*
    1.16 + * 
    1.17 + * This program is free software; you can redistribute it and/or modify
    1.18 + * it under the terms of the GNU Lesser General Public License as published by
    1.19 + * the Free Software Foundation; either version 2 of the License, or
    1.20 + * (at your option) any later version.
    1.21 + *
    1.22 + * This program is distributed in the hope that it will be useful,
    1.23 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.24 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.25 + * GNU General Public License for more details.
    1.26 + *
    1.27 + * You should have received a copy of the GNU Lesser General Public License
    1.28 + * along with this program; if not, write to the Free Software
    1.29 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.30 + */
    1.31 + 
    1.32 +#ifdef HAVE_CONFIG_H
    1.33 +#include "config.h"
    1.34 +#endif
    1.35 +
    1.36 +#include <assert.h>
    1.37 +
    1.38 +#include <glib/gprintf.h>
    1.39 +
    1.40 +#include "gmyth_scheduler.h"
    1.41 +#include "gmyth_util.h"
    1.42 +#include "gmyth_query.h"
    1.43 +#include "gmyth_socket.h"
    1.44 +#include "gmyth_debug.h"
    1.45 +
    1.46 +static void gmyth_scheduler_class_init  (GMythSchedulerClass *klass);
    1.47 +static void gmyth_scheduler_init        (GMythScheduler *object);
    1.48 +
    1.49 +static void gmyth_scheduler_dispose  (GObject *object);
    1.50 +static void gmyth_scheduler_finalize (GObject *object);
    1.51 +
    1.52 +static gint get_record_id_from_database (GMythScheduler *scheduler);
    1.53 +static void update_backend  (GMythScheduler *scheduler, gint record_id);
    1.54 +
    1.55 +G_DEFINE_TYPE(GMythScheduler, gmyth_scheduler, G_TYPE_OBJECT)
    1.56 +    
    1.57 +static void
    1.58 +gmyth_scheduler_class_init (GMythSchedulerClass *klass)
    1.59 +{
    1.60 +    GObjectClass *gobject_class;
    1.61 +
    1.62 +    gobject_class = (GObjectClass *) klass;
    1.63 +
    1.64 +    gobject_class->dispose  = gmyth_scheduler_dispose;
    1.65 +    gobject_class->finalize = gmyth_scheduler_finalize;	
    1.66 +}
    1.67 +
    1.68 +static void
    1.69 +gmyth_scheduler_init (GMythScheduler *sched)
    1.70 +{
    1.71 +    sched->recordid =0;
    1.72 +    sched->type = 0;
    1.73 +    sched->search = 0;
    1.74 +    sched->profile =  g_string_new("");
    1.75 +    
    1.76 +    sched->dupin = 0;
    1.77 +    sched->dupmethod = 0;
    1.78 +    sched->autoexpire = 0;
    1.79 +    sched->autotranscode = 0;
    1.80 +    sched->transcoder = 0;
    1.81 +
    1.82 +    sched->autocommflag = 0;
    1.83 +    sched->autouserjob1 = 0;
    1.84 +    sched->autouserjob2 = 0;
    1.85 +    sched->autouserjob3 = 0;
    1.86 +    sched->autouserjob4 = 0;
    1.87 +    
    1.88 +    sched->startoffset = 0;
    1.89 +    sched->endoffset = 0;
    1.90 +    sched->maxepisodes = 0;
    1.91 +    sched->maxnewest = 0;
    1.92 +
    1.93 +    sched->recpriority = 0;
    1.94 +    sched->recgroup = 0;
    1.95 +    sched->playgroup = 0;
    1.96 +    
    1.97 +    sched->prefinput = 0;
    1.98 +    sched->inactive = 0;
    1.99 +    
   1.100 +    sched->searchType = g_string_new("");
   1.101 +    sched->searchForWhat = g_string_new("");
   1.102 +    
   1.103 +    sched->msqlquery = gmyth_query_new ();
   1.104 +}
   1.105 +
   1.106 +static void
   1.107 +gmyth_scheduler_dispose  (GObject *object)
   1.108 +{
   1.109 +    GMythScheduler *scheduler = GMYTH_SCHEDULER (object);
   1.110 +
   1.111 +    if (scheduler->backend_info) { 
   1.112 +	g_object_unref (scheduler->backend_info);
   1.113 +	scheduler->backend_info = NULL;
   1.114 +    }
   1.115 +
   1.116 +    G_OBJECT_CLASS (gmyth_scheduler_parent_class)->dispose (object);
   1.117 +}
   1.118 +
   1.119 +static void
   1.120 +gmyth_scheduler_finalize (GObject *object)
   1.121 +{
   1.122 +    g_signal_handlers_destroy (object);
   1.123 +
   1.124 +    G_OBJECT_CLASS (gmyth_scheduler_parent_class)->finalize (object);
   1.125 +}
   1.126 +
   1.127 +/** Creates a new instance of GMythScheduler.
   1.128 + * 
   1.129 + * @return a new instance of GMythScheduler.
   1.130 + */
   1.131 +GMythScheduler*
   1.132 +gmyth_scheduler_new ()
   1.133 +{
   1.134 +    GMythScheduler *scheduler = 
   1.135 +        GMYTH_SCHEDULER (g_object_new(GMYTH_SCHEDULER_TYPE, NULL));
   1.136 +    
   1.137 +    return scheduler;
   1.138 +}
   1.139 +
   1.140 +gboolean
   1.141 +gmyth_scheduler_connect (GMythScheduler *scheduler, GMythBackendInfo *backend_info)
   1.142 +{
   1.143 +    return gmyth_scheduler_connect_with_timeout (scheduler, backend_info, 0);
   1.144 +}
   1.145 +
   1.146 +/** Connects to the Mysql database in the backend. The backend address
   1.147 + * is loaded from the GMythSettings instance.
   1.148 + * 
   1.149 + * @param scheduler the GMythScheduler instance to be connected.
   1.150 + * @return true if connection was success, false if failed.
   1.151 + */
   1.152 +gboolean
   1.153 +gmyth_scheduler_connect_with_timeout (GMythScheduler *scheduler,
   1.154 +	       GMythBackendInfo *backend_info, guint timeout)
   1.155 +{
   1.156 +    assert(scheduler);
   1.157 +    g_return_val_if_fail (backend_info != NULL, FALSE);
   1.158 +    
   1.159 +    g_object_ref (backend_info);
   1.160 +    scheduler->backend_info = backend_info;
   1.161 +
   1.162 +    if (scheduler->msqlquery == NULL) {
   1.163 +        g_warning ("[%s] GMythScheduler db initializing", __FUNCTION__);
   1.164 +        scheduler->msqlquery = gmyth_query_new ();		
   1.165 +    }
   1.166 +
   1.167 +    if (!gmyth_query_connect_with_timeout (scheduler->msqlquery, 
   1.168 +			    scheduler->backend_info, timeout)) {
   1.169 +    	g_warning ("[%s] Error while connecting to db", __FUNCTION__);
   1.170 +        return FALSE;
   1.171 +    }
   1.172 +
   1.173 +    return TRUE;
   1.174 +}
   1.175 +
   1.176 +/** Disconnects from the Mysql database in the backend.
   1.177 + * 
   1.178 + * @param scheduler the GMythScheduler instance to be disconnected
   1.179 + * @return true if disconnection was success, false if failed.
   1.180 + */
   1.181 +gboolean
   1.182 +gmyth_scheduler_disconnect (GMythScheduler *scheduler)
   1.183 +{
   1.184 +    assert(scheduler);
   1.185 +
   1.186 +    if (scheduler->msqlquery != NULL) {
   1.187 +        gmyth_query_disconnect (scheduler->msqlquery);
   1.188 +        g_object_unref (scheduler->msqlquery);
   1.189 +    }
   1.190 +
   1.191 +    return TRUE;
   1.192 +}
   1.193 +
   1.194 +/** Retrieves from the backend Mysql database the list of recording schedules.
   1.195 + * 
   1.196 + * @param scheduler The GMythScheduler instance.
   1.197 + * @param schedule_list the GList pointer to be filled with the loaded list of ScheduleInfo items.
   1.198 + * @return The amount of schedules retrieved from database, or -1 if error.
   1.199 + */
   1.200 +gint
   1.201 +gmyth_scheduler_get_schedule_list ( GMythScheduler *scheduler, GList **schedule_list)
   1.202 +{
   1.203 +    ScheduleInfo *schedule;
   1.204 +    MYSQL_RES *msql_res;
   1.205 +    GString *query_str = g_string_new ("");
   1.206 +    gchar *date_time = NULL;
   1.207 +    
   1.208 +    assert(scheduler);
   1.209 +    
   1.210 +    g_string_printf (query_str, 
   1.211 +    	"SELECT recordid,programid,chanid,starttime,startdate,"
   1.212 +    	"endtime,enddate,title,subtitle,description,category FROM record;");
   1.213 +
   1.214 +    if (scheduler->msqlquery == NULL) {
   1.215 +	g_warning ("[%s] Scheduler db connection not initialized", __FUNCTION__);
   1.216 +	return -1;
   1.217 +    }
   1.218 +    msql_res = gmyth_query_process_statement (scheduler->msqlquery, query_str->str);
   1.219 +
   1.220 +    if (msql_res == NULL) {
   1.221 +        g_warning ("DB retrieval of schedule list failed");
   1.222 +        return -1;
   1.223 +    } else {
   1.224 +        MYSQL_ROW row;
   1.225 +        *schedule_list = NULL;
   1.226 +        
   1.227 +        while((row = mysql_fetch_row (msql_res)) != NULL) {
   1.228 +        	schedule = g_new0(ScheduleInfo, 1);
   1.229 +        	
   1.230 +        	schedule->record_id  = g_ascii_strtoull (row[0], NULL, 10);
   1.231 +        	schedule->program_id = g_ascii_strtoull (row[1], NULL, 10);
   1.232 +            schedule->channel_id = g_ascii_strtoull (row[2], NULL, 10);
   1.233 +            
   1.234 +            /* generate a time_t from a time and a date db field */
   1.235 +            g_sprintf (date_time, "%sT%s", row[4], row[3]);
   1.236 +            
   1.237 +            schedule->start_time = gmyth_util_string_to_time_val (date_time);
   1.238 +            
   1.239 +            /* generate a time_t from a time and a date db field */
   1.240 +            g_sprintf (date_time, "%sT%s", row[6], row[5]);
   1.241 +            
   1.242 +            schedule->end_time = gmyth_util_string_to_time_val (date_time);
   1.243 +
   1.244 +            schedule->title = g_string_new (row[7]);
   1.245 +            schedule->subtitle = g_string_new (row[8]);
   1.246 +            schedule->description = g_string_new (row[9]);
   1.247 +            schedule->category = g_string_new (row[10]);
   1.248 +
   1.249 +           (*schedule_list) = g_list_append (*(schedule_list), schedule);
   1.250 +    	}
   1.251 +    }
   1.252 +
   1.253 +    mysql_free_result (msql_res);
   1.254 +    g_string_free(query_str, TRUE);
   1.255 +    g_free(date_time);
   1.256 +
   1.257 +    return (*schedule_list == NULL) ? 0 : g_list_length (*schedule_list);
   1.258 +}
   1.259 +
   1.260 +/** Retrieves from the backend Mysql database the list of recorded programs.
   1.261 + * 
   1.262 + * @param scheduler The GMythScheduler instance.
   1.263 + * @param recorded_list the GList pointer to be filled with the loaded RecordInfo items.
   1.264 + * @return The amount of recorded retrieved from database, or -1 if error.
   1.265 + */
   1.266 +gint
   1.267 +gmyth_scheduler_get_recorded_list (GMythScheduler *scheduler, GList **recorded_list)
   1.268 +{
   1.269 +    RecordedInfo *record;
   1.270 +    MYSQL_RES *msql_res;
   1.271 +    GString *query_str = g_string_new ("");
   1.272 +	
   1.273 +    assert(scheduler);
   1.274 +    
   1.275 +    g_string_printf (query_str, 
   1.276 +    	"SELECT recordid,programid,chanid,starttime,progstart,"
   1.277 +    	"endtime,progend,title,subtitle,description,category,filesize,basename FROM recorded WHERE recgroup != 'LiveTV'");
   1.278 +
   1.279 +    if (scheduler->msqlquery == NULL) {
   1.280 +	g_warning ("[%s] Scheduler db connection not initialized", __FUNCTION__);
   1.281 +	return -1;
   1.282 +    }
   1.283 +
   1.284 +    msql_res = gmyth_query_process_statement (scheduler->msqlquery, query_str->str);
   1.285 +
   1.286 +    if (msql_res == NULL) {
   1.287 +        g_warning ("DB retrieval of recording list failed");
   1.288 +        return -1;
   1.289 +    } else {
   1.290 +        MYSQL_ROW row;
   1.291 +        *recorded_list = NULL;
   1.292 +        
   1.293 +        while((row = mysql_fetch_row (msql_res))!=NULL){
   1.294 +       	    record = g_new0(RecordedInfo, 1);
   1.295 +            
   1.296 +            record->record_id  = (guint) g_ascii_strtoull (row[0], NULL, 10);
   1.297 +            record->program_id = (guint) g_ascii_strtoull (row[1], NULL, 10);
   1.298 +            record->channel_id = (guint) g_ascii_strtoull (row[2], NULL, 10);
   1.299 +            
   1.300 +            record->start_time = gmyth_util_string_to_time_val (row[3]);
   1.301 +            record->end_time = gmyth_util_string_to_time_val (row[5]);
   1.302 +	
   1.303 +            record->title = g_string_new (row[7]);
   1.304 +            record->subtitle = g_string_new (row[8]);
   1.305 +            record->description = g_string_new (row[9]);
   1.306 +            record->category = g_string_new (row[10]);
   1.307 +            record->filesize = g_ascii_strtoull (row[11], NULL, 10);
   1.308 +            record->basename = g_string_new (row[12]);
   1.309 +
   1.310 + 	    *recorded_list = g_list_append (*recorded_list, record);
   1.311 +    	}
   1.312 +    }
   1.313 +    
   1.314 +    mysql_free_result (msql_res);
   1.315 +    g_string_free(query_str, TRUE);
   1.316 +
   1.317 +    return (*recorded_list == NULL) ? 0 : g_list_length (*recorded_list);
   1.318 +}
   1.319 +
   1.320 +/** Requests the Mysql database in the backend to add a new schedule.
   1.321 + * 
   1.322 + * @param scheduler the GMythScheduler instance.
   1.323 + * @param schedule_info the ScheduleInfo with recording schedule information
   1.324 + * to be added. record_id = -1 to add a new schedule, otherwise this
   1.325 + * function will update the schedule in the db
   1.326 + * @return gboolean returns FALSE if some error occurs, TRUE otherwise
   1.327 + */
   1.328 +gboolean
   1.329 +gmyth_scheduler_add_schedule (GMythScheduler *scheduler,
   1.330 +                              ScheduleInfo *schedule_info)
   1.331 +{
   1.332 +    //GTimeVal *start_tm;
   1.333 +    //GTimeVal *end_tm;
   1.334 +
   1.335 +    MYSQL_RES *msql_res;
   1.336 +    GString *query_str = g_string_new ("");
   1.337 +    
   1.338 +    gchar *date_time = NULL; 
   1.339 +    
   1.340 +    assert(scheduler);
   1.341 +    
   1.342 +    if (scheduler->msqlquery == NULL) {
   1.343 +	g_warning ("[%s] Scheduler db connection not initialized", __FUNCTION__);
   1.344 +	return FALSE;
   1.345 +    }
   1.346 +	
   1.347 +    //TODO: verify if this funtion realy does what it should do!
   1.348 +    g_string_printf (query_str, "REPLACE INTO record "
   1.349 +	    "(recordid, type, chanid, starttime, "
   1.350 +	    "startdate, endtime, enddate, title,"
   1.351 +	    "profile, recpriority, maxnewest, inactive, "
   1.352 +	    "maxepisodes, autoexpire, startoffset, endoffset, "
   1.353 +	    "recgroup, dupmethod, dupin, station, "
   1.354 +	    "autocommflag, findday, findtime, findid, "
   1.355 +	    "search, autotranscode, transcoder, tsdefault, "
   1.356 +	    "autouserjob1, autouserjob2, autouserjob3, autouserjob4) "
   1.357 +	    " values ( %d, 1, %d, \"%s\","	//recordid, type, chanid, starttime
   1.358 +	    " \"%s\", \"%s\", \"%s\", \"%s\","
   1.359 +        //startdate, endtime, enddate, title
   1.360 +	    "DEFAULT, 0, 0, 0, "	//profile, recpriority, maxnewest, inactive
   1.361 +	    "0, 1, 0, 0, "			//maxepisodes, autoexpire, startoffset, endoffset
   1.362 +	    "DEFAULT, 6, 15, %d, " 	//recgroup, dupmethod, dupin, station
   1.363 +	    "1, %d, \"%s\", %d, "	//autocommflag, findday, findtime, findid
   1.364 +	    "5, 0, 29, 1, "			//search, autotranscode, transcoder, tsdefault
   1.365 +	    "0, 0, 0, 0 );",		//autouserjob1, autouserjob2, autouserjob3, autouserjob4
   1.366 +	    schedule_info->record_id, schedule_info->channel_id,
   1.367 +        gmyth_util_time_to_string_only_time( schedule_info->start_time ),
   1.368 +	    	gmyth_util_time_to_string_only_date( schedule_info->start_time ),
   1.369 +        gmyth_util_time_to_string_only_time( schedule_info->end_time ),
   1.370 +        gmyth_util_time_to_string_only_date( schedule_info->end_time ), 
   1.371 +        schedule_info->title->str, //title
   1.372 +    	schedule_info->channel_id,//station
   1.373 +    	(gmyth_util_time_val_to_date( schedule_info->start_time ))->tm_wday, //findday
   1.374 +   		gmyth_util_time_to_string_only_time( schedule_info->start_time ), //findtime
   1.375 +   		(gint)(schedule_info->start_time->tv_sec/60/60/24 + 719528)//findid
   1.376 +    );
   1.377 +    
   1.378 +    gmyth_debug ( "Sending query to MySQL = %s.", query_str->str );
   1.379 +
   1.380 +    msql_res = gmyth_query_process_statement (scheduler->msqlquery, query_str->str);
   1.381 +    
   1.382 +    /* FIXME: currently no way to detect db error in UPDATE, REPLACES!
   1.383 +    if (msql_res == NULL) {
   1.384 +        g_warning ("DB retrieval of recording list failed");
   1.385 +        return -1;
   1.386 +	}*/
   1.387 +    
   1.388 +    /* TODO: verify record_id = -1 semantics */
   1.389 +    if (schedule_info->record_id <= 0)
   1.390 +	    schedule_info->record_id = get_record_id_from_database(scheduler);
   1.391 +    
   1.392 +    /* Notify the backend of changes */
   1.393 +    update_backend(scheduler, schedule_info->record_id);
   1.394 +    
   1.395 +    /* free allocated memory */
   1.396 +    mysql_free_result (msql_res);
   1.397 +    g_string_free(query_str, TRUE);
   1.398 +    
   1.399 +    return 1;
   1.400 +}
   1.401 +
   1.402 +/** Requests the Mysql database in the backend to remove an existing schedule.
   1.403 + * 
   1.404 + * @param scheduler the GMythScheduler instance.
   1.405 + * @param record_id The schedule's record id to be removed
   1.406 + * @return gboolean TRUE if success, FALSE if error
   1.407 + */
   1.408 +gboolean
   1.409 +gmyth_scheduler_delete_schedule (GMythScheduler *scheduler, gint record_id)
   1.410 +{
   1.411 +
   1.412 +    MYSQL_RES *msql_res;
   1.413 +    GString *query_str = g_string_new ("");
   1.414 +
   1.415 +    assert(scheduler);
   1.416 +    
   1.417 +    if (scheduler->msqlquery == NULL) {
   1.418 +	g_warning ("[%s] Scheduler db connection not initialized", __FUNCTION__);
   1.419 +	return FALSE;
   1.420 +    }
   1.421 +
   1.422 +    //========================================
   1.423 +    g_string_printf (query_str, 
   1.424 +    	"DELETE FROM record WHERE recordid=%d", record_id);
   1.425 +
   1.426 +    msql_res = gmyth_query_process_statement (scheduler->msqlquery, query_str->str);
   1.427 +
   1.428 +    if (msql_res == NULL) {
   1.429 +    	g_warning ("[%s] Error while trying to delete a schedule in the database", __FUNCTION__);
   1.430 +    	return FALSE;
   1.431 +    }
   1.432 +
   1.433 +    update_backend(scheduler, record_id);// Notify the backend of the changes
   1.434 +    
   1.435 +    mysql_free_result (msql_res);
   1.436 +    g_string_free(query_str, TRUE);
   1.437 +
   1.438 +    return TRUE;
   1.439 +}
   1.440 +
   1.441 +/** Requests the Mysql database in the backend to remove an existing recorded item.
   1.442 + * 
   1.443 + * @param scheduler the GMythScheduler instance.
   1.444 + * @param record_id The recorded item id to be removed
   1.445 + * @return gboolean TRUE if success, FALSE if error
   1.446 + */
   1.447 +gboolean
   1.448 +gmyth_scheduler_delete_recorded (GMythScheduler *scheduler, gint record_id)
   1.449 +{
   1.450 +
   1.451 +    MYSQL_RES *msql_res;
   1.452 +
   1.453 +    GString *query_str = g_string_new ("");
   1.454 +
   1.455 +    assert(scheduler);
   1.456 +    
   1.457 +	if (scheduler->msqlquery == NULL) {
   1.458 +		g_warning ("[%s] Scheduler db connection not initialized", __FUNCTION__);
   1.459 +		return FALSE;
   1.460 +	}
   1.461 +
   1.462 +    //========================================
   1.463 +    g_string_printf (query_str, 
   1.464 +    	"DELETE FROM recorded WHERE recordid=%d", record_id);
   1.465 +
   1.466 +    msql_res = gmyth_query_process_statement (scheduler->msqlquery, query_str->str);
   1.467 +
   1.468 +    update_backend(scheduler, record_id);// Notify the backend of the changes
   1.469 +    
   1.470 +    mysql_free_result (msql_res);
   1.471 +    g_string_free(query_str, TRUE);
   1.472 +
   1.473 +    return TRUE;
   1.474 +}
   1.475 +
   1.476 +/** Retrieves an existing recorded item information from database. The information
   1.477 + * is used to fill the returned GMythProgramInfo.
   1.478 + * 
   1.479 + * @param scheduler The GMythScheduler instance.
   1.480 + * @param channel The channel associated to the record
   1.481 + * @param starttime The record start time
   1.482 + * @return A GMythProgramInfo struct with the requested record item
   1.483 + * information, or NULL if error.
   1.484 + */
   1.485 +GMythProgramInfo*
   1.486 +gmyth_scheduler_get_recorded (GMythScheduler *scheduler, 
   1.487 +                              GString *channel, GTimeVal* starttime)
   1.488 +{
   1.489 +	MYSQL_RES *msql_res;
   1.490 +	GMythProgramInfo *proginfo = NULL;
   1.491 +	GString *query_str = g_string_new("");
   1.492 +	gchar *time_str = gmyth_util_time_to_string_from_time_val (starttime);
   1.493 +
   1.494 +    assert(scheduler);
   1.495 +    
   1.496 +	if (scheduler->msqlquery == NULL) {
   1.497 +		g_warning ("[%s] Scheduler db connection not initialized", __FUNCTION__);
   1.498 +		return NULL;
   1.499 +	}
   1.500 +
   1.501 +	g_string_printf (query_str, "SELECT recorded.chanid,starttime,endtime,title, "
   1.502 +                  "subtitle,description,channel.channum, "
   1.503 +                  "channel.callsign,channel.name,channel.commfree, "
   1.504 +                  "channel.outputfilters,seriesid,programid,filesize, "
   1.505 +                  "lastmodified,stars,previouslyshown,originalairdate, "
   1.506 +                  "hostname,recordid,transcoder,playgroup, "
   1.507 +                  "recorded.recpriority,progstart,progend,basename,recgroup "
   1.508 +                  "FROM recorded "
   1.509 +                  "LEFT JOIN channel "
   1.510 +                  "ON recorded.chanid = channel.chanid "
   1.511 +                  "WHERE recorded.chanid = \"%s\" "
   1.512 +                  "AND starttime = \"%s\" ;",
   1.513 +                  channel->str, time_str);
   1.514 +
   1.515 +    msql_res = gmyth_query_process_statement (scheduler->msqlquery, query_str->str);
   1.516 +
   1.517 +    if (msql_res /*&& query.size() > 0*/) {
   1.518 +    	
   1.519 +    	MYSQL_ROW msql_row = mysql_fetch_row (msql_res);
   1.520 +    	if (msql_row) {
   1.521 +
   1.522 +    		proginfo = gmyth_program_info_new();
   1.523 +    	
   1.524 +	        proginfo->chanid = g_string_new (msql_row[0]);
   1.525 +	        proginfo->startts = gmyth_util_string_to_time_val (msql_row[23]);
   1.526 +	        proginfo->endts = gmyth_util_string_to_time_val (msql_row[24]);
   1.527 +	        proginfo->recstartts = gmyth_util_string_to_time_val (msql_row[1]);
   1.528 +	        proginfo->recendts = gmyth_util_string_to_time_val (msql_row[2]);
   1.529 +	        proginfo->title = g_string_new (msql_row[3]);
   1.530 +	        proginfo->subtitle = g_string_new (msql_row[4]);
   1.531 +	        proginfo->description = g_string_new (msql_row[5]);
   1.532 +	
   1.533 +	        proginfo->chanstr = g_string_new (msql_row[6]);
   1.534 +	        proginfo->chansign = g_string_new (msql_row[7]);
   1.535 +	        proginfo->channame = g_string_new (msql_row[0]);
   1.536 +	        proginfo->chancommfree = g_ascii_strtoull (msql_row[9], NULL, 10);
   1.537 +	        proginfo->chanOutputFilters = g_string_new (msql_row[10]);
   1.538 +	        proginfo->seriesid = g_string_new (msql_row[11]);
   1.539 +	        proginfo->programid = g_string_new (msql_row[12]);
   1.540 +	        proginfo->filesize = g_ascii_strtoull (msql_row[13], NULL, 10);
   1.541 +	
   1.542 +	        proginfo->lastmodified = gmyth_util_string_to_time_val (msql_row[14]);
   1.543 +	        
   1.544 +	        proginfo->stars = g_ascii_strtod (msql_row[15], NULL);
   1.545 +	        proginfo->repeat = g_ascii_strtoull (msql_row[16], NULL, 10);
   1.546 +	        
   1.547 +	        if (msql_row[17] == NULL) {
   1.548 +	            proginfo->originalAirDate = 0;
   1.549 +	            proginfo->hasAirDate = FALSE;
   1.550 +	        } else {
   1.551 +	            proginfo->originalAirDate = gmyth_util_string_to_time_val (msql_row[17]);
   1.552 +	            proginfo->hasAirDate = TRUE;
   1.553 +	        }
   1.554 +	        
   1.555 +	        proginfo->hostname = g_string_new (msql_row[18]);
   1.556 +	        proginfo->recordid = g_ascii_strtoull (msql_row[19], NULL, 10);
   1.557 +	        proginfo->transcoder = g_ascii_strtoull (msql_row[20], NULL, 10);
   1.558 +	
   1.559 +	        //proginfo->spread = -1;
   1.560 +			//proginfo->programflags = proginfo->getProgramFlags();
   1.561 +	
   1.562 +	        proginfo->recgroup = g_string_new (msql_row[26]);
   1.563 +	        proginfo->playgroup = g_string_new (msql_row[21]);
   1.564 +	        proginfo->recpriority = g_ascii_strtoull (msql_row[22], NULL, 10);
   1.565 +	
   1.566 +	        proginfo->pathname = g_string_new (msql_row[25]);
   1.567 +
   1.568 +	        gmyth_debug ("One program info loaded from mysql database\n");
   1.569 +    	}
   1.570 +    }
   1.571 +
   1.572 +    mysql_free_result (msql_res);
   1.573 +    g_string_free(query_str, TRUE);
   1.574 +    g_free(time_str);
   1.575 +
   1.576 +    return proginfo;
   1.577 +}
   1.578 +
   1.579 +/** Retrieves the next record id.
   1.580 + * 
   1.581 + * @param scheduler The GMythScheduler instance.
   1.582 + * @return gint record_id if success, -1 otherwise 
   1.583 + */
   1.584 +static gint
   1.585 +get_record_id_from_database (GMythScheduler *scheduler)
   1.586 +{
   1.587 +    gint record_id;
   1.588 +
   1.589 +    assert(scheduler);
   1.590 +    
   1.591 +	if (scheduler->msqlquery == NULL) {
   1.592 +		g_warning ("[%s] Scheduler db connection not initialized", __FUNCTION__);
   1.593 +		return 0;
   1.594 +	}
   1.595 +
   1.596 +    record_id = mysql_insert_id (scheduler->msqlquery->conn);
   1.597 +
   1.598 +	return record_id;
   1.599 +}	
   1.600 +	
   1.601 +/** Notifies the backend of an update in the db.
   1.602 + * 
   1.603 + * @param record_id the id of the modified recording.
   1.604 + */
   1.605 +static void
   1.606 +update_backend(GMythScheduler *scheduler, gint record_id)//fixme: put void and discovery record_id inside
   1.607 +{
   1.608 +    GMythSocket *socket;
   1.609 +    GMythStringList *strlist = gmyth_string_list_new ();
   1.610 +    GString *datastr = g_string_new ("RESCHEDULE_RECORDINGS ");
   1.611 +
   1.612 +    g_string_append_printf (datastr, "%d", record_id);
   1.613 +    gmyth_string_list_append_string (strlist, datastr);
   1.614 +
   1.615 +    socket = gmyth_socket_new ();
   1.616 +    if (gmyth_socket_connect (socket, scheduler->backend_info->hostname,
   1.617 +			    scheduler->backend_info->port)) {
   1.618 +        gmyth_socket_sendreceive_stringlist (socket, strlist);
   1.619 +    } else {
   1.620 +	g_warning ("[%s] Connection to backend failed!", __FUNCTION__);
   1.621 +    }
   1.622 +    
   1.623 +    g_string_free(datastr, TRUE);
   1.624 +    g_object_unref(strlist);
   1.625 +}
   1.626 +
   1.627 +void
   1.628 +gmyth_scheduler_recorded_info_get_preview (RecordedInfo *info, GByteArray* data)
   1.629 +{
   1.630 +}
   1.631 +
   1.632 +void
   1.633 +gmyth_scheduler_recorded_info_free (RecordedInfo *info)
   1.634 +{
   1.635 +    if (info->title != NULL)
   1.636 +        g_string_free (info->title, TRUE);
   1.637 +
   1.638 +    if (info->subtitle != NULL)
   1.639 +        g_string_free (info->subtitle, TRUE);
   1.640 +
   1.641 +    if (info->description != NULL)
   1.642 +        g_string_free (info->description, TRUE);
   1.643 +
   1.644 +    if (info->category != NULL)
   1.645 +        g_string_free (info->category, TRUE);
   1.646 +
   1.647 +    if (info->basename != NULL)
   1.648 +        g_string_free (info->basename, TRUE);
   1.649 +
   1.650 +    g_free (info);
   1.651 +}
   1.652 +
   1.653 +void
   1.654 +gmyth_scheduler_schedule_info_free (ScheduleInfo *info)
   1.655 +{
   1.656 +    if (info->title != NULL)
   1.657 +        g_string_free (info->title, TRUE);
   1.658 +
   1.659 +    if (info->subtitle != NULL)
   1.660 +        g_string_free (info->subtitle, TRUE);
   1.661 +
   1.662 +    if (info->description != NULL)
   1.663 +        g_string_free (info->description, TRUE);
   1.664 +
   1.665 +    if (info->category != NULL)
   1.666 +        g_string_free (info->category, TRUE);
   1.667 +
   1.668 +    g_free (info);
   1.669 +}
   1.670 +