gmyth/src/gmyth_scheduler.c
author melunko
Wed Dec 06 17:59:09 2006 +0000 (2006-12-06)
branchtrunk
changeset 207 6b697111497b
parent 131 d1ae310096bd
child 213 631f2cf13501
permissions -rw-r--r--
[svn r208] Added filesize field at RecordedInfo
leo_sobral@1
     1
/**
leo_sobral@1
     2
 * GMyth Library
leo_sobral@1
     3
 *
leo_sobral@1
     4
 * @file gmyth/gmyth_scheduler.c
leo_sobral@1
     5
 * 
leo_sobral@1
     6
 * @brief <p> The scheduler encapsulates all functions for browsing, scheduling
leo_sobral@1
     7
 * and modifying the recorded content.
leo_sobral@1
     8
 *
leo_sobral@1
     9
 * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
leo_sobral@1
    10
 * @author Alexsandro Jose Virginio dos Santos <alexsandro.santos@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 <assert.h>
leo_sobral@1
    30
leo_sobral@1
    31
#include "gmyth_scheduler.h"
leo_sobral@1
    32
leo_sobral@1
    33
#include "gmyth_util.h"
leo_sobral@1
    34
#include "gmyth_query.h"
melunko@117
    35
#include "gmyth_socket.h"
renatofilho@131
    36
#include "gmyth_debug.h"
leo_sobral@1
    37
leo_sobral@49
    38
static void gmyth_scheduler_class_init  (GMythSchedulerClass *klass);
leo_sobral@49
    39
static void gmyth_scheduler_init        (GMythScheduler *object);
leo_sobral@1
    40
leo_sobral@1
    41
static void gmyth_scheduler_dispose  (GObject *object);
leo_sobral@1
    42
static void gmyth_scheduler_finalize (GObject *object);
leo_sobral@1
    43
leo_sobral@1
    44
static gint get_record_id_from_database (GMythScheduler *scheduler);
melunko@117
    45
static void update_backend  (GMythScheduler *scheduler, gint record_id);
leo_sobral@1
    46
leo_sobral@1
    47
G_DEFINE_TYPE(GMythScheduler, gmyth_scheduler, G_TYPE_OBJECT)
leo_sobral@1
    48
    
leo_sobral@1
    49
static void
leo_sobral@1
    50
gmyth_scheduler_class_init (GMythSchedulerClass *klass)
leo_sobral@1
    51
{
melunko@117
    52
    GObjectClass *gobject_class;
leo_sobral@1
    53
leo_sobral@1
    54
    gobject_class = (GObjectClass *) klass;
leo_sobral@1
    55
leo_sobral@1
    56
    gobject_class->dispose  = gmyth_scheduler_dispose;
leo_sobral@1
    57
    gobject_class->finalize = gmyth_scheduler_finalize;	
leo_sobral@1
    58
}
leo_sobral@1
    59
leo_sobral@1
    60
static void
leo_sobral@1
    61
gmyth_scheduler_init (GMythScheduler *sched)
leo_sobral@1
    62
{
melunko@117
    63
    sched->recordid =0;
leo_sobral@1
    64
    sched->type = 0;
leo_sobral@1
    65
    sched->search = 0;
leo_sobral@1
    66
    sched->profile =  g_string_new("");
leo_sobral@1
    67
    
leo_sobral@1
    68
    sched->dupin = 0;
leo_sobral@1
    69
    sched->dupmethod = 0;
leo_sobral@1
    70
    sched->autoexpire = 0;
leo_sobral@1
    71
    sched->autotranscode = 0;
leo_sobral@1
    72
    sched->transcoder = 0;
leo_sobral@1
    73
leo_sobral@1
    74
    sched->autocommflag = 0;
leo_sobral@1
    75
    sched->autouserjob1 = 0;
leo_sobral@1
    76
    sched->autouserjob2 = 0;
leo_sobral@1
    77
    sched->autouserjob3 = 0;
leo_sobral@1
    78
    sched->autouserjob4 = 0;
leo_sobral@1
    79
    
leo_sobral@1
    80
    sched->startoffset = 0;
leo_sobral@1
    81
    sched->endoffset = 0;
leo_sobral@1
    82
    sched->maxepisodes = 0;
leo_sobral@1
    83
    sched->maxnewest = 0;
leo_sobral@1
    84
leo_sobral@1
    85
    sched->recpriority = 0;
leo_sobral@1
    86
    sched->recgroup = 0;
leo_sobral@1
    87
    sched->playgroup = 0;
leo_sobral@1
    88
    
leo_sobral@1
    89
    sched->prefinput = 0;
leo_sobral@1
    90
    sched->inactive = 0;
leo_sobral@1
    91
    
leo_sobral@1
    92
    sched->searchType = g_string_new("");
leo_sobral@1
    93
    sched->searchForWhat = g_string_new("");
leo_sobral@1
    94
    
leo_sobral@1
    95
    sched->msqlquery = gmyth_query_new ();
leo_sobral@1
    96
}
leo_sobral@1
    97
leo_sobral@1
    98
static void
leo_sobral@1
    99
gmyth_scheduler_dispose  (GObject *object)
leo_sobral@1
   100
{
melunko@117
   101
    GMythScheduler *scheduler = GMYTH_SCHEDULER (object);
melunko@117
   102
melunko@117
   103
    if (scheduler->backend_info) { 
melunko@117
   104
	g_object_unref (scheduler->backend_info);
melunko@117
   105
	scheduler->backend_info = NULL;
melunko@117
   106
    }
melunko@117
   107
melunko@117
   108
    G_OBJECT_CLASS (gmyth_scheduler_parent_class)->dispose (object);
leo_sobral@1
   109
}
leo_sobral@1
   110
leo_sobral@1
   111
static void
leo_sobral@1
   112
gmyth_scheduler_finalize (GObject *object)
leo_sobral@1
   113
{
melunko@117
   114
    g_signal_handlers_destroy (object);
leo_sobral@1
   115
leo_sobral@1
   116
    G_OBJECT_CLASS (gmyth_scheduler_parent_class)->finalize (object);
leo_sobral@1
   117
}
leo_sobral@1
   118
leo_sobral@1
   119
/** Creates a new instance of GMythScheduler.
leo_sobral@1
   120
 * 
leo_sobral@1
   121
 * @return a new instance of GMythScheduler.
leo_sobral@1
   122
 */
