]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-contact-widget.c
- Split info/edit/personal dialogs into different functions.
[empathy.git] / libempathy-gtk / empathy-contact-widget.c
index 6b70bcaafebfbfc488cd2d5c2eb7d1a10e5cde7e..165d48b442a73b184967c51524aaba5200855a3c 100644 (file)
@@ -1,6 +1,6 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /*
- * Copyright (C) 2007-2008 Collabora Ltd.
+ * Copyright (C) 2007-2009 Collabora Ltd.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
 #include <stdlib.h>
 
 #include <gtk/gtk.h>
-#include <glade/glade.h>
-#include <glib/gi18n.h>
+#include <glib/gi18n-lib.h>
 
 #include <libmissioncontrol/mc-account.h>
 #include <telepathy-glib/util.h>
 
-#include <libempathy/empathy-contact-factory.h>
+#include <libempathy/empathy-tp-contact-factory.h>
 #include <libempathy/empathy-contact-manager.h>
 #include <libempathy/empathy-contact-list.h>
 #include <libempathy/empathy-utils.h>
 #include "empathy-avatar-image.h"
 #include "empathy-ui-utils.h"
 
+#define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
+#include <libempathy/empathy-debug.h>
+
 /* Delay before updating the widget when the id entry changed (seconds) */
 #define ID_CHANGED_TIMEOUT 1
 
 typedef struct
 {
-  EmpathyContactFactory *factory;
+  EmpathyTpContactFactory *factory;
   EmpathyContactManager *manager;
   EmpathyContact *contact;
   EmpathyContactWidgetFlags flags;
-  GtkCellRenderer *renderer;
   guint widget_id_timeout;
 
   GtkWidget *vbox_contact_widget;
@@ -105,8 +106,6 @@ static void contact_widget_contact_update (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,
     GdkEventFocus *event, EmpathyContactWidget *information);
 static gboolean contact_widget_entry_alias_focus_event_cb (
@@ -157,18 +156,18 @@ empathy_contact_widget_new (EmpathyContact *contact,
                             EmpathyContactWidgetFlags flags)
 {
   EmpathyContactWidget *information;
-  GladeXML *glade;
+  GtkBuilder *gui;
+  TpConnection *connection;
   gchar *filename;
 
+  g_return_val_if_fail (contact == NULL || EMPATHY_IS_CONTACT (contact), NULL);
+
   information = g_slice_new0 (EmpathyContactWidget);
   information->flags = flags;
-  information->factory = empathy_contact_factory_new ();
 
-  filename = empathy_file_lookup ("empathy-contact-widget.glade",
+  filename = empathy_file_lookup ("empathy-contact-widget.ui",
       "libempathy-gtk");
-  glade = empathy_glade_get_file (filename,
-      "vbox_contact_widget",
-       NULL,
+  gui = empathy_builder_get_file (filename,
        "vbox_contact_widget", &information->vbox_contact_widget,
        "vbox_contact", &information->vbox_contact,
        "hbox_presence", &information->hbox_presence,
@@ -190,16 +189,13 @@ empathy_contact_widget_new (EmpathyContact *contact,
        NULL);
   g_free (filename);
 
-  empathy_glade_connect (glade,
-      information,
+  empathy_builder_connect (gui, information,
       "vbox_contact_widget", "destroy", contact_widget_destroy_cb,
       "entry_group", "changed", contact_widget_entry_group_changed_cb,
       "entry_group", "activate", contact_widget_entry_group_activate_cb,
       "button_group", "clicked", contact_widget_button_group_clicked_cb,
       NULL);
 
-  g_object_unref (glade);
-
   g_object_set_data (G_OBJECT (information->vbox_contact_widget),
       "EmpathyContactWidget",
       information);
@@ -210,11 +206,11 @@ empathy_contact_widget_new (EmpathyContact *contact,
   contact_widget_details_setup (information);
   contact_widget_client_setup (information);
 
-  contact_widget_set_contact (information, contact);
-
-  gtk_widget_show (information->vbox_contact_widget);
+  if (contact != NULL)
+      contact_widget_set_contact (information, contact);
 
-  return information->vbox_contact_widget;
+  return empathy_builder_unref_and_keep_widget (gui,
+    information->vbox_contact_widget);
 }
 
 EmpathyContact *
@@ -277,10 +273,6 @@ contact_widget_destroy_cb (GtkWidget *widget,
     {
       g_source_remove (information->widget_id_timeout);
     }
-  if (information->factory)
-    {
-      g_object_unref (information->factory);
-    }   
   if (information->manager)
     {
       g_object_unref (information->manager);
@@ -304,7 +296,9 @@ contact_widget_remove_contact (EmpathyContactWidget *information)
           contact_widget_groups_notify_cb, information);
 
       g_object_unref (information->contact);
+      g_object_unref (information->factory);
       information->contact = NULL;
+      information->factory = NULL;
     }
 }
 
@@ -317,7 +311,13 @@ contact_widget_set_contact (EmpathyContactWidget *information,
 
   contact_widget_remove_contact (information);
   if (contact)
+    {
+      TpConnection *connection;
+
+      connection = empathy_contact_get_connection (contact);
       information->contact = g_object_ref (contact);
+      information->factory = empathy_tp_contact_factory_dup_singleton (connection);
+    }
 
   /* Update information for widgets */
   contact_widget_contact_update (information);
@@ -493,39 +493,26 @@ widget_avatar_button_press_event_cb (GtkWidget *widget,
 }
 
 static void
-contact_widget_contact_setup (EmpathyContactWidget *information)
+update_avatar_chooser_account_cb (EmpathyAccountChooser *account_chooser,
+                                  EmpathyAvatarChooser *avatar_chooser)
 {
-  if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_AVATAR)
-    {
-      information->widget_avatar = empathy_avatar_chooser_new (
-          information->factory);
-      g_signal_connect (information->widget_avatar, "changed",
-            G_CALLBACK (contact_widget_avatar_changed_cb),
-            information);
-    }
-  else
-    {
-      information->widget_avatar = empathy_avatar_image_new ();
+  TpConnection *connection;
 
-      g_signal_connect (information->widget_avatar, "popup-menu",
-          G_CALLBACK (widget_avatar_popup_menu_cb), information);
-      g_signal_connect (information->widget_avatar, "button-press-event",
-          G_CALLBACK (widget_avatar_button_press_event_cb), information);
-    }
-  
-  gtk_box_pack_start (GTK_BOX (information->vbox_avatar),
-          information->widget_avatar,
-          FALSE, FALSE,
-          6);
-  gtk_widget_show (information->widget_avatar);
+  connection = empathy_account_chooser_get_connection (account_chooser);
+  g_object_set (avatar_chooser, "connection", connection, NULL);
+}
 
+static void
+contact_widget_contact_setup (EmpathyContactWidget *information)
+{
   /* Setup account label/chooser */
   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
     {
       information->widget_account = empathy_account_chooser_new ();
 
-      g_signal_connect (information->widget_account, "changed",
-            G_CALLBACK (contact_widget_account_changed_cb),
+      contact_widget_change_contact (information);
+      g_signal_connect_swapped (information->widget_account, "changed",
+            G_CALLBACK (contact_widget_change_contact),
             information);
     }
   else
@@ -541,6 +528,39 @@ contact_widget_contact_setup (EmpathyContactWidget *information)
            1, 2, 0, 1);
   gtk_widget_show (information->widget_account);
 
+  /* Set up avatar chooser/display */
+  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);
+      if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT)
+        {
+          g_signal_connect (information->widget_account, "changed",
+              G_CALLBACK (update_avatar_chooser_account_cb),
+              information->widget_avatar);
+          update_avatar_chooser_account_cb (
+              EMPATHY_ACCOUNT_CHOOSER (information->widget_account),
+              EMPATHY_AVATAR_CHOOSER (information->widget_avatar));
+        }
+    }
+  else
+    {
+      information->widget_avatar = empathy_avatar_image_new ();
+
+      g_signal_connect (information->widget_avatar, "popup-menu",
+          G_CALLBACK (widget_avatar_popup_menu_cb), information);
+      g_signal_connect (information->widget_avatar, "button-press-event",
+          G_CALLBACK (widget_avatar_button_press_event_cb), information);
+    }
+
+  gtk_box_pack_start (GTK_BOX (information->vbox_avatar),
+          information->widget_avatar,
+          FALSE, FALSE,
+          6);
+  gtk_widget_show (information->widget_avatar);
+
   /* Setup id label/entry */
   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
     {
@@ -622,12 +642,12 @@ contact_widget_contact_update (EmpathyContactWidget *information)
       if (account)
         {
           g_signal_handlers_block_by_func (information->widget_account,
-                   contact_widget_account_changed_cb,
+                   contact_widget_change_contact,
                    information);
           empathy_account_chooser_set_account (
               EMPATHY_ACCOUNT_CHOOSER (information->widget_account), account);
           g_signal_handlers_unblock_by_func (information->widget_account,
-              contact_widget_account_changed_cb, information);
+              contact_widget_change_contact, information);
         }
     }
   else
@@ -668,15 +688,47 @@ contact_widget_contact_update (EmpathyContactWidget *information)
     }
 }
 
+static void
+contact_widget_got_contact_cb (EmpathyTpContactFactory *factory,
+                               GList *contacts,
+                               gpointer user_data,
+                               GObject *weak_object)
+{
+  EmpathyContactWidget *information = user_data;
+
+  contact_widget_set_contact (information, contacts->data);
+}
+
+static void
+contact_widget_get_self_handle_cb (TpConnection *connection,
+                                   TpHandle self_handle,
+                                   const GError *error,
+                                   gpointer information,
+                                   GObject *weak_object)
+{
+  EmpathyTpContactFactory *factory;
+
+  if (error != NULL)
+    {
+      DEBUG ("Error: %s", error->message);
+      return;
+    }
+
+  factory = empathy_tp_contact_factory_dup_singleton (connection);
+  empathy_tp_contact_factory_get_from_handles (factory, 1, &self_handle,
+      contact_widget_got_contact_cb, information, NULL,
+      weak_object);
+  g_object_unref (factory);
+}
+
 static void
 contact_widget_change_contact (EmpathyContactWidget *information)
 {
-  EmpathyContact *contact;
-  McAccount *account;
+  TpConnection *connection;
 
-  account = empathy_account_chooser_get_account (
+  connection = empathy_account_chooser_get_connection (
       EMPATHY_ACCOUNT_CHOOSER (information->widget_account));
-  if (!account)
+  if (!connection)
       return;
 
   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_ID)
@@ -684,26 +736,23 @@ contact_widget_change_contact (EmpathyContactWidget *information)
       const gchar *id;
 
       id = gtk_entry_get_text (GTK_ENTRY (information->widget_id));
-      if (G_STR_EMPTY (id))
-          return;
+      if (!EMP_STR_EMPTY (id))
+        {
+          EmpathyTpContactFactory *factory;
 
-      contact = empathy_contact_factory_get_from_id (information->factory,
-          account, id);
+          factory = empathy_tp_contact_factory_dup_singleton (connection);
+          empathy_tp_contact_factory_get_from_ids (factory, 1, &id,
+              contact_widget_got_contact_cb, information, NULL,
+              G_OBJECT (information->vbox_contact_widget));
+          g_object_unref (factory);
+        }
     }
   else
     {
-      contact = empathy_contact_factory_get_user (information->factory,
-          account);
-    }
-
-  if (contact)
-    {
-      empathy_contact_run_until_ready (contact,
-          EMPATHY_CONTACT_READY_HANDLE |
-          EMPATHY_CONTACT_READY_ID,
-          NULL);
-      contact_widget_set_contact (information, contact);
-      g_object_unref (contact);
+      /* FIXME: TpConnection should have a SelfHandle property */
+      tp_cli_connection_call_get_self_handle (connection, -1,
+          contact_widget_get_self_handle_cb, information, NULL,
+          G_OBJECT (information->vbox_contact_widget));
     }
 }
 
