]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-contact-search-dialog.c
Merge branch 'gnome-3-8'
[empathy.git] / libempathy-gtk / empathy-contact-search-dialog.c
index 96cb64645091a64500302e10536c24825af034bc..2696f5abbeb2fdb9a8af808e434745175bbeae2d 100644 (file)
  *     Emilio Pozuelo Monfort <emilio.pozuelo@collabora.co.uk>
  */
 
-#include <glib/gi18n.h>
-
-#include <telepathy-glib/telepathy-glib.h>
+#include "config.h"
+#include "empathy-contact-search-dialog.h"
 
-#include <libempathy/empathy-contact-manager.h>
-#include <libempathy/empathy-tp-contact-factory.h>
+#include <glib/gi18n-lib.h>
 
-#include <libempathy-gtk/empathy-account-chooser.h>
-#include <libempathy-gtk/empathy-cell-renderer-text.h>
+#include "empathy-account-chooser.h"
+#include "empathy-cell-renderer-activatable.h"
+#include "empathy-cell-renderer-text.h"
+#include "empathy-client-factory.h"
+#include "empathy-images.h"
+#include "empathy-individual-information-dialog.h"
+#include "empathy-utils.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
-#include <libempathy/empathy-debug.h>
+#include "empathy-debug.h"
 
 #include "empathy-contact-search-dialog.h"
 
@@ -69,6 +72,9 @@ struct _EmpathyContactSearchDialogPrivate
   GtkWidget *no_contact_found;
   GtkWidget *search_entry;
   /* GtkWidget *server_entry; */
+  GtkWidget *message;
+  GtkWidget *message_window;
+  GtkWidget *message_label;
 };
 
 static void
@@ -104,12 +110,16 @@ on_searcher_reset (GObject *source_object,
   search = g_hash_table_new (g_str_hash, g_str_equal);
 
   search_criteria = gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
-  g_hash_table_insert (search, "fn", (gpointer) search_criteria);
+
+  if (tp_strv_contains (tp_contact_search_get_search_keys (searcher), ""))
+    g_hash_table_insert (search, "", (gpointer) search_criteria);
+  else
+    g_hash_table_insert (search, "fn", (gpointer) search_criteria);
 
   gtk_list_store_clear (priv->store);
   tp_contact_search_start (priv->searcher, search);
 
-  g_hash_table_destroy (search);
+  g_hash_table_unref (search);
 }
 
 static void
@@ -125,21 +135,28 @@ empathy_contact_search_dialog_do_search (EmpathyContactSearchDialog *self)
 }
 
 static void
-on_get_contact_factory_get_from_id_cb (TpConnection *connection,
-    EmpathyContact *contact,
-    const GError *error,
-    gpointer user_data,
-    GObject *object)
+on_get_contact_factory_get_from_id_cb (GObject *source,
+    GAsyncResult *result,
+    gpointer user_data)
 {
-    EmpathyContactManager *manager = empathy_contact_manager_dup_singleton ();
+  gchar *message = user_data;
+  GError *error = NULL;
+  EmpathyContact *contact;
+
+  contact = empathy_client_factory_dup_contact_by_id_finish (
+      EMPATHY_CLIENT_FACTORY (source), result, &error);
+  if (contact == NULL)
+    {
+      g_warning ("Error while getting the contact: %s", error->message);
+      g_error_free (error);
+      goto out;
+    }
 
-    if (error != NULL)
-      {
-        g_warning ("Error while getting the contact: %s", error->message);
-        return;
-      }
+  empathy_contact_add_to_contact_list (contact, message);
+  g_object_unref (contact);
 
-    empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager), contact, "");
+out:
+  g_free (message);
 }
 
 static void
@@ -150,8 +167,12 @@ add_selected_contact (EmpathyContactSearchDialog *self)
   TpConnection *conn;
   GtkTreeIter iter;
   GtkTreeModel *model;
+  GtkTextBuffer *buffer;
+  GtkTextIter start, end;
+  gchar *message;
   gboolean sel;
   gchar *id;
+  EmpathyClientFactory *factory;
 
   conn = empathy_account_chooser_get_connection (EMPATHY_ACCOUNT_CHOOSER (priv->chooser));
 
