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>
32 #include "empathy-account-manager.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)
46 EmpathyConnectivity *connectivity;
47 gulong state_change_signal_id;
49 TpConnectionPresenceType state;
51 TpConnectionPresenceType flash_state;
54 TpConnectionPresenceType away_saved_state;
55 TpConnectionPresenceType saved_state;
59 guint ext_away_timeout;
61 EmpathyAccountManager *manager;
65 SESSION_STATUS_AVAILABLE,
66 SESSION_STATUS_INVISIBLE,
69 SESSION_STATUS_UNKNOWN
80 G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT);
82 static EmpathyIdle * idle_singleton = NULL;
85 idle_presence_changed_cb (EmpathyAccountManager *manager,
86 TpConnectionPresenceType state,
88 gchar *status_message,
91 EmpathyIdlePriv *priv;
93 priv = GET_PRIV (idle);
95 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET)
96 /* Assume our presence is offline if MC reports UNSET */
97 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
99 DEBUG ("Presence changed to '%s' (%d) \"%s\"", status, state,
102 g_free (priv->status);
104 if (EMP_STR_EMPTY (status_message))
107 priv->status = g_strdup (status_message);
109 g_object_notify (G_OBJECT (idle), "state");
110 g_object_notify (G_OBJECT (idle), "status");
114 idle_ext_away_cb (EmpathyIdle *idle)
116 EmpathyIdlePriv *priv;
118 priv = GET_PRIV (idle);
120 DEBUG ("Going to extended autoaway");
121 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY);
122 priv->ext_away_timeout = 0;
128 idle_ext_away_stop (EmpathyIdle *idle)
130 EmpathyIdlePriv *priv;
132 priv = GET_PRIV (idle);
134 if (priv->ext_away_timeout) {
135 g_source_remove (priv->ext_away_timeout);
136 priv->ext_away_timeout = 0;
141 idle_ext_away_start (EmpathyIdle *idle)
143 EmpathyIdlePriv *priv;
145 priv = GET_PRIV (idle);
147 if (priv->ext_away_timeout != 0) {
150 priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
151 (GSourceFunc) idle_ext_away_cb,
156 idle_session_status_changed_cb (DBusGProxy *gs_proxy,
157 SessionStatus status,
160 EmpathyIdlePriv *priv;
163 priv = GET_PRIV (idle);
165 is_idle = (status == SESSION_STATUS_IDLE);
167 DEBUG ("Session idle state changed, %s -> %s",
168 priv->is_idle ? "yes" : "no",
169 is_idle ? "yes" : "no");
171 if (!priv->auto_away ||
172 (priv->saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET &&
173 (priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
174 priv->state == TP_CONNECTION_PRESENCE_TYPE_HIDDEN))) {
175 /* We don't want to go auto away OR we explicitely asked to be
176 * offline, nothing to do here */
177 priv->is_idle = is_idle;
181 if (is_idle && !priv->is_idle) {
182 TpConnectionPresenceType new_state;
183 /* We are now idle */
185 idle_ext_away_start (idle);
187 if (priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
188 /* We are disconnected, when coming back from away
189 * we want to restore the presence before the
191 priv->away_saved_state = priv->saved_state;
193 priv->away_saved_state = priv->state;
196 new_state = TP_CONNECTION_PRESENCE_TYPE_AWAY;
197 if (priv->state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
198 new_state = TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY;
201 DEBUG ("Going to autoaway. Saved state=%d, new state=%d",
202 priv->away_saved_state, new_state);
203 empathy_idle_set_state (idle, new_state);
204 } else if (!is_idle && priv->is_idle) {
205 const gchar *new_status;
206 /* We are no more idle, restore state */
208 idle_ext_away_stop (idle);
210 if (priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_AWAY ||
211 priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
212 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
215 new_status = priv->status;
218 /* Only try and set the presence if the away saved state is not
219 * unset. This is an odd case because it means that the session
220 * didn't notify us of the state change to idle, and as a
221 * result, we couldn't save the current state at that time.
223 if (priv->away_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
224 DEBUG ("Restoring state to %d, reset status to %s",
225 priv->away_saved_state, new_status);
227 empathy_idle_set_presence (idle,
228 priv->away_saved_state,
231 DEBUG ("Away saved state is unset. This means that we "
232 "weren't told when the session went idle. "
233 "As a result, I'm not trying to set presence");
236 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
239 priv->is_idle = is_idle;
243 idle_state_change_cb (EmpathyConnectivity *connectivity,
247 EmpathyIdlePriv *priv;
249 priv = GET_PRIV (idle);
252 /* We are no longer connected */
253 DEBUG ("Disconnected: Save state %d (%s)",
254 priv->state, priv->status);
255 priv->saved_state = priv->state;
256 g_free (priv->saved_status);
257 priv->saved_status = g_strdup (priv->status);
258 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
261 && priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
262 /* We are now connected */
263 DEBUG ("Reconnected: Restore state %d (%s)",
264 priv->saved_state, priv->saved_status);
265 empathy_idle_set_presence (idle,
268 priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
269 g_free (priv->saved_status);
270 priv->saved_status = NULL;
275 idle_finalize (GObject *object)
277 EmpathyIdlePriv *priv;
279 priv = GET_PRIV (object);
281 g_free (priv->status);
283 if (priv->gs_proxy) {
284 g_object_unref (priv->gs_proxy);
287 g_signal_handler_disconnect (priv->connectivity,
288 priv->state_change_signal_id);
289 priv->state_change_signal_id = 0;
291 g_object_unref (priv->connectivity);
293 idle_ext_away_stop (EMPATHY_IDLE (object));
297 idle_constructor (GType type,
299 GObjectConstructParam *props)
303 if (idle_singleton) {
304 retval = g_object_ref (idle_singleton);
306 retval = G_OBJECT_CLASS (empathy_idle_parent_class)->constructor
307 (type, n_props, props);
309 idle_singleton = EMPATHY_IDLE (retval);
310 g_object_add_weak_pointer (retval, (gpointer) &idle_singleton);
317 idle_get_property (GObject *object,
322 EmpathyIdlePriv *priv;
325 priv = GET_PRIV (object);
326 idle = EMPATHY_IDLE (object);
330 g_value_set_enum (value, empathy_idle_get_state (idle));
333 g_value_set_string (value, empathy_idle_get_status (idle));
335 case PROP_FLASH_STATE:
336 g_value_set_enum (value, empathy_idle_get_flash_state (idle));
339 g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
342 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
348 idle_set_property (GObject *object,
353 EmpathyIdlePriv *priv;
356 priv = GET_PRIV (object);
357 idle = EMPATHY_IDLE (object);
361 empathy_idle_set_state (idle, g_value_get_enum (value));
364 empathy_idle_set_status (idle, g_value_get_string (value));
366 case PROP_FLASH_STATE:
367 empathy_idle_set_flash_state (idle, g_value_get_enum (value));
370 empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
373 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
379 empathy_idle_class_init (EmpathyIdleClass *klass)
381 GObjectClass *object_class = G_OBJECT_CLASS (klass);
383 object_class->finalize = idle_finalize;
384 object_class->constructor = idle_constructor;
385 object_class->get_property = idle_get_property;
386 object_class->set_property = idle_set_property;
388 g_object_class_install_property (object_class,
390 g_param_spec_uint ("state",
393 0, NUM_TP_CONNECTION_PRESENCE_TYPES,
394 TP_CONNECTION_PRESENCE_TYPE_UNSET,
396 g_object_class_install_property (object_class,
398 g_param_spec_string ("status",
403 g_object_class_install_property (object_class,
405 g_param_spec_uint ("flash-state",
408 0, NUM_TP_CONNECTION_PRESENCE_TYPES,
409 TP_CONNECTION_PRESENCE_TYPE_UNSET,
412 g_object_class_install_property (object_class,
414 g_param_spec_boolean ("auto-away",
415 "Automatic set presence to away",
416 "Should it set presence to away if inactive",
420 g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
424 account_manager_ready_cb (EmpathyAccountManager *account_manager,
428 EmpathyIdlePriv *priv;
429 TpConnectionPresenceType state;
430 gchar *status, *status_message;
432 priv = GET_PRIV (idle);
434 state = empathy_account_manager_get_global_presence (priv->manager,
435 &status, &status_message);
437 idle_presence_changed_cb (account_manager, state, status,
438 status_message, idle);
441 g_free (status_message);
445 empathy_idle_init (EmpathyIdle *idle)
447 EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
448 EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
451 priv->is_idle = FALSE;
453 priv->manager = empathy_account_manager_dup_singleton ();
455 if (empathy_account_manager_is_ready (priv->manager)) {
456 priv->state = empathy_account_manager_get_global_presence (priv->manager,
457 NULL, &priv->status);
459 g_signal_connect (priv->manager, "notify::ready",
460 G_CALLBACK (account_manager_ready_cb), idle);
464 g_signal_connect (priv->manager, "global-presence-changed",
465 G_CALLBACK (idle_presence_changed_cb), idle);
467 priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
468 "org.gnome.SessionManager",
469 "/org/gnome/SessionManager/Presence",
470 "org.gnome.SessionManager.Presence");
471 if (priv->gs_proxy) {
472 dbus_g_proxy_add_signal (priv->gs_proxy, "StatusChanged",
473 G_TYPE_UINT, G_TYPE_INVALID);
474 dbus_g_proxy_connect_signal (priv->gs_proxy, "StatusChanged",
475 G_CALLBACK (idle_session_status_changed_cb),
478 DEBUG ("Failed to get gs proxy");
481 priv->connectivity = empathy_connectivity_dup_singleton ();
482 priv->state_change_signal_id = g_signal_connect (priv->connectivity,
483 "state-change", G_CALLBACK (idle_state_change_cb), idle);
487 empathy_idle_dup_singleton (void)
489 return g_object_new (EMPATHY_TYPE_IDLE, NULL);
492 TpConnectionPresenceType
493 empathy_idle_get_state (EmpathyIdle *idle)
495 EmpathyIdlePriv *priv;
497 priv = GET_PRIV (idle);
503 empathy_idle_set_state (EmpathyIdle *idle,
504 TpConnectionPresenceType state)
506 EmpathyIdlePriv *priv;
508 priv = GET_PRIV (idle);
510 empathy_idle_set_presence (idle, state, priv->status);
514 empathy_idle_get_status (EmpathyIdle *idle)
516 EmpathyIdlePriv *priv;
518 priv = GET_PRIV (idle);
521 return empathy_presence_get_default_message (priv->state);
528 empathy_idle_set_status (EmpathyIdle *idle,
531 EmpathyIdlePriv *priv;
533 priv = GET_PRIV (idle);
535 empathy_idle_set_presence (idle, priv->state, status);
538 TpConnectionPresenceType
539 empathy_idle_get_flash_state (EmpathyIdle *idle)
541 EmpathyIdlePriv *priv;
543 priv = GET_PRIV (idle);
545 return priv->flash_state;
549 empathy_idle_set_flash_state (EmpathyIdle *idle,
550 TpConnectionPresenceType state)
552 EmpathyIdlePriv *priv;
554 priv = GET_PRIV (idle);
556 priv->flash_state = state;
558 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
561 g_object_notify (G_OBJECT (idle), "flash-state");
565 empathy_idle_do_set_presence (EmpathyIdle *idle,
566 TpConnectionPresenceType status_type,
567 const gchar *status_message)
569 EmpathyIdlePriv *priv = GET_PRIV (idle);
570 const gchar *statuses[NUM_TP_CONNECTION_PRESENCE_TYPES] = {
583 g_assert (status_type > 0 && status_type < NUM_TP_CONNECTION_PRESENCE_TYPES);
585 status = statuses[status_type];
587 g_return_if_fail (status != NULL);
589 empathy_account_manager_request_global_presence (priv->manager,
590 status_type, status, status_message);
594 empathy_idle_set_presence (EmpathyIdle *idle,
595 TpConnectionPresenceType state,
598 EmpathyIdlePriv *priv;
599 const gchar *default_status;
601 priv = GET_PRIV (idle);
603 DEBUG ("Changing presence to %s (%d)", status, state);
605 /* Do not set translated default messages */
606 default_status = empathy_presence_get_default_message (state);
607 if (!tp_strdiff (status, default_status)) {
611 if (state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE &&
612 !empathy_connectivity_is_online (priv->connectivity)) {
613 DEBUG ("Empathy is not online");
615 priv->saved_state = state;
616 if (tp_strdiff (priv->status, status)) {
617 g_free (priv->saved_status);
618 priv->saved_status = NULL;
619 if (!EMP_STR_EMPTY (status)) {
620 priv->saved_status = g_strdup (status);
626 empathy_idle_do_set_presence (idle, state, status);
630 empathy_idle_get_auto_away (EmpathyIdle *idle)
632 EmpathyIdlePriv *priv = GET_PRIV (idle);
634 return priv->auto_away;
638 empathy_idle_set_auto_away (EmpathyIdle *idle,
641 EmpathyIdlePriv *priv = GET_PRIV (idle);
643 priv->auto_away = auto_away;
645 g_object_notify (G_OBJECT (idle), "auto-away");