]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-contact-widget.c
Add a public method to change the contact showed.
[empathy.git] / libempathy-gtk / empathy-contact-widget.c
index b3081b13098d065668751d9d48ef1e20ee4e1e85..a03571d9a1562a9d7186163f8f15a958d82bb4f8 100644 (file)
@@ -2,20 +2,19 @@
 /*
  * Copyright (C) 2007 Collabora Ltd.
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of the
- * License, or (at your option) any later version.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This program is distributed in the hope that it will be useful,
+ * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public
- * License along with this program; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  *
  * Authors: Xavier Claessens <xclaesse@gmail.com>
  */
 
 #include "empathy-contact-widget.h"
 #include "empathy-account-chooser.h"
+#include "empathy-avatar-chooser.h"
+#include "empathy-avatar-image.h"
 #include "empathy-ui-utils.h"
 
-/* Delay before updating the widget when the id entry changed (ms) */
-#define ID_CHANGED_TIMEOUT 500
+/* Delay before updating the widget when the id entry changed (seconds) */
+#define ID_CHANGED_TIMEOUT 1
 
 typedef struct {
        EmpathyContactFactory    *factory;
        EmpathyContactManager    *manager;
        EmpathyContact           *contact;
-       gboolean                  is_user;
-       EmpathyContactWidgetType  type;
+       EmpathyContactWidgetFlags flags;
        GtkCellRenderer          *renderer;
        guint                     widget_id_timeout;
 
@@ -66,7 +66,7 @@ typedef struct {
        GtkWidget                *image_state;
        GtkWidget                *label_status;
        GtkWidget                *table_contact;
-       GtkWidget                *hbox_contact;
+       GtkWidget                *vbox_avatar;
 
        /* Groups */
        GtkWidget                *vbox_groups;
@@ -99,7 +99,9 @@ static void     contact_widget_set_contact                (EmpathyContactWidget
                                                           EmpathyContact         *contact);
 static void     contact_widget_contact_setup              (EmpathyContactWidget  *information);
 static void     contact_widget_contact_update             (EmpathyContactWidget  *information);
-static gboolean contact_widget_update_contact             (EmpathyContactWidget  *information);
+static void     contact_widget_change_contact             (EmpathyContactWidget  *information);
+static void     contact_widget_avatar_changed_cb          (EmpathyAvatarChooser  *chooser,
+                                                          EmpathyContactWidget  *information);
 static void     contact_widget_account_changed_cb         (GtkComboBox           *widget,
                                                           EmpathyContactWidget  *information);
 static gboolean contact_widget_id_focus_out_cb            (GtkWidget             *widget,
@@ -147,19 +149,14 @@ enum {
 
 GtkWidget *
 empathy_contact_widget_new (EmpathyContact           *contact,
-                           EmpathyContactWidgetType  type)
+                           EmpathyContactWidgetFlags flags)
 {
        EmpathyContactWidget *information;
        GladeXML             *glade;
 
        information = g_slice_new0 (EmpathyContactWidget);
-       information->type = type;
+       information->flags = flags;
        information->factory = empathy_contact_factory_new ();
-       if (contact) {
-               information->is_user = empathy_contact_is_user (contact);
-       } else {
-               information->is_user = FALSE;
-       }
 
        glade = empathy_glade_get_file ("empathy-contact-widget.glade",
                                       "vbox_contact_widget",
@@ -171,7 +168,7 @@ empathy_contact_widget_new (EmpathyContact           *contact,
                                       "image_state", &information->image_state,
                                       "label_status", &information->label_status,
                                       "table_contact", &information->table_contact,
-                                      "hbox_contact", &information->hbox_contact,
+                                      "vbox_avatar", &information->vbox_avatar,
                                       "vbox_groups", &information->vbox_groups,
                                       "entry_group", &information->entry_group,
                                       "button_group", &information->button_group,
@@ -225,6 +222,44 @@ empathy_contact_widget_get_contact (GtkWidget *widget)
 
        return information->contact;
 }
+
+void
+empathy_contact_widget_set_contact (GtkWidget      *widget,
+                                   EmpathyContact *contact)
+{
+       EmpathyContactWidget *information;
+
+       g_return_if_fail (GTK_IS_WIDGET (widget));
+       g_return_if_fail (EMPATHY_IS_CONTACT (contact));
+
+       information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
+       if (!information) {
+               return;
+       }
+
+       contact_widget_set_contact (information, contact);
+}
+
+void
+empathy_contact_widget_set_account_filter (GtkWidget                       *widget,
+                                          EmpathyAccountChooserFilterFunc  filter,
+                                          gpointer                         user_data)
+{
+       EmpathyContactWidget  *information;
+       EmpathyAccountChooser *chooser;
+
+       g_return_if_fail (GTK_IS_WIDGET (widget));
+
+       information = g_object_get_data (G_OBJECT (widget), "EmpathyContactWidget");
+       if (!information) {
+               return;
+       }
+
+       chooser = EMPATHY_ACCOUNT_CHOOSER (information->widget_account);
+       if (chooser) {
+               empathy_account_chooser_set_filter (chooser, filter, user_data);
+       }
+}
        
 static void
 contact_widget_destroy_cb (GtkWidget            *widget,
@@ -271,6 +306,12 @@ static void
 contact_widget_set_contact (EmpathyContactWidget *information,
                            EmpathyContact        *contact)
 {
+       if (contact == information->contact ||
+           (contact && information->contact &&
+            empathy_contact_equal (contact, information->contact))) {
+               return;
+       }
+
        contact_widget_remove_contact (information);
        if (contact) {
                information->contact = g_object_ref (contact);
@@ -283,42 +324,10 @@ contact_widget_set_contact (EmpathyContactWidget *information,
        contact_widget_client_update (information);
 }
 
-static gboolean
-contact_widget_can_add_contact_to_account (McAccount *account,
-                                          gpointer   user_data)
-{
-       MissionControl *mc;
-       TpConn         *tp_conn;
-       McProfile      *profile;
-       const gchar    *protocol_name;
-
-       mc = empathy_mission_control_new ();
-       tp_conn = mission_control_get_connection (mc, account, NULL);
-       g_object_unref (mc);
-       if (tp_conn == NULL) {
-               /* Account is disconnected */
-               return FALSE;
-       }
-       g_object_unref (tp_conn);
-
-       profile = mc_account_get_profile (account);
-       protocol_name = mc_profile_get_protocol_name (profile);
-       if (strcmp (protocol_name, "local-xmpp") == 0) {
-               /* We can't add accounts to a XMPP LL connection
-                * FIXME: We should inspect the flags of the contact list group interface
-                */
-               g_object_unref (profile);
-               return FALSE;
-       }
-
-       g_object_unref (profile);
-       return TRUE;
-}
-
 static gboolean
 contact_widget_id_activate_timeout (EmpathyContactWidget *self)
 {
-       contact_widget_update_contact (self);
+       contact_widget_change_contact (self);
        return FALSE;
 }
 
@@ -331,28 +340,31 @@ contact_widget_id_changed_cb (GtkEntry             *entry,
        }
 
        self->widget_id_timeout =
-               g_timeout_add (ID_CHANGED_TIMEOUT,
-                              (GSourceFunc) contact_widget_id_activate_timeout,
-                              self);
+               g_timeout_add_seconds (ID_CHANGED_TIMEOUT,
+                                      (GSourceFunc) contact_widget_id_activate_timeout,
+                                      self);
 }
 
 static void
 contact_widget_contact_setup (EmpathyContactWidget *information)
 {
-       /* FIXME: Use EmpathyAvatarImage if (editable && is_user)  */
-       information->widget_avatar = gtk_image_new ();
-       gtk_box_pack_end (GTK_BOX (information->hbox_contact),
-                         information->widget_avatar,
-                         FALSE, FALSE,
-                         6);
+       if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR) {
+               information->widget_avatar = empathy_avatar_chooser_new ();
+               g_signal_connect (information->widget_avatar, "changed",
+                                 G_CALLBACK (contact_widget_avatar_changed_cb),
+                                 information);
+       } else {
+               information->widget_avatar = empathy_avatar_image_new ();
+       }
+       gtk_box_pack_start (GTK_BOX (information->vbox_avatar),
+                           information->widget_avatar,
+                           FALSE, FALSE,
+                           6);
+       gtk_widget_show (information->widget_avatar);
 
        /* Setup account label/chooser */
-       if (information->type == CONTACT_WIDGET_TYPE_ADD) {
+       if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT) {
                information->widget_account = empathy_account_chooser_new ();
-               empathy_account_chooser_set_filter (
-                       EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
-                       contact_widget_can_add_contact_to_account,
-                       NULL);
 
                g_signal_connect (information->widget_account, "changed",
                                  G_CALLBACK (contact_widget_account_changed_cb),
@@ -368,7 +380,7 @@ contact_widget_contact_setup (EmpathyContactWidget *information)
        gtk_widget_show (information->widget_account);
 
        /* Setup id label/entry */
-       if (information->type == CONTACT_WIDGET_TYPE_ADD) {
+       if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID) {
                information->widget_id = gtk_entry_new ();
                g_signal_connect (information->widget_id, "focus-out-event",
                                  G_CALLBACK (contact_widget_id_focus_out_cb),
@@ -387,7 +399,7 @@ contact_widget_contact_setup (EmpathyContactWidget *information)
        gtk_widget_show (information->widget_id);
 
        /* Setup alias label/entry */
-       if (information->type > CONTACT_WIDGET_TYPE_SHOW) {
+       if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ALIAS) {
                information->widget_alias = gtk_entry_new ();
                g_signal_connect (information->widget_alias, "focus-out-event",
                                  G_CALLBACK (contact_widget_entry_alias_focus_event_cb),
@@ -426,7 +438,7 @@ contact_widget_contact_update (EmpathyContactWidget *information)
        }
 
        /* Update account widget */
-       if (information->type == CONTACT_WIDGET_TYPE_ADD) {
+       if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT) {
                if (account) {
                        g_signal_handlers_block_by_func (information->widget_account,
                                                         contact_widget_account_changed_cb,
@@ -437,9 +449,6 @@ contact_widget_contact_update (EmpathyContactWidget *information)
                                                           contact_widget_account_changed_cb,
                                                           information);
                }
-               if (!G_STR_EMPTY (id)) {
-                       gtk_entry_set_text (GTK_ENTRY (information->widget_id), id);
-               }
        } else {
                if (account) {
                        const gchar *name;
@@ -447,9 +456,14 @@ contact_widget_contact_update (EmpathyContactWidget *information)
                        name = mc_account_get_display_name (account);
                        gtk_label_set_label (GTK_LABEL (information->widget_account), name);
                }
-               gtk_label_set_label (GTK_LABEL (information->widget_id), id);
        }
 
+       /* Update id widget */
+       if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID) {
+               gtk_entry_set_text (GTK_ENTRY (information->widget_id), id ? id : "");
+       } else {
+               gtk_label_set_label (GTK_LABEL (information->widget_id), id ? id : "");
+       }
        /* Update other widgets */
        if (information->contact) {
                contact_widget_name_notify_cb (information);
@@ -459,6 +473,7 @@ contact_widget_contact_update (EmpathyContactWidget *information)
                gtk_widget_show (information->label_alias);
                gtk_widget_show (information->widget_alias);
                gtk_widget_show (information->hbox_presence);
+               gtk_widget_show (information->widget_avatar);
        } else {
                gtk_widget_hide (information->label_alias);
                gtk_widget_hide (information->widget_alias);
@@ -467,35 +482,63 @@ contact_widget_contact_update (EmpathyContactWidget *information)
        }
 }
 
-static gboolean
-contact_widget_update_contact (EmpathyContactWidget *information)
+static void
+contact_widget_change_contact (EmpathyContactWidget *information)
 {
-       McAccount   *account;
-       const gchar *id;
+       EmpathyContact *contact;
+       McAccount      *account;
 
        account = empathy_account_chooser_get_account (EMPATHY_ACCOUNT_CHOOSER (information->widget_account));
-       id = gtk_entry_get_text (GTK_ENTRY (information->widget_id));
+       if (!account) {
+               return;
+       }
+
+       if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID) {
+               const gchar *id;
 
-       if (account && !G_STR_EMPTY (id)) {
-               EmpathyContact *contact;
+               id = gtk_entry_get_text (GTK_ENTRY (information->widget_id));
+               if (G_STR_EMPTY (id)) {
+                       return;
+               }
 
                contact = empathy_contact_factory_get_from_id (information->factory,
                                                               account, id);
-               contact_widget_set_contact (information, contact);
+       } else {
+               contact = empathy_contact_factory_get_user (information->factory,
+                                                           account);
+       }
 
-               if (contact) {
-                       g_object_unref (contact);
-               }
+       if (contact) {
+               contact_widget_set_contact (information, contact);
+               g_object_unref (contact);
        }
+}
 
-       return FALSE;
+static void
+contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
+                                 EmpathyContactWidget *information)
+{
+       if (information->contact &&
+           empathy_contact_is_user (information->contact)) {
+               McAccount   *account;
+               const gchar *data;
+               gsize        size;
+               const gchar *mime_type;
+
+               account = empathy_contact_get_account (information->contact);
+               empathy_avatar_chooser_get_image_data (EMPATHY_AVATAR_CHOOSER (information->widget_avatar),
+                                                      &data, &size, &mime_type);
+               empathy_contact_factory_set_avatar (information->factory,
+                                                   account,
+                                                   data, size, mime_type);
+       }
 }
 
 static void
 contact_widget_account_changed_cb (GtkComboBox          *widget,
                                   EmpathyContactWidget *information)
 {
-       contact_widget_update_contact (information);
+       contact_widget_change_contact (information);
 }
 
 static gboolean
@@ -503,7 +546,7 @@ contact_widget_id_focus_out_cb (GtkWidget            *widget,
                                GdkEventFocus        *event,
                                EmpathyContactWidget *information)
 {
-       contact_widget_update_contact (information);
+       contact_widget_change_contact (information);
        return FALSE;
 }
 
@@ -513,12 +556,12 @@ contact_widget_entry_alias_focus_event_cb (GtkEditable          *editable,
                                           EmpathyContactWidget *information)
 {
        if (information->contact) {
-               const gchar *name;
+               const gchar *alias;
 
-               name = gtk_entry_get_text (GTK_ENTRY (editable));
-               empathy_contact_factory_set_name (information->factory,
+               alias = gtk_entry_get_text (GTK_ENTRY (editable));
+               empathy_contact_factory_set_alias (information->factory,
                                                  information->contact,
-                                                 name);
+                                                 alias);
        }
 
        return FALSE;
@@ -550,25 +593,30 @@ contact_widget_presence_notify_cb (EmpathyContactWidget *information)
 static void
 contact_widget_avatar_notify_cb (EmpathyContactWidget *information)
 {
-       GdkPixbuf *avatar_pixbuf;
-
-       avatar_pixbuf = empathy_pixbuf_avatar_from_contact_scaled (information->contact,
-                                                                 48, 48);
+       EmpathyAvatar *avatar = NULL;
 
-       if (avatar_pixbuf) {
-               gtk_image_set_from_pixbuf (GTK_IMAGE (information->widget_avatar),
-                                          avatar_pixbuf);
-               gtk_widget_show  (information->widget_avatar);
-               g_object_unref (avatar_pixbuf);
+       if (information->contact) {
+               avatar = empathy_contact_get_avatar (information->contact);
+       }
+       if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR) {
+               g_signal_handlers_block_by_func (information->widget_avatar,
+                                                contact_widget_avatar_changed_cb,
+                                                information);
+               empathy_avatar_chooser_set (EMPATHY_AVATAR_CHOOSER (information->widget_avatar),
+                                           avatar);
+               g_signal_handlers_unblock_by_func (information->widget_avatar,
+                                                  contact_widget_avatar_changed_cb,
+                                                  information);
        } else {
-               gtk_widget_hide  (information->widget_avatar);
+               empathy_avatar_image_set (EMPATHY_AVATAR_IMAGE (information->widget_avatar),
+                                         avatar);
        }
 }
 
 static void
 contact_widget_groups_setup (EmpathyContactWidget *information)
 {
-       if (information->type > CONTACT_WIDGET_TYPE_SHOW) {
+       if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS) {
                information->manager = empathy_contact_manager_new ();
                contact_widget_model_setup (information);
        }
@@ -577,7 +625,7 @@ contact_widget_groups_setup (EmpathyContactWidget *information)
 static void
 contact_widget_groups_update (EmpathyContactWidget *information)
 {
-       if (information->type > CONTACT_WIDGET_TYPE_SHOW &&
+       if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS &&
            information->contact) {
                g_signal_connect_swapped (information->contact, "notify::groups",
                                          G_CALLBACK (contact_widget_groups_notify_cb),