]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-idle.c
Add empathy_tp_chat_get_connection() to direct access the channel's connection.
[empathy.git] / libempathy / empathy-idle.c
index 77e7c1ffa2297b277deb63a223c7c98e529003fa..eeb183892e64ddab5ec42931ff1431997d01a051 100644 (file)
@@ -1,21 +1,20 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /*
- * Copyright (C) 2007 Collabora Ltd.
+ * Copyright (C) 2007-2008 Collabora Ltd.
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of the
- * License, or (at your option) any later version.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This program is distributed in the hope that it will be useful,
+ * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public
- * License along with this program; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  * 
  * Authors: Xavier Claessens <xclaesse@gmail.com>
  */
 
 #include <string.h>
 
-#include <glib/gi18n.h>
+#include <glib/gi18n-lib.h>
 #include <dbus/dbus-glib.h>
 
-#include <libtelepathy/tp-helpers.h>
+#include <telepathy-glib/dbus.h>
+#include <telepathy-glib/util.h>
 #include <libmissioncontrol/mc-enum-types.h>
 
 #include "empathy-idle.h"
 #include "empathy-utils.h" 
-#include "empathy-debug.h"
-
-#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
-                      EMPATHY_TYPE_IDLE, EmpathyIdlePriv))
 
-#define DEBUG_DOMAIN "Idle"
+#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
+#include "empathy-debug.h"
 
 /* Number of seconds before entering extended autoaway. */
 #define EXT_AWAY_TIME (30*60)
 
-typedef enum {
-       NM_STATE_UNKNOWN,
-       NM_STATE_ASLEEP,
-       NM_STATE_CONNECTING,
-       NM_STATE_CONNECTED,
-       NM_STATE_DISCONNECTED
-} NMState;
-
-struct _EmpathyIdlePriv {
+#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIdle)
+typedef struct {
        MissionControl *mc;
        DBusGProxy     *gs_proxy;
        DBusGProxy     *nm_proxy;
+
        McPresence      state;
-       McPresence      flash_state;
        gchar          *status;
-       McPresence      saved_state;
-       gchar          *saved_status;
+       McPresence      flash_state;
+       gboolean        auto_away;
+       gboolean        use_nm;
+
+       McPresence      away_saved_state;
+       McPresence      nm_saved_state;
+       gchar          *nm_saved_status;
+
        gboolean        is_idle;
        gboolean        nm_connected;
-       gboolean        auto_away;
-       gboolean        auto_disconnect;
        guint           ext_away_timeout;
-};
+} EmpathyIdlePriv;
 
-static void     empathy_idle_class_init      (EmpathyIdleClass *klass);
-static void     empathy_idle_init            (EmpathyIdle      *idle);
-static void     idle_finalize                (GObject          *object);
-static void     idle_get_property            (GObject          *object,
-                                             guint             param_id,
-                                             GValue           *value,
-                                             GParamSpec       *pspec);
-static void     idle_set_property            (GObject          *object,
-                                             guint             param_id,
-                                             const GValue     *value,
-                                             GParamSpec       *pspec);
-static void     idle_presence_changed_cb     (MissionControl   *mc,
-                                             McPresence        state,
-                                             EmpathyIdle      *idle);
-static void     idle_session_idle_changed_cb (DBusGProxy       *gs_proxy,
-                                             gboolean          is_idle,
-                                             EmpathyIdle      *idle);
-static void     idle_nm_state_change_cb      (DBusGProxy       *proxy,
-                                             guint             state,
-                                             EmpathyIdle      *idle);
-static void     idle_ext_away_start          (EmpathyIdle      *idle);
-static void     idle_ext_away_stop           (EmpathyIdle      *idle);
-static gboolean idle_ext_away_cb             (EmpathyIdle      *idle);
+typedef enum {
+       NM_STATE_UNKNOWN,
+       NM_STATE_ASLEEP,
+       NM_STATE_CONNECTING,
+       NM_STATE_CONNECTED,
+       NM_STATE_DISCONNECTED
+} NMState;
 
 enum {
        PROP_0,
@@ -96,146 +74,195 @@ enum {
        PROP_STATUS,
        PROP_FLASH_STATE,
        PROP_AUTO_AWAY,
-       PROP_AUTO_DISCONNECT
+       PROP_USE_NM
 };
 
-G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT)
+G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT);
+
+static EmpathyIdle * idle_singleton = NULL;
 
 static void
