maemo-ui/src/mmyth_ui.c
author rosfran
Wed Oct 25 00:26:33 2006 +0100 (2006-10-25)
branchtrunk
changeset 51 3e490ac963ce
child 208 c3c073032757
permissions -rw-r--r--
[svn r52] Added reference to the GSTINTERFACES_LIBS (gst_x_overlay* functions).
     1 
     2 #include <glib-object.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 <gtk/gtk.h>
     9 #include <gdk/gdkx.h>
    10 #include <gst/interfaces/xoverlay.h>
    11 #include <assert.h>
    12 
    13 #include "mmyth_ui.h"
    14 #include "mmyth_uicommon.h"
    15 #include "mmyth_schedulerui.h"
    16 #include "mmyth_recordui.h"
    17 #include "mmyth_uisettings.h"
    18 #include "mmyth_epg_grid_view.h"
    19 #include "mmyth_tvplayer.h"
    20 
    21 /* GMyth library includes */
    22 #include "gmyth_context.h"
    23 
    24 #ifndef MAEMO_PLATFORM
    25 static gint button_press_handler (GtkWidget *widget, GdkEvent *event);
    26 #endif
    27 
    28 static MMythUiCommon *create_main_view (MMythUi * mmyth_ui);
    29 static MMythUiCommon *create_video_view (MMythUi * mmyth_ui);
    30 static MMythUiCommon *create_epg_grid_view (MMythUi * mmyth_ui);
    31 static MMythUiCommon *create_record_view (MMythUi * mmyth_ui);
    32 static MMythUiCommon *create_schedule_view (MMythUi * mmyth_ui);
    33 
    34 static void cb_video_close_button (GtkButton * button, gpointer user_data);
    35 static void cb_record_button (GtkButton * button, gpointer user_data);
    36 static void cb_menu_item_settings (GtkMenuItem *menuitem, gpointer user_data);
    37 
    38 /* main box from button box separator*/
    39 static GtkWidget *main_vseparator = NULL;
    40 
    41 GdkPixbuf *icon_sports, *icon_news, 
    42           *icon_movies, *icon_shows, *icon_default;
    43 
    44 #ifndef MAEMO_PLATFORM
    45 /* FIXME: */
    46 static MMythUi *popup_mmyth_ui;
    47 #endif
    48 
    49 MMythUi *
    50 mmyth_ui_initialize (
    51 #ifdef MAEMO_PLATFORM
    52 	HildonProgram *program,
    53 #endif
    54 	GtkWidget * main_window)
    55 {
    56     MMythUi *mmyth_ui;
    57 
    58     mmyth_ui = g_new0 (MMythUi, 1);
    59 
    60     mmyth_ui->main_window = main_window;    
    61     mmyth_ui->videow = NULL;
    62     mmyth_ui->mmyth_recordui = NULL;
    63     mmyth_ui->mmyth_schedulerui = NULL;
    64 	
    65     /* Horizontal box that divides the view into control and video area */
    66     mmyth_ui->main_hbox = gtk_hbox_new (FALSE, 0);
    67     gtk_widget_show (mmyth_ui->main_hbox);
    68     g_object_ref (mmyth_ui->main_hbox);
    69 
    70     main_bg_color.red   = 65000;
    71     main_bg_color.green = 65000;
    72     main_bg_color.blue  = 65000;
    73     
    74     
    75 #ifndef MAEMO_PLATFORM
    76     /* Popup menu */
    77     popup_mmyth_ui = mmyth_ui;
    78     g_signal_connect (G_OBJECT (mmyth_ui->main_hbox), "event",
    79                       G_CALLBACK (button_press_handler),
    80                       G_OBJECT (mmyth_ui->main_hbox));    
    81 
    82 #else // #ifdef MAEMO
    83 
    84     mmyth_ui->main_menu = GTK_MENU(gtk_menu_new());
    85     hildon_program_set_common_menu(program, mmyth_ui->main_menu);
    86 
    87     mmyth_ui->menu_setup = gtk_menu_item_new_with_label("Setup");
    88     gtk_widget_set_size_request (GTK_WIDGET (mmyth_ui->menu_setup), 150, 40);
    89     gtk_menu_append(mmyth_ui->main_menu, mmyth_ui->menu_setup);
    90 
    91     g_signal_connect(G_OBJECT(mmyth_ui->menu_setup), "activate", G_CALLBACK(cb_menu_item_settings), mmyth_ui);
    92 
    93     gtk_widget_show_all (GTK_WIDGET (mmyth_ui->main_menu));
    94 #endif
    95     
    96     // Main widget is mmyth_ui->main_hbox
    97     mmyth_ui->main_uicommon       = create_main_view (mmyth_ui);
    98 
    99     gtk_container_add (GTK_CONTAINER (mmyth_ui->main_window),
   100                        mmyth_ui->main_hbox);
   101 
   102     mmyth_ui_set_widget (mmyth_ui, mmyth_ui->main_uicommon);
   103 
   104     return mmyth_ui;
   105 }
   106 
   107 void
   108 mmyth_ui_finalize (MMythUi * mmyth_ui)
   109 {
   110     if (mmyth_ui != NULL) {
   111     	if (mmyth_ui->main_uicommon)
   112 	    	mmyth_uicommon_free (mmyth_ui->main_uicommon);
   113     	if (mmyth_ui->video_uicommon)
   114 		    mmyth_uicommon_free (mmyth_ui->video_uicommon);
   115 		if (mmyth_ui->epg_grid_uicommon)
   116 		    mmyth_uicommon_free (mmyth_ui->epg_grid_uicommon);
   117 		if (mmyth_ui->record_uicommon)
   118 		    mmyth_uicommon_free (mmyth_ui->record_uicommon);	    
   119 		if (mmyth_ui->schedule_uicommon)
   120 		    mmyth_uicommon_free (mmyth_ui->schedule_uicommon);
   121 
   122 	    g_free (mmyth_ui);
   123     }
   124 }
   125 
   126 void
   127 mmyth_ui_set_widget (MMythUi * mmyth_ui, MMythUiCommon * new_uicommon)
   128 {
   129 	if (new_uicommon == NULL) {
   130 		g_warning ("MMythUI setting a NULL UI_Common as current display\n");
   131 		return;
   132 	}
   133 		
   134     if (mmyth_ui->current_uicommon) {
   135         if (mmyth_ui->current_uicommon == mmyth_ui->video_uicommon) {
   136             gtk_widget_hide (mmyth_ui->current_uicommon->main_widget);
   137             gtk_widget_hide (mmyth_ui->videow);
   138         }
   139         else {
   140             gtk_container_remove (GTK_CONTAINER (mmyth_ui->main_hbox),
   141                                   mmyth_ui->current_uicommon->main_widget);
   142         }
   143 
   144         gtk_container_remove (GTK_CONTAINER (mmyth_ui->main_hbox),
   145                               mmyth_ui->current_uicommon->event_box);
   146         gtk_container_remove (GTK_CONTAINER (mmyth_ui->main_hbox),
   147                               main_vseparator);        
   148 
   149     }
   150 
   151     if (new_uicommon->main_widget == mmyth_ui->video_alignment) {
   152         //gst_player_video_show (GST_PLAYER_VIDEO(mmyth_ui->videow));
   153         gtk_widget_show (mmyth_ui->video_alignment);
   154         gtk_widget_show (mmyth_ui->videow);
   155     }
   156     else {
   157         /* FIXME: Fst call is NULL when mmyth_player_init fails */
   158         if(mmyth_ui->video_alignment != NULL)
   159             gtk_widget_hide (mmyth_ui->video_alignment);
   160         /* FIXME: Fst call is NULL when mmyth_player_init fails */
   161         if(mmyth_ui->videow != NULL)
   162             gtk_widget_hide (mmyth_ui->videow);
   163 
   164         gtk_box_pack_start (GTK_BOX (mmyth_ui->main_hbox),
   165                             new_uicommon->main_widget, TRUE, TRUE, 0);
   166     }
   167 
   168     if(main_vseparator == NULL) {
   169         /* FIXME: should free this variable*/
   170         main_vseparator = gtk_vseparator_new();
   171         g_object_ref (main_vseparator);                   
   172     }  
   173     gtk_box_pack_start (GTK_BOX (mmyth_ui->main_hbox),
   174                         main_vseparator, FALSE, FALSE, 0);    
   175     gtk_widget_show (main_vseparator);
   176 
   177     gtk_box_pack_end (GTK_BOX (mmyth_ui->main_hbox), new_uicommon->event_box, FALSE,
   178                       FALSE, 0);         
   179 
   180     mmyth_ui->current_uicommon = new_uicommon;
   181 
   182 }
   183 
   184 /* The close callback is the same for all windows*/
   185 static void
   186 cb_not_impl_button (GtkButton * button, gpointer user_data)
   187 {
   188     MMythUi *mmyth_ui = (MMythUi *) user_data;
   189 
   190     GtkWidget *msg_dialog = gtk_message_dialog_new (
   191                                            GTK_WINDOW(mmyth_ui->main_window),
   192                                            GTK_DIALOG_MODAL |
   193                                            GTK_DIALOG_DESTROY_WITH_PARENT,
   194                                            GTK_MESSAGE_INFO,
   195                                            GTK_BUTTONS_OK,
   196                                            "Feature not implemented");
   197     gtk_widget_set_size_request (msg_dialog, 350, 120);
   198 
   199     gtk_dialog_run (GTK_DIALOG (msg_dialog));
   200 
   201     gtk_widget_destroy(GTK_WIDGET(msg_dialog)); 
   202 }
   203 
   204 /******************************************************************************
   205  *                        POPUP MENU WIDGET METHODS                           *
   206  *****************************************************************************/
   207 
   208 static void 
   209 cb_menu_item_settings (GtkMenuItem *menuitem,
   210                             gpointer user_data)
   211 {
   212     
   213     MMythUi *mmyth_ui = (MMythUi*) user_data;
   214    
   215     if (mmyth_uisettings_run (GTK_WINDOW (mmyth_ui->main_window))) {
   216     	// If user changes the settings, we restart the context
   217     	g_debug ("[%s] Restarting mmyth_context to new settings", __FUNCTION__);
   218     	gmyth_context_initialize();
   219     }
   220 }
   221 
   222 #ifndef MAEMO_PLATFORM
   223 
   224 static void 
   225 detacher (GtkWidget *attach_widget,GtkMenu *menu) 
   226 {
   227 	
   228 }
   229 
   230 static void
   231 do_popup_menu (GtkWidget *my_widget, GdkEventButton *event)
   232 {
   233     GtkWidget *popup;
   234 
   235   int button, event_time;
   236 
   237   GtkWidget *item_general;
   238   GtkWidget *image;
   239 
   240   popup = gtk_menu_new ();
   241 
   242   item_general = gtk_image_menu_item_new_with_mnemonic ("Setup");
   243   gtk_widget_show (item_general);
   244   gtk_container_add (GTK_CONTAINER (popup), item_general);
   245 
   246   image = gtk_image_new_from_stock ("gtk-edit", GTK_ICON_SIZE_MENU);
   247   gtk_widget_show (image);
   248   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item_general), image);
   249 
   250   g_signal_connect (G_OBJECT(item_general), "activate", G_CALLBACK (cb_menu_item_settings), popup_mmyth_ui);
   251 
   252     if (event) {
   253       button = event->button;
   254       event_time = event->time;
   255     } else {
   256       button = 0;
   257       event_time = gtk_get_current_event_time ();
   258     }
   259     
   260     gtk_menu_attach_to_widget (GTK_MENU (popup), my_widget, detacher);
   261     gtk_menu_popup (GTK_MENU (popup), NULL, NULL, NULL, NULL, 
   262                   button, event_time);
   263     gtk_widget_show_all(popup);
   264 }
   265 
   266 /* Respond to a button-press by posting a menu passed in as widget.
   267  *
   268  * Note that the "widget" argument is the menu being posted, NOT
   269  * the button that was pressed.
   270  */
   271 static gint 
   272 button_press_handler (GtkWidget *widget, GdkEvent *event)
   273 {
   274 
   275     if (event->type == GDK_BUTTON_PRESS) {
   276       GdkEventButton *bevent = (GdkEventButton *) event; 
   277           /* Ignore double-clicks and triple-clicks */
   278       if (bevent->button == 3)
   279         {
   280           do_popup_menu (widget, bevent);
   281           return TRUE;
   282         }
   283     }
   284 
   285     /* Tell calling code that we have not handled this event; pass it on. */
   286     return FALSE;
   287 }
   288 #endif
   289 
   290 /******************************************************************************
   291  *                          MAIN APP VIEW METHODS                             *
   292  *****************************************************************************/
   293 
   294 static void
   295 cb_close_button (GtkButton * button, gpointer user_data)
   296 {
   297     MMythUi *mmyth_ui = (MMythUi *) user_data;
   298 
   299     mmyth_ui_set_widget (mmyth_ui, mmyth_ui->main_uicommon);
   300 }
   301 
   302 static void
   303 cb_watch_tv_button (GtkButton * button, gpointer user_data)
   304 {
   305     MMythUi *mmyth_ui = (MMythUi *) user_data;
   306 	gboolean res = FALSE;
   307 	
   308 	if (!(mmyth_ui->video_uicommon))
   309 		mmyth_ui->video_uicommon = create_video_view (mmyth_ui);
   310 	
   311 	// Creates the tv player that will retrieve the mythtv content, decode and show it
   312 	mmyth_ui->tvplayer = mmyth_tvplayer_new ();
   313 	/* choose here if this is a LiveTV session */
   314 	mmyth_ui->tvplayer->is_livetv = TRUE;
   315 
   316 	res = mmyth_tvplayer_initialize (mmyth_ui->tvplayer);
   317 
   318 	if (!res) {
   319 		g_warning ("[%s] Could not initialize tvplayer", __FUNCTION__);
   320 
   321 		g_object_unref (mmyth_ui->tvplayer);
   322 		mmyth_ui->tvplayer = NULL;
   323 
   324 		GtkWidget *dialog = gtk_message_dialog_new (
   325 				GTK_WINDOW(mmyth_ui->main_window),
   326 				GTK_DIALOG_DESTROY_WITH_PARENT,
   327                 GTK_MESSAGE_ERROR,
   328                 GTK_BUTTONS_CLOSE,
   329 			 	"MMyth found errors while starting TV Player, please check "
   330 			 	"the GStreamer installation");
   331 
   332 		gtk_dialog_run (GTK_DIALOG (dialog));
   333 		gtk_widget_destroy (dialog);
   334 		
   335 		return;
   336 	}
   337 	//res = mmyth_tvplayer_livetv_setup (mmyth_ui->tvplayer);
   338 	//
   339 	if (mmyth_ui && mmyth_ui->tvplayer && res) {
   340 		mmyth_tvplayer_set_widget (mmyth_ui->tvplayer, mmyth_ui->videow);
   341 		mmyth_tvplayer_start_playing (mmyth_ui->tvplayer);
   342 	} else {
   343 		// TODO: Show Alert with error description!
   344 		g_warning ("[%s] MMythUI can't initialize tv_player", __FUNCTION__);
   345 		g_object_unref (mmyth_ui->tvplayer);
   346 		mmyth_ui->tvplayer = NULL;
   347 		// FIXME: Show the exact error that happened
   348 		GtkWidget *dialog = gtk_message_dialog_new (
   349 				GTK_WINDOW(mmyth_ui->main_window),
   350 				GTK_DIALOG_DESTROY_WITH_PARENT,
   351                 GTK_MESSAGE_ERROR,
   352                 GTK_BUTTONS_CLOSE,
   353                 "Error while starting TV Player, please check if the backend"
   354                 " is running properly and a tv card is available!");
   355 		gtk_dialog_run (GTK_DIALOG (dialog));
   356 		gtk_widget_destroy (dialog);		
   357 		return;
   358 	}
   359 	mmyth_ui_set_widget (mmyth_ui, mmyth_ui->video_uicommon);
   360 	
   361 }
   362 
   363 static void 
   364 cb_epg_button (GtkButton * button, gpointer user_data)
   365 {
   366     MMythUi *mmyth_ui = (MMythUi *) user_data;
   367     
   368     if (!(mmyth_ui->epg_grid_uicommon))
   369     	mmyth_ui->epg_grid_uicommon = create_epg_grid_view(mmyth_ui);
   370     	
   371     mmyth_ui_set_widget (mmyth_ui, mmyth_ui->epg_grid_uicommon);
   372 }
   373 
   374 static MMythUiCommon *
   375 create_main_view (MMythUi * mmyth_ui)
   376 {
   377     MMythUiCommon *ui_common;
   378     GtkWidget *main_widget;
   379     GtkWidget *image;
   380 
   381 	g_debug ("Creating Main UI Common");
   382 	
   383     // FIXME: file path
   384 #ifdef MMYTH_DEV
   385     image = gtk_image_new_from_file ("../pixmaps/mmyth_logo.png");
   386 #else
   387     image = gtk_image_new_from_file ( PIX_DIR "mmyth_logo.png");
   388 #endif
   389     
   390     main_widget = gtk_event_box_new();
   391     
   392     gtk_container_add (GTK_CONTAINER (main_widget),
   393                        image);
   394 
   395 
   396     gtk_widget_show_all (main_widget);
   397     g_object_ref (main_widget);
   398 
   399     ui_common = mmyth_uicommon_new (main_widget,
   400                                     "Watch TV", "EPG", "Recording");
   401 
   402     /* Button signals */
   403     // FIXME
   404     g_signal_connect (G_OBJECT (ui_common->button1), "clicked",
   405                       G_CALLBACK (cb_watch_tv_button), mmyth_ui);
   406     g_signal_connect (G_OBJECT (ui_common->button2), "clicked",
   407                       G_CALLBACK (cb_epg_button), mmyth_ui);
   408     g_signal_connect (G_OBJECT (ui_common->button3), "clicked",
   409                       G_CALLBACK (cb_record_button), mmyth_ui);
   410 
   411     return ui_common;
   412 
   413 }
   414 
   415 /******************************************************************************
   416  *                         epg GRID VIEW METHODS                              *
   417  *****************************************************************************/
   418 
   419 static MMythUiCommon *
   420 create_epg_grid_view (MMythUi * mmyth_ui)
   421 {
   422     MMythUiCommon *ui_common;        
   423 
   424 	g_debug ("Creating EPG Grid UI Common");
   425 	
   426     GtkWidget *epg_grid_view = GTK_WIDGET (epg_grid_view_new (mmyth_ui));
   427     
   428     ui_common = mmyth_uicommon_new (epg_grid_view,
   429                                     "Play", "Record",
   430                                     "Close");    
   431     
   432     /* Button signals */
   433     g_signal_connect (G_OBJECT (ui_common->button1), "clicked",
   434                       G_CALLBACK (cb_not_impl_button), mmyth_ui);
   435     g_signal_connect (G_OBJECT (ui_common->button2), "clicked",
   436                       G_CALLBACK (cb_record_button), mmyth_ui);
   437     g_signal_connect (G_OBJECT (ui_common->button3), "clicked",
   438                       G_CALLBACK (cb_close_button), mmyth_ui);
   439     
   440     return ui_common;
   441 }
   442 /******************************************************************************
   443  *                         SCHEDULE VIEW METHODS                                *
   444  ******************************************************************************/
   445 
   446 static void
   447 cb_save_new_schedule (GtkButton *button, gpointer user_data)
   448 {
   449     MMythUi *mmyth_ui = (MMythUi *) user_data;
   450 
   451 	if (!(mmyth_ui->schedule_uicommon))
   452 		mmyth_ui->schedule_uicommon = create_schedule_view(mmyth_ui);
   453 		
   454     mmyth_schedulerui_save (mmyth_ui->mmyth_schedulerui);
   455     
   456     mmyth_recordui_reload_schedule (mmyth_ui->mmyth_recordui);
   457     
   458     mmyth_ui_set_widget (mmyth_ui, mmyth_ui->record_uicommon);
   459     
   460 }
   461 
   462 static void
   463 cb_edit_scheduled (GtkTreeView * tree_view, GtkTreePath *path, 
   464 		           GtkTreeViewColumn *column, gpointer user_data)
   465 {
   466     MMythUi *mmyth_ui = (MMythUi *) user_data;
   467     GtkTreeSelection *selection;
   468 	GtkTreeModel *list_store;
   469 	GtkTreeIter iter;
   470 	int index;
   471     //gint new_row = 0, record_id = 0;
   472 	ScheduleInfo *schedule_info;
   473 	GList *schedule_list;
   474 	//GtkTreeIter iter;
   475 	gint res;
   476 
   477 	//gtk_tree_store_clear(recordui->sch_tree_store);	
   478 
   479 	res = gmyth_scheduler_get_schedule_list(mmyth_ui->mmyth_recordui->scheduler, &(schedule_list));
   480 	if (res < 0) {
   481 		g_warning ("[%s] Retrieved NULL list of scheduled data from database", __FUNCTION__);
   482 		//return FALSE;
   483 	}
   484 	printf("\nX %d", res); fflush(stdout);
   485     
   486    	selection = 
   487         gtk_tree_view_get_selection(GTK_TREE_VIEW(mmyth_ui->mmyth_recordui->sch_treeview));
   488     
   489     gtk_tree_selection_get_selected(selection, &list_store, &iter);
   490 	gtk_tree_model_get(list_store, &iter, 4, &index, -1);
   491     
   492     mmyth_ui_set_widget (mmyth_ui, mmyth_ui->schedule_uicommon);
   493     
   494 	if (!(mmyth_ui->schedule_uicommon))
   495 		mmyth_ui->schedule_uicommon = create_schedule_view(mmyth_ui);
   496 		
   497 	schedule_list = g_list_nth(schedule_list, atoi(gtk_tree_path_to_string(path)));
   498 	schedule_info = (ScheduleInfo*) schedule_list->data;
   499 
   500     printf("\nstarttime: %ld", schedule_info->start_time); fflush(stdout);		
   501 }
   502 
   503 static MMythUiCommon *
   504 create_schedule_view (MMythUi * mmyth_ui)
   505 {
   506     MMythUiCommon *ui_common;        
   507     GtkWidget *schedule_widget;
   508     
   509     g_debug ("Creating Schedule UI Common");
   510 
   511 	mmyth_ui->mmyth_schedulerui = mmyth_schedulerui_new ();
   512 	if (mmyth_ui->mmyth_schedulerui == NULL) {
   513 		g_warning ("[%s] Error while creating scheduler ui", __FUNCTION__);
   514 		return NULL;
   515 	}
   516 	
   517 	schedule_widget = mmyth_ui->mmyth_schedulerui->main_widget;
   518 
   519     gtk_widget_show_all (schedule_widget);
   520     g_object_ref (schedule_widget);
   521     
   522     ui_common = mmyth_uicommon_new (schedule_widget,
   523                                     "Save", "Clear",
   524                                     "Cancel");    
   525 
   526     /* Button signals */
   527     g_signal_connect (G_OBJECT (ui_common->button1), "clicked",
   528                       G_CALLBACK (cb_save_new_schedule), mmyth_ui);
   529     g_signal_connect (G_OBJECT (ui_common->button2), "clicked",
   530                       G_CALLBACK (cb_not_impl_button), mmyth_ui);
   531     g_signal_connect (G_OBJECT (ui_common->button3), "clicked",
   532                       G_CALLBACK (cb_record_button), mmyth_ui);
   533     
   534     return ui_common;
   535 }
   536 /******************************************************************************
   537  *                         RECORD VIEW METHODS                                *
   538  ******************************************************************************/
   539 static void
   540 cb_record_button (GtkButton * button, gpointer user_data)
   541 {
   542     MMythUi *mmyth_ui = (MMythUi *) user_data;
   543 
   544 	if (!(mmyth_ui->record_uicommon)) {
   545 		mmyth_ui->record_uicommon = create_record_view (mmyth_ui);
   546 		mmyth_ui->schedule_uicommon = create_schedule_view (mmyth_ui);
   547 	}
   548 	
   549 	mmyth_recordui_reload_all (mmyth_ui->mmyth_recordui);
   550 	
   551     mmyth_ui_set_widget (mmyth_ui, mmyth_ui->record_uicommon);
   552 
   553 } 
   554 
   555 static void
   556 cb_record_close_button (GtkButton * button, gpointer user_data)
   557 {
   558     MMythUi *mmyth_ui = (MMythUi *) user_data;
   559 
   560 	mmyth_ui_set_widget(mmyth_ui, mmyth_ui->main_uicommon);
   561 
   562 	mmyth_recordui_free(mmyth_ui->mmyth_recordui);
   563 	
   564 	if (mmyth_ui->record_uicommon) {
   565 		gtk_widget_destroy (mmyth_ui->record_uicommon->main_widget);
   566 		mmyth_uicommon_free(mmyth_ui->record_uicommon);
   567 		mmyth_ui->record_uicommon = NULL;
   568 	}
   569 
   570 	if (mmyth_ui->schedule_uicommon) {
   571 	//	mmyth_uicommon_free(mmyth_ui->schedule_uicommon);
   572 	}	
   573 } 
   574 
   575 
   576 
   577 
   578 static void
   579 play_selected_recorded (gpointer user_data)
   580 {
   581     MMythUi *mmyth_ui = (MMythUi *) user_data;
   582     gboolean res = FALSE;
   583 
   584     gchar *path = mmyth_recordui_get_selected_recorded (mmyth_ui->mmyth_recordui);
   585     
   586     if (path == NULL) {
   587     	// This should never happens. Play button is just activated when
   588     	// a recording is selected.
   589     	g_warning ("[%s] Play button pressed while none recorded is selected", __FUNCTION__);
   590     	return;
   591     }
   592 
   593 	if (!(mmyth_ui->video_uicommon))
   594 		mmyth_ui->video_uicommon = create_video_view (mmyth_ui);
   595 	
   596 	// Creates the tv player that will retrieve the mythtv content, decode and show it
   597 	mmyth_ui->tvplayer = mmyth_tvplayer_new ();
   598 	g_debug ("[%s] New TV Player created: %d\n", __FUNCTION__, (int) (mmyth_ui->tvplayer));
   599 	res = mmyth_tvplayer_initialize (mmyth_ui->tvplayer);
   600 	if (!res) {
   601 		g_warning ("[%s] Could not initialize tvplayer", __FUNCTION__);
   602 
   603 		g_object_unref (mmyth_ui->tvplayer);
   604 		mmyth_ui->tvplayer = NULL;
   605 
   606 		GtkWidget *dialog = gtk_message_dialog_new (
   607 				GTK_WINDOW(mmyth_ui->main_window),
   608 				GTK_DIALOG_DESTROY_WITH_PARENT,
   609                 GTK_MESSAGE_ERROR,
   610                 GTK_BUTTONS_CLOSE,
   611 			 	"MMyth found errors while starting TV Player, please check "
   612 			 	"the GStreamer installation");
   613 
   614 		gtk_dialog_run (GTK_DIALOG (dialog));
   615 		gtk_widget_destroy (dialog);
   616 		
   617 		return;
   618 	}		
   619 
   620 	res = mmyth_tvplayer_record_setup (mmyth_ui->tvplayer, path);
   621 	
   622 	if (mmyth_ui && mmyth_ui->tvplayer && res) {
   623 		mmyth_tvplayer_set_widget (mmyth_ui->tvplayer, mmyth_ui->videow);
   624 		mmyth_tvplayer_start_playing (mmyth_ui->tvplayer);
   625 	} else {
   626 		// TODO: Show Alert with error description!
   627 		g_warning ("[%s] MMythUI can't initialize tv_player", __FUNCTION__);
   628 		g_object_unref (mmyth_ui->tvplayer);
   629 		mmyth_ui->tvplayer = NULL;
   630 		// FIXME: Show the exact error that happened
   631 		GtkWidget *dialog = gtk_message_dialog_new (
   632 				GTK_WINDOW(mmyth_ui->main_window),
   633 				GTK_DIALOG_DESTROY_WITH_PARENT,
   634                 GTK_MESSAGE_ERROR,
   635                 GTK_BUTTONS_CLOSE,
   636                 "Error while starting TV Player, please check if the backend"
   637                 " is running properly and a tv card is available!");
   638 		gtk_dialog_run (GTK_DIALOG (dialog));
   639 		gtk_widget_destroy (dialog);		
   640 		return;
   641 	}
   642 	
   643 	mmyth_ui_set_widget (mmyth_ui, mmyth_ui->video_uicommon);    
   644 }
   645 
   646 static void
   647 cb_play_clicked_recorded (GtkTreeView * tree_view, GtkTreePath *path, 
   648 		GtkTreeViewColumn *column, gpointer user_data)
   649 {
   650 	play_selected_recorded (user_data);
   651 }
   652 
   653 static void
   654 cb_play_selected (GtkButton * button, gpointer user_data)
   655 {
   656 	play_selected_recorded (user_data);
   657 }
   658 
   659 static void
   660 cb_schedule_button (GtkButton * button, gpointer user_data)
   661 {
   662     MMythUi *mmyth_ui = (MMythUi *) user_data;
   663 
   664     mmyth_ui_set_widget (mmyth_ui, mmyth_ui->schedule_uicommon);
   665 }
   666 
   667 void
   668 cb_switch_page (GtkNotebook *notebook, GtkNotebookPage *page,
   669 		guint page_num, gpointer user_data)
   670 {
   671     MMythUi *mmyth_ui = (MMythUi *) user_data;
   672     MMythUiCommon *ui_common; 
   673     
   674 	assert (mmyth_ui);
   675 	assert (mmyth_ui->record_uicommon);
   676 	
   677     ui_common = mmyth_ui->record_uicommon;
   678     
   679     if (page_num == 0) { // Switched to Schedule list
   680     	gtk_button_set_label (GTK_BUTTON (ui_common->button1), "New");
   681 		g_signal_handlers_disconnect_by_func (
   682 			G_OBJECT (ui_common->button1), G_CALLBACK (cb_play_selected), mmyth_ui);
   683 	    g_signal_connect (G_OBJECT (ui_common->button1), "clicked",
   684             G_CALLBACK (cb_schedule_button), mmyth_ui);
   685     } else if (page_num == 1) {
   686     	gtk_button_set_label (GTK_BUTTON (ui_common->button1), "Play");
   687 		g_signal_handlers_disconnect_by_func (
   688 			G_OBJECT (ui_common->button1), G_CALLBACK (cb_schedule_button), mmyth_ui);
   689 	    g_signal_connect (G_OBJECT (ui_common->button1), "clicked",
   690             G_CALLBACK (cb_play_selected), mmyth_ui);
   691     }
   692 }
   693                                             
   694                                             
   695 static MMythUiCommon *
   696 create_record_view (MMythUi * mmyth_ui)
   697 {
   698     MMythUiCommon *ui_common;        
   699     
   700    	g_debug ("Creating Record UI Common");
   701    	
   702 	mmyth_ui->mmyth_recordui = mmyth_recordui_new ();
   703 
   704 	// FIXME: Change MMythRecordUI to a GtkWidget child and avoid this call!
   705     gtk_widget_show_all (mmyth_ui->mmyth_recordui->scrolled_window);
   706     
   707     ui_common = mmyth_uicommon_new (mmyth_ui->mmyth_recordui->scrolled_window, "New", "Delete", "<<Back");    
   708     g_object_ref (mmyth_ui->mmyth_recordui->scrolled_window);
   709         
   710     /* Button signals */
   711     g_signal_connect (G_OBJECT (ui_common->button1), "clicked",
   712                       G_CALLBACK (cb_schedule_button), mmyth_ui);
   713     g_signal_connect (G_OBJECT (ui_common->button2), "clicked",
   714                       G_CALLBACK (mmyth_recordui_delete_selected), mmyth_ui->mmyth_recordui);
   715     g_signal_connect (G_OBJECT (ui_common->button3), "clicked",
   716                       G_CALLBACK (cb_record_close_button), mmyth_ui);
   717     g_signal_connect (G_OBJECT (mmyth_ui->mmyth_recordui->notebook),
   718 		      "switch-page", G_CALLBACK (cb_switch_page), mmyth_ui);
   719     g_signal_connect (G_OBJECT (mmyth_ui->mmyth_recordui->rec_treeview),
   720 		      "row-activated", G_CALLBACK (cb_play_clicked_recorded), mmyth_ui);
   721     g_signal_connect (G_OBJECT (mmyth_ui->mmyth_recordui->sch_treeview),
   722 		      "row-activated", G_CALLBACK (cb_edit_scheduled), mmyth_ui);
   723     return ui_common;
   724 }
   725 
   726 
   727 /******************************************************************************
   728  *                         GST VIDEO WIDGET METHODS                           *
   729  *****************************************************************************/
   730 
   731 static void
   732 cb_video_close_button (GtkButton * button, gpointer user_data)
   733 {
   734     MMythUi *mmyth_ui = (MMythUi *) user_data;
   735 	
   736 	g_debug ("MMythUI video close button pressed");
   737 	
   738 	if (mmyth_ui && mmyth_ui->tvplayer) {
   739 	    mmyth_tvplayer_stop_playing (mmyth_ui->tvplayer);
   740 
   741 		g_object_unref (mmyth_ui->tvplayer);
   742 		mmyth_ui->tvplayer = NULL;	    
   743 	} else {
   744 		g_warning ("cb_video_close_button called with NULL pointer\n");
   745 	}
   746 	
   747     mmyth_ui_set_widget (mmyth_ui, mmyth_ui->main_uicommon);
   748 }
   749 
   750 
   751 static MMythUiCommon *
   752 create_video_view (MMythUi * mmyth_ui)
   753 {
   754 
   755     MMythUiCommon *ui_common;
   756 
   757 	g_debug ("Creating Video UI Common");
   758 	
   759 	/* Creates widget to be user by MMythTVPlayer to draw the video */
   760     mmyth_ui->videow = gtk_drawing_area_new ();
   761     // FIXME: Get the widget size from settings
   762     gtk_widget_set_size_request (mmyth_ui->videow, 300, 240);
   763 
   764     //mmiptv_ui->logo = gdk_pixbuf_new_from_file ("logo.png", NULL);
   765 
   766 	// Creates an alignment to place the video widget inside
   767     mmyth_ui->video_alignment = gtk_alignment_new (0.5, 0.5, 1, 1);
   768     gtk_widget_hide (mmyth_ui->video_alignment);
   769 
   770     gtk_container_add (GTK_CONTAINER (mmyth_ui->video_alignment),
   771                        mmyth_ui->videow);
   772 
   773     /* Add the gst video widget to hbox. It should never be removed. */    
   774     /* FIXME: mmyth_ui->video_alignment == NULL when mmyth_player_init fails */
   775     if((mmyth_ui->main_hbox != NULL) && (mmyth_ui->video_alignment != NULL)) {
   776         gtk_box_pack_start (GTK_BOX (mmyth_ui->main_hbox),
   777                             mmyth_ui->video_alignment, TRUE, TRUE, 0);    
   778 	} else {
   779 		g_warning ("[%s] Error while adding video_alignment to main widget", __FUNCTION__);	
   780     }
   781 
   782     g_object_ref (mmyth_ui->videow);
   783     g_object_ref (mmyth_ui->video_alignment);
   784 
   785     ui_common = mmyth_uicommon_new (mmyth_ui->video_alignment,
   786                                     "  Full\nScreen", "Other\nServices",
   787                                     "Close");
   788                                     
   789     
   790     g_signal_connect (G_OBJECT (ui_common->button1), "clicked",
   791                       G_CALLBACK (cb_not_impl_button), mmyth_ui);
   792     g_signal_connect (G_OBJECT (ui_common->button2), "clicked",
   793                       G_CALLBACK (cb_not_impl_button), mmyth_ui);    
   794     g_signal_connect (G_OBJECT (ui_common->button3), "clicked",
   795                       G_CALLBACK (cb_video_close_button), mmyth_ui);
   796 
   797 	if (ui_common)
   798 		g_debug ("Video UI_Common sucessfull created");
   799 		
   800     return ui_common;
   801 }
   802 
   803 
   804 
   805 GtkWidget*
   806 mmyth_ui_get_video_widget (MMythUi *mmyth_ui) {
   807 	
   808 	if (mmyth_ui && mmyth_ui->videow) {
   809 	
   810 		return mmyth_ui->videow;
   811 	}
   812 	
   813 	return NULL;	
   814 }