2 * Copyright (C) 2007-2011 Collabora Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Xavier Claessens <xclaesse@gmail.com>
21 #include "empathy-presence-manager.h"
27 #include <glib/gi18n-lib.h>
28 #include <dbus/dbus-glib.h>
30 #include <telepathy-glib/account-manager.h>
31 #include <telepathy-glib/dbus.h>
32 #include <telepathy-glib/util.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 struct _EmpathyPresenceManagerPrivate
50 EmpathyConnectivity
*connectivity
;
54 TpConnectionPresenceType 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 (EmpathyPresenceManager
, empathy_presence_manager
, G_TYPE_OBJECT
);
93 static EmpathyPresenceManager
* singleton
= NULL
;
95 static const gchar
*presence_type_to_status
[NUM_TP_CONNECTION_PRESENCE_TYPES
] =
109 most_available_presence_changed (TpAccountManager
*manager
,
110 TpConnectionPresenceType state
,
112 gchar
*status_message
,
113 EmpathyPresenceManager
*self
)
115 if (state
== TP_CONNECTION_PRESENCE_TYPE_UNSET
)
116 /* Assume our presence is offline if MC reports UNSET */
117 state
= TP_CONNECTION_PRESENCE_TYPE_OFFLINE
;
119 DEBUG ("Presence changed to '%s' (%d) \"%s\"", status
, state
,
122 g_free (self
->priv
->status
);
123 self
->priv
->state
= state
;
124 if (EMP_STR_EMPTY (status_message
))
125 self
->priv
->status
= NULL
;
127 self
->priv
->status
= g_strdup (status_message
);
129 g_object_notify (G_OBJECT (self
), "state");
130 g_object_notify (G_OBJECT (self
), "status");
134 ext_away_cb (EmpathyPresenceManager
*self
)
136 DEBUG ("Going to extended autoaway");
137 empathy_presence_manager_set_state (self
,
138 TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY
);
139 self
->priv
->ext_away_timeout
= 0;
145 next_away_stop (EmpathyPresenceManager
*self
)
147 if (self
->priv
->ext_away_timeout
)
149 g_source_remove (self
->priv
->ext_away_timeout
);
150 self
->priv
->ext_away_timeout
= 0;
155 ext_away_start (EmpathyPresenceManager
*self
)
157 if (self
->priv
->ext_away_timeout
!= 0)
160 self
->priv
->ext_away_timeout
= g_timeout_add_seconds (EXT_AWAY_TIME
,
161 (GSourceFunc
) ext_away_cb
, self
);
165 session_status_changed_cb (DBusGProxy
*gs_proxy
,
166 SessionStatus status
,
167 EmpathyPresenceManager
*self
)
171 is_idle
= (status
== SESSION_STATUS_IDLE
);
173 DEBUG ("Session idle state changed, %s -> %s",
174 self
->priv
->is_idle
? "yes" : "no",
175 is_idle
? "yes" : "no");
177 if (!self
->priv
->auto_away
||
178 (self
->priv
->saved_state
== TP_CONNECTION_PRESENCE_TYPE_UNSET
&&
179 (self
->priv
->state
<= TP_CONNECTION_PRESENCE_TYPE_OFFLINE
||
180 self
->priv
->state
== TP_CONNECTION_PRESENCE_TYPE_HIDDEN
)))
182 /* We don't want to go auto away OR we explicitely asked to be
183 * offline, nothing to do here */
184 self
->priv
->is_idle
= is_idle
;
188 if (is_idle
&& !self
->priv
->is_idle
)
190 TpConnectionPresenceType new_state
;
191 /* We are now idle */
193 ext_away_start (self
);
195 if (self
->priv
->saved_state
!= TP_CONNECTION_PRESENCE_TYPE_UNSET
)
196 /* We are disconnected, when coming back from away
197 * we want to restore the presence before the
199 self
->priv
->away_saved_state
= self
->priv
->saved_state
;
201 self
->priv
->away_saved_state
= self
->priv
->state
;
203 new_state
= TP_CONNECTION_PRESENCE_TYPE_AWAY
;
204 if (self
->priv
->state
== TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY
)
205 new_state
= TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY
;
207 DEBUG ("Going to autoaway. Saved state=%d, new state=%d",
208 self
->priv
->away_saved_state
, new_state
);
209 empathy_presence_manager_set_state (self
, new_state
);
211 else if (!is_idle
&& self
->priv
->is_idle
)
213 /* We are no more idle, restore state */
215 next_away_stop (self
);
217 /* Only try and set the presence if the away saved state is not
218 * unset. This is an odd case because it means that the session
219 * didn't notify us of the state change to idle, and as a
220 * result, we couldn't save the current state at that time.
222 if (self
->priv
->away_saved_state
!= TP_CONNECTION_PRESENCE_TYPE_UNSET
)
224 DEBUG ("Restoring state to %d",
225 self
->priv
->away_saved_state
);
227 empathy_presence_manager_set_state (self
,
228 self
->priv
->away_saved_state
);
232 DEBUG ("Away saved state is unset. This means that we "
233 "weren't told when the session went idle. "
234 "As a result, I'm not trying to set presence");
237 self
->priv
->away_saved_state
= TP_CONNECTION_PRESENCE_TYPE_UNSET
;
240 self
->priv
->is_idle
= is_idle
;
244 state_change_cb (EmpathyConnectivity
*connectivity
,
246 EmpathyPresenceManager
*self
)
250 /* We are no longer connected */
251 DEBUG ("Disconnected: Save state %d (%s)",
252 self
->priv
->state
, self
->priv
->status
);
253 self
->priv
->saved_state
= self
->priv
->state
;
254 g_free (self
->priv
->saved_status
);
255 self
->priv
->saved_status
= g_strdup (self
->priv
->status
);
256 empathy_presence_manager_set_state (self
,
257 TP_CONNECTION_PRESENCE_TYPE_OFFLINE
);
260 && self
->priv
->saved_state
!= TP_CONNECTION_PRESENCE_TYPE_UNSET
)
262 /* We are now connected */
263 DEBUG ("Reconnected: Restore state %d (%s)",
264 self
->priv
->saved_state
, self
->priv
->saved_status
);
265 empathy_presence_manager_set_presence (self
,
266 self
->priv
->saved_state
,
267 self
->priv
->saved_status
);
268 self
->priv
->saved_state
= TP_CONNECTION_PRESENCE_TYPE_UNSET
;
269 g_free (self
->priv
->saved_status
);
270 self
->priv
->saved_status
= NULL
;
275 presence_manager_dispose (GObject
*object
)
277 EmpathyPresenceManager
*self
= (EmpathyPresenceManager
*) object
;
279 tp_clear_object (&self
->priv
->gs_proxy
);
280 tp_clear_object (&self
->priv
->manager
);
282 tp_clear_object (&self
->priv
->connectivity
);
283 tp_clear_pointer (&self
->priv
->connect_times
, g_hash_table_unref
);
285 next_away_stop (EMPATHY_PRESENCE_MANAGER (object
));
287 G_OBJECT_CLASS (empathy_presence_manager_parent_class
)->dispose (object
);
291 presence_manager_finalize (GObject
*object
)
293 EmpathyPresenceManager
*self
= (EmpathyPresenceManager
*) object
;
295 g_free (self
->priv
->status
);
296 g_free (self
->priv
->requested_status_message
);
298 G_OBJECT_CLASS (empathy_presence_manager_parent_class
)->finalize (object
);
302 presence_manager_constructor (GType type
,
304 GObjectConstructParam
*props
)
310 retval
= g_object_ref (singleton
);
314 retval
= G_OBJECT_CLASS (empathy_presence_manager_parent_class
)->
315 constructor (type
, n_props
, props
);
317 singleton
= EMPATHY_PRESENCE_MANAGER (retval
);
318 g_object_add_weak_pointer (retval
, (gpointer
) &singleton
);
325 empathy_presence_manager_get_status (EmpathyPresenceManager
*self
)
327 if (G_UNLIKELY (!self
->priv
->ready
))
328 g_critical (G_STRLOC
": %s called before AccountManager ready",
331 if (!self
->priv
->status
)
332 return empathy_presence_get_default_message (self
->priv
->state
);
334 return self
->priv
->status
;
338 presence_manager_get_property (GObject
*object
,
343 EmpathyPresenceManager
*self
= EMPATHY_PRESENCE_MANAGER (object
);
348 g_value_set_enum (value
, empathy_presence_manager_get_state (self
));
351 g_value_set_string (value
, empathy_presence_manager_get_status (self
));
354 g_value_set_boolean (value
,
355 empathy_presence_manager_get_auto_away (self
));
358 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
364 presence_manager_set_property (GObject
*object
,
369 EmpathyPresenceManager
*self
= EMPATHY_PRESENCE_MANAGER (object
);
374 empathy_presence_manager_set_state (self
, g_value_get_enum (value
));
377 empathy_presence_manager_set_status (self
, g_value_get_string (value
));
380 empathy_presence_manager_set_auto_away (self
,
381 g_value_get_boolean (value
));
384 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
390 empathy_presence_manager_class_init (EmpathyPresenceManagerClass
*klass
)
392 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
394 object_class
->dispose
= presence_manager_dispose
;
395 object_class
->finalize
= presence_manager_finalize
;
396 object_class
->constructor
= presence_manager_constructor
;
397 object_class
->get_property
= presence_manager_get_property
;
398 object_class
->set_property
= presence_manager_set_property
;
400 g_object_class_install_property (object_class
,
402 g_param_spec_uint ("state", "state", "state",
403 0, NUM_TP_CONNECTION_PRESENCE_TYPES
,
404 TP_CONNECTION_PRESENCE_TYPE_UNSET
,
407 g_object_class_install_property (object_class
,
409 g_param_spec_string ("status","status", "status",
413 g_object_class_install_property (object_class
,
415 g_param_spec_boolean ("auto-away", "Automatic set presence to away",
416 "Should it set presence to away if inactive",
420 g_type_class_add_private (object_class
,
421 sizeof (EmpathyPresenceManagerPrivate
));
425 account_status_changed_cb (TpAccount
*account
,
429 gchar
*dbus_error_name
,
433 EmpathyPresenceManager
*self
= EMPATHY_PRESENCE_MANAGER (user_data
);
436 if (new_status
== TP_CONNECTION_STATUS_CONNECTED
)
438 g_get_current_time (&val
);
439 g_hash_table_insert (self
->priv
->connect_times
, account
,
440 GINT_TO_POINTER (val
.tv_sec
));
442 else if (new_status
== TP_CONNECTION_STATUS_DISCONNECTED
)
444 g_hash_table_remove (self
->priv
->connect_times
, account
);
449 account_manager_ready_cb (GObject
*source_object
,
450 GAsyncResult
*result
,
453 EmpathyPresenceManager
*self
= user_data
;
454 TpAccountManager
*account_manager
= TP_ACCOUNT_MANAGER (source_object
);
455 TpConnectionPresenceType state
;
456 gchar
*status
, *status_message
;
458 GError
*error
= NULL
;
460 /* In case we've been finalized before reading this callback */
461 if (singleton
== NULL
)
464 self
->priv
->ready
= TRUE
;
466 if (!tp_account_manager_prepare_finish (account_manager
, result
, &error
))
468 DEBUG ("Failed to prepare account manager: %s", error
->message
);
469 g_error_free (error
);
473 state
= tp_account_manager_get_most_available_presence (self
->priv
->manager
,
474 &status
, &status_message
);
476 most_available_presence_changed (account_manager
, state
, status
,
477 status_message
, self
);
479 accounts
= tp_account_manager_get_valid_accounts (self
->priv
->manager
);
480 for (l
= accounts
; l
!= NULL
; l
= l
->next
)
482 tp_g_signal_connect_object (l
->data
, "status-changed",
483 G_CALLBACK (account_status_changed_cb
), self
, 0);
485 g_list_free (accounts
);
488 g_free (status_message
);
492 empathy_presence_manager_init (EmpathyPresenceManager
*self
)
496 self
->priv
= G_TYPE_INSTANCE_GET_PRIVATE (self
,
497 EMPATHY_TYPE_PRESENCE_MANAGER
, EmpathyPresenceManagerPrivate
);
499 self
->priv
->is_idle
= FALSE
;
501 self
->priv
->manager
= tp_account_manager_dup ();
503 tp_account_manager_prepare_async (self
->priv
->manager
, NULL
,
504 account_manager_ready_cb
, self
);
506 tp_g_signal_connect_object (self
->priv
->manager
,
507 "most-available-presence-changed",
508 G_CALLBACK (most_available_presence_changed
), self
, 0);
510 dbus
= tp_dbus_daemon_dup (NULL
);
512 self
->priv
->gs_proxy
= dbus_g_proxy_new_for_name (
513 tp_proxy_get_dbus_connection (dbus
),
514 "org.gnome.SessionManager",
515 "/org/gnome/SessionManager/Presence",
516 "org.gnome.SessionManager.Presence");
518 if (self
->priv
->gs_proxy
)
520 dbus_g_proxy_add_signal (self
->priv
->gs_proxy
, "StatusChanged",
521 G_TYPE_UINT
, G_TYPE_INVALID
);
522 dbus_g_proxy_connect_signal (self
->priv
->gs_proxy
, "StatusChanged",
523 G_CALLBACK (session_status_changed_cb
),
528 DEBUG ("Failed to get gs proxy");
531 g_object_unref (dbus
);
533 self
->priv
->connectivity
= empathy_connectivity_dup_singleton ();
535 tp_g_signal_connect_object (self
->priv
->connectivity
,
536 "state-change", G_CALLBACK (state_change_cb
), self
, 0);
538 self
->priv
->connect_times
= g_hash_table_new (g_direct_hash
, g_direct_equal
);
541 EmpathyPresenceManager
*
542 empathy_presence_manager_dup_singleton (void)
544 return g_object_new (EMPATHY_TYPE_PRESENCE_MANAGER
, NULL
);
547 TpConnectionPresenceType
548 empathy_presence_manager_get_state (EmpathyPresenceManager
*self
)
550 if (G_UNLIKELY (!self
->priv
->ready
))
551 g_critical (G_STRLOC
": %s called before AccountManager ready",
554 return self
->priv
->state
;
558 empathy_presence_manager_set_state (EmpathyPresenceManager
*self
,
559 TpConnectionPresenceType state
)
561 empathy_presence_manager_set_presence (self
, state
, self
->priv
->status
);
565 empathy_presence_manager_set_status (EmpathyPresenceManager
*self
,
568 empathy_presence_manager_set_presence (self
, self
->priv
->state
, status
);
572 empathy_presence_manager_do_set_presence (EmpathyPresenceManager
*self
,
573 TpConnectionPresenceType status_type
,
574 const gchar
*status_message
)
578 g_assert (status_type
> 0 && status_type
< NUM_TP_CONNECTION_PRESENCE_TYPES
);
580 status
= presence_type_to_status
[status_type
];
582 g_return_if_fail (status
!= NULL
);
584 /* We possibly should be sure that the account manager is prepared, but
585 * sometimes this isn't possible, like when exiting. In other words,
586 * we need a callback to empathy_presence_manager_set_presence to be sure the
587 * presence is set on all accounts successfully.
588 * However, in practice, this is fine as we've already prepared the
589 * account manager here in _init. */
590 tp_account_manager_set_all_requested_presences (self
->priv
->manager
,
591 status_type
, status
, status_message
);
595 empathy_presence_manager_set_presence (EmpathyPresenceManager
*self
,
596 TpConnectionPresenceType state
,
599 const gchar
*default_status
;
601 DEBUG ("Changing presence to %s (%d)", status
, state
);
603 g_free (self
->priv
->requested_status_message
);
604 self
->priv
->requested_presence_type
= state
;
605 self
->priv
->requested_status_message
= g_strdup (status
);
607 /* Do not set translated default messages */
608 default_status
= empathy_presence_get_default_message (state
);
609 if (!tp_strdiff (status
, default_status
))
612 if (state
!= TP_CONNECTION_PRESENCE_TYPE_OFFLINE
&&
613 !empathy_connectivity_is_online (self
->priv
->connectivity
))
615 DEBUG ("Empathy is not online");
617 self
->priv
->saved_state
= state
;
618 if (tp_strdiff (self
->priv
->status
, status
))
620 g_free (self
->priv
->saved_status
);
621 self
->priv
->saved_status
= NULL
;
622 if (!EMP_STR_EMPTY (status
))
623 self
->priv
->saved_status
= g_strdup (status
);
628 empathy_presence_manager_do_set_presence (self
, state
, status
);
632 empathy_presence_manager_get_auto_away (EmpathyPresenceManager
*self
)
634 return self
->priv
->auto_away
;
638 empathy_presence_manager_set_auto_away (EmpathyPresenceManager
*self
,
641 self
->priv
->auto_away
= auto_away
;
643 g_object_notify (G_OBJECT (self
), "auto-away");
646 TpConnectionPresenceType
647 empathy_presence_manager_get_requested_presence (EmpathyPresenceManager
*self
,
649 gchar
**status_message
)
652 *status
= g_strdup (presence_type_to_status
[
653 self
->priv
->requested_presence_type
]);
655 if (status_message
!= NULL
)
656 *status_message
= g_strdup (self
->priv
->requested_status_message
);
658 return self
->priv
->requested_presence_type
;
661 /* This function returns %TRUE if EmpathyPresenceManager considers the account
662 * @account as having just connected recently. Otherwise, it returns
663 * %FALSE. In doubt, %FALSE is returned. */
665 empathy_presence_manager_account_is_just_connected (
666 EmpathyPresenceManager
*self
,
673 if (tp_account_get_connection_status (account
, NULL
)
674 != TP_CONNECTION_STATUS_CONNECTED
)
677 ptr
= g_hash_table_lookup (self
->priv
->connect_times
, account
);
682 t
= GPOINTER_TO_INT (ptr
);
684 g_get_current_time (&val
);
686 return (val
.tv_sec
- t
) < ACCOUNT_IS_JUST_CONNECTED_SECONDS
;