]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-idle.c
Updated Basque language
[empathy.git] / libempathy / empathy-idle.c
index 92ab9f39f785d326ae6f5a5f1e5e0a198bb2ffbb..b8c7784d704605ef5020dd704782dbf888c4cc64 100644 (file)
 
 #include <telepathy-glib/dbus.h>
 #include <telepathy-glib/util.h>
-#include <libmissioncontrol/mc-enum-types.h>
 
+#include "empathy-account-manager.h"
 #include "empathy-idle.h"
 #include "empathy-utils.h"
+#include "empathy-connectivity.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
 #include "empathy-debug.h"
 
 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIdle)
 typedef struct {
-       MissionControl *mc;
        DBusGProxy     *gs_proxy;
-       DBusGProxy     *nm_proxy;
+       EmpathyConnectivity *connectivity;
+       gulong state_change_signal_id;
 
        TpConnectionPresenceType      state;
        gchar          *status;
        TpConnectionPresenceType      flash_state;
        gboolean        auto_away;
-       gboolean        use_nm;
 
        TpConnectionPresenceType      away_saved_state;
-       TpConnectionPresenceType      nm_saved_state;
-       gchar          *nm_saved_status;
+       TpConnectionPresenceType      saved_state;
+       gchar          *saved_status;
 
        gboolean        is_idle;
-       gboolean        nm_connected;
        guint           ext_away_timeout;
+
+       EmpathyAccountManager *manager;
 } EmpathyIdlePriv;
 
 typedef enum {
-       NM_STATE_UNKNOWN,
-       NM_STATE_ASLEEP,
-       NM_STATE_CONNECTING,
-       NM_STATE_CONNECTED,
-       NM_STATE_DISCONNECTED
-} NMState;
+       SESSION_STATUS_AVAILABLE,
+       SESSION_STATUS_INVISIBLE,
+       SESSION_STATUS_BUSY,
+       SESSION_STATUS_IDLE,
+       SESSION_STATUS_UNKNOWN
+} SessionStatus;
 
 enum {
        PROP_0,
        PROP_STATE,
        PROP_STATUS,
        PROP_FLASH_STATE,
-       PROP_AUTO_AWAY,
-       PROP_USE_NM
+       PROP_AUTO_AWAY
 };
 
 G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT);
@@ -82,23 +82,29 @@ G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT);
 static EmpathyIdle * idle_singleton = NULL;
 
 static void
-idle_presence_changed_cb (MissionControl *mc,
+idle_presence_changed_cb (EmpathyAccountManager *manager,
                          TpConnectionPresenceType state,
                          gchar          *status,
+                         gchar          *status_message,
                          EmpathyIdle    *idle)
 {
        EmpathyIdlePriv *priv;
 
        priv = GET_PRIV (idle);
 
-       DEBUG ("Presence changed to '%s' (%d)", status, state);
+       if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET)
+               /* Assume our presence is offline if MC reports UNSET */
+               state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
+
+       DEBUG ("Presence changed to '%s' (%d) \"%s\"", status, state,
+               status_message);
 
        g_free (priv->status);
        priv->state = state;
-       priv->status = NULL;
-       if (!EMP_STR_EMPTY (status)) {
-               priv->status = g_strdup (status);
-       }
+       if (EMP_STR_EMPTY (status_message))
+               priv->status = NULL;
+       else
+               priv->status = g_strdup (status_message);
 
        g_object_notify (G_OBJECT (idle), "state");
        g_object_notify (G_OBJECT (idle), "status");
@@ -147,20 +153,23 @@ idle_ext_away_start (EmpathyIdle *idle)
 }
 
 static void
