renatofilho@20: #include <gtk/gtk.h>
renatofilho@20: #include <glib.h>
renatofilho@20: #include <glib/gprintf.h>
renatofilho@20: #include <sys/types.h>
renatofilho@20: #include <sys/stat.h>
renatofilho@20: #include <unistd.h>
renatofilho@20: #include <string.h>
renatofilho@20: #include <stdio.h>
renatofilho@20: 
renatofilho@20: #include "mmyth_ui.h"
renatofilho@20: #include "mmyth_uicommon.h"
renatofilho@20: #include "mmyth_recordui.h"
renatofilho@20: #include "mmyth_schedulerui.h"
renatofilho@20: 
renatofilho@20: /* GMyth library includes */
rosfran@244: #include <gmyth/gmyth_scheduler.h>
rosfran@244: #include <gmyth/gmyth_common.h>
rosfran@244: #include <gmyth/gmyth_epg.h>
renatofilho@20: 
renatofilho@20: static void run_calendar_dialog (GtkButton *button, gpointer data);
renatofilho@20: 
renatofilho@20: static void add_channel_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);
renatofilho@20: static void add_time_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);
renatofilho@20: static void add_date_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);
renatofilho@20: static void add_duration_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);
renatofilho@20: static void add_frequency_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);
renatofilho@20: static void add_title_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);
renatofilho@20: 
renatofilho@20: MMythSchedulerUI*
rosfran@244: mmyth_schedulerui_new ( GMythBackendInfo *backend_info )
renatofilho@20: {
renatofilho@20: 	GtkWidget *scrolledwindow;	
renatofilho@20: 	GtkWidget *viewport;	
renatofilho@20: 	GtkWidget *head_hbox;
renatofilho@20: 	GtkWidget *fields_vbox;
renatofilho@20: 	GtkWidget *hseparator;
renatofilho@20: 	GtkWidget *label;
renatofilho@20: 
renatofilho@20: 	MMythSchedulerUI *scheduler_ui = g_new0 (MMythSchedulerUI, 1);
rosfran@244: 
rosfran@244: 	scheduler_ui->backend_info = backend_info;
renatofilho@20: 	
renatofilho@20: 	scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
renatofilho@20: 	scheduler_ui->main_widget = scrolledwindow;
renatofilho@20: 	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow),
renatofilho@20:   			GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
renatofilho@20: 
renatofilho@20: 	//Is this needed?  
renatofilho@20: 	viewport = gtk_viewport_new (NULL, NULL);
renatofilho@20: 	gtk_container_add (GTK_CONTAINER (scrolledwindow), viewport);
renatofilho@20: 
renatofilho@20: 	//Is this needed?
renatofilho@20: 	head_hbox = gtk_hbox_new (FALSE, 0);
renatofilho@20: 	gtk_container_add (GTK_CONTAINER (viewport), head_hbox);
renatofilho@20: 
renatofilho@20: 	fields_vbox = gtk_vbox_new (FALSE, 0);
renatofilho@20: 	gtk_box_pack_start (GTK_BOX (head_hbox), fields_vbox, TRUE, TRUE, 0);
renatofilho@20: 	gtk_container_set_border_width (GTK_CONTAINER (fields_vbox), 10);
renatofilho@20: 
renatofilho@20: 	label = gtk_label_new_with_mnemonic (("Manual Schedule Recording"));
renatofilho@20: 	gtk_box_pack_start (GTK_BOX (fields_vbox), label, FALSE, FALSE, 0);
renatofilho@20: 	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
renatofilho@20: 
renatofilho@20: 	hseparator = gtk_hseparator_new ();
renatofilho@20: 	gtk_box_pack_start (GTK_BOX (fields_vbox), hseparator, FALSE, TRUE, 0);
renatofilho@20: 
renatofilho@20: 	add_channel_field (scheduler_ui, fields_vbox);
renatofilho@20: 	add_time_field (scheduler_ui, fields_vbox);
renatofilho@20: 	add_date_field (scheduler_ui, fields_vbox);
renatofilho@20: 	add_duration_field (scheduler_ui, fields_vbox);
renatofilho@20: 	add_frequency_field (scheduler_ui, fields_vbox);
renatofilho@20: 	add_title_field (scheduler_ui, fields_vbox);
renatofilho@20: 	
renatofilho@20: 	return scheduler_ui;
renatofilho@20: }
renatofilho@20: 
renatofilho@20: static void
renatofilho@20: set_date_from_calendar (GtkCalendar *calendar, gpointer data)
renatofilho@20: {
renatofilho@20: 	char sched_date[24];
renatofilho@20: 
renatofilho@20: 	MMythSchedulerUI *scheduler_ui = (MMythSchedulerUI*) data;
renatofilho@20: 
renatofilho@20: 	// FIXME: Change this, save another value instead of month_temp, day_temp, ...	
renatofilho@20: 	gtk_calendar_get_date(GTK_CALENDAR(calendar), 
renatofilho@20: 		&(scheduler_ui->year_temp), &(scheduler_ui->month_temp), &(scheduler_ui->day_temp));
renatofilho@20: 
renatofilho@20: 	sched_date[23]='\0';
renatofilho@20: 	g_sprintf(sched_date, "%04d %02d %02d (yyyy mm dd)",
renatofilho@20: 		scheduler_ui->year_temp, scheduler_ui->month_temp+1, scheduler_ui->day_temp);
renatofilho@20: 
renatofilho@20: 	gtk_button_set_label(GTK_BUTTON(scheduler_ui->date_button), sched_date);
renatofilho@20: 
renatofilho@20: 	gtk_widget_destroy(scheduler_ui->calendar_dialog);
renatofilho@20: 	scheduler_ui->calendar_dialog = NULL;
renatofilho@20: 	scheduler_ui->calendar = NULL;
renatofilho@20: }
renatofilho@20: 
renatofilho@20: //calendar
renatofilho@20: static void
renatofilho@20: run_calendar_dialog (GtkButton *button, gpointer data)
renatofilho@20: {
renatofilho@20: 
renatofilho@20: 	GtkWidget *dialog_vbox;
renatofilho@20: 	MMythSchedulerUI *scheduler_ui = (MMythSchedulerUI*) data;
renatofilho@20: 
renatofilho@20: 	// calendar_dialog and calendar are been released at set_date_from_calendar ()
renatofilho@20: 	scheduler_ui->calendar_dialog = gtk_dialog_new ();
renatofilho@20: 	gtk_container_set_border_width (GTK_CONTAINER (scheduler_ui->calendar_dialog), 1);
renatofilho@20:   	gtk_window_set_title (GTK_WINDOW (scheduler_ui->calendar_dialog), "Select starting date");
renatofilho@20:   	gtk_window_set_position (GTK_WINDOW (scheduler_ui->calendar_dialog), GTK_WIN_POS_CENTER);
renatofilho@20: 	gtk_window_set_decorated (GTK_WINDOW (scheduler_ui->calendar_dialog), FALSE);
renatofilho@20: 
renatofilho@20: 	dialog_vbox = GTK_DIALOG (scheduler_ui->calendar_dialog)->vbox;
renatofilho@20: 
renatofilho@20: 	scheduler_ui->calendar = gtk_calendar_new ();
renatofilho@20: 
renatofilho@20: 	gtk_box_pack_start (GTK_BOX (dialog_vbox), scheduler_ui->calendar, TRUE, TRUE, 0);
renatofilho@20: 	gtk_calendar_display_options (GTK_CALENDAR (scheduler_ui->calendar),
renatofilho@20:         GTK_CALENDAR_SHOW_HEADING | GTK_CALENDAR_SHOW_DAY_NAMES);
renatofilho@20: 
renatofilho@20: 	gtk_widget_show_all (scheduler_ui->calendar_dialog);
renatofilho@20: 
renatofilho@20: 	g_signal_connect (G_OBJECT (scheduler_ui->calendar), "day-selected-double-click",
renatofilho@20:                       G_CALLBACK (set_date_from_calendar), data);
renatofilho@20: }
renatofilho@20: 
renatofilho@20: 
renatofilho@20: gboolean
renatofilho@20: mmyth_schedulerui_save (MMythSchedulerUI *scheduler_ui)
renatofilho@20: {
rosfran@244: 	GMythScheduler *scheduler;
renatofilho@20: 	ScheduleInfo *schedule_info;
renatofilho@20: 	GMythChannelInfo *channel_info;
renatofilho@20: 
renatofilho@20: 	GList *clist;
renatofilho@20: 	gint index, duration;
rosfran@244: 	gint frequency;
rosfran@244: 	struct tm start_tm;
rosfran@244: 
renatofilho@20: 	schedule_info = g_new0(ScheduleInfo, 1);
rosfran@244: 	if(schedule_info == NULL) {
rosfran@244: 		g_warning ("Error allocating memory");
rosfran@244: 		return FALSE;
rosfran@244: 	}
renatofilho@20: 
rosfran@244: 	clist = scheduler_ui->channel_list;
rosfran@244: 
rosfran@244: 	index = gtk_combo_box_get_active(GTK_COMBO_BOX(scheduler_ui->channel_combobox));
rosfran@244: 
rosfran@244: 	if (clist != NULL)
renatofilho@20: 		clist = g_list_nth(clist, index);
rosfran@244: 
rosfran@244: 	if (clist) {
rosfran@244: 		g_debug ("[%s] New schedule: %d", __FUNCTION__, index);
rosfran@244: 	} else {
rosfran@244: 		g_warning ("[%s] Error when adding new schedule", __FUNCTION__);
rosfran@244: 		return FALSE;
rosfran@244: 	}
rosfran@244: 
rosfran@244: 	channel_info = clist->data;
rosfran@244: 
rosfran@244: 	/* initialize schedule_info */
rosfran@244: 	schedule_info->channel_id = channel_info->channel_ID;
rosfran@244: 
rosfran@244: 	start_tm.tm_hour = 
rosfran@244: 		(int)gtk_spin_button_get_value(GTK_SPIN_BUTTON(scheduler_ui->hour_spinbutton));
renatofilho@20: 	start_tm.tm_min = 
rosfran@244: 		(int)gtk_spin_button_get_value(GTK_SPIN_BUTTON(scheduler_ui->min_spinbutton));
rosfran@244: 	start_tm.tm_sec = 0;
renatofilho@20: 
renatofilho@20: 	start_tm.tm_mday = (gint)scheduler_ui->day_temp;
renatofilho@20: 	start_tm.tm_mon =  (gint)scheduler_ui->month_temp;
renatofilho@20: 	start_tm.tm_year = (gint)scheduler_ui->year_temp - 1900; //years since 1900
rosfran@244: 	GTimeVal* time_val_local = g_new0( GTimeVal, 1 );
rosfran@244: 	time_val_local->tv_sec = timelocal(&start_tm);
renatofilho@20: 
rosfran@244: 	schedule_info->start_time = time_val_local;
rosfran@244: 	if ( NULL == schedule_info->start_time ) {
rosfran@244: 		g_warning ("timelocal error!\n");
rosfran@244: 		return FALSE;
rosfran@244: 	}
renatofilho@20: 
renatofilho@20: 	duration = (gint) gtk_spin_button_get_value(
rosfran@244: 			GTK_SPIN_BUTTON(scheduler_ui->duration_spinbutton));
rosfran@244: 	schedule_info->end_time = schedule_info->start_time + (duration*60);
renatofilho@20: 
rosfran@244: 	/* TODO: frequency is not implemented yet */
renatofilho@20: 	frequency = gtk_combo_box_get_active(GTK_COMBO_BOX(scheduler_ui->freq_combobox));
renatofilho@20: 
renatofilho@20: 	schedule_info->title = g_string_new("");
renatofilho@20: 	g_string_printf(schedule_info->title, "%s", 
rosfran@244: 			gtk_entry_get_text(GTK_ENTRY(scheduler_ui->title_entry)));
renatofilho@20: 
rosfran@244: 	/* FIXME: Architecture change to reuse the scheduler created in the recordui! */
rosfran@244: 	scheduler = gmyth_scheduler_new ();
renatofilho@20: 
rosfran@244: 	gmyth_scheduler_connect(scheduler, scheduler->backend_info);
renatofilho@20: 
rosfran@244: 	/* FIXME: set record_id = -1 to add a new schedule */
rosfran@244: 	schedule_info->record_id = -1;
rosfran@244: 	gmyth_scheduler_add_schedule (scheduler, schedule_info);
rosfran@244: 
rosfran@244: 	gmyth_scheduler_disconnect(scheduler);
rosfran@244: 
rosfran@244: 	/* free allocated memory */
rosfran@244: 	g_object_unref (scheduler);
rosfran@244: 	g_free (schedule_info);
rosfran@244: 
rosfran@244: 	return TRUE;
renatofilho@20: } 
renatofilho@20: 
renatofilho@20: static GtkWidget*
renatofilho@20: add_line (GtkWidget *vbox, const gchar *str)
renatofilho@20: {
renatofilho@20: 	GtkWidget *label;
renatofilho@20: 	GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
renatofilho@20: 
renatofilho@20: 	gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
renatofilho@20: 	gtk_container_set_border_width (GTK_CONTAINER (hbox), 3);
renatofilho@20: 	
renatofilho@20: 	label = gtk_label_new (str);
renatofilho@20: 	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
renatofilho@20: 	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);	
renatofilho@20: 
renatofilho@20: 	return hbox;
renatofilho@20: }
renatofilho@20: 
renatofilho@20: static void
renatofilho@20: add_channel_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
renatofilho@20: {
renatofilho@20: 	GtkWidget *combobox;
renatofilho@20: 	
renatofilho@20: 	GtkWidget *hbox = add_line (vbox, "Channel:    ");
renatofilho@20: 	
renatofilho@20: 	combobox = gtk_combo_box_new_text ();
renatofilho@20: 
renatofilho@20: 	scheduler_ui->channel_combobox = combobox;
renatofilho@20: 	gtk_box_pack_start (GTK_BOX (hbox), combobox, FALSE, FALSE, 0);
renatofilho@20: 			
renatofilho@20: 	GMythEPG *mmyth_epg = gmyth_epg_new ();
rosfran@244: 	if (!gmyth_epg_connect (mmyth_epg, scheduler_ui->backend_info)) {
renatofilho@20: 		// FIXME: Without this list the scheduler UI should not be shown!
renatofilho@20: 		g_warning ("[%s] Error when getting list of channels", __FUNCTION__);
renatofilho@20: 	}
renatofilho@20:   	
renatofilho@20: 	if (gmyth_epg_get_channel_list (mmyth_epg, &(scheduler_ui->channel_list)) < 0) {
renatofilho@20: 		g_debug ("[%s] Error while trying to retrieve channel list", __FUNCTION__);
renatofilho@20: 	} else {
renatofilho@20:  		GList *clist =  scheduler_ui->channel_list;
renatofilho@20: 		GMythChannelInfo *channel_info;
renatofilho@20: 
renatofilho@20: 		while (clist != NULL) {
renatofilho@20:   	  		channel_info = clist->data;
renatofilho@20:   	  		clist = clist->next;
renatofilho@20:   	  		gtk_combo_box_append_text (GTK_COMBO_BOX (scheduler_ui->channel_combobox), 
renatofilho@20:                                        (channel_info->channel_name->str));
renatofilho@20:   		}
renatofilho@20: 
renatofilho@20:        gtk_combo_box_set_active(GTK_COMBO_BOX (scheduler_ui->channel_combobox), 0);
renatofilho@20:   	}
renatofilho@20: }
renatofilho@20: 
renatofilho@20: static void
renatofilho@20: add_time_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
renatofilho@20: {
renatofilho@20: 	GtkWidget *label;
renatofilho@20: 	GtkObject *spinbutton_adj;
renatofilho@20: 	GtkWidget *hbox = add_line (vbox, "Time:         ");
renatofilho@20: 
renatofilho@20: 	time_t real_time;
renatofilho@20: 	struct tm sched_time;
renatofilho@20: 
renatofilho@20: 	time(&real_time);
renatofilho@20: 
renatofilho@20:     if (localtime_r((time_t *)&real_time, &sched_time) == NULL) {
renatofilho@20:         g_warning ("localtime_r error in mmyth_epg_grid_view!\n");
rosfran@244:         return;
renatofilho@20:     }
renatofilho@20:  
renatofilho@20: 	if (sched_time.tm_min>30){
renatofilho@20:   		sched_time.tm_hour = sched_time.tm_hour+1;
renatofilho@20:   		sched_time.tm_min = 0;
renatofilho@20: 	} else if (sched_time.tm_min>0) {
renatofilho@20:   		sched_time.tm_min = 30;
renatofilho@20: 	}
renatofilho@20: 
renatofilho@20:   	scheduler_ui->year_temp = (guint)sched_time.tm_year + 1900;
renatofilho@20:   	scheduler_ui->month_temp = (guint)sched_time.tm_mon;
renatofilho@20:   	scheduler_ui->day_temp = (guint)sched_time.tm_mday;
renatofilho@20:    
renatofilho@20: 	//hour entry
renatofilho@20: 	spinbutton_adj = gtk_adjustment_new (sched_time.tm_hour, 00, 23, 1, 10, 10);
renatofilho@20: 	scheduler_ui->hour_spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (spinbutton_adj), 1, 0);
renatofilho@20: 	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->hour_spinbutton, FALSE, FALSE, 0);
renatofilho@20: 
renatofilho@20: 	label = gtk_label_new ((" : "));
renatofilho@20: 	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
renatofilho@20: 	gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_RIGHT);
renatofilho@20: 	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
renatofilho@20: 
renatofilho@20: 	//minute entry
renatofilho@20: 	spinbutton_adj = gtk_adjustment_new (sched_time.tm_min, 0, 59, 1, 10, 10);
renatofilho@20: 	scheduler_ui->min_spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (spinbutton_adj), 1, 0);
renatofilho@20: 	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->min_spinbutton, FALSE, FALSE, 0);
renatofilho@20: 
renatofilho@20: 	label = gtk_label_new ((" (hh:mm)"));
renatofilho@20: 
renatofilho@20: 	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
renatofilho@20: 
renatofilho@20: }	
renatofilho@20: 
renatofilho@20: static void
renatofilho@20: add_date_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
renatofilho@20: {
renatofilho@20: 	char sched_date[24];
renatofilho@20: 	GtkWidget *hbox = add_line (vbox, "Date:        ");
renatofilho@20: 		
renatofilho@20: 	//sched_date = ctime(&real_time);
renatofilho@20:   	g_sprintf (sched_date, "%04d %02d %02d (yyyy mm dd)", scheduler_ui->year_temp, scheduler_ui->month_temp+1, scheduler_ui->day_temp);
renatofilho@20:   	sched_date[23]='\0';
renatofilho@20:   
renatofilho@20:   	scheduler_ui->date_button = gtk_button_new_with_label (sched_date);
renatofilho@20:   	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->date_button, FALSE, FALSE, 0);
renatofilho@20:   	gtk_button_set_relief (GTK_BUTTON (scheduler_ui->date_button), GTK_RELIEF_NONE);
renatofilho@20: 
renatofilho@20: 	g_signal_connect (G_OBJECT (scheduler_ui->date_button), "clicked",
renatofilho@20:                       G_CALLBACK (run_calendar_dialog), scheduler_ui);
renatofilho@20: 
renatofilho@20: }
renatofilho@20: 
renatofilho@20: static void
renatofilho@20: add_duration_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
renatofilho@20: {
renatofilho@20: 	GtkWidget *hbox = add_line (vbox, "Duration:   ");
renatofilho@20: 	GtkWidget *label;
renatofilho@20: 	GtkObject *spinbutton_adj;
renatofilho@20: 	
renatofilho@20: 	spinbutton_adj = gtk_adjustment_new (60, 5, 360, 5, 60, 60);
renatofilho@20:   	scheduler_ui->duration_spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (spinbutton_adj), 1, 0);
renatofilho@20:   	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->duration_spinbutton, FALSE, TRUE, 0);
renatofilho@20:   	gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (scheduler_ui->duration_spinbutton), TRUE);
renatofilho@20: 
renatofilho@20: 	label = gtk_label_new ((" (minutes)     "));
renatofilho@20:   	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
renatofilho@20: }
renatofilho@20: 
renatofilho@20: static void
renatofilho@20: add_frequency_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
renatofilho@20: {
renatofilho@20: 	
renatofilho@20: 	GtkWidget *hbox = add_line (vbox, "Frequency: ");	
renatofilho@20: 	
renatofilho@20: 	scheduler_ui->freq_combobox = gtk_combo_box_new_text ();
renatofilho@20: 	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->freq_combobox, FALSE, FALSE, 0);
renatofilho@20: 	gtk_combo_box_append_text (GTK_COMBO_BOX (scheduler_ui->freq_combobox), ("Only this day               "));
renatofilho@20: 	gtk_combo_box_append_text (GTK_COMBO_BOX (scheduler_ui->freq_combobox), ("Daily                            "));
renatofilho@20: 	gtk_combo_box_append_text (GTK_COMBO_BOX (scheduler_ui->freq_combobox), ("Weekly                         "));
renatofilho@20: 	gtk_combo_box_set_active(GTK_COMBO_BOX (scheduler_ui->freq_combobox), 0);
renatofilho@20: 
renatofilho@20: }
renatofilho@20: 
renatofilho@20: static void
renatofilho@20: add_title_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
renatofilho@20: {
renatofilho@20: 	GtkWidget *hbox = add_line (vbox, "Title:           ");
renatofilho@20: 	
renatofilho@20:   	scheduler_ui->title_entry = gtk_entry_new ();
renatofilho@20:   	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->title_entry, FALSE, FALSE, 0);
renatofilho@20:   	gtk_entry_set_text (GTK_ENTRY (scheduler_ui->title_entry), "(Optional)");
renatofilho@20: 	
renatofilho@20: }