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)
50 McPresence flash_state;
54 McPresence away_saved_state;
55 McPresence nm_saved_state;
58 gboolean nm_connected;
59 guint ext_away_timeout;
79 G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT);
81 static EmpathyIdle * idle_singleton = NULL;
84 idle_presence_changed_cb (MissionControl *mc,
89 EmpathyIdlePriv *priv;
91 priv = GET_PRIV (idle);
93 DEBUG ("Presence changed to '%s' (%d)", status, state);
95 g_free (priv->status);
98 if (!EMP_STR_EMPTY (status)) {
99 priv->status = g_strdup (status);
102 g_object_notify (G_OBJECT (idle), "state");
103 g_object_notify (G_OBJECT (idle), "status");
107 idle_ext_away_cb (EmpathyIdle *idle)
109 EmpathyIdlePriv *priv;
111 priv = GET_PRIV (idle);
113 DEBUG ("Going to extended autoaway");
114 empathy_idle_set_state (idle, MC_PRESENCE_EXTENDED_AWAY);
115 priv->ext_away_timeout = 0;
121 idle_ext_away_stop (EmpathyIdle *idle)
123 EmpathyIdlePriv *priv;
125 priv = GET_PRIV (idle);
127 if (priv->ext_away_timeout) {
128 g_source_remove (priv->ext_away_timeout);
129 priv->ext_away_timeout = 0;
134 idle_ext_away_start (EmpathyIdle *idle)
136 EmpathyIdlePriv *priv;
138 priv = GET_PRIV (idle);
140 if (priv->ext_away_timeout != 0) {
143 priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
144 (GSourceFunc) idle_ext_away_cb,
149 idle_session_idle_changed_cb (DBusGProxy *gs_proxy,
153 EmpathyIdlePriv *priv;
155 priv = GET_PRIV (idle);
157 DEBUG ("Session idle state changed, %s -> %s",
158 priv->is_idle ? "yes" : "no",
159 is_idle ? "yes" : "no");
161 if (!priv->auto_away ||
162 (priv->nm_saved_state == MC_PRESENCE_UNSET &&
163 (priv->state <= MC_PRESENCE_OFFLINE ||
164 priv->state == MC_PRESENCE_HIDDEN))) {
165 /* We don't want to go auto away OR we explicitely asked to be
166 * offline, nothing to do here */
167 priv->is_idle = is_idle;
171 if (is_idle && !priv->is_idle) {
172 McPresence new_state;
173 /* We are now idle */
175 idle_ext_away_start (idle);
177 if (priv->nm_saved_state != MC_PRESENCE_UNSET) {
178 /* We are disconnected, when coming back from away
179 * we want to restore the presence before the
181 priv->away_saved_state = priv->nm_saved_state;
183 priv->away_saved_state = priv->state;
186 new_state = MC_PRESENCE_AWAY;
187 if (priv->state == MC_PRESENCE_EXTENDED_AWAY) {
188 new_state = MC_PRESENCE_EXTENDED_AWAY;
191 DEBUG ("Going to autoaway. Saved state=%d, new state=%d",
192 priv->away_saved_state, new_state);
193 empathy_idle_set_state (idle, new_state);
194 } else if (!is_idle && priv->is_idle) {
195 const gchar *new_status;
196 /* We are no more idle, restore state */
198 idle_ext_away_stop (idle);
200 if (priv->away_saved_state == MC_PRESENCE_AWAY ||
201 priv->away_saved_state == MC_PRESENCE_EXTENDED_AWAY) {
202 priv->away_saved_state = MC_PRESENCE_AVAILABLE;
205 new_status = priv->status;
208 DEBUG ("Restoring state to %d, reset status to %s",
209 priv->away_saved_state, new_status);
211 empathy_idle_set_presence (idle,
212 priv->away_saved_state,
215 priv->away_saved_state = MC_PRESENCE_UNSET;
218 priv->is_idle = is_idle;
222 idle_nm_state_change_cb (DBusGProxy *proxy,
226 EmpathyIdlePriv *priv;
227 gboolean old_nm_connected;
228 gboolean new_nm_connected;
230 priv = GET_PRIV (idle);
236 old_nm_connected = priv->nm_connected;
237 new_nm_connected = !(state == NM_STATE_CONNECTING ||
238 state == NM_STATE_DISCONNECTED);
239 priv->nm_connected = TRUE; /* To be sure _set_state will work */
241 DEBUG ("New network state %d", state);
243 if (old_nm_connected && !new_nm_connected) {
244 /* We are no more connected */
245 DEBUG ("Disconnected: Save state %d", priv->state);
246 priv->nm_saved_state = priv->state;
247 empathy_idle_set_state (idle, MC_PRESENCE_OFFLINE);
249 else if (!old_nm_connected && new_nm_connected) {
250 /* We are now connected */
251 DEBUG ("Reconnected: Restore state %d", priv->nm_saved_state);
252 empathy_idle_set_state (idle, priv->nm_saved_state);
253 priv->nm_saved_state = MC_PRESENCE_UNSET;
256 priv->nm_connected = new_nm_connected;
260 idle_finalize (GObject *object)
262 EmpathyIdlePriv *priv;
264 priv = GET_PRIV (object);
266 g_free (priv->status);
267 g_object_unref (priv->mc);
269 if (priv->gs_proxy) {
270 g_object_unref (priv->gs_proxy);
273 idle_ext_away_stop (EMPATHY_IDLE (object));
277 idle_constructor (GType type,
279 GObjectConstructParam *props)
283 if (idle_singleton) {
284 retval = g_object_ref (idle_singleton);
286 retval = G_OBJECT_CLASS (empathy_idle_parent_class)->constructor
287 (type, n_props, props);
289 idle_singleton = EMPATHY_IDLE (retval);
290 g_object_add_weak_pointer (retval, (gpointer *) &idle_singleton);
297 idle_get_property (GObject *object,
302 EmpathyIdlePriv *priv;
305 priv = GET_PRIV (object);
306 idle = EMPATHY_IDLE (object);
310 g_value_set_enum (value, empathy_idle_get_state (idle));
313 g_value_set_string (value, empathy_idle_get_status (idle));
315 case PROP_FLASH_STATE:
316 g_value_set_enum (value, empathy_idle_get_flash_state (idle));
319 g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
322 g_value_set_boolean (value, empathy_idle_get_use_nm (idle));
325 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
331 idle_set_property (GObject *object,
336 EmpathyIdlePriv *priv;
339 priv = GET_PRIV (object);
340 idle = EMPATHY_IDLE (object);
344 empathy_idle_set_state (idle, g_value_get_enum (value));
347 empathy_idle_set_status (idle, g_value_get_string (value));
349 case PROP_FLASH_STATE:
350 empathy_idle_set_flash_state (idle, g_value_get_enum (value));
353 empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
356 empathy_idle_set_use_nm (idle, g_value_get_boolean (value));
359 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
365 empathy_idle_class_init (EmpathyIdleClass *klass)
367 GObjectClass *object_class = G_OBJECT_CLASS (klass);
369 object_class->finalize = idle_finalize;
370 object_class->constructor = idle_constructor;
371 object_class->get_property = idle_get_property;
372 object_class->set_property = idle_set_property;
374 g_object_class_install_property (object_class,
376 g_param_spec_enum ("state",
380 MC_PRESENCE_AVAILABLE,
382 g_object_class_install_property (object_class,
384 g_param_spec_string ("status",
389 g_object_class_install_property (object_class,
391 g_param_spec_enum ("flash-state",
398 g_object_class_install_property (object_class,
400 g_param_spec_boolean ("auto-away",
401 "Automatic set presence to away",
402 "Should it set presence to away if inactive",
406 g_object_class_install_property (object_class,
408 g_param_spec_boolean ("use-nm",
409 "Use Network Manager",
410 "Set presence according to Network Manager",
414 g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
418 empathy_idle_init (EmpathyIdle *idle)
420 DBusGConnection *system_bus;
421 GError *error = NULL;
422 EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
423 EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
426 priv->is_idle = FALSE;
427 priv->mc = empathy_mission_control_dup_singleton ();
428 priv->state = mission_control_get_presence_actual (priv->mc, &error);
430 DEBUG ("Error getting actual presence: %s", error->message);
432 priv->state = MC_PRESENCE_UNSET;
433 g_clear_error (&error);
435 priv->status = mission_control_get_presence_message_actual (priv->mc, &error);
436 if (error || EMP_STR_EMPTY (priv->status)) {
437 g_free (priv->status);
441 DEBUG ("Error getting actual presence message: %s", error->message);
442 g_clear_error (&error);
446 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
448 G_CALLBACK (idle_presence_changed_cb),
451 priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
452 "org.gnome.ScreenSaver",
453 "/org/gnome/ScreenSaver",
454 "org.gnome.ScreenSaver");
455 if (priv->gs_proxy) {
456 dbus_g_proxy_add_signal (priv->gs_proxy, "SessionIdleChanged",
459 dbus_g_proxy_connect_signal (priv->gs_proxy, "SessionIdleChanged",
460 G_CALLBACK (idle_session_idle_changed_cb),
463 DEBUG ("Failed to get gs proxy");
467 system_bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
469 DEBUG ("Failed to get system bus: %s",
470 error ? error->message : "No error given");
472 priv->nm_proxy = dbus_g_proxy_new_for_name (system_bus,
473 "org.freedesktop.NetworkManager",
474 "/org/freedesktop/NetworkManager",
475 "org.freedesktop.NetworkManager");
477 if (priv->nm_proxy) {
478 dbus_g_proxy_add_signal (priv->nm_proxy, "StateChange",
479 G_TYPE_UINT, G_TYPE_INVALID);
480 dbus_g_proxy_connect_signal (priv->nm_proxy, "StateChange",
481 G_CALLBACK (idle_nm_state_change_cb),
484 DEBUG ("Failed to get nm proxy");
487 priv->nm_connected = TRUE;
491 empathy_idle_dup_singleton (void)
493 return g_object_new (EMPATHY_TYPE_IDLE, NULL);
497 empathy_idle_get_state (EmpathyIdle *idle)
499 EmpathyIdlePriv *priv;
501 priv = GET_PRIV (idle);
507 empathy_idle_set_state (EmpathyIdle *idle,
510 EmpathyIdlePriv *priv;
512 priv = GET_PRIV (idle);
514 empathy_idle_set_presence (idle, state, priv->status);
518 empathy_idle_get_status (EmpathyIdle *idle)
520 EmpathyIdlePriv *priv;
522 priv = GET_PRIV (idle);
525 return empathy_presence_get_default_message (priv->state);
532 empathy_idle_set_status (EmpathyIdle *idle,
535 EmpathyIdlePriv *priv;
537 priv = GET_PRIV (idle);
539 empathy_idle_set_presence (idle, priv->state, status);
543 empathy_idle_get_flash_state (EmpathyIdle *idle)
545 EmpathyIdlePriv *priv;
547 priv = GET_PRIV (idle);
549 return priv->flash_state;
553 empathy_idle_set_flash_state (EmpathyIdle *idle,
556 EmpathyIdlePriv *priv;
558 priv = GET_PRIV (idle);
560 priv->flash_state = state;
562 if (state == MC_PRESENCE_UNSET) {
565 g_object_notify (G_OBJECT (idle), "flash-state");
569 empathy_idle_set_presence (EmpathyIdle *idle,
573 EmpathyIdlePriv *priv;
574 const gchar *default_status;
576 priv = GET_PRIV (idle);
578 DEBUG ("Changing presence to %s (%d)", status, state);
580 /* Do not set translated default messages */
581 default_status = empathy_presence_get_default_message (state);
582 if (!tp_strdiff (status, default_status)) {
586 if (!priv->nm_connected) {
587 DEBUG ("NM not connected");
589 priv->nm_saved_state = state;
590 if (tp_strdiff (priv->status, status)) {
591 g_free (priv->status);
593 if (!EMP_STR_EMPTY (status)) {
594 priv->status = g_strdup (status);
596 g_object_notify (G_OBJECT (idle), "status");
602 mission_control_set_presence (priv->mc, state, status, NULL, NULL);
606 empathy_idle_get_auto_away (EmpathyIdle *idle)
608 EmpathyIdlePriv *priv = GET_PRIV (idle);
610 return priv->auto_away;
614 empathy_idle_set_auto_away (EmpathyIdle *idle,
617 EmpathyIdlePriv *priv = GET_PRIV (idle);
619 priv->auto_away = auto_away;
621 g_object_notify (G_OBJECT (idle), "auto-away");
625 empathy_idle_get_use_nm (EmpathyIdle *idle)
627 EmpathyIdlePriv *priv = GET_PRIV (idle);
633 empathy_idle_set_use_nm (EmpathyIdle *idle,
636 EmpathyIdlePriv *priv = GET_PRIV (idle);
638 if (!priv->nm_proxy || use_nm == priv->use_nm) {
642 priv->use_nm = use_nm;
646 GError *error = NULL;
648 dbus_g_proxy_call (priv->nm_proxy, "state",
651 G_TYPE_UINT, &nm_status,
655 DEBUG ("Couldn't get NM state: %s", error->message);
656 g_clear_error (&error);
657 nm_status = NM_STATE_ASLEEP;
660 idle_nm_state_change_cb (priv->nm_proxy, nm_status, idle);
662 priv->nm_connected = TRUE;
663 if (priv->nm_saved_state != MC_PRESENCE_UNSET) {
664 empathy_idle_set_state (idle, priv->nm_saved_state);
666 priv->nm_saved_state = MC_PRESENCE_UNSET;
669 g_object_notify (G_OBJECT (idle), "use-nm");