]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-contact-list-view.c
More flexible API for EmpathyContactWidget, we now have flags to set
[empathy.git] / libempathy-gtk / empathy-contact-list-view.c
index 4aeac322d513f1a4c309a08931a331a5cc4b517d..e3816dda7ba8a5159db5e410e0de02531935940b 100644 (file)
 #include <gtk/gtk.h>
 #include <glade/glade.h>
 
+#include <libtelepathy/tp-helpers.h>
+
 #include <libmissioncontrol/mc-account.h>
 #include <libmissioncontrol/mission-control.h>
 
 #include <libempathy/empathy-contact-factory.h>
 #include <libempathy/empathy-contact-list.h>
 #include <libempathy/empathy-log-manager.h>
+#include <libempathy/empathy-tp-group.h>
 #include <libempathy/empathy-debug.h>
 #include <libempathy/empathy-utils.h>
 #include <libempathy/empathy-marshal.h>
@@ -47,6 +50,7 @@
 #include "empathy-contact-groups.h"
 #include "empathy-cell-renderer-expander.h"
 #include "empathy-cell-renderer-text.h"
+#include "empathy-cell-renderer-activatable.h"
 #include "empathy-ui-utils.h"
 #include "empathy-contact-dialogs.h"
 //#include "empathy-chat-invite.h"
@@ -68,6 +72,7 @@ struct _EmpathyContactListViewPriv {
        EmpathyContactListStore *store;
        GtkUIManager            *ui;
        GtkTreeRowReference     *drag_row;
+       gboolean                 interactive;
 };
 
 typedef struct {
@@ -133,7 +138,14 @@ static void        contact_list_view_pixbuf_cell_data_func     (GtkTreeViewColum
                                                                GtkCellRenderer            *cell,
                                                                GtkTreeModel               *model,
                                                                GtkTreeIter                *iter,
-                                                               EmpathyContactListView      *view);
+                                                               EmpathyContactListView     *view);
+#ifdef HAVE_VOIP
+static void        contact_list_view_voip_cell_data_func       (GtkTreeViewColumn          *tree_column,
+                                                               GtkCellRenderer            *cell,
+                                                               GtkTreeModel               *model,
+                                                               GtkTreeIter                *iter,
+                                                               EmpathyContactListView     *view);
+#endif
 static void        contact_list_view_avatar_cell_data_func     (GtkTreeViewColumn          *tree_column,
                                                                GtkCellRenderer            *cell,
                                                                GtkTreeModel               *model,
@@ -151,7 +163,8 @@ static void        contact_list_view_expander_cell_data_func   (GtkTreeViewColum
                                                                EmpathyContactListView      *view);
 static GtkWidget * contact_list_view_get_contact_menu          (EmpathyContactListView      *view,
                                                                gboolean                    can_send_file,
-                                                               gboolean                    can_show_log);
+                                                               gboolean                    can_show_log,
+                                                               gboolean                    can_voip);
 static gboolean    contact_list_view_button_press_event_cb     (EmpathyContactListView      *view,
                                                                GdkEventButton             *event,
                                                                gpointer                    user_data);
@@ -159,6 +172,11 @@ static void        contact_list_view_row_activated_cb          (EmpathyContactLi
                                                                GtkTreePath                *path,
                                                                GtkTreeViewColumn          *col,
                                                                gpointer                    user_data);
