# HG changeset patch
# User rosfran
# Date 1170968251 0
# Node ID 60bfeabc0af66e00fdc131433820cf5a6cc78ec6
# Parent  3e34d1a2e4030d6b8b9946ce78a64b0c0a6b1537
[svn r339] GObject's dispose methods organization, and reviewed clean-up actions.

diff -r 3e34d1a2e403 -r 60bfeabc0af6 gmyth/src/gmyth_livetv.c
--- a/gmyth/src/gmyth_livetv.c	Thu Feb 08 14:32:29 2007 +0000
+++ b/gmyth/src/gmyth_livetv.c	Thu Feb 08 20:57:31 2007 +0000
@@ -71,7 +71,8 @@
 	livetv->local_hostname = NULL;
 	livetv->file_transfer = NULL;
 	livetv->setup_done = FALSE;
-
+	
+	livetv->socket = NULL;
 	livetv->recorder = NULL;
 	livetv->tvchain = NULL;
 	livetv->proginfo = NULL;
@@ -88,12 +89,17 @@
 		g_object_unref (livetv->monitor);
 		livetv->monitor = NULL;
 	}
-
+	
 	if ( livetv->recorder != NULL ) {
 		g_object_unref (livetv->recorder);
 		livetv->recorder = NULL;
 	}
 
+	if ( livetv->socket != NULL ) {
+		g_object_unref (livetv->socket);
+		livetv->socket = NULL;
+	}
+	
 	if ( livetv->tvchain != NULL ) {
 		g_object_unref (livetv->tvchain);
 		livetv->tvchain = NULL;
@@ -119,7 +125,6 @@
 		g_object_unref (livetv->uri);
 		livetv->uri = NULL;
 	}
-
 	
 	G_OBJECT_CLASS (gmyth_livetv_parent_class)->dispose (object);
 }