leo_sobral@1
   123
GMythScheduler*
leo_sobral@1
   124
gmyth_scheduler_new ()
leo_sobral@1
   125
{
leo_sobral@1
   126
    GMythScheduler *scheduler = 
leo_sobral@1
   127
        GMYTH_SCHEDULER (g_object_new(GMYTH_SCHEDULER_TYPE, NULL));
leo_sobral@1
   128
    
leo_sobral@1
   129
    return scheduler;
leo_sobral@1
   130
}
leo_sobral@1
   131
leo_sobral@1
   132
/** Connects to the Mysql database in the backend. The backend address
leo_sobral@1
   133
 * is loaded from the GMythSettings instance.
leo_sobral@1
   134
 * 
leo_sobral@1
   135
 * @param scheduler the GMythScheduler instance to be connected.
leo_sobral@1
   136
 * @return true if connection was success, false if failed.
leo_sobral@1
   137
 */
leo_sobral@1
   138
gboolean
melunko@117
   139
gmyth_scheduler_connect (GMythScheduler *scheduler, GMythBackendInfo *backend_info)
leo_sobral@1
   140
{
leo_sobral@1
   141
    assert(scheduler);
melunko@117
   142
    g_return_val_if_fail (backend_info != NULL, FALSE);
melunko@117
   143
    
melunko@117
   144
    g_object_ref (backend_info);
melunko@117
   145
    scheduler->backend_info = backend_info;
leo_sobral@1
   146
leo_sobral@1
   147
    if (scheduler->msqlquery == NULL) {
leo_sobral@32
   148
        g_warning ("[%s] GMythScheduler db initializing", __FUNCTION__);
leo_sobral@32
   149
	    scheduler->msqlquery = gmyth_query_new ();		
leo_sobral@32
   150
	}
leo_sobral@1
   151
melunko@117
   152
    if (!gmyth_query_connect(scheduler->msqlquery, scheduler->backend_info)) {
leo_sobral@1
   153
    	g_warning ("[%s] Error while connecting to db", __FUNCTION__);
leo_sobral@1
   154
        return FALSE;
leo_sobral@1
   155
    }
leo_sobral@1
   156
leo_sobral@1
   157
    return TRUE;
leo_sobral@1
   158
}
leo_sobral@1
   159
leo_sobral@1
   160
/** Disconnects from the Mysql database in the backend.
leo_sobral@1
   161
 * 
leo_sobral@1
   162
 * @param scheduler the GMythScheduler instance to be disconnected
leo_sobral@1
   163
 * @return true if disconnection was success, false if failed.
leo_sobral@1
   164
 */
leo_sobral@1
   165
gboolean
leo_sobral@1
   166
gmyth_scheduler_disconnect (GMythScheduler *scheduler)
leo_sobral@1
   167
{
leo_sobral@1
   168
    assert(scheduler);
leo_sobral@1
   169
leo_sobral@1
   170
    if (scheduler->msqlquery != NULL) {
melunko@117
   171
        gmyth_query_disconnect (scheduler->msqlquery);
leo_sobral@1
   172
        g_object_unref (scheduler->msqlquery);
leo_sobral@1
   173
    }
leo_sobral@1
   174
leo_sobral@1
   175
    return TRUE;
leo_sobral@1
   176
}
leo_sobral@1
   177
leo_sobral@1
   178
/** Retrieves from the backend Mysql database the list of recording schedules.
leo_sobral@1
   179
 * 
leo_sobral@1
   180
 * @param scheduler The GMythScheduler instance.
leo_sobral@1
   181
 * @param schedule_list the GList pointer to be filled with the loaded list of ScheduleInfo items.
leo_sobral@1
   182
 * @return The amount of schedules retrieved from database, or -1 if error.
leo_sobral@1
   183
 */
leo_sobral@1
   184
gint
leo_sobral@1
   185
