]> git.0d.be Git - empathy.git/blobdiff - src/empathy-invite-participant-dialog.c
add an header bar to the main window
[empathy.git] / src / empathy-invite-participant-dialog.c
index f6471fbc71bc030b44a9d210bb62e2c34b69d545..437d8e6b7358bab9242923906201f0c59df889d3 100644 (file)
@@ -9,14 +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-contact-chooser.h>
-#include <libempathy-gtk/empathy-individual-view.h>
-#include <libempathy-gtk/empathy-ui-utils.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);
@@ -95,6 +95,90 @@ selection_changed_cb (GtkWidget *treeview,
   gtk_widget_set_sensitive (self->priv->invite_button, selected != NULL);
 }
 
+static void
+activate_cb (GtkWidget *chooser,
+    EmpathyInviteParticipantDialog *self)
+{
+  gtk_dialog_response (GTK_DIALOG (self), GTK_RESPONSE_ACCEPT);
+}
+
+/* Return the TpContact of @individual which is on the same connection as the
+ * EmpathyTpChat */
+static TpContact *
+get_tp_contact_for_chat (EmpathyInviteParticipantDialog *self,
+    FolksIndividual *individual)
+{
+  TpConnection *chat_conn;
+
+  chat_conn = tp_channel_get_connection (TP_CHANNEL (self->priv->tp_chat));
+  if (chat_conn == NULL)
+    return NULL;
+
+  return empathy_get_tp_contact_for_individual (individual, chat_conn);
+}
+
+static gboolean
+filter_individual (EmpathyContactChooser *chooser,
+    FolksIndividual *individual,
+    gboolean is_online,
+    gboolean searching,
+    gpointer user_data)
+{
+  EmpathyInviteParticipantDialog *self = user_data;
+  GList *members, *l;
+  TpContact *contact;
+  gboolean display = TRUE;
+
+  /* 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)
+    return FALSE;
+
+  /* Filter out contacts which are already in the 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;
+      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));
+
+      if (owner == NULL)
+        owner = empathy_contact_get_tp_contact (member);
+
+      if (owner == contact)
+        {
+          display = FALSE;
+          break;
+        }
+    }
+
+  g_list_free_full (members, g_object_unref);
+
+  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
 invite_participant_dialog_constructed (GObject *object)
 {
@@ -121,12 +205,18 @@ invite_participant_dialog_constructed (GObject *object)
   gtk_dialog_add_button (dialog, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
 
   /* contact chooser */
-  self->priv->chooser = empathy_contact_chooser_new (self->priv->tp_chat);
+  self->priv->chooser = empathy_contact_chooser_new ();
+
+  empathy_contact_chooser_set_filter_func (
+      EMPATHY_CONTACT_CHOOSER (self->priv->chooser), filter_individual, self);
+
   gtk_box_pack_start (GTK_BOX (content), self->priv->chooser, TRUE, TRUE, 6);
   gtk_widget_show (self->priv->chooser);
 
   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);
@@ -135,8 +225,17 @@ invite_participant_dialog_constructed (GObject *object)
   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
@@ -188,6 +287,16 @@ TpContact *
 empathy_invite_participant_dialog_get_selected (
     EmpathyInviteParticipantDialog *self)
 {
-  return empathy_contact_chooser_get_selected (
+  FolksIndividual *individual;
+  TpContact *contact;
+
+  individual = empathy_contact_chooser_dup_selected (
       EMPATHY_CONTACT_CHOOSER (self->priv->chooser));
+  if (individual == NULL)
+    return NULL;
+
+  contact = get_tp_contact_for_chat (self, individual);
+
+  g_object_unref (individual);
+  return contact;
 }