# HG changeset patch
# User rosfran
# Date 1170367818 0
# Node ID 1c020e9ef06d2453670cd922f111a10e1c55d286
# Parent  22a17127789ea88195c42ed85f7d17f87e023de7
[svn r323] Some updates in the LiveTV.

diff -r 22a17127789e -r 1c020e9ef06d gmyth/src/gmyth_file_transfer.c
--- a/gmyth/src/gmyth_file_transfer.c	Thu Feb 01 18:42:01 2007 +0000
+++ b/gmyth/src/gmyth_file_transfer.c	Thu Feb 01 22:10:18 2007 +0000
@@ -301,6 +301,7 @@
 
   transfer->sock = gmyth_socket_new ();
   gmyth_socket_connect (transfer->sock, transfer->backend_info->hostname, transfer->backend_info->port);
+  gmyth_debug("Connecting file transfer... (%s, %d)", transfer->backend_info->hostname, transfer->backend_info->port);
 
   strlist = gmyth_string_list_new();
   hostname = gmyth_socket_get_local_hostname();
diff -r 22a17127789e -r 1c020e9ef06d gmyth/src/gmyth_livetv.c
--- a/gmyth/src/gmyth_livetv.c	Thu Feb 01 18:42:01 2007 +0000
+++ b/gmyth/src/gmyth_livetv.c	Thu Feb 01 22:10:18 2007 +0000
@@ -381,7 +381,28 @@
 	{
 		gmyth_debug( "ProgramInfo is equals to NULL!!!" );
 		
-		return FALSE;
+    gint i;
+    gchar *channame = NULL;
+
+    fprintf(stderr, "problem getting current proginfo!\n");
+
+    /*
+     * mythbackend must not be tuned in to a channel, so keep
+     * changing channels until we find a valid one, or until
+     * we decide to give up.
+     */
+    for (i=1; i<1000; i++) {
+			if ( channame != NULL )
+				g_free(channame);
+      channame = g_strdup_printf( "%d", i );
+      if (gmyth_recorder_set_channel_name(livetv->recorder, channame) < 0) {
+      	continue;
+      }
+      prog_info = gmyth_recorder_get_next_program_info(livetv->recorder, BROWSE_DIRECTION_UP);
+      if (prog_info != NULL)
+      	break;
+    }
+		
 	}
 	/* prints program info data text */ 
 	gmyth_debug( "New ProgramInfo...\n" );
@@ -390,6 +411,11 @@
 	gmyth_debug( "Old ProgramInfo...\n" );
 	gmyth_program_info_print( livetv->proginfo );
 	
+	GMythProgramInfo* prog_inf = gmyth_recorder_get_next_program_info( livetv->recorder, BROWSE_DIRECTION_UP );
+	
+	gmyth_debug( "Next ProgramInfo...\n" );
+	gmyth_program_info_print( prog_inf );
+	
 	/* check if the program chain could be obtained from the MythTV protocol message */
 	if ( prog_info != NULL )
 	{
@@ -507,9 +533,9 @@
 		goto error;		
 	}
 	
-	//if ( !gmyth_livetv_monitor_handler_start( livetv ) )
-	//	goto error;
-	prog_info = gmyth_recorder_get_current_program_info( livetv->recorder );
+	prog_info = gmyth_recorder_get_next_program_info( livetv->recorder, BROWSE_DIRECTION_RIGHT );
+	
+	gmyth_debug( "Next ProgramInfo...\n" );
 	
 	if ( NULL == prog_info )
 	{
@@ -576,9 +602,9 @@
 	}
 	
 	if ( livetv->proginfo != NULL )
-  	gmyth_debug( "URI path = %s.\n", livetv->proginfo->pathname->str );
+  	gmyth_debug( "URI path (from program info) = %s.\n", livetv->proginfo->pathname->str );
   else
-  	gmyth_debug( "URI path = %s.\n", livetv->uri->uri->str ); 
+  	gmyth_debug( "URI path (from URI) = %s.\n", livetv->uri->uri->str ); 
   
   g_static_mutex_lock( &lock );
   
