1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2008 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library 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 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
26 #include <glib/gi18n-lib.h>
27 #include <dbus/dbus-glib.h>
29 #include <telepathy-glib/dbus.h>
30 #include <telepathy-glib/util.h>
31 #include <libmissioncontrol/mc-enum-types.h>
33 #include "empathy-idle.h"
34 #include "empathy-utils.h"
35 #include "empathy-connectivity.h"
37 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
38 #include "empathy-debug.h"
40 /* Number of seconds before entering extended autoaway. */
41 #define EXT_AWAY_TIME (30*60)
43 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIdle)
47 EmpathyConnectivity *connectivity;
49 TpConnectionPresenceType state;
51 TpConnectionPresenceType flash_state;
54 TpConnectionPresenceType away_saved_state;
55 TpConnectionPresenceType saved_state;
59 guint ext_away_timeout;
63 SESSION_STATUS_AVAILABLE,
64 SESSION_STATUS_INVISIBLE,
67 SESSION_STATUS_UNKNOWN
78 G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT);
80 static EmpathyIdle * idle_singleton = NULL;
83 idle_presence_changed_cb (MissionControl *mc,
84 TpConnectionPresenceType state,
88 EmpathyIdlePriv *priv;
90 priv = GET_PRIV (idle);
92 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET)
93 /* Assume our presence is offline if MC reports UNSET */
94 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
96 DEBUG ("Presence changed to '%s' (%d)", status, state);
98 g_free (priv->status);
101 if (!EMP_STR_EMPTY (status)) {
102 priv->status = g_strdup (status);
105 g_object_notify (G_OBJECT (idle), "state");
106 g_object_notify (G_OBJECT (idle), "status");
110 idle_ext_away_cb (EmpathyIdle *idle)
112 EmpathyIdlePriv *priv;
114 priv = GET_PRIV (idle);
116 DEBUG ("Going to extended autoaway");
117 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY);
118 priv->ext_away_timeout = 0;
124 idle_ext_away_stop (EmpathyIdle *idle)
126 EmpathyIdlePriv *priv;
128 priv = GET_PRIV (idle);
130 if (priv->ext_away_timeout) {
131 g_source_remove (priv->ext_away_timeout);
132 priv->ext_away_timeout = 0;
137 idle_ext_away_start (EmpathyIdle *idle)
139 EmpathyIdlePriv *priv;
141 priv = GET_PRIV (idle);
143 if (priv->ext_away_timeout != 0) {
146 priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
147 (GSourceFunc) idle_ext_away_cb,
152 idle_session_status_changed_cb (DBusGProxy *gs_proxy,
153 SessionStatus status,
156 EmpathyIdlePriv *priv;
159 priv = GET_PRIV (idle);
161 is_idle = (status == SESSION_STATUS_IDLE);
163 DEBUG ("Session idle state changed, %s -> %s",
164 priv->is_idle ? "yes" : "no",
165 is_idle ? "yes" : "no");
167 if (!priv->auto_away ||
168 (priv->saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET &&
169 (priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
170 priv->state == TP_CONNECTION_PRESENCE_TYPE_HIDDEN))) {
171 /* We don't want to go auto away OR we explicitely asked to be
172 * offline, nothing to do here */
173 priv->is_idle = is_idle;
177 if (is_idle && !priv->is_idle) {
178 TpConnectionPresenceType new_state;
179 /* We are now idle */
181 idle_ext_away_start (idle);
183 if (priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
184 /* We are disconnected, when coming back from away
185 * we want to restore the presence before the
187 priv->away_saved_state = priv->saved_state;
189 priv->away_saved_state = priv->state;
192 new_state = TP_CONNECTION_PRESENCE_TYPE_AWAY;
193 if (priv->state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
194 new_state = TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY;
197 DEBUG ("Going to autoaway. Saved state=%d, new state=%d",
198 priv->away_saved_state, new_state);
199 empathy_idle_set_state (idle, new_state);
200 } else if (!is_idle && priv->is_idle) {
201 const gchar *new_status;
202 /* We are no more idle, restore state */
204 idle_ext_away_stop (idle);
206 if (priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_AWAY ||
207 priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
208 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
211 new_status = priv->status;
214 DEBUG ("Restoring state to %d, reset status to %s",
215 priv->away_saved_state, new_status);
217 empathy_idle_set_presence (idle,
218 priv->away_saved_state,
221 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
224 priv->is_idle = is_idle;
228 idle_state_change_cb (EmpathyConnectivity *connectivity,
232 EmpathyIdlePriv *priv;
234 priv = GET_PRIV (idle);
237 /* We are no longer connected */
238 DEBUG ("Disconnected: Save state %d (%s)",
239 priv->state, priv->status);
240 priv->saved_state = priv->state;
241 g_free (priv->saved_status);
242 priv->saved_status = g_strdup (priv->status);
243 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
246 && priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
247 /* We are now connected */
248 DEBUG ("Reconnected: Restore state %d (%s)",
249 priv->saved_state, priv->saved_status);
250 empathy_idle_set_presence (idle,
253 priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
254 g_free (priv->saved_status);
255 priv->saved_status = NULL;
260 idle_finalize (GObject *object)
262 EmpathyIdlePriv *priv;
264 priv = GET_PRIV (object);
266 g_free (priv->status);
267 g_object_unref (priv->mc);
269 if (priv->gs_proxy) {
270 g_object_unref (priv->gs_proxy);
273 g_signal_handlers_disconnect_by_func (priv->connectivity,
274 idle_state_change_cb, object);
276 g_object_unref (priv->connectivity);
278 idle_ext_away_stop (EMPATHY_IDLE (object));
282 idle_constructor (GType type,
284 GObjectConstructParam *props)
288 if (idle_singleton) {
289 retval = g_object_ref (idle_singleton);
291 retval = G_OBJECT_CLASS (empathy_idle_parent_class)->constructor
292 (type, n_props, props);
294 idle_singleton = EMPATHY_IDLE (retval);
295 g_object_add_weak_pointer (retval, (gpointer) &idle_singleton);
302 idle_get_property (GObject *object,
307 EmpathyIdlePriv *priv;
310 priv = GET_PRIV (object);
311 idle = EMPATHY_IDLE (object);
315 g_value_set_enum (value, empathy_idle_get_state (idle));
318 g_value_set_string (value, empathy_idle_get_status (idle));
320 case PROP_FLASH_STATE:
321 g_value_set_enum (value, empathy_idle_get_flash_state (idle));
324 g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
327 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
333 idle_set_property (GObject *object,
338 EmpathyIdlePriv *priv;
341 priv = GET_PRIV (object);
342 idle = EMPATHY_IDLE (object);
346 empathy_idle_set_state (idle, g_value_get_enum (value));
349 empathy_idle_set_status (idle, g_value_get_string (value));
351 case PROP_FLASH_STATE:
352 empathy_idle_set_flash_state (idle, g_value_get_enum (value));
355 empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
358 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
364 empathy_idle_class_init (EmpathyIdleClass *klass)
366 GObjectClass *object_class = G_OBJECT_CLASS (klass);
368 object_class->finalize = idle_finalize;
369 object_class->constructor = idle_constructor;
370 object_class->get_property = idle_get_property;
371 object_class->set_property = idle_set_property;
373 g_object_class_install_property (object_class,
375 g_param_spec_uint ("state",
378 0, NUM_TP_CONNECTION_PRESENCE_TYPES,
379 TP_CONNECTION_PRESENCE_TYPE_UNSET,
381 g_object_class_install_property (object_class,
383 g_param_spec_string ("status",
388 g_object_class_install_property (object_class,
390 g_param_spec_uint ("flash-state",
393 0, NUM_TP_CONNECTION_PRESENCE_TYPES,
394 TP_CONNECTION_PRESENCE_TYPE_UNSET,
397 g_object_class_install_property (object_class,
399 g_param_spec_boolean ("auto-away",
400 "Automatic set presence to away",
401 "Should it set presence to away if inactive",
405 g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
408 static TpConnectionPresenceType
409 empathy_idle_get_actual_presence (EmpathyIdle *idle, GError **error)
412 EmpathyIdlePriv *priv = GET_PRIV (idle);
414 presence = mission_control_get_presence_actual (priv->mc, error);
417 case MC_PRESENCE_OFFLINE:
418 return TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
419 case MC_PRESENCE_AVAILABLE:
420 return TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
421 case MC_PRESENCE_AWAY:
422 return TP_CONNECTION_PRESENCE_TYPE_AWAY;
423 case MC_PRESENCE_EXTENDED_AWAY:
424 return TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY;
425 case MC_PRESENCE_HIDDEN:
426 return TP_CONNECTION_PRESENCE_TYPE_HIDDEN;
427 case MC_PRESENCE_DO_NOT_DISTURB:
428 return TP_CONNECTION_PRESENCE_TYPE_BUSY;
430 return TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
435 empathy_idle_init (EmpathyIdle *idle)
437 GError *error = NULL;
438 EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
439 EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
442 priv->is_idle = FALSE;
443 priv->mc = empathy_mission_control_dup_singleton ();
444 priv->state = empathy_idle_get_actual_presence (idle, &error);
446 DEBUG ("Error getting actual presence: %s", error->message);
448 /* Fallback to OFFLINE as that's what mission_control_get_presence_actual
449 does. This also ensure to always display the status icon (there is no
450 unset presence icon). */
451 priv->state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
452 g_clear_error (&error);
454 priv->status = mission_control_get_presence_message_actual (priv->mc, &error);
455 if (error || EMP_STR_EMPTY (priv->status)) {
456 g_free (priv->status);
460 DEBUG ("Error getting actual presence message: %s", error->message);
461 g_clear_error (&error);
465 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
467 G_CALLBACK (idle_presence_changed_cb),
470 priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
471 "org.gnome.SessionManager",
472 "/org/gnome/SessionManager/Presence",
473 "org.gnome.SessionManager.Presence");
474 if (priv->gs_proxy) {
475 dbus_g_proxy_add_signal (priv->gs_proxy, "StatusChanged",
476 G_TYPE_UINT, G_TYPE_INVALID);
477 dbus_g_proxy_connect_signal (priv->gs_proxy, "StatusChanged",
478 G_CALLBACK (idle_session_status_changed_cb),
481 DEBUG ("Failed to get gs proxy");
484 priv->connectivity = empathy_connectivity_dup_singleton ();
485 g_signal_connect (priv->connectivity, "state-change",
486 G_CALLBACK (idle_state_change_cb), idle);
490 empathy_idle_dup_singleton (void)
492 return g_object_new (EMPATHY_TYPE_IDLE, NULL);
495 TpConnectionPresenceType
496 empathy_idle_get_state (EmpathyIdle *idle)
498 EmpathyIdlePriv *priv;
500 priv = GET_PRIV (idle);
506 empathy_idle_set_state (EmpathyIdle *idle,
507 TpConnectionPresenceType state)
509 EmpathyIdlePriv *priv;
511 priv = GET_PRIV (idle);
513 empathy_idle_set_presence (idle, state, priv->status);
517 empathy_idle_get_status (EmpathyIdle *idle)
519 EmpathyIdlePriv *priv;
521 priv = GET_PRIV (idle);
524 return empathy_presence_get_default_message (priv->state);
531 empathy_idle_set_status (EmpathyIdle *idle,
534 EmpathyIdlePriv *priv;
536 priv = GET_PRIV (idle);
538 empathy_idle_set_presence (idle, priv->state, status);
541 TpConnectionPresenceType
542 empathy_idle_get_flash_state (EmpathyIdle *idle)
544 EmpathyIdlePriv *priv;
546 priv = GET_PRIV (idle);
548 return priv->flash_state;
552 empathy_idle_set_flash_state (EmpathyIdle *idle,
553 TpConnectionPresenceType state)
555 EmpathyIdlePriv *priv;
557 priv = GET_PRIV (idle);
559 priv->flash_state = state;
561 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
564 g_object_notify (G_OBJECT (idle), "flash-state");
568 empathy_idle_do_set_presence (EmpathyIdle *idle,
569 TpConnectionPresenceType state,
572 McPresence mc_state = MC_PRESENCE_UNSET;
573 EmpathyIdlePriv *priv = GET_PRIV (idle);
576 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
577 mc_state = MC_PRESENCE_OFFLINE;
579 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
580 mc_state = MC_PRESENCE_AVAILABLE;
582 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
583 mc_state = MC_PRESENCE_AWAY;
585 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
586 mc_state = MC_PRESENCE_EXTENDED_AWAY;
588 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
589 mc_state = MC_PRESENCE_HIDDEN;
591 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
592 mc_state = MC_PRESENCE_DO_NOT_DISTURB;
595 g_assert_not_reached ();
598 mission_control_set_presence (priv->mc, mc_state, status, NULL, NULL);
602 empathy_idle_set_presence (EmpathyIdle *idle,
603 TpConnectionPresenceType state,
606 EmpathyIdlePriv *priv;
607 const gchar *default_status;
609 priv = GET_PRIV (idle);
611 DEBUG ("Changing presence to %s (%d)", status, state);
613 /* Do not set translated default messages */
614 default_status = empathy_presence_get_default_message (state);
615 if (!tp_strdiff (status, default_status)) {
619 if (!empathy_connectivity_is_online (priv->connectivity)) {
620 DEBUG ("Empathy is not online");
622 if (tp_strdiff (priv->status, status)) {
623 g_free (priv->status);
625 if (!EMP_STR_EMPTY (status)) {
626 priv->status = g_strdup (status);
628 g_object_notify (G_OBJECT (idle), "status");
632 empathy_idle_do_set_presence (idle, state, status);
636 empathy_idle_get_auto_away (EmpathyIdle *idle)
638 EmpathyIdlePriv *priv = GET_PRIV (idle);
640 return priv->auto_away;
644 empathy_idle_set_auto_away (EmpathyIdle *idle,
647 EmpathyIdlePriv *priv = GET_PRIV (idle);
649 priv->auto_away = auto_away;
651 g_object_notify (G_OBJECT (idle), "auto-away");