]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-protocol-chooser.c
individual-menu: stop creating single_individual
[empathy.git] / libempathy-gtk / empathy-protocol-chooser.c
index c9dbb40275a5b1982b08a9d12df168393acc7c8b..ff8dac123f653bf09be12c684dbfe01e3dfda5b3 100644 (file)
  *          Jonny Lamb <jonny.lamb@collabora.co.uk>
  */
 
-#include <config.h>
-
-#include <string.h>
-
-#include <telepathy-glib/util.h>
-
-#include <gtk/gtk.h>
+#include "config.h"
+#include "empathy-protocol-chooser.h"
 
-#include <libempathy/empathy-utils.h>
-#include <libempathy/empathy-connection-managers.h>
+#include <glib/gi18n-lib.h>
+#include <tp-account-widgets/tpaw-connection-managers.h>
+#include <tp-account-widgets/tpaw-pixbuf-utils.h>
+#include <tp-account-widgets/tpaw-utils.h>
 
-#include "empathy-protocol-chooser.h"
 #include "empathy-ui-utils.h"
+#include "empathy-utils.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
-#include <libempathy/empathy-debug.h>
+#include "empathy-debug.h"
 
 /**
  * SECTION:empathy-protocol-chooser
@@ -61,19 +58,15 @@ typedef struct
   GtkListStore *store;
 
   gboolean dispose_run;
-  EmpathyConnectionManagers *cms;
 
   EmpathyProtocolChooserFilterFunc filter_func;
   gpointer filter_user_data;
-
-  GHashTable *protocols;
 } EmpathyProtocolChooserPriv;
 
 enum
 {
   COL_ICON,
   COL_LABEL,
-  COL_CM,
   COL_PROTOCOL,
   COL_COUNT
 };
@@ -81,190 +74,44 @@ enum
 G_DEFINE_TYPE (EmpathyProtocolChooser, empathy_protocol_chooser,
     GTK_TYPE_COMBO_BOX);
 
-static gint
-protocol_chooser_sort_protocol_value (TpConnectionManagerProtocol *protocol)
-{
-  guint i;
-  const gchar *names[] = {
-    "jabber",
-    "local-xmpp",
-    "gtalk",
-    NULL
-  };
-
-  for (i = 0 ; names[i]; i++)
-    {
-      if (strcmp (protocol->name, names[i]) == 0)
-        return i;
-    }
-
-  return i;
-}
-
-static gint
-protocol_chooser_sort_func (GtkTreeModel *model,
-    GtkTreeIter  *iter_a,
-    GtkTreeIter  *iter_b,
-    gpointer      user_data)
-{
-  TpConnectionManagerProtocol *protocol_a;
-  TpConnectionManagerProtocol *protocol_b;
-  gint cmp = 0;
-
-  gtk_tree_model_get (model, iter_a,
-      COL_PROTOCOL, &protocol_a,
-      -1);
-  gtk_tree_model_get (model, iter_b,
-      COL_PROTOCOL, &protocol_b,
-      -1);
-
-  cmp = protocol_chooser_sort_protocol_value (protocol_a);
-  cmp -= protocol_chooser_sort_protocol_value (protocol_b);
-  if (cmp == 0)
-    {
-      cmp = strcmp (protocol_a->name, protocol_b->name);
-    }
-
-  return cmp;
-}
-
-static const char *
-protocol_chooser_proto_name_to_display_name (const gchar *proto_name)
-{
-  int i;
-  static struct {
-    const gchar *proto;
-    const gchar *display;
-  } names[] = {
-    { "jabber", "Jabber" },
-    { "msn", "MSN" },
-    { "local-xmpp", N_("People Nearby") },
-    { "irc", "IRC" },
-    { "icq", "ICQ" },
-    { "aim", "AIM" },
-    { "yahoo", "Yahoo!" },
-    { "groupwise", "GroupWise" },
-    { "sip", "SIP" },
-    { NULL, NULL }
-  };
-
-  for (i = 0; names[i].proto != NULL; i++)
-    {
-      if (!tp_strdiff (proto_name, names[i].proto))
-        return names[i].display;
-    }
-
-  return NULL;
-}
-
 static void
-protocol_choosers_add_cm (EmpathyProtocolChooser *chooser,
-    TpConnectionManager *cm)
+protocol_chooser_add_protocol (EmpathyProtocolChooser *chooser,
+    TpawProtocol *protocol)
 {
   EmpathyProtocolChooserPriv *priv = GET_PRIV (chooser);
-  const TpConnectionManagerProtocol * const *iter;
+  GdkPixbuf *pixbuf;
 
-  for (iter = cm->protocols; iter != NULL && *iter != NULL; iter++)
-    {
-      const TpConnectionManagerProtocol *proto = *iter;
-      gchar *icon_name;
-      const gchar *display_name;
-      gchar *display_name_set;
-      const gchar *saved_cm_name;
-
-      saved_cm_name = g_hash_table_lookup (priv->protocols, proto->name);
-
-      if (!tp_strdiff (cm->name, "haze") && saved_cm_name != NULL &&
-          tp_strdiff (saved_cm_name, "haze"))
-        /* the CM we're adding is a haze implementation of something we already
-         * have; drop it.
-         */
-        continue;
-
-      if (tp_strdiff (cm->name, "haze") && !tp_strdiff (saved_cm_name, "haze"))
-        {
-          GtkTreeIter titer;
-          gboolean valid;
-          const TpConnectionManagerProtocol *haze_proto;
-          TpConnectionManager *haze_cm;
-
-          /* let's this CM replace the haze implementation */
-          valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->store),
-              &titer);
-
-          while (valid)
-            {
-              gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &titer,
-                  COL_PROTOCOL, &haze_proto,
-                  COL_CM, &haze_cm, -1);
-
-              if (haze_cm == NULL)
-                continue;
-
-              if (haze_proto == NULL)
-                {
-                  g_object_unref (haze_cm);
-                  continue;
-                }
-
-              if (!tp_strdiff (haze_cm->name, "haze") &&
-                  !tp_strdiff (haze_proto->name, proto->name))
-                {
-                  gtk_list_store_remove (priv->store, &titer);
-                  g_object_unref (haze_cm);
-                  break;
-                }
-
-              g_object_unref (haze_cm);
-              valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->store),
-                  &titer);
-            }
-        }
-
-      g_hash_table_insert (priv->protocols,
-          g_strdup (proto->name), g_strdup (cm->name));
-
-      icon_name = empathy_protocol_icon_name (proto->name);
-      display_name = protocol_chooser_proto_name_to_display_name (proto->name);
-
-      if (display_name == NULL)
-        display_name = proto->name;
-
-      gtk_list_store_insert_with_values (priv->store,
-          NULL, 0,
-          COL_ICON, icon_name,
-          COL_LABEL, display_name,
-          COL_CM, cm,
-          COL_PROTOCOL, proto,
-          -1);
+  pixbuf = tpaw_pixbuf_from_icon_name (tpaw_protocol_get_icon_name (protocol),
+      GTK_ICON_SIZE_BUTTON);
 