@@ -162,9 +183,20 @@ add_selected_contact (EmpathyContactSearchDialog *self)
 
   DEBUG ("Requested to add contact: %s", id);
 
-  empathy_tp_contact_factory_get_from_id (conn, id,
-      on_get_contact_factory_get_from_id_cb, NULL,
-      NULL, NULL);
+  buffer = gtk_text_view_get_buffer GTK_TEXT_VIEW (priv->message);
+  gtk_text_buffer_get_start_iter (buffer, &start);
+  gtk_text_buffer_get_end_iter (buffer, &end);
+  message = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
+
+  factory = empathy_client_factory_dup ();
+
+  empathy_client_factory_dup_contact_by_id_async (factory, conn, id,
+      on_get_contact_factory_get_from_id_cb, message);
+
+  g_object_unref (factory);
+
+  /* Close the dialog */
+  gtk_dialog_response (GTK_DIALOG (self), GTK_RESPONSE_CANCEL);
 }
 
 static void
@@ -253,13 +285,10 @@ _search_results_received (TpContactSearch *searcher,
   for (l = results; l != NULL; l = l->next)
     {
       TpContactSearchResult *result = l->data;
-      GtkTreeIter iter;
-
-      gtk_list_store_append (priv->store, &iter);
 
       name = tp_contact_search_result_get_field (result, "fn");
 
-      gtk_list_store_set (priv->store, &iter,
+      gtk_list_store_insert_with_values (priv->store, NULL, -1,
           NAME_COLUMN, name ? name->field_value[0] : NULL,
           LOGIN_COLUMN, tp_contact_search_result_get_identifier (result),
           -1);
@@ -313,6 +342,17 @@ on_selection_changed (GtkTreeSelection *selection,
   gtk_widget_set_sensitive (priv->add_button, sel);
 }
 
+static void
+check_request_message_available (EmpathyContactSearchDialog *self,
+    TpConnection *conn)
+{
+  EmpathyContactSearchDialogPrivate *priv = GET_PRIVATE (self);
+
+  gtk_widget_set_visible (priv->message_window,
+      tp_connection_get_can_change_contact_list (conn));
+  gtk_widget_set_visible (priv->message_label,
+      tp_connection_get_can_change_contact_list (conn));
+}
 
 static void
 _account_chooser_changed (EmpathyAccountChooser *chooser,
@@ -341,10 +381,13 @@ _account_chooser_changed (EmpathyAccountChooser *chooser,
       NULL, /* gtk_entry_get_text (GTK_ENTRY (priv->server_entry)), */
       0,
       on_searcher_created, self);
+
+  /* Make the request message textview sensitive if it can be used */
+  check_request_message_available (self, conn);
 }
 
 static void
-_on_button_search_clicked (GtkButton *button,
+_on_button_search_clicked (GtkWidget *widget,
     EmpathyContactSearchDialog *self)
 {
   empathy_contact_search_dialog_do_search (self);
@@ -368,67 +411,99 @@ on_server_changed_cb (GtkEditable *editable,
 }
 #endif
 
-typedef struct
+static void
+empathy_account_chooser_filter_supports_contact_search (
+    TpAccount *account,
+    EmpathyAccountChooserFilterResultCallback callback,
+    gpointer callback_data,
+    gpointer user_data)
+{
+  TpConnection *connection;
+  gboolean supported = FALSE;
+  TpCapabilities *caps;
+
+  connection = tp_account_get_connection (account);
+  if (connection == NULL)
+      goto out;
+
+  caps = tp_connection_get_capabilities (connection);
+  if (caps == NULL)
+      goto out;
+
+  supported = tp_capabilities_supports_contact_search (caps, NULL, NULL);
+
+out:
+  callback (supported, callback_data);
+}
+
+static void
+contact_search_dialog_row_activated_cb (GtkTreeView *tv,
+    GtkTreePath *path,
+    GtkTreeViewColumn *column,
+    EmpathyContactSearchDialog *self)
 {
-    EmpathyAccountChooserFilterResultCallback callback;
-    gpointer                                  user_data;
-} FilterCallbackData;
+  /* just emit the same response as the Add Button */
+  gtk_dialog_response (GTK_DIALOG (self), GTK_RESPONSE_APPLY);
+}
 
 static void
-supports_contact_search_cb (GObject *conn,
+on_profile_button_got_contact_cb (GObject *source,
     GAsyncResult *result,
     gpointer user_data)
 {
-  FilterCallbackData *data = user_data;
-  GError *myerr = NULL;
-  TpCapabilities *caps;
+  GError *error = NULL;
+  EmpathyContact *contact;
+  FolksIndividual *individual;
 
-  if (!tp_proxy_prepare_finish (conn, result, &myerr))
+  contact = empathy_client_factory_dup_contact_by_id_finish (
+        EMPATHY_CLIENT_FACTORY (source), result, &error);
+  if (contact == NULL)
     {
-      data->callback (FALSE, data->user_data);
-      g_slice_free (FilterCallbackData, data);
+      g_warning ("Error while getting the contact: %s", error->message);
+      g_error_free (error);
       return;
     }
 
-  caps = tp_connection_get_capabilities (TP_CONNECTION (conn));
-  data->callback (tp_capabilities_supports_contact_search (caps, NULL, NULL),
-    data->user_data);
+  individual = empathy_ensure_individual_from_tp_contact (
+    empathy_contact_get_tp_contact (contact));
+
+  empathy_display_individual_info (individual);
 
-  g_slice_free (FilterCallbackData, data);
+  g_object_unref (contact);
+  g_object_unref (individual);
 }
 
 static void
-empathy_account_chooser_filter_supports_contact_search (
-    TpAccount *account,
-    EmpathyAccountChooserFilterResultCallback callback,
-    gpointer callback_data,
-    gpointer user_data)
+on_profile_button_clicked_cb (EmpathyCellRendererActivatable *cell,
+    const gchar *path_string,
+    EmpathyContactSearchDialog *self)
 {
-  TpConnection *connection;
-  FilterCallbackData *cb_data;
-  GQuark features[] = { TP_CONNECTION_FEATURE_CAPABILITIES, 0 };
+  EmpathyContactSearchDialogPrivate *priv = GET_PRIVATE (self);
+  TpConnection *conn;
+  GtkTreeIter iter;
+  GtkTreeModel *model;
+  gboolean valid;
+  gchar *id;
+  EmpathyClientFactory *factory;
 
-  if (tp_account_get_connection_status (account, NULL)
-      != TP_CONNECTION_STATUS_CONNECTED)
-    {
-      callback (FALSE, callback_data);
-      return;
-    }
+  model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->tree_view));
 
-  /* check if CM supports contact search */
-  connection = tp_account_get_connection (account);
-  if (connection == NULL)
-    {
-      callback (FALSE, callback_data);
-      return;
-    }
+  conn = empathy_account_chooser_get_connection (
+      EMPATHY_ACCOUNT_CHOOSER (priv->chooser));
+
+  valid = gtk_tree_model_get_iter_from_string (model, &iter, path_string);
+  g_return_if_fail (valid == TRUE);
+
+  gtk_tree_model_get (model, &iter, LOGIN_COLUMN, &id, -1);
 
-  cb_data = g_slice_new0 (FilterCallbackData);
-  cb_data->callback = callback;
-  cb_data->user_data = callback_data;
+  DEBUG ("Requested to show profile for contact: %s", id);
 
-  tp_proxy_prepare_async (connection, features, supports_contact_search_cb,
-      cb_data);
+  factory = empathy_client_factory_dup ();
+
+  empathy_client_factory_dup_contact_by_id_async (factory, conn, id,
+      on_profile_button_got_contact_cb, self);
+
+  g_object_unref (factory);
 }
 
 static void
@@ -445,11 +520,11 @@ empathy_contact_search_dialog_init (EmpathyContactSearchDialog *self)
   /* Title */
   gtk_window_set_title (GTK_WINDOW (self), _("Search contacts"));
 
-  vbox = gtk_vbox_new (FALSE, 3);
+  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 3);
   gtk_container_set_border_width (GTK_CONTAINER (vbox), 6);
 
   /* Account chooser */
-  hbox = gtk_hbox_new (FALSE, 6);
+  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
   label = gtk_label_new (_("Account:"));
   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0);
   gtk_size_group_add_widget (size_group, label);
@@ -472,13 +547,15 @@ empathy_contact_search_dialog_init (EmpathyContactSearchDialog *self)
 #endif
 
   /* Search input */
-  hbox = gtk_hbox_new (FALSE, 6);
+  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
   label = gtk_label_new (_("Search: "));
   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0);
   gtk_size_group_add_widget (size_group, label);
 
   priv->search_entry = gtk_entry_new ();
   gtk_box_pack_start (GTK_BOX (hbox), priv->search_entry, TRUE, TRUE, 0);
