]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-contact-list-view.c
empathy-call-window.c: update priv->sending_video *before* changing widget's state
[empathy.git] / libempathy-gtk / empathy-contact-list-view.c
index 1a9890b79b14258899503a435e3d5788d4a9ae2c..ca224f52631be53cffa9043d08c45d62d78a4265 100644 (file)
 
 #include <string.h>
 
-#include <glib/gi18n.h>
+#include <glib/gi18n-lib.h>
+#include <gdk/gdkkeysyms.h>
 #include <gtk/gtk.h>
-#include <glade/glade.h>
 
+#include <telepathy-glib/util.h>
 #include <libmissioncontrol/mc-account.h>
 
-#include <libempathy/empathy-contact-factory.h>
+#include <libempathy/empathy-account-manager.h>
+#include <libempathy/empathy-call-factory.h>
+#include <libempathy/empathy-tp-contact-factory.h>
 #include <libempathy/empathy-contact-list.h>
 #include <libempathy/empathy-contact-groups.h>
+#include <libempathy/empathy-dispatcher.h>
 #include <libempathy/empathy-utils.h>
 
 #include "empathy-contact-list-view.h"
 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
 #include <libempathy/empathy-debug.h>
 
-/* Flashing delay for icons (milliseconds). */
-#define FLASH_TIMEOUT 500
-
 /* Active users are those which have recently changed state
  * (e.g. online, offline or from normal to a busy state).
  */
 
