Add an EmpathyIndividualMenu::link-contacts-activated signal
[empathy-mirror.git] / libempathy / empathy-idle.c
blob12686daa4e449472395bb4f8cee6fb611d3ac43e
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
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>
22 #include <config.h>
24 #include <string.h>
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
44 * for. */
45 #define ACCOUNT_IS_JUST_CONNECTED_SECONDS 10
47 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIdle)
48 typedef struct {
49 DBusGProxy *gs_proxy;
50 EmpathyConnectivity *connectivity;
51 gulong state_change_signal_id;
53 gboolean ready;
55 TpConnectionPresenceType state;
56 gchar *status;
57 gboolean auto_away;
59 TpConnectionPresenceType away_saved_state;
60 TpConnectionPresenceType saved_state;
61 gchar *saved_status;
63 gboolean is_idle;
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;
75 } EmpathyIdlePriv;
77 typedef enum {
78 SESSION_STATUS_AVAILABLE,
79 SESSION_STATUS_INVISIBLE,
80 SESSION_STATUS_BUSY,
81 SESSION_STATUS_IDLE,
82 SESSION_STATUS_UNKNOWN
83 } SessionStatus;
85 enum {
86 PROP_0,
87 PROP_STATE,
88 PROP_STATUS,
89 PROP_AUTO_AWAY
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] = {
97 NULL,
98 "offline",
99 "available",
100 "away",
101 "xa",
102 "hidden",
103 "busy",
104 NULL,
105 NULL,
108 static void
109 idle_presence_changed_cb (TpAccountManager *manager,
110 TpConnectionPresenceType state,
111 gchar *status,
112 gchar *status_message,
113 EmpathyIdle *idle)
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,
124 status_message);
126 g_free (priv->status);
127 priv->state = state;
128 if (EMP_STR_EMPTY (status_message))
129 priv->status = NULL;
130 else
131 priv->status = g_strdup (status_message);
133 g_object_notify (G_OBJECT (idle), "state");
134 g_object_notify (G_OBJECT (idle), "status");
137 static gboolean
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;
148 return FALSE;
151 static void
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;
164 static void
165 idle_ext_away_start (EmpathyIdle *idle)
167 EmpathyIdlePriv *priv;
169 priv = GET_PRIV (idle);
171 if (priv->ext_away_timeout != 0) {
172 return;
174 priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
175 (GSourceFunc) idle_ext_away_cb,
176 idle);
179 static void
180 idle_session_status_changed_cb (DBusGProxy *gs_proxy,
181 SessionStatus status,
182 EmpathyIdle *idle)
184 EmpathyIdlePriv *priv;
185 gboolean is_idle;
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;
202 return;
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
214 * disconnection. */
215 priv->away_saved_state = priv->saved_state;
216 } else {
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);
243 } else {
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;
255 static void
256 idle_state_change_cb (EmpathyConnectivity *connectivity,
257 gboolean new_online,
258 EmpathyIdle *idle)
260 EmpathyIdlePriv *priv;
262 priv = GET_PRIV (idle);
264 if (!new_online) {
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);
273 else if (new_online
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,
279 priv->saved_state,
280 priv->saved_status);
281 priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
282 g_free (priv->saved_status);
283 priv->saved_status = NULL;
287 static void
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));
319 static GObject *
320 idle_constructor (GType type,
321 guint n_props,
322 GObjectConstructParam *props)
324 GObject *retval;
326 if (idle_singleton) {
327 retval = g_object_ref (idle_singleton);
328 } else {
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);
336 return retval;
339 static const gchar *
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",
348 G_STRFUNC);
350 if (!priv->status) {
351 return empathy_presence_get_default_message (priv->state);
354 return priv->status;
357 static void
358 idle_get_property (GObject *object,
359 guint param_id,
360 GValue *value,
361 GParamSpec *pspec)
363 EmpathyIdlePriv *priv;
364 EmpathyIdle *idle;
366 priv = GET_PRIV (object);
367 idle = EMPATHY_IDLE (object);
369 switch (param_id) {
370 case PROP_STATE:
371 g_value_set_enum (value, empathy_idle_get_state (idle));
372 break;
373 case PROP_STATUS:
374 g_value_set_string (value, empathy_idle_get_status (idle));
375 break;
376 case PROP_AUTO_AWAY:
377 g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
378 break;
379 default:
380 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
381 break;
385 static void
386 idle_set_property (GObject *object,
387 guint param_id,
388 const GValue *value,
389 GParamSpec *pspec)
391 EmpathyIdlePriv *priv;
392 EmpathyIdle *idle;
394 priv = GET_PRIV (object);
395 idle = EMPATHY_IDLE (object);
397 switch (param_id) {
398 case PROP_STATE:
399 empathy_idle_set_state (idle, g_value_get_enum (value));
400 break;
401 case PROP_STATUS:
402 empathy_idle_set_status (idle, g_value_get_string (value));
403 break;
404 case PROP_AUTO_AWAY:
405 empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
406 break;
407 default:
408 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
409 break;
413 static void
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,
424 PROP_STATE,
425 g_param_spec_uint ("state",
426 "state",
427 "state",
428 0, NUM_TP_CONNECTION_PRESENCE_TYPES,
429 TP_CONNECTION_PRESENCE_TYPE_UNSET,
430 G_PARAM_READWRITE));
431 g_object_class_install_property (object_class,
432 PROP_STATUS,
433 g_param_spec_string ("status",
434 "status",
435 "status",
436 NULL,
437 G_PARAM_READWRITE));
439 g_object_class_install_property (object_class,
440 PROP_AUTO_AWAY,
441 g_param_spec_boolean ("auto-away",
442 "Automatic set presence to away",
443 "Should it set presence to away if inactive",
444 FALSE,
445 G_PARAM_READWRITE));
447 g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
450 static void
451 account_status_changed_cb (TpAccount *account,
452 guint old_status,
453 guint new_status,
454 guint reason,
455 gchar *dbus_error_name,
456 GHashTable *details,
457 gpointer user_data)
459 EmpathyIdle *idle = EMPATHY_IDLE (user_data);
460 EmpathyIdlePriv *priv = GET_PRIV (idle);
461 GTimeVal val;
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);
472 static void
473 account_manager_ready_cb (GObject *source_object,
474 GAsyncResult *result,
475 gpointer user_data)
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;
482 GList *accounts, *l;
483 GError *error = NULL;
485 /* In case we've been finalized before reading this callback */
486 if (idle_singleton == NULL)
487 return;
489 priv = GET_PRIV (idle);
490 priv->ready = TRUE;
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);
495 return;
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),
508 idle, 0);
510 g_list_free (accounts);
512 g_free (status);
513 g_free (status_message);
516 static void
517 empathy_idle_init (EmpathyIdle *idle)
519 EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
520 EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
521 TpDBusDaemon *dbus;
523 idle->priv = priv;
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),
547 idle, NULL);
548 } else {
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);
561 EmpathyIdle *
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",
576 G_STRFUNC);
578 return priv->state;
581 void
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);
592 void
593 empathy_idle_set_status (EmpathyIdle *idle,
594 const gchar *status)
596 EmpathyIdlePriv *priv;
598 priv = GET_PRIV (idle);
600 empathy_idle_set_presence (idle, priv->state, status);
603 static void
604 empathy_idle_do_set_presence (EmpathyIdle *idle,
605 TpConnectionPresenceType status_type,
606 const gchar *status_message)
608 EmpathyIdlePriv *priv = GET_PRIV (idle);
609 const gchar *status;
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);
627 void
628 empathy_idle_set_presence (EmpathyIdle *idle,
629 TpConnectionPresenceType state,
630 const gchar *status)
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)) {
646 status = NULL;
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);
661 return;
664 empathy_idle_do_set_presence (idle, state, status);
667 gboolean
668 empathy_idle_get_auto_away (EmpathyIdle *idle)
670 EmpathyIdlePriv *priv = GET_PRIV (idle);
672 return priv->auto_away;
675 void
676 empathy_idle_set_auto_away (EmpathyIdle *idle,
677 gboolean auto_away)
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,
688 gchar **status,
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. */
707 gboolean
708 empathy_idle_account_is_just_connected (EmpathyIdle *idle,
709 TpAccount *account)
711 EmpathyIdlePriv *priv = GET_PRIV (idle);
712 GTimeVal val;
713 gpointer ptr;
714 glong t;
716 if (tp_account_get_connection_status (account, NULL)
717 != TP_CONNECTION_STATUS_CONNECTED) {
718 return FALSE;
721 ptr = g_hash_table_lookup (priv->connect_times, account);
723 if (ptr == NULL) {
724 return FALSE;
727 t = GPOINTER_TO_INT (ptr);
729 g_get_current_time (&val);
731 return (val.tv_sec - t) < ACCOUNT_IS_JUST_CONNECTED_SECONDS;