+#ifdef HAVE_VOIP
+static void        contact_list_view_voip_activated_cb         (EmpathyCellRendererActivatable *cell,
+                                                               const gchar                *path_string,
+                                                               EmpathyContactListView     *view);
+#endif
 static void        contact_list_view_row_expand_or_collapse_cb (EmpathyContactListView      *view,
                                                                GtkTreeIter                *iter,
                                                                GtkTreePath                *path,
@@ -167,9 +185,12 @@ static void        contact_list_view_action_cb                 (GtkAction
                                                                EmpathyContactListView      *view);
 static void        contact_list_view_action_activated          (EmpathyContactListView      *view,
                                                                EmpathyContact              *contact);
+static void        contact_list_view_voip_activated            (EmpathyContactListView      *view,
+                                                               EmpathyContact              *contact);
 
 enum {
        PROP_0,
+       PROP_INTERACTIVE
 };
 
 static const GtkActionEntry entries[] = {
@@ -213,6 +234,12 @@ static const GtkActionEntry entries[] = {
          N_("_View Previous Conversations"), NULL, N_("View previous conversations with this contact"),
          G_CALLBACK (contact_list_view_action_cb)
        },
+#ifdef HAVE_VOIP
+       { "Call", EMPATHY_IMAGE_VOIP,
+         N_("_Call"), NULL, N_("Start a voice or video conversation with this contact"),
+         G_CALLBACK (contact_list_view_action_cb)
+       },
+#endif
 };
 
 static guint n_entries = G_N_ELEMENTS (entries);
@@ -221,6 +248,9 @@ static const gchar *ui_info =
        "<ui>"
        "  <popup name='Contact'>"
        "    <menuitem action='Chat'/>"
+#ifdef HAVE_VOIP
+       "    <menuitem action='Call'/>"
+#endif
        "    <menuitem action='Log'/>"
        "    <menuitem action='SendFile'/>"
        "    <separator/>"
@@ -296,6 +326,14 @@ empathy_contact_list_view_class_init (EmpathyContactListViewClass *klass)
                              G_TYPE_NONE,
                              3, EMPATHY_TYPE_CONTACT, G_TYPE_STRING, G_TYPE_STRING);
 
+       g_object_class_install_property (object_class,
+                                        PROP_INTERACTIVE,
+                                        g_param_spec_boolean ("interactive",
+                                                              "View is interactive",
+                                                              "Is the view interactive",
+                                                              FALSE,
+                                                              G_PARAM_READWRITE));
+
        g_type_class_add_private (object_class, sizeof (EmpathyContactListViewPriv));
 }
 
@@ -377,6 +415,9 @@ contact_list_view_get_property (GObject    *object,
        priv = GET_PRIV (object);
 
        switch (param_id) {
+       case PROP_INTERACTIVE:
+               g_value_set_boolean (value, priv->interactive);
+               break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
                break;
@@ -389,11 +430,15 @@ contact_list_view_set_property (GObject      *object,
                                const GValue *value,
                                GParamSpec   *pspec)
 {
+       EmpathyContactListView     *view = EMPATHY_CONTACT_LIST_VIEW (object);
        EmpathyContactListViewPriv *priv;
 
        priv = GET_PRIV (object);
 
        switch (param_id) {
+       case PROP_INTERACTIVE:
+               empathy_contact_list_view_set_interactive (view, g_value_get_boolean (value));
+               break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
                break;
@@ -415,6 +460,28 @@ empathy_contact_list_view_new (EmpathyContactListStore *store)
        return view;
 }
 
+void
+empathy_contact_list_view_set_interactive (EmpathyContactListView  *view,
+                                          gboolean                 interactive)
+{
+       EmpathyContactListViewPriv *priv = GET_PRIV (view);
+
+       g_return_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view));
+
+       priv->interactive = interactive;
+       g_object_notify (G_OBJECT (view), "interactive");
+}
+
+gboolean
+empathy_contact_list_view_get_interactive (EmpathyContactListView  *view)
+{
+       EmpathyContactListViewPriv *priv = GET_PRIV (view);
+
+       g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), FALSE);
+
+       return priv->interactive;
+}
+
 EmpathyContact *
 empathy_contact_list_view_get_selected (EmpathyContactListView *view)
 {
@@ -487,11 +554,12 @@ empathy_contact_list_view_get_group_menu (EmpathyContactListView *view)
 
 GtkWidget *
 empathy_contact_list_view_get_contact_menu (EmpathyContactListView *view,
-                                          EmpathyContact         *contact)
+                                           EmpathyContact         *contact)
 {
        EmpathyLogManager *log_manager;
        gboolean           can_show_log;
        gboolean           can_send_file;
+       gboolean           can_voip;
 
        g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST_VIEW (view), NULL);
        g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
