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;
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,
90 EmpathyIdlePriv *priv;
92 priv = GET_PRIV (idle);
94 DEBUG ("Presence changed to '%s' (%d)", status, state);
96 g_free (priv->status);
99 if (!EMP_STR_EMPTY (status)) {
100 priv->status = g_strdup (status);
103 g_object_notify (G_OBJECT (idle), "state");
104 g_object_notify (G_OBJECT (idle), "status");
108 idle_ext_away_cb (EmpathyIdle *idle)
110 EmpathyIdlePriv *priv;
112 priv = GET_PRIV (idle);
114 DEBUG ("Going to extended autoaway");
115 empathy_idle_set_state (idle, MC_PRESENCE_EXTENDED_AWAY);
116 priv->ext_away_timeout = 0;
122 idle_ext_away_stop (EmpathyIdle *idle)
124 EmpathyIdlePriv *priv;
126 priv = GET_PRIV (idle);
128 if (priv->ext_away_timeout) {
129 g_source_remove (priv->ext_away_timeout);
130 priv->ext_away_timeout = 0;
135 idle_ext_away_start (EmpathyIdle *idle)
137 EmpathyIdlePriv *priv;
139 priv = GET_PRIV (idle);
141 if (priv->ext_away_timeout != 0) {
144 priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
145 (GSourceFunc) idle_ext_away_cb,
150 idle_session_idle_changed_cb (DBusGProxy *gs_proxy,
154 EmpathyIdlePriv *priv;
156 priv = GET_PRIV (idle);
158 DEBUG ("Session idle state changed, %s -> %s",
159 priv->is_idle ? "yes" : "no",
160 is_idle ? "yes" : "no");
162 if (!priv->auto_away ||
163 (priv->nm_saved_state == MC_PRESENCE_UNSET &&
164 (priv->state <= MC_PRESENCE_OFFLINE ||
165 priv->state == MC_PRESENCE_HIDDEN))) {
166 /* We don't want to go auto away OR we explicitely asked to be
167 * offline, nothing to do here */
168 priv->is_idle = is_idle;
172 if (is_idle && !priv->is_idle) {
173 McPresence new_state;
174 /* We are now idle */
176 idle_ext_away_start (idle);
178 if (priv->nm_saved_state != MC_PRESENCE_UNSET) {
179 /* We are disconnected, when coming back from away
180 * we want to restore the presence before the
182 priv->away_saved_state = priv->nm_saved_state;
184 priv->away_saved_state = priv->state;
187 new_state = MC_PRESENCE_AWAY;
188 if (priv->state == MC_PRESENCE_EXTENDED_AWAY) {
189 new_state = MC_PRESENCE_EXTENDED_AWAY;
192 DEBUG ("Going to autoaway. Saved state=%d, new state=%d",
193 priv->away_saved_state, new_state);
194 empathy_idle_set_state (idle, new_state);
195 } else if (!is_idle && priv->is_idle) {
196 const gchar *new_status;
197 /* We are no more idle, restore state */
199 idle_ext_away_stop (idle);
201 if (priv->away_saved_state == MC_PRESENCE_AWAY ||
202 priv->away_saved_state == MC_PRESENCE_EXTENDED_AWAY) {
203 priv->away_saved_state = MC_PRESENCE_AVAILABLE;
206 new_status = priv->status;
209 DEBUG ("Restoring state to %d, reset status to %s",
210 priv->away_saved_state, new_status);
212 empathy_idle_set_presence (idle,
213 priv->away_saved_state,
216 priv->away_saved_state = MC_PRESENCE_UNSET;
219 priv->is_idle = is_idle;
223 idle_nm_state_change_cb (DBusGProxy *proxy,
227 EmpathyIdlePriv *priv;
228 gboolean old_nm_connected;
229 gboolean new_nm_connected;
231 priv = GET_PRIV (idle);
237 old_nm_connected = priv->nm_connected;
238 new_nm_connected = !(state == NM_STATE_CONNECTING ||
239 state == NM_STATE_DISCONNECTED);
240 priv->nm_connected = TRUE; /* To be sure _set_state will work */
242 DEBUG ("New network state %d", state);
244 if (old_nm_connected && !new_nm_connected) {
245 /* We are no more connected */
246 DEBUG ("Disconnected: Save state %d (%s)",
247 priv->state, priv->status);
248 priv->nm_saved_state = priv->state;
249 g_free (priv->nm_saved_status);
250 priv->nm_saved_status = g_strdup (priv->status);
251 empathy_idle_set_state (idle, MC_PRESENCE_OFFLINE);
253 else if (!old_nm_connected && new_nm_connected) {
254 /* We are now connected */
255 DEBUG ("Reconnected: Restore state %d (%s)",
256 priv->nm_saved_state, priv->nm_saved_status);
257 empathy_idle_set_presence (idle,
258 priv->nm_saved_state,
259 priv->nm_saved_status);
260 priv->nm_saved_state = MC_PRESENCE_UNSET;
261 g_free (priv->nm_saved_status);
262 priv->nm_saved_status = NULL;
265 priv->nm_connected = new_nm_connected;
269 idle_finalize (GObject *object)
271 EmpathyIdlePriv *priv;
273 priv = GET_PRIV (object);
275 g_free (priv->status);
276 g_object_unref (priv->mc);
278 if (priv->gs_proxy) {
279 g_object_unref (priv->gs_proxy);
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_value_set_boolean (value, empathy_idle_get_use_nm (idle));
334 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
340 idle_set_property (GObject *object,
345 EmpathyIdlePriv *priv;
348 priv = GET_PRIV (object);
349 idle = EMPATHY_IDLE (object);
353 empathy_idle_set_state (idle, g_value_get_enum (value));
356 empathy_idle_set_status (idle, g_value_get_string (value));
358 case PROP_FLASH_STATE:
359 empathy_idle_set_flash_state (idle, g_value_get_enum (value));
362 empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
365 empathy_idle_set_use_nm (idle, g_value_get_boolean (value));
368 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
374 empathy_idle_class_init (EmpathyIdleClass *klass)
376 GObjectClass *object_class = G_OBJECT_CLASS (klass);
378 object_class->finalize = idle_finalize;
379 object_class->constructor = idle_constructor;
380 object_class->get_property = idle_get_property;
381 object_class->set_property = idle_set_property;
383 g_object_class_install_property (object_class,
385 g_param_spec_enum ("state",
389 MC_PRESENCE_AVAILABLE,
391 g_object_class_install_property (object_class,
393 g_param_spec_string ("status",
398 g_object_class_install_property (object_class,
400 g_param_spec_enum ("flash-state",
407 g_object_class_install_property (object_class,
409 g_param_spec_boolean ("auto-away",
410 "Automatic set presence to away",
411 "Should it set presence to away if inactive",
415 g_object_class_install_property (object_class,
417 g_param_spec_boolean ("use-nm",
418 "Use Network Manager",
419 "Set presence according to Network Manager",
423 g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
427 empathy_idle_init (EmpathyIdle *idle)
429 DBusGConnection *system_bus;
430 GError *error = NULL;
431 EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
432 EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
435 priv->is_idle = FALSE;
436 priv->mc = empathy_mission_control_dup_singleton ();
437 priv->state = mission_control_get_presence_actual (priv->mc, &error);
439 DEBUG ("Error getting actual presence: %s", error->message);
441 priv->state = MC_PRESENCE_UNSET;
442 g_clear_error (&error);
444 priv->status = mission_control_get_presence_message_actual (priv->mc, &error);
445 if (error || EMP_STR_EMPTY (priv->status)) {
446 g_free (priv->status);
450 DEBUG ("Error getting actual presence message: %s", error->message);
451 g_clear_error (&error);
455 dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
457 G_CALLBACK (idle_presence_changed_cb),
460 priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
461 "org.gnome.ScreenSaver",
462 "/org/gnome/ScreenSaver",
463 "org.gnome.ScreenSaver");
464 if (priv->gs_proxy) {
465 dbus_g_proxy_add_signal (priv->gs_proxy, "SessionIdleChanged",
468 dbus_g_proxy_connect_signal (priv->gs_proxy, "SessionIdleChanged",
469 G_CALLBACK (idle_session_idle_changed_cb),
472 DEBUG ("Failed to get gs proxy");
476 system_bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
478 DEBUG ("Failed to get system bus: %s",
479 error ? error->message : "No error given");
481 priv->nm_proxy = dbus_g_proxy_new_for_name (system_bus,
482 "org.freedesktop.NetworkManager",
483 "/org/freedesktop/NetworkManager",
484 "org.freedesktop.NetworkManager");
486 if (priv->nm_proxy) {
487 dbus_g_proxy_add_signal (priv->nm_proxy, "StateChange",
488 G_TYPE_UINT, G_TYPE_INVALID);
489 dbus_g_proxy_connect_signal (priv->nm_proxy, "StateChange",
490 G_CALLBACK (idle_nm_state_change_cb),
493 DEBUG ("Failed to get nm proxy");
496 priv->nm_connected = TRUE;
500 empathy_idle_dup_singleton (void)
502 return g_object_new (EMPATHY_TYPE_IDLE, NULL);
506 empathy_idle_get_state (EmpathyIdle *idle)
508 EmpathyIdlePriv *priv;
510 priv = GET_PRIV (idle);
516 empathy_idle_set_state (EmpathyIdle *idle,
519 EmpathyIdlePriv *priv;
521 priv = GET_PRIV (idle);
523 empathy_idle_set_presence (idle, state, priv->status);
527 empathy_idle_get_status (EmpathyIdle *idle)
529 EmpathyIdlePriv *priv;
531 priv = GET_PRIV (idle);
534 return empathy_presence_get_default_message (priv->state);
541 empathy_idle_set_status (EmpathyIdle *idle,
544 EmpathyIdlePriv *priv;
546 priv = GET_PRIV (idle);
548 empathy_idle_set_presence (idle, priv->state, status);
552 empathy_idle_get_flash_state (EmpathyIdle *idle)
554 EmpathyIdlePriv *priv;
556 priv = GET_PRIV (idle);
558 return priv->flash_state;
562 empathy_idle_set_flash_state (EmpathyIdle *idle,
565 EmpathyIdlePriv *priv;
567 priv = GET_PRIV (idle);
569 priv->flash_state = state;
571 if (state == MC_PRESENCE_UNSET) {
574 g_object_notify (G_OBJECT (idle), "flash-state");
578 empathy_idle_set_presence (EmpathyIdle *idle,
582 EmpathyIdlePriv *priv;
583 const gchar *default_status;
585 priv = GET_PRIV (idle);
587 DEBUG ("Changing presence to %s (%d)", status, state);
589 /* Do not set translated default messages */
590 default_status = empathy_presence_get_default_message (state);
591 if (!tp_strdiff (status, default_status)) {
595 if (!priv->nm_connected) {
596 DEBUG ("NM not connected");
598 priv->nm_saved_state = state;
599 if (tp_strdiff (priv->status, status)) {
600 g_free (priv->status);
602 if (!EMP_STR_EMPTY (status)) {
603 priv->status = g_strdup (status);
605 g_object_notify (G_OBJECT (idle), "status");
611 mission_control_set_presence (priv->mc, state, status, NULL, NULL);
615 empathy_idle_get_auto_away (EmpathyIdle *idle)
617 EmpathyIdlePriv *priv = GET_PRIV (idle);
619 return priv->auto_away;
623 empathy_idle_set_auto_away (EmpathyIdle *idle,
626 EmpathyIdlePriv *priv = GET_PRIV (idle);
628 priv->auto_away = auto_away;
630 g_object_notify (G_OBJECT (idle), "auto-away");
634 empathy_idle_get_use_nm (EmpathyIdle *idle)
636 EmpathyIdlePriv *priv = GET_PRIV (idle);
642 empathy_idle_set_use_nm (EmpathyIdle *idle,
645 EmpathyIdlePriv *priv = GET_PRIV (idle);
647 if (!priv->nm_proxy || use_nm == priv->use_nm) {
651 priv->use_nm = use_nm;
655 GError *error = NULL;
657 dbus_g_proxy_call (priv->nm_proxy, "state",
660 G_TYPE_UINT, &nm_status,
664 DEBUG ("Couldn't get NM state: %s", error->message);
665 g_clear_error (&error);
666 nm_status = NM_STATE_ASLEEP;
669 idle_nm_state_change_cb (priv->nm_proxy, nm_status, idle);
671 priv->nm_connected = TRUE;
672 if (priv->nm_saved_state != MC_PRESENCE_UNSET) {
673 empathy_idle_set_state (idle, priv->nm_saved_state);
675 priv->nm_saved_state = MC_PRESENCE_UNSET;
678 g_object_notify (G_OBJECT (idle), "use-nm");