renatofilho@20: #include <string.h>
renatofilho@20: #include <stdlib.h>
renatofilho@20: #include <gtk/gtk.h>
renatofilho@20: #include <gdk/gdkkeysyms.h>
renatofilho@20: #include <time.h>
renatofilho@20: 
renatofilho@20: #include "mmyth_epg_grid_view.h"
renatofilho@20: #include "mmyth_epg_grid_widget.h"
renatofilho@20: 
renatofilho@20: /* Service genre */
renatofilho@20: #define GENRE_MIN               0
renatofilho@20: #define GENRE_MAX               10
renatofilho@20: #define GENRE_UNDEFINED         0
renatofilho@20: #define GENRE_MOVIE             1
renatofilho@20: #define GENRE_NEWS              2
renatofilho@20: #define GENRE_SHOW              3
renatofilho@20: #define GENRE_SPORTS            4
renatofilho@20: #define GENRE_CHILDREN          5
renatofilho@20: #define GENRE_MUSIC             6
renatofilho@20: #define GENRE_CULTURE           7
renatofilho@20: #define GENRE_SOCIAL            8
renatofilho@20: #define GENRE_EDUCATION         9
renatofilho@20: #define GENRE_LEISURE           10
renatofilho@20: 
renatofilho@20: #define NRO_HOURS 3
renatofilho@20: 
renatofilho@20: /* Function prototypes*/
renatofilho@20: static void update_service_details(MMythEpgGridWidget *object, 
renatofilho@20:                                    gpointer arg1, gpointer user_data);
renatofilho@20: static gboolean key_press_epg_grid_view(GtkWidget * widget, 
renatofilho@20:                                         GdkEventKey * event, 
renatofilho@20:                                         gpointer user_data);
renatofilho@20: 
renatofilho@20: static GtkWidget *mmyth_epg_grid_widget = NULL;
renatofilho@20: 
renatofilho@20: /* is a GtkEventBox */
renatofilho@20: static GtkWidget *program_details_area = NULL;
renatofilho@20: static GtkWidget *details_main_hbox = NULL;
renatofilho@20: static GtkWidget *details_vbox = NULL;
renatofilho@20: static GtkWidget *details_logo_vbox = NULL;
renatofilho@20: 
renatofilho@20: /* update signal callback from MMythEpgGridWidget */
renatofilho@20: static void
renatofilho@20: update_service_details(MMythEpgGridWidget *object, gpointer arg1, gpointer user_data) 
renatofilho@20: {
renatofilho@20: 	g_return_if_fail(arg1 != NULL);
renatofilho@20: 
renatofilho@20: 	EpgGridItem *epg_grid_item = (EpgGridItem *) arg1;
renatofilho@20: 	
renatofilho@20: 	gchar sel_prog_desc[100] = "<big><b>";
renatofilho@20: 	gchar time_buffer[50];
renatofilho@20: 
renatofilho@20:     /* FIXME: get first content from content_list*/
renatofilho@20:     GMythProgramInfo *proginfo = (GMythProgramInfo *) epg_grid_item->proginfo;
renatofilho@20: 
renatofilho@20:     if(proginfo) {
renatofilho@20:         GString *prog_name = proginfo->title;    
renatofilho@20:         GString *service_name = proginfo->chanid;
renatofilho@20:            
renatofilho@20:     	if(details_vbox != NULL)
renatofilho@20:   	        gtk_container_remove (GTK_CONTAINER (details_main_hbox), details_vbox);	
renatofilho@20:     
renatofilho@20:         /* update service description */
renatofilho@20:         strcat(sel_prog_desc, service_name->str);
renatofilho@20:         strcat(sel_prog_desc, "</b></big>");
renatofilho@20:     
renatofilho@20:         GtkWidget *fst_line_lbl = gtk_label_new(NULL);
renatofilho@20:         gtk_misc_set_alignment (GTK_MISC(fst_line_lbl), 0.0, 0.0); 
renatofilho@20:         gtk_label_set_markup(GTK_LABEL(fst_line_lbl), sel_prog_desc);     
renatofilho@20: 
renatofilho@20:         /* freeing char[] */
renatofilho@20:         sel_prog_desc[0] = 0;
renatofilho@20:         strcat(sel_prog_desc, "\t");
renatofilho@20:         strcat(sel_prog_desc, prog_name->str);    
renatofilho@20:             	        		
renatofilho@20:         struct tm loctime_start, loctime_end;
renatofilho@20:     
renatofilho@20:         // Convert it to local time representation. 
renatofilho@20:         /* FIXME: conversion from time to localtime is different
renatofilho@20:         in different machines */
rosfran@244:         long int schedule_start_time = proginfo->startts->tv_sec;
rosfran@244:         long int schedule_end_time   = proginfo->endts->tv_sec;
renatofilho@20:     
renatofilho@20:         if (localtime_r(&schedule_start_time, &loctime_start) == NULL) {
renatofilho@20:             g_warning ("localtime_r error in mmyth_epg_grid_view!\n");
renatofilho@20:         }
renatofilho@20:     
renatofilho@20:         #if 0 
renatofilho@20:             fprintf (stderr, asctime (loctime_start)); 
renatofilho@20:         #endif
renatofilho@20:     
renatofilho@20:         strftime (time_buffer, 100, "  %H:%M - ", &loctime_start);
renatofilho@20:         strcat(sel_prog_desc, time_buffer );
renatofilho@20:     
renatofilho@20:         if (localtime_r(&schedule_end_time, &loctime_end) == NULL) {
renatofilho@20:             g_warning ("localtime_r error in mmyth_epg_grid_view!\n");
renatofilho@20:         }
renatofilho@20: 
renatofilho@20:         #if 0
renatofilho@20:             fprintf (stderr, asctime (loctime_end));     		    
renatofilho@20:         #endif
renatofilho@20:     
renatofilho@20:         strftime (time_buffer, 100, "%H:%M\n", &loctime_end);
renatofilho@20:         strcat(sel_prog_desc, time_buffer );
renatofilho@20:     
renatofilho@20:         GtkWidget *snd_line_lbl = gtk_label_new(NULL);
renatofilho@20:         gtk_misc_set_alignment (GTK_MISC(snd_line_lbl), 0.0, 0.0); 
renatofilho@20:         gtk_label_set_markup(GTK_LABEL(snd_line_lbl), sel_prog_desc);     
renatofilho@20:     
renatofilho@20:         // add the current selected program description to the label
renatofilho@20:         details_vbox = gtk_vbox_new(FALSE, 0);
renatofilho@20:         GtkWidget *fst_line_hbox = gtk_hbox_new(FALSE, 0);        
renatofilho@20:         
renatofilho@20:         gtk_box_pack_start (GTK_BOX (fst_line_hbox),
renatofilho@20:                             fst_line_lbl, FALSE, FALSE, 6);
renatofilho@20:         gtk_box_pack_start (GTK_BOX (details_vbox),
renatofilho@20:                             fst_line_hbox, FALSE, FALSE, 0);
renatofilho@20:         gtk_box_pack_start (GTK_BOX (details_vbox),
renatofilho@20:                             snd_line_lbl, FALSE, FALSE, 0);
renatofilho@20:         gtk_box_pack_start (GTK_BOX (details_main_hbox),
renatofilho@20:                             details_vbox, FALSE, FALSE, 0);
renatofilho@20:         
renatofilho@20:         gtk_widget_show_all(details_main_hbox);
renatofilho@20:     }        
renatofilho@20: }
renatofilho@20: 
renatofilho@20: /* Callback for hardware keys */
renatofilho@20: static gboolean
renatofilho@20: key_press_epg_grid_view(GtkWidget * widget, 
renatofilho@20:                         GdkEventKey * event, gpointer user_data)
renatofilho@20: {
renatofilho@20:     MMythEpgGridWidget *mmyth_epg_grid_widget = (MMythEpgGridWidget *) user_data;
renatofilho@20: 
renatofilho@20:     return mmyth_epg_grid_widget_key_press(mmyth_epg_grid_widget, widget, event); 
renatofilho@20: }
renatofilho@20: 
renatofilho@20: GtkWidget *
renatofilho@20: epg_grid_view_new (MMythUi* mmyth_ui) 
renatofilho@20: {
renatofilho@20:     GtkWidget *scrolled_window;
renatofilho@20:     scrolled_window = gtk_scrolled_window_new (NULL, NULL);
renatofilho@20:     gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
renatofilho@20:                                     GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);                   
renatofilho@20:     
renatofilho@20:     gtk_widget_modify_bg(scrolled_window, GTK_STATE_NORMAL, &main_bg_color);  
renatofilho@20:     
renatofilho@20:     GtkWidget *main_vbox = gtk_vbox_new (FALSE, 0);      
renatofilho@20:     //gtk_container_set_border_width(main_vbox, 4);        
renatofilho@20:     
renatofilho@20:     GtkWidget *details_event_box = gtk_event_box_new();               
renatofilho@20:     gtk_widget_modify_bg(details_event_box, GTK_STATE_NORMAL, &main_bg_color);  
renatofilho@20:     
renatofilho@20:     program_details_area = gtk_vbox_new (FALSE, 0);
renatofilho@20:     gtk_container_add (GTK_CONTAINER (details_event_box),
renatofilho@20:                        program_details_area);   
renatofilho@20:     gtk_container_set_border_width(GTK_CONTAINER (program_details_area), 4);        
renatofilho@20: 
renatofilho@20: 	details_main_hbox = gtk_hbox_new (FALSE, 10);    
renatofilho@20: 
renatofilho@20: 	gtk_box_pack_start (GTK_BOX (program_details_area),
renatofilho@20: 	                    details_main_hbox, FALSE, FALSE, 0);                    
renatofilho@20: 	                    
renatofilho@20: 	details_logo_vbox = gtk_vbox_new (FALSE, 0);      	 
renatofilho@20:     
renatofilho@20: 	GtkWidget *details_desc_vbox = gtk_vbox_new (FALSE, 0);      
renatofilho@20: 
renatofilho@20:     gtk_box_pack_start (GTK_BOX (details_main_hbox),
renatofilho@20: 	                    details_desc_vbox, FALSE, FALSE, 0);                    
renatofilho@20:     gtk_box_pack_start (GTK_BOX (details_main_hbox),
renatofilho@20: 	                    details_logo_vbox, FALSE, FALSE, 0);                    	
renatofilho@20: 	
renatofilho@20:     gtk_widget_set_size_request (program_details_area, -1, 120);          
renatofilho@20:     
renatofilho@20:     mmyth_epg_grid_widget = mmyth_epg_grid_widget_new();
renatofilho@20:     g_signal_connect(mmyth_epg_grid_widget, "selection_updated", 
renatofilho@20:                      G_CALLBACK (update_service_details), NULL);    
renatofilho@20: 
renatofilho@20:     /* select by default the first service */
renatofilho@20:     /* depends on mount services */
renatofilho@20:     if (MMYTH_EPG_GRID_WIDGET(mmyth_epg_grid_widget)->epg_view_model) {
renatofilho@20: 	    GList *fst_service = (GList *) 
renatofilho@20: 	    	MMYTH_EPG_GRID_WIDGET(mmyth_epg_grid_widget)->epg_view_model->data;
renatofilho@20:     	mmyth_epg_grid_widget_update_service(MMYTH_EPG_GRID_WIDGET(mmyth_epg_grid_widget),
renatofilho@20:                                          fst_service);
renatofilho@20:     }
renatofilho@20:     
renatofilho@20:     gtk_box_pack_start (GTK_BOX (main_vbox),
renatofilho@20:                         details_event_box, FALSE, FALSE, 0); 
renatofilho@20:     gtk_box_pack_start (GTK_BOX (main_vbox),
renatofilho@20:                         gtk_hseparator_new(), FALSE, FALSE, 0);         
renatofilho@20:     gtk_box_pack_start (GTK_BOX (main_vbox),
renatofilho@20:                         mmyth_epg_grid_widget, FALSE, FALSE, 0);                    
renatofilho@20: 
renatofilho@20:     gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window),
renatofilho@20:                                            main_vbox);    
renatofilho@20:     
renatofilho@20:     /* Add hardware button listener to application */
renatofilho@20:     g_signal_connect(mmyth_ui->main_window, "key_press_event", 
renatofilho@20:                      G_CALLBACK (key_press_epg_grid_view), mmyth_epg_grid_widget);    
renatofilho@20: 
renatofilho@20:     gtk_widget_show_all (scrolled_window);
renatofilho@20:     
renatofilho@20:     return scrolled_window;
renatofilho@20: }
renatofilho@20: 
renatofilho@20: /*
renatofilho@20: DVBHScheduleEvent * 
renatofilho@20: mmyth_epg_grid_view_get_selected_schedule()
renatofilho@20: {
renatofilho@20:     return mmyth_epg_grid_get_selected_schedule
renatofilho@20:             (MMYTH_EPG_GRID_WIDGET(mmyth_epg_grid_widget));
renatofilho@20: }
renatofilho@20: */