@@ -501,12 +569,14 @@ empathy_contact_list_view_get_contact_menu (EmpathyContactListView *view,
                                                   empathy_contact_get_account (contact),
                                                   empathy_contact_get_id (contact),
                                                   FALSE);
-       can_send_file = FALSE;
        g_object_unref (log_manager);
+       can_send_file = FALSE;
+       can_voip = empathy_contact_can_voip (contact);
 
        return contact_list_view_get_contact_menu (view,
                                                   can_send_file,
-                                                  can_show_log);
+                                                  can_show_log,
+                                                  can_voip);
 }
 
 static void
@@ -567,6 +637,24 @@ contact_list_view_setup (EmpathyContactListView *view)
        gtk_tree_view_column_add_attribute (col, cell,
                                            "is_group", COL_IS_GROUP);
 
+#ifdef HAVE_VOIP
+       /* Voip Capability Icon */
+       cell = empathy_cell_renderer_activatable_new ();
+       gtk_tree_view_column_pack_start (col, cell, FALSE);
+       gtk_tree_view_column_set_cell_data_func (
+               col, cell,
+               (GtkTreeCellDataFunc) contact_list_view_voip_cell_data_func,
+               view, NULL);
+
+       g_object_set (cell,
+                     "visible", FALSE,
+                     NULL);
+
+       g_signal_connect (cell, "path-activated",
+                         G_CALLBACK (contact_list_view_voip_activated_cb),
+                         view);
+#endif
+
        /* Avatar */
        cell = gtk_cell_renderer_pixbuf_new ();
        gtk_tree_view_column_pack_start (col, cell, FALSE);
@@ -702,7 +790,6 @@ contact_list_view_drag_data_received (GtkWidget         *widget,
                g_object_unref (account);
        }
        g_object_unref (factory);
