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>
22 #include "empathy-presence-manager.h"
24 #include "empathy-utils.h"
26 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
27 #include "empathy-debug.h"
29 /* Number of seconds before entering extended autoaway. */
30 #define EXT_AWAY_TIME (30*60)
32 /* Number of seconds to consider an account in the "just connected" state
34 #define ACCOUNT_IS_JUST_CONNECTED_SECONDS 10
36 struct _EmpathyPresenceManagerPrivate
42 TpConnectionPresenceType state;
46 TpConnectionPresenceType away_saved_state;
49 guint ext_away_timeout;
51 TpAccountManager *manager;
53 /* pointer to a TpAccount --> glong of time of connection */
54 GHashTable *connect_times;
56 TpConnectionPresenceType requested_presence_type;
57 gchar *requested_status_message;
62 SESSION_STATUS_AVAILABLE,
63 SESSION_STATUS_INVISIBLE,
66 SESSION_STATUS_UNKNOWN
77 G_DEFINE_TYPE (EmpathyPresenceManager, empathy_presence_manager, G_TYPE_OBJECT);
79 static EmpathyPresenceManager * singleton = NULL;
81 static const gchar *presence_type_to_status[NUM_TP_CONNECTION_PRESENCE_TYPES] =
95 most_available_presence_changed (TpAccountManager *manager,
96 TpConnectionPresenceType state,
98 gchar *status_message,
99 EmpathyPresenceManager *self)
101 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET)
102 /* Assume our presence is offline if MC reports UNSET */
103 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
105 DEBUG ("Presence changed to '%s' (%d) \"%s\"", status, state,
108 g_free (self->priv->status);
109 self->priv->state = state;
110 if (EMP_STR_EMPTY (status_message))
111 self->priv->status = NULL;
113 self->priv->status = g_strdup (status_message);
115 g_object_notify (G_OBJECT (self), "state");
116 g_object_notify (G_OBJECT (self), "status");
120 ext_away_cb (EmpathyPresenceManager *self)
122 DEBUG ("Going to extended autoaway");
123 empathy_presence_manager_set_state (self,
124 TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY);
125 self->priv->ext_away_timeout = 0;
131 next_away_stop (EmpathyPresenceManager *self)
133 if (self->priv->ext_away_timeout)
135 g_source_remove (self->priv->ext_away_timeout);
136 self->priv->ext_away_timeout = 0;
141 ext_away_start (EmpathyPresenceManager *self)
143 if (self->priv->ext_away_timeout != 0)
146 self->priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
147 (GSourceFunc) ext_away_cb, self);
151 session_status_changed_cb (DBusGProxy *gs_proxy,
152 SessionStatus status,
153 EmpathyPresenceManager *self)
157 is_idle = (status == SESSION_STATUS_IDLE);
159 DEBUG ("Session idle state changed, %s -> %s",
160 self->priv->is_idle ? "yes" : "no",
161 is_idle ? "yes" : "no");
163 if (!self->priv->auto_away ||
164 (self->priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
165 self->priv->state == TP_CONNECTION_PRESENCE_TYPE_HIDDEN))
167 /* We don't want to go auto away OR we explicitely asked to be
168 * offline, nothing to do here */
169 self->priv->is_idle = is_idle;
173 if (is_idle && !self->priv->is_idle)
175 TpConnectionPresenceType new_state;
176 /* We are now idle */
178 ext_away_start (self);
180 self->priv->away_saved_state = self->priv->state;
182 new_state = TP_CONNECTION_PRESENCE_TYPE_AWAY;
183 if (self->priv->state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY)
184 new_state = TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY;
186 DEBUG ("Going to autoaway. Saved state=%d, new state=%d",
187 self->priv->away_saved_state, new_state);
188 empathy_presence_manager_set_state (self, new_state);
190 else if (!is_idle && self->priv->is_idle)
192 /* We are no more idle, restore state */
194 next_away_stop (self);
196 /* Only try and set the presence if the away saved state is not
197 * unset. This is an odd case because it means that the session
198 * didn't notify us of the state change to idle, and as a
199 * result, we couldn't save the current state at that time.
201 if (self->priv->away_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET)
203 DEBUG ("Restoring state to %d",
204 self->priv->away_saved_state);
206 empathy_presence_manager_set_state (self,
207 self->priv->away_saved_state);
211 DEBUG ("Away saved state is unset. This means that we "
212 "weren't told when the session went idle. "
213 "As a result, I'm not trying to set presence");
216 self->priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
219 self->priv->is_idle = is_idle;
223 presence_manager_dispose (GObject *object)
225 EmpathyPresenceManager *self = (EmpathyPresenceManager *) object;
227 tp_clear_object (&self->priv->gs_proxy);
228 tp_clear_object (&self->priv->manager);
230 tp_clear_pointer (&self->priv->connect_times, g_hash_table_unref);
232 next_away_stop (EMPATHY_PRESENCE_MANAGER (object));
234 G_OBJECT_CLASS (empathy_presence_manager_parent_class)->dispose (object);
238 presence_manager_finalize (GObject *object)
240 EmpathyPresenceManager *self = (EmpathyPresenceManager *) object;
242 g_free (self->priv->status);
243 g_free (self->priv->requested_status_message);
245 G_OBJECT_CLASS (empathy_presence_manager_parent_class)->finalize (object);
249 presence_manager_constructor (GType type,
251 GObjectConstructParam *props)
257 retval = g_object_ref (singleton);
261 retval = G_OBJECT_CLASS (empathy_presence_manager_parent_class)->
262 constructor (type, n_props, props);
264 singleton = EMPATHY_PRESENCE_MANAGER (retval);
265 g_object_add_weak_pointer (retval, (gpointer) &singleton);
272 empathy_presence_manager_get_status (EmpathyPresenceManager *self)
274 if (G_UNLIKELY (!self->priv->ready))
275 g_critical (G_STRLOC ": %s called before AccountManager ready",
278 if (!self->priv->status)
279 return empathy_presence_get_default_message (self->priv->state);
281 return self->priv->status;
285 presence_manager_get_property (GObject *object,
290 EmpathyPresenceManager *self = EMPATHY_PRESENCE_MANAGER (object);
295 g_value_set_enum (value, empathy_presence_manager_get_state (self));
298 g_value_set_string (value, empathy_presence_manager_get_status (self));
301 g_value_set_boolean (value,
302 empathy_presence_manager_get_auto_away (self));
305 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
311 presence_manager_set_property (GObject *object,
316 EmpathyPresenceManager *self = EMPATHY_PRESENCE_MANAGER (object);
321 empathy_presence_manager_set_state (self, g_value_get_enum (value));
324 empathy_presence_manager_set_status (self, g_value_get_string (value));
327 empathy_presence_manager_set_auto_away (self,
328 g_value_get_boolean (value));
331 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
337 empathy_presence_manager_class_init (EmpathyPresenceManagerClass *klass)
339 GObjectClass *object_class = G_OBJECT_CLASS (klass);
341 object_class->dispose = presence_manager_dispose;
342 object_class->finalize = presence_manager_finalize;
343 object_class->constructor = presence_manager_constructor;
344 object_class->get_property = presence_manager_get_property;
345 object_class->set_property = presence_manager_set_property;
347 g_object_class_install_property (object_class,
349 g_param_spec_uint ("state", "state", "state",
350 0, NUM_TP_CONNECTION_PRESENCE_TYPES,
351 TP_CONNECTION_PRESENCE_TYPE_UNSET,
354 g_object_class_install_property (object_class,
356 g_param_spec_string ("status","status", "status",
360 g_object_class_install_property (object_class,
362 g_param_spec_boolean ("auto-away", "Automatic set presence to away",
363 "Should it set presence to away if inactive",
367 g_type_class_add_private (object_class,
368 sizeof (EmpathyPresenceManagerPrivate));
372 account_status_changed_cb (TpAccount *account,
376 gchar *dbus_error_name,
380 EmpathyPresenceManager *self = EMPATHY_PRESENCE_MANAGER (user_data);
383 if (new_status == TP_CONNECTION_STATUS_CONNECTED)
385 g_get_current_time (&val);
386 g_hash_table_insert (self->priv->connect_times, account,
387 GINT_TO_POINTER (val.tv_sec));
389 else if (new_status == TP_CONNECTION_STATUS_DISCONNECTED)
391 g_hash_table_remove (self->priv->connect_times, account);
396 account_manager_ready_cb (GObject *source_object,
397 GAsyncResult *result,
400 EmpathyPresenceManager *self = user_data;
401 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
402 TpConnectionPresenceType state;
403 gchar *status, *status_message;
405 GError *error = NULL;
407 /* In case we've been finalized before reading this callback */
408 if (singleton == NULL)
411 self->priv->ready = TRUE;
413 if (!tp_proxy_prepare_finish (account_manager, result, &error))
415 DEBUG ("Failed to prepare account manager: %s", error->message);
416 g_error_free (error);
420 state = tp_account_manager_get_most_available_presence (self->priv->manager,
421 &status, &status_message);
423 most_available_presence_changed (account_manager, state, status,
424 status_message, self);
426 accounts = tp_account_manager_dup_valid_accounts (self->priv->manager);
427 for (l = accounts; l != NULL; l = l->next)
429 tp_g_signal_connect_object (l->data, "status-changed",
430 G_CALLBACK (account_status_changed_cb), self, 0);
432 g_list_free_full (accounts, g_object_unref);
435 g_free (status_message);
439 empathy_presence_manager_init (EmpathyPresenceManager *self)
443 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
444 EMPATHY_TYPE_PRESENCE_MANAGER, EmpathyPresenceManagerPrivate);
446 self->priv->is_idle = FALSE;
448 self->priv->manager = tp_account_manager_dup ();
450 tp_proxy_prepare_async (self->priv->manager, NULL,
451 account_manager_ready_cb, self);
453 tp_g_signal_connect_object (self->priv->manager,
454 "most-available-presence-changed",
455 G_CALLBACK (most_available_presence_changed), self, 0);
457 dbus = tp_dbus_daemon_dup (NULL);
459 self->priv->gs_proxy = dbus_g_proxy_new_for_name (
460 tp_proxy_get_dbus_connection (dbus),
461 "org.gnome.SessionManager",
462 "/org/gnome/SessionManager/Presence",
463 "org.gnome.SessionManager.Presence");
465 if (self->priv->gs_proxy)
467 dbus_g_proxy_add_signal (self->priv->gs_proxy, "StatusChanged",
468 G_TYPE_UINT, G_TYPE_INVALID);
469 dbus_g_proxy_connect_signal (self->priv->gs_proxy, "StatusChanged",
470 G_CALLBACK (session_status_changed_cb),
475 DEBUG ("Failed to get gs proxy");
478 g_object_unref (dbus);
480 self->priv->connect_times = g_hash_table_new (g_direct_hash, g_direct_equal);
483 EmpathyPresenceManager *
484 empathy_presence_manager_dup_singleton (void)
486 return g_object_new (EMPATHY_TYPE_PRESENCE_MANAGER, NULL);
489 TpConnectionPresenceType
490 empathy_presence_manager_get_state (EmpathyPresenceManager *self)
492 if (G_UNLIKELY (!self->priv->ready))
493 g_critical (G_STRLOC ": %s called before AccountManager ready",
496 return self->priv->state;
500 empathy_presence_manager_set_state (EmpathyPresenceManager *self,
501 TpConnectionPresenceType state)
503 empathy_presence_manager_set_presence (self, state, self->priv->status);
507 empathy_presence_manager_set_status (EmpathyPresenceManager *self,
510 empathy_presence_manager_set_presence (self, self->priv->state, status);
514 empathy_presence_manager_do_set_presence (EmpathyPresenceManager *self,
515 TpConnectionPresenceType status_type,
516 const gchar *status_message)
520 g_assert (status_type > 0 && status_type < NUM_TP_CONNECTION_PRESENCE_TYPES);
522 status = presence_type_to_status[status_type];
524 g_return_if_fail (status != NULL);
526 /* We possibly should be sure that the account manager is prepared, but
527 * sometimes this isn't possible, like when exiting. In other words,
528 * we need a callback to empathy_presence_manager_set_presence to be sure the
529 * presence is set on all accounts successfully.
530 * However, in practice, this is fine as we've already prepared the
531 * account manager here in _init. */
532 tp_account_manager_set_all_requested_presences (self->priv->manager,
533 status_type, status, status_message);
537 empathy_presence_manager_set_presence (EmpathyPresenceManager *self,
538 TpConnectionPresenceType state,
541 const gchar *default_status;
543 DEBUG ("Changing presence to %s (%d)", status, state);
545 g_free (self->priv->requested_status_message);
546 self->priv->requested_presence_type = state;
547 self->priv->requested_status_message = g_strdup (status);
549 /* Do not set translated default messages */
550 default_status = empathy_presence_get_default_message (state);
551 if (!tp_strdiff (status, default_status))
554 empathy_presence_manager_do_set_presence (self, state, status);
558 empathy_presence_manager_get_auto_away (EmpathyPresenceManager *self)
560 return self->priv->auto_away;
564 empathy_presence_manager_set_auto_away (EmpathyPresenceManager *self,
567 self->priv->auto_away = auto_away;
569 g_object_notify (G_OBJECT (self), "auto-away");
572 TpConnectionPresenceType
573 empathy_presence_manager_get_requested_presence (EmpathyPresenceManager *self,
575 gchar **status_message)
578 *status = g_strdup (presence_type_to_status[
579 self->priv->requested_presence_type]);
581 if (status_message != NULL)
582 *status_message = g_strdup (self->priv->requested_status_message);
584 return self->priv->requested_presence_type;
587 /* This function returns %TRUE if EmpathyPresenceManager considers the account
588 * @account as having just connected recently. Otherwise, it returns
589 * %FALSE. In doubt, %FALSE is returned. */
591 empathy_presence_manager_account_is_just_connected (
592 EmpathyPresenceManager *self,
599 if (tp_account_get_connection_status (account, NULL)
600 != TP_CONNECTION_STATUS_CONNECTED)
603 ptr = g_hash_table_lookup (self->priv->connect_times, account);
608 t = GPOINTER_TO_INT (ptr);
610 g_get_current_time (&val);
612 return (val.tv_sec - t) < ACCOUNT_IS_JUST_CONNECTED_SECONDS;