gmyth_scheduler_get_schedule_list ( GMythScheduler *scheduler, GList **schedule_list)
leo_sobral@1
   186
{
melunko@117
   187
    ScheduleInfo *schedule;
melunko@117
   188
    MYSQL_RES *msql_res;
leo_sobral@1
   189
    GString *query_str = g_string_new ("");
leo_sobral@1
   190
    GString *date_time = g_string_new ("");
leo_sobral@1
   191
    
leo_sobral@1
   192
    assert(scheduler);
leo_sobral@1
   193
    
leo_sobral@1
   194
    g_string_printf (query_str, 
leo_sobral@1
   195
    	"SELECT recordid,programid,chanid,starttime,startdate,"
leo_sobral@1
   196
    	"endtime,enddate,title,subtitle,description,category FROM record;");
leo_sobral@1
   197
melunko@117
   198
    if (scheduler->msqlquery == NULL) {
melunko@117
   199
	g_warning ("[%s] Scheduler db connection not initialized", __FUNCTION__);
melunko@117
   200
	return -1;
melunko@117
   201
    }
leo_sobral@1
   202
    msql_res = gmyth_query_process_statement (scheduler->msqlquery, query_str->str);
leo_sobral@1
   203
leo_sobral@1
   204
    if (msql_res == NULL) {
leo_sobral@1
   205
        g_warning ("DB retrieval of schedule list failed");
leo_sobral@1
   206
        return -1;
leo_sobral@1
   207
    } else {
leo_sobral@1
   208
        MYSQL_ROW row;
leo_sobral@1
   209
        *schedule_list = NULL;
leo_sobral@1
   210
        
leo_sobral@1
   211
        while((row = mysql_fetch_row (msql_res)) != NULL) {
leo_sobral@1
   212
        	schedule = g_new0(ScheduleInfo, 1);
leo_sobral@1
   213
        	
rosfran@96
   214
        	schedule->record_id  = g_ascii_strtoull (row[0], NULL, 10);
rosfran@96
   215
        	schedule->program_id = g_ascii_strtoull (row[1], NULL, 10);
rosfran@96
   216
            schedule->channel_id = g_ascii_strtoull (row[2], NULL, 10);
leo_sobral@1
   217
            
leo_sobral@1
   218
            /* generate a time_t from a time and a date db field */
leo_sobral@1
   219
            g_string_printf (date_time, "%s %s", row[4], row[3]);
leo_sobral@1
   220
            
leo_sobral@1
   221
            schedule->start_time = gmyth_util_string_to_time (date_time);
leo_sobral@1
   222
            
leo_sobral@1
   223
            /* generate a time_t from a time and a date db field */
leo_sobral@1
   224
            g_string_printf (date_time, "%s %s", row[6], row[5]);
leo_sobral@1
   225
            
leo_sobral@1
   226
            schedule->end_time = gmyth_util_string_to_time (date_time);
leo_sobral@1
   227
leo_sobral@1
   228
            schedule->title = g_string_new (row[7]);
leo_sobral@1
   229
            schedule->subtitle = g_string_new (row[8]);
leo_sobral@1
   230
            schedule->description = g_string_new (row[9]);
leo_sobral@1
   231
            schedule->category = g_string_new (row[10]);
leo_sobral@1
   232
leo_sobral@1
   233
           (*schedule_list) = g_list_append (*(schedule_list), schedule);
leo_sobral@1
   234
    	}
leo_sobral@1
   235
    }
leo_sobral@1
   236
leo_sobral@1
   237
    mysql_free_result (msql_res);
leo_sobral@1
   238
    g_string_free(query_str, TRUE);
leo_sobral@1
   239
    g_string_free(date_time, TRUE);
leo_sobral@1
   240
leo_sobral@1
   241
    return (*schedule_list == NULL) ? 0 : g_list_length (*schedule_list);
leo_sobral@1
   242
}
leo_sobral@1
   243
leo_sobral@1
   244
/** Retrieves from the backend Mysql database the list of recorded programs.
leo_sobral@1
   245
 * 
leo_sobral@1
   246
 * @param scheduler The GMythScheduler instance.
leo_sobral@1
   247
 * @param recorded_list the GList pointer to be filled with the loaded RecordInfo items.
leo_sobral@1
   248
 * @return The amount of recorded retrieved from database, or -1 if error.
leo_sobral@1
   249
 */
leo_sobral@1
   250
gint
leo_sobral@1
   251