-empathy_idle_class_init (EmpathyIdleClass *klass)
+idle_presence_changed_cb (MissionControl *mc,
+                         McPresence      state,
+                         gchar          *status,
+                         EmpathyIdle    *idle)
 {
-       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       EmpathyIdlePriv *priv;
 
-       object_class->finalize = idle_finalize;
-       object_class->get_property = idle_get_property;
-       object_class->set_property = idle_set_property;
+       priv = GET_PRIV (idle);
 
-       g_object_class_install_property (object_class,
-                                        PROP_STATE,
-                                        g_param_spec_enum ("state",
-                                                           "state",
-                                                           "state",
-                                                           MC_TYPE_PRESENCE,
-                                                           MC_PRESENCE_AVAILABLE,
-                                                           G_PARAM_READWRITE));
-       g_object_class_install_property (object_class,
-                                        PROP_STATUS,
-                                        g_param_spec_string ("status",
-                                                             "status",
-                                                             "status",
-                                                             NULL,
-                                                             G_PARAM_READWRITE));
-       g_object_class_install_property (object_class,
-                                        PROP_FLASH_STATE,
-                                        g_param_spec_enum ("flash-state",
-                                                           "flash-state",
-                                                           "flash-state",
-                                                           MC_TYPE_PRESENCE,
-                                                           MC_PRESENCE_UNSET,
-                                                           G_PARAM_READWRITE));
+       DEBUG ("Presence changed to '%s' (%d)", status, state);
 
-        g_object_class_install_property (object_class,
-                                         PROP_AUTO_AWAY,
-                                         g_param_spec_boolean ("auto-away",
-                                                               "Automatic set presence to away",
-                                                               "Should it set presence to away if inactive",
-                                                               FALSE,
-                                                               G_PARAM_READWRITE));
+       g_free (priv->status);
+       priv->state = state;
+       priv->status = NULL;
+       if (!EMP_STR_EMPTY (status)) {
+               priv->status = g_strdup (status);
+       }
 
-        g_object_class_install_property (object_class,
-                                         PROP_AUTO_DISCONNECT,
-                                         g_param_spec_boolean ("auto-disconnect",
-                                                               "Automatic set presence to offline",
-                                                               "Should it set presence to offline if NM is disconnected",
-                                                               FALSE,
-                                                               G_PARAM_READWRITE));
+       g_object_notify (G_OBJECT (idle), "state");
+       g_object_notify (G_OBJECT (idle), "status");
+}
 
-       g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
+static gboolean
+idle_ext_away_cb (EmpathyIdle *idle)
+{
+       EmpathyIdlePriv *priv;
+
+       priv = GET_PRIV (idle);
+
+       DEBUG ("Going to extended autoaway");
+       empathy_idle_set_state (idle, MC_PRESENCE_EXTENDED_AWAY);
+       priv->ext_away_timeout = 0;
+
+       return FALSE;
 }
 
 static void
-empathy_idle_init (EmpathyIdle *idle)
+idle_ext_away_stop (EmpathyIdle *idle)
 {
        EmpathyIdlePriv *priv;
-       DBusGConnection *system_bus;
-       GError          *error = NULL;
 
        priv = GET_PRIV (idle);
 
-       priv->is_idle = FALSE;
-       priv->mc = empathy_mission_control_new ();
-       priv->state = mission_control_get_presence_actual (priv->mc, NULL);
-       idle_presence_changed_cb (priv->mc, priv->state, idle);
+       if (priv->ext_away_timeout) {
+               g_source_remove (priv->ext_away_timeout);
+               priv->ext_away_timeout = 0;
+       }
+}
 
