]> git.0d.be Git - empathy.git/blobdiff - src/empathy-invite-participant-dialog.c
roster-window: fix crash if empathy_chatroom_manager_find() failed
[empathy.git] / src / empathy-invite-participant-dialog.c
index 379b23121029c2bf67fc904301f436cf25831020..437d8e6b7358bab9242923906201f0c59df889d3 100644 (file)
@@ -9,12 +9,14 @@
  *    Danielle Madeley <danielle.madeley@collabora.co.uk>
  */
 
-#include <glib/gi18n.h>
-#include <folks/folks-telepathy.h>
-
+#include "config.h"
 #include "empathy-invite-participant-dialog.h"
 
-#include <libempathy-gtk/empathy-individual-view.h>
+#include <glib/gi18n.h>
+#include <telepathy-glib/telepathy-glib-dbus.h>
+
+#include "empathy-contact-chooser.h"
+#include "empathy-utils.h"
 
 G_DEFINE_TYPE (EmpathyInviteParticipantDialog,
     empathy_invite_participant_dialog, GTK_TYPE_DIALOG);
@@ -28,9 +30,7 @@ struct _EmpathyInviteParticipantDialogPrivate
 {
   EmpathyTpChat *tp_chat;
 
-  EmpathyIndividualStore *store;
-  EmpathyIndividualView *view;
-
+  GtkWidget *chooser;
   GtkWidget *invite_button;
 };
 
@@ -82,43 +82,24 @@ invite_participant_dialog_dispose (GObject *object)
     object;
 
   tp_clear_object (&self->priv->tp_chat);
-  tp_clear_object (&self->priv->store);
 
   G_OBJECT_CLASS (empathy_invite_participant_dialog_parent_class)->dispose (
       object);
 }
 
 static void