gmyth_scheduler_get_recorded_list (GMythScheduler *scheduler, GList **recorded_list)
leo_sobral@1
   252
{
melunko@117
   253
    RecordedInfo *record;
melunko@117
   254
    MYSQL_RES *msql_res;
leo_sobral@1
   255
    GString *query_str = g_string_new ("");
leo_sobral@1
   256
    GString *date_time = g_string_new ("");
leo_sobral@1
   257
	
leo_sobral@1
   258
    assert(scheduler);
leo_sobral@1
   259
    
leo_sobral@1
   260
    g_string_printf (query_str, 
leo_sobral@1
   261
    	"SELECT recordid,programid,chanid,starttime,progstart,"
melunko@207
   262
    	"endtime,progend,title,subtitle,description,category,filesize,basename FROM recorded WHERE recgroup != 'LiveTV'");
leo_sobral@1
   263
leo_sobral@1
   264
	if (scheduler->msqlquery == NULL) {
leo_sobral@1
   265
		g_warning ("[%s] Scheduler db connection not initialized", __FUNCTION__);
leo_sobral@1
   266
		return -1;
leo_sobral@1
   267
	}
leo_sobral@1
   268
leo_sobral@1
   269
    msql_res = gmyth_query_process_statement (scheduler->msqlquery, query_str->str);
leo_sobral@1
   270
leo_sobral@1
   271
    if (msql_res == NULL) {
leo_sobral@1
   272
        g_warning ("DB retrieval of recording list failed");
leo_sobral@1
   273
        return -1;
leo_sobral@1
   274
	} else {
leo_sobral@1
   275
        MYSQL_ROW row;
leo_sobral@1
   276
        *recorded_list = NULL;
leo_sobral@1
   277
        
leo_sobral@1
   278
        while((row = mysql_fetch_row (msql_res))!=NULL){
leo_sobral@1
   279
        	record = g_new0(RecordedInfo, 1);
leo_sobral@1
   280
            
melunko@207
   281
            record->record_id  = (guint) g_ascii_strtoull (row[0], NULL, 10);
melunko@207
   282
            record->program_id = (guint) g_ascii_strtoull (row[1], NULL, 10);
melunko@207
   283
            record->channel_id = (guint) g_ascii_strtoull (row[2], NULL, 10);
leo_sobral@1
   284
            
leo_sobral@1
   285
            /* the db field time already contains the date. therefore
leo_sobral@1
   286
             * we are not using the date field */
leo_sobral@1
   287
            /* generate a time_t from a time and a date db field */
leo_sobral@1
   288
            /* g_string_printf (date_time, "%s %s", row[4], row[3]); */
leo_sobral@1
   289
            g_string_printf (date_time, "%s", row[3]); 
leo_sobral@1
   290
            
leo_sobral@1
   291
            record->start_time = gmyth_util_string_to_time (date_time);
leo_sobral@1
   292
leo_sobral@1
   293
            /* the db field time already contains the date. therefore
leo_sobral@1
   294
             * we are not using the date field */
leo_sobral@1
   295
            /* generate a time_t from a time and a date db field */
leo_sobral@1
   296
            /* g_string_printf (date_time, "%s %s", row[6], row[5]); */
leo_sobral@1
   297
            g_string_printf (date_time, "%s", row[5]); 
leo_sobral@1
   298
leo_sobral@1
   299
            record->end_time = gmyth_util_string_to_time (date_time);
leo_sobral@1
   300
	
leo_sobral@1
   301
            record->title = g_string_new (row[7]);
leo_sobral@1
   302
            record->subtitle = g_string_new (row[8]);
leo_sobral@1
   303
            record->description = g_string_new (row[9]);
leo_sobral@1
   304
            record->category = g_string_new (row[10]);
melunko@207
   305
            record->filesize = g_ascii_strtoull (row[11], NULL, 10);
melunko@207
   306
            record->basename = g_string_new (row[12]);
leo_sobral@1
   307
melunko@207
   308
 	    *recorded_list = g_list_append (*recorded_list, record);
leo_sobral@1
   309
    	}
leo_sobral@1
   310
    }
leo_sobral@1
   311
    
leo_sobral@1
   312
    mysql_free_result (msql_res);
leo_sobral@1
   313
    g_string_free(query_str, TRUE);
leo_sobral@1
   314
    g_string_free(date_time, TRUE);
leo_sobral@1
   315
leo_sobral@1
   316
    return (*recorded_list == NULL) ? 0 : g_list_length (*recorded_list);
leo_sobral@1
   317
}
leo_sobral@1
   318
leo_sobral@1
   319
/** Requests the Mysql database in the backend to add a new schedule.
leo_sobral@1
   320
 * 
leo_sobral@1
   321
 * @param scheduler the GMythScheduler instance.
leo_sobral@1
   322
 * @param schedule_info the ScheduleInfo with recording schedule information
leo_sobral@1
   323
 * to be added. record_id = -1 to add a new schedule, otherwise this
leo_sobral@1
   324
 * function will update the schedule in the db
leo_sobral@1
   325
 * @return gboolean returns FALSE if some error occurs, TRUE otherwise
leo_sobral@1
   326
 */
leo_sobral@1
   327
gboolean
leo_sobral@1
   328
gmyth_scheduler_add_schedule (GMythScheduler *scheduler,
leo_sobral@1
   329
                        	  ScheduleInfo *schedule_info)