-idle_session_idle_changed_cb (DBusGProxy  *gs_proxy,
-                             gboolean     is_idle,
-                             EmpathyIdle *idle)
+idle_session_status_changed_cb (DBusGProxy    *gs_proxy,
+                               SessionStatus  status,
+                               EmpathyIdle   *idle)
 {
        EmpathyIdlePriv *priv;
+       gboolean is_idle;
 
        priv = GET_PRIV (idle);
 
+       is_idle = (status == SESSION_STATUS_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 == TP_CONNECTION_PRESENCE_TYPE_UNSET &&
+           (priv->saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET &&
             (priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
              priv->state == TP_CONNECTION_PRESENCE_TYPE_HIDDEN))) {
                /* We don't want to go auto away OR we explicitely asked to be
@@ -175,11 +184,11 @@ idle_session_idle_changed_cb (DBusGProxy  *gs_proxy,
 
                idle_ext_away_start (idle);
 
-               if (priv->nm_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
+               if (priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_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;
+                       priv->away_saved_state = priv->saved_state;
                } else {
                        priv->away_saved_state = priv->state;
                }
@@ -220,50 +229,35 @@ idle_session_idle_changed_cb (DBusGProxy  *gs_proxy,
 }
 
 static void
-idle_nm_state_change_cb (DBusGProxy  *proxy,
-                        guint        state,
-                        EmpathyIdle *idle)
+idle_state_change_cb (EmpathyConnectivity *connectivity,
+                     gboolean new_online,
+                     EmpathyIdle *idle)
 {
        EmpathyIdlePriv *priv;
-       gboolean         old_nm_connected;
-       gboolean         new_nm_connected;
 
        priv = GET_PRIV (idle);
 
-       if (!priv->use_nm
-           || priv->nm_saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
-               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 */
+       if (!new_online) {
+               /* We are no longer 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);
+               priv->saved_state = priv->state;
+               g_free (priv->saved_status);
+               priv->saved_status = g_strdup (priv->status);
                empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
        }
-       else if (!old_nm_connected && new_nm_connected) {
+       else if (new_online
+                       && priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
                /* We are now connected */
                DEBUG ("Reconnected: Restore state %d (%s)",
-                               priv->nm_saved_state, priv->nm_saved_status);
+                               priv->saved_state, priv->saved_status);
                empathy_idle_set_presence (idle,
-                               priv->nm_saved_state,
-                               priv->nm_saved_status);
-               priv->nm_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
-               g_free (priv->nm_saved_status);
-               priv->nm_saved_status = NULL;
+                               priv->saved_state,
+                               priv->saved_status);
+               priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
+               g_free (priv->saved_status);
+               priv->saved_status = NULL;
        }
-
-       priv->nm_connected = new_nm_connected;
 }
 
 static void
@@ -274,12 +268,17 @@ idle_finalize (GObject *object)
        priv = GET_PRIV (object);
 
        g_free (priv->status);
-       g_object_unref (priv->mc);
 
        if (priv->gs_proxy) {
                g_object_unref (priv->gs_proxy);
        }
 
+       g_signal_handler_disconnect (priv->connectivity,
+                                    priv->state_change_signal_id);
+       priv->state_change_signal_id = 0;
+
+       g_object_unref (priv->connectivity);
+
        idle_ext_away_stop (EMPATHY_IDLE (object));
 }
 
@@ -328,9 +327,6 @@ idle_get_property (GObject    *object,
        case PROP_AUTO_AWAY:
                g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
                break;
-       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);
                break;
@@ -362,9 +358,6 @@ 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;
@@ -413,114 +406,43 @@ empathy_idle_class_init (EmpathyIdleClass *klass)
                                                                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 TpConnectionPresenceType
-empathy_idle_get_actual_presence (EmpathyIdle *idle, GError **error)
-{
-       McPresence presence;
-       EmpathyIdlePriv *priv = GET_PRIV (idle);
-
-       presence = mission_control_get_presence_actual (priv->mc, error);
-
-       switch (presence) {
-       case MC_PRESENCE_OFFLINE:
-               return TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
-       case MC_PRESENCE_AVAILABLE:
-               return TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
-       case MC_PRESENCE_AWAY:
-               return TP_CONNECTION_PRESENCE_TYPE_AWAY;
-       case MC_PRESENCE_EXTENDED_AWAY:
-               return TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY;
-       case MC_PRESENCE_HIDDEN:
-               return TP_CONNECTION_PRESENCE_TYPE_HIDDEN;
-       case MC_PRESENCE_DO_NOT_DISTURB:
-               return TP_CONNECTION_PRESENCE_TYPE_BUSY;
-       default:
-               return TP_CONNECTION_PRESENCE_TYPE_UNSET;
-       }
-}
-
 static void
 empathy_idle_init (EmpathyIdle *idle)
 {
-       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 = empathy_idle_get_actual_presence (idle, &error);
-       if (error) {
-               DEBUG ("Error getting actual presence: %s", error->message);
 
-               priv->state = TP_CONNECTION_PRESENCE_TYPE_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;
+       priv->manager = empathy_account_manager_dup_singleton ();
+       priv->state = empathy_account_manager_get_global_presence (priv->manager,
+               NULL, &priv->status);
 
-               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);
+       g_signal_connect (priv->manager, "global-presence-changed",
+               G_CALLBACK (idle_presence_changed_cb), idle);
 
        priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
-                                                   "org.gnome.ScreenSaver",
-                                                   "/org/gnome/ScreenSaver",
-                                                   "org.gnome.ScreenSaver");
+                                                   "org.gnome.SessionManager",
+                                                   "/org/gnome/SessionManager/Presence",
+                                                   "org.gnome.SessionManager.Presence");
        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 {
-               DEBUG ("Failed to get gs proxy");
-       }
-
-
-       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",
+               dbus_g_proxy_add_signal (priv->gs_proxy, "StatusChanged",
                                         G_TYPE_UINT, G_TYPE_INVALID);