@@ -278,15 +283,16 @@
 			GMythBackendInfo *backend_info )
 {
 	gboolean res = TRUE;
-
-	GMythSocket *socket = gmyth_socket_new ();
+	
+	if ( NULL == livetv->socket )
+		livetv->socket = gmyth_socket_new ();
 	
 	livetv->backend_info = backend_info;
 	
-	g_static_mutex_lock( &lock );
+	//g_static_mutex_lock( &lock );
 
 	/* FIME: Implement this at gmyth_socket */
-	res = gmyth_socket_connect_to_backend (socket, livetv->backend_info->hostname,
+	res = gmyth_socket_connect_to_backend (livetv->socket, livetv->backend_info->hostname,
 			livetv->backend_info->port, TRUE);
 	if (!res) {
 		g_warning ("[%s] LiveTV can not connect to backend", __FUNCTION__);	
@@ -303,17 +309,17 @@
 		goto error;
 	}
 	
-	if ( gmyth_remote_util_get_free_recorder_count (socket) <= 0 ) {
+	if ( gmyth_remote_util_get_free_recorder_count (livetv->socket) <= 0 ) {
 		gmyth_debug ("No free remote encoder available.");
 		res = FALSE;
 		goto error;
 	}
 
 	/* Gets the recorder num */
-	livetv->recorder = remote_request_next_free_recorder (socket, -1);
-	gmyth_socket_close_connection (socket);
+	livetv->recorder = remote_request_next_free_recorder (livetv->socket, -1);
+	//gmyth_socket_close_connection (socket);
 
-	if ( livetv->recorder == NULL ) {
+	if ( NULL == livetv->recorder ) {
 		g_warning ("[%s] None remote encoder available", __FUNCTION__);
 		res = FALSE;
 		goto error;
@@ -458,7 +464,7 @@
 	
 	livetv->uri = (GMythURI*)gmyth_backend_info_get_uri( backend_info );
 	
-	g_static_mutex_unlock( &lock );
+	//g_static_mutex_unlock( &lock );
 
 	if ( !gmyth_livetv_monitor_handler_start( livetv ) )
 	{
diff -r 3e34d1a2e403 -r 60bfeabc0af6 gmyth/src/gmyth_livetv.h
--- a/gmyth/src/gmyth_livetv.h	Thu Feb 08 14:32:29 2007 +0000
+++ b/gmyth/src/gmyth_livetv.h	Thu Feb 08 20:57:31 2007 +0000
@@ -61,6 +61,8 @@
 {
 	GObject 						parent;
 	
+	GMythSocket					*socket;
+	
 	GString 				 		*local_hostname;
 	
 	GMythBackendInfo 		*backend_info;
diff -r 3e34d1a2e403 -r 60bfeabc0af6 gmyth/src/gmyth_socket.c
--- a/gmyth/src/gmyth_socket.c	Thu Feb 08 14:32:29 2007 +0000
+++ b/gmyth/src/gmyth_socket.c	Thu Feb 08 20:57:31 2007 +0000
@@ -71,7 +71,7 @@
 
 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
 
-static GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
+/*static GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;*/
 
 static gchar* local_hostname = NULL;
 
@@ -553,6 +553,8 @@
 
     gmyth_socket->sd_io_ch = g_io_channel_unix_new (gmyth_socket->sd);
     
+    g_io_channel_set_close_on_unref (gmyth_socket->sd_io_ch, TRUE);
+    
     //GIOFlags flags = g_io_channel_get_flags (gmyth_socket->sd_io_ch);
 		/* unset the nonblock flag */
 		//flags &= ~G_IO_FLAG_NONBLOCK;
@@ -927,7 +929,7 @@
 
     /* verify if the input (read) buffer is ready to receive data */
 
-    g_static_mutex_lock( &mutex );
+    //g_static_mutex_lock( &mutex );
     //g_static_rw_lock_reader_lock (&rwlock);
     
     //buffer = g_new0 (gchar, MYTH_PROTOCOL_FIELD_SIZE);
@@ -936,8 +938,11 @@
  		{
  			gmyth_socket_connect( gmyth_socket, gmyth_socket->hostname, gmyth_socket->port );
  		}
- 			
-    io_status = g_io_channel_read_chars (gmyth_socket->sd_io_ch, buffer, MYTH_PROTOCOL_FIELD_SIZE, &bytes_read, &error);
+ 		
+ 		if ( gmyth_socket->sd_io_ch->is_readable )
+    	io_status = g_io_channel_read_chars (gmyth_socket->sd_io_ch, buffer, MYTH_PROTOCOL_FIELD_SIZE, &bytes_read, &error);
+    else
+    	return g_string_new("");
 
     /* verify if the input (read) buffer is ready to receive data */
     io_cond = g_io_channel_get_buffer_condition (gmyth_socket->sd_io_ch);
@@ -981,12 +986,16 @@
 		    bytes_read = 0;
 				if ( !( gmyth_socket != NULL && gmyth_socket->sd_io_ch != NULL) )
 					return NULL;
-		    io_status = g_io_channel_read_chars( gmyth_socket->sd_io_ch, buffer, len, &bytes_read, &error);
+		 		if ( gmyth_socket->sd_io_ch->is_readable )
+		    	io_status = g_io_channel_read_chars( gmyth_socket->sd_io_ch, buffer, len, &bytes_read, &error);
+		    else
+		    	return g_string_new("");
+		    	
 		    buffer[bytes_read] = '\0';
 			}
     }
 
-    g_static_mutex_unlock( &mutex );
+    //g_static_mutex_unlock( &mutex );
     //g_static_rw_lock_reader_unlock (&rwlock);
 
     gmyth_debug ("Response received from backend: {%s}\n", buffer);
diff -r 3e34d1a2e403 -r 60bfeabc0af6 gmyth/src/gmyth_stringlist.c
--- a/gmyth/src/gmyth_stringlist.c	Thu Feb 08 14:32:29 2007 +0000
+++ b/gmyth/src/gmyth_stringlist.c	Thu Feb 08 20:57:31 2007 +0000
@@ -66,6 +66,8 @@
 
 	if (gmyth_string_list->glist)
 		gmyth_string_list_clear_all(gmyth_string_list);
+		
+	g_list_free( gmyth_string_list->glist );
 
 	G_OBJECT_CLASS (gmyth_string_list_parent_class)->dispose (object);
 }
@@ -88,6 +90,7 @@
 gmyth_string_list_new ()
 {
     GMythStringList *gmyth_string_list = GMYTH_STRING_LIST (g_object_new (GMYTH_STRING_LIST_TYPE, NULL));
+    
     return gmyth_string_list;
 }
 
@@ -384,7 +387,8 @@
 gint
 gmyth_string_list_length ( GMythStringList *strlist )
 {
-	g_return_val_if_fail( strlist != NULL && strlist->glist != NULL, 0 );
+	if ( strlist != NULL && strlist->glist != NULL )
+		return 0;
 
 	return g_list_length (strlist->glist);
 }
diff -r 3e34d1a2e403 -r 60bfeabc0af6 gmyth/src/gmyth_util.c
--- a/gmyth/src/gmyth_util.c	Thu Feb 08 14:32:29 2007 +0000
+++ b/gmyth/src/gmyth_util.c	Thu Feb 08 20:57:31 2007 +0000
@@ -331,7 +331,7 @@
 	GTimeVal *time = g_new0( GTimeVal, 1 );
 	struct tm* tm_time = NULL;
 	time_t time_micros;
-	gint result;
+	gchar* result;
 
 	gmyth_debug( "[%s] time_str = %s. [%s]", time_str, time_str != NULL ? 
 					time_str : "[time string is NULL!]", time_str );