-      
+  gtk_list_store_insert_with_values (priv->store,
+      NULL, -1,
+      COL_ICON, pixbuf,
+      COL_LABEL, tpaw_protocol_get_display_name (protocol),
+      COL_PROTOCOL, protocol,
+      -1);
 
-      g_free (display_name_set);
-      g_free (icon_name);
-    }
+  g_clear_object (&pixbuf);
 }
 
 static void
-protocol_chooser_add_cms_list (EmpathyProtocolChooser *protocol_chooser,
-    GList *cms)
+protocol_chooser_get_protocols_cb (GObject *source,
+    GAsyncResult *result,
+    gpointer user_data)
 {
+  EmpathyProtocolChooser *protocol_chooser = user_data;
+  GList *protocols = NULL;
   GList *l;
 
-  for (l = cms; l != NULL; l = l->next)
-    protocol_choosers_add_cm (protocol_chooser, l->data);
+  if (!tpaw_protocol_get_all_finish (&protocols, result, NULL))
+    return;
+
+  for (l = protocols; l != NULL; l = l->next)
+    protocol_chooser_add_protocol (protocol_chooser, l->data);
 
   gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
-}
 
-static void
-protocol_chooser_cms_ready_cb (EmpathyConnectionManagers *cms,
-    GParamSpec *pspec,
-    EmpathyProtocolChooser *protocol_chooser)
-{
-  if (empathy_connection_managers_is_ready (cms))
-    protocol_chooser_add_cms_list
-        (protocol_chooser, empathy_connection_managers_get_cms (cms));
+  g_list_free_full (protocols, g_object_unref);
 }
 
 static void
