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;
478 if (!tp_account_manager_prepare_finish (account_manager, result, NULL)) {
482 state = tp_account_manager_get_most_available_presence (priv->manager,
483 &status, &status_message);
485 idle_presence_changed_cb (account_manager, state, status,
486 status_message, idle);
488 accounts = tp_account_manager_get_valid_accounts (priv->manager);
489 for (l = accounts; l != NULL; l = l->next) {
490 empathy_signal_connect_weak (l->data, "status-changed",
491 G_CALLBACK (account_status_changed_cb),
494 g_list_free (accounts);
497 g_free (status_message);
501 empathy_idle_init (EmpathyIdle *idle)
503 EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
504 EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
507 priv->is_idle = FALSE;
509 priv->manager = tp_account_manager_dup ();
511 tp_account_manager_prepare_async (priv->manager, NULL,
512 account_manager_ready_cb, idle);
514 g_signal_connect (priv->manager, "most-available-presence-changed",
515 G_CALLBACK (idle_presence_changed_cb), idle);
517 priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
518 "org.gnome.SessionManager",
519 "/org/gnome/SessionManager/Presence",
520 "org.gnome.SessionManager.Presence");
521 if (priv->gs_proxy) {
522 dbus_g_proxy_add_signal (priv->gs_proxy, "StatusChanged",
523 G_TYPE_UINT, G_TYPE_INVALID);
524 dbus_g_proxy_connect_signal (priv->gs_proxy, "StatusChanged",
525 G_CALLBACK (idle_session_status_changed_cb),
528 DEBUG ("Failed to get gs proxy");
531 priv->connectivity = empathy_connectivity_dup_singleton ();
532 priv->state_change_signal_id = g_signal_connect (priv->connectivity,
533 "state-change", G_CALLBACK (idle_state_change_cb), idle);
535 priv->connect_times = g_hash_table_new (g_direct_hash, g_direct_equal);
539 empathy_idle_dup_singleton (void)
541 return g_object_new (EMPATHY_TYPE_IDLE, NULL);
544 TpConnectionPresenceType
545 empathy_idle_get_state (EmpathyIdle *idle)
547 EmpathyIdlePriv *priv;
549 priv = GET_PRIV (idle);
555 empathy_idle_set_state (EmpathyIdle *idle,
556 TpConnectionPresenceType state)
558 EmpathyIdlePriv *priv;
560 priv = GET_PRIV (idle);
562 empathy_idle_set_presence (idle, state, priv->status);
566 empathy_idle_get_status (EmpathyIdle *idle)
568 EmpathyIdlePriv *priv;
570 priv = GET_PRIV (idle);
573 return empathy_presence_get_default_message (priv->state);
580 empathy_idle_set_status (EmpathyIdle *idle,
583 EmpathyIdlePriv *priv;
585 priv = GET_PRIV (idle);
587 empathy_idle_set_presence (idle, priv->state, status);
590 TpConnectionPresenceType
591 empathy_idle_get_flash_state (EmpathyIdle *idle)
593 EmpathyIdlePriv *priv;
595 priv = GET_PRIV (idle);
597 return priv->flash_state;
601 empathy_idle_set_flash_state (EmpathyIdle *idle,
602 TpConnectionPresenceType state)
604 EmpathyIdlePriv *priv;
606 priv = GET_PRIV (idle);
608 priv->flash_state = state;
610 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
613 g_object_notify (G_OBJECT (idle), "flash-state");
617 empathy_idle_do_set_presence (EmpathyIdle *idle,
618 TpConnectionPresenceType status_type,
619 const gchar *status_message)
621 EmpathyIdlePriv *priv = GET_PRIV (idle);
624 g_assert (status_type > 0 && status_type < NUM_TP_CONNECTION_PRESENCE_TYPES);
626 status = presence_type_to_status[status_type];
628 g_return_if_fail (status != NULL);
630 /* FIXME: Should be sure that the account manager is prepared, but
631 * sometimes this isn't possible, like when exiting. In other words,
632 * we need a callback to empathy_idle_set_presence to be sure the
633 * presence is set on all accounts successfully. */
634 tp_account_manager_set_all_requested_presences (priv->manager,
635 status_type, status, status_message);
639 empathy_idle_set_presence (EmpathyIdle *idle,
640 TpConnectionPresenceType state,
643 EmpathyIdlePriv *priv;
644 const gchar *default_status;
646 priv = GET_PRIV (idle);
648 DEBUG ("Changing presence to %s (%d)", status, state);
650 g_free (priv->requested_status_message);
651 priv->requested_presence_type = state;
652 priv->requested_status_message = g_strdup (status);
654 /* Do not set translated default messages */
655 default_status = empathy_presence_get_default_message (state);
656 if (!tp_strdiff (status, default_status)) {
660 if (state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE &&
661 !empathy_connectivity_is_online (priv->connectivity)) {
662 DEBUG ("Empathy is not online");
664 priv->saved_state = state;
665 if (tp_strdiff (priv->status, status)) {
666 g_free (priv->saved_status);
667 priv->saved_status = NULL;
668 if (!EMP_STR_EMPTY (status)) {
669 priv->saved_status = g_strdup (status);
675 empathy_idle_do_set_presence (idle, state, status);
679 empathy_idle_get_auto_away (EmpathyIdle *idle)
681 EmpathyIdlePriv *priv = GET_PRIV (idle);
683 return priv->auto_away;
687 empathy_idle_set_auto_away (EmpathyIdle *idle,
690 EmpathyIdlePriv *priv = GET_PRIV (idle);
692 priv->auto_away = auto_away;
694 g_object_notify (G_OBJECT (idle), "auto-away");
697 TpConnectionPresenceType
698 empathy_idle_get_requested_presence (EmpathyIdle *idle,
700 gchar **status_message)
702 EmpathyIdlePriv *priv = GET_PRIV (idle);
704 if (status != NULL) {
705 *status = g_strdup (presence_type_to_status[priv->requested_presence_type]);
708 if (status_message != NULL) {
709 *status_message = g_strdup (priv->requested_status_message);
712 return priv->requested_presence_type;
716 empathy_idle_account_is_just_connected (EmpathyIdle *idle,
719 EmpathyIdlePriv *priv = GET_PRIV (idle);
724 if (tp_account_get_connection_status (account, NULL)
725 != TP_CONNECTION_STATUS_CONNECTED) {
729 ptr = g_hash_table_lookup (priv->connect_times, account);
735 t = GPOINTER_TO_INT (ptr);
737 g_get_current_time (&val);
739 return (val.tv_sec - t) < 10;