# HG changeset patch
# User rosfran
# Date 1170875748 0
# Node ID be533d944ceb9ac776543cb34e7a05ade1db9d6f
# Parent  f32837103d171723840560cac6ec54d7918461cc
[svn r334] Put all the gmyth recorder and tvchain inseide get_file_info; removed reference to LiveTV.

diff -r f32837103d17 -r be533d944ceb libgnomevfs2-mythtv/modules/mythtv-method.c
--- a/libgnomevfs2-mythtv/modules/mythtv-method.c	Wed Feb 07 18:30:15 2007 +0000
+++ b/libgnomevfs2-mythtv/modules/mythtv-method.c	Wed Feb 07 19:15:48 2007 +0000
@@ -33,6 +33,8 @@
 #include <gmyth/gmyth_recorder.h>
 #include <gmyth/gmyth_backendinfo.h>
 #include <gmyth/gmyth_util.h>
+#include <gmyth/gmyth_remote_util.h>
+#include <gmyth/gmyth_tvchain.h>
 
 #define GST_MYTHTV_ID_NUM               1
 #define MYTHTV_VERSION_DEFAULT          30
@@ -285,9 +287,11 @@
                   GnomeVFSContext *context)
 {
     GMythFileTransfer *file_transfer = NULL;
-    GMythLiveTV 			*livetv 			 = NULL;
+    GMythRecorder			*recorder 		 = NULL;
+    GMythTVChain			*tvchain	 		 = NULL;
     GMythBackendInfo  *backend_info  = NULL;
     GMythURI					*gmyth_uri		 = NULL;
+		GMythSocket 			*socket 			 = NULL;		 
     gboolean 					is_livetv 		 = FALSE;
     gboolean					ret						 = TRUE;
     
@@ -313,42 +317,142 @@
         GNOME_VFS_PERM_OTHER_READ |
         GNOME_VFS_PERM_GROUP_READ;
         
-    g_print( "gnome_vfs_uri == %s | gmyth_uri == %s.",
+    g_print( "gnome_vfs_uri == %s | gmyth_uri == %s.\n",
     				gnome_vfs_uri_get_path (uri),
     				gmyth_uri_get_path (gmyth_uri) );
 
 	  /* Connect to the backend */
 	  if ( gmyth_uri != NULL && is_livetv == TRUE ) {
-	    livetv = gmyth_livetv_new ();	    
-	        
-	    gchar* channel_name = gmyth_uri_get_channel_name( gmyth_uri );
-	
-	    if ( channel_name != NULL ) {
-	      if (gmyth_livetv_channel_name_setup (livetv, channel_name,
-	              backend_info) == FALSE) {
-	        g_object_unref( gmyth_uri );
-	        ret = FALSE;
-	      }
-	    } else {
-	      if ( gmyth_livetv_setup (livetv, backend_info) == FALSE ) {
-	      	g_object_unref( gmyth_uri );
-	        ret = FALSE;
-	      }
-	    }
-	    
-	    g_print( "path = %s",  gmyth_uri_get_path (livetv->uri) );
-	
-	    file_transfer = gmyth_livetv_create_file_transfer (livetv);	    
-	
-	    if (NULL == file_transfer) {
-	      ret = FALSE;
-	    }
-	    g_print( "path = %s",  gmyth_uri_get_path (livetv->uri) );
-	    
-	    file_info->name = g_strdup ( gmyth_uri_get_path (livetv->uri) );
+	  	
+	  	gboolean res = TRUE;
+	  	
+	  	/* start to get file info from LiveTV remote encoder */
+	  	
+			socket = gmyth_socket_new ();
+			
+			/* FIME: Implement this at gmyth_socket */
+			res = gmyth_socket_connect_to_backend (socket, backend_info->hostname,
+					backend_info->port, TRUE);
+			if (!res) {
+				g_warning ("[%s] LiveTV can not connect to backend", __FUNCTION__);	
+				res = FALSE;
+				goto error;
+			}
+
+			if ( gmyth_remote_util_get_free_recorder_count (socket) <= 0 ) {
+				g_warning ("No free remote encoder available.");
+				res = FALSE;
+				goto error;
+			}
+		
+			/* Gets the recorder num */
+			recorder = remote_request_next_free_recorder (socket, -1);
+			gmyth_socket_close_connection (socket);
+		
+			if ( recorder == NULL ) {
+				g_warning ("[%s] None remote encoder available", __FUNCTION__);
+				res = FALSE;
+				goto error;
+			}
+			
+			/* Init remote encoder. Opens its control socket. */
+			res = gmyth_recorder_setup(recorder);
+			if ( !res ) {
+				g_warning ("[%s] Fail while setting remote encoder\n", __FUNCTION__);
+				res = FALSE;
+				goto error;
+			}
+			
+			/* Creates livetv chain handler */
+			tvchain = gmyth_tvchain_new();
+			gmyth_tvchain_initialize ( tvchain, backend_info );
+		
+			if ( tvchain == NULL || tvchain->tvchain_id == NULL ) {
+				res = FALSE;
+				goto error;
+			}
+			
+			// Spawn live tv. Uses the socket to send mythprotocol data to start livetv in the backend (remotelly)
+			res = gmyth_recorder_spawntv ( recorder,
+					gmyth_tvchain_get_id(tvchain) );
+			if (!res) {
+				g_warning ("[%s] Fail while spawn tv\n", __FUNCTION__);
+				res = FALSE;
+				goto error;
+			}
+			
+		  gchar* channel_name = gmyth_uri_get_channel_name( gmyth_uri );
+		    
+		  if ( res == TRUE ) {
+		    /* loop finished, set the max tries variable to zero again... */
+		    gint wait_to_transfer = 0;
+		
+		    while (wait_to_transfer++ < MYTHTV_TRANSFER_MAX_WAITS &&
+		        (gmyth_recorder_is_recording (recorder) == FALSE))
+		      g_usleep (500);
+		
+		    /* IS_RECORDING again, just like the MythTV backend does... */
+		    gmyth_recorder_is_recording (recorder);
+		    
+				if ( channel_name != NULL ) 
+				{
+					/* Pauses remote encoder. */
+					res = gmyth_recorder_pause_recording(recorder);
+					if ( !res ) {
+						g_warning ("[%s] Fail while pausing remote encoder\n", __FUNCTION__);
+						res = FALSE;
+						goto error;
+					}
+			
+			  	if ( gmyth_recorder_check_channel_name( recorder, channel_name ) )
+			  	{
+				  	if ( gmyth_recorder_set_channel_name( recorder, channel_name ) )
+				  	{
+				  		g_warning( "Channel changed!!! [%s].\n", channel_name );
+				  	}
+			  	}
+			
+				} /* if - changes the channel number */
+				
+				sleep( 5 );
+				
+		  }
+		  
+		  /* DEBUG message */  
+			GMythProgramInfo* prog_info = gmyth_recorder_get_current_program_info( recorder );
+			
+			if ( prog_info != NULL )
+			{
+  
+		    g_print( "path = %s",  prog_info->pathname->str );
+		    
+		    file_info->name = g_strdup ( g_strrstr( prog_info->pathname->str, "/" ) );
+		    
+	  		gint64 size = 0;
+	  		size = gmyth_recorder_get_file_position( recorder );
+	  		if( size < 0 )
+	  		{
+	  			//GMythProgramInfo *prog_info = gmyth_recorder_get_current_program_info ( recorder );
+	  			file_info->size = prog_info->filesize;
+	  		}
+				else
+					file_info->size = size; 
+		    
+			} else {
+				file_info->size = gmyth_recorder_get_file_position( recorder );
+			}
+
+	    if ( tvchain != NULL)
+	    	g_object_unref (tvchain);
+				
+	    if ( recorder != NULL )
+	    	g_object_unref (recorder);
+	    	
+	    g_object_unref( prog_info );
 
 	  } else {
-	
+	  	
+	  	/* start to get file info from remote file encoder */	
 	    file_transfer = gmyth_file_transfer_new (backend_info);
 	    
 	    /* Verifies if the file exists */
@@ -360,7 +464,8 @@
 	    /* sets the Playback monitor connection */
 	    ret = gmyth_file_transfer_open ( file_transfer, gmyth_uri_get_path (gmyth_uri) );
 	    
-	    file_info->name = g_strdup ( gnome_vfs_uri_get_path (uri) );	    
+	    file_info->name = g_strdup ( gnome_vfs_uri_get_path (uri) );
+	        
 		
 	  } /* if - LiveTV or not? */
 	  
@@ -370,38 +475,7 @@
 	    return GNOME_VFS_ERROR_NOT_OPEN;
 	  }
 	  */
-	  if ( is_livetv )
-	  {
-	  	gint64 size = 0;
-	  	if ( file_transfer->filesize < 0 )
-	  	{
-	  		size = gmyth_recorder_get_file_position( livetv->recorder );
-	  		if( size < 0 ) {	  			
-	  			GMythProgramInfo *prog_info = gmyth_recorder_get_current_program_info ( livetv->recorder );
-	  			size = prog_info->filesize;
-	  			
-	  			if ( prog_info )
-	  				g_object_unref( prog_info );	  								
-				}
-	  	}
-			else if ( file_transfer != NULL )
-				size = gmyth_file_transfer_get_filesize (file_transfer);
-						
-			if ( size < 0 )
-				file_info->size =(GnomeVFSFileSize) - 1;
-			else
-				file_info->size = size;
-
-	    /*if ( file_transfer != NULL)
-	    	g_object_unref (file_transfer);*/
-				
-	    if ( livetv != NULL )
-	    	g_object_unref (livetv);
-	    	        
-	    /*if ( file_transfer != NULL)
-	    	g_object_unref (file_transfer);*/
-
-	  } else if ( ret == TRUE  && file_transfer != NULL ) {
+		if ( ret == TRUE  && file_transfer != NULL ) {
     	file_info->size = gmyth_file_transfer_get_filesize (file_transfer);
 	    if ( file_transfer )
 	    	g_object_unref (file_transfer);
@@ -409,7 +483,8 @@
  	
     file_info->block_count = GNOME_VFS_FILE_INFO_FIELDS_BLOCK_COUNT;
 	  file_info->io_block_size = GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE;
-    
+	  
+error:
     if (backend_info)
    		g_object_unref (backend_info);