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