@@ -279,19 +126,9 @@ protocol_chooser_constructed (GObject *object)
 
   /* set up combo box with new store */
   priv->store = gtk_list_store_new (COL_COUNT,
-          G_TYPE_STRING,    /* Icon name */
+          GDK_TYPE_PIXBUF,  /* Icon */
           G_TYPE_STRING,    /* Label     */
-          G_TYPE_OBJECT,    /* CM */
-          G_TYPE_POINTER);  /* protocol   */
-
-  /* Set the protocol sort function */
-  gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->store),
-      COL_PROTOCOL,
-      protocol_chooser_sort_func,
-      NULL, NULL);
-  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->store),
-      COL_PROTOCOL,
-      GTK_SORT_ASCENDING);
+          G_TYPE_OBJECT);   /* protocol */
 
   gtk_combo_box_set_model (GTK_COMBO_BOX (object),
       GTK_TREE_MODEL (priv->store));
@@ -299,9 +136,8 @@ protocol_chooser_constructed (GObject *object)
   renderer = gtk_cell_renderer_pixbuf_new ();
   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, FALSE);
   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), renderer,
-      "icon-name", COL_ICON,
+      "pixbuf", COL_ICON,
       NULL);
-  g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
 
   renderer = gtk_cell_renderer_text_new ();
   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), renderer, TRUE);
@@ -309,12 +145,7 @@ protocol_chooser_constructed (GObject *object)
       "text", COL_LABEL,
       NULL);
 
-  if (empathy_connection_managers_is_ready (priv->cms))
-    protocol_chooser_add_cms_list (protocol_chooser,
-        empathy_connection_managers_get_cms (priv->cms));
-  else
-    g_signal_connect (priv->cms, "notify::ready",
-        G_CALLBACK (protocol_chooser_cms_ready_cb), protocol_chooser);
+  tpaw_protocol_get_all_async (protocol_chooser_get_protocols_cb, protocol_chooser);
 
   if (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->constructed)
     G_OBJECT_CLASS
@@ -329,28 +160,9 @@ empathy_protocol_chooser_init (EmpathyProtocolChooser *protocol_chooser)
         EMPATHY_TYPE_PROTOCOL_CHOOSER, EmpathyProtocolChooserPriv);
 
   priv->dispose_run = FALSE;
-  priv->cms = empathy_connection_managers_dup_singleton ();
-  priv->protocols = g_hash_table_new_full (g_str_hash, g_str_equal,
-      g_free, g_free);
-
   protocol_chooser->priv = priv;
 }
 
-static void
-protocol_chooser_finalize (GObject *object)
-{
-  EmpathyProtocolChooser *protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (object);
-  EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
-
-  if (priv->protocols)
-    {
-      g_hash_table_destroy (priv->protocols);
-      priv->protocols = NULL;
-    }
-
-  (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->finalize) (object);
-}
-
 static void
 protocol_chooser_dispose (GObject *object)
 {
@@ -368,12 +180,6 @@ protocol_chooser_dispose (GObject *object)
       priv->store = NULL;
     }
 
-  if (priv->cms)
-    {
-      g_object_unref (priv->cms);
-      priv->cms = NULL;
-    }
-
   (G_OBJECT_CLASS (empathy_protocol_chooser_parent_class)->dispose) (object);
 }
 
