maemo-ui/src/mmyth_recordui.c
author renatofilho
Wed May 16 00:16:07 2007 +0100 (2007-05-16)
branchtrunk
changeset 661 a4fb2dd520ba
parent 208 c3c073032757
child 754 cb885ee44618
permissions -rw-r--r--
[svn r667] fixed bug on is livetv
     1 #include <gtk/gtk.h>
     2 #include <glib.h>
     3 #include <sys/types.h>
     4 #include <sys/stat.h>
     5 #include <unistd.h>
     6 #include <string.h>
     7 #include <stdio.h>
     8 #include <stdlib.h>
     9 
    10 #include "mmyth_ui.h"
    11 #include "mmyth_recordui.h"
    12 
    13 /* GMyth library includes */
    14 #include <gmyth/gmyth_scheduler.h>
    15 #include <gmyth/gmyth_util.h>
    16 
    17 enum {
    18 	START_DATE_COLUMN = 0,
    19 	TITLE_COLUMN,
    20 	CHAN_ID_COLUMN,
    21 	END_TIME_COLUMN,
    22 	RECORD_ID_COLUMN,
    23 	BASENAME_COLUMN,
    24 	N_COLUMNS
    25 };
    26 
    27 gboolean
    28 mmyth_recordui_reload_all (MMythRecordUI *recordui)
    29 {
    30 	gboolean res = FALSE;
    31 
    32 	res = mmyth_recordui_reload_schedule (recordui);
    33 		
    34 	res = res & mmyth_recordui_reload_record (recordui);
    35 	
    36 	
    37 	if (!res)
    38 		g_warning ("[%s] Error while reloading schedule and recording content", __FUNCTION__);
    39 		
    40 	return res;
    41 }
    42 
    43 gboolean
    44 mmyth_recordui_reload_schedule (MMythRecordUI *recordui)
    45 {
    46 	gint new_row = 0;
    47 	ScheduleInfo *schedule_info;
    48 	GList *schedule_list;
    49 	GtkTreeIter iter;
    50      gchar *start_date_time = NULL;
    51     gchar *end_date_time = NULL;
    52     GString *str_aux = g_string_new("");
    53 	gint res;
    54 
    55 	gtk_tree_store_clear(recordui->sch_tree_store);	
    56 
    57 	res = gmyth_scheduler_get_schedule_list(recordui->scheduler, &(schedule_list));
    58 	if (res < 0) {
    59 		g_warning ("[%s] Retrieved NULL list of scheduled data from database", 
    60                    __FUNCTION__);
    61 		return FALSE;
    62 	}
    63 
    64 	for ( ; schedule_list; schedule_list = schedule_list->next) {
    65  	  	schedule_info = (ScheduleInfo*) schedule_list->data;
    66 
    67   	  	gtk_tree_store_insert(recordui->sch_tree_store, &iter, NULL, new_row++);
    68         
    69         start_date_time = gmyth_util_time_to_string_from_time_val(schedule_info->start_time);
    70         end_date_time = gmyth_util_time_to_string_from_time_val(schedule_info->end_time);
    71         
    72         g_string_printf(str_aux, "%d", schedule_info->channel_id);
    73         
    74 	  	gtk_tree_store_set(recordui->sch_tree_store, &iter,
    75 	  		START_DATE_COLUMN, start_date_time, 
    76 	  		TITLE_COLUMN, schedule_info->title->str,
    77 	  		CHAN_ID_COLUMN, str_aux->str,
    78 	  		END_TIME_COLUMN, end_date_time, //It doesn't appear
    79 	  		RECORD_ID_COLUMN, schedule_info->record_id, 
    80 			-1); //the last line is a hidden item to be used in searching tasks
    81  	}
    82   	
    83   	g_debug ("[%s] %d lines added to schedule list UI", __FUNCTION__, new_row);
    84   	
    85     /* free allocated memory */
    86     if(!start_date_time)
    87         g_free(start_date_time);
    88     if(!end_date_time)
    89         g_free(end_date_time);
    90     g_string_free(str_aux,  FALSE);
    91     
    92   	return TRUE;
    93 }
    94 
    95 gboolean
    96 mmyth_recordui_reload_record (MMythRecordUI *recordui)
    97 {
    98 	gint new_row = 0;
    99 	RecordedInfo *recorded_info;
   100 	GList *record_list = NULL;
   101 	GtkTreeIter iter;
   102     gchar *start_date_time = NULL;
   103     gchar *end_date_time = NULL;
   104     GString *str_aux = g_string_new("");
   105 	gint res;
   106 	
   107 	gtk_tree_store_clear(recordui->rec_tree_store);		
   108 
   109 	res = gmyth_scheduler_get_recorded_list(recordui->scheduler, &record_list);
   110 	if (res < 0) {
   111 		g_warning ("[%s] Retrieved NULL list of recorded data from database", __FUNCTION__);
   112 		return FALSE;
   113 	}
   114 	
   115 	for (; record_list; record_list = record_list->next) {
   116  	  	recorded_info = (RecordedInfo*) record_list->data;
   117 
   118   	  	gtk_tree_store_insert(recordui->rec_tree_store, &iter, NULL, new_row++);
   119 
   120         start_date_time = gmyth_util_time_to_string_from_time_val(recorded_info->start_time);
   121         end_date_time = gmyth_util_time_to_string_from_time_val(recorded_info->end_time);
   122 
   123         g_string_printf(str_aux, "%d", recorded_info->channel_id);
   124 
   125 	  	gtk_tree_store_set(recordui->rec_tree_store, &iter,
   126 	  		START_DATE_COLUMN, start_date_time, 
   127 	  		TITLE_COLUMN, recorded_info->title->str,
   128 	  		CHAN_ID_COLUMN, str_aux->str,
   129 	  		END_TIME_COLUMN, end_date_time, //It doesn't appear
   130 	  		RECORD_ID_COLUMN, recorded_info->record_id,
   131 			BASENAME_COLUMN, recorded_info->basename->str, -1); 
   132             //the last line is a hidden item to be used in searching tasks	  		
   133   	}
   134   	
   135   	g_debug ("[%s] %d lines added to record list UI", __FUNCTION__, new_row);
   136 
   137      /* free allocated memory */
   138     if( NULL != start_date_time)
   139         g_free(start_date_time);
   140     if( NULL != end_date_time)
   141         g_free(end_date_time);
   142     g_string_free(str_aux,  FALSE);
   143   	  	
   144   	return TRUE;
   145 }
   146 
   147 
   148 MMythRecordUI*
   149 mmyth_recordui_new( GMythBackendInfo* backend_info )
   150 {
   151 	MMythRecordUI *recordui = g_new0 (MMythRecordUI, 1);
   152 
   153 	g_return_val_if_fail( backend_info != NULL, NULL );
   154 
   155 	recordui->backend_info = backend_info;
   156 	
   157 	recordui->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
   158 	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (recordui->scrolled_window),
   159 			GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
   160 
   161 	recordui->viewport = gtk_viewport_new (NULL, NULL);
   162   	gtk_container_add (GTK_CONTAINER (recordui->scrolled_window), recordui->viewport);
   163 
   164   	recordui->notebook = gtk_notebook_new ();
   165   	gtk_container_set_border_width (GTK_CONTAINER (recordui->notebook), 1);
   166   	gtk_notebook_set_scrollable (GTK_NOTEBOOK (recordui->notebook), TRUE);
   167   	gtk_notebook_popup_enable (GTK_NOTEBOOK (recordui->notebook));
   168   	gtk_container_add (GTK_CONTAINER (recordui->viewport), recordui->notebook);
   169   	gtk_notebook_popup_disable(GTK_NOTEBOOK (recordui->notebook));
   170 
   171 	/* Schedule tab */
   172   	recordui->sch_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
   173   	gtk_container_add (GTK_CONTAINER (recordui->notebook), recordui->sch_scrolled_window);
   174   	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (recordui->sch_scrolled_window), 
   175                                     GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
   176   	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (recordui->sch_scrolled_window), 
   177                                          GTK_SHADOW_IN);
   178 
   179     /* The basename column in the sched_tree_store is not being used*/
   180   	recordui->sch_tree_store = 
   181         gtk_tree_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING,
   182   		                    G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING );
   183     
   184   	recordui->sch_treeview = 
   185         gtk_tree_view_new_with_model(GTK_TREE_MODEL(recordui->sch_tree_store));
   186   	gtk_container_add (GTK_CONTAINER (recordui->sch_scrolled_window), 
   187                        recordui->sch_treeview);
   188   	recordui->sch_renderer = gtk_cell_renderer_text_new();
   189   	//g_object_set(G_OBJECT(renderer1), "foreground", "green", "background", "black", NULL);
   190   	recordui->sch_column1 = 
   191         gtk_tree_view_column_new_with_attributes("Start time", recordui->sch_renderer, 
   192                                                  "text", START_DATE_COLUMN, NULL);
   193   	gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->sch_treeview), 
   194                                 recordui->sch_column1); 
   195   	recordui->sch_column2 = 
   196         gtk_tree_view_column_new_with_attributes("Title", recordui->sch_renderer, 
   197                                                  "text", TITLE_COLUMN, NULL);
   198   	gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->sch_treeview), 
   199                                 recordui->sch_column2);
   200   	recordui->sch_column3 = 
   201         gtk_tree_view_column_new_with_attributes("Channel", recordui->sch_renderer, 
   202                                                  "text", CHAN_ID_COLUMN, NULL);
   203   	gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->sch_treeview), 
   204                                 recordui->sch_column3);
   205   	gtk_tree_view_column_set_resizable(recordui->sch_column1, TRUE);
   206   	gtk_tree_view_column_set_resizable(recordui->sch_column2, TRUE);
   207   	gtk_tree_view_column_set_resizable(recordui->sch_column3, TRUE);
   208   	gtk_tree_view_column_set_reorderable(recordui->sch_column1, TRUE);
   209   	gtk_tree_view_column_set_reorderable(recordui->sch_column2, TRUE);
   210   	gtk_tree_view_column_set_reorderable(recordui->sch_column3, TRUE);  	  	  	  	
   211 //  recordui->sch_column4 = 
   212 //      gtk_tree_view_column_new_with_attributes("", recordui->sch_renderer, "text", END_TIME_COLUMN, NULL);
   213 //  gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->sch_treeview), 
   214 //                                            recordui->sch_column4);
   215 
   216   	recordui->sch_label = gtk_label_new (("Schedule"));
   217   	gtk_notebook_set_tab_label (GTK_NOTEBOOK (recordui->notebook), 
   218                                 gtk_notebook_get_nth_page (
   219                                                GTK_NOTEBOOK (recordui->notebook), 0), 
   220                                 recordui->sch_label);
   221 
   222   // Record items  tab
   223   //   g_object_set(G_OBJECT(renderer2), "foreground", "blue", NULL);
   224   	recordui->rec_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
   225   	gtk_container_add (GTK_CONTAINER (recordui->notebook), 
   226                        recordui->rec_scrolled_window);
   227   	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (recordui->rec_scrolled_window), 
   228                                     GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
   229   	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (recordui->rec_scrolled_window), 
   230                                          GTK_SHADOW_IN);
   231 
   232   	recordui->rec_tree_store = 
   233         gtk_tree_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING,
   234                 	        G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING);
   235   	recordui->rec_treeview = 
   236         gtk_tree_view_new_with_model(GTK_TREE_MODEL(recordui->rec_tree_store));
   237   	gtk_container_add (GTK_CONTAINER (recordui->rec_scrolled_window), 
   238                        recordui->rec_treeview);
   239   	recordui->rec_renderer = gtk_cell_renderer_text_new();
   240   	//g_object_set(G_OBJECT(renderer1), "foreground", "green", "background", "black", NULL);
   241  
   242   	recordui->rec_column1 = 
   243         gtk_tree_view_column_new_with_attributes("Start time", recordui->rec_renderer, 
   244                                                  "text", START_DATE_COLUMN, NULL);
   245   	gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->rec_treeview), 
   246                                 recordui->rec_column1);
   247   	recordui->rec_column2 = 
   248         gtk_tree_view_column_new_with_attributes("Title", recordui->rec_renderer, 
   249                                                  "text", TITLE_COLUMN, NULL);
   250   	gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->rec_treeview), 
   251                                 recordui->rec_column2);
   252   	recordui->rec_column3 = 
   253         gtk_tree_view_column_new_with_attributes("Channel", recordui->rec_renderer, 
   254                                                  "text", CHAN_ID_COLUMN, NULL);
   255   	gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->rec_treeview), 
   256                                 recordui->rec_column3);
   257   	gtk_tree_view_column_set_resizable(recordui->rec_column1, TRUE);
   258   	gtk_tree_view_column_set_resizable(recordui->rec_column2, TRUE);
   259   	gtk_tree_view_column_set_resizable(recordui->rec_column3, TRUE);
   260   	gtk_tree_view_column_set_reorderable(recordui->rec_column1, TRUE);
   261   	gtk_tree_view_column_set_reorderable(recordui->rec_column2, TRUE);
   262   	gtk_tree_view_column_set_reorderable(recordui->rec_column3, TRUE);
   263 //  recordui->rec_column4 = gtk_tree_view_column_new_with_attributes("", recordui->rec_renderer, "text", END_TIME_COLUMN, NULL);
   264 //  gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->rec_treeview), recordui->rec_column4);
   265 
   266   	recordui->rec_label = gtk_label_new (("Recorded"));
   267   	gtk_notebook_set_tab_label (GTK_NOTEBOOK (recordui->notebook), 
   268                                 gtk_notebook_get_nth_page (
   269                                       GTK_NOTEBOOK (recordui->notebook), 1), 
   270                                 recordui->rec_label);
   271   
   272   	// Gets the mmyth scheduler manager
   273   	recordui->scheduler = gmyth_scheduler_new ( backend_info );
   274     
   275     /* init connection to the backend */
   276     gmyth_scheduler_connect (recordui->scheduler, recordui->scheduler->backend_info);
   277     
   278 	return recordui;
   279 }
   280 
   281 void
   282 mmyth_recordui_free (MMythRecordUI *recordui)
   283 {
   284 	// FIXME: Release memory here!
   285     /* close connection to the backend */
   286     gmyth_scheduler_disconnect (recordui->scheduler);
   287 }
   288 
   289 void
   290 mmyth_recordui_delete_selected (GtkButton *button, MMythRecordUI *recordui)
   291 {
   292 	GtkTreeSelection *selection;
   293 	GtkTreeModel *list_store;
   294 	GtkTreeIter iter;
   295 	int index;
   296 	int curr_page = 0;
   297 	
   298 	curr_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(recordui->notebook));
   299 
   300 	if ( curr_page == 0) {
   301 		selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(recordui->sch_treeview));
   302 		if (selection != NULL) {
   303 			gtk_tree_selection_get_selected(selection, &list_store, &iter);
   304 			gtk_tree_model_get(list_store, &iter, RECORD_ID_COLUMN, &index, -1);
   305 			gmyth_scheduler_delete_schedule(recordui->scheduler, index);
   306 			mmyth_recordui_reload_schedule (recordui);
   307 			return;
   308 		}
   309 			
   310 	} else if (curr_page == 1) {
   311 		selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(recordui->rec_treeview));
   312 		if (selection != NULL) {
   313 			gtk_tree_selection_get_selected(selection, &list_store, &iter);
   314 			gtk_tree_model_get(list_store, &iter, RECORD_ID_COLUMN, &index, -1);
   315 			gmyth_scheduler_delete_recorded(recordui->scheduler, index);
   316 			mmyth_recordui_reload_record (recordui);
   317 			return;
   318 		}
   319 	}
   320 	
   321 	g_warning ("[%s] None element was removed from the list", __FUNCTION__);
   322 }
   323 
   324 /* FIXME: change this function name, it is returning the 
   325  * basename_column that represents the nuv filename of 
   326  * the recorded content */
   327 gchar*
   328 mmyth_recordui_get_selected_recorded (MMythRecordUI *recordui)
   329 {
   330 	GtkTreeSelection *selection = NULL;
   331 	GtkTreeModel *list_store = NULL;
   332 	GtkTreeIter iter;
   333 	gchar *path = NULL;
   334 
   335 	/* returning nuv filename, basename_column */
   336 	selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(recordui->rec_treeview));
   337 	if (gtk_tree_selection_get_selected (selection, &list_store, &iter)) {
   338 		gtk_tree_model_get(list_store, &iter, BASENAME_COLUMN, &path, -1);
   339 	}
   340 
   341 	// FIXME: MOVE THIS TO OTHER PLACE
   342 	return path;
   343 }