leo_sobral@1
   330
{
leo_sobral@1
   331
    struct tm start_tm;
leo_sobral@1
   332
    struct tm end_tm;
leo_sobral@1
   333
melunko@117
   334
    MYSQL_RES *msql_res;
leo_sobral@1
   335
    GString *query_str = g_string_new ("");
leo_sobral@1
   336
leo_sobral@1
   337
    assert(scheduler);
leo_sobral@1
   338
    
melunko@117
   339
    if (scheduler->msqlquery == NULL) {
melunko@117
   340
	g_warning ("[%s] Scheduler db connection not initialized", __FUNCTION__);
melunko@117
   341
	return 0;
melunko@117
   342
    }
leo_sobral@1
   343
	
leo_sobral@1
   344
	/* manipulating time */
melunko@117
   345
    if(localtime_r(&schedule_info->start_time, &start_tm) == NULL) {
leo_sobral@1
   346
        g_warning ("localtime_r error in libgmyth scheduler!\n");
leo_sobral@1
   347
        return FALSE;                 
leo_sobral@1
   348
    }
leo_sobral@1
   349
        
melunko@117
   350
    if(localtime_r(&schedule_info->end_time, &end_tm) == NULL) {
leo_sobral@1
   351
        g_warning ("localtime_r error in libgmyth scheduler!\n");
leo_sobral@1
   352
        return FALSE;
leo_sobral@1
   353
    }	
leo_sobral@1
   354
	
melunko@117
   355
    //TODO: verify if this funtion realy does what it should do!
melunko@117
   356
    g_string_printf (query_str, "REPLACE INTO record "
leo_sobral@1
   357
	    "(recordid, type, chanid, starttime, "
leo_sobral@1
   358
	    "startdate, endtime, enddate, title,"
leo_sobral@1
   359
	    "profile, recpriority, maxnewest, inactive, "
leo_sobral@1
   360
	    "maxepisodes, autoexpire, startoffset, endoffset, "
leo_sobral@1
   361
	    "recgroup, dupmethod, dupin, station, "
leo_sobral@1
   362
	    "autocommflag, findday, findtime, findid, "
leo_sobral@1
   363
	    "search, autotranscode, transcoder, tsdefault, "
leo_sobral@1
   364
	    "autouserjob1, autouserjob2, autouserjob3, autouserjob4) "
leo_sobral@1
   365
	    " values ( %d, 1, %d, \"%02d:%02d:00\","	//recordid, type, chanid, starttime
leo_sobral@1
   366
	    " \"%d-%02d-%02d\", \"%02d:%02d:00\", \"%04d-%02d-%02d\", \"%s\","
leo_sobral@1
   367
        //startdate, endtime, enddate, title
leo_sobral@1
   368
	    "DEFAULT, 0, 0, 0, "	//profile, recpriority, maxnewest, inactive
leo_sobral@1
   369
	    "0, 1, 0, 0, "			//maxepisodes, autoexpire, startoffset, endoffset
leo_sobral@1
   370
	    "DEFAULT, 6, 15, %d, " 	//recgroup, dupmethod, dupin, station
leo_sobral@1
   371
	    "1, %d, \"%02d:%02d:00\", %d, "	//autocommflag, findday, findtime, findid
leo_sobral@1
   372
	    "5, 0, 29, 1, "			//search, autotranscode, transcoder, tsdefault
leo_sobral@1
   373
	    "0, 0, 0, 0 );",		//autouserjob1, autouserjob2, autouserjob3, autouserjob4
leo_sobral@1
   374
	    schedule_info->record_id, schedule_info->channel_id,
leo_sobral@1
   375
        start_tm.tm_hour, start_tm.tm_min,
leo_sobral@1
   376
	    start_tm.tm_year+1900, start_tm.tm_mon+1, 
leo_sobral@1
   377
        start_tm.tm_mday,
leo_sobral@1
   378
        end_tm.tm_hour, end_tm.tm_min,
leo_sobral@1
   379
        end_tm.tm_year+1900, end_tm.tm_mon+1, 
leo_sobral@1
   380
        end_tm.tm_mday, schedule_info->title->str, //title
leo_sobral@1
   381
    	schedule_info->channel_id,//station
leo_sobral@1
   382
    	start_tm.tm_wday+1, //findday
leo_sobral@1
   383
   		start_tm.tm_hour, start_tm.tm_min, //findtime
leo_sobral@1
   384
   		(gint)(schedule_info->start_time/60/60/24 + 719528)//findid
leo_sobral@1
   385
    );
leo_sobral@1
   386
leo_sobral@1
   387
    msql_res = gmyth_query_process_statement (scheduler->msqlquery, query_str->str);
leo_sobral@1
   388
    
leo_sobral@1
   389
    /* FIXME: currently no way to detect db error in UPDATE, REPLACES!
leo_sobral@1
   390
    if (msql_res == NULL) {
leo_sobral@1
   391
        g_warning ("DB retrieval of recording list failed");
leo_sobral@1
   392
        return -1;
leo_sobral@1
   393
	}*/
leo_sobral@1
   394
    
leo_sobral@1
   395
    /* TODO: verify record_id = -1 semantics */
leo_sobral@1
   396
    if (schedule_info->record_id <= 0)
leo_sobral@1
   397
	    schedule_info->record_id = get_record_id_from_database(scheduler);
leo_sobral@1
   398
    
leo_sobral@1
   399
    /* Notify the backend of changes */
melunko@117
   400
    update_backend(scheduler, schedule_info->record_id);
leo_sobral@1
   401
    
leo_sobral@1
   402
    /* free allocated memory */
leo_sobral@1
   403
    mysql_free_result (msql_res);
leo_sobral@1
   404
    g_string_free(query_str, TRUE);
leo_sobral@1
   405
    
leo_sobral@1
   406
    return 1;
leo_sobral@1
   407
}
leo_sobral@1
   408
leo_sobral@1
   409
/** Requests the Mysql database in the backend to remove an existing schedule.
leo_sobral@1
   410
 * 
leo_sobral@1
   411
 * @param scheduler the GMythScheduler instance.
leo_sobral@1
   412
 * @param record_id The schedule's record id to be removed
leo_sobral@1
   413
 * @return gboolean TRUE if success, FALSE if error
leo_sobral@1
   414
 */
leo_sobral@1
   415
gboolean
leo_sobral@1
   416
