update tests/interactive/.gitignore
[empathy-mirror.git] / libempathy / empathy-presence-manager.c
blob9b1a39217df1e14eda9981866598471b50df10a6
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 "config.h"
23 #include "empathy-presence-manager.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"
36 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
37 #include "empathy-debug.h"
39 /* Number of seconds before entering extended autoaway. */
40 #define EXT_AWAY_TIME (30*60)
42 /* Number of seconds to consider an account in the "just connected" state
43 * for. */
44 #define ACCOUNT_IS_JUST_CONNECTED_SECONDS 10
46 struct _EmpathyPresenceManagerPrivate
48 DBusGProxy *gs_proxy;
50 gboolean ready;
52 TpConnectionPresenceType state;
53 gchar *status;
54 gboolean auto_away;
56 TpConnectionPresenceType away_saved_state;
58 gboolean is_idle;
59 guint ext_away_timeout;
61 TpAccountManager *manager;
63 /* pointer to a TpAccount --> glong of time of connection */
64 GHashTable *connect_times;
66 TpConnectionPresenceType requested_presence_type;
67 gchar *requested_status_message;
70 typedef enum
72 SESSION_STATUS_AVAILABLE,
73 SESSION_STATUS_INVISIBLE,
74 SESSION_STATUS_BUSY,
75 SESSION_STATUS_IDLE,
76 SESSION_STATUS_UNKNOWN
77 } SessionStatus;
79 enum
81 PROP_0,
82 PROP_STATE,
83 PROP_STATUS,
84 PROP_AUTO_AWAY
87 G_DEFINE_TYPE (EmpathyPresenceManager, empathy_presence_manager, G_TYPE_OBJECT);
89 static EmpathyPresenceManager * singleton = NULL;
91 static const gchar *presence_type_to_status[NUM_TP_CONNECTION_PRESENCE_TYPES] =
93 NULL,
94 "offline",
95 "available",
96 "away",
97 "xa",
98 "hidden",
99 "busy",
100 NULL,
101 NULL,
104 static void
105 most_available_presence_changed (TpAccountManager *manager,
106 TpConnectionPresenceType state,
107 gchar *status,
108 gchar *status_message,
109 EmpathyPresenceManager *self)
111 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET)
112 /* Assume our presence is offline if MC reports UNSET */
113 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
115 DEBUG ("Presence changed to '%s' (%d) \"%s\"", status, state,
116 status_message);
118 g_free (self->priv->status);
119 self->priv->state = state;
120 if (EMP_STR_EMPTY (status_message))
121 self->priv->status = NULL;
122 else
123 self->priv->status = g_strdup (status_message);
125 g_object_notify (G_OBJECT (self), "state");
126 g_object_notify (G_OBJECT (self), "status");
129 static gboolean
130 ext_away_cb (EmpathyPresenceManager *self)
132 DEBUG ("Going to extended autoaway");
133 empathy_presence_manager_set_state (self,
134 TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY);
135 self->priv->ext_away_timeout = 0;
137 return FALSE;
140 static void
141 next_away_stop (EmpathyPresenceManager *self)
143 if (self->priv->ext_away_timeout)
145 g_source_remove (self->priv->ext_away_timeout);
146 self->priv->ext_away_timeout = 0;
150 static void
151 ext_away_start (EmpathyPresenceManager *self)
153 if (self->priv->ext_away_timeout != 0)
154 return;
156 self->priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
157 (GSourceFunc) ext_away_cb, self);
160 static void
161 session_status_changed_cb (DBusGProxy *gs_proxy,
162 SessionStatus status,
163 EmpathyPresenceManager *self)
165 gboolean is_idle;
167 is_idle = (status == SESSION_STATUS_IDLE);
169 DEBUG ("Session idle state changed, %s -> %s",
170 self->priv->is_idle ? "yes" : "no",
171 is_idle ? "yes" : "no");
173 if (!self->priv->auto_away ||
174 (self->priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
175 self->priv->state == TP_CONNECTION_PRESENCE_TYPE_HIDDEN))
177 /* We don't want to go auto away OR we explicitely asked to be
178 * offline, nothing to do here */
179 self->priv->is_idle = is_idle;
180 return;
183 if (is_idle && !self->priv->is_idle)
185 TpConnectionPresenceType new_state;
186 /* We are now idle */
188 ext_away_start (self);
190 self->priv->away_saved_state = self->priv->state;
192 new_state = TP_CONNECTION_PRESENCE_TYPE_AWAY;
193 if (self->priv->state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY)
194 new_state = TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY;
196 DEBUG ("Going to autoaway. Saved state=%d, new state=%d",
197 self->priv->away_saved_state, new_state);
198 empathy_presence_manager_set_state (self, new_state);
200 else if (!is_idle && self->priv->is_idle)
202 /* We are no more idle, restore state */
204 next_away_stop (self);
206 /* Only try and set the presence if the away saved state is not
207 * unset. This is an odd case because it means that the session
208 * didn't notify us of the state change to idle, and as a
209 * result, we couldn't save the current state at that time.
211 if (self->priv->away_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET)
213 DEBUG ("Restoring state to %d",
214 self->priv->away_saved_state);
216 empathy_presence_manager_set_state (self,
217 self->priv->away_saved_state);
219 else
221 DEBUG ("Away saved state is unset. This means that we "
222 "weren't told when the session went idle. "
223 "As a result, I'm not trying to set presence");
226 self->priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
229 self->priv->is_idle = is_idle;
232 static void
233 presence_manager_dispose (GObject *object)
235 EmpathyPresenceManager *self = (EmpathyPresenceManager *) object;
237 tp_clear_object (&self->priv->gs_proxy);
238 tp_clear_object (&self->priv->manager);
240 tp_clear_pointer (&self->priv->connect_times, g_hash_table_unref);
242 next_away_stop (EMPATHY_PRESENCE_MANAGER (object));
244 G_OBJECT_CLASS (empathy_presence_manager_parent_class)->dispose (object);
247 static void
248 presence_manager_finalize (GObject *object)
250 EmpathyPresenceManager *self = (EmpathyPresenceManager *) object;
252 g_free (self->priv->status);
253 g_free (self->priv->requested_status_message);
255 G_OBJECT_CLASS (empathy_presence_manager_parent_class)->finalize (object);
258 static GObject *
259 presence_manager_constructor (GType type,
260 guint n_props,
261 GObjectConstructParam *props)
263 GObject *retval;
265 if (singleton)
267 retval = g_object_ref (singleton);
269 else
271 retval = G_OBJECT_CLASS (empathy_presence_manager_parent_class)->
272 constructor (type, n_props, props);
274 singleton = EMPATHY_PRESENCE_MANAGER (retval);
275 g_object_add_weak_pointer (retval, (gpointer) &singleton);
278 return retval;
281 static const gchar *
282 empathy_presence_manager_get_status (EmpathyPresenceManager *self)
284 if (G_UNLIKELY (!self->priv->ready))
285 g_critical (G_STRLOC ": %s called before AccountManager ready",
286 G_STRFUNC);
288 if (!self->priv->status)
289 return empathy_presence_get_default_message (self->priv->state);
291 return self->priv->status;
294 static void
295 presence_manager_get_property (GObject *object,
296 guint param_id,
297 GValue *value,
298 GParamSpec *pspec)
300 EmpathyPresenceManager *self = EMPATHY_PRESENCE_MANAGER (object);
302 switch (param_id)
304 case PROP_STATE:
305 g_value_set_enum (value, empathy_presence_manager_get_state (self));
306 break;
307 case PROP_STATUS:
308 g_value_set_string (value, empathy_presence_manager_get_status (self));
309 break;
310 case PROP_AUTO_AWAY:
311 g_value_set_boolean (value,
312 empathy_presence_manager_get_auto_away (self));
313 break;
314 default:
315 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
316 break;
320 static void
321 presence_manager_set_property (GObject *object,
322 guint param_id,
323 const GValue *value,
324 GParamSpec *pspec)
326 EmpathyPresenceManager *self = EMPATHY_PRESENCE_MANAGER (object);
328 switch (param_id)
330 case PROP_STATE:
331 empathy_presence_manager_set_state (self, g_value_get_enum (value));
332 break;
333 case PROP_STATUS:
334 empathy_presence_manager_set_status (self, g_value_get_string (value));
335 break;
336 case PROP_AUTO_AWAY:
337 empathy_presence_manager_set_auto_away (self,
338 g_value_get_boolean (value));
339 break;
340 default:
341 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
342 break;
346 static void
347 empathy_presence_manager_class_init (EmpathyPresenceManagerClass *klass)
349 GObjectClass *object_class = G_OBJECT_CLASS (klass);
351 object_class->dispose = presence_manager_dispose;
352 object_class->finalize = presence_manager_finalize;
353 object_class->constructor = presence_manager_constructor;
354 object_class->get_property = presence_manager_get_property;
355 object_class->set_property = presence_manager_set_property;
357 g_object_class_install_property (object_class,
358 PROP_STATE,
359 g_param_spec_uint ("state", "state", "state",
360 0, NUM_TP_CONNECTION_PRESENCE_TYPES,
361 TP_CONNECTION_PRESENCE_TYPE_UNSET,
362 G_PARAM_READWRITE));
364 g_object_class_install_property (object_class,
365 PROP_STATUS,
366 g_param_spec_string ("status","status", "status",
367 NULL,
368 G_PARAM_READWRITE));
370 g_object_class_install_property (object_class,
371 PROP_AUTO_AWAY,
372 g_param_spec_boolean ("auto-away", "Automatic set presence to away",
373 "Should it set presence to away if inactive",
374 FALSE,
375 G_PARAM_READWRITE));
377 g_type_class_add_private (object_class,
378 sizeof (EmpathyPresenceManagerPrivate));
381 static void
382 account_status_changed_cb (TpAccount *account,
383 guint old_status,
384 guint new_status,
385 guint reason,
386 gchar *dbus_error_name,
387 GHashTable *details,
388 gpointer user_data)
390 EmpathyPresenceManager *self = EMPATHY_PRESENCE_MANAGER (user_data);
391 GTimeVal val;
393 if (new_status == TP_CONNECTION_STATUS_CONNECTED)
395 g_get_current_time (&val);
396 g_hash_table_insert (self->priv->connect_times, account,
397 GINT_TO_POINTER (val.tv_sec));
399 else if (new_status == TP_CONNECTION_STATUS_DISCONNECTED)
401 g_hash_table_remove (self->priv->connect_times, account);
405 static void
406 account_manager_ready_cb (GObject *source_object,
407 GAsyncResult *result,
408 gpointer user_data)
410 EmpathyPresenceManager *self = user_data;
411 TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
412 TpConnectionPresenceType state;
413 gchar *status, *status_message;
414 GList *accounts, *l;
415 GError *error = NULL;
417 /* In case we've been finalized before reading this callback */
418 if (singleton == NULL)
419 return;
421 self->priv->ready = TRUE;
423 if (!tp_proxy_prepare_finish (account_manager, result, &error))
425 DEBUG ("Failed to prepare account manager: %s", error->message);
426 g_error_free (error);
427 return;
430 state = tp_account_manager_get_most_available_presence (self->priv->manager,
431 &status, &status_message);
433 most_available_presence_changed (account_manager, state, status,
434 status_message, self);
436 accounts = tp_account_manager_get_valid_accounts (self->priv->manager);
437 for (l = accounts; l != NULL; l = l->next)
439 tp_g_signal_connect_object (l->data, "status-changed",
440 G_CALLBACK (account_status_changed_cb), self, 0);
442 g_list_free (accounts);
444 g_free (status);
445 g_free (status_message);
448 static void
449 empathy_presence_manager_init (EmpathyPresenceManager *self)
451 TpDBusDaemon *dbus;
453 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
454 EMPATHY_TYPE_PRESENCE_MANAGER, EmpathyPresenceManagerPrivate);
456 self->priv->is_idle = FALSE;
458 self->priv->manager = tp_account_manager_dup ();
460 tp_proxy_prepare_async (self->priv->manager, NULL,
461 account_manager_ready_cb, self);
463 tp_g_signal_connect_object (self->priv->manager,
464 "most-available-presence-changed",
465 G_CALLBACK (most_available_presence_changed), self, 0);
467 dbus = tp_dbus_daemon_dup (NULL);
469 self->priv->gs_proxy = dbus_g_proxy_new_for_name (
470 tp_proxy_get_dbus_connection (dbus),
471 "org.gnome.SessionManager",
472 "/org/gnome/SessionManager/Presence",
473 "org.gnome.SessionManager.Presence");
475 if (self->priv->gs_proxy)
477 dbus_g_proxy_add_signal (self->priv->gs_proxy, "StatusChanged",
478 G_TYPE_UINT, G_TYPE_INVALID);
479 dbus_g_proxy_connect_signal (self->priv->gs_proxy, "StatusChanged",
480 G_CALLBACK (session_status_changed_cb),
481 self, NULL);
483 else
485 DEBUG ("Failed to get gs proxy");
488 g_object_unref (dbus);
490 self->priv->connect_times = g_hash_table_new (g_direct_hash, g_direct_equal);
493 EmpathyPresenceManager *
494 empathy_presence_manager_dup_singleton (void)
496 return g_object_new (EMPATHY_TYPE_PRESENCE_MANAGER, NULL);
499 TpConnectionPresenceType
500 empathy_presence_manager_get_state (EmpathyPresenceManager *self)
502 if (G_UNLIKELY (!self->priv->ready))
503 g_critical (G_STRLOC ": %s called before AccountManager ready",
504 G_STRFUNC);
506 return self->priv->state;
509 void
510 empathy_presence_manager_set_state (EmpathyPresenceManager *self,
511 TpConnectionPresenceType state)
513 empathy_presence_manager_set_presence (self, state, self->priv->status);
516 void
517 empathy_presence_manager_set_status (EmpathyPresenceManager *self,
518 const gchar *status)
520 empathy_presence_manager_set_presence (self, self->priv->state, status);
523 static void
524 empathy_presence_manager_do_set_presence (EmpathyPresenceManager *self,
525 TpConnectionPresenceType status_type,
526 const gchar *status_message)
528 const gchar *status;
530 g_assert (status_type > 0 && status_type < NUM_TP_CONNECTION_PRESENCE_TYPES);
532 status = presence_type_to_status[status_type];
534 g_return_if_fail (status != NULL);
536 /* We possibly should be sure that the account manager is prepared, but
537 * sometimes this isn't possible, like when exiting. In other words,
538 * we need a callback to empathy_presence_manager_set_presence to be sure the
539 * presence is set on all accounts successfully.
540 * However, in practice, this is fine as we've already prepared the
541 * account manager here in _init. */
542 tp_account_manager_set_all_requested_presences (self->priv->manager,
543 status_type, status, status_message);
546 void
547 empathy_presence_manager_set_presence (EmpathyPresenceManager *self,
548 TpConnectionPresenceType state,
549 const gchar *status)
551 const gchar *default_status;
553 DEBUG ("Changing presence to %s (%d)", status, state);
555 g_free (self->priv->requested_status_message);
556 self->priv->requested_presence_type = state;
557 self->priv->requested_status_message = g_strdup (status);
559 /* Do not set translated default messages */
560 default_status = empathy_presence_get_default_message (state);
561 if (!tp_strdiff (status, default_status))
562 status = NULL;
564 empathy_presence_manager_do_set_presence (self, state, status);
567 gboolean
568 empathy_presence_manager_get_auto_away (EmpathyPresenceManager *self)
570 return self->priv->auto_away;
573 void
574 empathy_presence_manager_set_auto_away (EmpathyPresenceManager *self,
575 gboolean auto_away)
577 self->priv->auto_away = auto_away;
579 g_object_notify (G_OBJECT (self), "auto-away");
582 TpConnectionPresenceType
583 empathy_presence_manager_get_requested_presence (EmpathyPresenceManager *self,
584 gchar **status,
585 gchar **status_message)
587 if (status != NULL)
588 *status = g_strdup (presence_type_to_status[
589 self->priv->requested_presence_type]);
591 if (status_message != NULL)
592 *status_message = g_strdup (self->priv->requested_status_message);
594 return self->priv->requested_presence_type;
597 /* This function returns %TRUE if EmpathyPresenceManager considers the account
598 * @account as having just connected recently. Otherwise, it returns
599 * %FALSE. In doubt, %FALSE is returned. */
600 gboolean
601 empathy_presence_manager_account_is_just_connected (
602 EmpathyPresenceManager *self,
603 TpAccount *account)
605 GTimeVal val;
606 gpointer ptr;
607 glong t;
609 if (tp_account_get_connection_status (account, NULL)
610 != TP_CONNECTION_STATUS_CONNECTED)
611 return FALSE;
613 ptr = g_hash_table_lookup (self->priv->connect_times, account);
615 if (ptr == NULL)
616 return FALSE;
618 t = GPOINTER_TO_INT (ptr);
620 g_get_current_time (&val);
622 return (val.tv_sec - t) < ACCOUNT_IS_JUST_CONNECTED_SECONDS;