@@ -589,23 +615,20 @@
   	livetv->file_transfer = NULL;
   }  	
 
-	livetv->file_transfer = gmyth_file_transfer_new( livetv->backend_info );
-
-  if ( NULL == livetv->file_transfer ) 
+  if ( livetv->uri != NULL )  
   {
-  	gmyth_debug( "Error: couldn't create the FileTransfer from LiveTV source!" );
-    goto done;
-  }
-  
-  if ( livetv->uri != NULL )  
-  { 
+  	/* 
   	if ( livetv->uri->path != NULL )
   	{
   		g_string_free( livetv->uri->path, FALSE );
   		livetv->uri->path = NULL;
   	}
+  	*/
+  	gmyth_debug( "URI is NULL, creating from the ProgramInfo pathname... (%s)", livetv->proginfo->pathname->str );
+  	livetv->uri->path = g_string_erase(livetv->uri->path, 0, -1);
   	livetv->uri->path = g_string_new( g_strrstr( livetv->proginfo->pathname->str, "/" ) );
   } else {
+  	gmyth_debug( "URI is NULL, creating from the ProgramInfo pathname... (%s)", livetv->proginfo->pathname->str );
   	livetv->uri = gmyth_uri_new_with_value( livetv->proginfo->pathname->str );
   }
   	
@@ -614,6 +637,14 @@
   	gmyth_debug( "Couldn't parse the URI to start LiveTV! [ uri = %s ]", livetv->proginfo->pathname->str );
   	goto done;  	
   }
+  
+	livetv->file_transfer = gmyth_file_transfer_new( livetv->backend_info );
+
+  if ( NULL == livetv->file_transfer ) 
+  {
+  	gmyth_debug( "Error: couldn't create the FileTransfer from LiveTV source!" );
+    goto done;
+  }
 
 	if ( !gmyth_file_transfer_open( livetv->file_transfer, livetv->uri != NULL ? gmyth_uri_get_path(livetv->uri) : 
 				livetv->proginfo->pathname->str ) )
diff -r 22a17127789e -r 1c020e9ef06d gmyth/src/gmyth_programinfo.c
--- a/gmyth/src/gmyth_programinfo.c	Thu Feb 01 18:42:01 2007 +0000
+++ b/gmyth/src/gmyth_programinfo.c	Thu Feb 01 22:10:18 2007 +0000
@@ -309,10 +309,11 @@
     gmyth_string_list_append_string (slist, prog->chansign); /* 6 */
     gmyth_string_list_append_string (slist, prog->channame); /* 7 */
     gmyth_string_list_append_string (slist, prog->pathname); /* 8 */
+    gmyth_string_list_append_int (slist, 0); /* 11 */
 
     // fixme
-    gmyth_string_list_append_int64 (slist, 100/*prog->filesize*/); /* 9 */
-    gmyth_string_list_append_int64 (slist, 10); /* 11 */
+    //gmyth_string_list_append_int64 (slist, 100/*prog->filesize*/); /* 9 */
+    gmyth_string_list_append_int64 (slist, 0); /* 11 */
 
     if (prog->startts)
         gmyth_string_list_append_int (slist, prog->startts->tv_sec);  /* 11 */ //DATETIME_TO_LIST(startts)
@@ -346,7 +347,7 @@
     gmyth_string_list_append_string (slist, prog->chanOutputFilters); /* 32 */
     gmyth_string_list_append_string (slist, prog->seriesid); /* 33 */
     gmyth_string_list_append_string (slist, prog->programid); /* 34 */
-    gmyth_string_list_append_string (slist, ""); /* 35 */
+    gmyth_string_list_append_char_array (slist, ""); /* 35 */
     gmyth_string_list_append_int (slist, prog->lastmodified != NULL ? prog->lastmodified->tv_sec : 0);  /* 36 */ //DATETIME_TO_LIST(lastmodified)
     gmyth_string_list_append_int (slist, 0);  /* 37 */ //FLOAT_TO_LIST(stars)
     gmyth_string_list_append_int (slist, prog->originalAirDate != NULL ? prog->originalAirDate->tv_sec : 0);  /* 38 */ //DATETIME_TO_LIST(QDateTime(originalAirDate))