+  g_signal_connect (priv->search_entry, "activate",
+      G_CALLBACK (_on_button_search_clicked), self);
 
   priv->find_button = gtk_button_new_from_stock (GTK_STOCK_FIND);
   g_signal_connect (priv->find_button, "clicked",
@@ -498,12 +575,15 @@ empathy_contact_search_dialog_init (EmpathyContactSearchDialog *self)
   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->tree_view));
   gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
 
+  g_signal_connect (priv->tree_view, "row-activated",
+      G_CALLBACK (contact_search_dialog_row_activated_cb), self);
   g_signal_connect (selection, "changed",
       G_CALLBACK (on_selection_changed), self);
 
   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->tree_view), FALSE);
 
   col = gtk_tree_view_column_new ();
+
   cell = empathy_cell_renderer_text_new ();
   gtk_tree_view_column_pack_start (col, cell, TRUE);
   /* EmpathyCellRendererText displays "name" above and "status" below.
@@ -514,14 +594,22 @@ empathy_contact_search_dialog_init (EmpathyContactSearchDialog *self)
   gtk_tree_view_column_add_attribute (col, cell,
       "status", NAME_COLUMN);
 
+  cell = empathy_cell_renderer_activatable_new ();
+  gtk_tree_view_column_pack_end (col, cell, FALSE);
+  g_object_set (cell, "stock-id", EMPATHY_IMAGE_CONTACT_INFORMATION, NULL);
+  g_signal_connect (cell, "path-activated",
+      G_CALLBACK (on_profile_button_clicked_cb), self);
+
   gtk_tree_view_append_column (GTK_TREE_VIEW (priv->tree_view), col);
 
   gtk_dialog_add_button (GTK_DIALOG (self),
       GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
 
   priv->add_button = gtk_dialog_add_button (GTK_DIALOG (self),
-      GTK_STOCK_ADD, GTK_RESPONSE_APPLY);
+      _("_Add Contact"), GTK_RESPONSE_APPLY);
   gtk_widget_set_sensitive (priv->add_button, FALSE);
+  gtk_button_set_image (GTK_BUTTON (priv->add_button),
+      gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_BUTTON));
 
   /* Pack the dialog */
   priv->notebook = gtk_notebook_new ();