-               dbus_g_proxy_connect_signal (priv->nm_proxy, "StateChange",
-                                            G_CALLBACK (idle_nm_state_change_cb),
+               dbus_g_proxy_connect_signal (priv->gs_proxy, "StatusChanged",
+                                            G_CALLBACK (idle_session_status_changed_cb),
                                             idle, NULL);
        } else {
-               DEBUG ("Failed to get nm proxy");
+               DEBUG ("Failed to get gs proxy");
        }
 
-       priv->nm_connected = TRUE;
+       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);
 }
 
 EmpathyIdle *
@@ -603,36 +525,31 @@ empathy_idle_set_flash_state (EmpathyIdle *idle,
 
 static void
 empathy_idle_do_set_presence (EmpathyIdle *idle,
-                          TpConnectionPresenceType   state,
-                          const gchar *status)
+                          TpConnectionPresenceType status_type,
+                          const gchar *status_message)
 {
-       McPresence mc_state = MC_PRESENCE_UNSET;
        EmpathyIdlePriv *priv = GET_PRIV (idle);
+       const gchar *statuses[NUM_TP_CONNECTION_PRESENCE_TYPES] = {
+               NULL,
+               "offline",
+               "available",
+               "away",
+               "xa",
+               "hidden",
+               "busy",
+               NULL,
+               NULL,
+       };
+       const gchar *status;
 
-       switch (state) {
-               case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
-                       mc_state = MC_PRESENCE_OFFLINE;
-                       break;
-               case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
-                       mc_state = MC_PRESENCE_AVAILABLE;
-                       break;
-               case TP_CONNECTION_PRESENCE_TYPE_AWAY:
-                       mc_state = MC_PRESENCE_AWAY;
-                       break;
-               case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
-                       mc_state = MC_PRESENCE_EXTENDED_AWAY;
-                       break;
-               case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
-                       mc_state = MC_PRESENCE_HIDDEN;
-                       break;
-               case TP_CONNECTION_PRESENCE_TYPE_BUSY:
-                       mc_state = MC_PRESENCE_DO_NOT_DISTURB;
-                       break;
-               default:
-                       g_assert_not_reached ();
-       }
+       g_assert (status_type > 0 && status_type < NUM_TP_CONNECTION_PRESENCE_TYPES);
+
+       status = statuses[status_type];
 
-       mission_control_set_presence (priv->mc, mc_state, status, NULL, NULL);
+       g_return_if_fail (status != NULL);
+
+       empathy_account_manager_request_global_presence (priv->manager,
+               status_type, status, status_message);
 }
 
 void
@@ -653,19 +570,18 @@ empathy_idle_set_presence (EmpathyIdle *idle,
                status = NULL;
        }
 
-       if (!priv->nm_connected) {
-               DEBUG ("NM not connected");
+       if (state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE &&
+                       !empathy_connectivity_is_online (priv->connectivity)) {
+               DEBUG ("Empathy is not online");
 
-               priv->nm_saved_state = state;
+               priv->saved_state = state;
                if (tp_strdiff (priv->status, status)) {
-                       g_free (priv->status);
-                       priv->status = NULL;
+                       g_free (priv->saved_status);
+                       priv->saved_status = NULL;
                        if (!EMP_STR_EMPTY (status)) {
-                               priv->status = g_strdup (status);
+                               priv->saved_status = g_strdup (status);
                        }
-                       g_object_notify (G_OBJECT (idle), "status");
                }
-
                return;
        }
 
@@ -691,51 +607,3 @@ empathy_idle_set_auto_away (EmpathyIdle *idle,
        g_object_notify (G_OBJECT (idle), "auto-away");
 }
 
-gboolean
-empathy_idle_get_use_nm (EmpathyIdle *idle)
-{
-       EmpathyIdlePriv *priv = GET_PRIV (idle);
-
-       return priv->use_nm;
-}
-
-void
-empathy_idle_set_use_nm (EmpathyIdle *idle,
-                        gboolean     use_nm)
-{
-       EmpathyIdlePriv *priv = GET_PRIV (idle);
-
-       if (!priv->nm_proxy || use_nm == priv->use_nm) {
-               return;
-       }
-
-       priv->use_nm = use_nm;
-
-       if (use_nm) {
-               guint   nm_status;
-               GError *error = NULL;
-
-               dbus_g_proxy_call (priv->nm_proxy, "state",
-                                  &error,
-                                  G_TYPE_INVALID,
-                                  G_TYPE_UINT, &nm_status,
-                                  G_TYPE_INVALID);
-
-               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;
-               if (priv->nm_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
-                       empathy_idle_set_state (idle, priv->nm_saved_state);
-               }
-               priv->nm_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
-       }
-
-       g_object_notify (G_OBJECT (idle), "use-nm");
-}
-