maemo-ui-old/src/mmyth_recordui.c
author melunko
Tue Sep 18 13:53:38 2007 +0100 (2007-09-18)
branchtrunk
changeset 848 37e112f474db
parent 754 cb885ee44618
permissions -rw-r--r--
[svn r854] Added debian folder for gmencoder deb package generation
     1 #include <gtk/gtk.h>
     2 #include <glib.h>
     3 #include <sys/types.h>
     4 #include <sys/stat.h>
     5 #include <unistd.h>
     6 #include <string.h>
     7 #include <stdio.h>
     8 #include <stdlib.h>
     9 
    10 #include "mmyth_ui.h"
    11 #include "mmyth_recordui.h"
    12 
    13 /*
    14  * GMyth library includes 
    15  */
    16 #include <gmyth/gmyth_scheduler.h>
    17 #include <gmyth/gmyth_util.h>
    18 
    19 enum {
    20     START_DATE_COLUMN = 0,
    21     TITLE_COLUMN,
    22     CHAN_ID_COLUMN,
    23     END_TIME_COLUMN,
    24     RECORD_ID_COLUMN,
    25     BASENAME_COLUMN,
    26     N_COLUMNS
    27 };
    28 
    29 gboolean
    30 mmyth_recordui_reload_all(MMythRecordUI * recordui)
    31 {
    32     gboolean        res = FALSE;
    33 
    34     res = mmyth_recordui_reload_schedule(recordui);
    35 
    36     res = res & mmyth_recordui_reload_record(recordui);
    37 
    38 
    39     if (!res)
    40         g_warning
    41             ("[%s] Error while reloading schedule and recording content",
    42              __FUNCTION__);
    43 
    44     return res;
    45 }
    46 
    47 gboolean
    48 mmyth_recordui_reload_schedule(MMythRecordUI * recordui)
    49 {
    50     gint            new_row = 0;
    51     ScheduleInfo   *schedule_info;
    52     GList          *schedule_list;
    53     GtkTreeIter     iter;
    54     gchar          *start_date_time = NULL;
    55     gchar          *end_date_time = NULL;
    56     GString        *str_aux = g_string_new("");
    57     gint            res;
    58 
    59     gtk_tree_store_clear(recordui->sch_tree_store);
    60 
    61     res =
    62         gmyth_scheduler_get_schedule_list(recordui->scheduler,
    63                                           &(schedule_list));
    64     if (res < 0) {
    65         g_warning
    66             ("[%s] Retrieved NULL list of scheduled data from database",
    67              __FUNCTION__);
    68         return FALSE;
    69     }
    70 
    71     for (; schedule_list; schedule_list = schedule_list->next) {
    72         schedule_info = (ScheduleInfo *) schedule_list->data;
    73 
    74         gtk_tree_store_insert(recordui->sch_tree_store, &iter, NULL,
    75                               new_row++);
    76 
    77         start_date_time =
    78             gmyth_util_time_to_string_from_time_val(schedule_info->
    79                                                     start_time);
    80         end_date_time =
    81             gmyth_util_time_to_string_from_time_val(schedule_info->
    82                                                     end_time);
    83 
    84         g_string_printf(str_aux, "%d", schedule_info->channel_id);
    85 
    86         gtk_tree_store_set(recordui->sch_tree_store, &iter, START_DATE_COLUMN, start_date_time, TITLE_COLUMN, schedule_info->title->str, CHAN_ID_COLUMN, str_aux->str, END_TIME_COLUMN, end_date_time,  // It 
    87                                                                                                                                                                                                         // doesn't 
    88                                                                                                                                                                                                         // appear
    89                            RECORD_ID_COLUMN, schedule_info->record_id, -1); // the 
    90                                                                             // last 
    91                                                                             // line 
    92                                                                             // is 
    93                                                                             // a 
    94                                                                             // hidden 
    95                                                                             // item 
    96                                                                             // to 
    97                                                                             // be 
    98                                                                             // used 
    99                                                                             // in 
   100                                                                             // searching 
   101                                                                             // tasks
   102     }
   103 
   104     g_debug("[%s] %d lines added to schedule list UI", __FUNCTION__,
   105             new_row);
   106 
   107     /*
   108      * free allocated memory 
   109      */
   110     if (!start_date_time)
   111         g_free(start_date_time);
   112     if (!end_date_time)
   113         g_free(end_date_time);
   114     g_string_free(str_aux, FALSE);
   115 
   116     return TRUE;
   117 }
   118 
   119 gboolean
   120 mmyth_recordui_reload_record(MMythRecordUI * recordui)
   121 {
   122     gint            new_row = 0;
   123     RecordedInfo   *recorded_info;
   124     GList          *record_list = NULL;
   125     GtkTreeIter     iter;
   126     gchar          *start_date_time = NULL;
   127     gchar          *end_date_time = NULL;
   128     GString        *str_aux = g_string_new("");
   129     gint            res;
   130 
   131     gtk_tree_store_clear(recordui->rec_tree_store);
   132 
   133     res =
   134         gmyth_scheduler_get_recorded_list(recordui->scheduler,
   135                                           &record_list);
   136     if (res < 0) {
   137         g_warning
   138             ("[%s] Retrieved NULL list of recorded data from database",
   139              __FUNCTION__);
   140         return FALSE;
   141     }
   142 
   143     for (; record_list; record_list = record_list->next) {
   144         recorded_info = (RecordedInfo *) record_list->data;
   145 
   146         gtk_tree_store_insert(recordui->rec_tree_store, &iter, NULL,
   147                               new_row++);
   148 
   149         start_date_time =
   150             gmyth_util_time_to_string_from_time_val(recorded_info->
   151                                                     start_time);
   152         end_date_time =
   153             gmyth_util_time_to_string_from_time_val(recorded_info->
   154                                                     end_time);
   155 
   156         g_string_printf(str_aux, "%d", recorded_info->channel_id);
   157 
   158         gtk_tree_store_set(recordui->rec_tree_store, &iter, START_DATE_COLUMN, start_date_time, TITLE_COLUMN, recorded_info->title->str, CHAN_ID_COLUMN, str_aux->str, END_TIME_COLUMN, end_date_time,  // It 
   159                                                                                                                                                                                                         // doesn't 
   160                                                                                                                                                                                                         // appear
   161                            RECORD_ID_COLUMN, recorded_info->record_id,
   162                            BASENAME_COLUMN, recorded_info->basename->str,
   163                            -1);
   164         // the last line is a hidden item to be used in searching tasks 
   165     }
   166 
   167     g_debug("[%s] %d lines added to record list UI", __FUNCTION__,
   168             new_row);
   169 
   170     /*
   171      * free allocated memory 
   172      */
   173     if (NULL != start_date_time)
   174         g_free(start_date_time);
   175     if (NULL != end_date_time)
   176         g_free(end_date_time);
   177     g_string_free(str_aux, FALSE);
   178 
   179     return TRUE;
   180 }
   181 
   182 
   183 MMythRecordUI  *
   184 mmyth_recordui_new(GMythBackendInfo * backend_info)
   185 {
   186     MMythRecordUI  *recordui = g_new0(MMythRecordUI, 1);
   187 
   188     g_return_val_if_fail(backend_info != NULL, NULL);
   189 
   190     recordui->backend_info = backend_info;
   191 
   192     recordui->scrolled_window = gtk_scrolled_window_new(NULL, NULL);
   193     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
   194                                    (recordui->scrolled_window),
   195                                    GTK_POLICY_AUTOMATIC,
   196                                    GTK_POLICY_AUTOMATIC);
   197 
   198     recordui->viewport = gtk_viewport_new(NULL, NULL);
   199     gtk_container_add(GTK_CONTAINER(recordui->scrolled_window),
   200                       recordui->viewport);
   201 
   202     recordui->notebook = gtk_notebook_new();
   203     gtk_container_set_border_width(GTK_CONTAINER(recordui->notebook), 1);
   204     gtk_notebook_set_scrollable(GTK_NOTEBOOK(recordui->notebook), TRUE);
   205     gtk_notebook_popup_enable(GTK_NOTEBOOK(recordui->notebook));
   206     gtk_container_add(GTK_CONTAINER(recordui->viewport),
   207                       recordui->notebook);
   208     gtk_notebook_popup_disable(GTK_NOTEBOOK(recordui->notebook));
   209 
   210     /*
   211      * Schedule tab 
   212      */
   213     recordui->sch_scrolled_window = gtk_scrolled_window_new(NULL, NULL);
   214     gtk_container_add(GTK_CONTAINER(recordui->notebook),
   215                       recordui->sch_scrolled_window);
   216     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
   217                                    (recordui->sch_scrolled_window),
   218                                    GTK_POLICY_AUTOMATIC,
   219                                    GTK_POLICY_AUTOMATIC);
   220     gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW
   221                                         (recordui->sch_scrolled_window),
   222                                         GTK_SHADOW_IN);
   223 
   224     /*
   225      * The basename column in the sched_tree_store is not being used
   226      */
   227     recordui->sch_tree_store =
   228         gtk_tree_store_new(N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING,
   229                            G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT,
   230                            G_TYPE_STRING);
   231 
   232     recordui->sch_treeview =
   233         gtk_tree_view_new_with_model(GTK_TREE_MODEL
   234                                      (recordui->sch_tree_store));
   235     gtk_container_add(GTK_CONTAINER(recordui->sch_scrolled_window),
   236                       recordui->sch_treeview);
   237     recordui->sch_renderer = gtk_cell_renderer_text_new();
   238     // g_object_set(G_OBJECT(renderer1), "foreground", "green",
   239     // "background", "black", NULL);
   240     recordui->sch_column1 =
   241         gtk_tree_view_column_new_with_attributes("Start time",
   242                                                  recordui->sch_renderer,
   243                                                  "text", START_DATE_COLUMN,
   244                                                  NULL);
   245     gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->sch_treeview),
   246                                 recordui->sch_column1);
   247     recordui->sch_column2 =
   248         gtk_tree_view_column_new_with_attributes("Title",
   249                                                  recordui->sch_renderer,
   250                                                  "text", TITLE_COLUMN,
   251                                                  NULL);
   252     gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->sch_treeview),
   253                                 recordui->sch_column2);
   254     recordui->sch_column3 =
   255         gtk_tree_view_column_new_with_attributes("Channel",
   256                                                  recordui->sch_renderer,
   257                                                  "text", CHAN_ID_COLUMN,
   258                                                  NULL);
   259     gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->sch_treeview),
   260                                 recordui->sch_column3);
   261     gtk_tree_view_column_set_resizable(recordui->sch_column1, TRUE);
   262     gtk_tree_view_column_set_resizable(recordui->sch_column2, TRUE);
   263     gtk_tree_view_column_set_resizable(recordui->sch_column3, TRUE);
   264     gtk_tree_view_column_set_reorderable(recordui->sch_column1, TRUE);
   265     gtk_tree_view_column_set_reorderable(recordui->sch_column2, TRUE);
   266     gtk_tree_view_column_set_reorderable(recordui->sch_column3, TRUE);
   267     // recordui->sch_column4 = 
   268     // gtk_tree_view_column_new_with_attributes("",
   269     // recordui->sch_renderer, "text", END_TIME_COLUMN, NULL);
   270     // gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->sch_treeview), 
   271     // recordui->sch_column4);
   272 
   273     recordui->sch_label = gtk_label_new(("Schedule"));
   274     gtk_notebook_set_tab_label(GTK_NOTEBOOK(recordui->notebook),
   275                                gtk_notebook_get_nth_page(GTK_NOTEBOOK
   276                                                          (recordui->
   277                                                           notebook), 0),
   278                                recordui->sch_label);
   279 
   280     // Record items tab
   281     // g_object_set(G_OBJECT(renderer2), "foreground", "blue", NULL);
   282     recordui->rec_scrolled_window = gtk_scrolled_window_new(NULL, NULL);
   283     gtk_container_add(GTK_CONTAINER(recordui->notebook),
   284                       recordui->rec_scrolled_window);
   285     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW
   286                                    (recordui->rec_scrolled_window),
   287                                    GTK_POLICY_AUTOMATIC,
   288                                    GTK_POLICY_AUTOMATIC);
   289     gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW
   290                                         (recordui->rec_scrolled_window),
   291                                         GTK_SHADOW_IN);
   292 
   293     recordui->rec_tree_store =
   294         gtk_tree_store_new(N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING,
   295                            G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT,
   296                            G_TYPE_STRING);
   297     recordui->rec_treeview =
   298         gtk_tree_view_new_with_model(GTK_TREE_MODEL
   299                                      (recordui->rec_tree_store));
   300     gtk_container_add(GTK_CONTAINER(recordui->rec_scrolled_window),
   301                       recordui->rec_treeview);
   302     recordui->rec_renderer = gtk_cell_renderer_text_new();
   303     // g_object_set(G_OBJECT(renderer1), "foreground", "green",
   304     // "background", "black", NULL);
   305 
   306     recordui->rec_column1 =
   307         gtk_tree_view_column_new_with_attributes("Start time",
   308                                                  recordui->rec_renderer,
   309                                                  "text", START_DATE_COLUMN,
   310                                                  NULL);
   311     gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->rec_treeview),
   312                                 recordui->rec_column1);
   313     recordui->rec_column2 =
   314         gtk_tree_view_column_new_with_attributes("Title",
   315                                                  recordui->rec_renderer,
   316                                                  "text", TITLE_COLUMN,
   317                                                  NULL);
   318     gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->rec_treeview),
   319                                 recordui->rec_column2);
   320     recordui->rec_column3 =
   321         gtk_tree_view_column_new_with_attributes("Channel",
   322                                                  recordui->rec_renderer,
   323                                                  "text", CHAN_ID_COLUMN,
   324                                                  NULL);
   325     gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->rec_treeview),
   326                                 recordui->rec_column3);
   327     gtk_tree_view_column_set_resizable(recordui->rec_column1, TRUE);
   328     gtk_tree_view_column_set_resizable(recordui->rec_column2, TRUE);
   329     gtk_tree_view_column_set_resizable(recordui->rec_column3, TRUE);
   330     gtk_tree_view_column_set_reorderable(recordui->rec_column1, TRUE);
   331     gtk_tree_view_column_set_reorderable(recordui->rec_column2, TRUE);
   332     gtk_tree_view_column_set_reorderable(recordui->rec_column3, TRUE);
   333     // recordui->rec_column4 =
   334     // gtk_tree_view_column_new_with_attributes("",
   335     // recordui->rec_renderer, "text", END_TIME_COLUMN, NULL);
   336     // gtk_tree_view_append_column(GTK_TREE_VIEW(recordui->rec_treeview),
   337     // recordui->rec_column4);
   338 
   339     recordui->rec_label = gtk_label_new(("Recorded"));
   340     gtk_notebook_set_tab_label(GTK_NOTEBOOK(recordui->notebook),
   341                                gtk_notebook_get_nth_page(GTK_NOTEBOOK
   342                                                          (recordui->
   343                                                           notebook), 1),
   344                                recordui->rec_label);
   345 
   346     // Gets the mmyth scheduler manager
   347     recordui->scheduler = gmyth_scheduler_new(backend_info);
   348 
   349     /*
   350      * init connection to the backend 
   351      */
   352     gmyth_scheduler_connect(recordui->scheduler,
   353                             recordui->scheduler->backend_info);
   354 
   355     return recordui;
   356 }
   357 
   358 void
   359 mmyth_recordui_free(MMythRecordUI * recordui)
   360 {
   361     // FIXME: Release memory here!
   362     /*
   363      * close connection to the backend 
   364      */
   365     gmyth_scheduler_disconnect(recordui->scheduler);
   366 }
   367 
   368 void
   369 mmyth_recordui_delete_selected(GtkButton * button,
   370                                MMythRecordUI * recordui)
   371 {
   372     GtkTreeSelection *selection;
   373     GtkTreeModel   *list_store;
   374     GtkTreeIter     iter;
   375     int             index;
   376     int             curr_page = 0;
   377 
   378     curr_page =
   379         gtk_notebook_get_current_page(GTK_NOTEBOOK(recordui->notebook));
   380 
   381     if (curr_page == 0) {
   382         selection =
   383             gtk_tree_view_get_selection(GTK_TREE_VIEW
   384                                         (recordui->sch_treeview));
   385         if (selection != NULL) {
   386             gtk_tree_selection_get_selected(selection, &list_store, &iter);
   387             gtk_tree_model_get(list_store, &iter, RECORD_ID_COLUMN, &index,
   388                                -1);
   389             gmyth_scheduler_delete_schedule(recordui->scheduler, index);
   390             mmyth_recordui_reload_schedule(recordui);
   391             return;
   392         }
   393 
   394     } else if (curr_page == 1) {
   395         selection =
   396             gtk_tree_view_get_selection(GTK_TREE_VIEW
   397                                         (recordui->rec_treeview));
   398         if (selection != NULL) {
   399             gtk_tree_selection_get_selected(selection, &list_store, &iter);
   400             gtk_tree_model_get(list_store, &iter, RECORD_ID_COLUMN, &index,
   401                                -1);
   402             gmyth_scheduler_delete_recorded(recordui->scheduler, index);
   403             mmyth_recordui_reload_record(recordui);
   404             return;
   405         }
   406     }
   407 
   408     g_warning("[%s] None element was removed from the list", __FUNCTION__);
   409 }
   410 
   411 /*
   412  * FIXME: change this function name, it is returning the basename_column
   413  * that represents the nuv filename of the recorded content 
   414  */
   415 gchar          *
   416 mmyth_recordui_get_selected_recorded(MMythRecordUI * recordui)
   417 {
   418     GtkTreeSelection *selection = NULL;
   419     GtkTreeModel   *list_store = NULL;
   420     GtkTreeIter     iter;
   421     gchar          *path = NULL;
   422 
   423     /*
   424      * returning nuv filename, basename_column 
   425      */
   426     selection =
   427         gtk_tree_view_get_selection(GTK_TREE_VIEW(recordui->rec_treeview));
   428     if (gtk_tree_selection_get_selected(selection, &list_store, &iter)) {
   429         gtk_tree_model_get(list_store, &iter, BASENAME_COLUMN, &path, -1);
   430     }
   431     // FIXME: MOVE THIS TO OTHER PLACE
   432     return path;
   433 }