]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-account-chooser.c
empathy-call-window.c: update priv->sending_video *before* changing widget's state
[empathy.git] / libempathy-gtk / empathy-account-chooser.c
index 13040b0dc4ca90ef7c9f3b13c9ebcb1bc1f4114d..5d11e8e1e288d823fe2a30bcb9b4104996f8211c 100644 (file)
 
 #include <string.h>
 
-#include <glib/gi18n.h>
+#include <glib/gi18n-lib.h>
 #include <gtk/gtk.h>
-#include <glade/glade.h>
 
-#include <libmissioncontrol/mc-account-monitor.h>
 #include <libmissioncontrol/mission-control.h>
 
+#include <libempathy/empathy-account-manager.h>
 #include <libempathy/empathy-utils.h>
 
 #include "empathy-ui-utils.h"
 #include "empathy-account-chooser.h"
 
+/**
+ * SECTION:empathy-account-chooser
+ * @title:EmpathyAccountChooser
+ * @short_description: A widget used to choose from a list of accounts
+ * @include: libempathy-gtk/empathy-account-chooser.h
+ *
+ * #EmpathyAccountChooser is a widget which extends #GtkComboBox to provide
+ * a chooser of available accounts.
+ */
+
+/**
+ * EmpathyAccountChooser:
+ * @parent: parent object
+ *
+ * Widget which extends #GtkComboBox to provide a chooser of available accounts.
+ */
+
 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountChooser)
 typedef struct {
-       MissionControl                 *mc;
-       McAccountMonitor               *monitor;
+       EmpathyAccountManager          *manager;
        gboolean                        set_active_item;
        gboolean                        has_all_option;
        EmpathyAccountChooserFilterFunc filter;
        gpointer                        filter_data;
-       gpointer                        token;
 } EmpathyAccountChooserPriv;
 
 typedef struct {
@@ -73,23 +87,23 @@ static void     account_chooser_set_property           (GObject
                                                        const GValue             *value,
                                                        GParamSpec               *pspec);
 static void     account_chooser_setup                  (EmpathyAccountChooser    *chooser);
-static void     account_chooser_account_created_cb     (McAccountMonitor         *monitor,
-                                                       const gchar              *unique_name,
+static void     account_chooser_account_created_cb     (EmpathyAccountManager    *manager,
+                                                       McAccount                *account,
                                                        EmpathyAccountChooser    *chooser);
 static void     account_chooser_account_add_foreach    (McAccount                *account,
                                                        EmpathyAccountChooser    *chooser);
-static void     account_chooser_account_deleted_cb     (McAccountMonitor         *monitor,
-                                                       const gchar              *unique_name,
+static void     account_chooser_account_deleted_cb     (EmpathyAccountManager    *manager,
+                                                       McAccount                *account,
                                                        EmpathyAccountChooser    *chooser);
 static void     account_chooser_account_remove_foreach (McAccount                *account,
                                                        EmpathyAccountChooser    *chooser);
 static void     account_chooser_update_iter            (EmpathyAccountChooser    *chooser,
                                                        GtkTreeIter              *iter);
-static void     account_chooser_status_changed_cb      (MissionControl           *mc,
-                                                       TpConnectionStatus        status,
-                                                       McPresence                presence,
+static void     account_chooser_connection_changed_cb  (EmpathyAccountManager    *manager,
+                                                       McAccount                *account,
                                                        TpConnectionStatusReason  reason,
-                                                       const gchar              *unique_name,
+                                                       TpConnectionStatus        new_status,
+                                                       TpConnectionStatus        old_status,
                                                        EmpathyAccountChooser    *chooser);
 static gboolean account_chooser_separator_func         (GtkTreeModel             *model,
                                                        GtkTreeIter              *iter,
@@ -115,6 +129,11 @@ empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
        object_class->get_property = account_chooser_get_property;
        object_class->set_property = account_chooser_set_property;
 
+       /**
+        * EmpathyAccountChooser:has-all-option:
+        *
+        * Have an additional option in the list to mean all accounts.
+        */
        g_object_class_install_property (object_class,
                                         PROP_HAS_ALL_OPTION,
                                         g_param_spec_boolean ("has-all-option",
@@ -136,26 +155,37 @@ empathy_account_chooser_init (EmpathyAccountChooser *chooser)
        priv->set_active_item = FALSE;
        priv->filter = NULL;
        priv->filter_data = NULL;
+
+       priv->manager = empathy_account_manager_dup_singleton ();
+
+       g_signal_connect (priv->manager, "account-created",
+                         G_CALLBACK (account_chooser_account_created_cb),
+                         chooser);
+       g_signal_connect (priv->manager, "account-deleted",
+                         G_CALLBACK (account_chooser_account_deleted_cb),
+                         chooser);
+       g_signal_connect (priv->manager, "account-connection-changed",
+                         G_CALLBACK (account_chooser_connection_changed_cb),
+                         chooser);
+
+       account_chooser_setup (EMPATHY_ACCOUNT_CHOOSER (chooser));
 }
 
 static void
 account_chooser_finalize (GObject *object)
 {
-       EmpathyAccountChooser     *chooser;
-       EmpathyAccountChooserPriv *priv;
+       EmpathyAccountChooserPriv *priv = GET_PRIV (object);
 
-       chooser = EMPATHY_ACCOUNT_CHOOSER (object);
-       priv = GET_PRIV (object);
-
-       g_signal_handlers_disconnect_by_func (priv->monitor,
+       g_signal_handlers_disconnect_by_func (priv->manager,
+                                             account_chooser_connection_changed_cb,
+                                             object);
+       g_signal_handlers_disconnect_by_func (priv->manager,
                                              account_chooser_account_created_cb,
-                                             chooser);
-       g_signal_handlers_disconnect_by_func (priv->monitor,
+                                             object);
+       g_signal_handlers_disconnect_by_func (priv->manager,
                                              account_chooser_account_deleted_cb,
-                                             chooser);
-       empathy_disconnect_account_status_changed (priv->token);
-       g_object_unref (priv->mc);
-       g_object_unref (priv->monitor);
+                                             object);
+       g_object_unref (priv->manager);
 
        G_OBJECT_CLASS (empathy_account_chooser_parent_class)->finalize (object);
 }
@@ -201,38 +231,35 @@ account_chooser_set_property (GObject      *object,
        };
 }
 
+/**
+ * empathy_account_chooser_new:
+ *
+ * Creates a new #EmpathyAccountChooser.
+ *
+ * Return value: A new #EmpathyAccountChooser
+ */
 GtkWidget *
 empathy_account_chooser_new (void)
 {
-       EmpathyAccountChooserPriv *priv;
-       McAccountMonitor         *monitor;
        GtkWidget                *chooser;
 
-       monitor = mc_account_monitor_new ();
        chooser = g_object_new (EMPATHY_TYPE_ACCOUNT_CHOOSER, NULL);
 
-       priv = GET_PRIV (chooser);
-
-       priv->mc = empathy_mission_control_new ();
-       priv->monitor = mc_account_monitor_new ();
-
-       g_signal_connect (priv->monitor, "account-created",
-                         G_CALLBACK (account_chooser_account_created_cb),
-                         chooser);
-       g_signal_connect (priv->monitor, "account-deleted",
-                         G_CALLBACK (account_chooser_account_deleted_cb),
-                         chooser);
-       priv->token = empathy_connect_to_account_status_changed (priv->mc,
-                                                  G_CALLBACK (account_chooser_status_changed_cb),
-                                                  chooser, NULL);
-
-       account_chooser_setup (EMPATHY_ACCOUNT_CHOOSER (chooser));
-
        return chooser;
 }
 
+/**
+ * empathy_account_chooser_dup_account:
+ * @chooser: an #EmpathyAccountChooser
+ *
+ * Returns the account which is currently selected in the chooser or %NULL
+ * if there is no account selected. The #McAccount returned should be
+ * unrefed with g_object_unref() when finished with.
+ *
+ * Return value: a new ref to the #McAccount currently selected, or %NULL.
+ */
 McAccount *
-empathy_account_chooser_get_account (EmpathyAccountChooser *chooser)
+empathy_account_chooser_dup_account (EmpathyAccountChooser *chooser)
 {
        EmpathyAccountChooserPriv *priv;
        McAccount                *account;
@@ -244,13 +271,53 @@ empathy_account_chooser_get_account (EmpathyAccountChooser *chooser)
        priv = GET_PRIV (chooser);
 
        model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
-       gtk_combo_box_get_active_iter (GTK_COMBO_BOX (chooser), &iter);
+       if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (chooser), &iter)) {
+               return NULL;
+       }
 
        gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
 
        return account;
 }
 
+/**
+ * empathy_account_chooser_get_connection:
+ * @chooser: an #EmpathyAccountChooser
+ *
+ * Returns a borrowed reference to the #TpConnection associated with the
+ * account currently selected. The caller must reference the returned object with
+ * g_object_ref() if it will be kept
+ *
+ * Return value: a borrowed reference to the #TpConnection associated with the
+ * account curently selected.
+ */
+TpConnection *
+empathy_account_chooser_get_connection (EmpathyAccountChooser *chooser)
+{
+       EmpathyAccountChooserPriv *priv;
+       McAccount                 *account;
+       TpConnection              *connection;
+
+       g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
+
+       priv = GET_PRIV (chooser);
+
+       account = empathy_account_chooser_dup_account (chooser);
+       connection = empathy_account_manager_get_connection (priv->manager, account);
+       g_object_unref (account);
+
+       return connection;
+}
+
+/**
+ * empathy_account_chooser_set_account:
+ * @chooser: an #EmpathyAccountChooser
+ * @account: an #McAccount
+ *
+ * Sets the currently selected account to @account, if it exists in the list.
+ *
+ * Return value: whether the chooser was set to @account.
+ */
 gboolean
 empathy_account_chooser_set_account (EmpathyAccountChooser *chooser,
                                     McAccount             *account)
@@ -276,6 +343,16 @@ empathy_account_chooser_set_account (EmpathyAccountChooser *chooser,
        return data.set;
 }
 
+/**
+ * empathy_account_chooser_get_has_all_option:
+ * @chooser: an #EmpathyAccountChooser
+ *
+ * Returns whether @chooser has the #EmpathyAccountChooser:has-all-option property
+ * set to true.
+ *
+ * Return value: whether @chooser has the #EmpathyAccountChooser:has-all-option property
+ * enabled.
+ */
 gboolean
 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
 {
@@ -288,6 +365,13 @@ empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
        return priv->has_all_option;
 }
 
+/**
+ * empathy_account_chooser_set_has_all_option:
+ * @chooser: an #EmpathyAccountChooser
+ * @has_all_option: a new value for the #EmpathyAccountChooser:has-all-option property
+ *
+ * Sets the #EmpathyAccountChooser:has-all-option property.
+ */
 void
 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
                                           gboolean              has_all_option)
@@ -403,15 +487,11 @@ account_chooser_setup (EmpathyAccountChooser *chooser)
 }
 
 static void
-account_chooser_account_created_cb (McAccountMonitor     *monitor,
-                                   const gchar          *unique_name,
+account_chooser_account_created_cb (EmpathyAccountManager *manager,
+                                   McAccount             *account,
                                    EmpathyAccountChooser *chooser)
 {
-       McAccount *account;
-
-       account = mc_account_lookup (unique_name);
        account_chooser_account_add_foreach (account, chooser);
-       g_object_unref (account);
 }
 
 static void
@@ -434,15 +514,11 @@ account_chooser_account_add_foreach (McAccount             *account,
 }
 
 static void
-account_chooser_account_deleted_cb (McAccountMonitor     *monitor,
-                                   const gchar          *unique_name,
+account_chooser_account_deleted_cb (EmpathyAccountManager *manager,
+                                   McAccount             *account,
                                    EmpathyAccountChooser *chooser)
 {
-       McAccount *account;
-
-       account = mc_account_lookup (unique_name);
        account_chooser_account_remove_foreach (account, chooser);
-       g_object_unref (account);
 }
 
 typedef struct {
@@ -553,21 +629,18 @@ account_chooser_update_iter (EmpathyAccountChooser *chooser,
 }
 
 static void
-account_chooser_status_changed_cb (MissionControl           *mc,
-                                  TpConnectionStatus        status,
-                                  McPresence                presence,
-                                  TpConnectionStatusReason  reason,
-                                  const gchar              *unique_name,
-                                  EmpathyAccountChooser    *chooser)
+account_chooser_connection_changed_cb (EmpathyAccountManager   *manager,
+                                      McAccount               *account,
+                                      TpConnectionStatusReason reason,
+                                      TpConnectionStatus       new_status,
+                                      TpConnectionStatus       old_status,
+                                      EmpathyAccountChooser   *chooser)
 {
-       McAccount   *account;
-       GtkTreeIter  iter;
+       GtkTreeIter iter;
 
-       account = mc_account_lookup (unique_name);
        if (account_chooser_find_account (chooser, account, &iter)) {
                account_chooser_update_iter (chooser, &iter);
        }
-       g_object_unref (account);
 }
 
 static gboolean
@@ -639,6 +712,15 @@ account_chooser_filter_foreach (GtkTreeModel *model,
        return FALSE;
 }
 
+/**
+ * empathy_account_chooser_set_filter:
+ * @chooser: an #EmpathyAccountChooser
+ * @filter: a filter
+ * @user_data: data to pass to @filter, or %NULL
+ *
+ * Sets a filter on the @chooser so only accounts that are %TRUE in the eyes
+ * of the filter are visible in the @chooser.
+ */
 void
 empathy_account_chooser_set_filter (EmpathyAccountChooser           *chooser,
                                     EmpathyAccountChooserFilterFunc  filter,
@@ -660,6 +742,27 @@ empathy_account_chooser_set_filter (EmpathyAccountChooser           *chooser,
        gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
 }
 
+/**
+ * EmpathyAccountChooserFilterFunc:
+ * @account: an #McAccount
+ * @user_data: user data, or %NULL
+ *
+ * A function which decides whether the account indicated by @account
+ * is visible.
+ *
+ * Return value: whether the account indicated by @account is visible.
+ */
+
+/**
+ * empathy_account_chooser_filter_is_connected:
+ * @account: an #McAccount
+ * @user_data: user data or %NULL
+ *
+ * A useful #EmpathyAccountChooserFilterFunc that one could pass into
+ * empathy_account_chooser_set_filter() and only show connected accounts.
+ *
+ * Return value: Whether @account is connected
+ */
 gboolean
 empathy_account_chooser_filter_is_connected (McAccount *account,
                                             gpointer   user_data)
@@ -669,7 +772,7 @@ empathy_account_chooser_filter_is_connected (McAccount *account,
 
        g_return_val_if_fail (MC_IS_ACCOUNT (account), FALSE);
 
-       mc = empathy_mission_control_new ();
+       mc = empathy_mission_control_dup_singleton ();
        status = mission_control_get_connection_status (mc, account, NULL);
        g_object_unref (mc);