-       dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
-                                    "PresenceStatusActual",
-                                    G_CALLBACK (idle_presence_changed_cb),
-                                    idle, NULL);
+static void
+idle_ext_away_start (EmpathyIdle *idle)
+{
+       EmpathyIdlePriv *priv;
 
-       priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
-                                                   "org.gnome.ScreenSaver",
-                                                   "/org/gnome/ScreenSaver",
-                                                   "org.gnome.ScreenSaver");
-       if (priv->gs_proxy) {
-               dbus_g_proxy_add_signal (priv->gs_proxy, "SessionIdleChanged",
-                                        G_TYPE_BOOLEAN,
-                                        G_TYPE_INVALID);
-               dbus_g_proxy_connect_signal (priv->gs_proxy, "SessionIdleChanged",
-                                            G_CALLBACK (idle_session_idle_changed_cb),
-                                            idle, NULL);
-       } else {
-               empathy_debug (DEBUG_DOMAIN, "Failed to get gs proxy");
+       priv = GET_PRIV (idle);
+
+       if (priv->ext_away_timeout != 0) {
+               return;
        }
+       priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
+                                                       (GSourceFunc) idle_ext_away_cb,
+                                                       idle);
+}
 
+static void
+idle_session_idle_changed_cb (DBusGProxy  *gs_proxy,
+                             gboolean     is_idle,
+                             EmpathyIdle *idle)
+{
+       EmpathyIdlePriv *priv;
 
-       system_bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
-       if (!system_bus) {
-               empathy_debug (DEBUG_DOMAIN, "Failed to get system bus: %s",
-                             error ? error->message : "No error given");
-       } else {
-               priv->nm_proxy = dbus_g_proxy_new_for_name (system_bus,
-                                                           "org.freedesktop.NetworkManager",
-                                                           "/org/freedesktop/NetworkManager",
-                                                           "org.freedesktop.NetworkManager");
+       priv = GET_PRIV (idle);
+
+       DEBUG ("Session idle state changed, %s -> %s",
+               priv->is_idle ? "yes" : "no",
+               is_idle ? "yes" : "no");
+
+       if (!priv->auto_away ||
+           (priv->nm_saved_state == MC_PRESENCE_UNSET &&
+            (priv->state <= MC_PRESENCE_OFFLINE ||
+             priv->state == MC_PRESENCE_HIDDEN))) {
+               /* We don't want to go auto away OR we explicitely asked to be
+                * offline, nothing to do here */
+               priv->is_idle = is_idle;
+               return;
        }
-       if (priv->nm_proxy) {
-               guint nm_status;
 
-               dbus_g_proxy_add_signal (priv->nm_proxy, "StateChange",
-                                        G_TYPE_UINT, G_TYPE_INVALID);
-               dbus_g_proxy_connect_signal (priv->nm_proxy, "StateChange",
-                                            G_CALLBACK (idle_nm_state_change_cb),
-                                            idle, NULL);
-               dbus_g_proxy_call (priv->nm_proxy, "state",
-                                  &error,
-                                  G_TYPE_INVALID,
-                                  G_TYPE_UINT, &nm_status,
-                                  G_TYPE_INVALID);
+       if (is_idle && !priv->is_idle) {
+               McPresence new_state;
+               /* We are now idle */
 
-               if (error) {
-                       /* Can't get actual status, NM is not working. */
-                       empathy_debug (DEBUG_DOMAIN, 
-                                      "Couldn't get NM state: %s",
-                                      error->message);
-                       g_clear_error (&error);
-                       g_object_unref (priv->nm_proxy);
-                       priv->nm_proxy = NULL;
-                       priv->nm_connected = TRUE;
+               idle_ext_away_start (idle);
+
+               if (priv->nm_saved_state != MC_PRESENCE_UNSET) {
+                       /* We are disconnected, when coming back from away
+                        * we want to restore the presence before the
+                        * disconnection. */
+                       priv->away_saved_state = priv->nm_saved_state;
                } else {
-                       priv->nm_connected = (nm_status == NM_STATE_CONNECTED);
+                       priv->away_saved_state = priv->state;
+               }
 
-                       empathy_debug (DEBUG_DOMAIN, "NetworkManager connected: %s",
-                                      priv->nm_connected ? "Yes" : "No");
+               new_state = MC_PRESENCE_AWAY;
+               if (priv->state == MC_PRESENCE_EXTENDED_AWAY) {
+                       new_state = MC_PRESENCE_EXTENDED_AWAY;
                }
 
-               if (!priv->nm_connected) {
-                       priv->saved_state = priv->state;
-                       priv->saved_status = g_strdup (priv->status);
+               DEBUG ("Going to autoaway. Saved state=%d, new state=%d",
+                       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 == MC_PRESENCE_AWAY ||
+                   priv->away_saved_state == MC_PRESENCE_EXTENDED_AWAY) {
+                       priv->away_saved_state = MC_PRESENCE_AVAILABLE;
+                       new_status = NULL;
+               } else {
+                       new_status = priv->status;
                }
-       } else {
-               empathy_debug (DEBUG_DOMAIN, "Failed to get nm proxy");
-               priv->nm_connected = TRUE;
+
+               DEBUG ("Restoring state to %d, reset status to %s",
+                       priv->away_saved_state, new_status);
+
+               empathy_idle_set_presence (idle,
+                                          priv->away_saved_state,
+                                          new_status);
+
+               priv->away_saved_state = MC_PRESENCE_UNSET;
+       }
+
+       priv->is_idle = is_idle;
+}
+
+static void
+idle_nm_state_change_cb (DBusGProxy  *proxy,
+                        guint        state,
+                        EmpathyIdle *idle)
+{
+       EmpathyIdlePriv *priv;
+       gboolean         old_nm_connected;
+       gboolean         new_nm_connected;
+
+       priv = GET_PRIV (idle);
+
+       if (!priv->use_nm) {
+               return;
+       }
+
+       old_nm_connected = priv->nm_connected;
+       new_nm_connected = !(state == NM_STATE_CONNECTING ||
+                            state == NM_STATE_DISCONNECTED);
+       priv->nm_connected = TRUE; /* To be sure _set_state will work */
+
+       DEBUG ("New network state %d", state);
+
+       if (old_nm_connected && !new_nm_connected) {
+               /* We are no more connected */
+               DEBUG ("Disconnected: Save state %d (%s)",
+                               priv->state, priv->status);
+               priv->nm_saved_state = priv->state;
+               g_free (priv->nm_saved_status);
+               priv->nm_saved_status = g_strdup (priv->status);
+               empathy_idle_set_state (idle, MC_PRESENCE_OFFLINE);
        }
+       else if (!old_nm_connected && new_nm_connected) {
+               /* We are now connected */
+               DEBUG ("Reconnected: Restore state %d (%s)",
+                               priv->nm_saved_state, priv->nm_saved_status);
+               empathy_idle_set_presence (idle,
+                               priv->nm_saved_state,
+                               priv->nm_saved_status);
+               priv->nm_saved_state = MC_PRESENCE_UNSET;
+               g_free (priv->nm_saved_status);
+               priv->nm_saved_status = NULL;
+       }
+
+       priv->nm_connected = new_nm_connected;
 }
 
 static void
@@ -246,7 +273,6 @@ idle_finalize (GObject *object)
        priv = GET_PRIV (object);
 
        g_free (priv->status);
-       g_free (priv->saved_status);
        g_object_unref (priv->mc);
 
        if (priv->gs_proxy) {
@@ -256,6 +282,26 @@ idle_finalize (GObject *object)
        idle_ext_away_stop (EMPATHY_IDLE (object));
 }
 
+static GObject *
+idle_constructor (GType type,
+                 guint n_props,
+                 GObjectConstructParam *props)
+{
+       GObject *retval;
+
+       if (idle_singleton) {
+               retval = g_object_ref (idle_singleton);
+       } else {
+               retval = G_OBJECT_CLASS (empathy_idle_parent_class)->constructor
+                       (type, n_props, props);
+
+               idle_singleton = EMPATHY_IDLE (retval);
+               g_object_add_weak_pointer (retval, (gpointer) &idle_singleton);
+       }
+
+       return retval;
+}
+
 static void
 idle_get_property (GObject    *object,
                   guint       param_id,
@@ -281,8 +327,8 @@ idle_get_property (GObject    *object,
        case PROP_AUTO_AWAY:
                g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
                break;
-       case PROP_AUTO_DISCONNECT:
-               g_value_set_boolean (value, empathy_idle_get_auto_disconnect (idle));
+       case PROP_USE_NM:
+               g_value_set_boolean (value, empathy_idle_get_use_nm (idle));
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
@@ -315,25 +361,145 @@ idle_set_property (GObject      *object,
        case PROP_AUTO_AWAY:
                empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
                break;
+       case PROP_USE_NM:
+               empathy_idle_set_use_nm (idle, g_value_get_boolean (value));
+               break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
                break;
        };
 }
 
-EmpathyIdle *
-empathy_idle_new (void)
+static void
+empathy_idle_class_init (EmpathyIdleClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+       object_class->finalize = idle_finalize;
+       object_class->constructor = idle_constructor;
+       object_class->get_property = idle_get_property;
+       object_class->set_property = idle_set_property;
+
+       g_object_class_install_property (object_class,
+                                        PROP_STATE,
+                                        g_param_spec_enum ("state",
+                                                           "state",
+                                                           "state",
+                                                           MC_TYPE_PRESENCE,
+                                                           MC_PRESENCE_AVAILABLE,
+                                                           G_PARAM_READWRITE));
+       g_object_class_install_property (object_class,
+                                        PROP_STATUS,
+                                        g_param_spec_string ("status",
+                                                             "status",
+                                                             "status",
+                                                             NULL,
+                                                             G_PARAM_READWRITE));
+       g_object_class_install_property (object_class,
+                                        PROP_FLASH_STATE,
+                                        g_param_spec_enum ("flash-state",
+                                                           "flash-state",
+                                                           "flash-state",
+                                                           MC_TYPE_PRESENCE,
+                                                           MC_PRESENCE_UNSET,
+                                                           G_PARAM_READWRITE));
+
+        g_object_class_install_property (object_class,
+                                         PROP_AUTO_AWAY,
+                                         g_param_spec_boolean ("auto-away",
+                                                               "Automatic set presence to away",
+                                                               "Should it set presence to away if inactive",
+                                                               FALSE,
+                                                               G_PARAM_READWRITE));
+
+        g_object_class_install_property (object_class,
+                                         PROP_USE_NM,
+                                         g_param_spec_boolean ("use-nm",
+                                                               "Use Network Manager",
+                                                               "Set presence according to Network Manager",
+                                                               FALSE,
+                                                               G_PARAM_READWRITE));
+
+       g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
+}
+
+static void
+empathy_idle_init (EmpathyIdle *idle)
 {
-       static EmpathyIdle *idle = NULL;
+       DBusGConnection *system_bus;
+       GError          *error = NULL;
+       EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
+               EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
+
+       idle->priv = priv;
+       priv->is_idle = FALSE;
+       priv->mc = empathy_mission_control_dup_singleton ();
+       priv->state = mission_control_get_presence_actual (priv->mc, &error);
+       if (error) {
+               DEBUG ("Error getting actual presence: %s", error->message);
+
+               priv->state = MC_PRESENCE_UNSET;
+               g_clear_error (&error);
+       }
+       priv->status = mission_control_get_presence_message_actual (priv->mc, &error);
+       if (error || EMP_STR_EMPTY (priv->status)) {
+               g_free (priv->status);
+               priv->status = NULL;
+
+               if (error) {
+                       DEBUG ("Error getting actual presence message: %s", error->message);
+                       g_clear_error (&error);
+               }
+       }
+
+       dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
+                                    "PresenceChanged",
+                                    G_CALLBACK (idle_presence_changed_cb),
+                                    idle, NULL);
 
-       if (!idle) {
-               idle = g_object_new (EMPATHY_TYPE_IDLE, NULL);
-               g_object_add_weak_pointer (G_OBJECT (idle), (gpointer) &idle);
+       priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
+                                                   "org.gnome.ScreenSaver",
+                                                   "/org/gnome/ScreenSaver",
+                                                   "org.gnome.ScreenSaver");
+       if (priv->gs_proxy) {
+               dbus_g_proxy_add_signal (priv->gs_proxy, "SessionIdleChanged",
+                                        G_TYPE_BOOLEAN,
+                                        G_TYPE_INVALID);
+               dbus_g_proxy_connect_signal (priv->gs_proxy, "SessionIdleChanged",
+                                            G_CALLBACK (idle_session_idle_changed_cb),
+                                            idle, NULL);
        } else {
-               g_object_ref (idle);
+               DEBUG ("Failed to get gs proxy");
        }
 
-       return idle;
+
+       system_bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
+       if (!system_bus) {
+               DEBUG ("Failed to get system bus: %s",
+                       error ? error->message : "No error given");
+       } else {
+               priv->nm_proxy = dbus_g_proxy_new_for_name (system_bus,
+                                                           "org.freedesktop.NetworkManager",
+                                                           "/org/freedesktop/NetworkManager",
+                                                           "org.freedesktop.NetworkManager");
+       }
+       if (priv->nm_proxy) {
+               dbus_g_proxy_add_signal (priv->nm_proxy, "StateChange",
+                                        G_TYPE_UINT, G_TYPE_INVALID);
+               dbus_g_proxy_connect_signal (priv->nm_proxy, "StateChange",
+                                            G_CALLBACK (idle_nm_state_change_cb),
+                                            idle, NULL);
+       } else {
+               DEBUG ("Failed to get nm proxy");
+       }
+
+       priv->nm_connected = TRUE;
+}
+
+EmpathyIdle *
+empathy_idle_dup_singleton (void)
+{
+       return g_object_new (EMPATHY_TYPE_IDLE, NULL);
 }
 
 McPresence
@@ -365,7 +531,7 @@ empathy_idle_get_status (EmpathyIdle *idle)
        priv = GET_PRIV (idle);
 
        if (!priv->status) {
-               return empathy_presence_state_get_default_status (priv->state);
+               return empathy_presence_get_default_message (priv->state);
        }
 
        return priv->status;
@@ -418,29 +584,31 @@ empathy_idle_set_presence (EmpathyIdle *idle,
 
        priv = GET_PRIV (idle);
 
-       empathy_debug (DEBUG_DOMAIN, "Changing presence to %s (%d)",
-                      status, state);
+       DEBUG ("Changing presence to %s (%d)", status, state);
+
+       /* Do not set translated default messages */
+       default_status = empathy_presence_get_default_message (state);
+       if (!tp_strdiff (status, default_status)) {
+               status = NULL;
+       }
 
        if (!priv->nm_connected) {
-               empathy_debug (DEBUG_DOMAIN,
-                              "NM not connected, saving requested presence");
+               DEBUG ("NM not connected");
+
+               priv->nm_saved_state = state;
+               if (tp_strdiff (priv->status, status)) {
+                       g_free (priv->status);
+                       priv->status = NULL;
+                       if (!EMP_STR_EMPTY (status)) {
+                               priv->status = g_strdup (status);
+                       }
+                       g_object_notify (G_OBJECT (idle), "status");
+               }
 
-               g_free (priv->saved_status);
-               priv->saved_state = state;
-               priv->saved_status = g_strdup (status);
                return;
        }
 
-       /* Do not set translated default messages */
-       default_status = empathy_presence_state_get_default_status (state);
-       if (status && strcmp (status, default_status) == 0) {
-               status = NULL;
-       }
-
-       mission_control_set_presence (priv->mc,
-                                     state,
-                                     status,
-                                     NULL, NULL);
+       mission_control_set_presence (priv->mc, state, status, NULL, NULL);
 }
 
 gboolean
@@ -463,179 +631,50 @@ empathy_idle_set_auto_away (EmpathyIdle *idle,
 }
 
 gboolean
-empathy_idle_get_auto_disconnect (EmpathyIdle *idle)
+empathy_idle_get_use_nm (EmpathyIdle *idle)
 {
        EmpathyIdlePriv *priv = GET_PRIV (idle);
 
-       return priv->auto_disconnect;
+       return priv->use_nm;
 }
 
 void
-empathy_idle_set_auto_disconnect (EmpathyIdle *idle,
-                                 gboolean     auto_disconnect)
+empathy_idle_set_use_nm (EmpathyIdle *idle,
+                        gboolean     use_nm)
 {
        EmpathyIdlePriv *priv = GET_PRIV (idle);
 
-       priv->auto_disconnect = auto_disconnect;
-
-       g_object_notify (G_OBJECT (idle), "auto-disconnect");
-}
-
-static void
-idle_presence_changed_cb (MissionControl *mc,
-                         McPresence      state,
-                         EmpathyIdle    *idle)
-{
-       EmpathyIdlePriv *priv;
-
-       priv = GET_PRIV (idle);
-
-       g_free (priv->status);
-       priv->state = state;
-       priv->status = mission_control_get_presence_message_actual (priv->mc, NULL);
-
-       if (G_STR_EMPTY (priv->status)) {
-               g_free (priv->status);
-               priv->status = NULL;
-       }
-
-       g_object_notify (G_OBJECT (idle), "state");
-       g_object_notify (G_OBJECT (idle), "status");
-}
-
-static void
-idle_session_idle_changed_cb (DBusGProxy  *gs_proxy,
-                             gboolean     is_idle,
-                             EmpathyIdle *idle)
-{
-       EmpathyIdlePriv *priv;
-
-       priv = GET_PRIV (idle);
-
-       empathy_debug (DEBUG_DOMAIN, "Session idle state changed, %s -> %s",
-                     priv->is_idle ? "yes" : "no",
-                     is_idle ? "yes" : "no");
-
-       if (priv->state <= MC_PRESENCE_OFFLINE ||
-           priv->state == MC_PRESENCE_HIDDEN ||
-           !priv->auto_away) {
-               /* We are not online or we don't want to go auto away,
-                * nothing to do here */
-               priv->is_idle = is_idle;
+       if (!priv->nm_proxy || use_nm == priv->use_nm) {
                return;
        }
 
-       if (is_idle && !priv->is_idle) {
-               McPresence new_state;
-               /* We are now idle */
+       priv->use_nm = use_nm;
 
-               if (priv->state == MC_PRESENCE_AWAY ||
-                          priv->state == MC_PRESENCE_EXTENDED_AWAY) {
-                       /* User set away manually, when coming back we restore
-                        * default presence. */
-                       new_state = priv->state;
-                       priv->saved_state = MC_PRESENCE_AVAILABLE;
-                       priv->saved_status = NULL;
-               } else {
-                       new_state = MC_PRESENCE_AWAY;
-                       priv->saved_state = priv->state;
-                       priv->saved_status = g_strdup (priv->status);
-               }
+       if (use_nm) {
+               guint   nm_status;
+               GError *error = NULL;
 
-               empathy_debug (DEBUG_DOMAIN, "Going to autoaway");
-               empathy_idle_set_state (idle, new_state);
-
-               idle_ext_away_start (idle);
-       } else if (!is_idle && priv->is_idle) {
-               /* We are no more idle, restore state */
-               idle_ext_away_stop (idle);
-
-               empathy_debug (DEBUG_DOMAIN, "Restoring state to %d %s",
-                             priv->saved_state,
-                             priv->saved_status);
-
-               empathy_idle_set_presence (idle,
-                                          priv->saved_state,
-                                          priv->saved_status);
-
-               g_free (priv->saved_status);
-               priv->saved_status = NULL;
-       }
-
-       priv->is_idle = is_idle;
-}
-
-static void
-idle_nm_state_change_cb (DBusGProxy  *proxy,
-                        guint        state,
-                        EmpathyIdle *idle)
-{
-       EmpathyIdlePriv *priv;
-
-       priv = GET_PRIV (idle);
-
-       empathy_debug (DEBUG_DOMAIN, "New network state (%d)", state);
-
-       if (!priv->auto_disconnect) {
-               return;
-       }
-
-       if (state != NM_STATE_CONNECTED && priv->nm_connected) {
-               /* We are no more connected */
-               idle_ext_away_stop (idle);
-               g_free (priv->saved_status);
-               priv->saved_state = priv->state;
-               priv->saved_status = g_strdup (priv->status);
+               dbus_g_proxy_call (priv->nm_proxy, "state",
+                                  &error,
+                                  G_TYPE_INVALID,
+                                  G_TYPE_UINT, &nm_status,
+                                  G_TYPE_INVALID);
 
-               empathy_idle_set_state (idle, MC_PRESENCE_OFFLINE);
-               priv->nm_connected = FALSE;
-       }
-       else if (state == NM_STATE_CONNECTED && !priv->nm_connected) {
-               /* We are now connected */
+               if (error) {
+                       DEBUG ("Couldn't get NM state: %s", error->message);
+                       g_clear_error (&error);
+                       nm_status = NM_STATE_ASLEEP;
+               }
+               
+               idle_nm_state_change_cb (priv->nm_proxy, nm_status, idle);
+       } else {
                priv->nm_connected = TRUE;
-               empathy_idle_set_presence (idle,
-                                          priv->saved_state,
-                                          priv->saved_status);
-       }
-}
-
-static void
-idle_ext_away_start (EmpathyIdle *idle)
-{
-       EmpathyIdlePriv *priv;
-
-       priv = GET_PRIV (idle);
-
-       idle_ext_away_stop (idle);
-       priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
-                                                       (GSourceFunc) idle_ext_away_cb,
-                                                       idle);
-}
-
-static void
-idle_ext_away_stop (EmpathyIdle *idle)
-{
-       EmpathyIdlePriv *priv;
-
-       priv = GET_PRIV (idle);
-
-       if (priv->ext_away_timeout) {
-               g_source_remove (priv->ext_away_timeout);
-               priv->ext_away_timeout = 0;
+               if (priv->nm_saved_state != MC_PRESENCE_UNSET) {
+                       empathy_idle_set_state (idle, priv->nm_saved_state);
+               }
+               priv->nm_saved_state = MC_PRESENCE_UNSET;
        }
-}
-
-static gboolean
-idle_ext_away_cb (EmpathyIdle *idle)
-{
-       EmpathyIdlePriv *priv;
 
-       priv = GET_PRIV (idle);
-
-       empathy_debug (DEBUG_DOMAIN, "Going to extended autoaway");
-       empathy_idle_set_state (idle, MC_PRESENCE_EXTENDED_AWAY);
-       priv->ext_away_timeout = 0;
-
-       return FALSE;
+       g_object_notify (G_OBJECT (idle), "use-nm");
 }