maemo-ui/src/mmyth_schedulerui.c
author renatofilho
Wed Jun 13 18:18:42 2007 +0100 (2007-06-13)
branchtrunk
changeset 749 fb471f7af20b
parent 208 c3c073032757
child 754 cb885ee44618
permissions -rw-r--r--
[svn r755] fixed file output permission
     1 #include <gtk/gtk.h>
     2 #include <glib.h>
     3 #include <glib/gprintf.h>
     4 #include <sys/types.h>
     5 #include <sys/stat.h>
     6 #include <unistd.h>
     7 #include <string.h>
     8 #include <stdio.h>
     9 
    10 #include "mmyth_ui.h"
    11 #include "mmyth_uicommon.h"
    12 #include "mmyth_recordui.h"
    13 #include "mmyth_schedulerui.h"
    14 
    15 /* GMyth library includes */
    16 #include <gmyth/gmyth_scheduler.h>
    17 #include <gmyth/gmyth_common.h>
    18 #include <gmyth/gmyth_epg.h>
    19 
    20 static void run_calendar_dialog (GtkButton *button, gpointer data);
    21 
    22 static void add_channel_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);
    23 static void add_time_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);
    24 static void add_date_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);
    25 static void add_duration_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);
    26 static void add_frequency_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);
    27 static void add_title_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);
    28 
    29 MMythSchedulerUI*
    30 mmyth_schedulerui_new ( GMythBackendInfo *backend_info )
    31 {
    32 	GtkWidget *scrolledwindow;	
    33 	GtkWidget *viewport;	
    34 	GtkWidget *head_hbox;
    35 	GtkWidget *fields_vbox;
    36 	GtkWidget *hseparator;
    37 	GtkWidget *label;
    38 
    39 	MMythSchedulerUI *scheduler_ui = g_new0 (MMythSchedulerUI, 1);
    40 
    41 	scheduler_ui->backend_info = backend_info;
    42 	
    43 	scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
    44 	scheduler_ui->main_widget = scrolledwindow;
    45 	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow),
    46   			GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    47 
    48 	//Is this needed?  
    49 	viewport = gtk_viewport_new (NULL, NULL);
    50 	gtk_container_add (GTK_CONTAINER (scrolledwindow), viewport);
    51 
    52 	//Is this needed?
    53 	head_hbox = gtk_hbox_new (FALSE, 0);
    54 	gtk_container_add (GTK_CONTAINER (viewport), head_hbox);
    55 
    56 	fields_vbox = gtk_vbox_new (FALSE, 0);
    57 	gtk_box_pack_start (GTK_BOX (head_hbox), fields_vbox, TRUE, TRUE, 0);
    58 	gtk_container_set_border_width (GTK_CONTAINER (fields_vbox), 10);
    59 
    60 	label = gtk_label_new_with_mnemonic (("Manual Schedule Recording"));
    61 	gtk_box_pack_start (GTK_BOX (fields_vbox), label, FALSE, FALSE, 0);
    62 	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
    63 
    64 	hseparator = gtk_hseparator_new ();
    65 	gtk_box_pack_start (GTK_BOX (fields_vbox), hseparator, FALSE, TRUE, 0);
    66 
    67 	add_channel_field (scheduler_ui, fields_vbox);
    68 	add_time_field (scheduler_ui, fields_vbox);
    69 	add_date_field (scheduler_ui, fields_vbox);
    70 	add_duration_field (scheduler_ui, fields_vbox);
    71 	add_frequency_field (scheduler_ui, fields_vbox);
    72 	add_title_field (scheduler_ui, fields_vbox);
    73 	
    74 	return scheduler_ui;
    75 }
    76 
    77 static void
    78 set_date_from_calendar (GtkCalendar *calendar, gpointer data)
    79 {
    80 	char sched_date[24];
    81 
    82 	MMythSchedulerUI *scheduler_ui = (MMythSchedulerUI*) data;
    83 
    84 	// FIXME: Change this, save another value instead of month_temp, day_temp, ...	
    85 	gtk_calendar_get_date(GTK_CALENDAR(calendar), 
    86 		&(scheduler_ui->year_temp), &(scheduler_ui->month_temp), &(scheduler_ui->day_temp));
    87 
    88 	sched_date[23]='\0';
    89 	g_sprintf(sched_date, "%04d %02d %02d (yyyy mm dd)",
    90 		scheduler_ui->year_temp, scheduler_ui->month_temp+1, scheduler_ui->day_temp);
    91 
    92 	gtk_button_set_label(GTK_BUTTON(scheduler_ui->date_button), sched_date);
    93 
    94 	gtk_widget_destroy(scheduler_ui->calendar_dialog);
    95 	scheduler_ui->calendar_dialog = NULL;
    96 	scheduler_ui->calendar = NULL;
    97 }
    98 
    99 //calendar
   100 static void
   101 run_calendar_dialog (GtkButton *button, gpointer data)
   102 {
   103 
   104 	GtkWidget *dialog_vbox;
   105 	MMythSchedulerUI *scheduler_ui = (MMythSchedulerUI*) data;
   106 
   107 	// calendar_dialog and calendar are been released at set_date_from_calendar ()
   108 	scheduler_ui->calendar_dialog = gtk_dialog_new ();
   109 	gtk_container_set_border_width (GTK_CONTAINER (scheduler_ui->calendar_dialog), 1);
   110   	gtk_window_set_title (GTK_WINDOW (scheduler_ui->calendar_dialog), "Select starting date");
   111   	gtk_window_set_position (GTK_WINDOW (scheduler_ui->calendar_dialog), GTK_WIN_POS_CENTER);
   112 	gtk_window_set_decorated (GTK_WINDOW (scheduler_ui->calendar_dialog), FALSE);
   113 
   114 	dialog_vbox = GTK_DIALOG (scheduler_ui->calendar_dialog)->vbox;
   115 
   116 	scheduler_ui->calendar = gtk_calendar_new ();
   117 
   118 	gtk_box_pack_start (GTK_BOX (dialog_vbox), scheduler_ui->calendar, TRUE, TRUE, 0);
   119 	gtk_calendar_display_options (GTK_CALENDAR (scheduler_ui->calendar),
   120         GTK_CALENDAR_SHOW_HEADING | GTK_CALENDAR_SHOW_DAY_NAMES);
   121 
   122 	gtk_widget_show_all (scheduler_ui->calendar_dialog);
   123 
   124 	g_signal_connect (G_OBJECT (scheduler_ui->calendar), "day-selected-double-click",
   125                       G_CALLBACK (set_date_from_calendar), data);
   126 }
   127 
   128 
   129 gboolean
   130 mmyth_schedulerui_save (MMythSchedulerUI *scheduler_ui)
   131 {
   132 	GMythScheduler *scheduler;
   133 	ScheduleInfo *schedule_info;
   134 	GMythChannelInfo *channel_info;
   135 
   136 	GList *clist;
   137 	gint index, duration;
   138 	gint frequency;
   139 	struct tm start_tm;
   140 
   141 	schedule_info = g_new0(ScheduleInfo, 1);
   142 	if(schedule_info == NULL) {
   143 		g_warning ("Error allocating memory");
   144 		return FALSE;
   145 	}
   146 
   147 	clist = scheduler_ui->channel_list;
   148 
   149 	index = gtk_combo_box_get_active(GTK_COMBO_BOX(scheduler_ui->channel_combobox));
   150 
   151 	if (clist != NULL)
   152 		clist = g_list_nth(clist, index);
   153 
   154 	if (clist) {
   155 		g_debug ("[%s] New schedule: %d", __FUNCTION__, index);
   156 	} else {
   157 		g_warning ("[%s] Error when adding new schedule", __FUNCTION__);
   158 		return FALSE;
   159 	}
   160 
   161 	channel_info = clist->data;
   162 
   163 	/* initialize schedule_info */
   164 	schedule_info->channel_id = channel_info->channel_ID;
   165 
   166 	start_tm.tm_hour = 
   167 		(int)gtk_spin_button_get_value(GTK_SPIN_BUTTON(scheduler_ui->hour_spinbutton));
   168 	start_tm.tm_min = 
   169 		(int)gtk_spin_button_get_value(GTK_SPIN_BUTTON(scheduler_ui->min_spinbutton));
   170 	start_tm.tm_sec = 0;
   171 
   172 	start_tm.tm_mday = (gint)scheduler_ui->day_temp;
   173 	start_tm.tm_mon =  (gint)scheduler_ui->month_temp;
   174 	start_tm.tm_year = (gint)scheduler_ui->year_temp - 1900; //years since 1900
   175 	GTimeVal* time_val_local = g_new0( GTimeVal, 1 );
   176 	time_val_local->tv_sec = timelocal(&start_tm);
   177 
   178 	schedule_info->start_time = time_val_local;
   179 	if ( NULL == schedule_info->start_time ) {
   180 		g_warning ("timelocal error!\n");
   181 		return FALSE;
   182 	}
   183 
   184 	duration = (gint) gtk_spin_button_get_value(
   185 			GTK_SPIN_BUTTON(scheduler_ui->duration_spinbutton));
   186 	schedule_info->end_time = schedule_info->start_time + (duration*60);
   187 
   188 	/* TODO: frequency is not implemented yet */
   189 	frequency = gtk_combo_box_get_active(GTK_COMBO_BOX(scheduler_ui->freq_combobox));
   190 
   191 	schedule_info->title = g_string_new("");
   192 	g_string_printf(schedule_info->title, "%s", 
   193 			gtk_entry_get_text(GTK_ENTRY(scheduler_ui->title_entry)));
   194 
   195 	/* FIXME: Architecture change to reuse the scheduler created in the recordui! */
   196 	scheduler = gmyth_scheduler_new ();
   197 
   198 	gmyth_scheduler_connect(scheduler, scheduler->backend_info);
   199 
   200 	/* FIXME: set record_id = -1 to add a new schedule */
   201 	schedule_info->record_id = -1;
   202 	gmyth_scheduler_add_schedule (scheduler, schedule_info);
   203 
   204 	gmyth_scheduler_disconnect(scheduler);
   205 
   206 	/* free allocated memory */
   207 	g_object_unref (scheduler);
   208 	g_free (schedule_info);
   209 
   210 	return TRUE;
   211 } 
   212 
   213 static GtkWidget*
   214 add_line (GtkWidget *vbox, const gchar *str)
   215 {
   216 	GtkWidget *label;
   217 	GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
   218 
   219 	gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
   220 	gtk_container_set_border_width (GTK_CONTAINER (hbox), 3);
   221 	
   222 	label = gtk_label_new (str);
   223 	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
   224 	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);	
   225 
   226 	return hbox;
   227 }
   228 
   229 static void
   230 add_channel_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
   231 {
   232 	GtkWidget *combobox;
   233 	
   234 	GtkWidget *hbox = add_line (vbox, "Channel:    ");
   235 	
   236 	combobox = gtk_combo_box_new_text ();
   237 
   238 	scheduler_ui->channel_combobox = combobox;
   239 	gtk_box_pack_start (GTK_BOX (hbox), combobox, FALSE, FALSE, 0);
   240 			
   241 	GMythEPG *mmyth_epg = gmyth_epg_new ();
   242 	if (!gmyth_epg_connect (mmyth_epg, scheduler_ui->backend_info)) {
   243 		// FIXME: Without this list the scheduler UI should not be shown!
   244 		g_warning ("[%s] Error when getting list of channels", __FUNCTION__);
   245 	}
   246   	
   247 	if (gmyth_epg_get_channel_list (mmyth_epg, &(scheduler_ui->channel_list)) < 0) {
   248 		g_debug ("[%s] Error while trying to retrieve channel list", __FUNCTION__);
   249 	} else {
   250  		GList *clist =  scheduler_ui->channel_list;
   251 		GMythChannelInfo *channel_info;
   252 
   253 		while (clist != NULL) {
   254   	  		channel_info = clist->data;
   255   	  		clist = clist->next;
   256   	  		gtk_combo_box_append_text (GTK_COMBO_BOX (scheduler_ui->channel_combobox), 
   257                                        (channel_info->channel_name->str));
   258   		}
   259 
   260        gtk_combo_box_set_active(GTK_COMBO_BOX (scheduler_ui->channel_combobox), 0);
   261   	}
   262 }
   263 
   264 static void
   265 add_time_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
   266 {
   267 	GtkWidget *label;
   268 	GtkObject *spinbutton_adj;
   269 	GtkWidget *hbox = add_line (vbox, "Time:         ");
   270 
   271 	time_t real_time;
   272 	struct tm sched_time;
   273 
   274 	time(&real_time);
   275 
   276     if (localtime_r((time_t *)&real_time, &sched_time) == NULL) {
   277         g_warning ("localtime_r error in mmyth_epg_grid_view!\n");
   278         return;
   279     }
   280  
   281 	if (sched_time.tm_min>30){
   282   		sched_time.tm_hour = sched_time.tm_hour+1;
   283   		sched_time.tm_min = 0;
   284 	} else if (sched_time.tm_min>0) {
   285   		sched_time.tm_min = 30;
   286 	}
   287 
   288   	scheduler_ui->year_temp = (guint)sched_time.tm_year + 1900;
   289   	scheduler_ui->month_temp = (guint)sched_time.tm_mon;
   290   	scheduler_ui->day_temp = (guint)sched_time.tm_mday;
   291    
   292 	//hour entry
   293 	spinbutton_adj = gtk_adjustment_new (sched_time.tm_hour, 00, 23, 1, 10, 10);
   294 	scheduler_ui->hour_spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (spinbutton_adj), 1, 0);
   295 	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->hour_spinbutton, FALSE, FALSE, 0);
   296 
   297 	label = gtk_label_new ((" : "));
   298 	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
   299 	gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_RIGHT);
   300 	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
   301 
   302 	//minute entry
   303 	spinbutton_adj = gtk_adjustment_new (sched_time.tm_min, 0, 59, 1, 10, 10);
   304 	scheduler_ui->min_spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (spinbutton_adj), 1, 0);
   305 	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->min_spinbutton, FALSE, FALSE, 0);
   306 
   307 	label = gtk_label_new ((" (hh:mm)"));
   308 
   309 	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
   310 
   311 }	
   312 
   313 static void
   314 add_date_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
   315 {
   316 	char sched_date[24];
   317 	GtkWidget *hbox = add_line (vbox, "Date:        ");
   318 		
   319 	//sched_date = ctime(&real_time);
   320   	g_sprintf (sched_date, "%04d %02d %02d (yyyy mm dd)", scheduler_ui->year_temp, scheduler_ui->month_temp+1, scheduler_ui->day_temp);
   321   	sched_date[23]='\0';
   322   
   323   	scheduler_ui->date_button = gtk_button_new_with_label (sched_date);
   324   	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->date_button, FALSE, FALSE, 0);
   325   	gtk_button_set_relief (GTK_BUTTON (scheduler_ui->date_button), GTK_RELIEF_NONE);
   326 
   327 	g_signal_connect (G_OBJECT (scheduler_ui->date_button), "clicked",
   328                       G_CALLBACK (run_calendar_dialog), scheduler_ui);
   329 
   330 }
   331 
   332 static void
   333 add_duration_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
   334 {
   335 	GtkWidget *hbox = add_line (vbox, "Duration:   ");
   336 	GtkWidget *label;
   337 	GtkObject *spinbutton_adj;
   338 	
   339 	spinbutton_adj = gtk_adjustment_new (60, 5, 360, 5, 60, 60);
   340   	scheduler_ui->duration_spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (spinbutton_adj), 1, 0);
   341   	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->duration_spinbutton, FALSE, TRUE, 0);
   342   	gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (scheduler_ui->duration_spinbutton), TRUE);
   343 
   344 	label = gtk_label_new ((" (minutes)     "));
   345   	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
   346 }
   347 
   348 static void
   349 add_frequency_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
   350 {
   351 	
   352 	GtkWidget *hbox = add_line (vbox, "Frequency: ");	
   353 	
   354 	scheduler_ui->freq_combobox = gtk_combo_box_new_text ();
   355 	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->freq_combobox, FALSE, FALSE, 0);
   356 	gtk_combo_box_append_text (GTK_COMBO_BOX (scheduler_ui->freq_combobox), ("Only this day               "));
   357 	gtk_combo_box_append_text (GTK_COMBO_BOX (scheduler_ui->freq_combobox), ("Daily                            "));
   358 	gtk_combo_box_append_text (GTK_COMBO_BOX (scheduler_ui->freq_combobox), ("Weekly                         "));
   359 	gtk_combo_box_set_active(GTK_COMBO_BOX (scheduler_ui->freq_combobox), 0);
   360 
   361 }
   362 
   363 static void
   364 add_title_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
   365 {
   366 	GtkWidget *hbox = add_line (vbox, "Title:           ");
   367 	
   368   	scheduler_ui->title_entry = gtk_entry_new ();
   369   	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->title_entry, FALSE, FALSE, 0);
   370   	gtk_entry_set_text (GTK_ENTRY (scheduler_ui->title_entry), "(Optional)");
   371 	
   372 }