rename text/{persona,individual}-id as they are not standard
[empathy-mirror.git] / libempathy / empathy-presence-manager.c
blobc3eae65834fae5f4e7afc2b5b00e7afd94593285
1 /*
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"
23 #include <config.h>
25 #include <string.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
44 * for. */
45 #define ACCOUNT_IS_JUST_CONNECTED_SECONDS 10
47 struct _EmpathyPresenceManagerPrivate
49 DBusGProxy *gs_proxy;
50 EmpathyConnectivity *connectivity;
52 gboolean ready;
54 TpConnectionPresenceType state;
55 gchar *status;
56 gboolean auto_away;
58 TpConnectionPresenceType away_saved_state;
59 TpConnectionPresenceType saved_state;
60 gchar *saved_status;
62 gboolean is_idle;
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;
74 typedef enum
76 SESSION_STATUS_AVAILABLE,
77 SESSION_STATUS_INVISIBLE,
78 SESSION_STATUS_BUSY,
79 SESSION_STATUS_IDLE,
80 SESSION_STATUS_UNKNOWN
81 } SessionStatus;
83 enum
85 PROP_0,
86 PROP_STATE,
87 PROP_STATUS,
88 PROP_AUTO_AWAY
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] =
97 NULL,
98 "offline",
99 "available",
100 "away",
101 "xa",
102 "hidden",
103 "busy",
104 NULL,
105 NULL,
108 static void
109 most_available_presence_changed (TpAccountManager *manager,
110 TpConnectionPresenceType state,
111 gchar *status,
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,
120 status_message);
122 g_free (self->priv->status);
123 self->priv->state = state;
124 if (EMP_STR_EMPTY (status_message))
125 self->priv->status = NULL;
126 else
127 self->priv->status = g_strdup (status_message);
129 g_object_notify (G_OBJECT (self), "state");
130 g_object_notify (G_OBJECT (self), "status");
133 static gboolean
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;
141 return FALSE;
144 static void
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;
154 static void
155 ext_away_start (EmpathyPresenceManager *self)
157 if (self->priv->ext_away_timeout != 0)
158 return;
160 self->priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
161 (GSourceFunc) ext_away_cb, self);
164 static void
165 session_status_changed_cb (DBusGProxy *gs_proxy,
166 SessionStatus status,
167 EmpathyPresenceManager *self)
169 gboolean is_idle;
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;
185 return;
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
198 * disconnection. */
199 self->priv->away_saved_state = self->priv->saved_state;
200 else
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);
230 else
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;
243 static void
244 state_change_cb (EmpathyConnectivity *connectivity,
245 gboolean new_online,
246 EmpathyPresenceManager *self)
248 if (!new_online)
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);
259 else if (new_online
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;
274 static void
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);
290 static void
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);
301 static GObject *
302 presence_manager_constructor (GType type,
303 guint n_props,
304 GObjectConstructParam *props)
306 GObject *retval;
308 if (singleton)
310 retval = g_object_ref (singleton);
312 else
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);
321 return retval;
324 static const gchar *
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",
329 G_STRFUNC);
331 if (!self->priv->status)
332 return empathy_presence_get_default_message (self->priv->state);
334 return self->priv->status;
337 static void
338 presence_manager_get_property (GObject *object,
339 guint param_id,
340 GValue *value,
341 GParamSpec *pspec)
343 EmpathyPresenceManager *self = EMPATHY_PRESENCE_MANAGER (object);
345 switch (param_id)
347 case PROP_STATE:
348 g_value_set_enum (value, empathy_presence_manager_get_state (self));
349 break;
350 case PROP_STATUS:
351 g_value_set_string (value, empathy_presence_manager_get_status (self));
352 break;
353 case PROP_AUTO_AWAY:
354 g_value_set_boolean (value,
355 empathy_presence_manager_get_auto_away (self));
356 break;
357 default:
358 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
359 break;
363 static void
364 presence_manager_set_property (GObject *object,
365 guint param_id,
366 const GValue *value,
367 GParamSpec *pspec)
369 EmpathyPresenceManager *self = EMPATHY_PRESENCE_MANAGER (object);
371 switch (param_id)
373 case PROP_STATE:
374 empathy_presence_manager_set_state (self, g_value_get_enum (value));
375 break;
376 case PROP_STATUS:
377 empathy_presence_manager_set_status (self, g_value_get_string (value));
378 break;
379 case PROP_AUTO_AWAY:
380 empathy_presence_manager_set_auto_away (self,
381 g_value_get_boolean (value));
382 break;
383 default:
384 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
385 break;
389 static void
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,
401 PROP_STATE,
402 g_param_spec_uint ("state", "state", "state",
403 0, NUM_TP_CONNECTION_PRESENCE_TYPES,
404 TP_CONNECTION_PRESENCE_TYPE_UNSET,
405 G_PARAM_READWRITE));
407 g_object_class_install_property (object_class,
408 PROP_STATUS,
409 g_param_spec_string ("status","status", "status",
410 NULL,
411 G_PARAM_READWRITE));
413 g_object_class_install_property (object_class,
414 PROP_AUTO_AWAY,
415 g_param_spec_boolean ("auto-away", "Automatic set presence to away",
416 "Should it set presence to away if inactive",
417 FALSE,
418 G_PARAM_READWRITE));
420 g_type_class_add_private (object_class,
421 sizeof (EmpathyPresenceManagerPrivate));
424 static void
425 account_status_changed_cb (TpAccount *account,
426 guint old_status,
427 guint new_status,
428 guint reason,
429 gchar *dbus_error_name,
430 GHashTable *details,
431 gpointer user_data)
433 EmpathyPresenceManager *self = EMPATHY_PRESENCE_MANAGER (user_data);
434 GTimeVal val;
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);
448 static void
449 account_manager_ready_cb (GObject *source_object,
450 GAsyncResult *result,
451 gpointer user_data)
453 EmpathyPresenceManager *self = user_data;
454 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
455 TpConnectionPresenceType state;
456 gchar *status, *status_message;
457 GList *accounts, *l;
458 GError *error = NULL;
460 /* In case we've been finalized before reading this callback */
461 if (singleton == NULL)
462 return;
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);
470 return;
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);
487 g_free (status);
488 g_free (status_message);
491 static void
492 empathy_presence_manager_init (EmpathyPresenceManager *self)
494 TpDBusDaemon *dbus;
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),
524 self, NULL);
526 else
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",
552 G_STRFUNC);
554 return self->priv->state;
557 void
558 empathy_presence_manager_set_state (EmpathyPresenceManager *self,
559 TpConnectionPresenceType state)
561 empathy_presence_manager_set_presence (self, state, self->priv->status);
564 void
565 empathy_presence_manager_set_status (EmpathyPresenceManager *self,
566 const gchar *status)
568 empathy_presence_manager_set_presence (self, self->priv->state, status);
571 static void
572 empathy_presence_manager_do_set_presence (EmpathyPresenceManager *self,
573 TpConnectionPresenceType status_type,
574 const gchar *status_message)
576 const gchar *status;
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);
594 void
595 empathy_presence_manager_set_presence (EmpathyPresenceManager *self,
596 TpConnectionPresenceType state,
597 const gchar *status)
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))
610 status = NULL;
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);
625 return;
628 empathy_presence_manager_do_set_presence (self, state, status);
631 gboolean
632 empathy_presence_manager_get_auto_away (EmpathyPresenceManager *self)
634 return self->priv->auto_away;
637 void
638 empathy_presence_manager_set_auto_away (EmpathyPresenceManager *self,
639 gboolean auto_away)
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,
648 gchar **status,
649 gchar **status_message)
651 if (status != NULL)
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. */
664 gboolean
665 empathy_presence_manager_account_is_just_connected (
666 EmpathyPresenceManager *self,
667 TpAccount *account)
669 GTimeVal val;
670 gpointer ptr;
671 glong t;
673 if (tp_account_get_connection_status (account, NULL)
674 != TP_CONNECTION_STATUS_CONNECTED)
675 return FALSE;
677 ptr = g_hash_table_lookup (self->priv->connect_times, account);
679 if (ptr == NULL)
680 return FALSE;
682 t = GPOINTER_TO_INT (ptr);
684 g_get_current_time (&val);
686 return (val.tv_sec - t) < ACCOUNT_IS_JUST_CONNECTED_SECONDS;