gmyth_scheduler_delete_schedule (GMythScheduler *scheduler, gint record_id)
leo_sobral@1
   417
{
leo_sobral@1
   418
melunko@117
   419
    MYSQL_RES *msql_res;
leo_sobral@1
   420
    GString *query_str = g_string_new ("");
leo_sobral@1
   421
leo_sobral@1
   422
    assert(scheduler);
leo_sobral@1
   423
    
melunko@117
   424
    if (scheduler->msqlquery == NULL) {
melunko@117
   425
	g_warning ("[%s] Scheduler db connection not initialized", __FUNCTION__);
melunko@117
   426
	return FALSE;
melunko@117
   427
    }
leo_sobral@1
   428
leo_sobral@1
   429
    //========================================
leo_sobral@1
   430
    g_string_printf (query_str, 
leo_sobral@1
   431
    	"DELETE FROM record WHERE recordid=%d", record_id);
leo_sobral@1
   432
leo_sobral@1
   433
    msql_res = gmyth_query_process_statement (scheduler->msqlquery, query_str->str);
leo_sobral@1
   434
leo_sobral@1
   435
    if (msql_res == NULL) {
leo_sobral@1
   436
    	g_warning ("[%s] Error while trying to delete a schedule in the database", __FUNCTION__);
leo_sobral@1
   437
    	return FALSE;
leo_sobral@1
   438
    }
leo_sobral@1
   439
melunko@117
   440
    update_backend(scheduler, record_id);// Notify the backend of the changes
leo_sobral@1
   441
    
leo_sobral@1
   442
    mysql_free_result (msql_res);
leo_sobral@1
   443
    g_string_free(query_str, TRUE);
leo_sobral@1
   444
melunko@117
   445
    return TRUE;
leo_sobral@1
   446
}
leo_sobral@1
   447
leo_sobral@1
   448
/** Requests the Mysql database in the backend to remove an existing recorded item.
leo_sobral@1
   449
 * 
leo_sobral@1
   450
 * @param scheduler the GMythScheduler instance.
leo_sobral@1
   451
 * @param record_id The recorded item id to be removed
leo_sobral@1
   452
 * @return gboolean TRUE if success, FALSE if error
leo_sobral@1
   453
 */
leo_sobral@1
   454
gboolean
leo_sobral@1
   455
gmyth_scheduler_delete_recorded (GMythScheduler *scheduler, gint record_id)
leo_sobral@1
   456
{
leo_sobral@1
   457
melunko@117
   458
    MYSQL_RES *msql_res;
melunko@117
   459
leo_sobral@1
   460
    GString *query_str = g_string_new ("");
leo_sobral@1
   461
leo_sobral@1
   462
    assert(scheduler);
leo_sobral@1
   463
    
leo_sobral@1
   464
	if (scheduler->msqlquery == NULL) {
leo_sobral@1
   465
		g_warning ("[%s] Scheduler db connection not initialized", __FUNCTION__);
leo_sobral@1
   466
		return FALSE;
leo_sobral@1
   467
	}
leo_sobral@1
   468
leo_sobral@1
   469
    //========================================
leo_sobral@1
   470
    g_string_printf (query_str, 
leo_sobral@1
   471
    	"DELETE FROM recorded WHERE recordid=%d", record_id);
leo_sobral@1
   472
leo_sobral@1
   473
    msql_res = gmyth_query_process_statement (scheduler->msqlquery, query_str->str);
leo_sobral@1
   474
melunko@117
   475
    update_backend(scheduler, record_id);// Notify the backend of the changes
leo_sobral@1
   476
    
leo_sobral@1
   477
    mysql_free_result (msql_res);
leo_sobral@1
   478
    g_string_free(query_str, TRUE);
leo_sobral@1
   479
melunko@117
   480
    return TRUE;
leo_sobral@1
   481
}
leo_sobral@1
   482
leo_sobral@1
   483
/** Retrieves an existing recorded item information from database. The information
leo_sobral@1
   484
 * is used to fill the returned GMythProgramInfo.
leo_sobral@1
   485
 * 
leo_sobral@1
   486
 * @param scheduler The GMythScheduler instance.
leo_sobral@1
   487
 * @param channel The channel associated to the record
leo_sobral@1
   488
 * @param starttime The record start time
leo_sobral@1
   489
 * @return A GMythProgramInfo struct with the requested record item
leo_sobral@1
   490
 * information, or NULL if error.
leo_sobral@1
   491
 */
leo_sobral@1
   492
GMythProgramInfo*
leo_sobral@1
   493
gmyth_scheduler_get_recorded (GMythScheduler *scheduler, 
leo_sobral@1
   494
                              GString *channel, time_t starttime)
