pidgin/pidgin

Add a contact property to PurpleAccount

18 months ago, Gary Kramlich
29ebd938c592
Parents 8c277214ce6f
Children 24e5305ebcb6
Add a contact property to PurpleAccount

This contact can be used by the protocol to store additional information about
the account, including the user id, alias, etc. When the protocol connects, it
will be responsible for adding this contact to the ContactManager once it knows
that the id is correct.

This will also be used in the future when PurpleMessage gets migrated to using
PurpleContact's as well.

Testing Done:
Connected an IRCv3 and demo accounts.

Reviewed at https://reviews.imfreedom.org/r/2064/
--- a/libpurple/account.c Wed Nov 16 22:37:33 2022 -0600
+++ b/libpurple/account.c Wed Nov 16 23:43:30 2022 -0600
@@ -93,6 +93,8 @@
PurpleConnectionErrorInfo *current_error; /* Errors */
PurpleNotification *error_notification;
+
+ PurpleContact *contact;
} PurpleAccountPrivate;
typedef struct
@@ -118,6 +120,7 @@
PROP_REMEMBER_PASSWORD,
PROP_PROXY_INFO,
PROP_ERROR,
+ PROP_CONTACT,
PROP_LAST
};
@@ -761,6 +764,21 @@
g_checksum_free(checksum);
}
+ /* Create the contact for the account and bind our properties to it. */
+ account->contact = purple_contact_new(account, NULL);
+ /* Skip id for now as it's construct only and exposing it is more work than
+ * it's worth right now.
+ */
+#if 0
+ g_object_bind_property(account, "id", account->contact, "id",
+ G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
+#endif
+ g_object_bind_property(account, "username", account->contact, "username",
+ G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
+ g_object_bind_property(account, "private-alias",
+ account->contact, "display-name",
+ G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
+
g_object_get(object,
"username", &username,
"protocol-id", &protocol_id,
@@ -805,6 +823,7 @@
g_clear_object(&account->gc);
g_clear_object(&account->presence);
+ g_clear_object(&account->contact);
G_OBJECT_CLASS(purple_account_parent_class)->dispose(object);
}
@@ -857,11 +876,9 @@
{
GObjectClass *obj_class = G_OBJECT_CLASS(klass);
+ obj_class->constructed = purple_account_constructed;
obj_class->dispose = purple_account_dispose;
obj_class->finalize = purple_account_finalize;
- obj_class->constructed = purple_account_constructed;
-
- /* Setup properties */
obj_class->get_property = purple_account_get_property;
obj_class->set_property = purple_account_set_property;
@@ -952,6 +969,19 @@
PURPLE_TYPE_CONNECTION_ERROR_INFO,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
+ /**
+ * PurpleAccount:contact:
+ *
+ * The [class@Purple.Contact] that represents this account.
+ *
+ * Since: 3.0.0
+ */
+ properties[PROP_CONTACT] = g_param_spec_object(
+ "contact", "contact",
+ "The contact for this account",
+ PURPLE_TYPE_CONTACT,
+ G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
+
g_object_class_install_properties(obj_class, PROP_LAST, properties);
}
@@ -2299,3 +2329,10 @@
return account->require_password;
}
+
+PurpleContact *
+purple_account_get_contact(PurpleAccount *account) {
+ g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);
+
+ return account->contact;
+}
--- a/libpurple/account.h Wed Nov 16 22:37:33 2022 -0600
+++ b/libpurple/account.h Wed Nov 16 23:43:30 2022 -0600
@@ -39,6 +39,7 @@
#include "connection.h"
#include "group.h"
#include "purpleconnectionerrorinfo.h"
+#include "purplecontact.h"
#include "purpleprotocol.h"
#include "purpleproxyinfo.h"
#include "status.h"
@@ -947,6 +948,22 @@
*/
gboolean purple_account_get_require_password(PurpleAccount *account);
+/**
+ * purple_account_get_contact:
+ * @account: The instance.
+ *
+ * Gets the [class@Purple.Contact] for @account that represents the user of
+ * @account.
+ *
+ * This can be used by protocol plugins to store additional information about
+ * the account.
+ *
+ * Returns: (transfer none): The contact that represents @account.
+ *
+ * Since: 3.0.0
+ */
+PurpleContact *purple_account_get_contact(PurpleAccount *account);
+
G_END_DECLS
#endif /* PURPLE_ACCOUNT_H */
--- a/libpurple/purplecontact.c Wed Nov 16 22:37:33 2022 -0600
+++ b/libpurple/purplecontact.c Wed Nov 16 23:43:30 2022 -0600
@@ -72,21 +72,6 @@
}
}
-static void
-purple_contact_set_id(PurpleContact *contact, const gchar *id) {
- g_return_if_fail(PURPLE_IS_CONTACT(contact));
-
- g_free(contact->id);
-
- if(id != NULL) {
- contact->id = g_strdup(id);
- } else {
- contact->id = g_uuid_string_random();
- }
-
- g_object_notify_by_pspec(G_OBJECT(contact), properties[PROP_ID]);
-}
-
/******************************************************************************
* GObject Implementation
*****************************************************************************/
@@ -234,7 +219,7 @@
"id", "id",
"The id of the contact",
NULL,
- G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
/**
* PurpleContact:account:
@@ -389,6 +374,16 @@
return contact->id;
}
+void
+purple_contact_set_id(PurpleContact *contact, const gchar *id) {
+ g_return_if_fail(PURPLE_IS_CONTACT(contact));
+
+ g_free(contact->id);
+ contact->id = g_strdup(id);
+
+ g_object_notify_by_pspec(G_OBJECT(contact), properties[PROP_ID]);
+}
+
const gchar *
purple_contact_get_username(PurpleContact *contact) {
g_return_val_if_fail(PURPLE_IS_CONTACT(contact), NULL);
--- a/libpurple/purplecontact.h Wed Nov 16 22:37:33 2022 -0600
+++ b/libpurple/purplecontact.h Wed Nov 16 23:43:30 2022 -0600
@@ -28,7 +28,6 @@
#include <gdk-pixbuf/gdk-pixbuf.h>
-#include <libpurple/account.h>
#include <libpurple/purplepresence.h>
#include <libpurple/purpletags.h>
@@ -56,6 +55,7 @@
PURPLE_CONTACT_PERMISSION_DENY,
} PurpleContactPermission;
+#include <libpurple/account.h>
#include <libpurple/purpleperson.h>
/**
@@ -109,6 +109,20 @@
const gchar *purple_contact_get_id(PurpleContact *contact);
/**
+ * purple_contact_set_id:
+ * @contact: The instance.
+ * @id: The new identifier.
+ *
+ * Sets the identifier of @contact to @id. Note, this should be used rarely if
+ * at all. The main intent of this, is for protocols to update the id of
+ * [property@Purple.Account:contact] when an account is connected if it is
+ * missing.
+ *
+ * Since: 3.0.0
+ */
+void purple_contact_set_id(PurpleContact *contact, const char *id);
+
+/**
* purple_contact_get_username:
* @contact: The instance.
*