# HG changeset patch
# Node ID 87882e8ead5ca8c32215453e3eb9d1aa98ad7d58
# Parent  b8ced4292cfff9de278659dff085ab3680e00360
diff --git a/pidgin/pidgincontactlist.c b/pidgin/pidgincontactlist.c
--- a/pidgin/pidgincontactlist.c
+++ b/pidgin/pidgincontactlist.c
@@ -206,6 +206,27 @@
 }
 
 static void
+pidgin_contact_list_create_dm_cb(GObject *source, GAsyncResult *result,
+                                 G_GNUC_UNUSED gpointer data)
+{
+	PurpleConversation *conversation = NULL;
+	GError *error = NULL;
+
+	conversation = purple_contact_create_dm_finish(PURPLE_CONTACT(source),
+	                                               result, &error);
+
+	if(error != NULL) {
+		g_warning("Failed to create dm for contact: %s", error->message);
+
+		g_clear_error(&error);
+
+		return;
+	}
+
+	purple_conversation_present(conversation);
+}
+
+static void
 pidgin_contact_list_activate_cb(GtkListView *self, guint position,
                                 G_GNUC_UNUSED gpointer data)
 {
@@ -226,8 +247,11 @@
 
 	info = purple_person_get_priority_contact_info(person);
 	conversation = purple_contact_find_dm(PURPLE_CONTACT(info));
-	if(!PURPLE_IS_CONVERSATION(conversation)) {
-		purple_contact_create_dm_async(PURPLE_CONTACT(info), NULL, NULL, NULL);
+	if(PURPLE_IS_CONVERSATION(conversation)) {
+		purple_conversation_present(conversation);
+	} else {
+		purple_contact_create_dm_async(PURPLE_CONTACT(info), NULL,
+		                               pidgin_contact_list_create_dm_cb, NULL);
 	}
 
 	g_clear_object(&person);
diff --git a/pidgin/pidgindisplaywindow.c b/pidgin/pidgindisplaywindow.c
--- a/pidgin/pidgindisplaywindow.c
+++ b/pidgin/pidgindisplaywindow.c
@@ -47,9 +47,11 @@
 
 	GtkWidget *plugin_list;
 
-	GListModel *base_model;
+	GListStore *base_model;
 	GListModel *selection_model;
 
+	PidginDisplayItem *conversations_item;
+
 	GListStore *conversation_model;
 };
 
@@ -256,6 +258,13 @@
 	pidgin_display_window_remove(window, conversation);
 }
 
+static void
+pidgin_display_window_conversation_present_cb(PurpleConversation *conversation,
+                                              gpointer data)
+{
+	pidgin_display_window_select(data, conversation);
+}
+
 /******************************************************************************
  * GObject Implementation
  *****************************************************************************/
@@ -285,7 +294,8 @@
 	g_object_ref(window->selection_model);
 
 	/* Setup the tree list model. */
-	tree_model = gtk_tree_list_model_new(window->base_model, FALSE, TRUE,
+	tree_model = gtk_tree_list_model_new(G_LIST_MODEL(window->base_model),
+	                                     FALSE, TRUE,
 	                                     (GtkTreeListModelCreateModelFunc)pidgin_display_window_create_model,
 	                                     window, NULL);
 
@@ -362,6 +372,8 @@
 	gtk_widget_class_bind_template_child(widget_class, PidginDisplayWindow,
 	                                     selection_model);
 	gtk_widget_class_bind_template_child(widget_class, PidginDisplayWindow,
+	                                     conversations_item);
+	gtk_widget_class_bind_template_child(widget_class, PidginDisplayWindow,
 	                                     conversation_model);
 
 	gtk_widget_class_bind_template_callback(widget_class,
@@ -461,6 +473,10 @@
 			                       item, "title",
 			                       G_BINDING_SYNC_CREATE);
 
+			g_signal_connect_object(purple_conversation, "present",
+			                        G_CALLBACK(pidgin_display_window_conversation_present_cb),
+			                        window, G_CONNECT_DEFAULT);
+
 			g_list_store_append(window->conversation_model, item);
 			g_clear_object(&item);
 		}
@@ -497,6 +513,10 @@
 	g_clear_object(&item);
 
 	if(found) {
+		g_signal_handlers_disconnect_by_func(conversation,
+		                                     G_CALLBACK(pidgin_display_window_conversation_present_cb),
+		                                     window);
+
 		g_list_store_remove(window->conversation_model, position);
 	}
 }
@@ -534,13 +554,43 @@
 pidgin_display_window_select(PidginDisplayWindow *window,
                              PurpleConversation *conversation)
 {
-	/* TODO: This is used by the unity and gestures plugins, but I'm really not
-	 * sure how to make this work yet without some hard-coding or something, so
-	 * I'm opting to stub it out for now.
-	 */
+	PidginDisplayItem *item = NULL;
+	guint position = 0;
+	gboolean found = FALSE;
+	gchar *id = NULL;
 
 	g_return_if_fail(PIDGIN_IS_DISPLAY_WINDOW(window));
 	g_return_if_fail(PURPLE_IS_CONVERSATION(conversation));
+
+	/* Create a wrapper item for our find function. */
+	id = g_uuid_string_random();
+	item = g_object_new(PIDGIN_TYPE_DISPLAY_ITEM, "id", id, NULL);
+	g_free(id);
+	g_object_set_data(G_OBJECT(item), "conversation", conversation);
+
+	found = g_list_store_find_with_equal_func(window->conversation_model,
+	                                          item,
+	                                          pidgin_display_window_find_conversation,
+	                                          &position);
+
+	if(found) {
+		/* Figure out where the conversations_item is because we need to add to
+		 * that position.
+		 */
+		guint conversations_position = 0;
+		guint real_position = 0;
+
+		g_list_store_find(window->base_model, window->conversations_item,
+		                  &conversations_position);
+
+		/* Since this is a nested model, we need to increment by one to get to
+		 * the correct item.
+		 */
+		real_position = conversations_position + position + 1;
+
+		gtk_single_selection_set_selected(GTK_SINGLE_SELECTION(window->selection_model),
+		                                  real_position);
+	}
 }
 
 void
diff --git a/pidgin/resources/display-window.ui b/pidgin/resources/display-window.ui
--- a/pidgin/resources/display-window.ui
+++ b/pidgin/resources/display-window.ui
@@ -105,7 +105,7 @@
       </object>
     </child>
     <child>
-      <object class="PidginDisplayItem">
+      <object class="PidginDisplayItem" id="conversations_item">
         <property name="icon-name">user-available-symbolic</property>
         <property name="id">__conversations__</property>
         <property name="title" translatable="1">Conversations</property>
