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 last_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->last_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 /* We are now idle, set state to away */
154 g_free (priv->last_status);
155 idle_ext_away_stop (idle);
157 priv->last_state = mission_control_get_presence_actual (priv->mc, NULL);
158 priv->last_status = mission_control_get_presence_message_actual (priv->mc, NULL);
160 gossip_debug (DEBUG_DOMAIN, "Going to autoaway");
161 mission_control_set_presence (priv->mc,
165 idle_ext_away_start (idle);
166 } else if (!is_idle && priv->is_idle) {
167 /* We are no more idle, restore state */
168 idle_ext_away_stop (idle);
170 gossip_debug (DEBUG_DOMAIN, "Restoring state to %d %s",
174 mission_control_set_presence (priv->mc,
180 priv->is_idle = is_idle;
184 idle_ext_away_start (EmpathyIdle *idle)
186 EmpathyIdlePriv *priv;
188 priv = GET_PRIV (idle);
190 idle_ext_away_stop (idle);
191 priv->ext_away_timeout = g_timeout_add (EXT_AWAY_TIME * 1000,
192 (GSourceFunc) idle_ext_away_cb,
197 idle_ext_away_stop (EmpathyIdle *idle)
199 EmpathyIdlePriv *priv;
201 priv = GET_PRIV (idle);
203 if (priv->ext_away_timeout) {
204 g_source_remove (priv->ext_away_timeout);
205 priv->ext_away_timeout = 0;
210 idle_ext_away_cb (EmpathyIdle *idle)
212 EmpathyIdlePriv *priv;
214 priv = GET_PRIV (idle);
216 gossip_debug (DEBUG_DOMAIN, "Going to extended autoaway");
217 mission_control_set_presence (priv->mc,
218 MC_PRESENCE_EXTENDED_AWAY,
219 _("Extended autoaway"),
222 priv->ext_away_timeout = 0;