@@ -410,16 +411,18 @@
     prog->subtitle = gmyth_string_list_get_string (slist, 1);
     prog->description = gmyth_string_list_get_string (slist, 2);
     prog->category = gmyth_string_list_get_string (slist, 3);
+    //gmyth_string_list_get_string (slist, 4);
     prog->chanid = gmyth_string_list_get_string (slist, 4);
-    prog->chanstr = gmyth_string_list_get_string (slist, 5);
-    prog->chansign = gmyth_string_list_get_string (slist, 6);
-    prog->channame = gmyth_string_list_get_string (slist, 7);
-    prog->pathname = gmyth_string_list_get_string (slist, 8);
+    prog->channame = gmyth_string_list_get_string (slist, 5);
+    prog->chanstr = gmyth_string_list_get_string (slist, 6);    
+    prog->chansign = gmyth_string_list_get_string (slist, 7);    
+    prog->pathname = gmyth_string_list_get_string (slist, 8);    
+    //gmyth_string_list_get_int (slist, 9);
+    
     prog->filesize = gmyth_string_list_get_int64 (slist, 9);
-    gmyth_string_list_get_int64 (slist, 10);
 
     prog->startts = gmyth_util_string_to_time_val( (gmyth_util_time_to_isoformat( 
-    					(time_t)gmyth_string_list_get_int (slist, 11) ))->str ); //DATETIME_TO_LIST(startts)
+    					(time_t)gmyth_string_list_get_int64 (slist, 11) ))->str ); //DATETIME_TO_LIST(startts)
     prog->endts = gmyth_util_string_to_time_val( (gmyth_util_time_to_isoformat( 
     					(time_t)gmyth_string_list_get_int (slist, 12) ))->str ); //DATETIME_TO_LIST(endts)
     prog->duplicate = gmyth_string_list_get_int (slist, 13);
diff -r 22a17127789e -r 1c020e9ef06d gmyth/src/gmyth_recorder.c
--- a/gmyth/src/gmyth_recorder.c	Thu Feb 01 18:42:01 2007 +0000
+++ b/gmyth/src/gmyth_recorder.c	Thu Feb 01 22:10:18 2007 +0000
@@ -583,6 +583,171 @@
 
 }
 
