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