@@ -722,18 +771,11 @@ contact_widget_avatar_changed_cb (EmpathyAvatarChooser *chooser,
       empathy_avatar_chooser_get_image_data (
           EMPATHY_AVATAR_CHOOSER (information->widget_avatar),
           &data, &size, &mime_type);
-      empathy_contact_factory_set_avatar (information->factory, account,
+      empathy_tp_contact_factory_set_avatar (information->factory,
           data, size, mime_type);
     }
 }
 
-static void
-contact_widget_account_changed_cb (GtkComboBox *widget,
-                                   EmpathyContactWidget *information)
-{
-  contact_widget_change_contact (information);
-}
-
 static gboolean
 contact_widget_id_focus_out_cb (GtkWidget *widget,
                                 GdkEventFocus *event,
@@ -753,7 +795,7 @@ contact_widget_entry_alias_focus_event_cb (GtkEditable *editable,
       const gchar *alias;
 
       alias = gtk_entry_get_text (GTK_ENTRY (editable));
-      empathy_contact_factory_set_alias (information->factory,
+      empathy_tp_contact_factory_set_alias (information->factory,
           information->contact, alias);
     }
 
@@ -809,7 +851,7 @@ contact_widget_groups_setup (EmpathyContactWidget *information)
 {
   if (information->flags & EMPATHY_CONTACT_WIDGET_EDIT_GROUPS)
     {
-      information->manager = empathy_contact_manager_new ();
+      information->manager = empathy_contact_manager_dup_singleton ();
       contact_widget_model_setup (information);
     }
 }
@@ -895,11 +937,6 @@ contact_widget_model_populate_columns (EmpathyContactWidget *information)
   gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
   gtk_tree_view_column_set_resizable (column,FALSE);
   gtk_tree_view_column_set_clickable (GTK_TREE_VIEW_COLUMN (column), TRUE);
-
-  if (information->renderer)
-      g_object_unref (information->renderer);
-
-  information->renderer = g_object_ref (renderer);
 }
 
 static void
@@ -960,7 +997,7 @@ contact_widget_model_find_name (EmpathyContactWidget *information,
   GtkTreeModel *model;
   FindName data;
 
-  if (G_STR_EMPTY (name))
+  if (EMP_STR_EMPTY (name))
       return FALSE;
 
   data.information = information;
@@ -1072,7 +1109,7 @@ contact_widget_entry_group_changed_cb (GtkEditable *editable,
       gtk_widget_set_sensitive (GTK_WIDGET (information->button_group), FALSE);
   else
       gtk_widget_set_sensitive (GTK_WIDGET (information->button_group),
-          !G_STR_EMPTY (group));
+          !EMP_STR_EMPTY (group));
 }
 
 static void