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>
23 #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"
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
44 #define ACCOUNT_IS_JUST_CONNECTED_SECONDS 10
46 struct _EmpathyPresenceManagerPrivate
52 TpConnectionPresenceType state
;
56 TpConnectionPresenceType away_saved_state
;
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
;
72 SESSION_STATUS_AVAILABLE
,
73 SESSION_STATUS_INVISIBLE
,
76 SESSION_STATUS_UNKNOWN
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
] =
105 most_available_presence_changed (TpAccountManager
*manager
,
106 TpConnectionPresenceType state
,
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
,
118 g_free (self
->priv
->status
);
119 self
->priv
->state
= state
;
120 if (EMP_STR_EMPTY (status_message
))
121 self
->priv
->status
= NULL
;
123 self
->priv
->status
= g_strdup (status_message
);
125 g_object_notify (G_OBJECT (self
), "state");
126 g_object_notify (G_OBJECT (self
), "status");
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;
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;
151 ext_away_start (EmpathyPresenceManager
*self
)
153 if (self
->priv
->ext_away_timeout
!= 0)
156 self
->priv
->ext_away_timeout
= g_timeout_add_seconds (EXT_AWAY_TIME
,
157 (GSourceFunc
) ext_away_cb
, self
);
161 session_status_changed_cb (DBusGProxy
*gs_proxy
,
162 SessionStatus status
,
163 EmpathyPresenceManager
*self
)
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
;
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
);
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
;
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
);
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
);
259 presence_manager_constructor (GType type
,
261 GObjectConstructParam
*props
)
267 retval
= g_object_ref (singleton
);
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
);
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",
288 if (!self
->priv
->status
)
289 return empathy_presence_get_default_message (self
->priv
->state
);
291 return self
->priv
->status
;
295 presence_manager_get_property (GObject
*object
,
300 EmpathyPresenceManager
*self
= EMPATHY_PRESENCE_MANAGER (object
);
305 g_value_set_enum (value
, empathy_presence_manager_get_state (self
));
308 g_value_set_string (value
, empathy_presence_manager_get_status (self
));
311 g_value_set_boolean (value
,
312 empathy_presence_manager_get_auto_away (self
));
315 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
321 presence_manager_set_property (GObject
*object
,
326 EmpathyPresenceManager
*self
= EMPATHY_PRESENCE_MANAGER (object
);
331 empathy_presence_manager_set_state (self
, g_value_get_enum (value
));
334 empathy_presence_manager_set_status (self
, g_value_get_string (value
));
337 empathy_presence_manager_set_auto_away (self
,
338 g_value_get_boolean (value
));
341 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
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
,
359 g_param_spec_uint ("state", "state", "state",
360 0, NUM_TP_CONNECTION_PRESENCE_TYPES
,
361 TP_CONNECTION_PRESENCE_TYPE_UNSET
,
364 g_object_class_install_property (object_class
,
366 g_param_spec_string ("status","status", "status",
370 g_object_class_install_property (object_class
,
372 g_param_spec_boolean ("auto-away", "Automatic set presence to away",
373 "Should it set presence to away if inactive",
377 g_type_class_add_private (object_class
,
378 sizeof (EmpathyPresenceManagerPrivate
));
382 account_status_changed_cb (TpAccount
*account
,
386 gchar
*dbus_error_name
,
390 EmpathyPresenceManager
*self
= EMPATHY_PRESENCE_MANAGER (user_data
);
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
);
406 account_manager_ready_cb (GObject
*source_object
,
407 GAsyncResult
*result
,
410 EmpathyPresenceManager
*self
= user_data
;
411 TpAccountManager
*account_manager
= TP_ACCOUNT_MANAGER (source_object
);
412 TpConnectionPresenceType state
;
413 gchar
*status
, *status_message
;
415 GError
*error
= NULL
;
417 /* In case we've been finalized before reading this callback */
418 if (singleton
== NULL
)
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
);
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
);
445 g_free (status_message
);
449 empathy_presence_manager_init (EmpathyPresenceManager
*self
)
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
),
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",
506 return self
->priv
->state
;
510 empathy_presence_manager_set_state (EmpathyPresenceManager
*self
,
511 TpConnectionPresenceType state
)
513 empathy_presence_manager_set_presence (self
, state
, self
->priv
->status
);
517 empathy_presence_manager_set_status (EmpathyPresenceManager
*self
,
520 empathy_presence_manager_set_presence (self
, self
->priv
->state
, status
);
524 empathy_presence_manager_do_set_presence (EmpathyPresenceManager
*self
,
525 TpConnectionPresenceType status_type
,
526 const gchar
*status_message
)
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
);
547 empathy_presence_manager_set_presence (EmpathyPresenceManager
*self
,
548 TpConnectionPresenceType state
,
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
))
564 empathy_presence_manager_do_set_presence (self
, state
, status
);
568 empathy_presence_manager_get_auto_away (EmpathyPresenceManager
*self
)
570 return self
->priv
->auto_away
;
574 empathy_presence_manager_set_auto_away (EmpathyPresenceManager
*self
,
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
,
585 gchar
**status_message
)
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. */
601 empathy_presence_manager_account_is_just_connected (
602 EmpathyPresenceManager
*self
,
609 if (tp_account_get_connection_status (account
, NULL
)
610 != TP_CONNECTION_STATUS_CONNECTED
)
613 ptr
= g_hash_table_lookup (self
->priv
->connect_times
, account
);
618 t
= GPOINTER_TO_INT (ptr
);
620 g_get_current_time (&val
);
622 return (val
.tv_sec
- t
) < ACCOUNT_IS_JUST_CONNECTED_SECONDS
;