leo_sobral@1
   495
{
leo_sobral@1
   496
	MYSQL_RES *msql_res;
leo_sobral@1
   497
	GMythProgramInfo *proginfo = NULL;
leo_sobral@1
   498
	GString *query_str = g_string_new("");
leo_sobral@1
   499
	GString *time_str = gmyth_util_time_to_string (starttime);
leo_sobral@1
   500
leo_sobral@1
   501
    assert(scheduler);
leo_sobral@1
   502
    
leo_sobral@1
   503
	if (scheduler->msqlquery == NULL) {
leo_sobral@1
   504
		g_warning ("[%s] Scheduler db connection not initialized", __FUNCTION__);
leo_sobral@1
   505
		return NULL;
leo_sobral@1
   506
	}
leo_sobral@1
   507
leo_sobral@1
   508
	g_string_printf (query_str, "SELECT recorded.chanid,starttime,endtime,title, "
leo_sobral@1
   509
                  "subtitle,description,channel.channum, "
leo_sobral@1
   510
                  "channel.callsign,channel.name,channel.commfree, "
leo_sobral@1
   511
                  "channel.outputfilters,seriesid,programid,filesize, "
leo_sobral@1
   512
                  "lastmodified,stars,previouslyshown,originalairdate, "
leo_sobral@1
   513
                  "hostname,recordid,transcoder,playgroup, "
leo_sobral@1
   514
                  "recorded.recpriority,progstart,progend,basename,recgroup "
leo_sobral@1
   515
                  "FROM recorded "
leo_sobral@1
   516
                  "LEFT JOIN channel "
leo_sobral@1
   517
                  "ON recorded.chanid = channel.chanid "
leo_sobral@1
   518
                  "WHERE recorded.chanid = \"%s\" "
leo_sobral@1
   519
                  "AND starttime = \"%s\" ;",
leo_sobral@1
   520
                  channel->str, time_str->str);
leo_sobral@1
   521
leo_sobral@1
   522
    msql_res = gmyth_query_process_statement (scheduler->msqlquery, query_str->str);
leo_sobral@1
   523
leo_sobral@1
   524
    if (msql_res /*&& query.size() > 0*/) {
leo_sobral@1
   525
    	
leo_sobral@1
   526
    	MYSQL_ROW msql_row = mysql_fetch_row (msql_res);
leo_sobral@1
   527
    	if (msql_row) {
leo_sobral@1
   528
leo_sobral@1
   529
    		proginfo = g_new0 (GMythProgramInfo, 1);
leo_sobral@1
   530
    	
leo_sobral@1
   531
	        proginfo->chanid = g_string_new (msql_row[0]);
leo_sobral@1
   532
	        proginfo->startts = gmyth_util_string_to_time (g_string_new (msql_row[23]));
leo_sobral@1
   533
	        proginfo->endts = gmyth_util_string_to_time (g_string_new (msql_row[24]));
leo_sobral@1
   534
	        proginfo->recstartts = gmyth_util_string_to_time (g_string_new (msql_row[1]));
leo_sobral@1
   535
	        proginfo->recendts = gmyth_util_string_to_time (g_string_new (msql_row[2]));
leo_sobral@1
   536
	        proginfo->title = g_string_new (msql_row[3]);
leo_sobral@1
   537
	        proginfo->subtitle = g_string_new (msql_row[4]);
leo_sobral@1
   538
	        proginfo->description = g_string_new (msql_row[5]);
leo_sobral@1
   539
	
leo_sobral@1
   540
	        proginfo->chanstr = g_string_new (msql_row[6]);
leo_sobral@1
   541
	        proginfo->chansign = g_string_new (msql_row[7]);
leo_sobral@1
   542
	        proginfo->channame = g_string_new (msql_row[0]);
rosfran@96
   543
	        proginfo->chancommfree = g_ascii_strtoull (msql_row[9], NULL, 10);
leo_sobral@1
   544
	        proginfo->chanOutputFilters = g_string_new (msql_row[10]);
leo_sobral@1
   545
	        proginfo->seriesid = g_string_new (msql_row[11]);
leo_sobral@1
   546
	        proginfo->programid = g_string_new (msql_row[12]);
rosfran@96
   547
	        proginfo->filesize = g_ascii_strtoull (msql_row[13], NULL, 10);
leo_sobral@1
   548
	
leo_sobral@1
   549
	        proginfo->lastmodified = gmyth_util_string_to_time (g_string_new (msql_row[14]));
leo_sobral@1
   550
	        
rosfran@96
   551
	        proginfo->stars = g_ascii_strtod (msql_row[15], NULL);
rosfran@96
   552
	        proginfo->repeat = g_ascii_strtoull (msql_row[16], NULL, 10);
leo_sobral@1
   553
	        
leo_sobral@1
   554
	        if (msql_row[17] == NULL) {
leo_sobral@1
   555
	            proginfo->originalAirDate = 0;
leo_sobral@1
   556
	            proginfo->hasAirDate = FALSE;
leo_sobral@1
   557
	        } else {
leo_sobral@1
   558
	            proginfo->originalAirDate = gmyth_util_string_to_time (g_string_new (msql_row[17]));
leo_sobral@1
   559
	            proginfo->hasAirDate = TRUE;
leo_sobral@1
   560
	        }
leo_sobral@1
   561
	        
leo_sobral@1
   562
	        proginfo->hostname = g_string_new (msql_row[18]);
rosfran@96
   563
	        proginfo->recordid = g_ascii_strtoull (msql_row[19], NULL, 10);
rosfran@96
   564
	        proginfo->transcoder = g_ascii_strtoull (msql_row[20], NULL, 10);
leo_sobral@1
   565
	
leo_sobral@1
   566
	        //proginfo->spread = -1;
leo_sobral@1
   567
			//proginfo->programflags = proginfo->getProgramFlags();
leo_sobral@1
   568
	
leo_sobral@1
   569
	        proginfo->recgroup = g_string_new (msql_row[26]);
leo_sobral@1
   570
	        proginfo->playgroup = g_string_new (msql_row[21]);
rosfran@96
   571
	        proginfo->recpriority = g_ascii_strtoull (msql_row[22], NULL, 10);
leo_sobral@1
   572
	
leo_sobral@1
   573
	        proginfo->pathname = g_string_new (msql_row[25]);
leo_sobral@1
   574
renatofilho@131
   575
	        gmyth_debug ("One program info loaded from mysql database\n");
leo_sobral@1
   576
    	}
leo_sobral@1
   577
    }
leo_sobral@1
   578
leo_sobral@1
   579
	mysql_free_result (msql_res);
leo_sobral@1
   580
    g_string_free(query_str, TRUE);
leo_sobral@1
   581
    g_string_free(time_str, TRUE);
leo_sobral@1
   582
leo_sobral@1
   583
    return proginfo;
leo_sobral@1
   584
}
leo_sobral@1
   585
