--- 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; +}