maemo-ui/src/mmyth_schedulerui.c
branchtrunk
changeset 41 686549eb5657
child 208 c3c073032757
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/maemo-ui/src/mmyth_schedulerui.c	Mon Oct 23 16:02:15 2006 +0100
     1.3 @@ -0,0 +1,368 @@
     1.4 +#include <gtk/gtk.h>
     1.5 +#include <glib.h>
     1.6 +#include <glib/gprintf.h>
     1.7 +#include <sys/types.h>
     1.8 +#include <sys/stat.h>
     1.9 +#include <unistd.h>
    1.10 +#include <string.h>
    1.11 +#include <stdio.h>
    1.12 +
    1.13 +#include "mmyth_ui.h"
    1.14 +#include "mmyth_uicommon.h"
    1.15 +#include "mmyth_recordui.h"
    1.16 +#include "mmyth_schedulerui.h"
    1.17 +
    1.18 +/* GMyth library includes */
    1.19 +#include "gmyth_scheduler.h"
    1.20 +#include "gmyth_common.h"
    1.21 +#include "gmyth_epg.h"
    1.22 +
    1.23 +static void run_calendar_dialog (GtkButton *button, gpointer data);
    1.24 +
    1.25 +static void add_channel_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);
    1.26 +static void add_time_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);
    1.27 +static void add_date_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);
    1.28 +static void add_duration_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);
    1.29 +static void add_frequency_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);
    1.30 +static void add_title_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox);
    1.31 +
    1.32 +MMythSchedulerUI*
    1.33 +mmyth_schedulerui_new (void)
    1.34 +{
    1.35 +	GtkWidget *scrolledwindow;	
    1.36 +	GtkWidget *viewport;	
    1.37 +	GtkWidget *head_hbox;
    1.38 +	GtkWidget *fields_vbox;
    1.39 +	GtkWidget *hseparator;
    1.40 +	GtkWidget *label;
    1.41 +
    1.42 +	MMythSchedulerUI *scheduler_ui = g_new0 (MMythSchedulerUI, 1);
    1.43 +	
    1.44 +	scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
    1.45 +	scheduler_ui->main_widget = scrolledwindow;
    1.46 +	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow),
    1.47 +  			GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    1.48 +
    1.49 +	//Is this needed?  
    1.50 +	viewport = gtk_viewport_new (NULL, NULL);
    1.51 +	gtk_container_add (GTK_CONTAINER (scrolledwindow), viewport);
    1.52 +
    1.53 +	//Is this needed?
    1.54 +	head_hbox = gtk_hbox_new (FALSE, 0);
    1.55 +	gtk_container_add (GTK_CONTAINER (viewport), head_hbox);
    1.56 +
    1.57 +	fields_vbox = gtk_vbox_new (FALSE, 0);
    1.58 +	gtk_box_pack_start (GTK_BOX (head_hbox), fields_vbox, TRUE, TRUE, 0);
    1.59 +	gtk_container_set_border_width (GTK_CONTAINER (fields_vbox), 10);
    1.60 +
    1.61 +	label = gtk_label_new_with_mnemonic (("Manual Schedule Recording"));
    1.62 +	gtk_box_pack_start (GTK_BOX (fields_vbox), label, FALSE, FALSE, 0);
    1.63 +	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
    1.64 +
    1.65 +	hseparator = gtk_hseparator_new ();
    1.66 +	gtk_box_pack_start (GTK_BOX (fields_vbox), hseparator, FALSE, TRUE, 0);
    1.67 +
    1.68 +	add_channel_field (scheduler_ui, fields_vbox);
    1.69 +	add_time_field (scheduler_ui, fields_vbox);
    1.70 +	add_date_field (scheduler_ui, fields_vbox);
    1.71 +	add_duration_field (scheduler_ui, fields_vbox);
    1.72 +	add_frequency_field (scheduler_ui, fields_vbox);
    1.73 +	add_title_field (scheduler_ui, fields_vbox);
    1.74 +	
    1.75 +	return scheduler_ui;
    1.76 +}
    1.77 +
    1.78 +static void
    1.79 +set_date_from_calendar (GtkCalendar *calendar, gpointer data)
    1.80 +{
    1.81 +	char sched_date[24];
    1.82 +
    1.83 +	MMythSchedulerUI *scheduler_ui = (MMythSchedulerUI*) data;
    1.84 +
    1.85 +	// FIXME: Change this, save another value instead of month_temp, day_temp, ...	
    1.86 +	gtk_calendar_get_date(GTK_CALENDAR(calendar), 
    1.87 +		&(scheduler_ui->year_temp), &(scheduler_ui->month_temp), &(scheduler_ui->day_temp));
    1.88 +
    1.89 +	sched_date[23]='\0';
    1.90 +	g_sprintf(sched_date, "%04d %02d %02d (yyyy mm dd)",
    1.91 +		scheduler_ui->year_temp, scheduler_ui->month_temp+1, scheduler_ui->day_temp);
    1.92 +
    1.93 +	gtk_button_set_label(GTK_BUTTON(scheduler_ui->date_button), sched_date);
    1.94 +
    1.95 +	gtk_widget_destroy(scheduler_ui->calendar_dialog);
    1.96 +	scheduler_ui->calendar_dialog = NULL;
    1.97 +	scheduler_ui->calendar = NULL;
    1.98 +}
    1.99 +
   1.100 +//calendar
   1.101 +static void
   1.102 +run_calendar_dialog (GtkButton *button, gpointer data)
   1.103 +{
   1.104 +
   1.105 +	GtkWidget *dialog_vbox;
   1.106 +	MMythSchedulerUI *scheduler_ui = (MMythSchedulerUI*) data;
   1.107 +
   1.108 +	// calendar_dialog and calendar are been released at set_date_from_calendar ()
   1.109 +	scheduler_ui->calendar_dialog = gtk_dialog_new ();
   1.110 +	gtk_container_set_border_width (GTK_CONTAINER (scheduler_ui->calendar_dialog), 1);
   1.111 +  	gtk_window_set_title (GTK_WINDOW (scheduler_ui->calendar_dialog), "Select starting date");
   1.112 +  	gtk_window_set_position (GTK_WINDOW (scheduler_ui->calendar_dialog), GTK_WIN_POS_CENTER);
   1.113 +	gtk_window_set_decorated (GTK_WINDOW (scheduler_ui->calendar_dialog), FALSE);
   1.114 +
   1.115 +	dialog_vbox = GTK_DIALOG (scheduler_ui->calendar_dialog)->vbox;
   1.116 +
   1.117 +	scheduler_ui->calendar = gtk_calendar_new ();
   1.118 +
   1.119 +	gtk_box_pack_start (GTK_BOX (dialog_vbox), scheduler_ui->calendar, TRUE, TRUE, 0);
   1.120 +	gtk_calendar_display_options (GTK_CALENDAR (scheduler_ui->calendar),
   1.121 +        GTK_CALENDAR_SHOW_HEADING | GTK_CALENDAR_SHOW_DAY_NAMES);
   1.122 +
   1.123 +	gtk_widget_show_all (scheduler_ui->calendar_dialog);
   1.124 +
   1.125 +	g_signal_connect (G_OBJECT (scheduler_ui->calendar), "day-selected-double-click",
   1.126 +                      G_CALLBACK (set_date_from_calendar), data);
   1.127 +}
   1.128 +
   1.129 +
   1.130 +gboolean
   1.131 +mmyth_schedulerui_save (MMythSchedulerUI *scheduler_ui)
   1.132 +{
   1.133 +    GMythScheduler *scheduler;
   1.134 +	ScheduleInfo *schedule_info;
   1.135 +	GMythChannelInfo *channel_info;
   1.136 +
   1.137 +	GList *clist;
   1.138 +	gint index, duration;
   1.139 +    gint frequency;
   1.140 +    struct tm start_tm;
   1.141 +	
   1.142 +	schedule_info = g_new0(ScheduleInfo, 1);
   1.143 +    if(schedule_info == NULL) {
   1.144 +        g_warning ("Error allocating memory");
   1.145 +        return FALSE;
   1.146 +    }
   1.147 +        
   1.148 +    clist = scheduler_ui->channel_list;
   1.149 +      
   1.150 +    index = gtk_combo_box_get_active(GTK_COMBO_BOX(scheduler_ui->channel_combobox));
   1.151 +
   1.152 +    if (clist != NULL)
   1.153 +		clist = g_list_nth(clist, index);
   1.154 +    
   1.155 +    if (clist) {
   1.156 +	    g_debug ("[%s] New schedule: %d", __FUNCTION__, index);
   1.157 +    } else {
   1.158 +	    g_warning ("[%s] Error when adding new schedule", __FUNCTION__);
   1.159 +	    return FALSE;
   1.160 +    }
   1.161 +    
   1.162 +    channel_info = clist->data;
   1.163 +    
   1.164 +    /* initialize schedule_info */
   1.165 +    schedule_info->channel_id = channel_info->channel_ID;
   1.166 +    
   1.167 +    start_tm.tm_hour = 
   1.168 +        (int)gtk_spin_button_get_value(GTK_SPIN_BUTTON(scheduler_ui->hour_spinbutton));
   1.169 +	start_tm.tm_min = 
   1.170 +        (int)gtk_spin_button_get_value(GTK_SPIN_BUTTON(scheduler_ui->min_spinbutton));
   1.171 +    start_tm.tm_sec = 0;
   1.172 +
   1.173 +	start_tm.tm_mday = (gint)scheduler_ui->day_temp;
   1.174 +	start_tm.tm_mon =  (gint)scheduler_ui->month_temp;
   1.175 +	start_tm.tm_year = (gint)scheduler_ui->year_temp - 1900; //years since 1900
   1.176 +
   1.177 +    schedule_info->start_time = timelocal(&start_tm);
   1.178 +    if (schedule_info->start_time == (time_t)(-1)) {
   1.179 +        g_warning ("timelocal error!\n");
   1.180 +        return FALSE;
   1.181 +    }
   1.182 +
   1.183 +	duration = (gint) gtk_spin_button_get_value(
   1.184 +                        GTK_SPIN_BUTTON(scheduler_ui->duration_spinbutton));
   1.185 +    schedule_info->end_time = schedule_info->start_time + (duration*60);
   1.186 +
   1.187 +    /* TODO: frequency is not implemented yet */
   1.188 +	frequency = gtk_combo_box_get_active(GTK_COMBO_BOX(scheduler_ui->freq_combobox));
   1.189 +
   1.190 +	schedule_info->title = g_string_new("");
   1.191 +	g_string_printf(schedule_info->title, "%s", 
   1.192 +                    gtk_entry_get_text(GTK_ENTRY(scheduler_ui->title_entry)));
   1.193 +	
   1.194 +    /* FIXME: Architecture change to reuse the scheduler created in the recordui! */
   1.195 +    scheduler = gmyth_scheduler_new ();
   1.196 +
   1.197 +    gmyth_scheduler_connect(scheduler);
   1.198 +
   1.199 +    /* FIXME: set record_id = -1 to add a new schedule */
   1.200 +    schedule_info->record_id = -1;
   1.201 +    gmyth_scheduler_add_schedule (scheduler, schedule_info);
   1.202 +
   1.203 +    gmyth_scheduler_disconnect(scheduler);
   1.204 +    
   1.205 +    /* free allocated memory */
   1.206 +    g_object_unref (scheduler);
   1.207 +    g_free (schedule_info);
   1.208 +	
   1.209 +    return TRUE;
   1.210 +} 
   1.211 +
   1.212 +static GtkWidget*
   1.213 +add_line (GtkWidget *vbox, const gchar *str)
   1.214 +{
   1.215 +	GtkWidget *label;
   1.216 +	GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
   1.217 +
   1.218 +	gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
   1.219 +	gtk_container_set_border_width (GTK_CONTAINER (hbox), 3);
   1.220 +	
   1.221 +	label = gtk_label_new (str);
   1.222 +	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
   1.223 +	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);	
   1.224 +
   1.225 +	return hbox;
   1.226 +}
   1.227 +
   1.228 +static void
   1.229 +add_channel_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
   1.230 +{
   1.231 +	GtkWidget *combobox;
   1.232 +	
   1.233 +	GtkWidget *hbox = add_line (vbox, "Channel:    ");
   1.234 +	
   1.235 +	combobox = gtk_combo_box_new_text ();
   1.236 +
   1.237 +	scheduler_ui->channel_combobox = combobox;
   1.238 +	gtk_box_pack_start (GTK_BOX (hbox), combobox, FALSE, FALSE, 0);
   1.239 +			
   1.240 +	GMythEPG *mmyth_epg = gmyth_epg_new ();
   1.241 +	if (!gmyth_epg_connect (mmyth_epg)) {
   1.242 +		// FIXME: Without this list the scheduler UI should not be shown!
   1.243 +		g_warning ("[%s] Error when getting list of channels", __FUNCTION__);
   1.244 +	}
   1.245 +  	
   1.246 +	if (gmyth_epg_get_channel_list (mmyth_epg, &(scheduler_ui->channel_list)) < 0) {
   1.247 +		g_debug ("[%s] Error while trying to retrieve channel list", __FUNCTION__);
   1.248 +	} else {
   1.249 + 		GList *clist =  scheduler_ui->channel_list;
   1.250 +		GMythChannelInfo *channel_info;
   1.251 +
   1.252 +		while (clist != NULL) {
   1.253 +  	  		channel_info = clist->data;
   1.254 +  	  		clist = clist->next;
   1.255 +  	  		gtk_combo_box_append_text (GTK_COMBO_BOX (scheduler_ui->channel_combobox), 
   1.256 +                                       (channel_info->channel_name->str));
   1.257 +  		}
   1.258 +
   1.259 +       gtk_combo_box_set_active(GTK_COMBO_BOX (scheduler_ui->channel_combobox), 0);
   1.260 +  	}
   1.261 +}
   1.262 +
   1.263 +static void
   1.264 +add_time_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
   1.265 +{
   1.266 +	GtkWidget *label;
   1.267 +	GtkObject *spinbutton_adj;
   1.268 +	GtkWidget *hbox = add_line (vbox, "Time:         ");
   1.269 +
   1.270 +	time_t real_time;
   1.271 +	struct tm sched_time;
   1.272 +
   1.273 +	time(&real_time);
   1.274 +
   1.275 +    if (localtime_r((time_t *)&real_time, &sched_time) == NULL) {
   1.276 +        g_warning ("localtime_r error in mmyth_epg_grid_view!\n");
   1.277 +        return NULL;
   1.278 +    }
   1.279 + 
   1.280 +	if (sched_time.tm_min>30){
   1.281 +  		sched_time.tm_hour = sched_time.tm_hour+1;
   1.282 +  		sched_time.tm_min = 0;
   1.283 +	} else if (sched_time.tm_min>0) {
   1.284 +  		sched_time.tm_min = 30;
   1.285 +	}
   1.286 +
   1.287 +  	scheduler_ui->year_temp = (guint)sched_time.tm_year + 1900;
   1.288 +  	scheduler_ui->month_temp = (guint)sched_time.tm_mon;
   1.289 +  	scheduler_ui->day_temp = (guint)sched_time.tm_mday;
   1.290 +   
   1.291 +	//hour entry
   1.292 +	spinbutton_adj = gtk_adjustment_new (sched_time.tm_hour, 00, 23, 1, 10, 10);
   1.293 +	scheduler_ui->hour_spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (spinbutton_adj), 1, 0);
   1.294 +	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->hour_spinbutton, FALSE, FALSE, 0);
   1.295 +
   1.296 +	label = gtk_label_new ((" : "));
   1.297 +	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
   1.298 +	gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_RIGHT);
   1.299 +	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
   1.300 +
   1.301 +	//minute entry
   1.302 +	spinbutton_adj = gtk_adjustment_new (sched_time.tm_min, 0, 59, 1, 10, 10);
   1.303 +	scheduler_ui->min_spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (spinbutton_adj), 1, 0);
   1.304 +	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->min_spinbutton, FALSE, FALSE, 0);
   1.305 +
   1.306 +	label = gtk_label_new ((" (hh:mm)"));
   1.307 +
   1.308 +	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
   1.309 +
   1.310 +}	
   1.311 +
   1.312 +static void
   1.313 +add_date_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
   1.314 +{
   1.315 +	char sched_date[24];
   1.316 +	GtkWidget *hbox = add_line (vbox, "Date:        ");
   1.317 +		
   1.318 +	//sched_date = ctime(&real_time);
   1.319 +  	g_sprintf (sched_date, "%04d %02d %02d (yyyy mm dd)", scheduler_ui->year_temp, scheduler_ui->month_temp+1, scheduler_ui->day_temp);
   1.320 +  	sched_date[23]='\0';
   1.321 +  
   1.322 +  	scheduler_ui->date_button = gtk_button_new_with_label (sched_date);
   1.323 +  	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->date_button, FALSE, FALSE, 0);
   1.324 +  	gtk_button_set_relief (GTK_BUTTON (scheduler_ui->date_button), GTK_RELIEF_NONE);
   1.325 +
   1.326 +	g_signal_connect (G_OBJECT (scheduler_ui->date_button), "clicked",
   1.327 +                      G_CALLBACK (run_calendar_dialog), scheduler_ui);
   1.328 +
   1.329 +}
   1.330 +
   1.331 +static void
   1.332 +add_duration_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
   1.333 +{
   1.334 +	GtkWidget *hbox = add_line (vbox, "Duration:   ");
   1.335 +	GtkWidget *label;
   1.336 +	GtkObject *spinbutton_adj;
   1.337 +	
   1.338 +	spinbutton_adj = gtk_adjustment_new (60, 5, 360, 5, 60, 60);
   1.339 +  	scheduler_ui->duration_spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (spinbutton_adj), 1, 0);
   1.340 +  	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->duration_spinbutton, FALSE, TRUE, 0);
   1.341 +  	gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (scheduler_ui->duration_spinbutton), TRUE);
   1.342 +
   1.343 +	label = gtk_label_new ((" (minutes)     "));
   1.344 +  	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
   1.345 +}
   1.346 +
   1.347 +static void
   1.348 +add_frequency_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
   1.349 +{
   1.350 +	
   1.351 +	GtkWidget *hbox = add_line (vbox, "Frequency: ");	
   1.352 +	
   1.353 +	scheduler_ui->freq_combobox = gtk_combo_box_new_text ();
   1.354 +	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->freq_combobox, FALSE, FALSE, 0);
   1.355 +	gtk_combo_box_append_text (GTK_COMBO_BOX (scheduler_ui->freq_combobox), ("Only this day               "));
   1.356 +	gtk_combo_box_append_text (GTK_COMBO_BOX (scheduler_ui->freq_combobox), ("Daily                            "));
   1.357 +	gtk_combo_box_append_text (GTK_COMBO_BOX (scheduler_ui->freq_combobox), ("Weekly                         "));
   1.358 +	gtk_combo_box_set_active(GTK_COMBO_BOX (scheduler_ui->freq_combobox), 0);
   1.359 +
   1.360 +}
   1.361 +
   1.362 +static void
   1.363 +add_title_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
   1.364 +{
   1.365 +	GtkWidget *hbox = add_line (vbox, "Title:           ");
   1.366 +	
   1.367 +  	scheduler_ui->title_entry = gtk_entry_new ();
   1.368 +  	gtk_box_pack_start (GTK_BOX (hbox), scheduler_ui->title_entry, FALSE, FALSE, 0);
   1.369 +  	gtk_entry_set_text (GTK_ENTRY (scheduler_ui->title_entry), "(Optional)");
   1.370 +	
   1.371 +}