-       g_object_unref (account);
        g_strfreev (strv);
 
        if (!contact) {
@@ -1001,6 +1088,33 @@ contact_list_view_pixbuf_cell_data_func (GtkTreeViewColumn     *tree_column,
        contact_list_view_cell_set_background (view, cell, is_group, is_active);
 }
 
+#ifdef HAVE_VOIP
+static void
+contact_list_view_voip_cell_data_func (GtkTreeViewColumn      *tree_column,
+                                      GtkCellRenderer        *cell,
+                                      GtkTreeModel           *model,
+                                      GtkTreeIter            *iter,
+                                      EmpathyContactListView *view)
+{
+       gboolean is_group;
+       gboolean is_active;
+       gboolean can_voip;
+
+       gtk_tree_model_get (model, iter,
+                           COL_IS_GROUP, &is_group,
+                           COL_IS_ACTIVE, &is_active,
+                           COL_CAN_VOIP, &can_voip,
+                           -1);
+
+       g_object_set (cell,
+                     "visible", !is_group && can_voip,
+                     "icon-name", EMPATHY_IMAGE_VOIP,
+                     NULL);
+
+       contact_list_view_cell_set_background (view, cell, is_group, is_active);
+}
+#endif
+
 static void
 contact_list_view_avatar_cell_data_func (GtkTreeViewColumn     *tree_column,
                                         GtkCellRenderer       *cell,
@@ -1093,7 +1207,8 @@ contact_list_view_expander_cell_data_func (GtkTreeViewColumn     *column,
 static GtkWidget *
 contact_list_view_get_contact_menu (EmpathyContactListView *view,
                                    gboolean               can_send_file,
-                                   gboolean               can_show_log)
+                                   gboolean               can_show_log,
+                                   gboolean               can_voip)
 {
        EmpathyContactListViewPriv *priv;
        GtkAction                 *action;
@@ -1105,6 +1220,11 @@ contact_list_view_get_contact_menu (EmpathyContactListView *view,
        action = gtk_ui_manager_get_action (priv->ui, "/Contact/Log");
        gtk_action_set_sensitive (action, can_show_log);
 
+#ifdef HAVE_VOIP
+       action = gtk_ui_manager_get_action (priv->ui, "/Contact/Call");
+       gtk_action_set_sensitive (action, can_voip);
+#endif
+
        action = gtk_ui_manager_get_action (priv->ui, "/Contact/SendFile");
        gtk_action_set_visible (action, can_send_file);
 
@@ -1127,12 +1247,12 @@ contact_list_view_button_press_event_cb (EmpathyContactListView *view,
        gboolean                   row_exists;
        GtkWidget                 *menu;
 
-       if (event->button != 3) {
+       priv = GET_PRIV (view);
+
+       if (!priv->interactive || event->button != 3) {
                return FALSE;
        }
 
-       priv = GET_PRIV (view);
-
        selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
        model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
 
@@ -1176,13 +1296,18 @@ contact_list_view_button_press_event_cb (EmpathyContactListView *view,
 
 static void
 contact_list_view_row_activated_cb (EmpathyContactListView *view,
-                                   GtkTreePath           *path,
-                                   GtkTreeViewColumn     *col,
-                                   gpointer               user_data)
+                                   GtkTreePath            *path,
+                                   GtkTreeViewColumn      *col,
+                                   gpointer                user_data)
 {
-       EmpathyContact *contact;
-       GtkTreeModel  *model;
-       GtkTreeIter    iter;
+       EmpathyContactListViewPriv *priv = GET_PRIV (view);
+       EmpathyContact             *contact;
+       GtkTreeModel               *model;
+       GtkTreeIter                 iter;
+
+       if (!priv->interactive) {
+               return;
+       }
 
        model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
 
@@ -1195,6 +1320,36 @@ contact_list_view_row_activated_cb (EmpathyContactListView *view,
        }
 }
 
+#ifdef HAVE_VOIP
+static void
+contact_list_view_voip_activated_cb (EmpathyCellRendererActivatable *cell,
+                                    const gchar                    *path_string,
+                                    EmpathyContactListView         *view)
+{
+       EmpathyContactListViewPriv *priv = GET_PRIV (view);
+       GtkTreeModel               *model;
+       GtkTreeIter                 iter;
+       EmpathyContact             *contact;
+
+       if (!priv->interactive) {
+               return;
+       }
+
+       model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
+       if (!gtk_tree_model_get_iter_from_string (model, &iter, path_string)) {
+               return;
+       }
+
+       gtk_tree_model_get (model, &iter, COL_CONTACT, &contact, -1);
+
+       if (contact) {
+               contact_list_view_voip_activated (view, contact);
+               g_object_unref (contact);
+       }
+}
+#endif
+
+
 static void
 contact_list_view_row_expand_or_collapse_cb (EmpathyContactListView *view,
                                             GtkTreeIter           *iter,
@@ -1243,11 +1398,14 @@ contact_list_view_action_cb (GtkAction             *action,
        if (contact && strcmp (name, "Chat") == 0) {
                contact_list_view_action_activated (view, contact);
        }
+       else if (contact && strcmp (name, "Call") == 0) {
+               contact_list_view_voip_activated (view, contact);
+       }
        else if (contact && strcmp (name, "Information") == 0) {
-               empathy_contact_information_dialog_show (contact, parent, FALSE);
+               empathy_contact_information_dialog_show (contact, parent, FALSE, FALSE);
        }
        else if (contact && strcmp (name, "Edit") == 0) {
-               empathy_contact_information_dialog_show (contact, parent, TRUE);
+               empathy_contact_information_dialog_show (contact, parent, TRUE, TRUE);
        }
        else if (contact && strcmp (name, "Remove") == 0) {
                /* FIXME: Ask for confirmation */
@@ -1292,3 +1450,10 @@ contact_list_view_action_activated (EmpathyContactListView *view,
        g_object_unref (mc);
 }
 
+static void
+contact_list_view_voip_activated (EmpathyContactListView *view,
+                                 EmpathyContact         *contact)
+{
+       empathy_call_contact (contact);
+}
+