+/**
+ * Requests the actual program info from the MythTV backend server.
+ * 
+ * @param rec_id The GMythRecorder record number.
+ * @return The GMythRecorder instance.
+ */
+GMythRecorder *
+gmyth_recorder_get_recorder_from_num ( gint rec_id )
+{
+	GMythRecorder* recorder = NULL;
+	GMythStringList *str_list;
+	GString *tmp_str = g_string_new( "GET_RECORDER_FROM_NUM" );
+	gint command_size = 0;
+	
+	gchar *recorder_host = NULL;
+	gint recorder_port;
+	
+	str_list = gmyth_string_list_new ();
+	
+	/* g_string_append_printf ( tmp_str, " %d", recorder->recorder_num ); */
+	 
+	gmyth_string_list_append_string (str_list, tmp_str);
+	gmyth_string_list_append_int (str_list, rec_id);
+
+	command_size = gmyth_socket_sendreceive_stringlist (recorder->myth_socket, str_list);
+
+  g_string_free (tmp_str, TRUE);
+
+  if (str_list == NULL) {
+  	g_warning ("[%s] GET_RECORDER_FROM_NUM request returned. Error getting recorder number %d, it is equals to NULL!!!", 
+		  			__FUNCTION__, rec_id);
+  	return NULL;
+	}
+	
+	if ( command_size > 0 )
+	{
+		recorder_host = gmyth_string_list_get_char_array( str_list, 0 );
+		recorder_port = gmyth_string_list_get_int( str_list, 1 );
+		
+		if ( g_strstr_len( recorder_host, strlen(recorder_host), "nohost" ) != NULL )
+		{
+			gmyth_debug( "No available recorder with the recorder ID number %d!", rec_id );
+		} else {
+	  
+		  recorder = gmyth_recorder_new( rec_id, g_string_new( recorder_host ), (gshort)recorder_port );
+		
+		  if ( NULL == recorder ) {
+		  	g_warning ("[%s] GET_RECORDER_FROM_NUM request returned. Error getting recorder number %d, it is equals to NULL!!!", 
+		  			__FUNCTION__, rec_id);
+		    g_object_unref (recorder);
+		  	return NULL;
+		  }
+		  
+		}
+	  
+	} else {
+		gmyth_debug( "Cannot find a valuable recorder with the recorder ID number %d, backend server error!", rec_id );
+	}
+
+  g_object_unref (str_list);
+  
+  if ( recorder_host != NULL )
+  	g_free( recorder_host );
+  
+  return recorder;
+
+}
+
+/**
+ * Requests the actual program info from the MythTV backend server.
+ * 
+ * @param recorder The GMythRecorder instance.
+ * @return The GMythProgramInfo next program info instance.
+ */
+GMythProgramInfo *
+gmyth_recorder_get_next_program_info ( GMythRecorder *recorder, const GMythRecorderBrowseDirection direction )
+{
+	GMythProgramInfo* actual_proginfo = NULL;
+	GMythProgramInfo* program_info = NULL;
+	GMythStringList *str_list;
+	GString *tmp_str = g_string_new( GMYTHTV_RECORDER_HEADER );
+	
+	gchar *date = NULL;
+  struct tm *tm;
+  time_t t;
+  
+  actual_proginfo = gmyth_recorder_get_current_program_info(recorder);
+	
+	str_list = gmyth_string_list_new ();
+	
+	g_string_append_printf ( tmp_str, " %d", recorder->recorder_num );
+	 
+  t = time(NULL);
+  tm = localtime(&t);
+  date = g_strdup_printf("%.4d%.2d%.2d%.2d%.2d%.2d",
+           tm->tm_year + 1900, tm->tm_mon + 1,
+           tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
+
+	gmyth_string_list_append_string (str_list, tmp_str);
+	gmyth_string_list_append_char_array (str_list, "GET_NEXT_PROGRAM_INFO");
+	gmyth_string_list_append_string (str_list, actual_proginfo->channame);
+  gmyth_string_list_append_string (str_list, actual_proginfo->chanid);
+  gmyth_string_list_append_int (str_list, direction);
+  gmyth_string_list_append_char_array (str_list, date);
+	
+	if ( gmyth_socket_sendreceive_stringlist (recorder->myth_socket, str_list) > 0 )
+	{
+  
+	  if (str_list == NULL) {
+	  	g_warning ("[%s] GET_NEXT_PROGRAM_INFO request returned. Error getting program info, it is equals to NULL!!!", 
+			  			__FUNCTION__);
+	  	goto done;
+		}
+		program_info = gmyth_program_info_new();
+		
+	  if ( NULL == program_info ) {
+	  	g_warning ("[%s] GET_NEXT_PROGRAM_INFO request returned. Error getting next program info, it is equals to NULL!!!", 
+	  			__FUNCTION__);
+	    g_object_unref (program_info);
+	  	goto done;
+	  }
+	  
+		program_info->title = gmyth_string_list_get_string (str_list, 0);
+		program_info->subtitle = gmyth_string_list_get_string (str_list, 1);
+		program_info->description = gmyth_string_list_get_string (str_list, 2);
+		program_info->category = gmyth_string_list_get_string (str_list, 3);
+		program_info->startts = gmyth_util_string_to_time_val( (gmyth_util_time_to_isoformat( 
+    					(time_t)gmyth_string_list_get_int (str_list, 4) ))->str );
+		program_info->endts = gmyth_util_string_to_time_val( (gmyth_util_time_to_isoformat( 
+    					(time_t)gmyth_string_list_get_int (str_list, 6) ))->str );
+	
+	  program_info->chansign = gmyth_string_list_get_string (str_list, 8);
+	  program_info->pathname = gmyth_string_list_get_string (str_list, 9);
+	  program_info->channame = gmyth_string_list_get_string (str_list, 10);
+	  program_info->chanid = gmyth_string_list_get_string (str_list, 11);
+	  if ( recorder->myth_socket->mythtv_version >= 12 ) {
+	          program_info->seriesid = gmyth_string_list_get_string (str_list, 12);
+	          program_info->programid = gmyth_string_list_get_string (str_list, 13);
+	  }
+	  
+	  if ( ( program_info->chanid != NULL && strlen( program_info->chanid->str ) > 0  ) &&
+	  	( program_info->channame != NULL && strlen( program_info->channame->str ) > 0 ) &&
+	  	( program_info->title != NULL && strlen( program_info->title->str ) > 0 ) ) 
+	  {
+	  		gmyth_debug( "OK!!! Got the next program info..." );
+	  } else {
+	  	gmyth_debug ("GET_NEXT_PROGRAM_INFO request returned. Error getting next program info, it is equals to NULL!!!" );
+	    g_object_unref (program_info);
+	    program_info = NULL;
+	  }
+	  
+	} /* if */
+
+done:
+  g_string_free (tmp_str, TRUE);
+
+  g_object_unref (str_list);
+  
+  if ( date != NULL )
+  	g_free( date );
+  
+  return program_info;
+
+}
+
 gint64
 gmyth_recorder_get_file_position ( GMythRecorder *recorder )
 {
diff -r 22a17127789e -r 1c020e9ef06d gmyth/src/gmyth_recorder.h
--- a/gmyth/src/gmyth_recorder.h	Thu Feb 01 18:42:01 2007 +0000
+++ b/gmyth/src/gmyth_recorder.h	Thu Feb 01 22:10:18 2007 +0000
@@ -80,7 +80,16 @@
 	CHANNEL_DIRECTION_SAME
 } GMythRecorderChannelChangeDirection;
 
-GType   gmyth_recorder_get_type   (void);
+typedef enum _GMythRecorderBrowseDirection {
+	BROWSE_DIRECTION_SAME = 0,     	  /* Stay in the same place */
+ 	BROWSE_DIRECTION_UP,          		/* Move up one slot (down one channel) */
+	BROWSE_DIRECTION_DOWN,        		/* Move down one slot (up one channel) */
+	BROWSE_DIRECTION_LEFT,        		/* Move left one slot (down one time slot) */
+	BROWSE_DIRECTION_RIGHT,       		/* Move right one slot (up one time slot) */
+	BROWSE_DIRECTION_FAVORITE    			/* Move to the next favorite slot */
+} GMythRecorderBrowseDirection;
+
+GType   gmyth_recorder_get_type   	(void);
 
 GMythRecorder* gmyth_recorder_new    (int num,
 	                                     GString *hostname,
@@ -112,7 +121,12 @@
 gboolean gmyth_recorder_pause_recording ( GMythRecorder *recorder );
 
 GMythProgramInfo *gmyth_recorder_get_current_program_info ( GMythRecorder *recorder );
-                                         
+
+GMythProgramInfo *gmyth_recorder_get_next_program_info ( GMythRecorder *recorder,
+												const GMythRecorderBrowseDirection direction);
+												
+GMythRecorder *gmyth_recorder_get_recorder_from_num ( gint rec_id );
+
 gint64 	 gmyth_recorder_get_file_position ( GMythRecorder *recorder );
 
 gboolean gmyth_recorder_is_recording ( GMythRecorder *recorder );
diff -r 22a17127789e -r 1c020e9ef06d gmyth/src/gmyth_tvchain.h
--- a/gmyth/src/gmyth_tvchain.h	Thu Feb 01 18:42:01 2007 +0000
+++ b/gmyth/src/gmyth_tvchain.h	Thu Feb 01 22:10:18 2007 +0000
@@ -74,7 +74,7 @@
 
 struct _GMythTVChain
 {
-    GObject parent;
+  GObject parent;
 
 	GString *tvchain_id;
 	GList   *tvchain_list;
@@ -83,7 +83,7 @@
 	GString *cur_chanid;
 	gint     cur_pos;
 
-    GMythBackendInfo *backend_info;
+  GMythBackendInfo *backend_info;
 };