-#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CONTACT_LIST_VIEW, EmpathyContactListViewPriv))
-
+#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContactListView)
 typedef struct {
        EmpathyContactListStore        *store;
        GtkTreeRowReference            *drag_row;
        EmpathyContactListFeatureFlags  list_features;
        EmpathyContactFeatureFlags      contact_features;
+       GtkWidget                      *tooltip_widget;
 } EmpathyContactListViewPriv;
 
 typedef struct {
@@ -116,7 +117,125 @@ static guint signals[LAST_SIGNAL];
 G_DEFINE_TYPE (EmpathyContactListView, empathy_contact_list_view, GTK_TYPE_TREE_VIEW);
 
 static void
-contact_list_view_drag_data_received (GtkWidget         *widget,
+contact_list_view_tooltip_destroy_cb (GtkWidget              *widget,
+                                     EmpathyContactListView *view)
+{
+       EmpathyContactListViewPriv *priv = GET_PRIV (view);
+       
+       if (priv->tooltip_widget) {
+               DEBUG ("Tooltip destroyed");
+               g_object_unref (priv->tooltip_widget);
+               priv->tooltip_widget = NULL;
+       }
+}
+
+static gboolean
+contact_list_view_query_tooltip_cb (EmpathyContactListView *view,
+                                   gint                    x,
+                                   gint                    y,
+                                   gboolean                keyboard_mode,
+                                   GtkTooltip             *tooltip,
+                                   gpointer                user_data)
+{
+       EmpathyContactListViewPriv *priv = GET_PRIV (view);
+       EmpathyContact             *contact;
+       GtkTreeModel               *model;
+       GtkTreeIter                 iter;
+       GtkTreePath                *path;
+       static gint                 running = 0;
+       gboolean                    ret = FALSE;
+
+       /* Avoid an infinite loop. See GNOME bug #574377 */
+       if (running > 0) {
+               return FALSE;
+       }
+       running++;
+
+       if (!gtk_tree_view_get_tooltip_context (GTK_TREE_VIEW (view), &x, &y,
+                                               keyboard_mode,
+                                               &model, &path, &iter)) {
+               goto OUT;
+       }
+
+       gtk_tree_view_set_tooltip_row (GTK_TREE_VIEW (view), tooltip, path);
+       gtk_tree_path_free (path);
+
+       gtk_tree_model_get (model, &iter,
+                           EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
+                           -1);
+       if (!contact) {
+               goto OUT;
+       }
+
+       if (!priv->tooltip_widget) {
+               priv->tooltip_widget = empathy_contact_widget_new (contact,
+                       EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP);
+               g_object_ref (priv->tooltip_widget);
+               g_signal_connect (priv->tooltip_widget, "destroy",
+                                 G_CALLBACK (contact_list_view_tooltip_destroy_cb),
+                                 view);
+               gtk_widget_show (priv->tooltip_widget);
+       } else {
+               empathy_contact_widget_set_contact (priv->tooltip_widget,
+                                                   contact);
+       }
+
+       gtk_tooltip_set_custom (tooltip, priv->tooltip_widget);
+       ret = TRUE;
+
+       g_object_unref (contact);
+OUT:
+       running--;
+
+       return ret;
+}
+
+typedef struct {
+       gchar *new_group;
+       gchar *old_group;
+       GdkDragAction action;
+} DndGetContactData;
+
+static void
+contact_list_view_dnd_get_contact_free (DndGetContactData *data)
+{
+       g_free (data->new_group);
+       g_free (data->old_group);
+       g_slice_free (DndGetContactData, data);
+}
+
+static void
+contact_list_view_drag_got_contact (EmpathyTpContactFactory *factory,
+                                   EmpathyContact          *contact,
+                                   const GError            *error,
+                                   gpointer                 user_data,
+                                   GObject                 *view)
+{
+       EmpathyContactListViewPriv *priv = GET_PRIV (view);
+       DndGetContactData          *data = user_data;
+       EmpathyContactList         *list;
+
+       if (error != NULL) {
+               DEBUG ("Error: %s", error->message);
+               return;
+       }
+
+       DEBUG ("contact %s (%d) dragged from '%s' to '%s'",
+               empathy_contact_get_id (contact),
+               empathy_contact_get_handle (contact),
+               data->old_group, data->new_group);
+
+       list = empathy_contact_list_store_get_list_iface (priv->store);
+       if (data->new_group) {
+               empathy_contact_list_add_to_group (list, contact, data->new_group);
+       }
+       if (data->old_group && data->action == GDK_ACTION_MOVE) {       
+               empathy_contact_list_remove_from_group (list, contact, data->old_group);
+       }
+}
+
+static void
+contact_list_view_drag_data_received (GtkWidget         *view,
                                      GdkDragContext    *context,
                                      gint               x,
                                      gint               y,
@@ -125,88 +244,102 @@ contact_list_view_drag_data_received (GtkWidget         *widget,
                                      guint              time)
 {
        EmpathyContactListViewPriv *priv;
-       EmpathyContactList         *list;
-       EmpathyContactFactory      *factory;
+       EmpathyAccountManager      *account_manager;
+       EmpathyTpContactFactory    *factory = NULL;
        McAccount                  *account;
        GtkTreeModel               *model;
-       GtkTreePath                *path;
        GtkTreeViewDropPosition     position;
-       EmpathyContact             *contact = NULL;
+       GtkTreePath                *path;
        const gchar                *id;
-       gchar                     **strv;
+       gchar                     **strv = NULL;
+       const gchar                *account_id;
+       const gchar                *contact_id;
        gchar                      *new_group = NULL;
        gchar                      *old_group = NULL;
+       DndGetContactData          *data;
        gboolean                    is_row;
+       gboolean                    success = TRUE;
 
-       priv = GET_PRIV (widget);
-
-       id = (const gchar*) selection->data;
-       DEBUG ("Received %s%s drag & drop contact from roster with id:'%s'",
-               context->action == GDK_ACTION_MOVE ? "move" : "",
-               context->action == GDK_ACTION_COPY ? "copy" : "",
-               id);
+       priv = GET_PRIV (view);
+       model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
 
-       strv = g_strsplit (id, "/", 2);
-       factory = empathy_contact_factory_new ();
-       account = mc_account_lookup (strv[0]);
-       if (account) {
-               contact = empathy_contact_factory_get_from_id (factory,
-                                                              account,
-                                                              strv[1]);
-               g_object_unref (account);
-       }
-       g_object_unref (factory);
-       g_strfreev (strv);
+       /* Get destination group information. */
+       is_row = gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (view),
+                                                   x,
+                                                   y,
+                                                   &path,
+                                                   &position);
 
-       if (!contact) {
-               DEBUG ("No contact found associated with drag & drop");
-               return;
+       if (is_row) {
+               new_group = empathy_contact_list_store_get_parent_group (model,
+                       path, NULL);
+               gtk_tree_path_free (path);
        }
 
-       empathy_contact_run_until_ready (contact,
-                                        EMPATHY_CONTACT_READY_HANDLE,
-                                        NULL);
-
-       model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));
-
        /* Get source group information. */
        if (priv->drag_row) {
                path = gtk_tree_row_reference_get_path (priv->drag_row);
                if (path) {
-                       old_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
+                       old_group = empathy_contact_list_store_get_parent_group (
+                               model, path, NULL);
                        gtk_tree_path_free (path);
                }
        }
 
-       /* Get destination group information. */
-       is_row = gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
-                                                   x,
-                                                   y,
-                                                   &path,
-                                                   &position);
-
-       if (is_row) {
-               new_group = empathy_contact_list_store_get_parent_group (model, path, NULL);
-               gtk_tree_path_free (path);
+       if (!tp_strdiff (old_group, new_group)) {
+               g_free (new_group);
+               g_free (old_group);
+               goto OUT;
        }
 
-       DEBUG ("contact %s (%d) dragged from '%s' to '%s'",
-               empathy_contact_get_id (contact),
-               empathy_contact_get_handle (contact),
-               old_group, new_group);
+       id = (const gchar*) selection->data;
+       DEBUG ("Received %s%s drag & drop contact from roster with id:'%s'",
+               context->action == GDK_ACTION_MOVE ? "move" : "",
+               context->action == GDK_ACTION_COPY ? "copy" : "",
+               id);
 
-       list = empathy_contact_list_store_get_list_iface (priv->store);
-       if (new_group) {
-               empathy_contact_list_add_to_group (list, contact, new_group);
+       strv = g_strsplit (id, "/", 2);
+       account_id = strv[0];
+       contact_id = strv[1];
+       account = mc_account_lookup (account_id);
+       if (account) {
+               TpConnection *connection;
+
+               /* FIXME: We assume we have already an account manager */
+               account_manager = empathy_account_manager_dup_singleton ();
+               connection = empathy_account_manager_get_connection (account_manager,
+                                                                    account);
+               if (connection) {
+                       factory = empathy_tp_contact_factory_dup_singleton (connection);
+               }
+               g_object_unref (account_manager);
        }
-       if (old_group && context->action == GDK_ACTION_MOVE) {  
-               empathy_contact_list_remove_from_group (list, contact, old_group);
+
+       if (!factory) {
+               DEBUG ("Failed to get factory for account '%s'", account_id);
+               success = FALSE;
+               g_free (new_group);
+               g_free (old_group);
+               goto OUT;
        }
 
-       g_free (old_group);
-       g_free (new_group);
+       data = g_slice_new0 (DndGetContactData);
+       data->new_group = new_group;
+       data->old_group = old_group;
+       data->action = context->action;
+
+       /* FIXME: We should probably wait for the cb before calling
+        * gtk_drag_finish */
+       empathy_tp_contact_factory_get_from_id (factory, contact_id,
+               contact_list_view_drag_got_contact,
+               data, (GDestroyNotify) contact_list_view_dnd_get_contact_free,
+               G_OBJECT (view));
+
+       g_object_unref (factory);
 
-       gtk_drag_finish (context, TRUE, FALSE, GDK_CURRENT_TIME);
+OUT:
+       g_strfreev (strv);
+       gtk_drag_finish (context, success, FALSE, GDK_CURRENT_TIME);
 }
 
 static gboolean
@@ -341,7 +474,7 @@ contact_list_view_drag_data_get (GtkWidget        *widget,
 
        gtk_tree_path_free (src_path);
 
-       contact = empathy_contact_list_view_get_selected (EMPATHY_CONTACT_LIST_VIEW (widget));
+       contact = empathy_contact_list_view_dup_selected (EMPATHY_CONTACT_LIST_VIEW (widget));
        if (!contact) {
                return;
        }
@@ -406,17 +539,13 @@ contact_list_view_popup_menu_idle_cb (gpointer user_data)
                menu = empathy_contact_list_view_get_group_menu (data->view);
        }
 
-       if (!menu) {
-               goto OUT;
+       if (menu) {
+               gtk_widget_show (menu);
+               gtk_menu_popup (GTK_MENU (menu),
+                               NULL, NULL, NULL, NULL,
+                               data->button, data->time);
        }
 
-       gtk_widget_show (menu);
-
-       gtk_menu_popup (GTK_MENU (menu),
-                       NULL, NULL, NULL, NULL,
-                       data->button, data->time);
-
-OUT:
        g_slice_free (MenuPopupData, data);
 
        return FALSE;
@@ -440,11 +569,28 @@ contact_list_view_button_press_event_cb (EmpathyContactListView *view,
        return FALSE;
 }
 
+static gboolean
+contact_list_view_key_press_event_cb (EmpathyContactListView *view,
+                                     GdkEventKey            *event,
+                                     gpointer                user_data)
+{
+       if (event->keyval == GDK_Menu) {
+               MenuPopupData *data;
+
+               data = g_slice_new (MenuPopupData);
+               data->view = view;
+               data->button = 0;
+               data->time = event->time;
+               g_idle_add (contact_list_view_popup_menu_idle_cb, data);
+       }
+
+       return FALSE;
+}
+
 static void
-contact_list_view_row_activated_cb (EmpathyContactListView *view,
-                                   GtkTreePath            *path,
-                                   GtkTreeViewColumn      *col,
-                                   gpointer                user_data)
+contact_list_view_row_activated (GtkTreeView       *view,
+                                GtkTreePath       *path,
+                                GtkTreeViewColumn *column)
 {
        EmpathyContactListViewPriv *priv = GET_PRIV (view);
        EmpathyContact             *contact;
@@ -455,15 +601,15 @@ contact_list_view_row_activated_cb (EmpathyContactListView *view,
                return;
        }
 
-       model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
-
+       model = GTK_TREE_MODEL (priv->store);
        gtk_tree_model_get_iter (model, &iter, path);
        gtk_tree_model_get (model, &iter,
                            EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
                            -1);
 
        if (contact) {
-               empathy_chat_with_contact (contact);
+               DEBUG ("Starting a chat");
+               empathy_dispatcher_chat_with_contact (contact, NULL, NULL);
                g_object_unref (contact);
        }
 }
@@ -492,7 +638,11 @@ contact_list_view_voip_activated_cb (EmpathyCellRendererActivatable *cell,
                            -1);
 
        if (contact) {
-               empathy_call_with_contact (contact);
+               EmpathyCallFactory *factory;
+
+               factory = empathy_call_factory_get ();
+               empathy_call_factory_new_call (factory, contact);
+
                g_object_unref (contact);
        }
 }
@@ -713,7 +863,7 @@ contact_list_view_row_has_child_toggled_cb (GtkTreeModel          *model,
                            EMPATHY_CONTACT_LIST_STORE_COL_NAME, &name,
                            -1);
 
-       if (!is_group || G_STR_EMPTY (name)) {
+       if (!is_group || EMP_STR_EMPTY (name)) {
                g_free (name);
                return;
        }
@@ -858,6 +1008,7 @@ contact_list_view_set_list_features (EmpathyContactListView         *view,
                                     EmpathyContactListFeatureFlags  features)
 {
        EmpathyContactListViewPriv *priv = GET_PRIV (view);
+       gboolean                    has_tooltip;
 
        g_return_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view));
 
@@ -882,9 +1033,13 @@ contact_list_view_set_list_features (EmpathyContactListView         *view,
                                   G_N_ELEMENTS (drag_types_dest),
                                   GDK_ACTION_MOVE | GDK_ACTION_COPY);
        } else {
-               /* FIXME: URI could still be  droped depending on FT feature */
+               /* FIXME: URI could still be droped depending on FT feature */
                gtk_drag_dest_unset (GTK_WIDGET (view));
        }
+
+       /* Update has-tooltip */
+       has_tooltip = (features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_TOOLTIP) != 0;
+       gtk_widget_set_has_tooltip (GTK_WIDGET (view), has_tooltip);
 }
 
 static void
@@ -897,6 +1052,9 @@ contact_list_view_finalize (GObject *object)
        if (priv->store) {
                g_object_unref (priv->store);
        }
+       if (priv->tooltip_widget) {
+               gtk_widget_destroy (priv->tooltip_widget);
+       }
 
        G_OBJECT_CLASS (empathy_contact_list_view_parent_class)->finalize (object);
 }
