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