Added error argument to protocol add/remove funcs, along with checks to ensure protocol being added/removed is valid soc.2013.gobjectification.plugins

Sun, 01 Sep 2013 00:58:23 +0530

author
Ankit Vani <a@nevitus.org>
date
Sun, 01 Sep 2013 00:58:23 +0530
branch
soc.2013.gobjectification.plugins
changeset 36627
a3b0d16f69ef
parent 36626
18fc361b3704
child 36628
3d43ca8d69ee

Added error argument to protocol add/remove funcs, along with checks to ensure protocol being added/removed is valid

libpurple/protocols.c file | annotate | diff | comparison | revisions
libpurple/protocols.h file | annotate | diff | comparison | revisions
--- a/libpurple/protocols.c	Sun Sep 01 00:38:40 2013 +0530
+++ b/libpurple/protocols.c	Sun Sep 01 00:58:23 2013 +0530
@@ -743,7 +743,7 @@
 }
 
 PurpleProtocol *
-purple_protocols_add(GType protocol_type)
+purple_protocols_add(GType protocol_type, GError **error)
 {
 	PurpleProtocol *protocol;
 
@@ -751,7 +751,34 @@
 	                     protocol_type != G_TYPE_NONE, NULL);
 
 	protocol = g_object_new(protocol_type, NULL);
+
+	if (!purple_protocol_get_id(protocol)) {
+		g_set_error(error, PURPLE_PROTOCOLS_DOMAIN, 0,
+		            _("Protocol does not provide an ID"));
+
+		g_object_unref(protocol);
+		return NULL;
+	}
+
 	if (purple_protocols_find(purple_protocol_get_id(protocol))) {
+		g_set_error(error, PURPLE_PROTOCOLS_DOMAIN, 0,
+		            _("A protocol with the ID %s is already added."),
+		            purple_protocol_get_id(protocol));
+
+		g_object_unref(protocol);
+		return NULL;
+	}
+
+	/* Make sure the protocol implements the required functions */
+	if (!PURPLE_PROTOCOL_IMPLEMENTS(protocol, list_icon) ||
+	    !PURPLE_PROTOCOL_IMPLEMENTS(protocol, login)     ||
+	    !PURPLE_PROTOCOL_IMPLEMENTS(protocol, close))
+	{
+		g_set_error(error, PURPLE_PROTOCOLS_DOMAIN, 0,
+		            _("Protocol %s does not implement all the required "
+		            "functions (list_icon, login and close)"),
+		            purple_protocol_get_id(protocol));
+
 		g_object_unref(protocol);
 		return NULL;
 	}
@@ -761,10 +788,18 @@
 	return protocol;
 }
 
-gboolean purple_protocols_remove(PurpleProtocol *protocol)
+gboolean purple_protocols_remove(PurpleProtocol *protocol, GError **error)
 {
-	if (purple_protocols_find(purple_protocol_get_id(protocol)) == NULL)
+	g_return_val_if_fail(protocol != NULL, FALSE);
+	g_return_val_if_fail(purple_protocol_get_id(protocol) != NULL, FALSE);
+
+	if (purple_protocols_find(purple_protocol_get_id(protocol)) == NULL) {
+		g_set_error(error, PURPLE_PROTOCOLS_DOMAIN, 0,
+		            _("Protocol %s is not added."),
+		            purple_protocol_get_id(protocol));
+
 		return FALSE;
+	}
 
 	g_hash_table_remove(protocols, purple_protocol_get_id(protocol));
 	return TRUE;
--- a/libpurple/protocols.h	Sun Sep 01 00:38:40 2013 +0530
+++ b/libpurple/protocols.h	Sun Sep 01 00:58:23 2013 +0530
@@ -27,6 +27,8 @@
 #ifndef _PURPLE_PROTOCOLS_H_
 #define _PURPLE_PROTOCOLS_H_
 
+#define PURPLE_PROTOCOLS_DOMAIN  (g_quark_from_static_string("protocols"))
+
 #define PURPLE_TYPE_PROTOCOL_ACTION  (purple_protocol_action_get_type())
 
 /** @copydoc _PurpleProtocolAction */
@@ -564,10 +566,12 @@
  * Adds a protocol to the list of protocols.
  *
  * @param protocol_type  The type of the protocol to add.
+ * @param error  Return location for a #GError or @c NULL. If provided, this
+ *               will be set to the reason if adding fails.
  *
  * @return The protocol instance if the protocol was added, else @c NULL.
  */
-PurpleProtocol *purple_protocols_add(GType protocol_type);
+PurpleProtocol *purple_protocols_add(GType protocol_type, GError **error);
 
 /**
  * Removes a protocol from the list of protocols. This will disconnect all
@@ -575,10 +579,12 @@
  * and protocol options.
  *
  * @param protocol  The protocol to remove.
- *
+ * @param error  Return location for a #GError or @c NULL. If provided, this
+ *               will be set to the reason if removing fails.
+
  * @return TRUE if the protocol was removed, else FALSE.
  */
-gboolean purple_protocols_remove(PurpleProtocol *protocol);
+gboolean purple_protocols_remove(PurpleProtocol *protocol, GError **error);
 
 /**
  * Returns a list of all loaded protocols.

mercurial