leo_sobral@1
   586
/** Retrieves the next record id.
leo_sobral@1
   587
 * 
leo_sobral@1
   588
 * @param scheduler The GMythScheduler instance.
leo_sobral@1
   589
 * @return gint record_id if success, -1 otherwise 
leo_sobral@1
   590
 */
leo_sobral@1
   591
static gint
leo_sobral@1
   592
get_record_id_from_database (GMythScheduler *scheduler)
leo_sobral@1
   593
{
leo_sobral@1
   594
    gint record_id;
leo_sobral@1
   595
leo_sobral@1
   596
    assert(scheduler);
leo_sobral@1
   597
    
leo_sobral@1
   598
	if (scheduler->msqlquery == NULL) {
leo_sobral@1
   599
		g_warning ("[%s] Scheduler db connection not initialized", __FUNCTION__);
leo_sobral@1
   600
		return 0;
leo_sobral@1
   601
	}
leo_sobral@1
   602
leo_sobral@1
   603
    record_id = mysql_insert_id (scheduler->msqlquery->conn);
leo_sobral@1
   604
leo_sobral@1
   605
	return record_id;
leo_sobral@1
   606
}	
leo_sobral@1
   607
	
leo_sobral@1
   608
/** Notifies the backend of an update in the db.
leo_sobral@1
   609
 * 
leo_sobral@1
   610
 * @param record_id the id of the modified recording.
leo_sobral@1
   611
 */
leo_sobral@1
   612
static void
melunko@117
   613
update_backend(GMythScheduler *scheduler, gint record_id)//fixme: put void and discovery record_id inside
leo_sobral@1
   614
{
melunko@117
   615
    GMythSocket *socket;
leo_sobral@1
   616
    GMythStringList *strlist = gmyth_string_list_new ();
leo_sobral@1
   617
    GString *datastr = g_string_new ("RESCHEDULE_RECORDINGS ");
leo_sobral@1
   618
leo_sobral@1
   619
    g_string_append_printf (datastr, "%d", record_id);
leo_sobral@1
   620
    gmyth_string_list_append_string (strlist, datastr);
leo_sobral@1
   621
melunko@117
   622
    socket = gmyth_socket_new ();
melunko@117
   623
    if (gmyth_socket_connect (socket, scheduler->backend_info->hostname,
melunko@117
   624
			    scheduler->backend_info->port)) {
melunko@117
   625
        gmyth_socket_sendreceive_stringlist (socket, strlist);
melunko@117
   626
    } else {
melunko@117
   627
	g_warning ("[%s] Connection to backend failed!", __FUNCTION__);
melunko@117
   628
    }
leo_sobral@1
   629
    
leo_sobral@1
   630
    g_string_free(datastr, TRUE);
leo_sobral@1
   631
    g_object_unref(strlist);
leo_sobral@1
   632
}
renatofilho@129
   633
renatofilho@129
   634
void
renatofilho@129
   635
gmyth_scheduler_record_info_get_preview (RecordedInfo *info, GByteArray* data)
renatofilho@129
   636
{
renatofilho@129
   637
}
renatofilho@129
   638
renatofilho@129
   639
void
renatofilho@129
   640
gmyth_scheduler_record_info_free (RecordedInfo *info)
renatofilho@129
   641
{
renatofilho@129
   642
    if (info->title != NULL)
renatofilho@129
   643
        g_string_free (info->title, TRUE);
renatofilho@129
   644
renatofilho@129
   645
    if (info->subtitle != NULL)
renatofilho@129
   646
        g_string_free (info->subtitle, TRUE);
renatofilho@129
   647
renatofilho@129
   648
    if (info->description != NULL)
renatofilho@129
   649
        g_string_free (info->description, TRUE);
renatofilho@129
   650
renatofilho@129
   651
    if (info->category != NULL)
renatofilho@129
   652
        g_string_free (info->category, TRUE);
renatofilho@129
   653
renatofilho@129
   654
    if (info->basename != NULL)
renatofilho@129
   655
        g_string_free (info->basename, TRUE);
renatofilho@129
   656
renatofilho@129
   657
    g_free (info);
renatofilho@129
   658
}
renatofilho@129
   659
renatofilho@129
   660
void
renatofilho@129
   661
gmyth_scheduler_schedule_info_free (ScheduleInfo *info)
renatofilho@129
   662
{
renatofilho@129
   663
    if (info->title != NULL)
renatofilho@129
   664
        g_string_free (info->title, TRUE);
renatofilho@129
   665
renatofilho@129
   666
    if (info->subtitle != NULL)
renatofilho@129
   667
        g_string_free (info->subtitle, TRUE);
renatofilho@129
   668
renatofilho@129
   669
    if (info->description != NULL)
renatofilho@129
   670
        g_string_free (info->description, TRUE);
renatofilho@129
   671
renatofilho@129
   672
    if (info->category != NULL)
renatofilho@129
   673
        g_string_free (info->category, TRUE);
renatofilho@129
   674
renatofilho@129
   675
    g_free (info);
renatofilho@129
   676
}
renatofilho@129
   677