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