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;
59 TpConnectionPresenceType away_saved_state;
60 TpConnectionPresenceType saved_state;
64 guint ext_away_timeout;
66 TpAccountManager *manager;
67 gulong idle_presence_changed_id;
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
92 G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT);
94 static EmpathyIdle * idle_singleton = NULL;
96 static const gchar *presence_type_to_status[NUM_TP_CONNECTION_PRESENCE_TYPES] = {
109 idle_presence_changed_cb (TpAccountManager *manager,
110 TpConnectionPresenceType state,
112 gchar *status_message,
115 EmpathyIdlePriv *priv;
117 priv = GET_PRIV (idle);
119 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET)
120 /* Assume our presence is offline if MC reports UNSET */
121 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
123 DEBUG ("Presence changed to '%s' (%d) \"%s\"", status, state,
126 g_free (priv->status);
128 if (EMP_STR_EMPTY (status_message))
131 priv->status = g_strdup (status_message);
133 g_object_notify (G_OBJECT (idle), "state");
134 g_object_notify (G_OBJECT (idle), "status");
138 idle_ext_away_cb (EmpathyIdle *idle)
140 EmpathyIdlePriv *priv;
142 priv = GET_PRIV (idle);
144 DEBUG ("Going to extended autoaway");
145 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY);
146 priv->ext_away_timeout = 0;
152 idle_ext_away_stop (EmpathyIdle *idle)
154 EmpathyIdlePriv *priv;
156 priv = GET_PRIV (idle);
158 if (priv->ext_away_timeout) {
159 g_source_remove (priv->ext_away_timeout);
160 priv->ext_away_timeout = 0;
165 idle_ext_away_start (EmpathyIdle *idle)
167 EmpathyIdlePriv *priv;
169 priv = GET_PRIV (idle);
171 if (priv->ext_away_timeout != 0) {
174 priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
175 (GSourceFunc) idle_ext_away_cb,
180 idle_session_status_changed_cb (DBusGProxy *gs_proxy,
181 SessionStatus status,
184 EmpathyIdlePriv *priv;
187 priv = GET_PRIV (idle);
189 is_idle = (status == SESSION_STATUS_IDLE);
191 DEBUG ("Session idle state changed, %s -> %s",
192 priv->is_idle ? "yes" : "no",
193 is_idle ? "yes" : "no");
195 if (!priv->auto_away ||
196 (priv->saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET &&
197 (priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
198 priv->state == TP_CONNECTION_PRESENCE_TYPE_HIDDEN))) {
199 /* We don't want to go auto away OR we explicitely asked to be
200 * offline, nothing to do here */
201 priv->is_idle = is_idle;
205 if (is_idle && !priv->is_idle) {
206 TpConnectionPresenceType new_state;
207 /* We are now idle */
209 idle_ext_away_start (idle);
211 if (priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
212 /* We are disconnected, when coming back from away
213 * we want to restore the presence before the
215 priv->away_saved_state = priv->saved_state;
217 priv->away_saved_state = priv->state;
220 new_state = TP_CONNECTION_PRESENCE_TYPE_AWAY;
221 if (priv->state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
222 new_state = TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY;
225 DEBUG ("Going to autoaway. Saved state=%d, new state=%d",
226 priv->away_saved_state, new_state);
227 empathy_idle_set_state (idle, new_state);
228 } else if (!is_idle && priv->is_idle) {
229 const gchar *new_status;
230 /* We are no more idle, restore state */
232 idle_ext_away_stop (idle);
234 if (priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_AWAY ||
235 priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
236 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
239 new_status = priv->status;
242 /* Only try and set the presence if the away saved state is not
243 * unset. This is an odd case because it means that the session
244 * didn't notify us of the state change to idle, and as a
245 * result, we couldn't save the current state at that time.
247 if (priv->away_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
248 DEBUG ("Restoring state to %d, reset status to %s",
249 priv->away_saved_state, new_status);
251 empathy_idle_set_presence (idle,
252 priv->away_saved_state,
255 DEBUG ("Away saved state is unset. This means that we "
256 "weren't told when the session went idle. "
257 "As a result, I'm not trying to set presence");
260 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
263 priv->is_idle = is_idle;
267 idle_state_change_cb (EmpathyConnectivity *connectivity,
271 EmpathyIdlePriv *priv;
273 priv = GET_PRIV (idle);
276 /* We are no longer connected */
277 DEBUG ("Disconnected: Save state %d (%s)",
278 priv->state, priv->status);
279 priv->saved_state = priv->state;
280 g_free (priv->saved_status);
281 priv->saved_status = g_strdup (priv->status);
282 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
285 && priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
286 /* We are now connected */
287 DEBUG ("Reconnected: Restore state %d (%s)",
288 priv->saved_state, priv->saved_status);
289 empathy_idle_set_presence (idle,
292 priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
293 g_free (priv->saved_status);
294 priv->saved_status = NULL;
299 idle_finalize (GObject *object)
301 EmpathyIdlePriv *priv;
303 priv = GET_PRIV (object);
305 g_free (priv->status);
306 g_free (priv->requested_status_message);
308 if (priv->gs_proxy) {
309 g_object_unref (priv->gs_proxy);
312 g_signal_handler_disconnect (priv->connectivity,
313 priv->state_change_signal_id);
314 priv->state_change_signal_id = 0;
316 if (priv->manager != NULL) {
317 g_signal_handler_disconnect (priv->manager,
318 priv->idle_presence_changed_id);
319 g_object_unref (priv->manager);
322 g_object_unref (priv->connectivity);
324 g_hash_table_destroy (priv->connect_times);
325 priv->connect_times = NULL;
327 idle_ext_away_stop (EMPATHY_IDLE (object));
331 idle_constructor (GType type,
333 GObjectConstructParam *props)
337 if (idle_singleton) {
338 retval = g_object_ref (idle_singleton);
340 retval = G_OBJECT_CLASS (empathy_idle_parent_class)->constructor
341 (type, n_props, props);
343 idle_singleton = EMPATHY_IDLE (retval);
344 g_object_add_weak_pointer (retval, (gpointer) &idle_singleton);
351 empathy_idle_get_status (EmpathyIdle *idle)
353 EmpathyIdlePriv *priv;
355 priv = GET_PRIV (idle);
357 if (G_UNLIKELY (!priv->ready))
358 g_critical (G_STRLOC ": %s called before AccountManager ready",
362 return empathy_presence_get_default_message (priv->state);
369 idle_get_property (GObject *object,
374 EmpathyIdlePriv *priv;
377 priv = GET_PRIV (object);
378 idle = EMPATHY_IDLE (object);
382 g_value_set_enum (value, empathy_idle_get_state (idle));
385 g_value_set_string (value, empathy_idle_get_status (idle));
388 g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
391 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
397 idle_set_property (GObject *object,
402 EmpathyIdlePriv *priv;
405 priv = GET_PRIV (object);
406 idle = EMPATHY_IDLE (object);
410 empathy_idle_set_state (idle, g_value_get_enum (value));
413 empathy_idle_set_status (idle, g_value_get_string (value));
416 empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
419 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
425 empathy_idle_class_init (EmpathyIdleClass *klass)
427 GObjectClass *object_class = G_OBJECT_CLASS (klass);
429 object_class->finalize = idle_finalize;
430 object_class->constructor = idle_constructor;
431 object_class->get_property = idle_get_property;
432 object_class->set_property = idle_set_property;
434 g_object_class_install_property (object_class,
436 g_param_spec_uint ("state",
439 0, NUM_TP_CONNECTION_PRESENCE_TYPES,
440 TP_CONNECTION_PRESENCE_TYPE_UNSET,
442 g_object_class_install_property (object_class,
444 g_param_spec_string ("status",
450 g_object_class_install_property (object_class,
452 g_param_spec_boolean ("auto-away",
453 "Automatic set presence to away",
454 "Should it set presence to away if inactive",
458 g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
462 account_status_changed_cb (TpAccount *account,
466 gchar *dbus_error_name,
470 EmpathyIdle *idle = EMPATHY_IDLE (user_data);
471 EmpathyIdlePriv *priv = GET_PRIV (idle);
474 if (new_status == TP_CONNECTION_STATUS_CONNECTED) {
475 g_get_current_time (&val);
476 g_hash_table_insert (priv->connect_times, account,
477 GINT_TO_POINTER (val.tv_sec));
478 } else if (new_status == TP_CONNECTION_STATUS_DISCONNECTED) {
479 g_hash_table_remove (priv->connect_times, account);
484 account_manager_ready_cb (GObject *source_object,
485 GAsyncResult *result,
488 EmpathyIdle *idle = user_data;
489 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
490 EmpathyIdlePriv *priv;
491 TpConnectionPresenceType state;
492 gchar *status, *status_message;
494 GError *error = NULL;
496 /* In case we've been finalized before reading this callback */
497 if (idle_singleton == NULL)
500 priv = GET_PRIV (idle);
503 if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
504 DEBUG ("Failed to prepare account manager: %s", error->message);
505 g_error_free (error);
509 state = tp_account_manager_get_most_available_presence (priv->manager,
510 &status, &status_message);
512 idle_presence_changed_cb (account_manager, state, status,
513 status_message, idle);
515 accounts = tp_account_manager_get_valid_accounts (priv->manager);
516 for (l = accounts; l != NULL; l = l->next) {
517 empathy_signal_connect_weak (l->data, "status-changed",
518 G_CALLBACK (account_status_changed_cb),
521 g_list_free (accounts);
524 g_free (status_message);
528 empathy_idle_init (EmpathyIdle *idle)
530 EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
531 EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
534 priv->is_idle = FALSE;
536 priv->manager = tp_account_manager_dup ();
538 tp_account_manager_prepare_async (priv->manager, NULL,
539 account_manager_ready_cb, idle);
541 priv->idle_presence_changed_id = g_signal_connect (priv->manager,
542 "most-available-presence-changed",
543 G_CALLBACK (idle_presence_changed_cb), idle);
545 priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
546 "org.gnome.SessionManager",
547 "/org/gnome/SessionManager/Presence",
548 "org.gnome.SessionManager.Presence");
549 if (priv->gs_proxy) {
550 dbus_g_proxy_add_signal (priv->gs_proxy, "StatusChanged",
551 G_TYPE_UINT, G_TYPE_INVALID);
552 dbus_g_proxy_connect_signal (priv->gs_proxy, "StatusChanged",
553 G_CALLBACK (idle_session_status_changed_cb),
556 DEBUG ("Failed to get gs proxy");
559 priv->connectivity = empathy_connectivity_dup_singleton ();
560 priv->state_change_signal_id = g_signal_connect (priv->connectivity,
561 "state-change", G_CALLBACK (idle_state_change_cb), idle);
563 priv->connect_times = g_hash_table_new (g_direct_hash, g_direct_equal);
567 empathy_idle_dup_singleton (void)
569 return g_object_new (EMPATHY_TYPE_IDLE, NULL);
572 TpConnectionPresenceType
573 empathy_idle_get_state (EmpathyIdle *idle)
575 EmpathyIdlePriv *priv;
577 priv = GET_PRIV (idle);
579 if (G_UNLIKELY (!priv->ready))
580 g_critical (G_STRLOC ": %s called before AccountManager ready",
587 empathy_idle_set_state (EmpathyIdle *idle,
588 TpConnectionPresenceType state)
590 EmpathyIdlePriv *priv;
592 priv = GET_PRIV (idle);
594 empathy_idle_set_presence (idle, state, priv->status);
598 empathy_idle_set_status (EmpathyIdle *idle,
601 EmpathyIdlePriv *priv;
603 priv = GET_PRIV (idle);
605 empathy_idle_set_presence (idle, priv->state, status);
609 empathy_idle_do_set_presence (EmpathyIdle *idle,
610 TpConnectionPresenceType status_type,
611 const gchar *status_message)
613 EmpathyIdlePriv *priv = GET_PRIV (idle);
616 g_assert (status_type > 0 && status_type < NUM_TP_CONNECTION_PRESENCE_TYPES);
618 status = presence_type_to_status[status_type];
620 g_return_if_fail (status != NULL);
622 /* We possibly should be sure that the account manager is prepared, but
623 * sometimes this isn't possible, like when exiting. In other words,
624 * we need a callback to empathy_idle_set_presence to be sure the
625 * presence is set on all accounts successfully.
626 * However, in practice, this is fine as we've already prepared the
627 * account manager here in _init. */
628 tp_account_manager_set_all_requested_presences (priv->manager,
629 status_type, status, status_message);
633 empathy_idle_set_presence (EmpathyIdle *idle,
634 TpConnectionPresenceType state,
637 EmpathyIdlePriv *priv;
638 const gchar *default_status;
640 priv = GET_PRIV (idle);
642 DEBUG ("Changing presence to %s (%d)", status, state);
644 g_free (priv->requested_status_message);
645 priv->requested_presence_type = state;
646 priv->requested_status_message = g_strdup (status);
648 /* Do not set translated default messages */
649 default_status = empathy_presence_get_default_message (state);
650 if (!tp_strdiff (status, default_status)) {
654 if (state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE &&
655 !empathy_connectivity_is_online (priv->connectivity)) {
656 DEBUG ("Empathy is not online");
658 priv->saved_state = state;
659 if (tp_strdiff (priv->status, status)) {
660 g_free (priv->saved_status);
661 priv->saved_status = NULL;
662 if (!EMP_STR_EMPTY (status)) {
663 priv->saved_status = g_strdup (status);
669 empathy_idle_do_set_presence (idle, state, status);
673 empathy_idle_get_auto_away (EmpathyIdle *idle)
675 EmpathyIdlePriv *priv = GET_PRIV (idle);
677 return priv->auto_away;
681 empathy_idle_set_auto_away (EmpathyIdle *idle,
684 EmpathyIdlePriv *priv = GET_PRIV (idle);
686 priv->auto_away = auto_away;
688 g_object_notify (G_OBJECT (idle), "auto-away");
691 TpConnectionPresenceType
692 empathy_idle_get_requested_presence (EmpathyIdle *idle,
694 gchar **status_message)
696 EmpathyIdlePriv *priv = GET_PRIV (idle);
698 if (status != NULL) {
699 *status = g_strdup (presence_type_to_status[priv->requested_presence_type]);
702 if (status_message != NULL) {
703 *status_message = g_strdup (priv->requested_status_message);
706 return priv->requested_presence_type;
709 /* This function returns %TRUE if EmpathyIdle considers the account
710 * @account as having just connected recently. Otherwise, it returns
711 * %FALSE. In doubt, %FALSE is returned. */
713 empathy_idle_account_is_just_connected (EmpathyIdle *idle,
716 EmpathyIdlePriv *priv = GET_PRIV (idle);
721 if (tp_account_get_connection_status (account, NULL)
722 != TP_CONNECTION_STATUS_CONNECTED) {
726 ptr = g_hash_table_lookup (priv->connect_times, account);
732 t = GPOINTER_TO_INT (ptr);
734 g_get_current_time (&val);
736 return (val.tv_sec - t) < ACCOUNT_IS_JUST_CONNECTED_SECONDS;