1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 Collabora Ltd.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Xavier Claessens <xclaesse@gmail.com>
25 #include <glib/gi18n.h>
26 #include <dbus/dbus-glib.h>
28 #include <libtelepathy/tp-helpers.h>
30 #include <libmissioncontrol/mission-control.h>
32 #include "empathy-idle.h"
33 #include "gossip-utils.h"
34 #include "gossip-debug.h"
36 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
37 EMPATHY_TYPE_IDLE, EmpathyIdlePriv))
39 #define DEBUG_DOMAIN "Idle"
41 /* Number of seconds before entering extended autoaway. */
42 #define EXT_AWAY_TIME (30*60)
48 struct _EmpathyIdlePriv {
52 McPresence saved_state;
54 guint ext_away_timeout;
57 static void empathy_idle_class_init (EmpathyIdleClass *klass);
58 static void empathy_idle_init (EmpathyIdle *idle);
59 static void idle_finalize (GObject *object);
60 static void idle_session_idle_changed_cb (DBusGProxy *gs_proxy,
63 static void idle_ext_away_start (EmpathyIdle *idle);
64 static void idle_ext_away_stop (EmpathyIdle *idle);
65 static gboolean idle_ext_away_cb (EmpathyIdle *idle);
67 //static guint signals[LAST_SIGNAL];
69 G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT)
72 empathy_idle_class_init (EmpathyIdleClass *klass)
74 GObjectClass *object_class = G_OBJECT_CLASS (klass);
76 object_class->finalize = idle_finalize;
78 g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
82 empathy_idle_init (EmpathyIdle *idle)
84 EmpathyIdlePriv *priv;
86 priv = GET_PRIV (idle);
88 priv->is_idle = FALSE;
89 priv->mc = gossip_mission_control_new ();
90 priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
91 "org.gnome.ScreenSaver",
92 "/org/gnome/ScreenSaver",
93 "org.gnome.ScreenSaver");
94 if (!priv->gs_proxy) {
95 gossip_debug (DEBUG_DOMAIN, "Failed to get gs proxy");
99 dbus_g_proxy_add_signal (priv->gs_proxy, "SessionIdleChanged",
102 dbus_g_proxy_connect_signal (priv->gs_proxy, "SessionIdleChanged",
103 G_CALLBACK (idle_session_idle_changed_cb),
108 idle_finalize (GObject *object)
110 EmpathyIdlePriv *priv;
112 priv = GET_PRIV (object);
114 g_free (priv->saved_status);
115 g_object_unref (priv->mc);
117 if (priv->gs_proxy) {
118 g_object_unref (priv->gs_proxy);
121 idle_ext_away_stop (EMPATHY_IDLE (object));
125 empathy_idle_new (void)
127 static EmpathyIdle *idle = NULL;
130 idle = g_object_new (EMPATHY_TYPE_IDLE, NULL);
131 g_object_add_weak_pointer (G_OBJECT (idle), (gpointer) &idle);
140 idle_session_idle_changed_cb (DBusGProxy *gs_proxy,
144 EmpathyIdlePriv *priv;
146 priv = GET_PRIV (idle);
148 gossip_debug (DEBUG_DOMAIN, "Session idle state changed, %s -> %s",
149 priv->is_idle ? "yes" : "no",
150 is_idle ? "yes" : "no");
152 if (is_idle && !priv->is_idle) {
153 McPresence new_state = MC_PRESENCE_AWAY;
154 /* We are now idle, set state to away */
156 priv->saved_state = mission_control_get_presence_actual (priv->mc, NULL);
157 priv->saved_status = mission_control_get_presence_message_actual (priv->mc, NULL);
159 if (priv->saved_state <= MC_PRESENCE_OFFLINE ||
160 priv->saved_state == MC_PRESENCE_HIDDEN) {
161 /* We are not online so nothing to do here */
163 } else if (priv->saved_state == MC_PRESENCE_AWAY ||
164 priv->saved_state == MC_PRESENCE_EXTENDED_AWAY) {
165 /* User set away manually, when coming back we restore
166 * default presence. */
167 new_state = priv->saved_state;
168 priv->saved_state = MC_PRESENCE_AVAILABLE;
169 priv->saved_status = NULL;
172 gossip_debug (DEBUG_DOMAIN, "Going to autoaway");
173 mission_control_set_presence (priv->mc,
177 idle_ext_away_start (idle);
178 } else if (!is_idle && priv->is_idle) {
179 /* We are no more idle, restore state */
180 idle_ext_away_stop (idle);
182 gossip_debug (DEBUG_DOMAIN, "Restoring state to %d %s",
186 mission_control_set_presence (priv->mc,
190 g_free (priv->saved_status);
191 priv->saved_status = NULL;
194 priv->is_idle = is_idle;
198 idle_ext_away_start (EmpathyIdle *idle)
200 EmpathyIdlePriv *priv;
202 priv = GET_PRIV (idle);
204 idle_ext_away_stop (idle);
205 priv->ext_away_timeout = g_timeout_add (EXT_AWAY_TIME * 1000,
206 (GSourceFunc) idle_ext_away_cb,
211 idle_ext_away_stop (EmpathyIdle *idle)
213 EmpathyIdlePriv *priv;
215 priv = GET_PRIV (idle);
217 if (priv->ext_away_timeout) {
218 g_source_remove (priv->ext_away_timeout);
219 priv->ext_away_timeout = 0;
224 idle_ext_away_cb (EmpathyIdle *idle)
226 EmpathyIdlePriv *priv;
228 priv = GET_PRIV (idle);
230 gossip_debug (DEBUG_DOMAIN, "Going to extended autoaway");
231 mission_control_set_presence (priv->mc,
232 MC_PRESENCE_EXTENDED_AWAY,
236 priv->ext_away_timeout = 0;