# HG changeset patch
# User leo_sobral
# Date 1174660490 0
# Node ID f310d1d086b5b2163e9d2a033f90e600da97ff08
# Parent  1a9af071ab1f80dd61f42ee4e7858ef7363f994a
[svn r446] Object ref fixed

diff -r 1a9af071ab1f -r f310d1d086b5 libgnomevfs2-mythtv/modules/mythtv-method.c
--- a/libgnomevfs2-mythtv/modules/mythtv-method.c	Fri Mar 23 13:56:18 2007 +0000
+++ b/libgnomevfs2-mythtv/modules/mythtv-method.c	Fri Mar 23 14:34:50 2007 +0000
@@ -22,6 +22,8 @@
 
 #include <string.h>
 #include <glib.h>
+#include <glib/gprintf.h>
+#include <glib/gstdio.h>
 #include <math.h>
 
 #include <libgnomevfs/gnome-vfs-module.h>
@@ -42,11 +44,11 @@
 #define MYTHTV_TRANSFER_MAX_WAITS       100
 
 /* internal GnomeVFS plug-in buffer size ( 120 Kbytes ) */
-#define MYTHTV_BUFFER_SIZE							1024*80
+#define MYTHTV_BUFFER_SIZE                          80*1024
 /* internally sized GnomeVFS plug-in buffer ( 4 Kbytes ) */
-#define MYTHTV_MAX_VFS_BUFFER_SIZE			4096
+#define MYTHTV_MAX_VFS_BUFFER_SIZE                  4096
 /* maximum number of bytes to be requested to the MythTV backend ( 64 Kbytes ) */
-#define MYTHTV_MAX_REQUEST_SIZE					1024*64
+#define MYTHTV_MAX_REQUEST_SIZE                     64*1024
 
 static GnomeVFSResult do_read (GnomeVFSMethod * method,
 			       GnomeVFSMethodHandle * method_handle,
@@ -73,6 +75,10 @@
 } MythtvHandle;
 
 //static MythtvHandle *myth_handle = NULL;
+#ifdef DEBUG
+static FILE *fpout = NULL;
+static gboolean first = TRUE;
+#endif
 
 static GnomeVFSResult
 do_open (GnomeVFSMethod * method,
@@ -96,6 +102,7 @@
 		return GNOME_VFS_ERROR_INVALID_HOST_NAME;
 	}
 
