[gyrus] Replace gnet with GSocketClient from gio



commit 790d7e27f8c3c8e3dd214f4235d6b3d8a02c18d4
Author: Claudio Saavedra <csaavedra igalia com>
Date:   Tue Dec 28 13:11:56 2010 +0200

    Replace gnet with GSocketClient from gio
    
    Use a GSocketClient instead of the deprecated GNet library for
    connection to the servers. This is a very simple migration, without
    taking advantages of the asynchronous API of gio yet.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=593660

 README                    |    2 +-
 configure.ac              |    4 +-
 src/gyrus-admin-mailbox.c |    7 +--
 src/gyrus-admin-private.h |    5 +-
 src/gyrus-admin.c         |  156 ++++++++++++++++++++++++++++++---------------
 src/gyrus-admin.h         |    5 +-
 6 files changed, 114 insertions(+), 65 deletions(-)
---
diff --git a/README b/README
index d0a24e9..4f3c206 100644
--- a/README
+++ b/README
@@ -30,7 +30,7 @@ The needed dependencies include:
 
 gconf-2.0 (>= 2.0.0)
 gtk+-2.0 (>= 2.18.0)
-gnet-2.0 (>= 2.0.0)
+gio (>= 2.22.0)
 
 * Features
 
diff --git a/configure.ac b/configure.ac
index 226e7cc..1adfd4e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -22,14 +22,14 @@ dnl GNOME 2 force flags
 GNOME_COMPILE_WARNINGS(maximum)
 
 GTK_REQUIRED=2.18.0
-GNET_REQUIRED=2.0.0
+GIO_REQUIRED=2.22.0
 GCONF_REQUIRED=2.0.0
 GTK_PRINT_REQUIRED=2.10.0
 GNUTLS_REQUIRED=1.0.0
 
 PKG_CHECK_MODULES(GYRUS,
                   gtk+-2.0 >= $GTK_REQUIRED
-		  gnet-2.0 >= $GNET_REQUIRED
+		  gio-2.0 >= $GIO_REQUIRED
 		  gconf-2.0 >= $GCONF_REQUIRED
 		  gtk+-unix-print-2.0 >= $GTK_PRINT_REQUIRED)
 
diff --git a/src/gyrus-admin-mailbox.c b/src/gyrus-admin-mailbox.c
index b1289ca..f4cde05 100644
--- a/src/gyrus-admin-mailbox.c
+++ b/src/gyrus-admin-mailbox.c
@@ -27,7 +27,6 @@
 
 #include <gtk/gtk.h>
 #include <glib/gprintf.h>
-#include <gnet.h>
 #include <string.h>
 
 #include "glib/gi18n.h"
