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 <nm-client.h>
32 #include <telepathy-glib/dbus.h>
33 #include <telepathy-glib/util.h>
35 #include "empathy-account-manager.h"
36 #include "empathy-idle.h"
37 #include "empathy-utils.h"
39 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
40 #include "empathy-debug.h"
42 /* Number of seconds before entering extended autoaway. */
43 #define EXT_AWAY_TIME (30*60)
45 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIdle)
52 TpConnectionPresenceType state;
54 TpConnectionPresenceType flash_state;
58 TpConnectionPresenceType away_saved_state;
59 TpConnectionPresenceType nm_saved_state;
60 gchar *nm_saved_status;
63 gboolean nm_connected;
64 guint ext_away_timeout;
66 EmpathyAccountManager *manager;
70 SESSION_STATUS_AVAILABLE,
71 SESSION_STATUS_INVISIBLE,
74 SESSION_STATUS_UNKNOWN
86 G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT);
88 static EmpathyIdle * idle_singleton = NULL;
91 idle_presence_changed_cb (EmpathyAccountManager *manager,
92 TpConnectionPresenceType state,
94 gchar *status_message,
97 EmpathyIdlePriv *priv;
99 priv = GET_PRIV (idle);
101 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET)
102 /* Assume our presence is offline if MC reports UNSET */
103 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
105 DEBUG ("Presence changed to '%s' (%d)", status, state);
107 g_free (priv->status);
110 if (!EMP_STR_EMPTY (status)) {
111 priv->status = g_strdup (status);
114 g_object_notify (G_OBJECT (idle), "state");
115 g_object_notify (G_OBJECT (idle), "status");
119 idle_ext_away_cb (EmpathyIdle *idle)
121 EmpathyIdlePriv *priv;
123 priv = GET_PRIV (idle);
125 DEBUG ("Going to extended autoaway");
126 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY);
127 priv->ext_away_timeout = 0;
133 idle_ext_away_stop (EmpathyIdle *idle)
135 EmpathyIdlePriv *priv;
137 priv = GET_PRIV (idle);
139 if (priv->ext_away_timeout) {
140 g_source_remove (priv->ext_away_timeout);
141 priv->ext_away_timeout = 0;
146 idle_ext_away_start (EmpathyIdle *idle)
148 EmpathyIdlePriv *priv;
150 priv = GET_PRIV (idle);
152 if (priv->ext_away_timeout != 0) {
155 priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
156 (GSourceFunc) idle_ext_away_cb,
161 idle_session_status_changed_cb (DBusGProxy *gs_proxy,
162 SessionStatus status,
165 EmpathyIdlePriv *priv;
168 priv = GET_PRIV (idle);
170 is_idle = (status == SESSION_STATUS_IDLE);
172 DEBUG ("Session idle state changed, %s -> %s",
173 priv->is_idle ? "yes" : "no",
174 is_idle ? "yes" : "no");
176 if (!priv->auto_away ||
177 (priv->nm_saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET &&
178 (priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
179 priv->state == TP_CONNECTION_PRESENCE_TYPE_HIDDEN))) {
180 /* We don't want to go auto away OR we explicitely asked to be
181 * offline, nothing to do here */
182 priv->is_idle = is_idle;
186 if (is_idle && !priv->is_idle) {
187 TpConnectionPresenceType new_state;
188 /* We are now idle */
190 idle_ext_away_start (idle);
192 if (priv->nm_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
193 /* We are disconnected, when coming back from away
194 * we want to restore the presence before the
196 priv->away_saved_state = priv->nm_saved_state;
198 priv->away_saved_state = priv->state;
201 new_state = TP_CONNECTION_PRESENCE_TYPE_AWAY;
202 if (priv->state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
203 new_state = TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY;
206 DEBUG ("Going to autoaway. Saved state=%d, new state=%d",
207 priv->away_saved_state, new_state);
208 empathy_idle_set_state (idle, new_state);
209 } else if (!is_idle && priv->is_idle) {
210 const gchar *new_status;
211 /* We are no more idle, restore state */
213 idle_ext_away_stop (idle);
215 if (priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_AWAY ||
216 priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
217 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
220 new_status = priv->status;
223 DEBUG ("Restoring state to %d, reset status to %s",
224 priv->away_saved_state, new_status);
226 empathy_idle_set_presence (idle,
227 priv->away_saved_state,
230 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
233 priv->is_idle = is_idle;
238 idle_nm_state_change_cb (NMClient *client,
239 const GParamSpec *pspec,
242 EmpathyIdlePriv *priv;
243 gboolean old_nm_connected;
244 gboolean new_nm_connected;
247 priv = GET_PRIV (idle);
253 state = nm_client_get_state (priv->nm_client);
254 old_nm_connected = priv->nm_connected;
255 new_nm_connected = !(state == NM_STATE_CONNECTING ||
256 state == NM_STATE_DISCONNECTED);
257 priv->nm_connected = TRUE; /* To be sure _set_state will work */
259 DEBUG ("New network state %d", state);
261 if (old_nm_connected && !new_nm_connected) {
262 /* We are no more connected */
263 DEBUG ("Disconnected: Save state %d (%s)",
264 priv->state, priv->status);
265 priv->nm_saved_state = priv->state;
266 g_free (priv->nm_saved_status);
267 priv->nm_saved_status = g_strdup (priv->status);
268 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
270 else if (!old_nm_connected && new_nm_connected
271 && priv->nm_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
272 /* We are now connected */
273 DEBUG ("Reconnected: Restore state %d (%s)",
274 priv->nm_saved_state, priv->nm_saved_status);
275 empathy_idle_set_presence (idle,
276 priv->nm_saved_state,
277 priv->nm_saved_status);
278 priv->nm_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
279 g_free (priv->nm_saved_status);
280 priv->nm_saved_status = NULL;
283 priv->nm_connected = new_nm_connected;
288 idle_finalize (GObject *object)
290 EmpathyIdlePriv *priv;
292 priv = GET_PRIV (object);
294 g_free (priv->status);
296 if (priv->gs_proxy) {
297 g_object_unref (priv->gs_proxy);
300 if (priv->manager != NULL)
301 g_object_unref (priv->manager);
304 if (priv->nm_client) {
305 g_object_unref (priv->nm_client);
309 idle_ext_away_stop (EMPATHY_IDLE (object));
313 idle_constructor (GType type,
315 GObjectConstructParam *props)
319 if (idle_singleton) {
320 retval = g_object_ref (idle_singleton);
322 retval = G_OBJECT_CLASS (empathy_idle_parent_class)->constructor
323 (type, n_props, props);
325 idle_singleton = EMPATHY_IDLE (retval);
326 g_object_add_weak_pointer (retval, (gpointer) &idle_singleton);
333 idle_get_property (GObject *object,
338 EmpathyIdlePriv *priv;
341 priv = GET_PRIV (object);
342 idle = EMPATHY_IDLE (object);
346 g_value_set_enum (value, empathy_idle_get_state (idle));
349 g_value_set_string (value, empathy_idle_get_status (idle));
351 case PROP_FLASH_STATE:
352 g_value_set_enum (value, empathy_idle_get_flash_state (idle));
355 g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
358 g_value_set_boolean (value, empathy_idle_get_use_nm (idle));
361 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
367 idle_set_property (GObject *object,
372 EmpathyIdlePriv *priv;
375 priv = GET_PRIV (object);
376 idle = EMPATHY_IDLE (object);
380 empathy_idle_set_state (idle, g_value_get_enum (value));
383 empathy_idle_set_status (idle, g_value_get_string (value));
385 case PROP_FLASH_STATE:
386 empathy_idle_set_flash_state (idle, g_value_get_enum (value));
389 empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
392 empathy_idle_set_use_nm (idle, g_value_get_boolean (value));
395 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
401 empathy_idle_class_init (EmpathyIdleClass *klass)
403 GObjectClass *object_class = G_OBJECT_CLASS (klass);
405 object_class->finalize = idle_finalize;
406 object_class->constructor = idle_constructor;
407 object_class->get_property = idle_get_property;
408 object_class->set_property = idle_set_property;
410 g_object_class_install_property (object_class,
412 g_param_spec_uint ("state",
415 0, NUM_TP_CONNECTION_PRESENCE_TYPES,
416 TP_CONNECTION_PRESENCE_TYPE_UNSET,
418 g_object_class_install_property (object_class,
420 g_param_spec_string ("status",
425 g_object_class_install_property (object_class,
427 g_param_spec_uint ("flash-state",
430 0, NUM_TP_CONNECTION_PRESENCE_TYPES,
431 TP_CONNECTION_PRESENCE_TYPE_UNSET,
434 g_object_class_install_property (object_class,
436 g_param_spec_boolean ("auto-away",
437 "Automatic set presence to away",
438 "Should it set presence to away if inactive",
442 g_object_class_install_property (object_class,
444 g_param_spec_boolean ("use-nm",
445 "Use Network Manager",
446 "Set presence according to Network Manager",
448 G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
450 g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
454 empathy_idle_init (EmpathyIdle *idle)
456 EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
457 EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
460 priv->is_idle = FALSE;
462 priv->manager = empathy_account_manager_dup_singleton ();
463 priv->state = empathy_account_manager_get_global_presence (priv->manager,
464 NULL, &priv->status);
467 g_signal_connect (priv->manager, "global-presence-changed",
468 G_CALLBACK (idle_presence_changed_cb), idle);
470 priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
471 "org.gnome.SessionManager",
472 "/org/gnome/SessionManager/Presence",
473 "org.gnome.SessionManager.Presence");
474 if (priv->gs_proxy) {
475 dbus_g_proxy_add_signal (priv->gs_proxy, "StatusChanged",
476 G_TYPE_UINT, G_TYPE_INVALID);
477 dbus_g_proxy_connect_signal (priv->gs_proxy, "StatusChanged",
478 G_CALLBACK (idle_session_status_changed_cb),
481 DEBUG ("Failed to get gs proxy");
485 priv->nm_client = nm_client_new ();
486 if (priv->nm_client) {
487 g_signal_connect (priv->nm_client, "notify::" NM_CLIENT_STATE,
488 G_CALLBACK (idle_nm_state_change_cb),
491 DEBUG ("Failed to get nm proxy");
497 empathy_idle_dup_singleton (void)
499 return g_object_new (EMPATHY_TYPE_IDLE, NULL);
502 TpConnectionPresenceType
503 empathy_idle_get_state (EmpathyIdle *idle)
505 EmpathyIdlePriv *priv;
507 priv = GET_PRIV (idle);
513 empathy_idle_set_state (EmpathyIdle *idle,
514 TpConnectionPresenceType state)
516 EmpathyIdlePriv *priv;
518 priv = GET_PRIV (idle);
520 empathy_idle_set_presence (idle, state, priv->status);
524 empathy_idle_get_status (EmpathyIdle *idle)
526 EmpathyIdlePriv *priv;
528 priv = GET_PRIV (idle);
531 return empathy_presence_get_default_message (priv->state);
538 empathy_idle_set_status (EmpathyIdle *idle,
541 EmpathyIdlePriv *priv;
543 priv = GET_PRIV (idle);
545 empathy_idle_set_presence (idle, priv->state, status);
548 TpConnectionPresenceType
549 empathy_idle_get_flash_state (EmpathyIdle *idle)
551 EmpathyIdlePriv *priv;
553 priv = GET_PRIV (idle);
555 return priv->flash_state;
559 empathy_idle_set_flash_state (EmpathyIdle *idle,
560 TpConnectionPresenceType state)
562 EmpathyIdlePriv *priv;
564 priv = GET_PRIV (idle);
566 priv->flash_state = state;
568 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
571 g_object_notify (G_OBJECT (idle), "flash-state");
575 empathy_idle_do_set_presence (EmpathyIdle *idle,
576 TpConnectionPresenceType status_type,
577 const gchar *status_message)
579 EmpathyIdlePriv *priv = GET_PRIV (idle);
580 const gchar *statusses[NUM_TP_CONNECTION_PRESENCE_TYPES] = {
593 g_assert (status_type > 0 && status_type < NUM_TP_CONNECTION_PRESENCE_TYPES);
595 status = statusses[status_type];
597 g_return_if_fail (status != NULL);
599 empathy_account_manager_request_global_presence (priv->manager,
600 status_type, status, status_message);
604 empathy_idle_set_presence (EmpathyIdle *idle,
605 TpConnectionPresenceType state,
608 EmpathyIdlePriv *priv;
609 const gchar *default_status;
611 priv = GET_PRIV (idle);
613 DEBUG ("Changing presence to %s (%d)", status, state);
615 /* Do not set translated default messages */
616 default_status = empathy_presence_get_default_message (state);
617 if (!tp_strdiff (status, default_status)) {
621 if (!priv->nm_connected) {
622 DEBUG ("NM not connected");
624 priv->nm_saved_state = state;
625 if (tp_strdiff (priv->status, status)) {
626 g_free (priv->status);
628 if (!EMP_STR_EMPTY (status)) {
629 priv->status = g_strdup (status);
631 g_object_notify (G_OBJECT (idle), "status");
637 empathy_idle_do_set_presence (idle, state, status);
641 empathy_idle_get_auto_away (EmpathyIdle *idle)
643 EmpathyIdlePriv *priv = GET_PRIV (idle);
645 return priv->auto_away;
649 empathy_idle_set_auto_away (EmpathyIdle *idle,
652 EmpathyIdlePriv *priv = GET_PRIV (idle);
654 priv->auto_away = auto_away;
656 g_object_notify (G_OBJECT (idle), "auto-away");
660 empathy_idle_get_use_nm (EmpathyIdle *idle)
662 EmpathyIdlePriv *priv = GET_PRIV (idle);
668 empathy_idle_set_use_nm (EmpathyIdle *idle,
671 EmpathyIdlePriv *priv = GET_PRIV (idle);
674 if (!priv->nm_client || use_nm == priv->use_nm) {
679 priv->use_nm = use_nm;
683 idle_nm_state_change_cb (priv->nm_client, NULL, idle);
688 priv->nm_connected = TRUE;
689 if (priv->nm_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
690 empathy_idle_set_state (idle, priv->nm_saved_state);
692 priv->nm_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
695 g_object_notify (G_OBJECT (idle), "use-nm");