1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
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>
26 #include <glib/gi18n-lib.h>
27 #include <dbus/dbus-glib.h>
29 #include <telepathy-glib/dbus.h>
30 #include <telepathy-glib/util.h>
32 #include "empathy-account-manager.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 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIdle)
46 EmpathyConnectivity *connectivity;
47 gulong state_change_signal_id;
49 TpConnectionPresenceType state;
51 TpConnectionPresenceType flash_state;
54 TpConnectionPresenceType away_saved_state;
55 TpConnectionPresenceType saved_state;
59 guint ext_away_timeout;
61 EmpathyAccountManager *manager;
65 SESSION_STATUS_AVAILABLE,
66 SESSION_STATUS_INVISIBLE,
69 SESSION_STATUS_UNKNOWN
80 G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT);
82 static EmpathyIdle * idle_singleton = NULL;
85 idle_presence_changed_cb (EmpathyAccountManager *manager,
86 TpConnectionPresenceType state,
88 gchar *status_message,
91 EmpathyIdlePriv *priv;
93 priv = GET_PRIV (idle);
95 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET)
96 /* Assume our presence is offline if MC reports UNSET */
97 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
99 DEBUG ("Presence changed to '%s' (%d) \"%s\"", status, state,
102 g_free (priv->status);
104 if (EMP_STR_EMPTY (status_message))
107 priv->status = g_strdup (status_message);
109 g_object_notify (G_OBJECT (idle), "state");
110 g_object_notify (G_OBJECT (idle), "status");
114 idle_ext_away_cb (EmpathyIdle *idle)
116 EmpathyIdlePriv *priv;
118 priv = GET_PRIV (idle);
120 DEBUG ("Going to extended autoaway");
121 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY);
122 priv->ext_away_timeout = 0;
128 idle_ext_away_stop (EmpathyIdle *idle)
130 EmpathyIdlePriv *priv;
132 priv = GET_PRIV (idle);
134 if (priv->ext_away_timeout) {
135 g_source_remove (priv->ext_away_timeout);
136 priv->ext_away_timeout = 0;
141 idle_ext_away_start (EmpathyIdle *idle)
143 EmpathyIdlePriv *priv;
145 priv = GET_PRIV (idle);
147 if (priv->ext_away_timeout != 0) {
150 priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
151 (GSourceFunc) idle_ext_away_cb,
156 idle_session_status_changed_cb (DBusGProxy *gs_proxy,
157 SessionStatus status,
160 EmpathyIdlePriv *priv;
163 priv = GET_PRIV (idle);
165 is_idle = (status == SESSION_STATUS_IDLE);
167 DEBUG ("Session idle state changed, %s -> %s",
168 priv->is_idle ? "yes" : "no",
169 is_idle ? "yes" : "no");
171 if (!priv->auto_away ||
172 (priv->saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET &&
173 (priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
174 priv->state == TP_CONNECTION_PRESENCE_TYPE_HIDDEN))) {
175 /* We don't want to go auto away OR we explicitely asked to be
176 * offline, nothing to do here */
177 priv->is_idle = is_idle;
181 if (is_idle && !priv->is_idle) {
182 TpConnectionPresenceType new_state;
183 /* We are now idle */
185 idle_ext_away_start (idle);
187 if (priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
188 /* We are disconnected, when coming back from away
189 * we want to restore the presence before the
191 priv->away_saved_state = priv->saved_state;
193 priv->away_saved_state = priv->state;
196 new_state = TP_CONNECTION_PRESENCE_TYPE_AWAY;
197 if (priv->state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
198 new_state = TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY;
201 DEBUG ("Going to autoaway. Saved state=%d, new state=%d",
202 priv->away_saved_state, new_state);
203 empathy_idle_set_state (idle, new_state);
204 } else if (!is_idle && priv->is_idle) {
205 const gchar *new_status;
206 /* We are no more idle, restore state */
208 idle_ext_away_stop (idle);
210 if (priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_AWAY ||
211 priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
212 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
215 new_status = priv->status;
218 DEBUG ("Restoring state to %d, reset status to %s",
219 priv->away_saved_state, new_status);
221 empathy_idle_set_presence (idle,
222 priv->away_saved_state,
225 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
228 priv->is_idle = is_idle;
232 idle_state_change_cb (EmpathyConnectivity *connectivity,
236 EmpathyIdlePriv *priv;
238 priv = GET_PRIV (idle);
241 /* We are no longer connected */
242 DEBUG ("Disconnected: Save state %d (%s)",
243 priv->state, priv->status);
244 priv->saved_state = priv->state;
245 g_free (priv->saved_status);
246 priv->saved_status = g_strdup (priv->status);
247 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
250 && priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
251 /* We are now connected */
252 DEBUG ("Reconnected: Restore state %d (%s)",
253 priv->saved_state, priv->saved_status);
254 empathy_idle_set_presence (idle,
257 priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
258 g_free (priv->saved_status);
259 priv->saved_status = NULL;
264 idle_finalize (GObject *object)
266 EmpathyIdlePriv *priv;
268 priv = GET_PRIV (object);
270 g_free (priv->status);
272 if (priv->gs_proxy) {
273 g_object_unref (priv->gs_proxy);
276 g_signal_handler_disconnect (priv->connectivity,
277 priv->state_change_signal_id);
278 priv->state_change_signal_id = 0;
280 g_object_unref (priv->connectivity);
282 idle_ext_away_stop (EMPATHY_IDLE (object));
286 idle_constructor (GType type,
288 GObjectConstructParam *props)
292 if (idle_singleton) {
293 retval = g_object_ref (idle_singleton);
295 retval = G_OBJECT_CLASS (empathy_idle_parent_class)->constructor
296 (type, n_props, props);
298 idle_singleton = EMPATHY_IDLE (retval);
299 g_object_add_weak_pointer (retval, (gpointer) &idle_singleton);
306 idle_get_property (GObject *object,
311 EmpathyIdlePriv *priv;
314 priv = GET_PRIV (object);
315 idle = EMPATHY_IDLE (object);
319 g_value_set_enum (value, empathy_idle_get_state (idle));
322 g_value_set_string (value, empathy_idle_get_status (idle));
324 case PROP_FLASH_STATE:
325 g_value_set_enum (value, empathy_idle_get_flash_state (idle));
328 g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
331 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
337 idle_set_property (GObject *object,
342 EmpathyIdlePriv *priv;
345 priv = GET_PRIV (object);
346 idle = EMPATHY_IDLE (object);
350 empathy_idle_set_state (idle, g_value_get_enum (value));
353 empathy_idle_set_status (idle, g_value_get_string (value));
355 case PROP_FLASH_STATE:
356 empathy_idle_set_flash_state (idle, g_value_get_enum (value));
359 empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
362 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
368 empathy_idle_class_init (EmpathyIdleClass *klass)
370 GObjectClass *object_class = G_OBJECT_CLASS (klass);
372 object_class->finalize = idle_finalize;
373 object_class->constructor = idle_constructor;
374 object_class->get_property = idle_get_property;
375 object_class->set_property = idle_set_property;
377 g_object_class_install_property (object_class,
379 g_param_spec_uint ("state",
382 0, NUM_TP_CONNECTION_PRESENCE_TYPES,
383 TP_CONNECTION_PRESENCE_TYPE_UNSET,
385 g_object_class_install_property (object_class,
387 g_param_spec_string ("status",
392 g_object_class_install_property (object_class,
394 g_param_spec_uint ("flash-state",
397 0, NUM_TP_CONNECTION_PRESENCE_TYPES,
398 TP_CONNECTION_PRESENCE_TYPE_UNSET,
401 g_object_class_install_property (object_class,
403 g_param_spec_boolean ("auto-away",
404 "Automatic set presence to away",
405 "Should it set presence to away if inactive",
409 g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
413 empathy_idle_init (EmpathyIdle *idle)
415 EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
416 EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
419 priv->is_idle = FALSE;
421 priv->manager = empathy_account_manager_dup_singleton ();
422 priv->state = empathy_account_manager_get_global_presence (priv->manager,
423 NULL, &priv->status);
426 g_signal_connect (priv->manager, "global-presence-changed",
427 G_CALLBACK (idle_presence_changed_cb), idle);
429 priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
430 "org.gnome.SessionManager",
431 "/org/gnome/SessionManager/Presence",
432 "org.gnome.SessionManager.Presence");
433 if (priv->gs_proxy) {
434 dbus_g_proxy_add_signal (priv->gs_proxy, "StatusChanged",
435 G_TYPE_UINT, G_TYPE_INVALID);
436 dbus_g_proxy_connect_signal (priv->gs_proxy, "StatusChanged",
437 G_CALLBACK (idle_session_status_changed_cb),
440 DEBUG ("Failed to get gs proxy");
443 priv->connectivity = empathy_connectivity_dup_singleton ();
444 priv->state_change_signal_id = g_signal_connect (priv->connectivity,
445 "state-change", G_CALLBACK (idle_state_change_cb), idle);
449 empathy_idle_dup_singleton (void)
451 return g_object_new (EMPATHY_TYPE_IDLE, NULL);
454 TpConnectionPresenceType
455 empathy_idle_get_state (EmpathyIdle *idle)
457 EmpathyIdlePriv *priv;
459 priv = GET_PRIV (idle);
465 empathy_idle_set_state (EmpathyIdle *idle,
466 TpConnectionPresenceType state)
468 EmpathyIdlePriv *priv;
470 priv = GET_PRIV (idle);
472 empathy_idle_set_presence (idle, state, priv->status);
476 empathy_idle_get_status (EmpathyIdle *idle)
478 EmpathyIdlePriv *priv;
480 priv = GET_PRIV (idle);
483 return empathy_presence_get_default_message (priv->state);
490 empathy_idle_set_status (EmpathyIdle *idle,
493 EmpathyIdlePriv *priv;
495 priv = GET_PRIV (idle);
497 empathy_idle_set_presence (idle, priv->state, status);
500 TpConnectionPresenceType
501 empathy_idle_get_flash_state (EmpathyIdle *idle)
503 EmpathyIdlePriv *priv;
505 priv = GET_PRIV (idle);
507 return priv->flash_state;
511 empathy_idle_set_flash_state (EmpathyIdle *idle,
512 TpConnectionPresenceType state)
514 EmpathyIdlePriv *priv;
516 priv = GET_PRIV (idle);
518 priv->flash_state = state;
520 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
523 g_object_notify (G_OBJECT (idle), "flash-state");
527 empathy_idle_do_set_presence (EmpathyIdle *idle,
528 TpConnectionPresenceType status_type,
529 const gchar *status_message)
531 EmpathyIdlePriv *priv = GET_PRIV (idle);
532 const gchar *statuses[NUM_TP_CONNECTION_PRESENCE_TYPES] = {
545 g_assert (status_type > 0 && status_type < NUM_TP_CONNECTION_PRESENCE_TYPES);
547 status = statuses[status_type];
549 g_return_if_fail (status != NULL);
551 empathy_account_manager_request_global_presence (priv->manager,
552 status_type, status, status_message);
556 empathy_idle_set_presence (EmpathyIdle *idle,
557 TpConnectionPresenceType state,
560 EmpathyIdlePriv *priv;
561 const gchar *default_status;
563 priv = GET_PRIV (idle);
565 DEBUG ("Changing presence to %s (%d)", status, state);
567 /* Do not set translated default messages */
568 default_status = empathy_presence_get_default_message (state);
569 if (!tp_strdiff (status, default_status)) {
573 if (!empathy_connectivity_is_online (priv->connectivity)) {
574 DEBUG ("Empathy is not online");
576 if (tp_strdiff (priv->status, status)) {
577 g_free (priv->status);
579 if (!EMP_STR_EMPTY (status)) {
580 priv->status = g_strdup (status);
582 g_object_notify (G_OBJECT (idle), "status");
586 empathy_idle_do_set_presence (idle, state, status);
590 empathy_idle_get_auto_away (EmpathyIdle *idle)
592 EmpathyIdlePriv *priv = GET_PRIV (idle);
594 return priv->auto_away;
598 empathy_idle_set_auto_away (EmpathyIdle *idle,
601 EmpathyIdlePriv *priv = GET_PRIV (idle);
603 priv->auto_away = auto_away;
605 g_object_notify (G_OBJECT (idle), "auto-away");