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 /* We are no more idle, restore state */
231 idle_ext_away_stop (idle
);
233 /* Only try and set the presence if the away saved state is not
234 * unset. This is an odd case because it means that the session
235 * didn't notify us of the state change to idle, and as a
236 * result, we couldn't save the current state at that time.
238 if (priv
->away_saved_state
!= TP_CONNECTION_PRESENCE_TYPE_UNSET
) {
239 DEBUG ("Restoring state to %d",
240 priv
->away_saved_state
);
242 empathy_idle_set_state (idle
,priv
->away_saved_state
);
244 DEBUG ("Away saved state is unset. This means that we "
245 "weren't told when the session went idle. "
246 "As a result, I'm not trying to set presence");
249 priv
->away_saved_state
= TP_CONNECTION_PRESENCE_TYPE_UNSET
;
252 priv
->is_idle
= is_idle
;
256 idle_state_change_cb (EmpathyConnectivity
*connectivity
,
260 EmpathyIdlePriv
*priv
;
262 priv
= GET_PRIV (idle
);
265 /* We are no longer connected */
266 DEBUG ("Disconnected: Save state %d (%s)",
267 priv
->state
, priv
->status
);
268 priv
->saved_state
= priv
->state
;
269 g_free (priv
->saved_status
);
270 priv
->saved_status
= g_strdup (priv
->status
);
271 empathy_idle_set_state (idle
, TP_CONNECTION_PRESENCE_TYPE_OFFLINE
);
274 && priv
->saved_state
!= TP_CONNECTION_PRESENCE_TYPE_UNSET
) {
275 /* We are now connected */
276 DEBUG ("Reconnected: Restore state %d (%s)",
277 priv
->saved_state
, priv
->saved_status
);
278 empathy_idle_set_presence (idle
,
281 priv
->saved_state
= TP_CONNECTION_PRESENCE_TYPE_UNSET
;
282 g_free (priv
->saved_status
);
283 priv
->saved_status
= NULL
;
288 idle_finalize (GObject
*object
)
290 EmpathyIdlePriv
*priv
;
292 priv
= GET_PRIV (object
);
294 g_free (priv
->status
);
295 g_free (priv
->requested_status_message
);
297 if (priv
->gs_proxy
) {
298 g_object_unref (priv
->gs_proxy
);
301 g_signal_handler_disconnect (priv
->connectivity
,
302 priv
->state_change_signal_id
);
303 priv
->state_change_signal_id
= 0;
305 if (priv
->manager
!= NULL
) {
306 g_signal_handler_disconnect (priv
->manager
,
307 priv
->idle_presence_changed_id
);
308 g_object_unref (priv
->manager
);
311 g_object_unref (priv
->connectivity
);
313 g_hash_table_destroy (priv
->connect_times
);
314 priv
->connect_times
= NULL
;
316 idle_ext_away_stop (EMPATHY_IDLE (object
));
320 idle_constructor (GType type
,
322 GObjectConstructParam
*props
)
326 if (idle_singleton
) {
327 retval
= g_object_ref (idle_singleton
);
329 retval
= G_OBJECT_CLASS (empathy_idle_parent_class
)->constructor
330 (type
, n_props
, props
);
332 idle_singleton
= EMPATHY_IDLE (retval
);
333 g_object_add_weak_pointer (retval
, (gpointer
) &idle_singleton
);
340 empathy_idle_get_status (EmpathyIdle
*idle
)
342 EmpathyIdlePriv
*priv
;
344 priv
= GET_PRIV (idle
);
346 if (G_UNLIKELY (!priv
->ready
))
347 g_critical (G_STRLOC
": %s called before AccountManager ready",
351 return empathy_presence_get_default_message (priv
->state
);
358 idle_get_property (GObject
*object
,
363 EmpathyIdlePriv
*priv
;
366 priv
= GET_PRIV (object
);
367 idle
= EMPATHY_IDLE (object
);
371 g_value_set_enum (value
, empathy_idle_get_state (idle
));
374 g_value_set_string (value
, empathy_idle_get_status (idle
));
377 g_value_set_boolean (value
, empathy_idle_get_auto_away (idle
));
380 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
386 idle_set_property (GObject
*object
,
391 EmpathyIdlePriv
*priv
;
394 priv
= GET_PRIV (object
);
395 idle
= EMPATHY_IDLE (object
);
399 empathy_idle_set_state (idle
, g_value_get_enum (value
));
402 empathy_idle_set_status (idle
, g_value_get_string (value
));
405 empathy_idle_set_auto_away (idle
, g_value_get_boolean (value
));
408 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
414 empathy_idle_class_init (EmpathyIdleClass
*klass
)
416 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
418 object_class
->finalize
= idle_finalize
;
419 object_class
->constructor
= idle_constructor
;
420 object_class
->get_property
= idle_get_property
;
421 object_class
->set_property
= idle_set_property
;
423 g_object_class_install_property (object_class
,
425 g_param_spec_uint ("state",
428 0, NUM_TP_CONNECTION_PRESENCE_TYPES
,
429 TP_CONNECTION_PRESENCE_TYPE_UNSET
,
431 g_object_class_install_property (object_class
,
433 g_param_spec_string ("status",
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
= user_data
;
478 TpAccountManager
*account_manager
= TP_ACCOUNT_MANAGER (source_object
);
479 EmpathyIdlePriv
*priv
;
480 TpConnectionPresenceType state
;
481 gchar
*status
, *status_message
;
483 GError
*error
= NULL
;
485 /* In case we've been finalized before reading this callback */
486 if (idle_singleton
== NULL
)
489 priv
= GET_PRIV (idle
);
492 if (!tp_account_manager_prepare_finish (account_manager
, result
, &error
)) {
493 DEBUG ("Failed to prepare account manager: %s", error
->message
);
494 g_error_free (error
);
498 state
= tp_account_manager_get_most_available_presence (priv
->manager
,
499 &status
, &status_message
);
501 idle_presence_changed_cb (account_manager
, state
, status
,
502 status_message
, idle
);
504 accounts
= tp_account_manager_get_valid_accounts (priv
->manager
);
505 for (l
= accounts
; l
!= NULL
; l
= l
->next
) {
506 tp_g_signal_connect_object (l
->data
, "status-changed",
507 G_CALLBACK (account_status_changed_cb
),
510 g_list_free (accounts
);
513 g_free (status_message
);
517 empathy_idle_init (EmpathyIdle
*idle
)
519 EmpathyIdlePriv
*priv
= G_TYPE_INSTANCE_GET_PRIVATE (idle
,
520 EMPATHY_TYPE_IDLE
, EmpathyIdlePriv
);
524 priv
->is_idle
= FALSE
;
526 priv
->manager
= tp_account_manager_dup ();
528 tp_account_manager_prepare_async (priv
->manager
, NULL
,
529 account_manager_ready_cb
, idle
);
531 priv
->idle_presence_changed_id
= g_signal_connect (priv
->manager
,
532 "most-available-presence-changed",
533 G_CALLBACK (idle_presence_changed_cb
), idle
);
535 dbus
= tp_dbus_daemon_dup (NULL
);
537 priv
->gs_proxy
= dbus_g_proxy_new_for_name (
538 tp_proxy_get_dbus_connection (dbus
),
539 "org.gnome.SessionManager",
540 "/org/gnome/SessionManager/Presence",
541 "org.gnome.SessionManager.Presence");
542 if (priv
->gs_proxy
) {
543 dbus_g_proxy_add_signal (priv
->gs_proxy
, "StatusChanged",
544 G_TYPE_UINT
, G_TYPE_INVALID
);
545 dbus_g_proxy_connect_signal (priv
->gs_proxy
, "StatusChanged",
546 G_CALLBACK (idle_session_status_changed_cb
),
549 DEBUG ("Failed to get gs proxy");
552 g_object_unref (dbus
);
554 priv
->connectivity
= empathy_connectivity_dup_singleton ();
555 priv
->state_change_signal_id
= g_signal_connect (priv
->connectivity
,
556 "state-change", G_CALLBACK (idle_state_change_cb
), idle
);
558 priv
->connect_times
= g_hash_table_new (g_direct_hash
, g_direct_equal
);
562 empathy_idle_dup_singleton (void)
564 return g_object_new (EMPATHY_TYPE_IDLE
, NULL
);
567 TpConnectionPresenceType
568 empathy_idle_get_state (EmpathyIdle
*idle
)
570 EmpathyIdlePriv
*priv
;
572 priv
= GET_PRIV (idle
);
574 if (G_UNLIKELY (!priv
->ready
))
575 g_critical (G_STRLOC
": %s called before AccountManager ready",
582 empathy_idle_set_state (EmpathyIdle
*idle
,
583 TpConnectionPresenceType state
)
585 EmpathyIdlePriv
*priv
;
587 priv
= GET_PRIV (idle
);
589 empathy_idle_set_presence (idle
, state
, priv
->status
);
593 empathy_idle_set_status (EmpathyIdle
*idle
,
596 EmpathyIdlePriv
*priv
;
598 priv
= GET_PRIV (idle
);
600 empathy_idle_set_presence (idle
, priv
->state
, status
);
604 empathy_idle_do_set_presence (EmpathyIdle
*idle
,
605 TpConnectionPresenceType status_type
,
606 const gchar
*status_message
)
608 EmpathyIdlePriv
*priv
= GET_PRIV (idle
);
611 g_assert (status_type
> 0 && status_type
< NUM_TP_CONNECTION_PRESENCE_TYPES
);
613 status
= presence_type_to_status
[status_type
];
615 g_return_if_fail (status
!= NULL
);
617 /* We possibly should be sure that the account manager is prepared, but
618 * sometimes this isn't possible, like when exiting. In other words,
619 * we need a callback to empathy_idle_set_presence to be sure the
620 * presence is set on all accounts successfully.
621 * However, in practice, this is fine as we've already prepared the
622 * account manager here in _init. */
623 tp_account_manager_set_all_requested_presences (priv
->manager
,
624 status_type
, status
, status_message
);
628 empathy_idle_set_presence (EmpathyIdle
*idle
,
629 TpConnectionPresenceType state
,
632 EmpathyIdlePriv
*priv
;
633 const gchar
*default_status
;
635 priv
= GET_PRIV (idle
);
637 DEBUG ("Changing presence to %s (%d)", status
, state
);
639 g_free (priv
->requested_status_message
);
640 priv
->requested_presence_type
= state
;
641 priv
->requested_status_message
= g_strdup (status
);
643 /* Do not set translated default messages */
644 default_status
= empathy_presence_get_default_message (state
);
645 if (!tp_strdiff (status
, default_status
)) {
649 if (state
!= TP_CONNECTION_PRESENCE_TYPE_OFFLINE
&&
650 !empathy_connectivity_is_online (priv
->connectivity
)) {
651 DEBUG ("Empathy is not online");
653 priv
->saved_state
= state
;
654 if (tp_strdiff (priv
->status
, status
)) {
655 g_free (priv
->saved_status
);
656 priv
->saved_status
= NULL
;
657 if (!EMP_STR_EMPTY (status
)) {
658 priv
->saved_status
= g_strdup (status
);
664 empathy_idle_do_set_presence (idle
, state
, status
);
668 empathy_idle_get_auto_away (EmpathyIdle
*idle
)
670 EmpathyIdlePriv
*priv
= GET_PRIV (idle
);
672 return priv
->auto_away
;
676 empathy_idle_set_auto_away (EmpathyIdle
*idle
,
679 EmpathyIdlePriv
*priv
= GET_PRIV (idle
);
681 priv
->auto_away
= auto_away
;
683 g_object_notify (G_OBJECT (idle
), "auto-away");
686 TpConnectionPresenceType
687 empathy_idle_get_requested_presence (EmpathyIdle
*idle
,
689 gchar
**status_message
)
691 EmpathyIdlePriv
*priv
= GET_PRIV (idle
);
693 if (status
!= NULL
) {
694 *status
= g_strdup (presence_type_to_status
[priv
->requested_presence_type
]);
697 if (status_message
!= NULL
) {
698 *status_message
= g_strdup (priv
->requested_status_message
);
701 return priv
->requested_presence_type
;
704 /* This function returns %TRUE if EmpathyIdle considers the account
705 * @account as having just connected recently. Otherwise, it returns
706 * %FALSE. In doubt, %FALSE is returned. */
708 empathy_idle_account_is_just_connected (EmpathyIdle
*idle
,
711 EmpathyIdlePriv
*priv
= GET_PRIV (idle
);
716 if (tp_account_get_connection_status (account
, NULL
)
717 != TP_CONNECTION_STATUS_CONNECTED
) {
721 ptr
= g_hash_table_lookup (priv
->connect_times
, account
);
727 t
= GPOINTER_TO_INT (ptr
);
729 g_get_current_time (&val
);
731 return (val
.tv_sec
- t
) < ACCOUNT_IS_JUST_CONNECTED_SECONDS
;