qulogic/pidgin

Add purple_contact_find_dm

5 months ago, Gary Kramlich
eca0c08ed2a3
Parents 82fffff36df6
Children c4ea929fde6f
Add purple_contact_find_dm

This is a common use case and is a bit of code, so it's good to have a helper
like this.

Testing Done:
Created dm's via the contact list and made sure they worked. The previous update /r/2851 broke instant messages for all protocol plugins that are still using PurpleIMConversation which need to be updated anyways.

Reviewed at https://reviews.imfreedom.org/r/2853/
--- a/libpurple/purplecontact.c Fri Dec 01 01:09:14 2023 -0600
+++ b/libpurple/purplecontact.c Fri Dec 01 02:18:28 2023 -0600
@@ -18,8 +18,7 @@
#include "purplecontact.h"
-#include "purpleenums.h"
-#include "util.h"
+#include "purpleconversationmanager.h"
struct _PurpleContact {
PurpleContactInfo parent;
@@ -141,3 +140,37 @@
return contact->account;
}
+
+PurpleConversation *
+purple_contact_find_dm(PurpleContact *contact, gboolean create) {
+ PurpleConversation *conversation = NULL;
+ PurpleConversationManager *manager = NULL;
+ const char *name = NULL;
+
+ g_return_val_if_fail(PURPLE_IS_CONTACT(contact), NULL);
+
+ manager = purple_conversation_manager_get_default();
+ name = purple_contact_info_get_username(PURPLE_CONTACT_INFO(contact));
+ conversation = purple_conversation_manager_find_im(manager,
+ contact->account,
+ name);
+
+ if(PURPLE_IS_CONVERSATION(conversation)) {
+ return conversation;
+ }
+
+ if(create) {
+ conversation = g_object_new(
+ PURPLE_TYPE_CONVERSATION,
+ "account", contact->account,
+ "name", name,
+ "type", PurpleConversationTypeDM,
+ NULL);
+ purple_conversation_manager_register(manager, conversation);
+
+ /* The manager holds a reference, so we're just returning that. */
+ g_object_unref(conversation);
+ }
+
+ return conversation;
+}
--- a/libpurple/purplecontact.h Fri Dec 01 01:09:14 2023 -0600
+++ b/libpurple/purplecontact.h Fri Dec 01 02:18:28 2023 -0600
@@ -40,6 +40,7 @@
PurpleContactInfo)
#include "purpleaccount.h"
+#include "purpleconversation.h"
/**
* PurpleContact:
@@ -79,6 +80,25 @@
PURPLE_AVAILABLE_IN_3_0
PurpleAccount *purple_contact_get_account(PurpleContact *contact);
+/**
+ * purple_contact_find_dm:
+ * @contact: The instance.
+ * @create: Whether or not to create a new DM if one can't be found.
+ *
+ * Attempts to find a conversation for @contact in the default
+ * [class@ContactManager] by using @contact's username.
+ *
+ * If no existing direct messages exists for @contact and @create is %TRUE,
+ * then a new direct message will be created and registered in the default
+ * [class@ConversationManager].
+ *
+ * Returns: (transfer none) (nullable): The conversation or %NULL.
+ *
+ * Since: 3.0.0
+ */
+PURPLE_AVAILABLE_IN_3_0
+PurpleConversation *purple_contact_find_dm(PurpleContact *contact, gboolean create);
+
G_END_DECLS
#endif /* PURPLE_CONTACT_H */
--- a/pidgin/pidgincontactlist.c Fri Dec 01 01:09:14 2023 -0600
+++ b/pidgin/pidgincontactlist.c Fri Dec 01 02:18:28 2023 -0600
@@ -191,13 +191,9 @@
pidgin_contact_list_activate_cb(GtkListView *self, guint position,
G_GNUC_UNUSED gpointer data)
{
- PurpleAccount *account = NULL;
PurpleContactInfo *info = NULL;
- PurpleConversation *conversation = NULL;
- PurpleConversationManager *manager = NULL;
PurplePerson *person = NULL;
GtkSelectionModel *model = NULL;
- const char *name = NULL;
model = gtk_list_view_get_model(self);
@@ -210,22 +206,7 @@
}
info = purple_person_get_priority_contact_info(person);
- account = purple_contact_get_account(PURPLE_CONTACT(info));
- name = purple_contact_info_get_username(info);
-
- manager = purple_conversation_manager_get_default();
- conversation = purple_conversation_manager_find_im(manager, account, name);
-
- if(!PURPLE_IS_CONVERSATION(conversation)) {
- conversation = g_object_new(
- PURPLE_TYPE_CONVERSATION,
- "account", account,
- "name", name,
- "type", PurpleConversationTypeDM,
- NULL);
- purple_conversation_manager_register(manager, conversation);
- g_clear_object(&conversation);
- }
+ purple_contact_find_dm(PURPLE_CONTACT(info), TRUE);
g_clear_object(&person);
}