[svn r73] Deals with another kind of MythTV backend messages (changing TV chain).
3 #include <glib/gprintf.h>
11 #include "mmyth_uicommon.h"
12 #include "mmyth_recordui.h"
13 #include "mmyth_schedulerui.h"
15 /* GMyth library includes */
16 #include "gmyth_scheduler.h"
17 #include "gmyth_common.h"
18 #include "gmyth_epg.h"
20 static void run_calendar_dialog (GtkButton *button, gpointer data);
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);
30 mmyth_schedulerui_new (void)
32 GtkWidget *scrolledwindow;
35 GtkWidget *fields_vbox;
36 GtkWidget *hseparator;
39 MMythSchedulerUI *scheduler_ui = g_new0 (MMythSchedulerUI, 1);
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);
47 viewport = gtk_viewport_new (NULL, NULL);
48 gtk_container_add (GTK_CONTAINER (scrolledwindow), viewport);
51 head_hbox = gtk_hbox_new (FALSE, 0);
52 gtk_container_add (GTK_CONTAINER (viewport), head_hbox);
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);
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);
62 hseparator = gtk_hseparator_new ();
63 gtk_box_pack_start (GTK_BOX (fields_vbox), hseparator, FALSE, TRUE, 0);
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);
76 set_date_from_calendar (GtkCalendar *calendar, gpointer data)
80 MMythSchedulerUI *scheduler_ui = (MMythSchedulerUI*) data;
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));
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);
90 gtk_button_set_label(GTK_BUTTON(scheduler_ui->date_button), sched_date);
92 gtk_widget_destroy(scheduler_ui->calendar_dialog);
93 scheduler_ui->calendar_dialog = NULL;
94 scheduler_ui->calendar = NULL;
99 run_calendar_dialog (GtkButton *button, gpointer data)
102 GtkWidget *dialog_vbox;
103 MMythSchedulerUI *scheduler_ui = (MMythSchedulerUI*) data;
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);
112 dialog_vbox = GTK_DIALOG (scheduler_ui->calendar_dialog)->vbox;
114 scheduler_ui->calendar = gtk_calendar_new ();
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);
120 gtk_widget_show_all (scheduler_ui->calendar_dialog);
122 g_signal_connect (G_OBJECT (scheduler_ui->calendar), "day-selected-double-click",
123 G_CALLBACK (set_date_from_calendar), data);
128 mmyth_schedulerui_save (MMythSchedulerUI *scheduler_ui)
130 GMythScheduler *scheduler;
131 ScheduleInfo *schedule_info;
132 GMythChannelInfo *channel_info;
135 gint index, duration;
139 schedule_info = g_new0(ScheduleInfo, 1);
140 if(schedule_info == NULL) {
141 g_warning ("Error allocating memory");
145 clist = scheduler_ui->channel_list;
147 index = gtk_combo_box_get_active(GTK_COMBO_BOX(scheduler_ui->channel_combobox));
150 clist = g_list_nth(clist, index);
153 g_debug ("[%s] New schedule: %d", __FUNCTION__, index);
155 g_warning ("[%s] Error when adding new schedule", __FUNCTION__);
159 channel_info = clist->data;
161 /* initialize schedule_info */
162 schedule_info->channel_id = channel_info->channel_ID;
165 (int)gtk_spin_button_get_value(GTK_SPIN_BUTTON(scheduler_ui->hour_spinbutton));
167 (int)gtk_spin_button_get_value(GTK_SPIN_BUTTON(scheduler_ui->min_spinbutton));
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
174 schedule_info->start_time = timelocal(&start_tm);
175 if (schedule_info->start_time == (time_t)(-1)) {
176 g_warning ("timelocal error!\n");
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);
184 /* TODO: frequency is not implemented yet */
185 frequency = gtk_combo_box_get_active(GTK_COMBO_BOX(scheduler_ui->freq_combobox));
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)));
191 /* FIXME: Architecture change to reuse the scheduler created in the recordui! */
192 scheduler = gmyth_scheduler_new ();
194 gmyth_scheduler_connect(scheduler);
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);
200 gmyth_scheduler_disconnect(scheduler);
202 /* free allocated memory */
203 g_object_unref (scheduler);
204 g_free (schedule_info);
210 add_line (GtkWidget *vbox, const gchar *str)
213 GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
215 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
216 gtk_container_set_border_width (GTK_CONTAINER (hbox), 3);
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);
226 add_channel_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
230 GtkWidget *hbox = add_line (vbox, "Channel: ");
232 combobox = gtk_combo_box_new_text ();
234 scheduler_ui->channel_combobox = combobox;
235 gtk_box_pack_start (GTK_BOX (hbox), combobox, FALSE, FALSE, 0);
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__);
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__);
246 GList *clist = scheduler_ui->channel_list;
247 GMythChannelInfo *channel_info;
249 while (clist != NULL) {
250 channel_info = clist->data;
252 gtk_combo_box_append_text (GTK_COMBO_BOX (scheduler_ui->channel_combobox),
253 (channel_info->channel_name->str));
256 gtk_combo_box_set_active(GTK_COMBO_BOX (scheduler_ui->channel_combobox), 0);
261 add_time_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
264 GtkObject *spinbutton_adj;
265 GtkWidget *hbox = add_line (vbox, "Time: ");
268 struct tm sched_time;
272 if (localtime_r((time_t *)&real_time, &sched_time) == NULL) {
273 g_warning ("localtime_r error in mmyth_epg_grid_view!\n");
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;
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;
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);
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);
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);
303 label = gtk_label_new ((" (hh:mm)"));
305 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
310 add_date_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
313 GtkWidget *hbox = add_line (vbox, "Date: ");
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);
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);
323 g_signal_connect (G_OBJECT (scheduler_ui->date_button), "clicked",
324 G_CALLBACK (run_calendar_dialog), scheduler_ui);
329 add_duration_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
331 GtkWidget *hbox = add_line (vbox, "Duration: ");
333 GtkObject *spinbutton_adj;
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);
340 label = gtk_label_new ((" (minutes) "));
341 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
345 add_frequency_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
348 GtkWidget *hbox = add_line (vbox, "Frequency: ");
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);
360 add_title_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
362 GtkWidget *hbox = add_line (vbox, "Title: ");
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)");