@@ -384,7 +190,6 @@ empathy_protocol_chooser_class_init (EmpathyProtocolChooserClass *klass)
 
   object_class->constructed = protocol_chooser_constructed;
   object_class->dispose = protocol_chooser_dispose;
-  object_class->finalize = protocol_chooser_finalize;
 
   g_type_class_add_private (object_class, sizeof (EmpathyProtocolChooserPriv));
 }
@@ -396,16 +201,23 @@ protocol_chooser_filter_visible_func (GtkTreeModel *model,
 {
   EmpathyProtocolChooser *protocol_chooser = user_data;
   EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
-  TpConnectionManager *cm = NULL;
-  TpConnectionManagerProtocol *protocol = NULL;
+  TpawProtocol *protocol;
+  TpProtocol *tp_protocol;
   gboolean visible = FALSE;
 
-  gtk_tree_model_get (model, iter, COL_CM, &cm, COL_PROTOCOL, &protocol, -1);
+  gtk_tree_model_get (model, iter,
+      COL_PROTOCOL, &protocol,
+      -1);
+
+  tp_protocol = tp_connection_manager_get_protocol_object (
+      tpaw_protocol_get_cm (protocol),
+      tpaw_protocol_get_protocol_name (protocol));
 
-  if (cm != NULL && protocol != NULL)
+  if (tp_protocol != NULL)
     {
-      visible = priv->filter_func (cm, protocol, priv->filter_user_data);
-      g_object_unref (cm);
+      visible = priv->filter_func (tpaw_protocol_get_cm (protocol),
+          tp_protocol, tpaw_protocol_get_service_name (protocol),
+          priv->filter_user_data);
     }
 
   return visible;
@@ -414,22 +226,21 @@ protocol_chooser_filter_visible_func (GtkTreeModel *model,
 /* public methods */
 
 /**
- * empathy_protocol_chooser_get_selected_protocol:
+ * empathy_protocol_chooser_dup_selected:
  * @protocol_chooser: an #EmpathyProtocolChooser
  *
- * Returns a pointer to the selected #TpConnectionManagerProtocol in
+ * Returns a pointer to the selected #TpawProtocol in
  * @protocol_chooser.
  *
- * Return value: a pointer to the selected #TpConnectionManagerProtocol
+ * Return value: a pointer to the selected #TpawProtocol
  */
-TpConnectionManager *
+TpawProtocol *
 empathy_protocol_chooser_dup_selected (
-    EmpathyProtocolChooser *protocol_chooser,
-    TpConnectionManagerProtocol **protocol)
+    EmpathyProtocolChooser *protocol_chooser)
 {
   GtkTreeIter iter;
-  TpConnectionManager *cm = NULL;
   GtkTreeModel *cur_model;
+  TpawProtocol *protocol = NULL;
 
   g_return_val_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser), NULL);
 
@@ -441,16 +252,11 @@ empathy_protocol_chooser_dup_selected (
   if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (protocol_chooser), &iter))
     {
       gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
-          COL_CM, &cm,
+          COL_PROTOCOL, &protocol,
           -1);
-
-      if (protocol != NULL)
-        gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
-            COL_PROTOCOL, protocol,
-            -1);
     }
 
-  return cm;
+  return protocol;
 }
 
 /**
@@ -494,3 +300,20 @@ empathy_protocol_chooser_set_visible (EmpathyProtocolChooser *protocol_chooser,
 
   gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
 }
+
+TpawAccountSettings *
+empathy_protocol_chooser_create_account_settings (EmpathyProtocolChooser *self)
+{
+  TpawProtocol *protocol;
+  TpawAccountSettings *settings;
+
+  protocol = empathy_protocol_chooser_dup_selected (self);
+  if (protocol == NULL)
+    return NULL;
+
+  settings = tpaw_protocol_create_account_settings (protocol);
+
+  tp_clear_object (&protocol);
+
+  return settings;
+}