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/account-manager.h>
30 #include <telepathy-glib/dbus.h>
31 #include <telepathy-glib/util.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 /* Number of seconds to consider an account in the "just connected" state
45 #define ACCOUNT_IS_JUST_CONNECTED_SECONDS 10
47 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIdle)
50 EmpathyConnectivity *connectivity;
51 gulong state_change_signal_id;
55 TpConnectionPresenceType state;
57 TpConnectionPresenceType flash_state;
60 TpConnectionPresenceType away_saved_state;
61 TpConnectionPresenceType saved_state;
65 guint ext_away_timeout;
67 TpAccountManager *manager;
69 /* pointer to a TpAccount --> glong of time of connection */
70 GHashTable *connect_times;
72 TpConnectionPresenceType requested_presence_type;
73 gchar *requested_status_message;
78 SESSION_STATUS_AVAILABLE,
79 SESSION_STATUS_INVISIBLE,
82 SESSION_STATUS_UNKNOWN
93 G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT);
95 static EmpathyIdle * idle_singleton = NULL;
97 static const gchar *presence_type_to_status[NUM_TP_CONNECTION_PRESENCE_TYPES] = {
110 idle_presence_changed_cb (TpAccountManager *manager,
111 TpConnectionPresenceType state,
113 gchar *status_message,
116 EmpathyIdlePriv *priv;
118 priv = GET_PRIV (idle);
120 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET)
121 /* Assume our presence is offline if MC reports UNSET */
122 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
124 DEBUG ("Presence changed to '%s' (%d) \"%s\"", status, state,
127 g_free (priv->status);
129 if (EMP_STR_EMPTY (status_message))
132 priv->status = g_strdup (status_message);
134 g_object_notify (G_OBJECT (idle), "state");
135 g_object_notify (G_OBJECT (idle), "status");
139 idle_ext_away_cb (EmpathyIdle *idle)
141 EmpathyIdlePriv *priv;
143 priv = GET_PRIV (idle);
145 DEBUG ("Going to extended autoaway");
146 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY);
147 priv->ext_away_timeout = 0;
153 idle_ext_away_stop (EmpathyIdle *idle)
155 EmpathyIdlePriv *priv;
157 priv = GET_PRIV (idle);
159 if (priv->ext_away_timeout) {
160 g_source_remove (priv->ext_away_timeout);
161 priv->ext_away_timeout = 0;
166 idle_ext_away_start (EmpathyIdle *idle)
168 EmpathyIdlePriv *priv;
170 priv = GET_PRIV (idle);
172 if (priv->ext_away_timeout != 0) {
175 priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
176 (GSourceFunc) idle_ext_away_cb,
181 idle_session_status_changed_cb (DBusGProxy *gs_proxy,
182 SessionStatus status,
185 EmpathyIdlePriv *priv;
188 priv = GET_PRIV (idle);
190 is_idle = (status == SESSION_STATUS_IDLE);
192 DEBUG ("Session idle state changed, %s -> %s",
193 priv->is_idle ? "yes" : "no",
194 is_idle ? "yes" : "no");
196 if (!priv->auto_away ||
197 (priv->saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET &&
198 (priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
199 priv->state == TP_CONNECTION_PRESENCE_TYPE_HIDDEN))) {
200 /* We don't want to go auto away OR we explicitely asked to be
201 * offline, nothing to do here */
202 priv->is_idle = is_idle;
206 if (is_idle && !priv->is_idle) {
207 TpConnectionPresenceType new_state;
208 /* We are now idle */
210 idle_ext_away_start (idle);
212 if (priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
213 /* We are disconnected, when coming back from away
214 * we want to restore the presence before the
216 priv->away_saved_state = priv->saved_state;
218 priv->away_saved_state = priv->state;
221 new_state = TP_CONNECTION_PRESENCE_TYPE_AWAY;
222 if (priv->state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
223 new_state = TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY;
226 DEBUG ("Going to autoaway. Saved state=%d, new state=%d",
227 priv->away_saved_state, new_state);
228 empathy_idle_set_state (idle, new_state);
229 } else if (!is_idle && priv->is_idle) {
230 const gchar *new_status;
231 /* We are no more idle, restore state */
233 idle_ext_away_stop (idle);
235 if (priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_AWAY ||
236 priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
237 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
240 new_status = priv->status;
243 /* Only try and set the presence if the away saved state is not
244 * unset. This is an odd case because it means that the session
245 * didn't notify us of the state change to idle, and as a
246 * result, we couldn't save the current state at that time.
248 if (priv->away_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
249 DEBUG ("Restoring state to %d, reset status to %s",
250 priv->away_saved_state, new_status);
252 empathy_idle_set_presence (idle,
253 priv->away_saved_state,
256 DEBUG ("Away saved state is unset. This means that we "
257 "weren't told when the session went idle. "
258 "As a result, I'm not trying to set presence");
261 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
264 priv->is_idle = is_idle;
268 idle_state_change_cb (EmpathyConnectivity *connectivity,
272 EmpathyIdlePriv *priv;
274 priv = GET_PRIV (idle);
277 /* We are no longer connected */
278 DEBUG ("Disconnected: Save state %d (%s)",
279 priv->state, priv->status);
280 priv->saved_state = priv->state;
281 g_free (priv->saved_status);
282 priv->saved_status = g_strdup (priv->status);
283 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
286 && priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
287 /* We are now connected */
288 DEBUG ("Reconnected: Restore state %d (%s)",
289 priv->saved_state, priv->saved_status);
290 empathy_idle_set_presence (idle,
293 priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
294 g_free (priv->saved_status);
295 priv->saved_status = NULL;
300 idle_finalize (GObject *object)
302 EmpathyIdlePriv *priv;
304 priv = GET_PRIV (object);
306 g_free (priv->status);
307 g_free (priv->requested_status_message);
309 if (priv->gs_proxy) {
310 g_object_unref (priv->gs_proxy);
313 g_signal_handler_disconnect (priv->connectivity,
314 priv->state_change_signal_id);
315 priv->state_change_signal_id = 0;
317 g_object_unref (priv->connectivity);
319 g_hash_table_destroy (priv->connect_times);
320 priv->connect_times = NULL;
322 idle_ext_away_stop (EMPATHY_IDLE (object));
326 idle_constructor (GType type,
328 GObjectConstructParam *props)
332 if (idle_singleton) {
333 retval = g_object_ref (idle_singleton);
335 retval = G_OBJECT_CLASS (empathy_idle_parent_class)->constructor
336 (type, n_props, props);
338 idle_singleton = EMPATHY_IDLE (retval);
339 g_object_add_weak_pointer (retval, (gpointer) &idle_singleton);
346 idle_get_property (GObject *object,
351 EmpathyIdlePriv *priv;
354 priv = GET_PRIV (object);
355 idle = EMPATHY_IDLE (object);
359 g_value_set_enum (value, empathy_idle_get_state (idle));
362 g_value_set_string (value, empathy_idle_get_status (idle));
364 case PROP_FLASH_STATE:
365 g_value_set_enum (value, empathy_idle_get_flash_state (idle));
368 g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
371 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
377 idle_set_property (GObject *object,
382 EmpathyIdlePriv *priv;
385 priv = GET_PRIV (object);
386 idle = EMPATHY_IDLE (object);
390 empathy_idle_set_state (idle, g_value_get_enum (value));
393 empathy_idle_set_status (idle, g_value_get_string (value));
395 case PROP_FLASH_STATE:
396 empathy_idle_set_flash_state (idle, g_value_get_enum (value));
399 empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
402 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
408 empathy_idle_class_init (EmpathyIdleClass *klass)
410 GObjectClass *object_class = G_OBJECT_CLASS (klass);
412 object_class->finalize = idle_finalize;
413 object_class->constructor = idle_constructor;
414 object_class->get_property = idle_get_property;
415 object_class->set_property = idle_set_property;
417 g_object_class_install_property (object_class,
419 g_param_spec_uint ("state",
422 0, NUM_TP_CONNECTION_PRESENCE_TYPES,
423 TP_CONNECTION_PRESENCE_TYPE_UNSET,
425 g_object_class_install_property (object_class,
427 g_param_spec_string ("status",
432 g_object_class_install_property (object_class,
434 g_param_spec_uint ("flash-state",
437 0, NUM_TP_CONNECTION_PRESENCE_TYPES,
438 TP_CONNECTION_PRESENCE_TYPE_UNSET,
441 g_object_class_install_property (object_class,
443 g_param_spec_boolean ("auto-away",
444 "Automatic set presence to away",
445 "Should it set presence to away if inactive",
449 g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
453 account_status_changed_cb (TpAccount *account,
457 gchar *dbus_error_name,
461 EmpathyIdle *idle = EMPATHY_IDLE (user_data);
462 EmpathyIdlePriv *priv = GET_PRIV (idle);
465 if (new_status == TP_CONNECTION_STATUS_CONNECTED) {
466 g_get_current_time (&val);
467 g_hash_table_insert (priv->connect_times, account,
468 GINT_TO_POINTER (val.tv_sec));
469 } else if (new_status == TP_CONNECTION_STATUS_DISCONNECTED) {
470 g_hash_table_remove (priv->connect_times, account);
475 account_manager_ready_cb (GObject *source_object,
476 GAsyncResult *result,
479 EmpathyIdle *idle = EMPATHY_IDLE (user_data);
480 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
481 EmpathyIdlePriv *priv = GET_PRIV (idle);
482 TpConnectionPresenceType state;
483 gchar *status, *status_message;
485 GError *error = NULL;
489 if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
490 DEBUG ("Failed to prepare account manager: %s", error->message);
491 g_error_free (error);
495 state = tp_account_manager_get_most_available_presence (priv->manager,
496 &status, &status_message);
498 idle_presence_changed_cb (account_manager, state, status,
499 status_message, idle);
501 accounts = tp_account_manager_get_valid_accounts (priv->manager);
502 for (l = accounts; l != NULL; l = l->next) {
503 empathy_signal_connect_weak (l->data, "status-changed",
504 G_CALLBACK (account_status_changed_cb),
507 g_list_free (accounts);
510 g_free (status_message);
514 empathy_idle_init (EmpathyIdle *idle)
516 EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
517 EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
520 priv->is_idle = FALSE;
522 priv->manager = tp_account_manager_dup ();
524 tp_account_manager_prepare_async (priv->manager, NULL,
525 account_manager_ready_cb, idle);
527 g_signal_connect (priv->manager, "most-available-presence-changed",
528 G_CALLBACK (idle_presence_changed_cb), idle);
530 priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
531 "org.gnome.SessionManager",
532 "/org/gnome/SessionManager/Presence",
533 "org.gnome.SessionManager.Presence");
534 if (priv->gs_proxy) {
535 dbus_g_proxy_add_signal (priv->gs_proxy, "StatusChanged",
536 G_TYPE_UINT, G_TYPE_INVALID);
537 dbus_g_proxy_connect_signal (priv->gs_proxy, "StatusChanged",
538 G_CALLBACK (idle_session_status_changed_cb),
541 DEBUG ("Failed to get gs proxy");
544 priv->connectivity = empathy_connectivity_dup_singleton ();
545 priv->state_change_signal_id = g_signal_connect (priv->connectivity,
546 "state-change", G_CALLBACK (idle_state_change_cb), idle);
548 priv->connect_times = g_hash_table_new (g_direct_hash, g_direct_equal);
552 empathy_idle_dup_singleton (void)
554 return g_object_new (EMPATHY_TYPE_IDLE, NULL);
557 TpConnectionPresenceType
558 empathy_idle_get_state (EmpathyIdle *idle)
560 EmpathyIdlePriv *priv;
562 priv = GET_PRIV (idle);
564 if (G_UNLIKELY (!priv->ready))
565 g_critical (G_STRLOC ": %s called before AccountManager ready",
572 empathy_idle_set_state (EmpathyIdle *idle,
573 TpConnectionPresenceType state)
575 EmpathyIdlePriv *priv;
577 priv = GET_PRIV (idle);
579 empathy_idle_set_presence (idle, state, priv->status);
583 empathy_idle_get_status (EmpathyIdle *idle)
585 EmpathyIdlePriv *priv;
587 priv = GET_PRIV (idle);
589 if (G_UNLIKELY (!priv->ready))
590 g_critical (G_STRLOC ": %s called before AccountManager ready",
594 return empathy_presence_get_default_message (priv->state);
601 empathy_idle_set_status (EmpathyIdle *idle,
604 EmpathyIdlePriv *priv;
606 priv = GET_PRIV (idle);
608 empathy_idle_set_presence (idle, priv->state, status);
611 TpConnectionPresenceType
612 empathy_idle_get_flash_state (EmpathyIdle *idle)
614 EmpathyIdlePriv *priv;
616 priv = GET_PRIV (idle);
618 if (G_UNLIKELY (!priv->ready))
619 g_critical (G_STRLOC ": %s called before AccountManager ready",
622 return priv->flash_state;
626 empathy_idle_set_flash_state (EmpathyIdle *idle,
627 TpConnectionPresenceType state)
629 EmpathyIdlePriv *priv;
631 priv = GET_PRIV (idle);
633 priv->flash_state = state;
635 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
638 g_object_notify (G_OBJECT (idle), "flash-state");
642 empathy_idle_do_set_presence (EmpathyIdle *idle,
643 TpConnectionPresenceType status_type,
644 const gchar *status_message)
646 EmpathyIdlePriv *priv = GET_PRIV (idle);
649 g_assert (status_type > 0 && status_type < NUM_TP_CONNECTION_PRESENCE_TYPES);
651 status = presence_type_to_status[status_type];
653 g_return_if_fail (status != NULL);
655 /* We possibly should be sure that the account manager is prepared, but
656 * sometimes this isn't possible, like when exiting. In other words,
657 * we need a callback to empathy_idle_set_presence to be sure the
658 * presence is set on all accounts successfully.
659 * However, in practice, this is fine as we've already prepared the
660 * account manager here in _init. */
661 tp_account_manager_set_all_requested_presences (priv->manager,
662 status_type, status, status_message);
666 empathy_idle_set_presence (EmpathyIdle *idle,
667 TpConnectionPresenceType state,
670 EmpathyIdlePriv *priv;
671 const gchar *default_status;
673 priv = GET_PRIV (idle);
675 DEBUG ("Changing presence to %s (%d)", status, state);
677 g_free (priv->requested_status_message);
678 priv->requested_presence_type = state;
679 priv->requested_status_message = g_strdup (status);
681 /* Do not set translated default messages */
682 default_status = empathy_presence_get_default_message (state);
683 if (!tp_strdiff (status, default_status)) {
687 if (state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE &&
688 !empathy_connectivity_is_online (priv->connectivity)) {
689 DEBUG ("Empathy is not online");
691 priv->saved_state = state;
692 if (tp_strdiff (priv->status, status)) {
693 g_free (priv->saved_status);
694 priv->saved_status = NULL;
695 if (!EMP_STR_EMPTY (status)) {
696 priv->saved_status = g_strdup (status);
702 empathy_idle_do_set_presence (idle, state, status);
706 empathy_idle_get_auto_away (EmpathyIdle *idle)
708 EmpathyIdlePriv *priv = GET_PRIV (idle);
710 return priv->auto_away;
714 empathy_idle_set_auto_away (EmpathyIdle *idle,
717 EmpathyIdlePriv *priv = GET_PRIV (idle);
719 priv->auto_away = auto_away;
721 g_object_notify (G_OBJECT (idle), "auto-away");
724 TpConnectionPresenceType
725 empathy_idle_get_requested_presence (EmpathyIdle *idle,
727 gchar **status_message)
729 EmpathyIdlePriv *priv = GET_PRIV (idle);
731 if (status != NULL) {
732 *status = g_strdup (presence_type_to_status[priv->requested_presence_type]);
735 if (status_message != NULL) {
736 *status_message = g_strdup (priv->requested_status_message);
739 return priv->requested_presence_type;
742 /* This function returns %TRUE if EmpathyIdle considers the account
743 * @account as having just connected recently. Otherwise, it returns
744 * %FALSE. In doubt, %FALSE is returned. */
746 empathy_idle_account_is_just_connected (EmpathyIdle *idle,
749 EmpathyIdlePriv *priv = GET_PRIV (idle);
754 if (tp_account_get_connection_status (account, NULL)
755 != TP_CONNECTION_STATUS_CONNECTED) {
759 ptr = g_hash_table_lookup (priv->connect_times, account);
765 t = GPOINTER_TO_INT (ptr);
767 g_get_current_time (&val);
769 return (val.tv_sec - t) < ACCOUNT_IS_JUST_CONNECTED_SECONDS;