@@ -550,12 +638,37 @@ empathy_contact_search_dialog_init (EmpathyContactSearchDialog *self)
 
   gtk_box_pack_start (GTK_BOX (vbox), priv->notebook, TRUE, TRUE, 3);
 
+  /* Request message textview */
+  priv->message_label = gtk_label_new (
+       _("Your message introducing yourself:"));
+  gtk_misc_set_alignment (GTK_MISC (priv->message_label), 0, 0.5);
+
+  priv->message = gtk_text_view_new ();
+  gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (priv->message),
+      GTK_WRAP_WORD_CHAR);
+  gtk_text_buffer_set_text (
+      gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->message)),
+      _("Please let me see when you're online. Thanks!"), -1);
+
+  priv->message_window = gtk_scrolled_window_new (NULL, NULL);
+  gtk_scrolled_window_set_shadow_type (
+      GTK_SCROLLED_WINDOW (priv->message_window),
+      GTK_SHADOW_ETCHED_IN);
+  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->message_window),
+      GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
+
+  gtk_container_add (GTK_CONTAINER (priv->message_window), priv->message);
+
+  gtk_box_pack_start (GTK_BOX (vbox), priv->message_label, FALSE, TRUE, 3);
+  gtk_box_pack_start (GTK_BOX (vbox), priv->message_window, FALSE, TRUE, 3);
+
   gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (
           GTK_DIALOG (self))), vbox, TRUE, TRUE, 0);
 
   gtk_window_set_default_size (GTK_WINDOW (self), 200, 400);
   gtk_widget_show_all (vbox);
   gtk_widget_hide (priv->spinner);
+  g_object_unref (size_group);
 }
 
 GtkWidget *