+    // FIXME: myth_handle is always NULL here
 	if ((NULL == myth_handle) || !myth_handle->configured) {
 		myth_handle = g_new0 (MythtvHandle, 1);
 
@@ -131,7 +138,7 @@
 		if (gmyth_uri != NULL && myth_handle->is_livetv == TRUE) {
 
 			if (NULL == myth_handle->livetv) {
-				myth_handle->livetv = gmyth_livetv_new ();
+				myth_handle->livetv = gmyth_livetv_new (backend_info);
 
 				myth_handle->channel_name =
 				    gmyth_uri_get_channel_name (gmyth_uri);
@@ -141,17 +148,13 @@
 					 myth_handle->channel_name);
 
 				if (myth_handle->channel_name != NULL) {
-					if (gmyth_livetv_channel_name_setup
-					    (myth_handle->livetv,
-					     myth_handle->channel_name,
-					     backend_info) == FALSE) {
+					if (gmyth_livetv_channel_name_setup (myth_handle->livetv,
+    					     myth_handle->channel_name) == FALSE) {
 						g_object_unref (gmyth_uri);
 						ret = FALSE;
 					}
 				} else {
-					if (gmyth_livetv_setup
-					    (myth_handle->livetv,
-					     backend_info) == FALSE) {
+					if (gmyth_livetv_setup (myth_handle->livetv) == FALSE) {
 						g_object_unref (gmyth_uri);
 						ret = FALSE;
 					}
@@ -160,8 +163,7 @@
 
 			if (NULL == myth_handle->file_transfer) {
 				myth_handle->file_transfer =
-				    gmyth_livetv_create_file_transfer
-				    (myth_handle->livetv);
+				    gmyth_livetv_create_file_transfer (myth_handle->livetv);
 
 				if (NULL == myth_handle->file_transfer) {
 					ret = FALSE;
@@ -186,10 +188,8 @@
 					myth_handle->file_transfer = NULL;
 					ret = FALSE;
 				}
-			}
-			/* if - FileTransfer is NULL, or not */
+			} /* if - FileTransfer is NULL, or not */
 		} else {
-
 			if (NULL == myth_handle->file_transfer) {
 
 				myth_handle->file_transfer =
@@ -211,8 +211,7 @@
 							      (gmyth_uri));
 
 			}
-
-		}		/* if - LiveTV or not? */
+		} /* if - LiveTV or not? */
 
 		if (ret == FALSE) {
 			g_debug ("MythTV FileTransfer open error.\n");
@@ -236,9 +235,7 @@
 		if ( /*myth_handle->file_transfer->filesize <= 0 && */
 		    myth_handle->is_livetv) {
 			myth_handle->content_size =
-			    gmyth_recorder_get_file_position (myth_handle->
-							      livetv->
-							      recorder);
+			    gmyth_recorder_get_file_position (myth_handle->livetv->recorder);
 		} else {
 			myth_handle->content_size =
 			    myth_handle->file_transfer->filesize;
@@ -256,7 +253,8 @@
 	 GnomeVFSMethodHandle * method_handle,
 	 gpointer buffer,
 	 GnomeVFSFileSize num_bytes,
-	 GnomeVFSFileSize * bytes_read, GnomeVFSContext * context)
+	 GnomeVFSFileSize * bytes_read, 
+     GnomeVFSContext * context)
 {
 	MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
 	GnomeVFSFileSize bytes_to_read = num_bytes;
@@ -284,7 +282,6 @@
 		gint buffer_size;
 
 		while (MYTHTV_BUFFER_SIZE != myth_handle->buffer_remain) {
-
 			/* resize buffer length request to no more than MYTHTV_MAX_REQUEST_SIZE */
 			if ((MYTHTV_BUFFER_SIZE -
 			     myth_handle->buffer_remain) <=
@@ -297,8 +294,7 @@
 
 			GByteArray *tmp_buffer = g_byte_array_new ();
 
-			g_debug
-			    ("Asking %d bytes (there is %d bytes in the buffer)\n",
+			g_debug ("Asking %d bytes (there is %d bytes in the buffer)\n",
 			     buffer_size, myth_handle->buffer_remain);
 
 			gint len =
@@ -332,6 +328,7 @@
 		}		/* while - iterates until fills the internal buffer */
 
 	}
+        
 	/* if - got from the network, or not */
 	bytes_to_read =
 	    (bytes_to_read >
@@ -367,16 +364,16 @@
 
 	//if ( NULL == myth_handle || myth_handle->configured ) {
 
+	if (myth_handle->is_livetv && myth_handle->livetv != NULL) {
+		g_object_unref (myth_handle->livetv);
+		myth_handle->livetv = NULL;
+	}
+   
 	if (myth_handle->file_transfer != NULL) {
 		g_object_unref (myth_handle->file_transfer);
 		myth_handle->file_transfer = NULL;
 	}
 
-	if (myth_handle->is_livetv && myth_handle->livetv != NULL) {
-		g_object_unref (myth_handle->livetv);
-		myth_handle->livetv = NULL;
-	}
-
 	if (myth_handle->buffer) {
 		g_byte_array_free (myth_handle->buffer, TRUE);
 		myth_handle->buffer = NULL;
@@ -384,6 +381,8 @@
 
 	myth_handle->configured = FALSE;
 
+	g_free (myth_handle->channel_name);
+    
 	g_free (myth_handle);
 
 	myth_handle = NULL;
@@ -427,6 +426,7 @@
 
 	is_livetv = gmyth_uri_is_livetv (gmyth_uri);
 
+    file_info->valid_fields = 0;
 	file_info->valid_fields = file_info->valid_fields
 	    | GNOME_VFS_FILE_INFO_FIELDS_TYPE
 	    | GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE
@@ -451,19 +451,16 @@
 		/* FIME: Implement this at gmyth_socket */
 		res =
 		    gmyth_socket_connect_to_backend (socket,
-						     backend_info->
-						     hostname,
+						     backend_info->hostname,
 						     backend_info->port,
 						     TRUE);
 		if (!res) {
-			g_debug ("[%s] LiveTV can not connect to backend",
-				 __FUNCTION__);
+			g_debug ("[%s] LiveTV can not connect to backend",  __FUNCTION__);
 			res = FALSE;
 			goto error;
 		}
 
-		if (gmyth_remote_util_get_free_recorder_count (socket) <=
-		    0) {
+		if (gmyth_remote_util_get_free_recorder_count (socket) <= 0) {
 			g_debug ("No free remote encoder available.");
 			res = FALSE;
 			goto error;
@@ -472,12 +469,11 @@
 		/* Gets the recorder num */
 		recorder = remote_request_next_free_recorder (socket, -1);
 
-		if ( socket != NULL )
+		if (socket != NULL)
 			g_object_unref (socket);
 
 		if (recorder == NULL) {
-			g_debug ("[%s] None remote encoder available",
-				 __FUNCTION__);
+			g_debug ("[%s] None remote encoder available", __FUNCTION__);
 			res = FALSE;
 			goto error;
 		}
@@ -485,9 +481,7 @@
 		/* Init remote encoder. Opens its control socket. */
 		res = gmyth_recorder_setup (recorder);
 		if (!res) {
-			g_debug
-			    ("[%s] Fail while setting remote encoder\n",
-			     __FUNCTION__);
+			g_debug ("[%s] Fail while setting remote encoder\n", __FUNCTION__);
 			res = FALSE;
 			goto error;
 		}
@@ -501,9 +495,7 @@
 			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));
+		res = gmyth_recorder_spawntv (recorder, gmyth_tvchain_get_id(tvchain));
 		if (!res) {
 			g_warning ("[%s] Fail while spawn tv\n",
 				   __FUNCTION__);
@@ -527,8 +519,8 @@
 
 		} else {
 			file_info->name = g_strdup ("LiveTV.nuv");
-			file_info->size =
-			    gmyth_recorder_get_file_position (recorder);
+            /* Size being overrided below ... */
+			//file_info->size = gmyth_recorder_get_file_position (recorder);
 		}
 
 		if (recorder != NULL)
@@ -540,47 +532,44 @@
 		if (tvchain != NULL)
 			g_object_unref (tvchain);
 
-		file_info->size = (GnomeVFSFileSize) - 1;
+		//file_info->size = (GnomeVFSFileSize) - 1;
 
 	} else {
 
-		/* start to get file info from remote file encoder */
-		file_transfer = gmyth_file_transfer_new (backend_info);
+        /* start to get file info from remote file encoder */
+        file_transfer = gmyth_file_transfer_new (backend_info);
 
 		/* Verifies if the file exists */
-		if (!gmyth_util_file_exists
-		    (backend_info, gmyth_uri_get_path (gmyth_uri))) {
-			g_object_unref (backend_info);
-			return GNOME_VFS_ERROR_NOT_FOUND;
+    	if (!gmyth_util_file_exists (backend_info, gmyth_uri_get_path (gmyth_uri))) {
+            g_object_unref (backend_info);
+            return GNOME_VFS_ERROR_NOT_FOUND;
 		}
 
 		/* sets the Playback monitor connection */
-		ret =
-		    gmyth_file_transfer_open (file_transfer,
-					      gmyth_uri_get_path
-					      (gmyth_uri));
+		ret = gmyth_file_transfer_open (file_transfer,
+					      gmyth_uri_get_path (gmyth_uri));
 
 		file_info->name = g_strdup (gnome_vfs_uri_get_path (uri));
 
-	}			/* if - LiveTV or not? */
+	} /* if - LiveTV or not? */
 
 	if (ret == FALSE) {
 		g_debug ("MythTV FileTransfer open error\n");
 		return GNOME_VFS_ERROR_NOT_OPEN;
 	}
 
+    /* Just for recorded content */
 	if (ret == TRUE && file_transfer != NULL) {
 		file_info->size =
 		    gmyth_file_transfer_get_filesize (file_transfer);
+
+        file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_SIZE;
+        
 		if (file_transfer)
 			g_object_unref (file_transfer);
 	}
 
-	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:
+error:
 	if (backend_info)
 		g_object_unref (backend_info);