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>
31 #include <libmissioncontrol/mc-enum-types.h>
33 #include "empathy-idle.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 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIdle)
48 TpConnectionPresenceType state;
50 TpConnectionPresenceType flash_state;
54 TpConnectionPresenceType away_saved_state;
55 TpConnectionPresenceType nm_saved_state;
56 gchar *nm_saved_status;
59 gboolean nm_connected;
60 guint ext_away_timeout;
80 G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT);
82 static EmpathyIdle * idle_singleton = NULL;
85 idle_presence_changed_cb (MissionControl *mc,
86 TpConnectionPresenceType state,
90 EmpathyIdlePriv *priv;
92 priv = GET_PRIV (idle);
94 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET)
95 /* Assume our presence is offline if MC reports UNSET */
96 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
98 DEBUG ("Presence changed to '%s' (%d)", status, state);
100 g_free (priv->status);
103 if (!EMP_STR_EMPTY (status)) {
104 priv->status = g_strdup (status);
107 g_object_notify (G_OBJECT (idle), "state");
108 g_object_notify (G_OBJECT (idle), "status");
112 idle_ext_away_cb (EmpathyIdle *idle)
114 EmpathyIdlePriv *priv;
116 priv = GET_PRIV (idle);
118 DEBUG ("Going to extended autoaway");
119 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY);
120 priv->ext_away_timeout = 0;
126 idle_ext_away_stop (EmpathyIdle *idle)
128 EmpathyIdlePriv *priv;
130 priv = GET_PRIV (idle);
132 if (priv->ext_away_timeout) {
133 g_source_remove (priv->ext_away_timeout);
134 priv->ext_away_timeout = 0;
139 idle_ext_away_start (EmpathyIdle *idle)
141 EmpathyIdlePriv *priv;
143 priv = GET_PRIV (idle);
145 if (priv->ext_away_timeout != 0) {
148 priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
149 (GSourceFunc) idle_ext_away_cb,
154 idle_session_idle_changed_cb (DBusGProxy *gs_proxy,
158 EmpathyIdlePriv *priv;
160 priv = GET_PRIV (idle);
162 DEBUG ("Session idle state changed, %s -> %s",
163 priv->is_idle ? "yes" : "no",
164 is_idle ? "yes" : "no");
166 if (!priv->auto_away ||
167 (priv->nm_saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET &&
168 (priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
169 priv->state == TP_CONNECTION_PRESENCE_TYPE_HIDDEN))) {
170 /* We don't want to go auto away OR we explicitely asked to be
171 * offline, nothing to do here */
172 priv->is_idle = is_idle;
176 if (is_idle && !priv->is_idle) {
177 TpConnectionPresenceType new_state;
178 /* We are now idle */
180 idle_ext_away_start (idle);
182 if (priv->nm_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
183 /* We are disconnected, when coming back from away
184 * we want to restore the presence before the
186 priv->away_saved_state = priv->nm_saved_state;
188 priv->away_saved_state = priv->state;
191 new_state = TP_CONNECTION_PRESENCE_TYPE_AWAY;
192 if (priv->state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
193 new_state = TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY;
196 DEBUG ("Going to autoaway. Saved state=%d, new state=%d",
197 priv->away_saved_state, new_state);
198 empathy_idle_set_state (idle, new_state);
199 } else if (!is_idle && priv->is_idle) {
200 const gchar *new_status;
201 /* We are no more idle, restore state */
203 idle_ext_away_stop (idle);
205 if (priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_AWAY ||
206 priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
207 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
210 new_status = priv->status;
213 DEBUG ("Restoring state to %d, reset status to %s",
214 priv->away_saved_state, new_status);
216 empathy_idle_set_presence (idle,
217 priv->away_saved_state,
220 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
223 priv->is_idle = is_idle;
227 idle_nm_state_change_cb (DBusGProxy *proxy,
231 EmpathyIdlePriv *priv;
232 gboolean old_nm_connected;
233 gboolean new_nm_connected;
235 priv = GET_PRIV (idle);
238 || priv->nm_saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
242 old_nm_connected = priv->nm_connected;
243 new_nm_connected = !(state == NM_STATE_CONNECTING ||
244 state == NM_STATE_DISCONNECTED);
245 priv->nm_connected = TRUE; /* To be sure _set_state will work */
247 DEBUG ("New network state %d", state);
249 if (old_nm_connected && !new_nm_connected) {
250 /* We are no more connected */
251 DEBUG ("Disconnected: Save state %d (%s)",
252 priv->state, priv->status);
253 priv->nm_saved_state = priv->state;
254 g_free (priv->nm_saved_status);
255 priv->nm_saved_status = g_strdup (priv->status);
256 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
258 else if (!old_nm_connected && new_nm_connected) {
259 /* We are now connected */
260 DEBUG ("Reconnected: Restore state %d (%s)",
261 priv->nm_saved_state, priv->nm_saved_status);
262 empathy_idle_set_presence (idle,
263 priv->nm_saved_state,
264 priv->nm_saved_status);
265 priv->nm_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
266 g_free (priv->nm_saved_status);
267 priv->nm_saved_status = NULL;
270 priv->nm_connected = new_nm_connected;
274 idle_finalize (GObject *object)
276 EmpathyIdlePriv *priv;
278 priv = GET_PRIV (object);
280 g_free (priv->status);
281 g_object_unref (priv->mc);
283 if (priv->gs_proxy) {
284 g_object_unref (priv->gs_proxy);
287 idle_ext_away_stop (EMPATHY_IDLE (object));
291 idle_constructor (GType type,
293 GObjectConstructParam *props)
297 if (idle_singleton) {
298 retval = g_object_ref (idle_singleton);
300 retval = G_OBJECT_CLASS (empathy_idle_parent_class)->constructor
301 (type, n_props, props);
303 idle_singleton = EMPATHY_IDLE (retval);
304 g_object_add_weak_pointer (retval, (gpointer) &idle_singleton);
311 idle_get_property (GObject *object,
316 EmpathyIdlePriv *priv;
319 priv = GET_PRIV (object);
320 idle = EMPATHY_IDLE (object);
324 g_value_set_enum (value, empathy_idle_get_state (idle));
327 g_value_set_string (value, empathy_idle_get_status (idle));
329 case PROP_FLASH_STATE:
330 g_value_set_enum (value, empathy_idle_get_flash_state (idle));
333 g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
336 g_value_set_boolean (value, empathy_idle_get_use_nm (idle));
339 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
345 idle_set_property (GObject *object,
350 EmpathyIdlePriv *priv;
353 priv = GET_PRIV (object);
354 idle = EMPATHY_IDLE (object);
358 empathy_idle_set_state (idle, g_value_get_enum (value));
361 empathy_idle_set_status (idle, g_value_get_string (value));
363 case PROP_FLASH_STATE:
364 empathy_idle_set_flash_state (idle, g_value_get_enum (value));
367 empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
370 empathy_idle_set_use_nm (idle, g_value_get_boolean (value));
373 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
379 empathy_idle_class_init (EmpathyIdleClass *klass)
381 GObjectClass *object_class = G_OBJECT_CLASS (klass);
383 object_class->finalize = idle_finalize;
384 object_class->constructor = idle_constructor;
385 object_class->get_property = idle_get_property;
386 object_class->set_property = idle_set_property;
388 g_object_class_install_property (object_class,
390 g_param_spec_uint ("state",
393 0, NUM_TP_CONNECTION_PRESENCE_TYPES,
394 TP_CONNECTION_PRESENCE_TYPE_UNSET,
396 g_object_class_install_property (object_class,
398 g_param_spec_string ("status",
403 g_object_class_install_property (object_class,
405 g_param_spec_uint ("flash-state",
408 0, NUM_TP_CONNECTION_PRESENCE_TYPES,
409 TP_CONNECTION_PRESENCE_TYPE_UNSET,
412 g_object_class_install_property (object_class,
414 g_param_spec_boolean ("auto-away",
415 "Automatic set presence to away",
416 "Should it set presence to away if inactive",
420 g_object_class_install_property (object_class,
422 g_param_spec_boolean ("use-nm",
423 "Use Network Manager",
424 "Set presence according to Network Manager",
428 g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
431 static TpConnectionPresenceType
432 empathy_idle_get_actual_presence (EmpathyIdle *idle, GError **error)
435 EmpathyIdlePriv *priv = GET_PRIV (idle);
437 presence = mission_control_get_presence_actual (priv->mc, error);
440 case MC_PRESENCE_OFFLINE:
441 return TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
442 case MC_PRESENCE_AVAILABLE:
443 return TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
444 case MC_PRESENCE_AWAY:
445 return TP_CONNECTION_PRESENCE_TYPE_AWAY;
446 case MC_PRESENCE_EXTENDED_AWAY:
447 return TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY;
448 case MC_PRESENCE_HIDDEN:
449 return TP_CONNECTION_PRESENCE_TYPE_HIDDEN;
450 case MC_PRESENCE_DO_NOT_DISTURB:
451 return TP_CONNECTION_PRESENCE_TYPE_BUSY;
453 return TP_CONNECTION_PRESENCE_TYPE_UNSET;
458 empathy_idle_init (EmpathyIdle *idle)
460 DBusGConnection *system_bus;
461 GError *error = NULL;
462 EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
463 EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
466 priv->is_idle = FALSE;
467 priv->mc = empathy_mission_control_dup_singleton ();
468 priv->state = empathy_idle_get_actual_presence (idle, &error);
470 DEBUG ("Error getting actual presence: %s", error->message);
472 /* Fallback to OFFLINE as that's what mission_control_get_presence_actual
473 does. This also ensure to always display the status icon (there is no
474 unset presence icon). */
475 priv->state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
476 g_clear_error (&error);
478 priv->status = mission_control_get_presence_message_actual (priv->mc, &error);
479 if (error || EMP_STR_EMPTY (priv->status)) {
480 g_free (priv->status);
484 DEBUG ("Error getting actual presence message: %s", error->message);
485 g_clear_error (&error);
489 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
491 G_CALLBACK (idle_presence_changed_cb),
494 priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
495 "org.gnome.ScreenSaver",
496 "/org/gnome/ScreenSaver",
497 "org.gnome.ScreenSaver");
498 if (priv->gs_proxy) {
499 dbus_g_proxy_add_signal (priv->gs_proxy, "SessionIdleChanged",
502 dbus_g_proxy_connect_signal (priv->gs_proxy, "SessionIdleChanged",
503 G_CALLBACK (idle_session_idle_changed_cb),
506 DEBUG ("Failed to get gs proxy");
510 system_bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
512 DEBUG ("Failed to get system bus: %s",
513 error ? error->message : "No error given");
515 priv->nm_proxy = dbus_g_proxy_new_for_name (system_bus,
516 "org.freedesktop.NetworkManager",
517 "/org/freedesktop/NetworkManager",
518 "org.freedesktop.NetworkManager");
520 if (priv->nm_proxy) {
521 dbus_g_proxy_add_signal (priv->nm_proxy, "StateChange",
522 G_TYPE_UINT, G_TYPE_INVALID);
523 dbus_g_proxy_connect_signal (priv->nm_proxy, "StateChange",
524 G_CALLBACK (idle_nm_state_change_cb),
527 DEBUG ("Failed to get nm proxy");
530 priv->nm_connected = TRUE;
534 empathy_idle_dup_singleton (void)
536 return g_object_new (EMPATHY_TYPE_IDLE, NULL);
539 TpConnectionPresenceType
540 empathy_idle_get_state (EmpathyIdle *idle)
542 EmpathyIdlePriv *priv;
544 priv = GET_PRIV (idle);
550 empathy_idle_set_state (EmpathyIdle *idle,
551 TpConnectionPresenceType state)
553 EmpathyIdlePriv *priv;
555 priv = GET_PRIV (idle);
557 empathy_idle_set_presence (idle, state, priv->status);
561 empathy_idle_get_status (EmpathyIdle *idle)
563 EmpathyIdlePriv *priv;
565 priv = GET_PRIV (idle);
568 return empathy_presence_get_default_message (priv->state);
575 empathy_idle_set_status (EmpathyIdle *idle,
578 EmpathyIdlePriv *priv;
580 priv = GET_PRIV (idle);
582 empathy_idle_set_presence (idle, priv->state, status);
585 TpConnectionPresenceType
586 empathy_idle_get_flash_state (EmpathyIdle *idle)
588 EmpathyIdlePriv *priv;
590 priv = GET_PRIV (idle);
592 return priv->flash_state;
596 empathy_idle_set_flash_state (EmpathyIdle *idle,
597 TpConnectionPresenceType state)
599 EmpathyIdlePriv *priv;
601 priv = GET_PRIV (idle);
603 priv->flash_state = state;
605 if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
608 g_object_notify (G_OBJECT (idle), "flash-state");
612 empathy_idle_do_set_presence (EmpathyIdle *idle,
613 TpConnectionPresenceType state,
616 McPresence mc_state = MC_PRESENCE_UNSET;
617 EmpathyIdlePriv *priv = GET_PRIV (idle);
620 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
621 mc_state = MC_PRESENCE_OFFLINE;
623 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
624 mc_state = MC_PRESENCE_AVAILABLE;
626 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
627 mc_state = MC_PRESENCE_AWAY;
629 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
630 mc_state = MC_PRESENCE_EXTENDED_AWAY;
632 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
633 mc_state = MC_PRESENCE_HIDDEN;
635 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
636 mc_state = MC_PRESENCE_DO_NOT_DISTURB;
639 g_assert_not_reached ();
642 mission_control_set_presence (priv->mc, mc_state, status, NULL, NULL);
646 empathy_idle_set_presence (EmpathyIdle *idle,
647 TpConnectionPresenceType state,
650 EmpathyIdlePriv *priv;
651 const gchar *default_status;
653 priv = GET_PRIV (idle);
655 DEBUG ("Changing presence to %s (%d)", status, state);
657 /* Do not set translated default messages */
658 default_status = empathy_presence_get_default_message (state);
659 if (!tp_strdiff (status, default_status)) {
663 if (!priv->nm_connected) {
664 DEBUG ("NM not connected");
666 priv->nm_saved_state = state;
667 if (tp_strdiff (priv->status, status)) {
668 g_free (priv->status);
670 if (!EMP_STR_EMPTY (status)) {
671 priv->status = g_strdup (status);
673 g_object_notify (G_OBJECT (idle), "status");
679 empathy_idle_do_set_presence (idle, state, status);
683 empathy_idle_get_auto_away (EmpathyIdle *idle)
685 EmpathyIdlePriv *priv = GET_PRIV (idle);
687 return priv->auto_away;
691 empathy_idle_set_auto_away (EmpathyIdle *idle,
694 EmpathyIdlePriv *priv = GET_PRIV (idle);
696 priv->auto_away = auto_away;
698 g_object_notify (G_OBJECT (idle), "auto-away");
702 empathy_idle_get_use_nm (EmpathyIdle *idle)
704 EmpathyIdlePriv *priv = GET_PRIV (idle);
710 empathy_idle_set_use_nm (EmpathyIdle *idle,
713 EmpathyIdlePriv *priv = GET_PRIV (idle);
715 if (!priv->nm_proxy || use_nm == priv->use_nm) {
719 priv->use_nm = use_nm;
723 GError *error = NULL;
725 dbus_g_proxy_call (priv->nm_proxy, "state",
728 G_TYPE_UINT, &nm_status,
732 DEBUG ("Couldn't get NM state: %s", error->message);
733 g_clear_error (&error);
734 nm_status = NM_STATE_ASLEEP;
737 idle_nm_state_change_cb (priv->nm_proxy, nm_status, idle);
739 priv->nm_connected = TRUE;
740 if (priv->nm_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
741 empathy_idle_set_state (idle, priv->nm_saved_state);
743 priv->nm_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
746 g_object_notify (G_OBJECT (idle), "use-nm");