maemo-ui/src/mmyth_recordui.c
author melunko
Tue Oct 24 14:30:57 2006 +0100 (2006-10-24)
branchtrunk
changeset 46 be7aba49c535
child 208 c3c073032757
permissions -rw-r--r--
[svn r47] Added to src/Makefile.am
     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_scheduler.h"
    15 #include "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     GString *start_date_time = NULL;
    51     GString *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(schedule_info->start_time);
    70         end_date_time = gmyth_util_time_to_string(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->str, 
    76 	  		TITLE_COLUMN, schedule_info->title->str,
    77 	  		CHAN_ID_COLUMN, str_aux->str,
    78 	  		END_TIME_COLUMN, end_date_time->str, //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_string_free(start_date_time, FALSE);
    88     if(!end_date_time)
    89         g_string_free(end_date_time, FALSE);
    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     GString *start_date_time = NULL;
   103     GString *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(recorded_info->start_time);
   121         end_date_time = gmyth_util_time_to_string(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->str, 
   127 	  		TITLE_COLUMN, recorded_info->title->str,
   128 	  		CHAN_ID_COLUMN, str_aux->str,
   129 	  		END_TIME_COLUMN, end_date_time->str, //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   	return TRUE;
   138 }
   139 
   140 
   141 MMythRecordUI*
   142 mmyth_recordui_new(void)
   143 {
   144 	MMythRecordUI *recordui = g_new0 (MMythRecordUI, 1);
   145 	
   146 	recordui->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
   147 	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (recordui->scrolled_window),
   148 			GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
   149 
   150 	recordui->viewport = gtk_viewport_new (NULL, NULL);
   151   	gtk_container_add (GTK_CONTAINER (recordui->scrolled_window), recordui->viewport);
   152 
   153   	recordui->notebook = gtk_notebook_new ();
   154   	gtk_container_set_border_width (GTK_CONTAINER (recordui->notebook), 1);
   155   	gtk_notebook_set_scrollable (GTK_NOTEBOOK (recordui->notebook), TRUE);
   156   	gtk_notebook_popup_enable (GTK_NOTEBOOK (recordui->notebook));
   157   	gtk_container_add (GTK_CONTAINER (recordui->viewport), recordui->notebook);
   158   	gtk_notebook_popup_disable(GTK_NOTEBOOK (recordui->notebook));
   159 
   160 	/* Schedule tab */
   161   	recordui->sch_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
   162   	gtk_container_add (GTK_CONTAINER (recordui->notebook), recordui->sch_scrolled_window);
   163   	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (recordui->sch_scrolled_window), 
   164                                     GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
   165   	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (recordui->sch_scrolled_window), 
   166                                          GTK_SHADOW_IN);
   167 
   168     /* The basename column in the sched_tree_store is not being used*/
   169   	recordui->sch_tree_store = 
   170         gtk_tree_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING,
   171   		                    G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING );
   172     
   173   	recordui->sch_treeview = 
   174         gtk_tree_view_new_with_model(GTK_TREE_MODEL(recordui->sch_tree_store));
   175   	gtk_container_add (GTK_CONTAINER (recordui->sch_scrolled_window), 
   176                        recordui->sch_treeview);
   177   	recordui->sch_renderer = gtk_cell_renderer_text_new();
   178   	//g_object_set(G_OBJECT(renderer1), "foreground", "green", "background", "black", NULL);
   179   	recordui->sch_column1 = 
   180         gtk_tree_view_column_new_with_attributes("Start time", recordui->sch_renderer, 
   181                                                  "text", START_DATE_COLUMN, NULL);
   182   	gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->sch_treeview), 
   183                                 recordui->sch_column1); 
   184   	recordui->sch_column2 = 
   185         gtk_tree_view_column_new_with_attributes("Title", recordui->sch_renderer, 
   186                                                  "text", TITLE_COLUMN, NULL);
   187   	gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->sch_treeview), 
   188                                 recordui->sch_column2);
   189   	recordui->sch_column3 = 
   190         gtk_tree_view_column_new_with_attributes("Channel", recordui->sch_renderer, 
   191                                                  "text", CHAN_ID_COLUMN, NULL);
   192   	gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->sch_treeview), 
   193                                 recordui->sch_column3);
   194   	gtk_tree_view_column_set_resizable(recordui->sch_column1, TRUE);
   195   	gtk_tree_view_column_set_resizable(recordui->sch_column2, TRUE);
   196   	gtk_tree_view_column_set_resizable(recordui->sch_column3, TRUE);
   197   	gtk_tree_view_column_set_reorderable(recordui->sch_column1, TRUE);
   198   	gtk_tree_view_column_set_reorderable(recordui->sch_column2, TRUE);
   199   	gtk_tree_view_column_set_reorderable(recordui->sch_column3, TRUE);  	  	  	  	
   200 //  recordui->sch_column4 = 
   201 //      gtk_tree_view_column_new_with_attributes("", recordui->sch_renderer, "text", END_TIME_COLUMN, NULL);
   202 //  gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->sch_treeview), 
   203 //                                            recordui->sch_column4);
   204 
   205   	recordui->sch_label = gtk_label_new (("Schedule"));
   206   	gtk_notebook_set_tab_label (GTK_NOTEBOOK (recordui->notebook), 
   207                                 gtk_notebook_get_nth_page (
   208                                                GTK_NOTEBOOK (recordui->notebook), 0), 
   209                                 recordui->sch_label);
   210 
   211   // Record items  tab
   212   //   g_object_set(G_OBJECT(renderer2), "foreground", "blue", NULL);
   213   	recordui->rec_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
   214   	gtk_container_add (GTK_CONTAINER (recordui->notebook), 
   215                        recordui->rec_scrolled_window);
   216   	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (recordui->rec_scrolled_window), 
   217                                     GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
   218   	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (recordui->rec_scrolled_window), 
   219                                          GTK_SHADOW_IN);
   220 
   221   	recordui->rec_tree_store = 
   222         gtk_tree_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING,
   223                 	        G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING);
   224   	recordui->rec_treeview = 
   225         gtk_tree_view_new_with_model(GTK_TREE_MODEL(recordui->rec_tree_store));
   226   	gtk_container_add (GTK_CONTAINER (recordui->rec_scrolled_window), 
   227                        recordui->rec_treeview);
   228   	recordui->rec_renderer = gtk_cell_renderer_text_new();
   229   	//g_object_set(G_OBJECT(renderer1), "foreground", "green", "background", "black", NULL);
   230  
   231   	recordui->rec_column1 = 
   232         gtk_tree_view_column_new_with_attributes("Start time", recordui->rec_renderer, 
   233                                                  "text", START_DATE_COLUMN, NULL);
   234   	gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->rec_treeview), 
   235                                 recordui->rec_column1);
   236   	recordui->rec_column2 = 
   237         gtk_tree_view_column_new_with_attributes("Title", recordui->rec_renderer, 
   238                                                  "text", TITLE_COLUMN, NULL);
   239   	gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->rec_treeview), 
   240                                 recordui->rec_column2);
   241   	recordui->rec_column3 = 
   242         gtk_tree_view_column_new_with_attributes("Channel", recordui->rec_renderer, 
   243                                                  "text", CHAN_ID_COLUMN, NULL);
   244   	gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->rec_treeview), 
   245                                 recordui->rec_column3);
   246   	gtk_tree_view_column_set_resizable(recordui->rec_column1, TRUE);
   247   	gtk_tree_view_column_set_resizable(recordui->rec_column2, TRUE);
   248   	gtk_tree_view_column_set_resizable(recordui->rec_column3, TRUE);
   249   	gtk_tree_view_column_set_reorderable(recordui->rec_column1, TRUE);
   250   	gtk_tree_view_column_set_reorderable(recordui->rec_column2, TRUE);
   251   	gtk_tree_view_column_set_reorderable(recordui->rec_column3, TRUE);
   252 //  recordui->rec_column4 = gtk_tree_view_column_new_with_attributes("", recordui->rec_renderer, "text", END_TIME_COLUMN, NULL);
   253 //  gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->rec_treeview), recordui->rec_column4);
   254 
   255   	recordui->rec_label = gtk_label_new (("Recorded"));
   256   	gtk_notebook_set_tab_label (GTK_NOTEBOOK (recordui->notebook), 
   257                                 gtk_notebook_get_nth_page (
   258                                       GTK_NOTEBOOK (recordui->notebook), 1), 
   259                                 recordui->rec_label);
   260   
   261   	// Gets the mmyth scheduler manager
   262   	recordui->scheduler = gmyth_scheduler_new ();
   263     
   264     /* init connection to the backend */
   265     gmyth_scheduler_connect (recordui->scheduler);
   266     
   267 	return recordui;
   268 }
   269 
   270 void
   271 mmyth_recordui_free (MMythRecordUI *recordui)
   272 {
   273 	// FIXME: Release memory here!
   274     /* close connection to the backend */
   275     gmyth_scheduler_disconnect (recordui->scheduler);
   276 }
   277 
   278 void
   279 mmyth_recordui_delete_selected (GtkButton *button, MMythRecordUI *recordui)
   280 {
   281 	GtkTreeSelection *selection;
   282 	GtkTreeModel *list_store;
   283 	GtkTreeIter iter;
   284 	int index;
   285 	int curr_page = 0;
   286 	
   287 	curr_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(recordui->notebook));
   288 
   289 	if ( curr_page == 0) {
   290 		selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(recordui->sch_treeview));
   291 		if (selection != NULL) {
   292 			gtk_tree_selection_get_selected(selection, &list_store, &iter);
   293 			gtk_tree_model_get(list_store, &iter, RECORD_ID_COLUMN, &index, -1);
   294 			gmyth_scheduler_delete_schedule(recordui->scheduler, index);
   295 			mmyth_recordui_reload_schedule (recordui);
   296 			return;
   297 		}
   298 			
   299 	} else if (curr_page == 1) {
   300 		selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(recordui->rec_treeview));
   301 		if (selection != NULL) {
   302 			gtk_tree_selection_get_selected(selection, &list_store, &iter);
   303 			gtk_tree_model_get(list_store, &iter, RECORD_ID_COLUMN, &index, -1);
   304 			gmyth_scheduler_delete_recorded(recordui->scheduler, index);
   305 			mmyth_recordui_reload_record (recordui);
   306 			return;
   307 		}
   308 	}
   309 	
   310 	g_warning ("[%s] None element was removed from the list", __FUNCTION__);
   311 }
   312 
   313 /* FIXME: change this function name, it is returning the 
   314  * basename_column that represents the nuv filename of 
   315  * the recorded content */
   316 gchar*
   317 mmyth_recordui_get_selected_recorded (MMythRecordUI *recordui)
   318 {
   319 	GtkTreeSelection *selection = NULL;
   320 	GtkTreeModel *list_store = NULL;
   321 	GtkTreeIter iter;
   322 	gchar *path = NULL;
   323 
   324 	/* returning nuv filename, basename_column */
   325 	selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(recordui->rec_treeview));
   326 	if (gtk_tree_selection_get_selected (selection, &list_store, &iter)) {
   327 		gtk_tree_model_get(list_store, &iter, BASENAME_COLUMN, &path, -1);
   328 	}
   329 
   330 	// FIXME: MOVE THIS TO OTHER PLACE
   331 	return path;
   332 }