]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-idle.c
Import tools from telepathy-glib 0.7.3 and build a static libemp-extensions.la.
[empathy.git] / libempathy / empathy-idle.c
index 87389277c3f290d45c44edcb3491ec53c9775f76..0f23037bdabe2ca176a8a73b7ffb70919c40143b 100644 (file)
@@ -2,20 +2,19 @@
 /*
  * Copyright (C) 2007 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>
  */
@@ -54,15 +53,20 @@ struct _EmpathyIdlePriv {
        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;
+
+       gboolean        away_reset_status;
+       McPresence      away_saved_state;
+       gboolean        nm_reset_status;
+       McPresence      nm_saved_state;
+
        gboolean        is_idle;
        gboolean        nm_connected;
-       gboolean        auto_away;
-       gboolean        auto_disconnect;
        guint           ext_away_timeout;
 };
 
@@ -79,6 +83,7 @@ static void     idle_set_property            (GObject          *object,
                                              GParamSpec       *pspec);
 static void     idle_presence_changed_cb     (MissionControl   *mc,
                                              McPresence        state,
+                                             gchar            *status,
                                              EmpathyIdle      *idle);
 static void     idle_session_idle_changed_cb (DBusGProxy       *gs_proxy,
                                              gboolean          is_idle,
@@ -96,7 +101,7 @@ enum {
        PROP_STATUS,
        PROP_FLASH_STATE,
        PROP_AUTO_AWAY,
-       PROP_AUTO_DISCONNECT
+       PROP_USE_NM
 };
 
 G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT)
@@ -143,10 +148,10 @@ empathy_idle_class_init (EmpathyIdleClass *klass)
                                                                G_PARAM_READWRITE));
 
         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",
+                                         PROP_USE_NM,
+                                         g_param_spec_boolean ("use-nm",
+                                                               "Use Network Manager",
+                                                               "Set presence according to Network Manager",
                                                                FALSE,
                                                                G_PARAM_READWRITE));
 
@@ -165,10 +170,10 @@ empathy_idle_init (EmpathyIdle *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);
+       priv->status = mission_control_get_presence_message_actual (priv->mc, NULL);
 
        dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
-                                    "PresenceStatusActual",
+                                    "PresenceChanged",
                                     G_CALLBACK (idle_presence_changed_cb),
                                     idle, NULL);
 
@@ -199,43 +204,16 @@ empathy_idle_init (EmpathyIdle *idle)
                                                            "org.freedesktop.NetworkManager");
        }
        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 (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;
-               } else {
-                       priv->nm_connected = (nm_status == NM_STATE_CONNECTED);
-
-                       empathy_debug (DEBUG_DOMAIN, "NetworkManager connected: %s",
-                                      priv->nm_connected ? "Yes" : "No");
-               }
-
-               if (!priv->nm_connected) {
-                       priv->saved_state = priv->state;
-                       priv->saved_status = g_strdup (priv->status);
-               }
        } else {
                empathy_debug (DEBUG_DOMAIN, "Failed to get nm proxy");
-               priv->nm_connected = TRUE;
        }
+
+       priv->nm_connected = TRUE;
 }
 
 static void
@@ -246,7 +224,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) {
@@ -281,8 +258,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,6 +292,9 @@ 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;
@@ -365,7 +345,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;
@@ -422,17 +402,12 @@ empathy_idle_set_presence (EmpathyIdle *idle,
                       status, state);
 
        if (!priv->nm_connected) {
-               empathy_debug (DEBUG_DOMAIN,
-                              "NM not connected, saving requested presence");
-
-               g_free (priv->saved_status);
-               priv->saved_state = state;
-               priv->saved_status = g_strdup (status);
+               empathy_debug (DEBUG_DOMAIN, "NM not connected");
                return;
        }
 
        /* Do not set translated default messages */
-       default_status = empathy_presence_state_get_default_status (state);
+       default_status = empathy_presence_get_default_message (state);
        if (status && strcmp (status, default_status) == 0) {
                status = NULL;
        }
@@ -463,40 +438,73 @@ 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;
+       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);
 
