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;
53 TpConnectionPresenceType state;
55 TpConnectionPresenceType flash_state;
58 TpConnectionPresenceType away_saved_state;
59 TpConnectionPresenceType saved_state;
63 guint ext_away_timeout;
65 TpAccountManager *manager;
67 /* pointer to a TpAccount --> glong of time of connection */
68 GHashTable *connect_times;
70 TpConnectionPresenceType requested_presence_type;
71 gchar *requested_status_message;
76 SESSION_STATUS_AVAILABLE,
77 SESSION_STATUS_INVISIBLE,
80 SESSION_STATUS_UNKNOWN
91 G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT);
93 static EmpathyIdle * idle_singleton = NULL;
95 static const gchar *presence_type_to_status[NUM_TP_CONNECTION_PRESENCE_TYPES] = {
108 idle_presence_changed_cb (TpAccountManager *manager,
109 TpConnectionPresenceType state,
111 gchar *status_message,
114 EmpathyIdlePriv *priv;
116 priv = GET_PRIV (idle);
118 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET)
119 /* Assume our presence is offline if MC reports UNSET */
120 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
122 DEBUG ("Presence changed to '%s' (%d) \"%s\"", status, state,
125 g_free (priv->status);
127 if (EMP_STR_EMPTY (status_message))
130 priv->status = g_strdup (status_message);
132 g_object_notify (G_OBJECT (idle), "state");
133 g_object_notify (G_OBJECT (idle), "status");
137 idle_ext_away_cb (EmpathyIdle *idle)
139 EmpathyIdlePriv *priv;
141 priv = GET_PRIV (idle);
143 DEBUG ("Going to extended autoaway");
144 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY);
145 priv->ext_away_timeout = 0;
151 idle_ext_away_stop (EmpathyIdle *idle)
153 EmpathyIdlePriv *priv;
155 priv = GET_PRIV (idle);
157 if (priv->ext_away_timeout) {
158 g_source_remove (priv->ext_away_timeout);
159 priv->ext_away_timeout = 0;
164 idle_ext_away_start (EmpathyIdle *idle)
166 EmpathyIdlePriv *priv;
168 priv = GET_PRIV (idle);
170 if (priv->ext_away_timeout != 0) {
173 priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
174 (GSourceFunc) idle_ext_away_cb,
179 idle_session_status_changed_cb (DBusGProxy *gs_proxy,
180 SessionStatus status,
183 EmpathyIdlePriv *priv;
186 priv = GET_PRIV (idle);
188 is_idle = (status == SESSION_STATUS_IDLE);
190 DEBUG ("Session idle state changed, %s -> %s",
191 priv->is_idle ? "yes" : "no",
192 is_idle ? "yes" : "no");
194 if (!priv->auto_away ||
195 (priv->saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET &&
196 (priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
197 priv->state == TP_CONNECTION_PRESENCE_TYPE_HIDDEN))) {
198 /* We don't want to go auto away OR we explicitely asked to be
199 * offline, nothing to do here */
200 priv->is_idle = is_idle;
204 if (is_idle && !priv->is_idle) {
205 TpConnectionPresenceType new_state;
206 /* We are now idle */
208 idle_ext_away_start (idle);
210 if (priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
211 /* We are disconnected, when coming back from away
212 * we want to restore the presence before the
214 priv->away_saved_state = priv->saved_state;
216 priv->away_saved_state = priv->state;
219 new_state = TP_CONNECTION_PRESENCE_TYPE_AWAY;
220 if (priv->state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
221 new_state = TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY;
224 DEBUG ("Going to autoaway. Saved state=%d, new state=%d",
225 priv->away_saved_state, new_state);
226 empathy_idle_set_state (idle, new_state);
227 } else if (!is_idle && priv->is_idle) {
228 const gchar *new_status;
229 /* We are no more idle, restore state */
231 idle_ext_away_stop (idle);
233 if (priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_AWAY ||
234 priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
235 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
238 new_status = priv->status;
241 /* Only try and set the presence if the away saved state is not
242 * unset. This is an odd case because it means that the session
243 * didn't notify us of the state change to idle, and as a
244 * result, we couldn't save the current state at that time.
246 if (priv->away_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
247 DEBUG ("Restoring state to %d, reset status to %s",
248 priv->away_saved_state, new_status);
250 empathy_idle_set_presence (idle,
251 priv->away_saved_state,
254 DEBUG ("Away saved state is unset. This means that we "
255 "weren't told when the session went idle. "
256 "As a result, I'm not trying to set presence");
259 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
262 priv->is_idle = is_idle;
266 idle_state_change_cb (EmpathyConnectivity *connectivity,
270 EmpathyIdlePriv *priv;
272 priv = GET_PRIV (idle);
275 /* We are no longer connected */
276 DEBUG ("Disconnected: Save state %d (%s)",
277 priv->state, priv->status);
278 priv->saved_state = priv->state;
279 g_free (priv->saved_status);
280 priv->saved_status = g_strdup (priv->status);
281 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
284 && priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
285 /* We are now connected */
286 DEBUG ("Reconnected: Restore state %d (%s)",
287 priv->saved_state, priv->saved_status);
288 empathy_idle_set_presence (idle,
291 priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
292 g_free (priv->saved_status);
293 priv->saved_status = NULL;
298 idle_finalize (GObject *object)
300 EmpathyIdlePriv *priv;
302 priv = GET_PRIV (object);
304 g_free (priv->status);
305 g_free (priv->requested_status_message);
307 if (priv->gs_proxy) {
308 g_object_unref (priv->gs_proxy);
311 g_signal_handler_disconnect (priv->connectivity,
312 priv->state_change_signal_id);
313 priv->state_change_signal_id = 0;
315 g_object_unref (priv->connectivity);
317 g_hash_table_destroy (priv->connect_times);
318 priv->connect_times = NULL;
320 idle_ext_away_stop (EMPATHY_IDLE (object));
324 idle_constructor (GType type,
326 GObjectConstructParam *props)
330 if (idle_singleton) {
331 retval = g_object_ref (idle_singleton);
333 retval = G_OBJECT_CLASS (empathy_idle_parent_class)->constructor
334 (type, n_props, props);
336 idle_singleton = EMPATHY_IDLE (retval);
337 g_object_add_weak_pointer (retval, (gpointer) &idle_singleton);
344 idle_get_property (GObject *object,
349 EmpathyIdlePriv *priv;
352 priv = GET_PRIV (object);
353 idle = EMPATHY_IDLE (object);
357 g_value_set_enum (value, empathy_idle_get_state (idle));
360 g_value_set_string (value, empathy_idle_get_status (idle));
362 case PROP_FLASH_STATE:
363 g_value_set_enum (value, empathy_idle_get_flash_state (idle));
366 g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
369 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
375 idle_set_property (GObject *object,
380 EmpathyIdlePriv *priv;
383 priv = GET_PRIV (object);
384 idle = EMPATHY_IDLE (object);
388 empathy_idle_set_state (idle, g_value_get_enum (value));
391 empathy_idle_set_status (idle, g_value_get_string (value));
393 case PROP_FLASH_STATE:
394 empathy_idle_set_flash_state (idle, g_value_get_enum (value));
397 empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
400 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
406 empathy_idle_class_init (EmpathyIdleClass *klass)
408 GObjectClass *object_class = G_OBJECT_CLASS (klass);
410 object_class->finalize = idle_finalize;
411 object_class->constructor = idle_constructor;
412 object_class->get_property = idle_get_property;
413 object_class->set_property = idle_set_property;
415 g_object_class_install_property (object_class,
417 g_param_spec_uint ("state",
420 0, NUM_TP_CONNECTION_PRESENCE_TYPES,
421 TP_CONNECTION_PRESENCE_TYPE_UNSET,
423 g_object_class_install_property (object_class,
425 g_param_spec_string ("status",
430 g_object_class_install_property (object_class,
432 g_param_spec_uint ("flash-state",
435 0, NUM_TP_CONNECTION_PRESENCE_TYPES,
436 TP_CONNECTION_PRESENCE_TYPE_UNSET,
439 g_object_class_install_property (object_class,
441 g_param_spec_boolean ("auto-away",
442 "Automatic set presence to away",
443 "Should it set presence to away if inactive",
447 g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
451 account_status_changed_cb (TpAccount *account,
455 gchar *dbus_error_name,
459 EmpathyIdle *idle = EMPATHY_IDLE (user_data);
460 EmpathyIdlePriv *priv = GET_PRIV (idle);
463 if (new_status == TP_CONNECTION_STATUS_CONNECTED) {
464 g_get_current_time (&val);
465 g_hash_table_insert (priv->connect_times, account,
466 GINT_TO_POINTER (val.tv_sec));
467 } else if (new_status == TP_CONNECTION_STATUS_DISCONNECTED) {
468 g_hash_table_remove (priv->connect_times, account);
473 account_manager_ready_cb (GObject *source_object,
474 GAsyncResult *result,
477 EmpathyIdle *idle = EMPATHY_IDLE (user_data);
478 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
479 EmpathyIdlePriv *priv = GET_PRIV (idle);
480 TpConnectionPresenceType state;
481 gchar *status, *status_message;
483 GError *error = NULL;
485 if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
486 DEBUG ("Failed to prepare account manager: %s", error->message);
487 g_error_free (error);
491 state = tp_account_manager_get_most_available_presence (priv->manager,
492 &status, &status_message);
494 idle_presence_changed_cb (account_manager, state, status,
495 status_message, idle);
497 accounts = tp_account_manager_get_valid_accounts (priv->manager);
498 for (l = accounts; l != NULL; l = l->next) {
499 empathy_signal_connect_weak (l->data, "status-changed",
500 G_CALLBACK (account_status_changed_cb),
503 g_list_free (accounts);
506 g_free (status_message);
510 empathy_idle_init (EmpathyIdle *idle)
512 EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
513 EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
516 priv->is_idle = FALSE;
518 priv->manager = tp_account_manager_dup ();
520 tp_account_manager_prepare_async (priv->manager, NULL,
521 account_manager_ready_cb, idle);
523 g_signal_connect (priv->manager, "most-available-presence-changed",
524 G_CALLBACK (idle_presence_changed_cb), idle);
526 priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
527 "org.gnome.SessionManager",
528 "/org/gnome/SessionManager/Presence",
529 "org.gnome.SessionManager.Presence");
530 if (priv->gs_proxy) {
531 dbus_g_proxy_add_signal (priv->gs_proxy, "StatusChanged",
532 G_TYPE_UINT, G_TYPE_INVALID);
533 dbus_g_proxy_connect_signal (priv->gs_proxy, "StatusChanged",
534 G_CALLBACK (idle_session_status_changed_cb),
537 DEBUG ("Failed to get gs proxy");
540 priv->connectivity = empathy_connectivity_dup_singleton ();
541 priv->state_change_signal_id = g_signal_connect (priv->connectivity,
542 "state-change", G_CALLBACK (idle_state_change_cb), idle);
544 priv->connect_times = g_hash_table_new (g_direct_hash, g_direct_equal);
548 empathy_idle_dup_singleton (void)
550 return g_object_new (EMPATHY_TYPE_IDLE, NULL);
553 TpConnectionPresenceType
554 empathy_idle_get_state (EmpathyIdle *idle)
556 EmpathyIdlePriv *priv;
558 priv = GET_PRIV (idle);
564 empathy_idle_set_state (EmpathyIdle *idle,
565 TpConnectionPresenceType state)
567 EmpathyIdlePriv *priv;
569 priv = GET_PRIV (idle);
571 empathy_idle_set_presence (idle, state, priv->status);
575 empathy_idle_get_status (EmpathyIdle *idle)
577 EmpathyIdlePriv *priv;
579 priv = GET_PRIV (idle);
582 return empathy_presence_get_default_message (priv->state);
589 empathy_idle_set_status (EmpathyIdle *idle,
592 EmpathyIdlePriv *priv;
594 priv = GET_PRIV (idle);
596 empathy_idle_set_presence (idle, priv->state, status);
599 TpConnectionPresenceType
600 empathy_idle_get_flash_state (EmpathyIdle *idle)
602 EmpathyIdlePriv *priv;
604 priv = GET_PRIV (idle);
606 return priv->flash_state;
610 empathy_idle_set_flash_state (EmpathyIdle *idle,
611 TpConnectionPresenceType state)
613 EmpathyIdlePriv *priv;
615 priv = GET_PRIV (idle);
617 priv->flash_state = state;
619 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
622 g_object_notify (G_OBJECT (idle), "flash-state");
626 empathy_idle_do_set_presence (EmpathyIdle *idle,
627 TpConnectionPresenceType status_type,
628 const gchar *status_message)
630 EmpathyIdlePriv *priv = GET_PRIV (idle);
633 g_assert (status_type > 0 && status_type < NUM_TP_CONNECTION_PRESENCE_TYPES);
635 status = presence_type_to_status[status_type];
637 g_return_if_fail (status != NULL);
639 /* We possibly should be sure that the account manager is prepared, but
640 * sometimes this isn't possible, like when exiting. In other words,
641 * we need a callback to empathy_idle_set_presence to be sure the
642 * presence is set on all accounts successfully.
643 * However, in practice, this is fine as we've already prepared the
644 * account manager here in _init. */
645 tp_account_manager_set_all_requested_presences (priv->manager,
646 status_type, status, status_message);
650 empathy_idle_set_presence (EmpathyIdle *idle,
651 TpConnectionPresenceType state,
654 EmpathyIdlePriv *priv;
655 const gchar *default_status;
657 priv = GET_PRIV (idle);
659 DEBUG ("Changing presence to %s (%d)", status, state);
661 g_free (priv->requested_status_message);
662 priv->requested_presence_type = state;
663 priv->requested_status_message = g_strdup (status);
665 /* Do not set translated default messages */
666 default_status = empathy_presence_get_default_message (state);
667 if (!tp_strdiff (status, default_status)) {
671 if (state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE &&
672 !empathy_connectivity_is_online (priv->connectivity)) {
673 DEBUG ("Empathy is not online");
675 priv->saved_state = state;
676 if (tp_strdiff (priv->status, status)) {
677 g_free (priv->saved_status);
678 priv->saved_status = NULL;
679 if (!EMP_STR_EMPTY (status)) {
680 priv->saved_status = g_strdup (status);
686 empathy_idle_do_set_presence (idle, state, status);
690 empathy_idle_get_auto_away (EmpathyIdle *idle)
692 EmpathyIdlePriv *priv = GET_PRIV (idle);
694 return priv->auto_away;
698 empathy_idle_set_auto_away (EmpathyIdle *idle,
701 EmpathyIdlePriv *priv = GET_PRIV (idle);
703 priv->auto_away = auto_away;
705 g_object_notify (G_OBJECT (idle), "auto-away");
708 TpConnectionPresenceType
709 empathy_idle_get_requested_presence (EmpathyIdle *idle,
711 gchar **status_message)
713 EmpathyIdlePriv *priv = GET_PRIV (idle);
715 if (status != NULL) {
716 *status = g_strdup (presence_type_to_status[priv->requested_presence_type]);
719 if (status_message != NULL) {
720 *status_message = g_strdup (priv->requested_status_message);
723 return priv->requested_presence_type;
726 /* This function returns %TRUE if EmpathyIdle considers the account
727 * @account as having just connected recently. Otherwise, it returns
728 * %FALSE. In doubt, %FALSE is returned. */
730 empathy_idle_account_is_just_connected (EmpathyIdle *idle,
733 EmpathyIdlePriv *priv = GET_PRIV (idle);
738 if (tp_account_get_connection_status (account, NULL)
739 != TP_CONNECTION_STATUS_CONNECTED) {
743 ptr = g_hash_table_lookup (priv->connect_times, account);
749 t = GPOINTER_TO_INT (ptr);
751 g_get_current_time (&val);
753 return (val.tv_sec - t) < ACCOUNT_IS_JUST_CONNECTED_SECONDS;