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