Rename Purple.ProtocolManager.register/unregister to add/remove

Fri, 01 Nov 2024 00:30:41 -0500

author
Gary Kramlich <grim@reaperworld.com>
date
Fri, 01 Nov 2024 00:30:41 -0500
changeset 43034
b45543113d52
parent 43033
c28c7a6710be
child 43035
c6caa9bd7afe

Rename Purple.ProtocolManager.register/unregister to add/remove

A bunch of stuff uses this, so the old methods got marked as deprecated as well
as a few other things that are handled by the Gio.ListModel implementation.

Testing Done:
Called in the turtles as we don't have any unit tests for the protocol manager.

Reviewed at https://reviews.imfreedom.org/r/3625/

libpurple/purpleprotocolmanager.c file | annotate | diff | comparison | revisions
libpurple/purpleprotocolmanager.h file | annotate | diff | comparison | revisions
--- a/libpurple/purpleprotocolmanager.c	Fri Nov 01 00:28:41 2024 -0500
+++ b/libpurple/purpleprotocolmanager.c	Fri Nov 01 00:30:41 2024 -0500
@@ -34,6 +34,8 @@
 static GParamSpec *properties[N_PROPERTIES] = {NULL, };
 
 enum {
+	SIG_ADDED,
+	SIG_REMOVED,
 	SIG_REGISTERED,
 	SIG_UNREGISTERED,
 	N_SIGNALS,
@@ -167,6 +169,48 @@
 	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
 
 	/**
+	 * PurpleProtocolManager::added:
+	 * @manager: The #PurpleProtocolManager instance.
+	 * @protocol: The #PurpleProtocol that was added.
+	 *
+	 * Emitted after @protocol has been added to @manager.
+	 *
+	 * Since: 3.0
+	 */
+	signals[SIG_ADDED] = g_signal_new_class_handler(
+		"added",
+		G_OBJECT_CLASS_TYPE(klass),
+		G_SIGNAL_RUN_LAST,
+		NULL,
+		NULL,
+		NULL,
+		NULL,
+		G_TYPE_NONE,
+		1,
+		PURPLE_TYPE_PROTOCOL);
+
+	/**
+	 * PurpleProtocolManager::removed:
+	 * @manager: The #PurpleProtocolManager instance.
+	 * @protocol: The #PurpleProtocol that was removed.
+	 *
+	 * Emitted after @protocol has been removed from @manager.
+	 *
+	 * Since: 3.0
+	 */
+	signals[SIG_REMOVED] = g_signal_new_class_handler(
+		"removed",
+		G_OBJECT_CLASS_TYPE(klass),
+		G_SIGNAL_RUN_LAST,
+		NULL,
+		NULL,
+		NULL,
+		NULL,
+		G_TYPE_NONE,
+		1,
+		PURPLE_TYPE_PROTOCOL);
+
+	/**
 	 * PurpleProtocolManager::registered:
 	 * @manager: The #PurpleProtocolManager instance.
 	 * @protocol: The #PurpleProtocol that was registered.
@@ -174,6 +218,8 @@
 	 * Emitted after @protocol has been registered in @manager.
 	 *
 	 * Since: 3.0
+	 *
+	 * Deprecated: 3.0
 	 */
 	signals[SIG_REGISTERED] = g_signal_new_class_handler(
 		"registered",
@@ -195,6 +241,8 @@
 	 * Emitted after @protocol has been unregistered from @manager.
 	 *
 	 * Since: 3.0
+	 *
+	 * Deprecated: 3.0
 	 */
 	signals[SIG_UNREGISTERED] = g_signal_new_class_handler(
 		"unregistered",
@@ -227,14 +275,9 @@
 /******************************************************************************
  * Public API
  *****************************************************************************/
-PurpleProtocolManager *
-purple_protocol_manager_get_default(void) {
-	return default_manager;
-}
-
 gboolean
-purple_protocol_manager_register(PurpleProtocolManager *manager,
-                                 PurpleProtocol *protocol, GError **error)
+purple_protocol_manager_add(PurpleProtocolManager *manager,
+                            PurpleProtocol *protocol, GError **error)
 {
 	const gchar *id = NULL;
 
@@ -244,7 +287,7 @@
 	id = purple_protocol_get_id(protocol);
 	if(g_hash_table_lookup(manager->protocols, id) != NULL) {
 		g_set_error(error, PURPLE_PROTOCOL_MANAGER_DOMAIN, 0,
-		            _("protocol %s is already registered"), id);
+		            _("protocol %s has already been added"), id);
 
 		return FALSE;
 	}
@@ -254,52 +297,35 @@
 	g_ptr_array_add(manager->list, protocol);
 	g_list_model_items_changed(G_LIST_MODEL(manager), manager->list->len - 1, 0, 1);
 
+	g_signal_emit(G_OBJECT(manager), signals[SIG_ADDED], 0, protocol);
 	g_signal_emit(G_OBJECT(manager), signals[SIG_REGISTERED], 0, protocol);
 
 	return TRUE;
 }
 
+PurpleProtocolManager *
+purple_protocol_manager_get_default(void) {
+	return default_manager;
+}
+
+gboolean
+purple_protocol_manager_register(PurpleProtocolManager *manager,
+                                 PurpleProtocol *protocol, GError **error)
+{
+	g_return_val_if_fail(PURPLE_IS_PROTOCOL_MANAGER(manager), FALSE);
+	g_return_val_if_fail(PURPLE_IS_PROTOCOL(protocol), FALSE);
+
+	return purple_protocol_manager_add(manager, protocol, error);
+}
+
 gboolean
 purple_protocol_manager_unregister(PurpleProtocolManager *manager,
                                    PurpleProtocol *protocol, GError **error)
 {
-	const gchar *id = NULL;
-	gboolean ret = FALSE;
-
 	g_return_val_if_fail(PURPLE_IS_PROTOCOL_MANAGER(manager), FALSE);
 	g_return_val_if_fail(PURPLE_IS_PROTOCOL(protocol), FALSE);
 
-	/* We need to hold a reference on the protocol as typically we will be
-	 * holding the only reference on the protocol when this is called and we
-	 * will need to pass it to the signal emission after it's removed from the
-	 * hash table that'll unref it.
-	 */
-	g_object_ref(protocol);
-
-	id = purple_protocol_get_id(protocol);
-
-	if(g_hash_table_remove(manager->protocols, id)) {
-		guint position;
-
-		if(g_ptr_array_find(manager->list, protocol, &position)) {
-			g_ptr_array_remove_index(manager->list, position);
-			g_list_model_items_changed(G_LIST_MODEL(manager), position, 1, 0);
-		}
-
-		g_signal_emit(G_OBJECT(manager), signals[SIG_UNREGISTERED], 0,
-		              protocol);
-
-		ret = TRUE;
-	} else {
-		g_set_error(error, PURPLE_PROTOCOL_MANAGER_DOMAIN, 0,
-		            _("protocol %s is not registered"), id);
-
-		ret = FALSE;
-	}
-
-	g_object_unref(protocol);
-
-	return ret;
+	return purple_protocol_manager_remove(manager, protocol, error);
 }
 
 PurpleProtocol *
@@ -340,3 +366,47 @@
 
 	return g_hash_table_get_values(manager->protocols);
 }
+
+gboolean
+purple_protocol_manager_remove(PurpleProtocolManager *manager,
+                               PurpleProtocol *protocol, GError **error)
+{
+	const gchar *id = NULL;
+	gboolean ret = FALSE;
+
+	g_return_val_if_fail(PURPLE_IS_PROTOCOL_MANAGER(manager), FALSE);
+	g_return_val_if_fail(PURPLE_IS_PROTOCOL(protocol), FALSE);
+
+	/* We need to hold a reference on the protocol as typically we will be
+	 * holding the only reference on the protocol when this is called and we
+	 * will need to pass it to the signal emission after it's removed from the
+	 * hash table that'll unref it.
+	 */
+	g_object_ref(protocol);
+
+	id = purple_protocol_get_id(protocol);
+
+	if(g_hash_table_remove(manager->protocols, id)) {
+		guint position;
+
+		if(g_ptr_array_find(manager->list, protocol, &position)) {
+			g_ptr_array_remove_index(manager->list, position);
+			g_list_model_items_changed(G_LIST_MODEL(manager), position, 1, 0);
+		}
+
+		g_signal_emit(G_OBJECT(manager), signals[SIG_REMOVED], 0, protocol);
+		g_signal_emit(G_OBJECT(manager), signals[SIG_UNREGISTERED], 0,
+		              protocol);
+
+		ret = TRUE;
+	} else {
+		g_set_error(error, PURPLE_PROTOCOL_MANAGER_DOMAIN, 0,
+		            _("protocol %s has not been added"), id);
+
+		ret = FALSE;
+	}
+
+	g_object_unref(protocol);
+
+	return ret;
+}
--- a/libpurple/purpleprotocolmanager.h	Fri Nov 01 00:28:41 2024 -0500
+++ b/libpurple/purpleprotocolmanager.h	Fri Nov 01 00:30:41 2024 -0500
@@ -56,7 +56,7 @@
  * PurpleProtocolManager:
  *
  * #PurpleProtocolManager keeps track of all protocols and emits signals when
- * protocols are registered and unregistered.
+ * protocols are added and removed.
  *
  * Since: 3.0
  */
@@ -74,6 +74,22 @@
 typedef void (*PurpleProtocolManagerForeachFunc)(PurpleProtocol *protocol, gpointer data);
 
 /**
+ * purple_protocol_manager_add:
+ * @manager: The #PurpleProtocolManager instance.
+ * @protocol: The #PurpleProtocol to add.
+ * @error: Return address for a #GError, or %NULL.
+ *
+ * Adds @protocol to @manager.
+ *
+ * Returns: %TRUE if @protocol was successfully added with @manager, %FALSE
+ *          otherwise.
+ *
+ * Since: 3.0
+ */
+PURPLE_AVAILABLE_IN_3_0
+gboolean purple_protocol_manager_add(PurpleProtocolManager *manager, PurpleProtocol *protocol, GError **error);
+
+/**
  * purple_protocol_manager_get_default:
  *
  * Gets the default #PurpleProtocolManager instance.
@@ -97,8 +113,10 @@
  *          %FALSE otherwise.
  *
  * Since: 3.0
+ *
+ * Deprecated: 3.0
  */
-PURPLE_AVAILABLE_IN_3_0
+PURPLE_DEPRECATED_FOR(purple_protocol_manager_add)
 gboolean purple_protocol_manager_register(PurpleProtocolManager *manager, PurpleProtocol *protocol, GError **error);
 
 /**
@@ -113,8 +131,10 @@
  *          %FALSE otherwise.
  *
  * Since: 3.0
+ *
+ * Deprecated: 3.0
  */
-PURPLE_AVAILABLE_IN_3_0
+PURPLE_DEPRECATED_FOR(purple_protocol_manager_remove)
 gboolean purple_protocol_manager_unregister(PurpleProtocolManager *manager, PurpleProtocol *protocol, GError **error);
 
 /**
@@ -140,8 +160,10 @@
  * Calls @func for each #PurpleProtocol that @manager knows about.
  *
  * Since: 3.0
+ *
+ * Deprecated: 3.0
  */
-PURPLE_AVAILABLE_IN_3_0
+PURPLE_DEPRECATED
 void purple_protocol_manager_foreach(PurpleProtocolManager *manager, PurpleProtocolManagerForeachFunc func, gpointer data);
 
 /**
@@ -155,9 +177,27 @@
  *          containing all of the #PurpleProtocols registered with @manager.
  *
  * Since: 3.0
+ *
+ * Deprecated: 3.0
+ */
+PURPLE_DEPRECATED
+GList *purple_protocol_manager_get_all(PurpleProtocolManager *manager);
+
+/**
+ * purple_protocol_manager_remove:
+ * @manager: The #PurpleProtocolManager instance.
+ * @protocol: The #PurpleProtocol to remove.
+ * @error: Return address for a #GError, or %NULL.
+ *
+ * Removes @protocol from @manager.
+ *
+ * Returns: %TRUE if @protocol was successfully removed from @manager, %FALSE
+ *          otherwise.
+ *
+ * Since: 3.0
  */
 PURPLE_AVAILABLE_IN_3_0
-GList *purple_protocol_manager_get_all(PurpleProtocolManager *manager);
+gboolean purple_protocol_manager_remove(PurpleProtocolManager *manager, PurpleProtocol *protocol, GError **error);
 
 G_END_DECLS
 

mercurial