-       g_object_notify (G_OBJECT (idle), "auto-disconnect");
+               if (error) {
+                       empathy_debug (DEBUG_DOMAIN, 
+                                      "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 {
+               if (!priv->nm_connected) {
+                       empathy_idle_set_state (idle, priv->nm_saved_state);
+               }
+               priv->nm_connected = TRUE;
+               priv->nm_saved_state = MC_PRESENCE_UNSET;
+       }
+
+       g_object_notify (G_OBJECT (idle), "use-nm");
 }
 
 static void
 idle_presence_changed_cb (MissionControl *mc,
                          McPresence      state,
+                         gchar          *status,
                          EmpathyIdle    *idle)
 {
        EmpathyIdlePriv *priv;
 
        priv = GET_PRIV (idle);
 
+       empathy_debug (DEBUG_DOMAIN, "Presence changed to '%s' (%d)",
+                      status, state);
+
        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;
+       priv->status = NULL;
+       if (!G_STR_EMPTY (status)) {
+               priv->status = g_strdup (status);
        }
 
        g_object_notify (G_OBJECT (idle), "state");
@@ -530,16 +538,16 @@ idle_session_idle_changed_cb (DBusGProxy  *gs_proxy,
                /* We are now idle */
 
                if (priv->state == MC_PRESENCE_AWAY ||
-                          priv->state == MC_PRESENCE_EXTENDED_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;
+                       priv->away_saved_state = MC_PRESENCE_AVAILABLE;
+                       priv->away_reset_status = TRUE;
                } else {
                        new_state = MC_PRESENCE_AWAY;
-                       priv->saved_state = priv->state;
-                       priv->saved_status = g_strdup (priv->status);
+                       priv->away_saved_state = priv->state;
+                       priv->away_reset_status = FALSE;
                }
 
                empathy_debug (DEBUG_DOMAIN, "Going to autoaway");
@@ -550,16 +558,23 @@ idle_session_idle_changed_cb (DBusGProxy  *gs_proxy,
                /* 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_debug (DEBUG_DOMAIN, "Restoring state to %d, reset status: %s",
+                             priv->away_saved_state,
+                             priv->away_reset_status ? "Yes" : "No");
 
-               empathy_idle_set_presence (idle,
-                                          priv->saved_state,
-                                          priv->saved_status);
+               if (priv->nm_connected) {
+                       empathy_idle_set_presence (idle,
+                                                  priv->away_saved_state,
+                                                  priv->away_reset_status ? NULL : priv->status);
+               } else {
+                       /* We can't restore state now, will do when NM gets
+                        * connected. */
+                       priv->nm_saved_state = priv->away_saved_state;
+                       priv->nm_reset_status = priv->away_reset_status;
+               }
 
-               g_free (priv->saved_status);
-               priv->saved_status = NULL;
+               priv->away_saved_state = MC_PRESENCE_UNSET;
+               priv->away_reset_status = FALSE;
        }
 
        priv->is_idle = is_idle;
@@ -571,32 +586,40 @@ idle_nm_state_change_cb (DBusGProxy  *proxy,
                         EmpathyIdle *idle)
 {
        EmpathyIdlePriv *priv;
+       gboolean         old_nm_connected;
+       gboolean         new_nm_connected;
 
        priv = GET_PRIV (idle);
 
-       empathy_debug (DEBUG_DOMAIN, "New network state (%d)", state);
+       empathy_debug (DEBUG_DOMAIN, "New network state (%d), in use = %s",
+                      state, priv->use_nm ? "Yes" : "No");
 
-       if (!priv->auto_disconnect) {
+       if (!priv->use_nm) {
                return;
        }
 
-       if (state != NM_STATE_CONNECTED && priv->nm_connected) {
+       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 */
+
+       if (old_nm_connected && !new_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);
 
+               priv->nm_saved_state = priv->state;
                empathy_idle_set_state (idle, MC_PRESENCE_OFFLINE);
-               priv->nm_connected = FALSE;
        }
-       else if (state == NM_STATE_CONNECTED && !priv->nm_connected) {
+       else if (!old_nm_connected && new_nm_connected) {
                /* We are now connected */
-               priv->nm_connected = TRUE;
                empathy_idle_set_presence (idle,
-                                          priv->saved_state,
-                                          priv->saved_status);
+                                          priv->nm_saved_state,
+                                          priv->nm_reset_status ? NULL : priv->status);
+               priv->nm_saved_state = MC_PRESENCE_UNSET;
+               priv->nm_reset_status = FALSE;
        }
+
+       priv->nm_connected = new_nm_connected;
 }
 
 static void
@@ -607,9 +630,9 @@ idle_ext_away_start (EmpathyIdle *idle)
        priv = GET_PRIV (idle);
 
        idle_ext_away_stop (idle);
-       priv->ext_away_timeout = g_timeout_add (EXT_AWAY_TIME * 1000,
-                                               (GSourceFunc) idle_ext_away_cb,
-                                               idle);
+       priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
+                                                       (GSourceFunc) idle_ext_away_cb,
+                                                       idle);
 }
 
 static void