]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-idle.c
Updated.
[empathy.git] / libempathy / empathy-idle.c
index 8150effd3941580d1c3bbdf0d5b8893d4cde6eca..da04b57fc7cb8cd9c7e4bfdc578d7375d3a2f11b 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>
  */
 #include <dbus/dbus-glib.h>
 
 #include <libtelepathy/tp-helpers.h>
+#include <libmissioncontrol/mc-enum-types.h>
 
 #include "empathy-idle.h"
-#include "gossip-utils.h" 
-#include "gossip-debug.h"
+#include "empathy-utils.h" 
+#include "empathy-debug.h"
 
 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
                       EMPATHY_TYPE_IDLE, EmpathyIdlePriv))
@@ -42,7 +42,7 @@
 #define EXT_AWAY_TIME (30*60)
 
 typedef enum {
-       NM_STATE_UNKNOWN = 0,
+       NM_STATE_UNKNOWN,
        NM_STATE_ASLEEP,
        NM_STATE_CONNECTING,
        NM_STATE_CONNECTED,
@@ -53,13 +53,15 @@ struct _EmpathyIdlePriv {
        MissionControl *mc;
        DBusGProxy     *gs_proxy;
        DBusGProxy     *nm_proxy;
-       gboolean        is_idle;
        McPresence      state;
        McPresence      flash_state;
        gchar          *status;
        McPresence      saved_state;
        gchar          *saved_status;
+       gboolean        is_idle;
        gboolean        nm_connected;
+       gboolean        auto_away;
+       gboolean        auto_disconnect;
        guint           ext_away_timeout;
 };
 
@@ -91,7 +93,9 @@ enum {
        PROP_0,
        PROP_STATE,
        PROP_STATUS,
-       PROP_FLASH_STATE
+       PROP_FLASH_STATE,
+       PROP_AUTO_AWAY,
+       PROP_AUTO_DISCONNECT
 };
 
 G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT)