@@ -956,8 +1114,9 @@ contact_list_view_set_property (GObject      *object,
 static void
 empathy_contact_list_view_class_init (EmpathyContactListViewClass *klass)
 {
-       GObjectClass   *object_class = G_OBJECT_CLASS (klass);
-       GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+       GObjectClass     *object_class = G_OBJECT_CLASS (klass);
+       GtkWidgetClass   *widget_class = GTK_WIDGET_CLASS (klass);
+       GtkTreeViewClass *tree_view_class = GTK_TREE_VIEW_CLASS (klass);
 
        object_class->finalize = contact_list_view_finalize;
        object_class->get_property = contact_list_view_get_property;
@@ -968,12 +1127,13 @@ empathy_contact_list_view_class_init (EmpathyContactListViewClass *klass)
        widget_class->drag_begin         = contact_list_view_drag_begin;
        widget_class->drag_data_get      = contact_list_view_drag_data_get;
        widget_class->drag_end           = contact_list_view_drag_end;
-       /* FIXME: noticed but when you drag the row over the treeview
-        * fast, it seems to stop redrawing itself, if we don't
-        * connect this signal, all is fine.
-        */
        widget_class->drag_motion        = contact_list_view_drag_motion;
 
+       /* We use the class method to let user of this widget to connect to
+        * the signal and stop emission of the signal so the default handler
+        * won't be called. */
+       tree_view_class->row_activated = contact_list_view_row_activated;
+
        signals[DRAG_CONTACT_RECEIVED] =
                g_signal_new ("drag-contact-received",
                              G_OBJECT_CLASS_TYPE (klass),
@@ -1014,6 +1174,10 @@ empathy_contact_list_view_class_init (EmpathyContactListViewClass *klass)
 static void
 empathy_contact_list_view_init (EmpathyContactListView *view)
 {
+       EmpathyContactListViewPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (view,
+               EMPATHY_TYPE_CONTACT_LIST_VIEW, EmpathyContactListViewPriv);
+
+       view->priv = priv;
        /* Get saved group states. */
        empathy_contact_groups_get_all ();
 
@@ -1022,22 +1186,21 @@ empathy_contact_list_view_init (EmpathyContactListView *view)
                                              NULL, NULL);
 
        /* Connect to tree view signals rather than override. */
-       g_signal_connect (view,
-                         "button-press-event",
+       g_signal_connect (view, "button-press-event",
                          G_CALLBACK (contact_list_view_button_press_event_cb),
                          NULL);
-       g_signal_connect (view,
-                         "row-activated",
-                         G_CALLBACK (contact_list_view_row_activated_cb),
+       g_signal_connect (view, "key-press-event",
+                         G_CALLBACK (contact_list_view_key_press_event_cb),
                          NULL);
-       g_signal_connect (view,
-                         "row-expanded",
+       g_signal_connect (view, "row-expanded",
                          G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
                          GINT_TO_POINTER (TRUE));
-       g_signal_connect (view,
-                         "row-collapsed",
+       g_signal_connect (view, "row-collapsed",
                          G_CALLBACK (contact_list_view_row_expand_or_collapse_cb),
                          GINT_TO_POINTER (FALSE));
+       g_signal_connect (view, "query-tooltip",
+                         G_CALLBACK (contact_list_view_query_tooltip_cb),
+                         NULL);
 }
 
 EmpathyContactListView *
@@ -1055,7 +1218,7 @@ empathy_contact_list_view_new (EmpathyContactListStore        *store,
 }
 
 EmpathyContact *
-empathy_contact_list_view_get_selected (EmpathyContactListView *view)
+empathy_contact_list_view_dup_selected (EmpathyContactListView *view)
 {
        EmpathyContactListViewPriv *priv;
        GtkTreeSelection          *selection;
@@ -1113,31 +1276,22 @@ empathy_contact_list_view_get_selected_group (EmpathyContactListView *view)
 
 static gboolean
 contact_list_view_remove_dialog_show (GtkWindow   *parent, 
-                                     const gchar *window_title, 
-                                     const gchar *text)
+                                     const gchar *message, 
+                                     const gchar *secondary_text)
 {
-       GtkWidget *dialog, *label, *image, *hbox;
+       GtkWidget *dialog;
        gboolean res;
        
-       dialog = gtk_dialog_new_with_buttons (window_title, parent,
-                                             GTK_DIALOG_MODAL,
-                                             GTK_STOCK_DELETE, GTK_RESPONSE_YES,
-                                             GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
-                                             NULL);
-       gtk_dialog_set_has_separator (GTK_DIALOG(dialog), FALSE);
+       dialog = gtk_message_dialog_new (parent, GTK_DIALOG_MODAL,
+                                        GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
+                                        "%s", message);
+       gtk_dialog_add_buttons (GTK_DIALOG (dialog),
+                               GTK_STOCK_CANCEL, GTK_RESPONSE_NO,
+                               GTK_STOCK_DELETE, GTK_RESPONSE_YES,
+                               NULL);
+       gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
+                                                 "%s", secondary_text);
         
-       label = gtk_label_new (text);
-       image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_QUESTION, GTK_ICON_SIZE_DIALOG);
-        
-       hbox = gtk_hbox_new (FALSE, 5);
-       gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
-       gtk_box_pack_start_defaults (GTK_BOX (hbox), image);
-       gtk_box_pack_start_defaults (GTK_BOX (hbox), label);     
-       gtk_box_pack_start_defaults (GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox);
-
-       gtk_widget_show (image);
-       gtk_widget_show (label);
-       gtk_widget_show (hbox);
        gtk_widget_show (dialog);
         
        res = gtk_dialog_run (GTK_DIALOG (dialog));
@@ -1230,7 +1384,7 @@ contact_list_view_remove_activate_cb (GtkMenuItem            *menuitem,
        EmpathyContactListViewPriv *priv = GET_PRIV (view);
        EmpathyContact             *contact;
                
-       contact = empathy_contact_list_view_get_selected (view);
+       contact = empathy_contact_list_view_dup_selected (view);
 
        if (contact) {
                gchar     *text; 
@@ -1263,17 +1417,16 @@ empathy_contact_list_view_get_contact_menu (EmpathyContactListView *view)
 
        g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
 
-       contact = empathy_contact_list_view_get_selected (view);
+       contact = empathy_contact_list_view_dup_selected (view);
        if (!contact) {
                return NULL;
        }
 
        menu = empathy_contact_menu_new (contact, priv->contact_features);
 
-       if (!menu &&
-           !(priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_REMOVE)) {
+       if (!(priv->list_features & EMPATHY_CONTACT_LIST_FEATURE_CONTACT_REMOVE)) {
                g_object_unref (contact);
-               return NULL;
+               return menu;
        }
 
        if (menu) {