]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-protocol-chooser.c
coding style fixes
[empathy.git] / libempathy-gtk / empathy-protocol-chooser.c
index ebb198455201c853ed7e0fcfbe081dba7e0715ea..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 <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
 typedef struct
 {
   GtkListStore *store;
+
   gboolean dispose_run;
 
+  EmpathyProtocolChooserFilterFunc filter_func;
+  gpointer filter_user_data;
 } EmpathyProtocolChooserPriv;
 
 enum
 {
   COL_ICON,
   COL_LABEL,
-  COL_CM,
   COL_PROTOCOL,
   COL_COUNT
 };
@@ -74,107 +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",
-    "salut",
-    "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;
-
-  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 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;
-
-  for (iter = cm->protocols; iter != NULL && *iter != NULL; iter++)
-    {
-      const TpConnectionManagerProtocol *proto = *iter;
-      gchar *icon_name;
-      gchar *display_name;
-
+  GdkPixbuf *pixbuf;
 
-      icon_name = g_strdup_printf ("im-%s", proto->name);
+  pixbuf = tpaw_pixbuf_from_icon_name (tpaw_protocol_get_icon_name (protocol),
+      GTK_ICON_SIZE_BUTTON);
 
-      if (!tp_strdiff (cm->name, "haze"))
-        display_name = g_strdup_printf ("%s (Haze)", proto->name);
-      else
-        display_name = g_strdup (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);
+  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);
-      g_free (icon_name);
-    }
+  g_clear_object (&pixbuf);
 }
 
-
 static void
-protocol_choosers_cms_listed (TpConnectionManager * const *cms,
-    gsize n_cms,
-    const GError *error,
-    gpointer user_data,
-    GObject *weak_object)
+protocol_chooser_get_protocols_cb (GObject *source,
+    GAsyncResult *result,
+    gpointer user_data)
 {
-  TpConnectionManager * const *iter;
+  EmpathyProtocolChooser *protocol_chooser = user_data;
+  GList *protocols = NULL;
+  GList *l;
 
-  if (error !=NULL)
-    {
-      DEBUG ("Failed to get connection managers: %s", error->message);
-      return;
-    }
+  if (!tpaw_protocol_get_all_finish (&protocols, result, NULL))
+    return;
 
-  for (iter = cms ; iter != NULL && *iter != NULL; iter++)
-    protocol_choosers_add_cm (EMPATHY_PROTOCOL_CHOOSER (weak_object),
-      *iter);
+  for (l = protocols; l != NULL; l = l->next)
+    protocol_chooser_add_protocol (protocol_chooser, l->data);
 
-  gtk_combo_box_set_active (GTK_COMBO_BOX (weak_object), 0);
+  gtk_combo_box_set_active (GTK_COMBO_BOX (protocol_chooser), 0);
+
+  g_list_free_full (protocols, g_object_unref);
 }
 
 static void
@@ -182,19 +119,16 @@ protocol_chooser_constructed (GObject *object)
 {
   EmpathyProtocolChooser *protocol_chooser;
   EmpathyProtocolChooserPriv *priv;
-
   GtkCellRenderer *renderer;
-  TpDBusDaemon *dbus;
 
   priv = GET_PRIV (object);
   protocol_chooser = EMPATHY_PROTOCOL_CHOOSER (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   */
+          G_TYPE_OBJECT);   /* protocol */
 
   gtk_combo_box_set_model (GTK_COMBO_BOX (object),
       GTK_TREE_MODEL (priv->store));
@@ -202,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);
@@ -212,22 +145,11 @@ protocol_chooser_constructed (GObject *object)
       "text", COL_LABEL,
       NULL);
 
-  dbus = tp_dbus_daemon_dup (NULL);
-  tp_list_connection_managers (dbus, protocol_choosers_cms_listed,
-    NULL, NULL, object);
-  g_object_unref (dbus);
-
-  /* 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);
+  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 (empathy_protocol_chooser_parent_class)->constructed (object);
+    G_OBJECT_CLASS
+      (empathy_protocol_chooser_parent_class)->constructed (object);
 }
 
 static void
@@ -238,7 +160,6 @@ empathy_protocol_chooser_init (EmpathyProtocolChooser *protocol_chooser)
         EMPATHY_TYPE_PROTOCOL_CHOOSER, EmpathyProtocolChooserPriv);
 
   priv->dispose_run = FALSE;
-
   protocol_chooser->priv = priv;
 }
 
@@ -273,67 +194,126 @@ empathy_protocol_chooser_class_init (EmpathyProtocolChooserClass *klass)
   g_type_class_add_private (object_class, sizeof (EmpathyProtocolChooserPriv));
 }
 
-/**
- * empathy_protocol_chooser_get_selected_protocol:
- * @protocol_chooser: an #EmpathyProtocolChooser
- *
- * Returns a pointer to the selected #TpConnectionManagerProtocol in
- * @protocol_chooser.
- *
- * Return value: a pointer to the selected #TpConnectionManagerProtocol
- */
-TpConnectionManager *empathy_protocol_chooser_dup_selected (
-    EmpathyProtocolChooser *protocol_chooser,
-    TpConnectionManagerProtocol **protocol)
+static gboolean
+protocol_chooser_filter_visible_func (GtkTreeModel *model,
+    GtkTreeIter *iter,
+    gpointer user_data)
 {
+  EmpathyProtocolChooser *protocol_chooser = user_data;
   EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
-  GtkTreeIter iter;
-  TpConnectionManager *cm = NULL;
+  TpawProtocol *protocol;
+  TpProtocol *tp_protocol;
+  gboolean visible = FALSE;
 
-  g_return_val_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser), NULL);
+  gtk_tree_model_get (model, iter,
+      COL_PROTOCOL, &protocol,
+      -1);
 
