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,
233 EmpathyIdlePriv *priv;
235 priv = GET_PRIV (idle);
237 if (old_online && !new_online) {
238 /* We are no longer connected */
239 DEBUG ("Disconnected: Save state %d (%s)",
240 priv->state, priv->status);
241 priv->saved_state = priv->state;
242 g_free (priv->saved_status);
243 priv->saved_status = g_strdup (priv->status);
244 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
246 else if (!old_online && new_online
247 && priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
248 /* We are now connected */
249 DEBUG ("Reconnected: Restore state %d (%s)",
250 priv->saved_state, priv->saved_status);
251 empathy_idle_set_presence (idle,
254 priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
255 g_free (priv->saved_status);
256 priv->saved_status = NULL;
261 idle_use_conn_cb (GObject *object,
265 EmpathyIdlePriv *priv = GET_PRIV (idle);
267 if (!empathy_connectivity_get_use_conn (priv->connectivity)) {
268 if (priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
269 empathy_idle_set_state (idle, priv->saved_state);
272 priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
277 idle_finalize (GObject *object)
279 EmpathyIdlePriv *priv;
281 priv = GET_PRIV (object);
283 g_free (priv->status);
284 g_object_unref (priv->mc);
286 if (priv->gs_proxy) {
287 g_object_unref (priv->gs_proxy);
290 g_signal_handlers_disconnect_by_func (priv->connectivity,
291 idle_state_change_cb, object);
292 g_signal_handlers_disconnect_by_func (priv->connectivity,
293 idle_use_conn_cb, object);
295 g_object_unref (priv->connectivity);
297 idle_ext_away_stop (EMPATHY_IDLE (object));
301 idle_constructor (GType type,
303 GObjectConstructParam *props)
307 if (idle_singleton) {
308 retval = g_object_ref (idle_singleton);
310 retval = G_OBJECT_CLASS (empathy_idle_parent_class)->constructor
311 (type, n_props, props);
313 idle_singleton = EMPATHY_IDLE (retval);
314 g_object_add_weak_pointer (retval, (gpointer) &idle_singleton);
321 idle_get_property (GObject *object,
326 EmpathyIdlePriv *priv;
329 priv = GET_PRIV (object);
330 idle = EMPATHY_IDLE (object);
334 g_value_set_enum (value, empathy_idle_get_state (idle));
337 g_value_set_string (value, empathy_idle_get_status (idle));
339 case PROP_FLASH_STATE:
340 g_value_set_enum (value, empathy_idle_get_flash_state (idle));
343 g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
346 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
352 idle_set_property (GObject *object,
357 EmpathyIdlePriv *priv;
360 priv = GET_PRIV (object);
361 idle = EMPATHY_IDLE (object);
365 empathy_idle_set_state (idle, g_value_get_enum (value));
368 empathy_idle_set_status (idle, g_value_get_string (value));
370 case PROP_FLASH_STATE:
371 empathy_idle_set_flash_state (idle, g_value_get_enum (value));
374 empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
377 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
383 empathy_idle_class_init (EmpathyIdleClass *klass)
385 GObjectClass *object_class = G_OBJECT_CLASS (klass);
387 object_class->finalize = idle_finalize;
388 object_class->constructor = idle_constructor;
389 object_class->get_property = idle_get_property;
390 object_class->set_property = idle_set_property;
392 g_object_class_install_property (object_class,
394 g_param_spec_uint ("state",
397 0, NUM_TP_CONNECTION_PRESENCE_TYPES,
398 TP_CONNECTION_PRESENCE_TYPE_UNSET,
400 g_object_class_install_property (object_class,
402 g_param_spec_string ("status",
407 g_object_class_install_property (object_class,
409 g_param_spec_uint ("flash-state",
412 0, NUM_TP_CONNECTION_PRESENCE_TYPES,
413 TP_CONNECTION_PRESENCE_TYPE_UNSET,
416 g_object_class_install_property (object_class,
418 g_param_spec_boolean ("auto-away",
419 "Automatic set presence to away",
420 "Should it set presence to away if inactive",
424 g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
427 static TpConnectionPresenceType
428 empathy_idle_get_actual_presence (EmpathyIdle *idle, GError **error)
431 EmpathyIdlePriv *priv = GET_PRIV (idle);
433 presence = mission_control_get_presence_actual (priv->mc, error);
436 case MC_PRESENCE_OFFLINE:
437 return TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
438 case MC_PRESENCE_AVAILABLE:
439 return TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
440 case MC_PRESENCE_AWAY:
441 return TP_CONNECTION_PRESENCE_TYPE_AWAY;
442 case MC_PRESENCE_EXTENDED_AWAY:
443 return TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY;
444 case MC_PRESENCE_HIDDEN:
445 return TP_CONNECTION_PRESENCE_TYPE_HIDDEN;
446 case MC_PRESENCE_DO_NOT_DISTURB:
447 return TP_CONNECTION_PRESENCE_TYPE_BUSY;
449 return TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
454 empathy_idle_init (EmpathyIdle *idle)
456 GError *error = NULL;
457 EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
458 EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
461 priv->is_idle = FALSE;
462 priv->mc = empathy_mission_control_dup_singleton ();
463 priv->state = empathy_idle_get_actual_presence (idle, &error);
465 DEBUG ("Error getting actual presence: %s", error->message);
467 /* Fallback to OFFLINE as that's what mission_control_get_presence_actual
468 does. This also ensure to always display the status icon (there is no
469 unset presence icon). */
470 priv->state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
471 g_clear_error (&error);
473 priv->status = mission_control_get_presence_message_actual (priv->mc, &error);
474 if (error || EMP_STR_EMPTY (priv->status)) {
475 g_free (priv->status);
479 DEBUG ("Error getting actual presence message: %s", error->message);
480 g_clear_error (&error);
484 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
486 G_CALLBACK (idle_presence_changed_cb),
489 priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
490 "org.gnome.SessionManager",
491 "/org/gnome/SessionManager/Presence",
492 "org.gnome.SessionManager.Presence");
493 if (priv->gs_proxy) {
494 dbus_g_proxy_add_signal (priv->gs_proxy, "StatusChanged",
495 G_TYPE_UINT, G_TYPE_INVALID);
496 dbus_g_proxy_connect_signal (priv->gs_proxy, "StatusChanged",
497 G_CALLBACK (idle_session_status_changed_cb),
500 DEBUG ("Failed to get gs proxy");
503 priv->connectivity = empathy_connectivity_dup_singleton ();
504 g_signal_connect (priv->connectivity, "state-change",
505 G_CALLBACK (idle_state_change_cb), idle);
506 g_signal_connect (priv->connectivity, "notify::use-conn",
507 G_CALLBACK (idle_use_conn_cb), idle);
511 empathy_idle_dup_singleton (void)
513 return g_object_new (EMPATHY_TYPE_IDLE, NULL);
516 TpConnectionPresenceType
517 empathy_idle_get_state (EmpathyIdle *idle)
519 EmpathyIdlePriv *priv;
521 priv = GET_PRIV (idle);
527 empathy_idle_set_state (EmpathyIdle *idle,
528 TpConnectionPresenceType state)
530 EmpathyIdlePriv *priv;
532 priv = GET_PRIV (idle);
534 empathy_idle_set_presence (idle, state, priv->status);
538 empathy_idle_get_status (EmpathyIdle *idle)
540 EmpathyIdlePriv *priv;
542 priv = GET_PRIV (idle);
545 return empathy_presence_get_default_message (priv->state);
552 empathy_idle_set_status (EmpathyIdle *idle,
555 EmpathyIdlePriv *priv;
557 priv = GET_PRIV (idle);
559 empathy_idle_set_presence (idle, priv->state, status);
562 TpConnectionPresenceType
563 empathy_idle_get_flash_state (EmpathyIdle *idle)
565 EmpathyIdlePriv *priv;
567 priv = GET_PRIV (idle);
569 return priv->flash_state;
573 empathy_idle_set_flash_state (EmpathyIdle *idle,
574 TpConnectionPresenceType state)
576 EmpathyIdlePriv *priv;
578 priv = GET_PRIV (idle);
580 priv->flash_state = state;
582 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
585 g_object_notify (G_OBJECT (idle), "flash-state");
589 empathy_idle_do_set_presence (EmpathyIdle *idle,
590 TpConnectionPresenceType state,
593 McPresence mc_state = MC_PRESENCE_UNSET;
594 EmpathyIdlePriv *priv = GET_PRIV (idle);
597 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
598 mc_state = MC_PRESENCE_OFFLINE;
600 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
601 mc_state = MC_PRESENCE_AVAILABLE;
603 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
604 mc_state = MC_PRESENCE_AWAY;
606 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
607 mc_state = MC_PRESENCE_EXTENDED_AWAY;
609 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
610 mc_state = MC_PRESENCE_HIDDEN;
612 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
613 mc_state = MC_PRESENCE_DO_NOT_DISTURB;
616 g_assert_not_reached ();
619 mission_control_set_presence (priv->mc, mc_state, status, NULL, NULL);
623 empathy_idle_set_presence (EmpathyIdle *idle,
624 TpConnectionPresenceType state,
627 EmpathyIdlePriv *priv;
628 const gchar *default_status;
630 priv = GET_PRIV (idle);
632 DEBUG ("Changing presence to %s (%d)", status, state);
634 /* Do not set translated default messages */
635 default_status = empathy_presence_get_default_message (state);
636 if (!tp_strdiff (status, default_status)) {
640 if (!empathy_connectivity_is_online (priv->connectivity)) {
641 DEBUG ("Empathy is not online");
643 if (tp_strdiff (priv->status, status)) {
644 g_free (priv->status);
646 if (!EMP_STR_EMPTY (status)) {
647 priv->status = g_strdup (status);
649 g_object_notify (G_OBJECT (idle), "status");
653 empathy_idle_do_set_presence (idle, state, status);
657 empathy_idle_get_auto_away (EmpathyIdle *idle)
659 EmpathyIdlePriv *priv = GET_PRIV (idle);
661 return priv->auto_away;
665 empathy_idle_set_auto_away (EmpathyIdle *idle,
668 EmpathyIdlePriv *priv = GET_PRIV (idle);
670 priv->auto_away = auto_away;
672 g_object_notify (G_OBJECT (idle), "auto-away");