pidgin/pidgin

Parents d039c29040d3
Children fefb84b37fc4
Add PurpleAccount:connected and PurpleAccount:disconnected signals

This will be propagated by PurpleAccountManager at a later time, but were
implemented separately.

Testing Done:
Temporarily added `g_warnings` where the signal emissions where to verify they were being emitted.

I didn't add unit tests for this as there's a lot of boiler plate for that which will hopefully be changing soon.

Reviewed at https://reviews.imfreedom.org/r/2439/
--- a/libpurple/account.c Tue May 23 00:18:04 2023 -0500
+++ b/libpurple/account.c Tue May 23 00:21:15 2023 -0500
@@ -111,6 +111,8 @@
enum {
SIG_SETTING_CHANGED,
+ SIG_CONNECTED,
+ SIG_DISCONNECTED,
N_SIGNALS
};
static guint signals[N_SIGNALS] = {0, };
@@ -418,6 +420,23 @@
}
}
+static void
+purple_account_connection_state_cb(GObject *obj,
+ G_GNUC_UNUSED GParamSpec *pspec,
+ gpointer data)
+{
+ PurpleAccount *account = data;
+ PurpleConnection *connection = PURPLE_CONNECTION(obj);
+ PurpleConnectionState state = PURPLE_CONNECTION_STATE_DISCONNECTED;
+
+ state = purple_connection_get_state(connection);
+ if(state == PURPLE_CONNECTION_STATE_CONNECTED) {
+ g_signal_emit(account, signals[SIG_CONNECTED], 0);
+ } else if(state == PURPLE_CONNECTION_STATE_DISCONNECTED) {
+ g_signal_emit(account, signals[SIG_DISCONNECTED], 0);
+ }
+}
+
/******************************************************************************
* XmlNode Helpers
*****************************************************************************/
@@ -927,6 +946,48 @@
G_TYPE_NONE,
1,
G_TYPE_STRING);
+
+ /**
+ * PurpleAccount::connected:
+ * @account: The account instance.
+ *
+ * This is emitted when the [property@Account:connection]'s
+ * [property@Connection:state] has changed to
+ * %PURPLE_CONNECTION_STATE_CONNECTED.
+ *
+ * Since: 3.0.0
+ */
+ signals[SIG_CONNECTED] = g_signal_new_class_handler(
+ "connected",
+ G_OBJECT_CLASS_TYPE(klass),
+ G_SIGNAL_RUN_LAST,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ G_TYPE_NONE,
+ 0);
+
+ /**
+ * PurpleAccount::disconnected:
+ * @account: The account instance.
+ *
+ * This is emitted when the [property@Account:connection]'s
+ * [property@Connection:state] has changed to
+ * %PURPLE_CONNECTION_STATE_DISCONNECTED.
+ *
+ * Since: 3.0.0
+ */
+ signals[SIG_DISCONNECTED] = g_signal_new_class_handler(
+ "disconnected",
+ G_OBJECT_CLASS_TYPE(klass),
+ G_SIGNAL_RUN_LAST,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ G_TYPE_NONE,
+ 0);
}
/******************************************************************************
@@ -1223,9 +1284,34 @@
purple_account_set_connection(PurpleAccount *account, PurpleConnection *gc) {
g_return_if_fail(PURPLE_IS_ACCOUNT(account));
+ /* If we got the same pointer, bail. */
+ if(account->gc == gc) {
+ return;
+ }
+
+ /* Remove our old signal handler. */
+ if(PURPLE_IS_CONNECTION(account->gc)) {
+ g_signal_handlers_disconnect_by_func(account->gc,
+ purple_account_connection_state_cb,
+ account);
+ }
+
if(g_set_object(&account->gc, gc)) {
+ if(PURPLE_IS_CONNECTION(account->gc)) {
+ g_signal_connect(account->gc, "notify::state",
+ G_CALLBACK(purple_account_connection_state_cb),
+ account);
+ }
+
g_object_notify_by_pspec(G_OBJECT(account),
properties[PROP_CONNECTION]);
+ } else {
+ /* If the set didn't work, restore our old signal. */
+ if(PURPLE_IS_CONNECTION(account->gc)) {
+ g_signal_connect(account->gc, "notify::state",
+ G_CALLBACK(purple_account_connection_state_cb),
+ account);
+ }
}
}