-  if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (protocol_chooser), &iter))
-    {
-      gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
-          COL_CM, &cm,
-          -1);
+  tp_protocol = tp_connection_manager_get_protocol_object (
+      tpaw_protocol_get_cm (protocol),
+      tpaw_protocol_get_protocol_name (protocol));
 
-      if (protocol != NULL)
-        gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
-            COL_PROTOCOL, protocol,
-            -1);
+  if (tp_protocol != NULL)
+    {
+      visible = priv->filter_func (tpaw_protocol_get_cm (protocol),
+          tp_protocol, tpaw_protocol_get_service_name (protocol),
+          priv->filter_user_data);
     }
 
-  return cm;
+  return visible;
 }
 
+/* public methods */
+
 /**
- * empathy_protocol_chooser_n_protocols:
+ * empathy_protocol_chooser_dup_selected:
  * @protocol_chooser: an #EmpathyProtocolChooser
  *
- * Returns the number of protocols in @protocol_chooser.
+ * Returns a pointer to the selected #TpawProtocol in
+ * @protocol_chooser.
  *
- * Return value: the number of protocols in @protocol_chooser
+ * Return value: a pointer to the selected #TpawProtocol
  */
-gint
-empathy_protocol_chooser_n_protocols (EmpathyProtocolChooser *protocol_chooser)
+TpawProtocol *
+empathy_protocol_chooser_dup_selected (
+    EmpathyProtocolChooser *protocol_chooser)
 {
-  EmpathyProtocolChooserPriv *priv = GET_PRIV (protocol_chooser);
+  GtkTreeIter iter;
+  GtkTreeModel *cur_model;
+  TpawProtocol *protocol = NULL;
+
+  g_return_val_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser), NULL);
+
+  /* get the current model from the chooser, as we could either be filtering
+   * or not.
+   */
+  cur_model = gtk_combo_box_get_model (GTK_COMBO_BOX (protocol_chooser));
 
-  g_return_val_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser), 0);
+  if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (protocol_chooser), &iter))
+    {
+      gtk_tree_model_get (GTK_TREE_MODEL (cur_model), &iter,
+          COL_PROTOCOL, &protocol,
+          -1);
+    }
 
-  return gtk_tree_model_iter_n_children (GTK_TREE_MODEL (priv->store), NULL);
+  return protocol;
 }
 
 /**
  * empathy_protocol_chooser_new:
  *
- * Creates a new #EmpathyProtocolChooser widget.
+ * Triggers the creation of a new #EmpathyProtocolChooser.
  *
  * Return value: a new #EmpathyProtocolChooser widget
  */
+
 GtkWidget *
 empathy_protocol_chooser_new (void)
 {
   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_PROTOCOL_CHOOSER, NULL));
 }
+
+void
+empathy_protocol_chooser_set_visible (EmpathyProtocolChooser *protocol_chooser,
+    EmpathyProtocolChooserFilterFunc func,
+    gpointer user_data)
+{
+  EmpathyProtocolChooserPriv *priv;
+  GtkTreeModel *filter_model;
+
+  g_return_if_fail (EMPATHY_IS_PROTOCOL_CHOOSER (protocol_chooser));
+
+  priv = GET_PRIV (protocol_chooser);
+  priv->filter_func = func;
+  priv->filter_user_data = user_data;
+
+  filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store),
+      NULL);
+  gtk_combo_box_set_model (GTK_COMBO_BOX (protocol_chooser), filter_model);
+  g_object_unref (filter_model);
+
+  gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER
+      (filter_model), protocol_chooser_filter_visible_func,
+      protocol_chooser, NULL);
+
+  gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (filter_model));
+
+  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;
+}