rosfran@244: #include <gtk/gtk.h>
rosfran@244: #include <glib.h>
renatofilho@20: #include <sys/types.h>
renatofilho@20: #include <sys/stat.h>
renatofilho@20: #include <unistd.h>
renatofilho@20: #include <string.h>
renatofilho@20: #include <stdio.h>
renatofilho@20: #include <stdlib.h>
renatofilho@20: 
renatofilho@20: #include "mmyth_ui.h"
renatofilho@20: #include "mmyth_recordui.h"
renatofilho@20: 
renatofilho@20: /* GMyth library includes */
rosfran@244: #include <gmyth/gmyth_scheduler.h>
rosfran@244: #include <gmyth/gmyth_util.h>
renatofilho@20: 
renatofilho@20: enum {
renatofilho@20: 	START_DATE_COLUMN = 0,
renatofilho@20: 	TITLE_COLUMN,
renatofilho@20: 	CHAN_ID_COLUMN,
renatofilho@20: 	END_TIME_COLUMN,
renatofilho@20: 	RECORD_ID_COLUMN,
renatofilho@20: 	BASENAME_COLUMN,
renatofilho@20: 	N_COLUMNS
renatofilho@20: };
renatofilho@20: 
renatofilho@20: gboolean
renatofilho@20: mmyth_recordui_reload_all (MMythRecordUI *recordui)
renatofilho@20: {
renatofilho@20: 	gboolean res = FALSE;
renatofilho@20: 
renatofilho@20: 	res = mmyth_recordui_reload_schedule (recordui);
renatofilho@20: 		
renatofilho@20: 	res = res & mmyth_recordui_reload_record (recordui);
renatofilho@20: 	
renatofilho@20: 	
renatofilho@20: 	if (!res)
renatofilho@20: 		g_warning ("[%s] Error while reloading schedule and recording content", __FUNCTION__);
renatofilho@20: 		
renatofilho@20: 	return res;
renatofilho@20: }
renatofilho@20: 
renatofilho@20: gboolean
renatofilho@20: mmyth_recordui_reload_schedule (MMythRecordUI *recordui)
renatofilho@20: {
renatofilho@20: 	gint new_row = 0;
renatofilho@20: 	ScheduleInfo *schedule_info;
renatofilho@20: 	GList *schedule_list;
renatofilho@20: 	GtkTreeIter iter;
rosfran@244:      gchar *start_date_time = NULL;
rosfran@244:     gchar *end_date_time = NULL;
renatofilho@20:     GString *str_aux = g_string_new("");
renatofilho@20: 	gint res;
renatofilho@20: 
renatofilho@20: 	gtk_tree_store_clear(recordui->sch_tree_store);	
renatofilho@20: 
renatofilho@20: 	res = gmyth_scheduler_get_schedule_list(recordui->scheduler, &(schedule_list));
renatofilho@20: 	if (res < 0) {
renatofilho@20: 		g_warning ("[%s] Retrieved NULL list of scheduled data from database", 
renatofilho@20:                    __FUNCTION__);
renatofilho@20: 		return FALSE;
renatofilho@20: 	}
renatofilho@20: 
renatofilho@20: 	for ( ; schedule_list; schedule_list = schedule_list->next) {
renatofilho@20:  	  	schedule_info = (ScheduleInfo*) schedule_list->data;
renatofilho@20: 
renatofilho@20:   	  	gtk_tree_store_insert(recordui->sch_tree_store, &iter, NULL, new_row++);
renatofilho@20:         
rosfran@244:         start_date_time = gmyth_util_time_to_string_from_time_val(schedule_info->start_time);
rosfran@244:         end_date_time = gmyth_util_time_to_string_from_time_val(schedule_info->end_time);
renatofilho@20:         
renatofilho@20:         g_string_printf(str_aux, "%d", schedule_info->channel_id);
renatofilho@20:         
renatofilho@20: 	  	gtk_tree_store_set(recordui->sch_tree_store, &iter,
rosfran@244: 	  		START_DATE_COLUMN, start_date_time, 
renatofilho@20: 	  		TITLE_COLUMN, schedule_info->title->str,
renatofilho@20: 	  		CHAN_ID_COLUMN, str_aux->str,
rosfran@244: 	  		END_TIME_COLUMN, end_date_time, //It doesn't appear
renatofilho@20: 	  		RECORD_ID_COLUMN, schedule_info->record_id, 
renatofilho@20: 			-1); //the last line is a hidden item to be used in searching tasks
renatofilho@20:  	}
renatofilho@20:   	
renatofilho@20:   	g_debug ("[%s] %d lines added to schedule list UI", __FUNCTION__, new_row);
renatofilho@20:   	
renatofilho@20:     /* free allocated memory */
renatofilho@20:     if(!start_date_time)
rosfran@244:         g_free(start_date_time);
renatofilho@20:     if(!end_date_time)
rosfran@244:         g_free(end_date_time);
renatofilho@20:     g_string_free(str_aux,  FALSE);
renatofilho@20:     
renatofilho@20:   	return TRUE;
renatofilho@20: }
renatofilho@20: 
renatofilho@20: gboolean
renatofilho@20: mmyth_recordui_reload_record (MMythRecordUI *recordui)
renatofilho@20: {
renatofilho@20: 	gint new_row = 0;
renatofilho@20: 	RecordedInfo *recorded_info;
renatofilho@20: 	GList *record_list = NULL;
renatofilho@20: 	GtkTreeIter iter;
rosfran@244:     gchar *start_date_time = NULL;
rosfran@244:     gchar *end_date_time = NULL;
renatofilho@20:     GString *str_aux = g_string_new("");
renatofilho@20: 	gint res;
renatofilho@20: 	
renatofilho@20: 	gtk_tree_store_clear(recordui->rec_tree_store);		
renatofilho@20: 
renatofilho@20: 	res = gmyth_scheduler_get_recorded_list(recordui->scheduler, &record_list);
renatofilho@20: 	if (res < 0) {
renatofilho@20: 		g_warning ("[%s] Retrieved NULL list of recorded data from database", __FUNCTION__);
renatofilho@20: 		return FALSE;
renatofilho@20: 	}
renatofilho@20: 	
renatofilho@20: 	for (; record_list; record_list = record_list->next) {
renatofilho@20:  	  	recorded_info = (RecordedInfo*) record_list->data;
renatofilho@20: 
renatofilho@20:   	  	gtk_tree_store_insert(recordui->rec_tree_store, &iter, NULL, new_row++);
renatofilho@20: 
rosfran@244:         start_date_time = gmyth_util_time_to_string_from_time_val(recorded_info->start_time);
rosfran@244:         end_date_time = gmyth_util_time_to_string_from_time_val(recorded_info->end_time);
renatofilho@20: 
renatofilho@20:         g_string_printf(str_aux, "%d", recorded_info->channel_id);
renatofilho@20: 
renatofilho@20: 	  	gtk_tree_store_set(recordui->rec_tree_store, &iter,
rosfran@244: 	  		START_DATE_COLUMN, start_date_time, 
renatofilho@20: 	  		TITLE_COLUMN, recorded_info->title->str,
renatofilho@20: 	  		CHAN_ID_COLUMN, str_aux->str,
rosfran@244: 	  		END_TIME_COLUMN, end_date_time, //It doesn't appear
renatofilho@20: 	  		RECORD_ID_COLUMN, recorded_info->record_id,
renatofilho@20: 			BASENAME_COLUMN, recorded_info->basename->str, -1); 
renatofilho@20:             //the last line is a hidden item to be used in searching tasks	  		
renatofilho@20:   	}
renatofilho@20:   	
renatofilho@20:   	g_debug ("[%s] %d lines added to record list UI", __FUNCTION__, new_row);
rosfran@244: 
rosfran@244:      /* free allocated memory */
rosfran@244:     if( NULL != start_date_time)
rosfran@244:         g_free(start_date_time);
rosfran@244:     if( NULL != end_date_time)
rosfran@244:         g_free(end_date_time);
rosfran@244:     g_string_free(str_aux,  FALSE);
renatofilho@20:   	  	
renatofilho@20:   	return TRUE;
renatofilho@20: }
renatofilho@20: 
renatofilho@20: 
renatofilho@20: MMythRecordUI*
rosfran@244: mmyth_recordui_new( GMythBackendInfo* backend_info )
renatofilho@20: {
renatofilho@20: 	MMythRecordUI *recordui = g_new0 (MMythRecordUI, 1);
rosfran@244: 
rosfran@244: 	g_return_val_if_fail( backend_info != NULL, NULL );
rosfran@244: 
rosfran@244: 	recordui->backend_info = backend_info;
renatofilho@20: 	
renatofilho@20: 	recordui->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
renatofilho@20: 	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (recordui->scrolled_window),
renatofilho@20: 			GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
renatofilho@20: 
renatofilho@20: 	recordui->viewport = gtk_viewport_new (NULL, NULL);
renatofilho@20:   	gtk_container_add (GTK_CONTAINER (recordui->scrolled_window), recordui->viewport);
renatofilho@20: 
renatofilho@20:   	recordui->notebook = gtk_notebook_new ();
renatofilho@20:   	gtk_container_set_border_width (GTK_CONTAINER (recordui->notebook), 1);
renatofilho@20:   	gtk_notebook_set_scrollable (GTK_NOTEBOOK (recordui->notebook), TRUE);
renatofilho@20:   	gtk_notebook_popup_enable (GTK_NOTEBOOK (recordui->notebook));
renatofilho@20:   	gtk_container_add (GTK_CONTAINER (recordui->viewport), recordui->notebook);
renatofilho@20:   	gtk_notebook_popup_disable(GTK_NOTEBOOK (recordui->notebook));
renatofilho@20: 
renatofilho@20: 	/* Schedule tab */
renatofilho@20:   	recordui->sch_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
renatofilho@20:   	gtk_container_add (GTK_CONTAINER (recordui->notebook), recordui->sch_scrolled_window);
renatofilho@20:   	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (recordui->sch_scrolled_window), 
renatofilho@20:                                     GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
renatofilho@20:   	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (recordui->sch_scrolled_window), 
renatofilho@20:                                          GTK_SHADOW_IN);
renatofilho@20: 
renatofilho@20:     /* The basename column in the sched_tree_store is not being used*/
renatofilho@20:   	recordui->sch_tree_store = 
renatofilho@20:         gtk_tree_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING,
renatofilho@20:   		                    G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING );
renatofilho@20:     
renatofilho@20:   	recordui->sch_treeview = 
renatofilho@20:         gtk_tree_view_new_with_model(GTK_TREE_MODEL(recordui->sch_tree_store));
renatofilho@20:   	gtk_container_add (GTK_CONTAINER (recordui->sch_scrolled_window), 
renatofilho@20:                        recordui->sch_treeview);
renatofilho@20:   	recordui->sch_renderer = gtk_cell_renderer_text_new();
renatofilho@20:   	//g_object_set(G_OBJECT(renderer1), "foreground", "green", "background", "black", NULL);
renatofilho@20:   	recordui->sch_column1 = 
renatofilho@20:         gtk_tree_view_column_new_with_attributes("Start time", recordui->sch_renderer, 
renatofilho@20:                                                  "text", START_DATE_COLUMN, NULL);
renatofilho@20:   	gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->sch_treeview), 
renatofilho@20:                                 recordui->sch_column1); 
renatofilho@20:   	recordui->sch_column2 = 
renatofilho@20:         gtk_tree_view_column_new_with_attributes("Title", recordui->sch_renderer, 
renatofilho@20:                                                  "text", TITLE_COLUMN, NULL);
renatofilho@20:   	gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->sch_treeview), 
renatofilho@20:                                 recordui->sch_column2);
renatofilho@20:   	recordui->sch_column3 = 
renatofilho@20:         gtk_tree_view_column_new_with_attributes("Channel", recordui->sch_renderer, 
renatofilho@20:                                                  "text", CHAN_ID_COLUMN, NULL);
renatofilho@20:   	gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->sch_treeview), 
renatofilho@20:                                 recordui->sch_column3);
renatofilho@20:   	gtk_tree_view_column_set_resizable(recordui->sch_column1, TRUE);
renatofilho@20:   	gtk_tree_view_column_set_resizable(recordui->sch_column2, TRUE);
renatofilho@20:   	gtk_tree_view_column_set_resizable(recordui->sch_column3, TRUE);
renatofilho@20:   	gtk_tree_view_column_set_reorderable(recordui->sch_column1, TRUE);
renatofilho@20:   	gtk_tree_view_column_set_reorderable(recordui->sch_column2, TRUE);
renatofilho@20:   	gtk_tree_view_column_set_reorderable(recordui->sch_column3, TRUE);  	  	  	  	
renatofilho@20: //  recordui->sch_column4 = 
renatofilho@20: //      gtk_tree_view_column_new_with_attributes("", recordui->sch_renderer, "text", END_TIME_COLUMN, NULL);
renatofilho@20: //  gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->sch_treeview), 
renatofilho@20: //                                            recordui->sch_column4);
renatofilho@20: 
renatofilho@20:   	recordui->sch_label = gtk_label_new (("Schedule"));
renatofilho@20:   	gtk_notebook_set_tab_label (GTK_NOTEBOOK (recordui->notebook), 
renatofilho@20:                                 gtk_notebook_get_nth_page (
renatofilho@20:                                                GTK_NOTEBOOK (recordui->notebook), 0), 
renatofilho@20:                                 recordui->sch_label);
renatofilho@20: 
renatofilho@20:   // Record items  tab
renatofilho@20:   //   g_object_set(G_OBJECT(renderer2), "foreground", "blue", NULL);
renatofilho@20:   	recordui->rec_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
renatofilho@20:   	gtk_container_add (GTK_CONTAINER (recordui->notebook), 
renatofilho@20:                        recordui->rec_scrolled_window);
renatofilho@20:   	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (recordui->rec_scrolled_window), 
renatofilho@20:                                     GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
renatofilho@20:   	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (recordui->rec_scrolled_window), 
renatofilho@20:                                          GTK_SHADOW_IN);
renatofilho@20: 
renatofilho@20:   	recordui->rec_tree_store = 
renatofilho@20:         gtk_tree_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING,
renatofilho@20:                 	        G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING);
renatofilho@20:   	recordui->rec_treeview = 
renatofilho@20:         gtk_tree_view_new_with_model(GTK_TREE_MODEL(recordui->rec_tree_store));
renatofilho@20:   	gtk_container_add (GTK_CONTAINER (recordui->rec_scrolled_window), 
renatofilho@20:                        recordui->rec_treeview);
renatofilho@20:   	recordui->rec_renderer = gtk_cell_renderer_text_new();
renatofilho@20:   	//g_object_set(G_OBJECT(renderer1), "foreground", "green", "background", "black", NULL);
renatofilho@20:  
renatofilho@20:   	recordui->rec_column1 = 
renatofilho@20:         gtk_tree_view_column_new_with_attributes("Start time", recordui->rec_renderer, 
renatofilho@20:                                                  "text", START_DATE_COLUMN, NULL);
renatofilho@20:   	gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->rec_treeview), 
renatofilho@20:                                 recordui->rec_column1);
renatofilho@20:   	recordui->rec_column2 = 
renatofilho@20:         gtk_tree_view_column_new_with_attributes("Title", recordui->rec_renderer, 
renatofilho@20:                                                  "text", TITLE_COLUMN, NULL);
renatofilho@20:   	gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->rec_treeview), 
renatofilho@20:                                 recordui->rec_column2);
renatofilho@20:   	recordui->rec_column3 = 
renatofilho@20:         gtk_tree_view_column_new_with_attributes("Channel", recordui->rec_renderer, 
renatofilho@20:                                                  "text", CHAN_ID_COLUMN, NULL);
renatofilho@20:   	gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->rec_treeview), 
renatofilho@20:                                 recordui->rec_column3);
renatofilho@20:   	gtk_tree_view_column_set_resizable(recordui->rec_column1, TRUE);
renatofilho@20:   	gtk_tree_view_column_set_resizable(recordui->rec_column2, TRUE);
renatofilho@20:   	gtk_tree_view_column_set_resizable(recordui->rec_column3, TRUE);
renatofilho@20:   	gtk_tree_view_column_set_reorderable(recordui->rec_column1, TRUE);
renatofilho@20:   	gtk_tree_view_column_set_reorderable(recordui->rec_column2, TRUE);
renatofilho@20:   	gtk_tree_view_column_set_reorderable(recordui->rec_column3, TRUE);
renatofilho@20: //  recordui->rec_column4 = gtk_tree_view_column_new_with_attributes("", recordui->rec_renderer, "text", END_TIME_COLUMN, NULL);
renatofilho@20: //  gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->rec_treeview), recordui->rec_column4);
renatofilho@20: 
renatofilho@20:   	recordui->rec_label = gtk_label_new (("Recorded"));
renatofilho@20:   	gtk_notebook_set_tab_label (GTK_NOTEBOOK (recordui->notebook), 
renatofilho@20:                                 gtk_notebook_get_nth_page (
renatofilho@20:                                       GTK_NOTEBOOK (recordui->notebook), 1), 
renatofilho@20:                                 recordui->rec_label);
renatofilho@20:   
renatofilho@20:   	// Gets the mmyth scheduler manager
rosfran@244:   	recordui->scheduler = gmyth_scheduler_new ( backend_info );
renatofilho@20:     
renatofilho@20:     /* init connection to the backend */
rosfran@208:     gmyth_scheduler_connect (recordui->scheduler, recordui->scheduler->backend_info);
renatofilho@20:     
renatofilho@20: 	return recordui;
renatofilho@20: }
renatofilho@20: 
renatofilho@20: void
renatofilho@20: mmyth_recordui_free (MMythRecordUI *recordui)
renatofilho@20: {
renatofilho@20: 	// FIXME: Release memory here!
renatofilho@20:     /* close connection to the backend */
renatofilho@20:     gmyth_scheduler_disconnect (recordui->scheduler);
renatofilho@20: }
renatofilho@20: 
renatofilho@20: void
renatofilho@20: mmyth_recordui_delete_selected (GtkButton *button, MMythRecordUI *recordui)
renatofilho@20: {
renatofilho@20: 	GtkTreeSelection *selection;
renatofilho@20: 	GtkTreeModel *list_store;
renatofilho@20: 	GtkTreeIter iter;
renatofilho@20: 	int index;
renatofilho@20: 	int curr_page = 0;
renatofilho@20: 	
renatofilho@20: 	curr_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(recordui->notebook));
renatofilho@20: 
renatofilho@20: 	if ( curr_page == 0) {
renatofilho@20: 		selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(recordui->sch_treeview));
renatofilho@20: 		if (selection != NULL) {
renatofilho@20: 			gtk_tree_selection_get_selected(selection, &list_store, &iter);
renatofilho@20: 			gtk_tree_model_get(list_store, &iter, RECORD_ID_COLUMN, &index, -1);
renatofilho@20: 			gmyth_scheduler_delete_schedule(recordui->scheduler, index);
renatofilho@20: 			mmyth_recordui_reload_schedule (recordui);
renatofilho@20: 			return;
renatofilho@20: 		}
renatofilho@20: 			
renatofilho@20: 	} else if (curr_page == 1) {
renatofilho@20: 		selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(recordui->rec_treeview));
renatofilho@20: 		if (selection != NULL) {
renatofilho@20: 			gtk_tree_selection_get_selected(selection, &list_store, &iter);
renatofilho@20: 			gtk_tree_model_get(list_store, &iter, RECORD_ID_COLUMN, &index, -1);
renatofilho@20: 			gmyth_scheduler_delete_recorded(recordui->scheduler, index);
renatofilho@20: 			mmyth_recordui_reload_record (recordui);
renatofilho@20: 			return;
renatofilho@20: 		}
renatofilho@20: 	}
renatofilho@20: 	
renatofilho@20: 	g_warning ("[%s] None element was removed from the list", __FUNCTION__);
renatofilho@20: }
renatofilho@20: 
renatofilho@20: /* FIXME: change this function name, it is returning the 
renatofilho@20:  * basename_column that represents the nuv filename of 
renatofilho@20:  * the recorded content */
renatofilho@20: gchar*
renatofilho@20: mmyth_recordui_get_selected_recorded (MMythRecordUI *recordui)
renatofilho@20: {
renatofilho@20: 	GtkTreeSelection *selection = NULL;
renatofilho@20: 	GtkTreeModel *list_store = NULL;
renatofilho@20: 	GtkTreeIter iter;
renatofilho@20: 	gchar *path = NULL;
renatofilho@20: 
renatofilho@20: 	/* returning nuv filename, basename_column */
renatofilho@20: 	selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(recordui->rec_treeview));
renatofilho@20: 	if (gtk_tree_selection_get_selected (selection, &list_store, &iter)) {
renatofilho@20: 		gtk_tree_model_get(list_store, &iter, BASENAME_COLUMN, &path, -1);
renatofilho@20: 	}
renatofilho@20: 
renatofilho@20: 	// FIXME: MOVE THIS TO OTHER PLACE
renatofilho@20: 	return path;
renatofilho@20: }