@@ -261,11 +260,10 @@ static gboolean
 gyrus_admin_mailbox_exists (GyrusAdmin *admin,
 			    const gchar *mailbox)
 {
-	GIOError error = G_IO_ERROR_NONE;
 	GyrusImapStatus status;
 	
 	gchar *msg = g_strdup_printf (". list \"\" \"%s\"\n", mailbox);
-	error = gyrus_admin_write_channel (admin, msg);
+	gyrus_admin_write_channel (admin, msg);
 	g_free (msg);
 
 	status = gyrus_admin_listen_channel (admin, &msg, NULL);
@@ -318,7 +316,6 @@ gboolean
 gyrus_admin_mailbox_new (GyrusAdmin *admin, const gchar *mailbox,
 			 const gchar *path, gint quota, gchar **error)
 {
-	GIOError ioerror = G_IO_ERROR_NONE;
 	GyrusImapStatus status;
 	gchar *new_mailbox;
 	gchar *msg;
@@ -349,7 +346,7 @@ gyrus_admin_mailbox_new (GyrusAdmin *admin, const gchar *mailbox,
 	}
 
 	msg = g_strdup_printf (". create \"%s\"\n", new_mailbox);
-	ioerror = gyrus_admin_write_channel (admin, msg);
+	gyrus_admin_write_channel (admin, msg);
 	g_free (msg);
 
 	status = gyrus_admin_listen_channel (admin, &msg, NULL);
diff --git a/src/gyrus-admin-private.h b/src/gyrus-admin-private.h
index 99dc8e0..e0d8afc 100644
--- a/src/gyrus-admin-private.h
+++ b/src/gyrus-admin-private.h
@@ -66,8 +66,9 @@ struct _GyrusAdminPrivate {
 	GtkWidget *label_acl;
 	GtkWidget *scrolled_acl;
 	
-	GIOChannel *iochannel;
-	GTcpSocket *tcp_socket;
+	GSocketConnection *connection;
+	GSocketClient *tcp_socket;
+	gchar *buffer;
 
 	GyrusSession *session;
 };
diff --git a/src/gyrus-admin.c b/src/gyrus-admin.c
index e606178..02b3862 100644
--- a/src/gyrus-admin.c
+++ b/src/gyrus-admin.c
@@ -24,6 +24,7 @@
 #include <config.h>
 
 #include <gtk/gtk.h>
+#include <gio/gio.h>
 #include <string.h>
 #include <glib/gi18n.h>
 
@@ -233,7 +234,8 @@ gyrus_admin_init (GyrusAdmin *admin)
 {
 	admin->priv = GYRUS_ADMIN_GET_PRIVATE (admin);
 	admin->priv->session = NULL;
-	
+	admin->priv->buffer = NULL;
+
 	gyrus_admin_init_get_widgets (admin);
 	gyrus_admin_initialize_mailbox_tree_view (admin, FALSE);
 	gyrus_admin_initialize_acl_tree_view (admin);
@@ -254,6 +256,11 @@ gyrus_admin_finalize (GObject *object)
 	
 	gyrus_session_free (admin->priv->session);
 
+	if (admin->priv->buffer) {
+		g_free (admin->priv->buffer);
+		admin->priv->buffer = NULL;
+	}
+
 	G_OBJECT_CLASS (gyrus_admin_parent_class)->finalize (object);
 }
 
@@ -298,9 +305,8 @@ gyrus_admin_set_separator_char_auto (GyrusAdmin *admin)
 {
 	gchar **str_v;
 	gchar *msg = g_strdup (". list \"\" \"\"\n");
-	GIOError error = G_IO_ERROR_NONE;
 
-	error = gyrus_admin_write_channel (admin, msg);
+	gyrus_admin_write_channel (admin, msg);
 	g_free (msg);
 
 	gyrus_admin_listen_channel (admin, &msg, NULL);
@@ -323,7 +329,7 @@ gyrus_admin_get_users_list (GyrusAdmin *admin)
 	GtkTreeStore *store_corrupt;
 	gchar *msg;
 	gchar **current_mailbox;
-	GIOError error = G_IO_ERROR_NONE;
+	gboolean success;
 	gint depth, old_depth = 1;
 	GtkTreeIter iter_new, iter_parent, iter_child, iter_corrupt;
 	gboolean is_valid;
@@ -342,9 +348,9 @@ gyrus_admin_get_users_list (GyrusAdmin *admin)
 	gtk_tree_store_clear (store_corrupt);
 
 	msg = g_strdup (". list * *\n");
-	error = gyrus_admin_write_channel (admin, msg);
+	success = gyrus_admin_write_channel (admin, msg);
 	g_free (msg);
-	g_return_if_fail (error == G_IO_ERROR_NONE);
+	g_return_if_fail (success == TRUE);
 
 	while ((status = gyrus_admin_listen_channel (admin, &msg, NULL)) ==
 	       GYRUS_IMAP_STATUS_LIST) {
@@ -631,47 +637,91 @@ gyrus_admin_initialize_acl_tree_view (GyrusAdmin *admin)
 	gtk_tree_view_set_model (view, GTK_TREE_MODEL (model));
 }
 
-static GTcpSocket *
-gyrus_admin_create_socket_from_session (GyrusSession *session)
+static GSocketClient *
+gyrus_admin_create_socket_from_session (GyrusAdmin *admin, GyrusSession *session)
 {
-	GInetAddr *addr;
-	GTcpSocket *sock;
+	GSocketClient *sock;
+	GSocketConnectable *connectable;
 	gchar *msg;
 
 	g_return_val_if_fail (session, NULL);
-	
-	addr = gnet_inetaddr_new (session->host, session->port);
-	if (!addr) {
-		msg = g_strdup_printf (_("%s could not be found. Please check the name and try again."),
-				       session->host);
+
+	sock = g_socket_client_new ();
+	if (sock == NULL) {
+		msg = g_strdup (_("Couldn't create the client socket."));
 		gyrus_common_show_message (NULL, GTK_MESSAGE_ERROR, msg);
 		g_free (msg);
 		return NULL;
 	}
 
-	sock = gnet_tcp_socket_new (addr);
-	gnet_inetaddr_delete (addr);
+	connectable = g_network_address_parse (session->host, session->port, NULL);
+	if (connectable == NULL) {
+		msg = g_strdup (_("Couldn't parse the server address."));
+		gyrus_common_show_message (NULL, GTK_MESSAGE_ERROR, msg);
+		g_free (msg);
+		g_object_unref (sock);
+		return NULL;
+	}
 
-	if (!sock) {
+	admin->priv->connection = g_socket_client_connect (sock, connectable, NULL, NULL);
+	if (admin->priv->connection == NULL) {
 		msg = g_strdup_printf (_("Could not connect to %s, port %d."),
 				       session->host, session->port);
 		gyrus_common_show_message (NULL, GTK_MESSAGE_ERROR, msg);
 		g_free (msg);
+		g_object_unref (sock);
 		return NULL;
 	}
+	g_object_unref (connectable);
 
 	return sock;
 }
 
-static void
-gyrus_admin_initialize_channel (GyrusAdmin *admin)
+static gchar *
+socket_readline (GyrusAdmin *admin, gsize *len)
 {
-	g_return_if_fail (admin->priv->iochannel == NULL);
-	g_return_if_fail (admin->priv->tcp_socket);
-	admin->priv->iochannel =
-		gnet_tcp_socket_get_io_channel (admin->priv->tcp_socket);
-	
-	g_assert (admin->priv->iochannel);
+	GInputStream *stream;
+	gchar *retval;
+	gchar **toks;
+	gchar *n_buffer;
+	stream = g_io_stream_get_input_stream (G_IO_STREAM (admin->priv->connection));
+
+	if (admin->priv->buffer == NULL) {
+		admin->priv->buffer = g_malloc0 (BUFFER_SIZE * sizeof (gchar));
+		g_input_stream_read (stream, admin->priv->buffer,
+				     BUFFER_SIZE - 1, NULL, NULL);
+	}
+
+	toks = g_strsplit (admin->priv->buffer, "\n", 2);
+	g_free (admin->priv->buffer);
+
+	/* "Someth". We need to feed the buffer. */
+	while (toks[0] != NULL && toks[1] == NULL) {
+		admin->priv->buffer = g_malloc0 (BUFFER_SIZE * sizeof (gchar));
+		g_input_stream_read (stream,
+				     admin->priv->buffer,
+				     BUFFER_SIZE - 1,
+				     NULL, NULL);
+
+		n_buffer = g_strconcat (toks[0], admin->priv->buffer, NULL);
+		g_free (admin->priv->buffer);
+		g_strfreev (toks);
+		toks = g_strsplit (n_buffer, "\n", 2);
+		g_free (n_buffer);
+	}
+
+	retval = toks[0];
+	admin->priv->buffer = toks[1];
+	g_free (toks);
+
+	if (admin->priv->buffer && strcmp (admin->priv->buffer, "") == 0) {
+		g_free (admin->priv->buffer);
+		admin->priv->buffer = NULL;
+	}
+
+	*len = strlen (retval);
+
+	return retval;
 }
 
 /* Allocates @msg_len bytes of memory to store the message.
@@ -679,10 +729,9 @@ gyrus_admin_initialize_channel (GyrusAdmin *admin)
 GyrusImapStatus
 gyrus_admin_listen_channel (GyrusAdmin *admin, gchar **message, gint *msg_len)
 {
-	gchar buffer[BUFFER_SIZE];
 	gsize bytes_read;
 	gchar **tokens = NULL;
-	GIOError error;
+	GError *error = NULL;
 	GyrusImapStatus imap_status;
 
 	*message = NULL;
@@ -695,19 +744,11 @@ gyrus_admin_listen_channel (GyrusAdmin *admin, gchar **message, gint *msg_len)
 
 		error = G_IO_ERROR_NONE;
 		imap_status = GYRUS_IMAP_STATUS_OK;
-		
-		error = gnet_io_channel_readline (admin->priv->iochannel,
-						  buffer, BUFFER_SIZE,
-						  &bytes_read);
-		if (error != G_IO_ERROR_NONE) {
-			g_warning ("%i", error);
-			return TRUE;
-		}
-		
+		*message = socket_readline (admin, &bytes_read);
+
 #ifdef DEBUG_PLAIN_CHANNEL
-		g_print ("read:  %s", buffer);
+		g_print ("read:  %s", *message);
 #endif
-		*message = g_strdup (buffer);
 		tokens = g_strsplit (*message, " ", 0);
 		if (msg_len != NULL)
 			*msg_len = bytes_read;
@@ -737,20 +778,26 @@ gyrus_admin_listen_channel (GyrusAdmin *admin, gchar **message, gint *msg_len)
 	return imap_status;
 }
 
-GIOError
+gboolean
 gyrus_admin_write_channel (GyrusAdmin *admin, gchar *message)
 {
+	GOutputStream *stream;
 	guint msg_len;
 	gsize bytes_written;
-	GIOError error = G_IO_ERROR_NONE;
+	GError *error = NULL;
 
 	msg_len = g_utf8_strlen (message, -1);
-	error = gnet_io_channel_writen (admin->priv->iochannel, message,
-					msg_len, &bytes_written);
+	stream = g_io_stream_get_output_stream (G_IO_STREAM (admin->priv->connection));
+	bytes_written = g_output_stream_write (stream, message, msg_len, NULL, &error);
 #ifdef DEBUG_PLAIN_CHANNEL
- 	g_print ("written: %s", message); 
+	g_print ("written: %s", message);
 #endif
-	return error;
+	if (error) {
+		g_error_free (error);
+		return FALSE;
+	} else {
+		return TRUE;
+	}
 }
 
 gboolean
@@ -765,8 +812,15 @@ gyrus_admin_logout (GyrusAdmin *admin)
 	status = gyrus_admin_listen_channel (admin, &msg, NULL);
 
 	if (status == GYRUS_IMAP_STATUS_BYE) {
-		gnet_tcp_socket_delete(admin->priv->tcp_socket);
-		admin->priv->iochannel = NULL;
+		g_io_stream_close (G_IO_STREAM (admin->priv->connection), NULL, NULL);
+		g_object_unref (admin->priv->connection);
+		admin->priv->connection = NULL;
+
+		g_object_unref (admin->priv->tcp_socket);
+		admin->priv->tcp_socket = NULL;
+
+		g_free (admin->priv->buffer);
+		admin->priv->buffer = NULL;
 		g_free (msg);
 	}
 
@@ -860,19 +914,17 @@ imap_quote_string (const char *str)
 static GyrusAdminLoginError
 gyrus_admin_login (GyrusAdmin *admin)
 {
-	GTcpSocket *socket;
+	GSocketClient *socket;
 	gchar *msg;
 	gchar *q_password;
 	GyrusImapStatus status;
 	GyrusAdminLoginError error = GYRUS_ADMIN_LOGIN_OK;
-	socket = gyrus_admin_create_socket_from_session (admin->priv->session);
+	socket = gyrus_admin_create_socket_from_session (admin, admin->priv->session);
 	if (!socket)
 		return GYRUS_ADMIN_LOGIN_NO_HOST;
 	
 	admin->priv->tcp_socket = socket;
 
-	gyrus_admin_initialize_channel (admin);
-
 	status = gyrus_admin_listen_channel (admin, &msg, NULL);
 	g_free (msg);
 
@@ -1055,7 +1107,7 @@ gyrus_admin_is_connected (GyrusAdmin *admin)
 {
 	g_return_val_if_fail (GYRUS_IS_ADMIN (admin), FALSE);
 	g_return_val_if_fail (admin != NULL, FALSE);
-	return (admin->priv->iochannel != NULL);
+	return (admin->priv->connection != NULL);
 }
 
 const gchar*
diff --git a/src/gyrus-admin.h b/src/gyrus-admin.h
index 1d1ed64..c8d92d3 100644
--- a/src/gyrus-admin.h
+++ b/src/gyrus-admin.h
@@ -24,7 +24,6 @@
 #define GYRUS_ADMIN_H
 
 #include <gtk/gtk.h>
-#include <gnet.h>
 #include "gyrus-session.h"
 
 #define GYRUS_TYPE_ADMIN            (gyrus_admin_get_type ())
@@ -171,9 +170,9 @@ GyrusImapStatus gyrus_admin_listen_channel (GyrusAdmin *admin, gchar **message,
    valid IMAP command, following the RFC. To read the response of 
    the server, use gyrus_admin_listen_channel ().
 
-   Returns: a GIOError message indicating if sucessful or not.
+   Returns: %TRUE if successful, %FALSE otherwise
 */
-GIOError gyrus_admin_write_channel (GyrusAdmin *admin, gchar *message);
+gboolean gyrus_admin_write_channel (GyrusAdmin *admin, gchar *message);
 
 /**
    Parses a LIST server message and get the mailbox name from it.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]