/pidgin/main: 08ec2e6e0174: libpurple: Add purple_connection_g_e...

Mike Ruprecht cmaiku at gmail.com
Sun May 15 11:11:26 EDT 2016


Changeset: 08ec2e6e0174c533e88a0be0a919f45fc2e229c9
Author:	 Mike Ruprecht <cmaiku at gmail.com>
Date:	 2016-04-29 03:09 -0500
Branch:	 default
URL: https://hg.pidgin.im/pidgin/main/rev/08ec2e6e0174

Description:

libpurple: Add purple_connection_g_error() to simplify handling G_IO_ERRORs

This patch adds purple_connection_g_error() to simplify handling
G_IO_ERRORs. This is especially useful for GSocketConnections and
generally with Gio. It behaves like purple_connection_error(), but
takes a GError, translating it to purple's error codes.

diffstat:

 libpurple/connection.c |  38 ++++++++++++++++++++++++++++++++++++++
 libpurple/connection.h |  21 +++++++++++++++++++++
 2 files changed, 59 insertions(+), 0 deletions(-)

diffs (79 lines):

diff --git a/libpurple/connection.c b/libpurple/connection.c
--- a/libpurple/connection.c
+++ b/libpurple/connection.c
@@ -542,6 +542,44 @@ purple_connection_ssl_error (PurpleConne
 		purple_ssl_strerror(ssl_error));
 }
 
+void
+purple_connection_g_error(PurpleConnection *pc, const GError *error,
+		const gchar *description)
+{
+	PurpleConnectionError reason;
+	gchar *tmp;
+
+	if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
+		/* Not a connection error. Ignore. */
+		return;
+	}
+
+	if (error->domain == G_TLS_ERROR) {
+		switch (error->code) {
+			case G_TLS_ERROR_UNAVAILABLE:
+				reason = PURPLE_CONNECTION_ERROR_NO_SSL_SUPPORT;
+			case G_TLS_ERROR_NOT_TLS:
+			case G_TLS_ERROR_HANDSHAKE:
+				reason = PURPLE_CONNECTION_ERROR_ENCRYPTION_ERROR;
+			case G_TLS_ERROR_BAD_CERTIFICATE:
+			case G_TLS_ERROR_CERTIFICATE_REQUIRED:
+				reason = PURPLE_CONNECTION_ERROR_CERT_OTHER_ERROR;
+			case G_TLS_ERROR_EOF:
+			case G_TLS_ERROR_MISC:
+			default:
+				reason = PURPLE_CONNECTION_ERROR_NETWORK_ERROR;
+		}
+	} else if (error->domain == G_IO_ERROR) {
+		reason = PURPLE_CONNECTION_ERROR_NETWORK_ERROR;
+	} else {
+		reason = PURPLE_CONNECTION_ERROR_OTHER_ERROR;
+	}
+
+	tmp = g_strdup_printf(description, error->message);
+	purple_connection_error(pc, reason, tmp);
+	g_free(tmp);
+}
+
 gboolean
 purple_connection_error_is_fatal (PurpleConnectionError reason)
 {
diff --git a/libpurple/connection.h b/libpurple/connection.h
--- a/libpurple/connection.h
+++ b/libpurple/connection.h
@@ -506,6 +506,27 @@ void
 purple_connection_ssl_error (PurpleConnection *gc,
                              PurpleSslErrorType ssl_error);
 
+/*
+ * purple_connection_g_error
+ * @gc: Connection the error is associated with
+ * @error: Error information
+ * @description: Extra string which further explains the error.
+ *               Substitutes a "%s" with the GError message.
+ *
+ * Closes a connection similar to purple_connection_error(), but
+ * takes a GError which is then converted to purple error codes.
+ *
+ * This function ignores G_IO_ERROR_CANCELLED, returning without
+ * closing the connection. This can be used as a shortcut when
+ * cancelling connections, as this is commonly done when shutting
+ * down a connection. If G_IO_ERROR_CANCELLED needs to be caught,
+ * do so with g_error_matches() prior to calling this function.
+ */
+void
+purple_connection_g_error(PurpleConnection *pc,
+                          const GError *error,
+                          const gchar *description);
+
 /**
  * purple_connection_error_is_fatal:
  *



More information about the Commits mailing list