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 #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 TpAccountManager *manager;
62 GHashTable *connect_times;
64 TpConnectionPresenceType requested_presence_type;
65 gchar *requested_status_message;
70 SESSION_STATUS_AVAILABLE,
71 SESSION_STATUS_INVISIBLE,
74 SESSION_STATUS_UNKNOWN
85 G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT);
87 static EmpathyIdle * idle_singleton = NULL;
89 static const gchar *presence_type_to_status[NUM_TP_CONNECTION_PRESENCE_TYPES] = {
102 idle_presence_changed_cb (TpAccountManager *manager,
103 TpConnectionPresenceType state,
105 gchar *status_message,
108 EmpathyIdlePriv *priv;
110 priv = GET_PRIV (idle);
112 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET)
113 /* Assume our presence is offline if MC reports UNSET */
114 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
116 DEBUG ("Presence changed to '%s' (%d) \"%s\"", status, state,
119 g_free (priv->status);
121 if (EMP_STR_EMPTY (status_message))
124 priv->status = g_strdup (status_message);
126 g_object_notify (G_OBJECT (idle), "state");
127 g_object_notify (G_OBJECT (idle), "status");
131 idle_ext_away_cb (EmpathyIdle *idle)
133 EmpathyIdlePriv *priv;
135 priv = GET_PRIV (idle);
137 DEBUG ("Going to extended autoaway");
138 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY);
139 priv->ext_away_timeout = 0;
145 idle_ext_away_stop (EmpathyIdle *idle)
147 EmpathyIdlePriv *priv;
149 priv = GET_PRIV (idle);
151 if (priv->ext_away_timeout) {
152 g_source_remove (priv->ext_away_timeout);
153 priv->ext_away_timeout = 0;
158 idle_ext_away_start (EmpathyIdle *idle)
160 EmpathyIdlePriv *priv;
162 priv = GET_PRIV (idle);
164 if (priv->ext_away_timeout != 0) {
167 priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
168 (GSourceFunc) idle_ext_away_cb,
173 idle_session_status_changed_cb (DBusGProxy *gs_proxy,
174 SessionStatus status,
177 EmpathyIdlePriv *priv;
180 priv = GET_PRIV (idle);
182 is_idle = (status == SESSION_STATUS_IDLE);
184 DEBUG ("Session idle state changed, %s -> %s",
185 priv->is_idle ? "yes" : "no",
186 is_idle ? "yes" : "no");
188 if (!priv->auto_away ||
189 (priv->saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET &&
190 (priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
191 priv->state == TP_CONNECTION_PRESENCE_TYPE_HIDDEN))) {
192 /* We don't want to go auto away OR we explicitely asked to be
193 * offline, nothing to do here */
194 priv->is_idle = is_idle;
198 if (is_idle && !priv->is_idle) {
199 TpConnectionPresenceType new_state;
200 /* We are now idle */
202 idle_ext_away_start (idle);
204 if (priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
205 /* We are disconnected, when coming back from away
206 * we want to restore the presence before the
208 priv->away_saved_state = priv->saved_state;
210 priv->away_saved_state = priv->state;
213 new_state = TP_CONNECTION_PRESENCE_TYPE_AWAY;
214 if (priv->state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
215 new_state = TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY;
218 DEBUG ("Going to autoaway. Saved state=%d, new state=%d",
219 priv->away_saved_state, new_state);
220 empathy_idle_set_state (idle, new_state);
221 } else if (!is_idle && priv->is_idle) {
222 const gchar *new_status;
223 /* We are no more idle, restore state */
225 idle_ext_away_stop (idle);
227 if (priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_AWAY ||
228 priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
229 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
232 new_status = priv->status;
235 /* Only try and set the presence if the away saved state is not
236 * unset. This is an odd case because it means that the session
237 * didn't notify us of the state change to idle, and as a
238 * result, we couldn't save the current state at that time.
240 if (priv->away_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
241 DEBUG ("Restoring state to %d, reset status to %s",
242 priv->away_saved_state, new_status);
244 empathy_idle_set_presence (idle,
245 priv->away_saved_state,
248 DEBUG ("Away saved state is unset. This means that we "
249 "weren't told when the session went idle. "
250 "As a result, I'm not trying to set presence");
253 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
256 priv->is_idle = is_idle;
260 idle_state_change_cb (EmpathyConnectivity *connectivity,
264 EmpathyIdlePriv *priv;
266 priv = GET_PRIV (idle);
269 /* We are no longer connected */
270 DEBUG ("Disconnected: Save state %d (%s)",
271 priv->state, priv->status);
272 priv->saved_state = priv->state;
273 g_free (priv->saved_status);
274 priv->saved_status = g_strdup (priv->status);
275 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
278 && priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
279 /* We are now connected */
280 DEBUG ("Reconnected: Restore state %d (%s)",
281 priv->saved_state, priv->saved_status);
282 empathy_idle_set_presence (idle,
285 priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
286 g_free (priv->saved_status);
287 priv->saved_status = NULL;
292 idle_finalize (GObject *object)
294 EmpathyIdlePriv *priv;
296 priv = GET_PRIV (object);
298 g_free (priv->status);
299 g_free (priv->requested_status_message);
301 if (priv->gs_proxy) {
302 g_object_unref (priv->gs_proxy);
305 g_signal_handler_disconnect (priv->connectivity,
306 priv->state_change_signal_id);
307 priv->state_change_signal_id = 0;
309 g_object_unref (priv->connectivity);
311 g_hash_table_destroy (priv->connect_times);
312 priv->connect_times = NULL;
314 idle_ext_away_stop (EMPATHY_IDLE (object));
318 idle_constructor (GType type,
320 GObjectConstructParam *props)
324 if (idle_singleton) {
325 retval = g_object_ref (idle_singleton);
327 retval = G_OBJECT_CLASS (empathy_idle_parent_class)->constructor
328 (type, n_props, props);
330 idle_singleton = EMPATHY_IDLE (retval);
331 g_object_add_weak_pointer (retval, (gpointer) &idle_singleton);
338 idle_get_property (GObject *object,
343 EmpathyIdlePriv *priv;
346 priv = GET_PRIV (object);
347 idle = EMPATHY_IDLE (object);
351 g_value_set_enum (value, empathy_idle_get_state (idle));
354 g_value_set_string (value, empathy_idle_get_status (idle));
356 case PROP_FLASH_STATE:
357 g_value_set_enum (value, empathy_idle_get_flash_state (idle));
360 g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
363 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
369 idle_set_property (GObject *object,
374 EmpathyIdlePriv *priv;
377 priv = GET_PRIV (object);
378 idle = EMPATHY_IDLE (object);
382 empathy_idle_set_state (idle, g_value_get_enum (value));
385 empathy_idle_set_status (idle, g_value_get_string (value));
387 case PROP_FLASH_STATE:
388 empathy_idle_set_flash_state (idle, g_value_get_enum (value));
391 empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
394 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
400 empathy_idle_class_init (EmpathyIdleClass *klass)
402 GObjectClass *object_class = G_OBJECT_CLASS (klass);
404 object_class->finalize = idle_finalize;
405 object_class->constructor = idle_constructor;
406 object_class->get_property = idle_get_property;
407 object_class->set_property = idle_set_property;
409 g_object_class_install_property (object_class,
411 g_param_spec_uint ("state",
414 0, NUM_TP_CONNECTION_PRESENCE_TYPES,
415 TP_CONNECTION_PRESENCE_TYPE_UNSET,
417 g_object_class_install_property (object_class,
419 g_param_spec_string ("status",
424 g_object_class_install_property (object_class,
426 g_param_spec_uint ("flash-state",
429 0, NUM_TP_CONNECTION_PRESENCE_TYPES,
430 TP_CONNECTION_PRESENCE_TYPE_UNSET,
433 g_object_class_install_property (object_class,
435 g_param_spec_boolean ("auto-away",
436 "Automatic set presence to away",
437 "Should it set presence to away if inactive",
441 g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
445 account_status_changed_cb (TpAccount *account,
449 gchar *dbus_error_name,
453 EmpathyIdle *idle = EMPATHY_IDLE (user_data);
454 EmpathyIdlePriv *priv = GET_PRIV (idle);
457 if (new_status == TP_CONNECTION_STATUS_CONNECTED) {
458 g_get_current_time (&val);
459 g_hash_table_insert (priv->connect_times, account,
460 GINT_TO_POINTER (val.tv_sec));
461 } else if (new_status == TP_CONNECTION_STATUS_DISCONNECTED) {
462 g_hash_table_remove (priv->connect_times, account);
467 account_manager_ready_cb (GObject *source_object,
468 GAsyncResult *result,
471 EmpathyIdle *idle = EMPATHY_IDLE (user_data);
472 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
473 EmpathyIdlePriv *priv = GET_PRIV (idle);
474 TpConnectionPresenceType state;
475 gchar *status, *status_message;
477 GError *error = NULL;
479 if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
480 DEBUG ("Failed to prepare account manager: %s", error->message);
481 g_error_free (error);
485 state = tp_account_manager_get_most_available_presence (priv->manager,
486 &status, &status_message);
488 idle_presence_changed_cb (account_manager, state, status,
489 status_message, idle);
491 accounts = tp_account_manager_get_valid_accounts (priv->manager);
492 for (l = accounts; l != NULL; l = l->next) {
493 empathy_signal_connect_weak (l->data, "status-changed",
494 G_CALLBACK (account_status_changed_cb),
497 g_list_free (accounts);
500 g_free (status_message);
504 empathy_idle_init (EmpathyIdle *idle)
506 EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
507 EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
510 priv->is_idle = FALSE;
512 priv->manager = tp_account_manager_dup ();
514 tp_account_manager_prepare_async (priv->manager, NULL,
515 account_manager_ready_cb, idle);
517 g_signal_connect (priv->manager, "most-available-presence-changed",
518 G_CALLBACK (idle_presence_changed_cb), idle);
520 priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
521 "org.gnome.SessionManager",
522 "/org/gnome/SessionManager/Presence",
523 "org.gnome.SessionManager.Presence");
524 if (priv->gs_proxy) {
525 dbus_g_proxy_add_signal (priv->gs_proxy, "StatusChanged",
526 G_TYPE_UINT, G_TYPE_INVALID);
527 dbus_g_proxy_connect_signal (priv->gs_proxy, "StatusChanged",
528 G_CALLBACK (idle_session_status_changed_cb),
531 DEBUG ("Failed to get gs proxy");
534 priv->connectivity = empathy_connectivity_dup_singleton ();
535 priv->state_change_signal_id = g_signal_connect (priv->connectivity,
536 "state-change", G_CALLBACK (idle_state_change_cb), idle);
538 priv->connect_times = g_hash_table_new (g_direct_hash, g_direct_equal);
542 empathy_idle_dup_singleton (void)
544 return g_object_new (EMPATHY_TYPE_IDLE, NULL);
547 TpConnectionPresenceType
548 empathy_idle_get_state (EmpathyIdle *idle)
550 EmpathyIdlePriv *priv;
552 priv = GET_PRIV (idle);
558 empathy_idle_set_state (EmpathyIdle *idle,
559 TpConnectionPresenceType state)
561 EmpathyIdlePriv *priv;
563 priv = GET_PRIV (idle);
565 empathy_idle_set_presence (idle, state, priv->status);
569 empathy_idle_get_status (EmpathyIdle *idle)
571 EmpathyIdlePriv *priv;
573 priv = GET_PRIV (idle);
576 return empathy_presence_get_default_message (priv->state);
583 empathy_idle_set_status (EmpathyIdle *idle,
586 EmpathyIdlePriv *priv;
588 priv = GET_PRIV (idle);
590 empathy_idle_set_presence (idle, priv->state, status);
593 TpConnectionPresenceType
594 empathy_idle_get_flash_state (EmpathyIdle *idle)
596 EmpathyIdlePriv *priv;
598 priv = GET_PRIV (idle);
600 return priv->flash_state;
604 empathy_idle_set_flash_state (EmpathyIdle *idle,
605 TpConnectionPresenceType state)
607 EmpathyIdlePriv *priv;
609 priv = GET_PRIV (idle);
611 priv->flash_state = state;
613 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
616 g_object_notify (G_OBJECT (idle), "flash-state");
620 empathy_idle_do_set_presence (EmpathyIdle *idle,
621 TpConnectionPresenceType status_type,
622 const gchar *status_message)
624 EmpathyIdlePriv *priv = GET_PRIV (idle);
627 g_assert (status_type > 0 && status_type < NUM_TP_CONNECTION_PRESENCE_TYPES);
629 status = presence_type_to_status[status_type];
631 g_return_if_fail (status != NULL);
633 /* FIXME: Should be sure that the account manager is prepared, but
634 * sometimes this isn't possible, like when exiting. In other words,
635 * we need a callback to empathy_idle_set_presence to be sure the
636 * presence is set on all accounts successfully. */
637 tp_account_manager_set_all_requested_presences (priv->manager,
638 status_type, status, status_message);
642 empathy_idle_set_presence (EmpathyIdle *idle,
643 TpConnectionPresenceType state,
646 EmpathyIdlePriv *priv;
647 const gchar *default_status;
649 priv = GET_PRIV (idle);
651 DEBUG ("Changing presence to %s (%d)", status, state);
653 g_free (priv->requested_status_message);
654 priv->requested_presence_type = state;
655 priv->requested_status_message = g_strdup (status);
657 /* Do not set translated default messages */
658 default_status = empathy_presence_get_default_message (state);
659 if (!tp_strdiff (status, default_status)) {
663 if (state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE &&
664 !empathy_connectivity_is_online (priv->connectivity)) {
665 DEBUG ("Empathy is not online");
667 priv->saved_state = state;
668 if (tp_strdiff (priv->status, status)) {
669 g_free (priv->saved_status);
670 priv->saved_status = NULL;
671 if (!EMP_STR_EMPTY (status)) {
672 priv->saved_status = g_strdup (status);
678 empathy_idle_do_set_presence (idle, state, status);
682 empathy_idle_get_auto_away (EmpathyIdle *idle)
684 EmpathyIdlePriv *priv = GET_PRIV (idle);
686 return priv->auto_away;
690 empathy_idle_set_auto_away (EmpathyIdle *idle,
693 EmpathyIdlePriv *priv = GET_PRIV (idle);
695 priv->auto_away = auto_away;
697 g_object_notify (G_OBJECT (idle), "auto-away");
700 TpConnectionPresenceType
701 empathy_idle_get_requested_presence (EmpathyIdle *idle,
703 gchar **status_message)
705 EmpathyIdlePriv *priv = GET_PRIV (idle);
707 if (status != NULL) {
708 *status = g_strdup (presence_type_to_status[priv->requested_presence_type]);
711 if (status_message != NULL) {
712 *status_message = g_strdup (priv->requested_status_message);
715 return priv->requested_presence_type;
719 empathy_idle_account_is_just_connected (EmpathyIdle *idle,
722 EmpathyIdlePriv *priv = GET_PRIV (idle);
727 if (tp_account_get_connection_status (account, NULL)
728 != TP_CONNECTION_STATUS_CONNECTED) {
732 ptr = g_hash_table_lookup (priv->connect_times, account);
738 t = GPOINTER_TO_INT (ptr);
740 g_get_current_time (&val);
742 return (val.tv_sec - t) < 10;