maemo-ui/src/mmyth_ui.c
branchtrunk
changeset 32 6b9c77b9817e
child 208 c3c073032757
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/maemo-ui/src/mmyth_ui.c	Wed Oct 18 20:38:47 2006 +0100
     1.3 @@ -0,0 +1,814 @@
     1.4 +
     1.5 +#include <glib-object.h>
     1.6 +#include <sys/types.h>
     1.7 +#include <sys/stat.h>
     1.8 +#include <unistd.h>
     1.9 +#include <string.h>
    1.10 +#include <stdio.h>
    1.11 +#include <gtk/gtk.h>
    1.12 +#include <gdk/gdkx.h>
    1.13 +#include <gst/interfaces/xoverlay.h>
    1.14 +#include <assert.h>
    1.15 +
    1.16 +#include "mmyth_ui.h"
    1.17 +#include "mmyth_uicommon.h"
    1.18 +#include "mmyth_schedulerui.h"
    1.19 +#include "mmyth_recordui.h"
    1.20 +#include "mmyth_uisettings.h"
    1.21 +#include "mmyth_epg_grid_view.h"
    1.22 +#include "mmyth_tvplayer.h"
    1.23 +
    1.24 +/* GMyth library includes */
    1.25 +#include "gmyth_context.h"
    1.26 +
    1.27 +#ifndef MAEMO_PLATFORM
    1.28 +static gint button_press_handler (GtkWidget *widget, GdkEvent *event);
    1.29 +#endif
    1.30 +
    1.31 +static MMythUiCommon *create_main_view (MMythUi * mmyth_ui);
    1.32 +static MMythUiCommon *create_video_view (MMythUi * mmyth_ui);
    1.33 +static MMythUiCommon *create_epg_grid_view (MMythUi * mmyth_ui);
    1.34 +static MMythUiCommon *create_record_view (MMythUi * mmyth_ui);
    1.35 +static MMythUiCommon *create_schedule_view (MMythUi * mmyth_ui);
    1.36 +
    1.37 +static void cb_video_close_button (GtkButton * button, gpointer user_data);
    1.38 +static void cb_record_button (GtkButton * button, gpointer user_data);
    1.39 +static void cb_menu_item_settings (GtkMenuItem *menuitem, gpointer user_data);
    1.40 +
    1.41 +/* main box from button box separator*/
    1.42 +static GtkWidget *main_vseparator = NULL;
    1.43 +
    1.44 +GdkPixbuf *icon_sports, *icon_news, 
    1.45 +          *icon_movies, *icon_shows, *icon_default;
    1.46 +
    1.47 +#ifndef MAEMO_PLATFORM
    1.48 +/* FIXME: */
    1.49 +static MMythUi *popup_mmyth_ui;
    1.50 +#endif
    1.51 +
    1.52 +MMythUi *
    1.53 +mmyth_ui_initialize (
    1.54 +#ifdef MAEMO_PLATFORM
    1.55 +	HildonProgram *program,
    1.56 +#endif
    1.57 +	GtkWidget * main_window)
    1.58 +{
    1.59 +    MMythUi *mmyth_ui;
    1.60 +
    1.61 +    mmyth_ui = g_new0 (MMythUi, 1);
    1.62 +
    1.63 +    mmyth_ui->main_window = main_window;    
    1.64 +    mmyth_ui->videow = NULL;
    1.65 +    mmyth_ui->mmyth_recordui = NULL;
    1.66 +    mmyth_ui->mmyth_schedulerui = NULL;
    1.67 +	
    1.68 +    /* Horizontal box that divides the view into control and video area */
    1.69 +    mmyth_ui->main_hbox = gtk_hbox_new (FALSE, 0);
    1.70 +    gtk_widget_show (mmyth_ui->main_hbox);
    1.71 +    g_object_ref (mmyth_ui->main_hbox);
    1.72 +
    1.73 +    main_bg_color.red   = 65000;
    1.74 +    main_bg_color.green = 65000;
    1.75 +    main_bg_color.blue  = 65000;
    1.76 +    
    1.77 +    
    1.78 +#ifndef MAEMO_PLATFORM
    1.79 +    /* Popup menu */
    1.80 +    popup_mmyth_ui = mmyth_ui;
    1.81 +    g_signal_connect (G_OBJECT (mmyth_ui->main_hbox), "event",
    1.82 +                      G_CALLBACK (button_press_handler),
    1.83 +                      G_OBJECT (mmyth_ui->main_hbox));    
    1.84 +
    1.85 +#else // #ifdef MAEMO
    1.86 +
    1.87 +    mmyth_ui->main_menu = GTK_MENU(gtk_menu_new());
    1.88 +    hildon_program_set_common_menu(program, mmyth_ui->main_menu);
    1.89 +
    1.90 +    mmyth_ui->menu_setup = gtk_menu_item_new_with_label("Setup");
    1.91 +    gtk_widget_set_size_request (GTK_WIDGET (mmyth_ui->menu_setup), 150, 40);
    1.92 +    gtk_menu_append(mmyth_ui->main_menu, mmyth_ui->menu_setup);
    1.93 +
    1.94 +    g_signal_connect(G_OBJECT(mmyth_ui->menu_setup), "activate", G_CALLBACK(cb_menu_item_settings), mmyth_ui);
    1.95 +
    1.96 +    gtk_widget_show_all (GTK_WIDGET (mmyth_ui->main_menu));
    1.97 +#endif
    1.98 +    
    1.99 +    // Main widget is mmyth_ui->main_hbox
   1.100 +    mmyth_ui->main_uicommon       = create_main_view (mmyth_ui);
   1.101 +
   1.102 +    gtk_container_add (GTK_CONTAINER (mmyth_ui->main_window),
   1.103 +                       mmyth_ui->main_hbox);
   1.104 +
   1.105 +    mmyth_ui_set_widget (mmyth_ui, mmyth_ui->main_uicommon);
   1.106 +
   1.107 +    return mmyth_ui;
   1.108 +}
   1.109 +
   1.110 +void
   1.111 +mmyth_ui_finalize (MMythUi * mmyth_ui)
   1.112 +{
   1.113 +    if (mmyth_ui != NULL) {
   1.114 +    	if (mmyth_ui->main_uicommon)
   1.115 +	    	mmyth_uicommon_free (mmyth_ui->main_uicommon);
   1.116 +    	if (mmyth_ui->video_uicommon)
   1.117 +		    mmyth_uicommon_free (mmyth_ui->video_uicommon);
   1.118 +		if (mmyth_ui->epg_grid_uicommon)
   1.119 +		    mmyth_uicommon_free (mmyth_ui->epg_grid_uicommon);
   1.120 +		if (mmyth_ui->record_uicommon)
   1.121 +		    mmyth_uicommon_free (mmyth_ui->record_uicommon);	    
   1.122 +		if (mmyth_ui->schedule_uicommon)
   1.123 +		    mmyth_uicommon_free (mmyth_ui->schedule_uicommon);
   1.124 +
   1.125 +	    g_free (mmyth_ui);
   1.126 +    }
   1.127 +}
   1.128 +
   1.129 +void
   1.130 +mmyth_ui_set_widget (MMythUi * mmyth_ui, MMythUiCommon * new_uicommon)
   1.131 +{
   1.132 +	if (new_uicommon == NULL) {
   1.133 +		g_warning ("MMythUI setting a NULL UI_Common as current display\n");
   1.134 +		return;
   1.135 +	}
   1.136 +		
   1.137 +    if (mmyth_ui->current_uicommon) {
   1.138 +        if (mmyth_ui->current_uicommon == mmyth_ui->video_uicommon) {
   1.139 +            gtk_widget_hide (mmyth_ui->current_uicommon->main_widget);
   1.140 +            gtk_widget_hide (mmyth_ui->videow);
   1.141 +        }
   1.142 +        else {
   1.143 +            gtk_container_remove (GTK_CONTAINER (mmyth_ui->main_hbox),
   1.144 +                                  mmyth_ui->current_uicommon->main_widget);
   1.145 +        }
   1.146 +
   1.147 +        gtk_container_remove (GTK_CONTAINER (mmyth_ui->main_hbox),
   1.148 +                              mmyth_ui->current_uicommon->event_box);
   1.149 +        gtk_container_remove (GTK_CONTAINER (mmyth_ui->main_hbox),
   1.150 +                              main_vseparator);        
   1.151 +
   1.152 +    }
   1.153 +
   1.154 +    if (new_uicommon->main_widget == mmyth_ui->video_alignment) {
   1.155 +        //gst_player_video_show (GST_PLAYER_VIDEO(mmyth_ui->videow));
   1.156 +        gtk_widget_show (mmyth_ui->video_alignment);
   1.157 +        gtk_widget_show (mmyth_ui->videow);
   1.158 +    }
   1.159 +    else {
   1.160 +        /* FIXME: Fst call is NULL when mmyth_player_init fails */
   1.161 +        if(mmyth_ui->video_alignment != NULL)
   1.162 +            gtk_widget_hide (mmyth_ui->video_alignment);
   1.163 +        /* FIXME: Fst call is NULL when mmyth_player_init fails */
   1.164 +        if(mmyth_ui->videow != NULL)
   1.165 +            gtk_widget_hide (mmyth_ui->videow);
   1.166 +
   1.167 +        gtk_box_pack_start (GTK_BOX (mmyth_ui->main_hbox),
   1.168 +                            new_uicommon->main_widget, TRUE, TRUE, 0);
   1.169 +    }
   1.170 +
   1.171 +    if(main_vseparator == NULL) {
   1.172 +        /* FIXME: should free this variable*/
   1.173 +        main_vseparator = gtk_vseparator_new();
   1.174 +        g_object_ref (main_vseparator);                   
   1.175 +    }  
   1.176 +    gtk_box_pack_start (GTK_BOX (mmyth_ui->main_hbox),
   1.177 +                        main_vseparator, FALSE, FALSE, 0);    
   1.178 +    gtk_widget_show (main_vseparator);
   1.179 +
   1.180 +    gtk_box_pack_end (GTK_BOX (mmyth_ui->main_hbox), new_uicommon->event_box, FALSE,
   1.181 +                      FALSE, 0);         
   1.182 +
   1.183 +    mmyth_ui->current_uicommon = new_uicommon;
   1.184 +
   1.185 +}
   1.186 +
   1.187 +/* The close callback is the same for all windows*/
   1.188 +static void
   1.189 +cb_not_impl_button (GtkButton * button, gpointer user_data)
   1.190 +{
   1.191 +    MMythUi *mmyth_ui = (MMythUi *) user_data;
   1.192 +
   1.193 +    GtkWidget *msg_dialog = gtk_message_dialog_new (
   1.194 +                                           GTK_WINDOW(mmyth_ui->main_window),
   1.195 +                                           GTK_DIALOG_MODAL |
   1.196 +                                           GTK_DIALOG_DESTROY_WITH_PARENT,
   1.197 +                                           GTK_MESSAGE_INFO,
   1.198 +                                           GTK_BUTTONS_OK,
   1.199 +                                           "Feature not implemented");
   1.200 +    gtk_widget_set_size_request (msg_dialog, 350, 120);
   1.201 +
   1.202 +    gtk_dialog_run (GTK_DIALOG (msg_dialog));
   1.203 +
   1.204 +    gtk_widget_destroy(GTK_WIDGET(msg_dialog)); 
   1.205 +}
   1.206 +
   1.207 +/******************************************************************************
   1.208 + *                        POPUP MENU WIDGET METHODS                           *
   1.209 + *****************************************************************************/
   1.210 +
   1.211 +static void 
   1.212 +cb_menu_item_settings (GtkMenuItem *menuitem,
   1.213 +                            gpointer user_data)
   1.214 +{
   1.215 +    
   1.216 +    MMythUi *mmyth_ui = (MMythUi*) user_data;
   1.217 +   
   1.218 +    if (mmyth_uisettings_run (GTK_WINDOW (mmyth_ui->main_window))) {
   1.219 +    	// If user changes the settings, we restart the context
   1.220 +    	g_debug ("[%s] Restarting mmyth_context to new settings", __FUNCTION__);
   1.221 +    	gmyth_context_initialize();
   1.222 +    }
   1.223 +}
   1.224 +
   1.225 +#ifndef MAEMO_PLATFORM
   1.226 +
   1.227 +static void 
   1.228 +detacher (GtkWidget *attach_widget,GtkMenu *menu) 
   1.229 +{
   1.230 +	
   1.231 +}
   1.232 +
   1.233 +static void
   1.234 +do_popup_menu (GtkWidget *my_widget, GdkEventButton *event)
   1.235 +{
   1.236 +    GtkWidget *popup;
   1.237 +
   1.238 +  int button, event_time;
   1.239 +
   1.240 +  GtkWidget *item_general;
   1.241 +  GtkWidget *image;
   1.242 +
   1.243 +  popup = gtk_menu_new ();
   1.244 +
   1.245 +  item_general = gtk_image_menu_item_new_with_mnemonic ("Setup");
   1.246 +  gtk_widget_show (item_general);
   1.247 +  gtk_container_add (GTK_CONTAINER (popup), item_general);
   1.248 +
   1.249 +  image = gtk_image_new_from_stock ("gtk-edit", GTK_ICON_SIZE_MENU);
   1.250 +  gtk_widget_show (image);
   1.251 +  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item_general), image);
   1.252 +
   1.253 +  g_signal_connect (G_OBJECT(item_general), "activate", G_CALLBACK (cb_menu_item_settings), popup_mmyth_ui);
   1.254 +
   1.255 +    if (event) {
   1.256 +      button = event->button;
   1.257 +      event_time = event->time;
   1.258 +    } else {
   1.259 +      button = 0;
   1.260 +      event_time = gtk_get_current_event_time ();
   1.261 +    }
   1.262 +    
   1.263 +    gtk_menu_attach_to_widget (GTK_MENU (popup), my_widget, detacher);
   1.264 +    gtk_menu_popup (GTK_MENU (popup), NULL, NULL, NULL, NULL, 
   1.265 +                  button, event_time);
   1.266 +    gtk_widget_show_all(popup);
   1.267 +}
   1.268 +
   1.269 +/* Respond to a button-press by posting a menu passed in as widget.
   1.270 + *
   1.271 + * Note that the "widget" argument is the menu being posted, NOT
   1.272 + * the button that was pressed.
   1.273 + */
   1.274 +static gint 
   1.275 +button_press_handler (GtkWidget *widget, GdkEvent *event)
   1.276 +{
   1.277 +
   1.278 +    if (event->type == GDK_BUTTON_PRESS) {
   1.279 +      GdkEventButton *bevent = (GdkEventButton *) event; 
   1.280 +          /* Ignore double-clicks and triple-clicks */
   1.281 +      if (bevent->button == 3)
   1.282 +        {
   1.283 +          do_popup_menu (widget, bevent);
   1.284 +          return TRUE;
   1.285 +        }
   1.286 +    }
   1.287 +
   1.288 +    /* Tell calling code that we have not handled this event; pass it on. */
   1.289 +    return FALSE;
   1.290 +}
   1.291 +#endif
   1.292 +
   1.293 +/******************************************************************************
   1.294 + *                          MAIN APP VIEW METHODS                             *
   1.295 + *****************************************************************************/
   1.296 +
   1.297 +static void
   1.298 +cb_close_button (GtkButton * button, gpointer user_data)
   1.299 +{
   1.300 +    MMythUi *mmyth_ui = (MMythUi *) user_data;
   1.301 +
   1.302 +    mmyth_ui_set_widget (mmyth_ui, mmyth_ui->main_uicommon);
   1.303 +}
   1.304 +
   1.305 +static void
   1.306 +cb_watch_tv_button (GtkButton * button, gpointer user_data)
   1.307 +{
   1.308 +    MMythUi *mmyth_ui = (MMythUi *) user_data;
   1.309 +	gboolean res = FALSE;
   1.310 +	
   1.311 +	if (!(mmyth_ui->video_uicommon))
   1.312 +		mmyth_ui->video_uicommon = create_video_view (mmyth_ui);
   1.313 +	
   1.314 +	// Creates the tv player that will retrieve the mythtv content, decode and show it
   1.315 +	mmyth_ui->tvplayer = mmyth_tvplayer_new ();
   1.316 +	/* choose here if this is a LiveTV session */
   1.317 +	mmyth_ui->tvplayer->is_livetv = TRUE;
   1.318 +
   1.319 +	res = mmyth_tvplayer_initialize (mmyth_ui->tvplayer);
   1.320 +
   1.321 +	if (!res) {
   1.322 +		g_warning ("[%s] Could not initialize tvplayer", __FUNCTION__);
   1.323 +
   1.324 +		g_object_unref (mmyth_ui->tvplayer);
   1.325 +		mmyth_ui->tvplayer = NULL;
   1.326 +
   1.327 +		GtkWidget *dialog = gtk_message_dialog_new (
   1.328 +				GTK_WINDOW(mmyth_ui->main_window),
   1.329 +				GTK_DIALOG_DESTROY_WITH_PARENT,
   1.330 +                GTK_MESSAGE_ERROR,
   1.331 +                GTK_BUTTONS_CLOSE,
   1.332 +			 	"MMyth found errors while starting TV Player, please check "
   1.333 +			 	"the GStreamer installation");
   1.334 +
   1.335 +		gtk_dialog_run (GTK_DIALOG (dialog));
   1.336 +		gtk_widget_destroy (dialog);
   1.337 +		
   1.338 +		return;
   1.339 +	}
   1.340 +	//res = mmyth_tvplayer_livetv_setup (mmyth_ui->tvplayer);
   1.341 +	//
   1.342 +	if (mmyth_ui && mmyth_ui->tvplayer && res) {
   1.343 +		mmyth_tvplayer_set_widget (mmyth_ui->tvplayer, mmyth_ui->videow);
   1.344 +		mmyth_tvplayer_start_playing (mmyth_ui->tvplayer);
   1.345 +	} else {
   1.346 +		// TODO: Show Alert with error description!
   1.347 +		g_warning ("[%s] MMythUI can't initialize tv_player", __FUNCTION__);
   1.348 +		g_object_unref (mmyth_ui->tvplayer);
   1.349 +		mmyth_ui->tvplayer = NULL;
   1.350 +		// FIXME: Show the exact error that happened
   1.351 +		GtkWidget *dialog = gtk_message_dialog_new (
   1.352 +				GTK_WINDOW(mmyth_ui->main_window),
   1.353 +				GTK_DIALOG_DESTROY_WITH_PARENT,
   1.354 +                GTK_MESSAGE_ERROR,
   1.355 +                GTK_BUTTONS_CLOSE,
   1.356 +                "Error while starting TV Player, please check if the backend"
   1.357 +                " is running properly and a tv card is available!");
   1.358 +		gtk_dialog_run (GTK_DIALOG (dialog));
   1.359 +		gtk_widget_destroy (dialog);		
   1.360 +		return;
   1.361 +	}
   1.362 +	mmyth_ui_set_widget (mmyth_ui, mmyth_ui->video_uicommon);
   1.363 +	
   1.364 +}
   1.365 +
   1.366 +static void 
   1.367 +cb_epg_button (GtkButton * button, gpointer user_data)
   1.368 +{
   1.369 +    MMythUi *mmyth_ui = (MMythUi *) user_data;
   1.370 +    
   1.371 +    if (!(mmyth_ui->epg_grid_uicommon))
   1.372 +    	mmyth_ui->epg_grid_uicommon = create_epg_grid_view(mmyth_ui);
   1.373 +    	
   1.374 +    mmyth_ui_set_widget (mmyth_ui, mmyth_ui->epg_grid_uicommon);
   1.375 +}
   1.376 +
   1.377 +static MMythUiCommon *
   1.378 +create_main_view (MMythUi * mmyth_ui)
   1.379 +{
   1.380 +    MMythUiCommon *ui_common;
   1.381 +    GtkWidget *main_widget;
   1.382 +    GtkWidget *image;
   1.383 +
   1.384 +	g_debug ("Creating Main UI Common");
   1.385 +	
   1.386 +    // FIXME: file path
   1.387 +#ifdef MMYTH_DEV
   1.388 +    image = gtk_image_new_from_file ("../pixmaps/mmyth_logo.png");
   1.389 +#else
   1.390 +    image = gtk_image_new_from_file ( PIX_DIR "mmyth_logo.png");
   1.391 +#endif
   1.392 +    
   1.393 +    main_widget = gtk_event_box_new();
   1.394 +    
   1.395 +    gtk_container_add (GTK_CONTAINER (main_widget),
   1.396 +                       image);
   1.397 +
   1.398 +
   1.399 +    gtk_widget_show_all (main_widget);
   1.400 +    g_object_ref (main_widget);
   1.401 +
   1.402 +    ui_common = mmyth_uicommon_new (main_widget,
   1.403 +                                    "Watch TV", "EPG", "Recording");
   1.404 +
   1.405 +    /* Button signals */
   1.406 +    // FIXME
   1.407 +    g_signal_connect (G_OBJECT (ui_common->button1), "clicked",
   1.408 +                      G_CALLBACK (cb_watch_tv_button), mmyth_ui);
   1.409 +    g_signal_connect (G_OBJECT (ui_common->button2), "clicked",
   1.410 +                      G_CALLBACK (cb_epg_button), mmyth_ui);
   1.411 +    g_signal_connect (G_OBJECT (ui_common->button3), "clicked",
   1.412 +                      G_CALLBACK (cb_record_button), mmyth_ui);
   1.413 +
   1.414 +    return ui_common;
   1.415 +
   1.416 +}
   1.417 +
   1.418 +/******************************************************************************
   1.419 + *                         epg GRID VIEW METHODS                              *
   1.420 + *****************************************************************************/
   1.421 +
   1.422 +static MMythUiCommon *
   1.423 +create_epg_grid_view (MMythUi * mmyth_ui)
   1.424 +{
   1.425 +    MMythUiCommon *ui_common;        
   1.426 +
   1.427 +	g_debug ("Creating EPG Grid UI Common");
   1.428 +	
   1.429 +    GtkWidget *epg_grid_view = GTK_WIDGET (epg_grid_view_new (mmyth_ui));
   1.430 +    
   1.431 +    ui_common = mmyth_uicommon_new (epg_grid_view,
   1.432 +                                    "Play", "Record",
   1.433 +                                    "Close");    
   1.434 +    
   1.435 +    /* Button signals */
   1.436 +    g_signal_connect (G_OBJECT (ui_common->button1), "clicked",
   1.437 +                      G_CALLBACK (cb_not_impl_button), mmyth_ui);
   1.438 +    g_signal_connect (G_OBJECT (ui_common->button2), "clicked",
   1.439 +                      G_CALLBACK (cb_record_button), mmyth_ui);
   1.440 +    g_signal_connect (G_OBJECT (ui_common->button3), "clicked",
   1.441 +                      G_CALLBACK (cb_close_button), mmyth_ui);
   1.442 +    
   1.443 +    return ui_common;
   1.444 +}
   1.445 +/******************************************************************************
   1.446 + *                         SCHEDULE VIEW METHODS                                *
   1.447 + ******************************************************************************/
   1.448 +
   1.449 +static void
   1.450 +cb_save_new_schedule (GtkButton *button, gpointer user_data)
   1.451 +{
   1.452 +    MMythUi *mmyth_ui = (MMythUi *) user_data;
   1.453 +
   1.454 +	if (!(mmyth_ui->schedule_uicommon))
   1.455 +		mmyth_ui->schedule_uicommon = create_schedule_view(mmyth_ui);
   1.456 +		
   1.457 +    mmyth_schedulerui_save (mmyth_ui->mmyth_schedulerui);
   1.458 +    
   1.459 +    mmyth_recordui_reload_schedule (mmyth_ui->mmyth_recordui);
   1.460 +    
   1.461 +    mmyth_ui_set_widget (mmyth_ui, mmyth_ui->record_uicommon);
   1.462 +    
   1.463 +}
   1.464 +
   1.465 +static void
   1.466 +cb_edit_scheduled (GtkTreeView * tree_view, GtkTreePath *path, 
   1.467 +		           GtkTreeViewColumn *column, gpointer user_data)
   1.468 +{
   1.469 +    MMythUi *mmyth_ui = (MMythUi *) user_data;
   1.470 +    GtkTreeSelection *selection;
   1.471 +	GtkTreeModel *list_store;
   1.472 +	GtkTreeIter iter;
   1.473 +	int index;
   1.474 +    //gint new_row = 0, record_id = 0;
   1.475 +	ScheduleInfo *schedule_info;
   1.476 +	GList *schedule_list;
   1.477 +	//GtkTreeIter iter;
   1.478 +	gint res;
   1.479 +
   1.480 +	//gtk_tree_store_clear(recordui->sch_tree_store);	
   1.481 +
   1.482 +	res = gmyth_scheduler_get_schedule_list(mmyth_ui->mmyth_recordui->scheduler, &(schedule_list));
   1.483 +	if (res < 0) {
   1.484 +		g_warning ("[%s] Retrieved NULL list of scheduled data from database", __FUNCTION__);
   1.485 +		//return FALSE;
   1.486 +	}
   1.487 +	printf("\nX %d", res); fflush(stdout);
   1.488 +    
   1.489 +   	selection = 
   1.490 +        gtk_tree_view_get_selection(GTK_TREE_VIEW(mmyth_ui->mmyth_recordui->sch_treeview));
   1.491 +    
   1.492 +    gtk_tree_selection_get_selected(selection, &list_store, &iter);
   1.493 +	gtk_tree_model_get(list_store, &iter, 4, &index, -1);
   1.494 +    
   1.495 +    mmyth_ui_set_widget (mmyth_ui, mmyth_ui->schedule_uicommon);
   1.496 +    
   1.497 +	if (!(mmyth_ui->schedule_uicommon))
   1.498 +		mmyth_ui->schedule_uicommon = create_schedule_view(mmyth_ui);
   1.499 +		
   1.500 +	schedule_list = g_list_nth(schedule_list, atoi(gtk_tree_path_to_string(path)));
   1.501 +	schedule_info = (ScheduleInfo*) schedule_list->data;
   1.502 +
   1.503 +    printf("\nstarttime: %ld", schedule_info->start_time); fflush(stdout);		
   1.504 +}
   1.505 +
   1.506 +static MMythUiCommon *
   1.507 +create_schedule_view (MMythUi * mmyth_ui)
   1.508 +{
   1.509 +    MMythUiCommon *ui_common;        
   1.510 +    GtkWidget *schedule_widget;
   1.511 +    
   1.512 +    g_debug ("Creating Schedule UI Common");
   1.513 +
   1.514 +	mmyth_ui->mmyth_schedulerui = mmyth_schedulerui_new ();
   1.515 +	if (mmyth_ui->mmyth_schedulerui == NULL) {
   1.516 +		g_warning ("[%s] Error while creating scheduler ui", __FUNCTION__);
   1.517 +		return NULL;
   1.518 +	}
   1.519 +	
   1.520 +	schedule_widget = mmyth_ui->mmyth_schedulerui->main_widget;
   1.521 +
   1.522 +    gtk_widget_show_all (schedule_widget);
   1.523 +    g_object_ref (schedule_widget);
   1.524 +    
   1.525 +    ui_common = mmyth_uicommon_new (schedule_widget,
   1.526 +                                    "Save", "Clear",
   1.527 +                                    "Cancel");    
   1.528 +
   1.529 +    /* Button signals */
   1.530 +    g_signal_connect (G_OBJECT (ui_common->button1), "clicked",
   1.531 +                      G_CALLBACK (cb_save_new_schedule), mmyth_ui);
   1.532 +    g_signal_connect (G_OBJECT (ui_common->button2), "clicked",
   1.533 +                      G_CALLBACK (cb_not_impl_button), mmyth_ui);
   1.534 +    g_signal_connect (G_OBJECT (ui_common->button3), "clicked",
   1.535 +                      G_CALLBACK (cb_record_button), mmyth_ui);
   1.536 +    
   1.537 +    return ui_common;
   1.538 +}
   1.539 +/******************************************************************************
   1.540 + *                         RECORD VIEW METHODS                                *
   1.541 + ******************************************************************************/
   1.542 +static void
   1.543 +cb_record_button (GtkButton * button, gpointer user_data)
   1.544 +{
   1.545 +    MMythUi *mmyth_ui = (MMythUi *) user_data;
   1.546 +
   1.547 +	if (!(mmyth_ui->record_uicommon)) {
   1.548 +		mmyth_ui->record_uicommon = create_record_view (mmyth_ui);
   1.549 +		mmyth_ui->schedule_uicommon = create_schedule_view (mmyth_ui);
   1.550 +	}
   1.551 +	
   1.552 +	mmyth_recordui_reload_all (mmyth_ui->mmyth_recordui);
   1.553 +	
   1.554 +    mmyth_ui_set_widget (mmyth_ui, mmyth_ui->record_uicommon);
   1.555 +
   1.556 +} 
   1.557 +
   1.558 +static void
   1.559 +cb_record_close_button (GtkButton * button, gpointer user_data)
   1.560 +{
   1.561 +    MMythUi *mmyth_ui = (MMythUi *) user_data;
   1.562 +
   1.563 +	mmyth_ui_set_widget(mmyth_ui, mmyth_ui->main_uicommon);
   1.564 +
   1.565 +	mmyth_recordui_free(mmyth_ui->mmyth_recordui);
   1.566 +	
   1.567 +	if (mmyth_ui->record_uicommon) {
   1.568 +		gtk_widget_destroy (mmyth_ui->record_uicommon->main_widget);
   1.569 +		mmyth_uicommon_free(mmyth_ui->record_uicommon);
   1.570 +		mmyth_ui->record_uicommon = NULL;
   1.571 +	}
   1.572 +
   1.573 +	if (mmyth_ui->schedule_uicommon) {
   1.574 +	//	mmyth_uicommon_free(mmyth_ui->schedule_uicommon);
   1.575 +	}	
   1.576 +} 
   1.577 +
   1.578 +
   1.579 +
   1.580 +
   1.581 +static void
   1.582 +play_selected_recorded (gpointer user_data)
   1.583 +{
   1.584 +    MMythUi *mmyth_ui = (MMythUi *) user_data;
   1.585 +    gboolean res = FALSE;
   1.586 +
   1.587 +    gchar *path = mmyth_recordui_get_selected_recorded (mmyth_ui->mmyth_recordui);
   1.588 +    
   1.589 +    if (path == NULL) {
   1.590 +    	// This should never happens. Play button is just activated when
   1.591 +    	// a recording is selected.
   1.592 +    	g_warning ("[%s] Play button pressed while none recorded is selected", __FUNCTION__);
   1.593 +    	return;
   1.594 +    }
   1.595 +
   1.596 +	if (!(mmyth_ui->video_uicommon))
   1.597 +		mmyth_ui->video_uicommon = create_video_view (mmyth_ui);
   1.598 +	
   1.599 +	// Creates the tv player that will retrieve the mythtv content, decode and show it
   1.600 +	mmyth_ui->tvplayer = mmyth_tvplayer_new ();
   1.601 +	g_debug ("[%s] New TV Player created: %d\n", __FUNCTION__, (int) (mmyth_ui->tvplayer));
   1.602 +	res = mmyth_tvplayer_initialize (mmyth_ui->tvplayer);
   1.603 +	if (!res) {
   1.604 +		g_warning ("[%s] Could not initialize tvplayer", __FUNCTION__);
   1.605 +
   1.606 +		g_object_unref (mmyth_ui->tvplayer);
   1.607 +		mmyth_ui->tvplayer = NULL;
   1.608 +
   1.609 +		GtkWidget *dialog = gtk_message_dialog_new (
   1.610 +				GTK_WINDOW(mmyth_ui->main_window),
   1.611 +				GTK_DIALOG_DESTROY_WITH_PARENT,
   1.612 +                GTK_MESSAGE_ERROR,
   1.613 +                GTK_BUTTONS_CLOSE,
   1.614 +			 	"MMyth found errors while starting TV Player, please check "
   1.615 +			 	"the GStreamer installation");
   1.616 +
   1.617 +		gtk_dialog_run (GTK_DIALOG (dialog));
   1.618 +		gtk_widget_destroy (dialog);
   1.619 +		
   1.620 +		return;
   1.621 +	}		
   1.622 +
   1.623 +	res = mmyth_tvplayer_record_setup (mmyth_ui->tvplayer, path);
   1.624 +	
   1.625 +	if (mmyth_ui && mmyth_ui->tvplayer && res) {
   1.626 +		mmyth_tvplayer_set_widget (mmyth_ui->tvplayer, mmyth_ui->videow);
   1.627 +		mmyth_tvplayer_start_playing (mmyth_ui->tvplayer);
   1.628 +	} else {
   1.629 +		// TODO: Show Alert with error description!
   1.630 +		g_warning ("[%s] MMythUI can't initialize tv_player", __FUNCTION__);
   1.631 +		g_object_unref (mmyth_ui->tvplayer);
   1.632 +		mmyth_ui->tvplayer = NULL;
   1.633 +		// FIXME: Show the exact error that happened
   1.634 +		GtkWidget *dialog = gtk_message_dialog_new (
   1.635 +				GTK_WINDOW(mmyth_ui->main_window),
   1.636 +				GTK_DIALOG_DESTROY_WITH_PARENT,
   1.637 +                GTK_MESSAGE_ERROR,
   1.638 +                GTK_BUTTONS_CLOSE,
   1.639 +                "Error while starting TV Player, please check if the backend"
   1.640 +                " is running properly and a tv card is available!");
   1.641 +		gtk_dialog_run (GTK_DIALOG (dialog));
   1.642 +		gtk_widget_destroy (dialog);		
   1.643 +		return;
   1.644 +	}
   1.645 +	
   1.646 +	mmyth_ui_set_widget (mmyth_ui, mmyth_ui->video_uicommon);    
   1.647 +}
   1.648 +
   1.649 +static void
   1.650 +cb_play_clicked_recorded (GtkTreeView * tree_view, GtkTreePath *path, 
   1.651 +		GtkTreeViewColumn *column, gpointer user_data)
   1.652 +{
   1.653 +	play_selected_recorded (user_data);
   1.654 +}
   1.655 +
   1.656 +static void
   1.657 +cb_play_selected (GtkButton * button, gpointer user_data)
   1.658 +{
   1.659 +	play_selected_recorded (user_data);
   1.660 +}
   1.661 +
   1.662 +static void
   1.663 +cb_schedule_button (GtkButton * button, gpointer user_data)
   1.664 +{
   1.665 +    MMythUi *mmyth_ui = (MMythUi *) user_data;
   1.666 +
   1.667 +    mmyth_ui_set_widget (mmyth_ui, mmyth_ui->schedule_uicommon);
   1.668 +}
   1.669 +
   1.670 +void
   1.671 +cb_switch_page (GtkNotebook *notebook, GtkNotebookPage *page,
   1.672 +		guint page_num, gpointer user_data)
   1.673 +{
   1.674 +    MMythUi *mmyth_ui = (MMythUi *) user_data;
   1.675 +    MMythUiCommon *ui_common; 
   1.676 +    
   1.677 +	assert (mmyth_ui);
   1.678 +	assert (mmyth_ui->record_uicommon);
   1.679 +	
   1.680 +    ui_common = mmyth_ui->record_uicommon;
   1.681 +    
   1.682 +    if (page_num == 0) { // Switched to Schedule list
   1.683 +    	gtk_button_set_label (GTK_BUTTON (ui_common->button1), "New");
   1.684 +		g_signal_handlers_disconnect_by_func (
   1.685 +			G_OBJECT (ui_common->button1), G_CALLBACK (cb_play_selected), mmyth_ui);
   1.686 +	    g_signal_connect (G_OBJECT (ui_common->button1), "clicked",
   1.687 +            G_CALLBACK (cb_schedule_button), mmyth_ui);
   1.688 +    } else if (page_num == 1) {
   1.689 +    	gtk_button_set_label (GTK_BUTTON (ui_common->button1), "Play");
   1.690 +		g_signal_handlers_disconnect_by_func (
   1.691 +			G_OBJECT (ui_common->button1), G_CALLBACK (cb_schedule_button), mmyth_ui);
   1.692 +	    g_signal_connect (G_OBJECT (ui_common->button1), "clicked",
   1.693 +            G_CALLBACK (cb_play_selected), mmyth_ui);
   1.694 +    }
   1.695 +}
   1.696 +                                            
   1.697 +                                            
   1.698 +static MMythUiCommon *
   1.699 +create_record_view (MMythUi * mmyth_ui)
   1.700 +{
   1.701 +    MMythUiCommon *ui_common;        
   1.702 +    
   1.703 +   	g_debug ("Creating Record UI Common");
   1.704 +   	
   1.705 +	mmyth_ui->mmyth_recordui = mmyth_recordui_new ();
   1.706 +
   1.707 +	// FIXME: Change MMythRecordUI to a GtkWidget child and avoid this call!
   1.708 +    gtk_widget_show_all (mmyth_ui->mmyth_recordui->scrolled_window);
   1.709 +    
   1.710 +    ui_common = mmyth_uicommon_new (mmyth_ui->mmyth_recordui->scrolled_window, "New", "Delete", "<<Back");    
   1.711 +    g_object_ref (mmyth_ui->mmyth_recordui->scrolled_window);
   1.712 +        
   1.713 +    /* Button signals */
   1.714 +    g_signal_connect (G_OBJECT (ui_common->button1), "clicked",
   1.715 +                      G_CALLBACK (cb_schedule_button), mmyth_ui);
   1.716 +    g_signal_connect (G_OBJECT (ui_common->button2), "clicked",
   1.717 +                      G_CALLBACK (mmyth_recordui_delete_selected), mmyth_ui->mmyth_recordui);
   1.718 +    g_signal_connect (G_OBJECT (ui_common->button3), "clicked",
   1.719 +                      G_CALLBACK (cb_record_close_button), mmyth_ui);
   1.720 +    g_signal_connect (G_OBJECT (mmyth_ui->mmyth_recordui->notebook),
   1.721 +		      "switch-page", G_CALLBACK (cb_switch_page), mmyth_ui);
   1.722 +    g_signal_connect (G_OBJECT (mmyth_ui->mmyth_recordui->rec_treeview),
   1.723 +		      "row-activated", G_CALLBACK (cb_play_clicked_recorded), mmyth_ui);
   1.724 +    g_signal_connect (G_OBJECT (mmyth_ui->mmyth_recordui->sch_treeview),
   1.725 +		      "row-activated", G_CALLBACK (cb_edit_scheduled), mmyth_ui);
   1.726 +    return ui_common;
   1.727 +}
   1.728 +
   1.729 +
   1.730 +/******************************************************************************
   1.731 + *                         GST VIDEO WIDGET METHODS                           *
   1.732 + *****************************************************************************/
   1.733 +
   1.734 +static void
   1.735 +cb_video_close_button (GtkButton * button, gpointer user_data)
   1.736 +{
   1.737 +    MMythUi *mmyth_ui = (MMythUi *) user_data;
   1.738 +	
   1.739 +	g_debug ("MMythUI video close button pressed");
   1.740 +	
   1.741 +	if (mmyth_ui && mmyth_ui->tvplayer) {
   1.742 +	    mmyth_tvplayer_stop_playing (mmyth_ui->tvplayer);
   1.743 +
   1.744 +		g_object_unref (mmyth_ui->tvplayer);
   1.745 +		mmyth_ui->tvplayer = NULL;	    
   1.746 +	} else {
   1.747 +		g_warning ("cb_video_close_button called with NULL pointer\n");
   1.748 +	}
   1.749 +	
   1.750 +    mmyth_ui_set_widget (mmyth_ui, mmyth_ui->main_uicommon);
   1.751 +}
   1.752 +
   1.753 +
   1.754 +static MMythUiCommon *
   1.755 +create_video_view (MMythUi * mmyth_ui)
   1.756 +{
   1.757 +
   1.758 +    MMythUiCommon *ui_common;
   1.759 +
   1.760 +	g_debug ("Creating Video UI Common");
   1.761 +	
   1.762 +	/* Creates widget to be user by MMythTVPlayer to draw the video */
   1.763 +    mmyth_ui->videow = gtk_drawing_area_new ();
   1.764 +    // FIXME: Get the widget size from settings
   1.765 +    gtk_widget_set_size_request (mmyth_ui->videow, 300, 240);
   1.766 +
   1.767 +    //mmiptv_ui->logo = gdk_pixbuf_new_from_file ("logo.png", NULL);
   1.768 +
   1.769 +	// Creates an alignment to place the video widget inside
   1.770 +    mmyth_ui->video_alignment = gtk_alignment_new (0.5, 0.5, 1, 1);
   1.771 +    gtk_widget_hide (mmyth_ui->video_alignment);
   1.772 +
   1.773 +    gtk_container_add (GTK_CONTAINER (mmyth_ui->video_alignment),
   1.774 +                       mmyth_ui->videow);
   1.775 +
   1.776 +    /* Add the gst video widget to hbox. It should never be removed. */    
   1.777 +    /* FIXME: mmyth_ui->video_alignment == NULL when mmyth_player_init fails */
   1.778 +    if((mmyth_ui->main_hbox != NULL) && (mmyth_ui->video_alignment != NULL)) {
   1.779 +        gtk_box_pack_start (GTK_BOX (mmyth_ui->main_hbox),
   1.780 +                            mmyth_ui->video_alignment, TRUE, TRUE, 0);    
   1.781 +	} else {
   1.782 +		g_warning ("[%s] Error while adding video_alignment to main widget", __FUNCTION__);	
   1.783 +    }
   1.784 +
   1.785 +    g_object_ref (mmyth_ui->videow);
   1.786 +    g_object_ref (mmyth_ui->video_alignment);
   1.787 +
   1.788 +    ui_common = mmyth_uicommon_new (mmyth_ui->video_alignment,
   1.789 +                                    "  Full\nScreen", "Other\nServices",
   1.790 +                                    "Close");
   1.791 +                                    
   1.792 +    
   1.793 +    g_signal_connect (G_OBJECT (ui_common->button1), "clicked",
   1.794 +                      G_CALLBACK (cb_not_impl_button), mmyth_ui);
   1.795 +    g_signal_connect (G_OBJECT (ui_common->button2), "clicked",
   1.796 +                      G_CALLBACK (cb_not_impl_button), mmyth_ui);    
   1.797 +    g_signal_connect (G_OBJECT (ui_common->button3), "clicked",
   1.798 +                      G_CALLBACK (cb_video_close_button), mmyth_ui);
   1.799 +
   1.800 +	if (ui_common)
   1.801 +		g_debug ("Video UI_Common sucessfull created");
   1.802 +		
   1.803 +    return ui_common;
   1.804 +}
   1.805 +
   1.806 +
   1.807 +
   1.808 +GtkWidget*
   1.809 +mmyth_ui_get_video_widget (MMythUi *mmyth_ui) {
   1.810 +	
   1.811 +	if (mmyth_ui && mmyth_ui->videow) {
   1.812 +	
   1.813 +		return mmyth_ui->videow;
   1.814 +	}
   1.815 +	
   1.816 +	return NULL;	
   1.817 +}