-empathy_invite_participant_dialog_class_init (
-    EmpathyInviteParticipantDialogClass *klass)
+selection_changed_cb (GtkWidget *treeview,
+    FolksIndividual *selected,
+    EmpathyInviteParticipantDialog *self)
 {
-  GObjectClass *object_class = G_OBJECT_CLASS (klass);
-
-  object_class->get_property = invite_participant_dialog_get_property;
-  object_class->set_property = invite_participant_dialog_set_property;
-  object_class->dispose = invite_participant_dialog_dispose;
-
-  g_type_class_add_private (object_class,
-      sizeof (EmpathyInviteParticipantDialogPrivate));
-
-  g_object_class_install_property (object_class,
-      PROP_TP_CHAT,
-      g_param_spec_object ("tp-chat", "EmpathyTpChat", "EmpathyTpChat",
-          EMPATHY_TYPE_TP_CHAT,
-          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
+  gtk_widget_set_sensitive (self->priv->invite_button, selected != NULL);
 }
 
 static void
-view_selection_changed_cb (GtkWidget *treeview,
+activate_cb (GtkWidget *chooser,
     EmpathyInviteParticipantDialog *self)
 {
-  FolksIndividual *individual;
-
-  individual = empathy_individual_view_dup_selected (self->priv->view);
-
-  gtk_widget_set_sensitive (self->priv->invite_button, individual != NULL);
-
-  g_object_unref (individual);
+  gtk_dialog_response (GTK_DIALOG (self), GTK_RESPONSE_ACCEPT);
 }
 
 /* Return the TpContact of @individual which is on the same connection as the
@@ -127,111 +108,86 @@ static TpContact *
 get_tp_contact_for_chat (EmpathyInviteParticipantDialog *self,
     FolksIndividual *individual)
 {
-  GList *personas, *l;
   TpConnection *chat_conn;
 
-  chat_conn = empathy_tp_chat_get_connection (self->priv->tp_chat);
-
-  personas = folks_individual_get_personas (individual);
-
-  for (l = personas; l != NULL; l = g_list_next (l))
-    {
-      TpfPersona *persona = l->data;
-      TpContact *contact;
-      TpConnection *contact_conn;
-
-      if (!TPF_IS_PERSONA (persona))
-        continue;
-
-      contact = tpf_persona_get_contact (persona);
-      if (contact == NULL)
-        continue;
-
-      contact_conn = tp_contact_get_connection (contact);
-
-      if (!tp_strdiff (tp_proxy_get_object_path (contact_conn),
-            tp_proxy_get_object_path (chat_conn)))
-        return contact;
-    }
+  chat_conn = tp_channel_get_connection (TP_CHANNEL (self->priv->tp_chat));
+  if (chat_conn == NULL)
+    return NULL;
 
-  return NULL;
+  return empathy_get_tp_contact_for_individual (individual, chat_conn);
 }
 
 static gboolean
-filter_func (GtkTreeModel *model,
-    GtkTreeIter *iter,
+filter_individual (EmpathyContactChooser *chooser,
+    FolksIndividual *individual,
+    gboolean is_online,
+    gboolean searching,
     gpointer user_data)
 {
   EmpathyInviteParticipantDialog *self = user_data;
-  FolksIndividual *individual;
-  TpContact *contact;
-  gboolean is_online;
   GList *members, *l;
-  gboolean display = FALSE;
-  TpChannel *channel;
-
-  gtk_tree_model_get (model, iter,
-      EMPATHY_INDIVIDUAL_STORE_COL_INDIVIDUAL, &individual,
-      EMPATHY_INDIVIDUAL_STORE_COL_IS_ONLINE, &is_online,
-      -1);
+  TpContact *contact;
+  gboolean display = TRUE;
 
-  if (individual == NULL || !is_online)
-    goto out;
+  /* Filter out offline contacts if we are not searching */
+  if (!searching && !is_online)
+    return FALSE;
 
   /* Filter out individuals not having a persona on the same connection as the
    * EmpathyTpChat. */
   contact = get_tp_contact_for_chat (self, individual);
 
   if (contact == NULL)
-    goto out;
+    return FALSE;
 
   /* Filter out contacts which are already in the chat */
-  members = empathy_contact_list_get_members (EMPATHY_CONTACT_LIST (
-        self->priv->tp_chat));
-
-  display = TRUE;
-  channel = empathy_tp_chat_get_channel (self->priv->tp_chat);
+  members = empathy_tp_chat_get_members (self->priv->tp_chat);
 
   for (l = members; l != NULL; l = g_list_next (l))
     {
       EmpathyContact *member = l->data;
-      TpHandle handle;
+      TpContact *owner;
+
+      /* Try to get the non-channel specific contact. */
+      owner = tp_channel_group_get_contact_owner (
+          TP_CHANNEL (self->priv->tp_chat),
+          empathy_contact_get_tp_contact (member));
 
-      /* Try to get the non-channel specific handle. */
-      handle = tp_channel_group_get_handle_owner (channel,
-          empathy_contact_get_handle (member));
-      if (handle == 0)
-        handle = empathy_contact_get_handle (member);
+      if (owner == NULL)
+        owner = empathy_contact_get_tp_contact (member);
 
-      if (handle == tp_contact_get_handle (contact))
+      if (owner == contact)
         {
           display = FALSE;
           break;
         }
     }
 
-  g_list_foreach (members, (GFunc) g_object_unref, NULL);
-  g_list_free (members);
+  g_list_free_full (members, g_object_unref);
 
-out:
-  tp_clear_object (&individual);
   return display;
 }
 
+static gboolean
+has_contact_list (EmpathyInviteParticipantDialog *self)
+{
+  TpConnection *conn;
+
+  conn = tp_channel_get_connection (TP_CHANNEL (self->priv->tp_chat));
+
+  return tp_proxy_has_interface_by_id (conn,
+      TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_LIST);
+}
+
 static void
-empathy_invite_participant_dialog_init (EmpathyInviteParticipantDialog *self)
+invite_participant_dialog_constructed (GObject *object)
 {
+  EmpathyInviteParticipantDialog *self =
+    (EmpathyInviteParticipantDialog *) object;
   GtkDialog *dialog = GTK_DIALOG (self);
   GtkWidget *label;
   char *str;
   GtkWidget *content;
-  EmpathyIndividualManager *mgr;
-  GtkTreeSelection *selection;
-  GtkWidget *scroll;
-
-  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (
-      self, EMPATHY_TYPE_INVITE_PARTICIPANT_DIALOG,
-      EmpathyInviteParticipantDialogPrivate);
 
   content = gtk_dialog_get_content_area (dialog);
 
@@ -248,31 +204,19 @@ empathy_invite_participant_dialog_init (EmpathyInviteParticipantDialog *self)
 
   gtk_dialog_add_button (dialog, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
 
-  /* Add the treeview */
-  mgr = empathy_individual_manager_dup_singleton ();
-  self->priv->store = empathy_individual_store_new (mgr);
-  g_object_unref (mgr);
-
-  empathy_individual_store_set_show_groups (self->priv->store, FALSE);
-
-  self->priv->view = empathy_individual_view_new (self->priv->store,
-      EMPATHY_INDIVIDUAL_VIEW_FEATURE_NONE, EMPATHY_INDIVIDUAL_FEATURE_NONE);
-
-  empathy_individual_view_set_custom_filter (self->priv->view,
-      filter_func, self);
-
-  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self->priv->view));
+  /* contact chooser */
+  self->priv->chooser = empathy_contact_chooser_new ();
 
-  g_signal_connect (selection, "changed",
-      G_CALLBACK (view_selection_changed_cb), self);
+  empathy_contact_chooser_set_filter_func (
+      EMPATHY_CONTACT_CHOOSER (self->priv->chooser), filter_individual, self);
 
-  scroll = gtk_scrolled_window_new (NULL, NULL);
+  gtk_box_pack_start (GTK_BOX (content), self->priv->chooser, TRUE, TRUE, 6);
+  gtk_widget_show (self->priv->chooser);
 
-  gtk_container_add (GTK_CONTAINER (scroll), GTK_WIDGET (self->priv->view));
-
-  gtk_box_pack_start (GTK_BOX (content), scroll, TRUE, TRUE, 6);
-  gtk_widget_show (GTK_WIDGET (self->priv->view));
-  gtk_widget_show (scroll);
+  g_signal_connect (self->priv->chooser, "selection-changed",
+      G_CALLBACK (selection_changed_cb), self);
+  g_signal_connect (self->priv->chooser, "activate",
+      G_CALLBACK (activate_cb), self);
 
   self->priv->invite_button = gtk_dialog_add_button (dialog, _("Invite"),
       GTK_RESPONSE_ACCEPT);
@@ -281,8 +225,46 @@ empathy_invite_participant_dialog_init (EmpathyInviteParticipantDialog *self)
   gtk_window_set_title (GTK_WINDOW (self), _("Invite Participant"));
   gtk_window_set_role (GTK_WINDOW (self), "invite_participant");
 
-  /* Set a default height so a few contacts are displayed */
-  gtk_window_set_default_size (GTK_WINDOW (self), -1, 400);
+  if (has_contact_list (self))
+    {
+      /* Set a default height so a few contacts are displayed */
+      gtk_window_set_default_size (GTK_WINDOW (self), -1, 400);
+    }
+  else
+    {
+      /* No need to display an empty treeview (ie IRC) */
+      empathy_contact_chooser_show_tree_view (
+          EMPATHY_CONTACT_CHOOSER (self->priv->chooser), FALSE);
+    }
+}
+
+static void
+empathy_invite_participant_dialog_class_init (
+    EmpathyInviteParticipantDialogClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->get_property = invite_participant_dialog_get_property;
+  object_class->set_property = invite_participant_dialog_set_property;
+  object_class->constructed = invite_participant_dialog_constructed;
+  object_class->dispose = invite_participant_dialog_dispose;
+
+  g_type_class_add_private (object_class,
+      sizeof (EmpathyInviteParticipantDialogPrivate));
+
+  g_object_class_install_property (object_class,
+      PROP_TP_CHAT,
+      g_param_spec_object ("tp-chat", "EmpathyTpChat", "EmpathyTpChat",
+          EMPATHY_TYPE_TP_CHAT,
+          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
+}
+
+static void
+empathy_invite_participant_dialog_init (EmpathyInviteParticipantDialog *self)
+{
+  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (
+      self, EMPATHY_TYPE_INVITE_PARTICIPANT_DIALOG,
+      EmpathyInviteParticipantDialogPrivate);
 }
 
 GtkWidget *
@@ -308,7 +290,8 @@ empathy_invite_participant_dialog_get_selected (
   FolksIndividual *individual;
   TpContact *contact;
 
-  individual = empathy_individual_view_dup_selected (self->priv->view);
+  individual = empathy_contact_chooser_dup_selected (
+      EMPATHY_CONTACT_CHOOSER (self->priv->chooser));
   if (individual == NULL)
     return NULL;