[svn r381] Solved minor problem with the time value.
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/gmyth_scheduler.h>
17 #include <gmyth/gmyth_common.h>
18 #include <gmyth/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 ( GMythBackendInfo *backend_info )
32 GtkWidget *scrolledwindow;
35 GtkWidget *fields_vbox;
36 GtkWidget *hseparator;
39 MMythSchedulerUI *scheduler_ui = g_new0 (MMythSchedulerUI, 1);
41 scheduler_ui->backend_info = backend_info;
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);
49 viewport = gtk_viewport_new (NULL, NULL);
50 gtk_container_add (GTK_CONTAINER (scrolledwindow), viewport);
53 head_hbox = gtk_hbox_new (FALSE, 0);
54 gtk_container_add (GTK_CONTAINER (viewport), head_hbox);
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);
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);
64 hseparator = gtk_hseparator_new ();
65 gtk_box_pack_start (GTK_BOX (fields_vbox), hseparator, FALSE, TRUE, 0);
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);
78 set_date_from_calendar (GtkCalendar *calendar, gpointer data)
82 MMythSchedulerUI *scheduler_ui = (MMythSchedulerUI*) data;
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));
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);
92 gtk_button_set_label(GTK_BUTTON(scheduler_ui->date_button), sched_date);
94 gtk_widget_destroy(scheduler_ui->calendar_dialog);
95 scheduler_ui->calendar_dialog = NULL;
96 scheduler_ui->calendar = NULL;
101 run_calendar_dialog (GtkButton *button, gpointer data)
104 GtkWidget *dialog_vbox;
105 MMythSchedulerUI *scheduler_ui = (MMythSchedulerUI*) data;
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);
114 dialog_vbox = GTK_DIALOG (scheduler_ui->calendar_dialog)->vbox;
116 scheduler_ui->calendar = gtk_calendar_new ();
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);
122 gtk_widget_show_all (scheduler_ui->calendar_dialog);
124 g_signal_connect (G_OBJECT (scheduler_ui->calendar), "day-selected-double-click",
125 G_CALLBACK (set_date_from_calendar), data);
130 mmyth_schedulerui_save (MMythSchedulerUI *scheduler_ui)
132 GMythScheduler *scheduler;
133 ScheduleInfo *schedule_info;
134 GMythChannelInfo *channel_info;
137 gint index, duration;
141 schedule_info = g_new0(ScheduleInfo, 1);
142 if(schedule_info == NULL) {
143 g_warning ("Error allocating memory");
147 clist = scheduler_ui->channel_list;
149 index = gtk_combo_box_get_active(GTK_COMBO_BOX(scheduler_ui->channel_combobox));
152 clist = g_list_nth(clist, index);
155 g_debug ("[%s] New schedule: %d", __FUNCTION__, index);
157 g_warning ("[%s] Error when adding new schedule", __FUNCTION__);
161 channel_info = clist->data;
163 /* initialize schedule_info */
164 schedule_info->channel_id = channel_info->channel_ID;
167 (int)gtk_spin_button_get_value(GTK_SPIN_BUTTON(scheduler_ui->hour_spinbutton));
169 (int)gtk_spin_button_get_value(GTK_SPIN_BUTTON(scheduler_ui->min_spinbutton));
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);
178 schedule_info->start_time = time_val_local;
179 if ( NULL == schedule_info->start_time ) {
180 g_warning ("timelocal error!\n");
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);
188 /* TODO: frequency is not implemented yet */
189 frequency = gtk_combo_box_get_active(GTK_COMBO_BOX(scheduler_ui->freq_combobox));
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)));
195 /* FIXME: Architecture change to reuse the scheduler created in the recordui! */
196 scheduler = gmyth_scheduler_new ();
198 gmyth_scheduler_connect(scheduler, scheduler->backend_info);
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);
204 gmyth_scheduler_disconnect(scheduler);
206 /* free allocated memory */
207 g_object_unref (scheduler);
208 g_free (schedule_info);
214 add_line (GtkWidget *vbox, const gchar *str)
217 GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
219 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
220 gtk_container_set_border_width (GTK_CONTAINER (hbox), 3);
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);
230 add_channel_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
234 GtkWidget *hbox = add_line (vbox, "Channel: ");
236 combobox = gtk_combo_box_new_text ();
238 scheduler_ui->channel_combobox = combobox;
239 gtk_box_pack_start (GTK_BOX (hbox), combobox, FALSE, FALSE, 0);
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__);
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__);
250 GList *clist = scheduler_ui->channel_list;
251 GMythChannelInfo *channel_info;
253 while (clist != NULL) {
254 channel_info = clist->data;
256 gtk_combo_box_append_text (GTK_COMBO_BOX (scheduler_ui->channel_combobox),
257 (channel_info->channel_name->str));
260 gtk_combo_box_set_active(GTK_COMBO_BOX (scheduler_ui->channel_combobox), 0);
265 add_time_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
268 GtkObject *spinbutton_adj;
269 GtkWidget *hbox = add_line (vbox, "Time: ");
272 struct tm sched_time;
276 if (localtime_r((time_t *)&real_time, &sched_time) == NULL) {
277 g_warning ("localtime_r error in mmyth_epg_grid_view!\n");
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;
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;
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);
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);
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);
307 label = gtk_label_new ((" (hh:mm)"));
309 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
314 add_date_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
317 GtkWidget *hbox = add_line (vbox, "Date: ");
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);
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);
327 g_signal_connect (G_OBJECT (scheduler_ui->date_button), "clicked",
328 G_CALLBACK (run_calendar_dialog), scheduler_ui);
333 add_duration_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
335 GtkWidget *hbox = add_line (vbox, "Duration: ");
337 GtkObject *spinbutton_adj;
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);
344 label = gtk_label_new ((" (minutes) "));
345 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
349 add_frequency_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
352 GtkWidget *hbox = add_line (vbox, "Frequency: ");
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);
364 add_title_field (MMythSchedulerUI *scheduler_ui, GtkWidget *vbox)
366 GtkWidget *hbox = add_line (vbox, "Title: ");
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)");