@@ -107,11 +111,10 @@ empathy_idle_class_init (EmpathyIdleClass *klass)
 
        g_object_class_install_property (object_class,
                                         PROP_STATE,
-                                        g_param_spec_uint ("state",
+                                        g_param_spec_enum ("state",
                                                            "state",
                                                            "state",
-                                                           MC_PRESENCE_UNSET,
-                                                           LAST_MC_PRESENCE,
+                                                           MC_TYPE_PRESENCE,
                                                            MC_PRESENCE_AVAILABLE,
                                                            G_PARAM_READWRITE));
        g_object_class_install_property (object_class,
@@ -123,14 +126,29 @@ empathy_idle_class_init (EmpathyIdleClass *klass)
                                                              G_PARAM_READWRITE));
        g_object_class_install_property (object_class,
                                         PROP_FLASH_STATE,
-                                        g_param_spec_uint ("flash-state",
+                                        g_param_spec_enum ("flash-state",
                                                            "flash-state",
                                                            "flash-state",
-                                                           MC_PRESENCE_UNSET,
-                                                           LAST_MC_PRESENCE,
+                                                           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_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_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
 }
 
@@ -144,7 +162,7 @@ empathy_idle_init (EmpathyIdle *idle)
        priv = GET_PRIV (idle);
 
        priv->is_idle = FALSE;
-       priv->mc = gossip_mission_control_new ();
+       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);
 
@@ -165,13 +183,13 @@ empathy_idle_init (EmpathyIdle *idle)
                                             G_CALLBACK (idle_session_idle_changed_cb),
                                             idle, NULL);
        } else {
-               gossip_debug (DEBUG_DOMAIN, "Failed to get gs proxy");
+               empathy_debug (DEBUG_DOMAIN, "Failed to get gs proxy");
        }
 
 
        system_bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
        if (!system_bus) {
-               gossip_debug (DEBUG_DOMAIN, "Failed to get system bus: %s",
+               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,
@@ -180,16 +198,43 @@ 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 {
-               gossip_debug (DEBUG_DOMAIN, "Failed to get nm proxy");
+               empathy_debug (DEBUG_DOMAIN, "Failed to get nm proxy");
+               priv->nm_connected = TRUE;
        }
-       /* FIXME: get value */
-       priv->nm_connected = TRUE;
 }
 
 static void
@@ -224,13 +269,19 @@ idle_get_property (GObject    *object,
 
        switch (param_id) {
        case PROP_STATE:
-               g_value_set_uint (value, empathy_idle_get_state (idle));
+               g_value_set_enum (value, empathy_idle_get_state (idle));
                break;
        case PROP_STATUS:
                g_value_set_string (value, empathy_idle_get_status (idle));
                break;
        case PROP_FLASH_STATE:
-               g_value_set_uint (value, empathy_idle_get_flash_state (idle));
+               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;
+       case PROP_AUTO_DISCONNECT:
+               g_value_set_boolean (value, empathy_idle_get_auto_disconnect (idle));
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
@@ -252,13 +303,16 @@ idle_set_property (GObject      *object,
 
        switch (param_id) {
        case PROP_STATE:
-               empathy_idle_set_state (idle, g_value_get_uint (value));
+               empathy_idle_set_state (idle, g_value_get_enum (value));
                break;
        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_uint (value));
+               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;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
@@ -310,7 +364,7 @@ empathy_idle_get_status (EmpathyIdle *idle)
        priv = GET_PRIV (idle);
 
        if (!priv->status) {
-               return gossip_presence_state_get_default_status (priv->state);
+               return empathy_presence_state_get_default_status (priv->state);
        }
 
        return priv->status;
@@ -363,7 +417,13 @@ empathy_idle_set_presence (EmpathyIdle *idle,
 
        priv = GET_PRIV (idle);
 
+       empathy_debug (DEBUG_DOMAIN, "Changing presence to %s (%d)",
+                      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);
@@ -371,7 +431,7 @@ empathy_idle_set_presence (EmpathyIdle *idle,
        }
 
        /* Do not set translated default messages */
-       default_status = gossip_presence_state_get_default_status (state);
+       default_status = empathy_presence_state_get_default_status (state);
        if (status && strcmp (status, default_status) == 0) {
                status = NULL;
        }
@@ -382,6 +442,44 @@ empathy_idle_set_presence (EmpathyIdle *idle,
                                      NULL, NULL);
 }
 
+gboolean
+empathy_idle_get_auto_away (EmpathyIdle *idle)
+{
+       EmpathyIdlePriv *priv = GET_PRIV (idle);
+
+       return priv->auto_away;
+}
+
+void
+empathy_idle_set_auto_away (EmpathyIdle *idle,
+                           gboolean     auto_away)
+{
+       EmpathyIdlePriv *priv = GET_PRIV (idle);
+
+       priv->auto_away = auto_away;
+
+       g_object_notify (G_OBJECT (idle), "auto-away");
+}
+
+gboolean
+empathy_idle_get_auto_disconnect (EmpathyIdle *idle)
+{
+       EmpathyIdlePriv *priv = GET_PRIV (idle);
+
+       return priv->auto_disconnect;
+}
+
+void
+empathy_idle_set_auto_disconnect (EmpathyIdle *idle,
+                                 gboolean     auto_disconnect)
+{
+       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,
@@ -413,13 +511,15 @@ idle_session_idle_changed_cb (DBusGProxy  *gs_proxy,
 
        priv = GET_PRIV (idle);
 
-       gossip_debug (DEBUG_DOMAIN, "Session idle state changed, %s -> %s",
+       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) {
-               /* We are not online so nothing to do here */
+           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;
                return;
        }
@@ -441,7 +541,7 @@ idle_session_idle_changed_cb (DBusGProxy  *gs_proxy,
                        priv->saved_status = g_strdup (priv->status);
                }
 
-               gossip_debug (DEBUG_DOMAIN, "Going to autoaway");
+               empathy_debug (DEBUG_DOMAIN, "Going to autoaway");
                empathy_idle_set_state (idle, new_state);
 
                idle_ext_away_start (idle);
@@ -449,7 +549,7 @@ idle_session_idle_changed_cb (DBusGProxy  *gs_proxy,
                /* We are no more idle, restore state */
                idle_ext_away_stop (idle);
 
-               gossip_debug (DEBUG_DOMAIN, "Restoring state to %d %s",
+               empathy_debug (DEBUG_DOMAIN, "Restoring state to %d %s",
                              priv->saved_state,
                              priv->saved_status);
 
@@ -473,10 +573,13 @@ idle_nm_state_change_cb (DBusGProxy  *proxy,
 
        priv = GET_PRIV (idle);
 
-       gossip_debug (DEBUG_DOMAIN, "New network state (%d)", state);
+       empathy_debug (DEBUG_DOMAIN, "New network state (%d)", state);
+
+       if (!priv->auto_disconnect) {
+               return;
+       }
 
-       if (state != NM_STATE_CONNECTED &&
-           priv->state > MC_PRESENCE_OFFLINE) {
+       if (state != NM_STATE_CONNECTED && priv->nm_connected) {
                /* We are no more connected */
                idle_ext_away_stop (idle);
                g_free (priv->saved_status);
@@ -486,8 +589,7 @@ idle_nm_state_change_cb (DBusGProxy  *proxy,
                empathy_idle_set_state (idle, MC_PRESENCE_OFFLINE);
                priv->nm_connected = FALSE;
        }
-       else if (priv->state <= MC_PRESENCE_OFFLINE &&
-                state == NM_STATE_CONNECTED) {
+       else if (state == NM_STATE_CONNECTED && !priv->nm_connected) {
                /* We are now connected */
                priv->nm_connected = TRUE;
                empathy_idle_set_presence (idle,
@@ -504,9 +606,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
@@ -529,7 +631,7 @@ idle_ext_away_cb (EmpathyIdle *idle)
 
        priv = GET_PRIV (idle);
 
-       gossip_debug (DEBUG_DOMAIN, "Going to extended autoaway");
+       empathy_debug (DEBUG_DOMAIN, "Going to extended autoaway");
        empathy_idle_set_state (idle, MC_PRESENCE_EXTENDED_AWAY);
        priv->ext_away_timeout = 0;