]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-idle.c
Merge branch 'sasl'
[empathy.git] / libempathy / empathy-idle.c
index 8a0fcd88d1bcebc3904cb3fc352eeb4de4632d56..12686daa4e449472395bb4f8cee6fb611d3ac43e 100644 (file)
@@ -50,9 +50,10 @@ typedef struct {
        EmpathyConnectivity *connectivity;
        gulong state_change_signal_id;
 
+       gboolean ready;
+
        TpConnectionPresenceType      state;
        gchar          *status;
-       TpConnectionPresenceType      flash_state;
        gboolean        auto_away;
 
        TpConnectionPresenceType      away_saved_state;
@@ -63,6 +64,7 @@ typedef struct {
        guint           ext_away_timeout;
 
        TpAccountManager *manager;
+       gulong idle_presence_changed_id;
 
        /* pointer to a TpAccount --> glong of time of connection */
        GHashTable *connect_times;
@@ -84,7 +86,6 @@ enum {
        PROP_0,
        PROP_STATE,
        PROP_STATUS,
-       PROP_FLASH_STATE,
        PROP_AUTO_AWAY
 };
 
@@ -225,31 +226,20 @@ idle_session_status_changed_cb (DBusGProxy    *gs_proxy,
                        priv->away_saved_state, new_state);
                empathy_idle_set_state (idle, new_state);
        } else if (!is_idle && priv->is_idle) {
-               const gchar *new_status;
                /* We are no more idle, restore state */
 
                idle_ext_away_stop (idle);
 
-               if (priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_AWAY ||
-                   priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
-                       priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
-                       new_status = NULL;
-               } else {
-                       new_status = priv->status;
-               }
-
                /* Only try and set the presence if the away saved state is not
                 * unset. This is an odd case because it means that the session
                 * didn't notify us of the state change to idle, and as a
                 * result, we couldn't save the current state at that time.
                 */
                if (priv->away_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
-                       DEBUG ("Restoring state to %d, reset status to %s",
-                               priv->away_saved_state, new_status);
+                       DEBUG ("Restoring state to %d",
+                               priv->away_saved_state);
 
-                       empathy_idle_set_presence (idle,
-                                                  priv->away_saved_state,
-                                                  new_status);
+                       empathy_idle_set_state (idle,priv->away_saved_state);
                } else {
                        DEBUG ("Away saved state is unset. This means that we "
                               "weren't told when the session went idle. "
@@ -312,6 +302,12 @@ idle_finalize (GObject *object)
                                     priv->state_change_signal_id);
        priv->state_change_signal_id = 0;
 
+       if (priv->manager != NULL) {
+               g_signal_handler_disconnect (priv->manager,
+                       priv->idle_presence_changed_id);
+               g_object_unref (priv->manager);
+       }
+
        g_object_unref (priv->connectivity);
 
        g_hash_table_destroy (priv->connect_times);
@@ -340,6 +336,24 @@ idle_constructor (GType type,
        return retval;
 }
 
+static const gchar *
+empathy_idle_get_status (EmpathyIdle *idle)
+{
+       EmpathyIdlePriv *priv;
+
+       priv = GET_PRIV (idle);
+
+       if (G_UNLIKELY (!priv->ready))
+               g_critical (G_STRLOC ": %s called before AccountManager ready",
+                               G_STRFUNC);
+
+       if (!priv->status) {
+               return empathy_presence_get_default_message (priv->state);
+       }
+
+       return priv->status;
+}
+
 static void
 idle_get_property (GObject    *object,
                   guint       param_id,
@@ -359,9 +373,6 @@ idle_get_property (GObject    *object,
        case PROP_STATUS:
                g_value_set_string (value, empathy_idle_get_status (idle));
                break;
-       case PROP_FLASH_STATE:
-               g_value_set_enum (value, empathy_idle_get_flash_state (idle));
-               break;
        case PROP_AUTO_AWAY:
                g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
                break;
@@ -390,9 +401,6 @@ idle_set_property (GObject      *object,
        case PROP_STATUS:
                empathy_idle_set_status (idle, g_value_get_string (value));
                break;
-       case PROP_FLASH_STATE:
-               empathy_idle_set_flash_state (idle, g_value_get_enum (value));
-               break;
        case PROP_AUTO_AWAY:
                empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
                break;
@@ -427,14 +435,6 @@ empathy_idle_class_init (EmpathyIdleClass *klass)
                                                              "status",
                                                              NULL,
                                                              G_PARAM_READWRITE));
-       g_object_class_install_property (object_class,
-                                        PROP_FLASH_STATE,
-                                        g_param_spec_uint ("flash-state",
-                                                           "flash-state",
-                                                           "flash-state",
-                                                           0, NUM_TP_CONNECTION_PRESENCE_TYPES,
-                                                           TP_CONNECTION_PRESENCE_TYPE_UNSET,
-                                                           G_PARAM_READWRITE));
 
         g_object_class_install_property (object_class,
                                          PROP_AUTO_AWAY,
@@ -474,14 +474,21 @@ account_manager_ready_cb (GObject *source_object,
                          GAsyncResult *result,
                          gpointer user_data)
 {
-       EmpathyIdle *idle = EMPATHY_IDLE (user_data);
+       EmpathyIdle *idle = user_data;
        TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
-       EmpathyIdlePriv *priv = GET_PRIV (idle);
+       EmpathyIdlePriv *priv;
        TpConnectionPresenceType state;
        gchar *status, *status_message;
        GList *accounts, *l;
        GError *error = NULL;
 
+       /* In case we've been finalized before reading this callback */
+       if (idle_singleton == NULL)
+               return;
+
+       priv = GET_PRIV (idle);
+       priv->ready = TRUE;
+
        if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
                DEBUG ("Failed to prepare account manager: %s", error->message);
                g_error_free (error);
@@ -496,9 +503,9 @@ account_manager_ready_cb (GObject *source_object,
 
        accounts = tp_account_manager_get_valid_accounts (priv->manager);
        for (l = accounts; l != NULL; l = l->next) {
-               empathy_signal_connect_weak (l->data, "status-changed",
+               tp_g_signal_connect_object (l->data, "status-changed",
                                             G_CALLBACK (account_status_changed_cb),
-                                            G_OBJECT (idle));
+                                            idle, 0);
        }
        g_list_free (accounts);
 
@@ -511,6 +518,7 @@ empathy_idle_init (EmpathyIdle *idle)
 {
        EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
                EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
+       TpDBusDaemon *dbus;
 
        idle->priv = priv;
        priv->is_idle = FALSE;
@@ -520,10 +528,14 @@ empathy_idle_init (EmpathyIdle *idle)
        tp_account_manager_prepare_async (priv->manager, NULL,
            account_manager_ready_cb, idle);
 
-       g_signal_connect (priv->manager, "most-available-presence-changed",
+       priv->idle_presence_changed_id = g_signal_connect (priv->manager,
+               "most-available-presence-changed",
                G_CALLBACK (idle_presence_changed_cb), idle);
 
-       priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
+       dbus = tp_dbus_daemon_dup (NULL);
+
+       priv->gs_proxy = dbus_g_proxy_new_for_name (
+                                                   tp_proxy_get_dbus_connection (dbus),
                                                    "org.gnome.SessionManager",
                                                    "/org/gnome/SessionManager/Presence",
                                                    "org.gnome.SessionManager.Presence");
@@ -537,6 +549,8 @@ empathy_idle_init (EmpathyIdle *idle)
                DEBUG ("Failed to get gs proxy");
        }
 
+       g_object_unref (dbus);
+
        priv->connectivity = empathy_connectivity_dup_singleton ();
        priv->state_change_signal_id = g_signal_connect (priv->connectivity,
            "state-change", G_CALLBACK (idle_state_change_cb), idle);
@@ -557,6 +571,10 @@ empathy_idle_get_state (EmpathyIdle *idle)
 
        priv = GET_PRIV (idle);
 
+       if (G_UNLIKELY (!priv->ready))
+               g_critical (G_STRLOC ": %s called before AccountManager ready",
+                               G_STRFUNC);
+
        return priv->state;
 }
 
@@ -571,20 +589,6 @@ empathy_idle_set_state (EmpathyIdle *idle,
        empathy_idle_set_presence (idle, state, priv->status);
 }
 
-const gchar *
-empathy_idle_get_status (EmpathyIdle *idle)
-{
-       EmpathyIdlePriv *priv;
-
-       priv = GET_PRIV (idle);
-
-       if (!priv->status) {
-               return empathy_presence_get_default_message (priv->state);
-       }
-
-       return priv->status;
-}
-
 void
 empathy_idle_set_status (EmpathyIdle *idle,
                         const gchar *status)
@@ -596,32 +600,6 @@ empathy_idle_set_status (EmpathyIdle *idle,
        empathy_idle_set_presence (idle, priv->state, status);
 }
 
-TpConnectionPresenceType
-empathy_idle_get_flash_state (EmpathyIdle *idle)
-{
-       EmpathyIdlePriv *priv;
-
-       priv = GET_PRIV (idle);
-
-       return priv->flash_state;
-}
-
-void
-empathy_idle_set_flash_state (EmpathyIdle *idle,
-                             TpConnectionPresenceType   state)
-{
-       EmpathyIdlePriv *priv;
-
-       priv = GET_PRIV (idle);
-
-       priv->flash_state = state;
-
-       if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
-       }
-
-       g_object_notify (G_OBJECT (idle), "flash-state");
-}
-
 static void
 empathy_idle_do_set_presence (EmpathyIdle *idle,
                           TpConnectionPresenceType status_type,