]> git.0d.be Git - empathy.git/blob - libempathy/empathy-idle.c
empathy-utils: remove trailing tabs
[empathy.git] / libempathy / empathy-idle.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  *
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.
9  *
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.
14  *
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
18  *
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25
26 #include <glib/gi18n-lib.h>
27 #include <dbus/dbus-glib.h>
28
29 #include <telepathy-glib/dbus.h>
30 #include <telepathy-glib/util.h>
31 #include <libmissioncontrol/mc-enum-types.h>
32
33 #include "empathy-idle.h"
34 #include "empathy-utils.h"
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
37 #include "empathy-debug.h"
38
39 /* Number of seconds before entering extended autoaway. */
40 #define EXT_AWAY_TIME (30*60)
41
42 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIdle)
43 typedef struct {
44         MissionControl *mc;
45         DBusGProxy     *gs_proxy;
46         DBusGProxy     *nm_proxy;
47
48         TpConnectionPresenceType      state;
49         gchar          *status;
50         TpConnectionPresenceType      flash_state;
51         gboolean        auto_away;
52         gboolean        use_nm;
53
54         TpConnectionPresenceType      away_saved_state;
55         TpConnectionPresenceType      nm_saved_state;
56         gchar          *nm_saved_status;
57
58         gboolean        is_idle;
59         gboolean        nm_connected;
60         guint           ext_away_timeout;
61 } EmpathyIdlePriv;
62
63 typedef enum {
64         NM_STATE_UNKNOWN,
65         NM_STATE_ASLEEP,
66         NM_STATE_CONNECTING,
67         NM_STATE_CONNECTED,
68         NM_STATE_DISCONNECTED
69 } NMState;
70
71 enum {
72         PROP_0,
73         PROP_STATE,
74         PROP_STATUS,
75         PROP_FLASH_STATE,
76         PROP_AUTO_AWAY,
77         PROP_USE_NM
78 };
79
80 G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT);
81
82 static EmpathyIdle * idle_singleton = NULL;
83
84 static void
85 idle_presence_changed_cb (MissionControl *mc,
86                           TpConnectionPresenceType state,
87                           gchar          *status,
88                           EmpathyIdle    *idle)
89 {
90         EmpathyIdlePriv *priv;
91
92         priv = GET_PRIV (idle);
93
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;
97
98         DEBUG ("Presence changed to '%s' (%d)", status, state);
99
100         g_free (priv->status);
101         priv->state = state;
102         priv->status = NULL;
103         if (!EMP_STR_EMPTY (status)) {
104                 priv->status = g_strdup (status);
105         }
106
107         g_object_notify (G_OBJECT (idle), "state");
108         g_object_notify (G_OBJECT (idle), "status");
109 }
110
111 static gboolean
112 idle_ext_away_cb (EmpathyIdle *idle)
113 {
114         EmpathyIdlePriv *priv;
115
116         priv = GET_PRIV (idle);
117
118         DEBUG ("Going to extended autoaway");
119         empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY);
120         priv->ext_away_timeout = 0;
121
122         return FALSE;
123 }
124
125 static void
126 idle_ext_away_stop (EmpathyIdle *idle)
127 {
128         EmpathyIdlePriv *priv;
129
130         priv = GET_PRIV (idle);
131
132         if (priv->ext_away_timeout) {
133                 g_source_remove (priv->ext_away_timeout);
134                 priv->ext_away_timeout = 0;
135         }
136 }
137
138 static void
139 idle_ext_away_start (EmpathyIdle *idle)
140 {
141         EmpathyIdlePriv *priv;
142
143         priv = GET_PRIV (idle);
144
145         if (priv->ext_away_timeout != 0) {
146                 return;
147         }
148         priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
149                                                         (GSourceFunc) idle_ext_away_cb,
150                                                         idle);
151 }
152
153 static void
154 idle_session_idle_changed_cb (DBusGProxy  *gs_proxy,
155                               gboolean     is_idle,
156                               EmpathyIdle *idle)
157 {
158         EmpathyIdlePriv *priv;
159
160         priv = GET_PRIV (idle);
161
162         DEBUG ("Session idle state changed, %s -> %s",
163                 priv->is_idle ? "yes" : "no",
164                 is_idle ? "yes" : "no");
165
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;
173                 return;
174         }
175
176         if (is_idle && !priv->is_idle) {
177                 TpConnectionPresenceType new_state;
178                 /* We are now idle */
179
180                 idle_ext_away_start (idle);
181
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
185                          * disconnection. */
186                         priv->away_saved_state = priv->nm_saved_state;
187                 } else {
188                         priv->away_saved_state = priv->state;
189                 }
190
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;
194                 }
195
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 */
202
203                 idle_ext_away_stop (idle);
204
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;
208                         new_status = NULL;
209                 } else {
210                         new_status = priv->status;
211                 }
212
213                 DEBUG ("Restoring state to %d, reset status to %s",
214                         priv->away_saved_state, new_status);
215
216                 empathy_idle_set_presence (idle,
217                                            priv->away_saved_state,
218                                            new_status);
219
220                 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
221         }
222
223         priv->is_idle = is_idle;
224 }
225
226 static void
227 idle_nm_state_change_cb (DBusGProxy  *proxy,
228                          guint        state,
229                          EmpathyIdle *idle)
230 {
231         EmpathyIdlePriv *priv;
232         gboolean         old_nm_connected;
233         gboolean         new_nm_connected;
234
235         priv = GET_PRIV (idle);
236
237         if (!priv->use_nm
238             || priv->nm_saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
239                 return;
240         }
241
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 */
246
247         DEBUG ("New network state %d", state);
248
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);
257         }
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;
268         }
269
270         priv->nm_connected = new_nm_connected;
271 }
272
273 static void
274 idle_finalize (GObject *object)
275 {
276         EmpathyIdlePriv *priv;
277
278         priv = GET_PRIV (object);
279
280         g_free (priv->status);
281         g_object_unref (priv->mc);
282
283         if (priv->gs_proxy) {
284                 g_object_unref (priv->gs_proxy);
285         }
286
287         idle_ext_away_stop (EMPATHY_IDLE (object));
288 }
289
290 static GObject *
291 idle_constructor (GType type,
292                   guint n_props,
293                   GObjectConstructParam *props)
294 {
295         GObject *retval;
296
297         if (idle_singleton) {
298                 retval = g_object_ref (idle_singleton);
299         } else {
300                 retval = G_OBJECT_CLASS (empathy_idle_parent_class)->constructor
301                         (type, n_props, props);
302
303                 idle_singleton = EMPATHY_IDLE (retval);
304                 g_object_add_weak_pointer (retval, (gpointer) &idle_singleton);
305         }
306
307         return retval;
308 }
309
310 static void
311 idle_get_property (GObject    *object,
312                    guint       param_id,
313                    GValue     *value,
314                    GParamSpec *pspec)
315 {
316         EmpathyIdlePriv *priv;
317         EmpathyIdle     *idle;
318
319         priv = GET_PRIV (object);
320         idle = EMPATHY_IDLE (object);
321
322         switch (param_id) {
323         case PROP_STATE:
324                 g_value_set_enum (value, empathy_idle_get_state (idle));
325                 break;
326         case PROP_STATUS:
327                 g_value_set_string (value, empathy_idle_get_status (idle));
328                 break;
329         case PROP_FLASH_STATE:
330                 g_value_set_enum (value, empathy_idle_get_flash_state (idle));
331                 break;
332         case PROP_AUTO_AWAY:
333                 g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
334                 break;
335         case PROP_USE_NM:
336                 g_value_set_boolean (value, empathy_idle_get_use_nm (idle));
337                 break;
338         default:
339                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
340                 break;
341         };
342 }
343
344 static void
345 idle_set_property (GObject      *object,
346                    guint         param_id,
347                    const GValue *value,
348                    GParamSpec   *pspec)
349 {
350         EmpathyIdlePriv *priv;
351         EmpathyIdle     *idle;
352
353         priv = GET_PRIV (object);
354         idle = EMPATHY_IDLE (object);
355
356         switch (param_id) {
357         case PROP_STATE:
358                 empathy_idle_set_state (idle, g_value_get_enum (value));
359                 break;
360         case PROP_STATUS:
361                 empathy_idle_set_status (idle, g_value_get_string (value));
362                 break;
363         case PROP_FLASH_STATE:
364                 empathy_idle_set_flash_state (idle, g_value_get_enum (value));
365                 break;
366         case PROP_AUTO_AWAY:
367                 empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
368                 break;
369         case PROP_USE_NM:
370                 empathy_idle_set_use_nm (idle, g_value_get_boolean (value));
371                 break;
372         default:
373                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
374                 break;
375         };
376 }
377
378 static void
379 empathy_idle_class_init (EmpathyIdleClass *klass)
380 {
381         GObjectClass *object_class = G_OBJECT_CLASS (klass);
382
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;
387
388         g_object_class_install_property (object_class,
389                                          PROP_STATE,
390                                          g_param_spec_uint ("state",
391                                                             "state",
392                                                             "state",
393                                                             0, NUM_TP_CONNECTION_PRESENCE_TYPES,
394                                                             TP_CONNECTION_PRESENCE_TYPE_UNSET,
395                                                             G_PARAM_READWRITE));
396         g_object_class_install_property (object_class,
397                                          PROP_STATUS,
398                                          g_param_spec_string ("status",
399                                                               "status",
400                                                               "status",
401                                                               NULL,
402                                                               G_PARAM_READWRITE));
403         g_object_class_install_property (object_class,
404                                          PROP_FLASH_STATE,
405                                          g_param_spec_uint ("flash-state",
406                                                             "flash-state",
407                                                             "flash-state",
408                                                             0, NUM_TP_CONNECTION_PRESENCE_TYPES,
409                                                             TP_CONNECTION_PRESENCE_TYPE_UNSET,
410                                                             G_PARAM_READWRITE));
411
412          g_object_class_install_property (object_class,
413                                           PROP_AUTO_AWAY,
414                                           g_param_spec_boolean ("auto-away",
415                                                                 "Automatic set presence to away",
416                                                                 "Should it set presence to away if inactive",
417                                                                 FALSE,
418                                                                 G_PARAM_READWRITE));
419
420          g_object_class_install_property (object_class,
421                                           PROP_USE_NM,
422                                           g_param_spec_boolean ("use-nm",
423                                                                 "Use Network Manager",
424                                                                 "Set presence according to Network Manager",
425                                                                 FALSE,
426                                                                 G_PARAM_READWRITE));
427
428         g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
429 }
430
431 static TpConnectionPresenceType
432 empathy_idle_get_actual_presence (EmpathyIdle *idle, GError **error)
433 {
434         McPresence presence;
435         EmpathyIdlePriv *priv = GET_PRIV (idle);
436
437         presence = mission_control_get_presence_actual (priv->mc, error);
438
439         switch (presence) {
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;
452         default:
453                 return TP_CONNECTION_PRESENCE_TYPE_UNSET;
454         }
455 }
456
457 static void
458 empathy_idle_init (EmpathyIdle *idle)
459 {
460         DBusGConnection *system_bus;
461         GError          *error = NULL;
462         EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
463                 EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
464
465         idle->priv = priv;
466         priv->is_idle = FALSE;
467         priv->mc = empathy_mission_control_dup_singleton ();
468         priv->state = empathy_idle_get_actual_presence (idle, &error);
469         if (error) {
470                 DEBUG ("Error getting actual presence: %s", error->message);
471
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);
477         }
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);
481                 priv->status = NULL;
482
483                 if (error) {
484                         DEBUG ("Error getting actual presence message: %s", error->message);
485                         g_clear_error (&error);
486                 }
487         }
488
489         dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
490                                      "PresenceChanged",
491                                      G_CALLBACK (idle_presence_changed_cb),
492                                      idle, NULL);
493
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",
500                                          G_TYPE_BOOLEAN,
501                                          G_TYPE_INVALID);
502                 dbus_g_proxy_connect_signal (priv->gs_proxy, "SessionIdleChanged",
503                                              G_CALLBACK (idle_session_idle_changed_cb),
504                                              idle, NULL);
505         } else {
506                 DEBUG ("Failed to get gs proxy");
507         }
508
509
510         system_bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
511         if (!system_bus) {
512                 DEBUG ("Failed to get system bus: %s",
513                         error ? error->message : "No error given");
514         } else {
515                 priv->nm_proxy = dbus_g_proxy_new_for_name (system_bus,
516                                                             "org.freedesktop.NetworkManager",
517                                                             "/org/freedesktop/NetworkManager",
518                                                             "org.freedesktop.NetworkManager");
519         }
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),
525                                              idle, NULL);
526         } else {
527                 DEBUG ("Failed to get nm proxy");
528         }
529
530         priv->nm_connected = TRUE;
531 }
532
533 EmpathyIdle *
534 empathy_idle_dup_singleton (void)
535 {
536         return g_object_new (EMPATHY_TYPE_IDLE, NULL);
537 }
538
539 TpConnectionPresenceType
540 empathy_idle_get_state (EmpathyIdle *idle)
541 {
542         EmpathyIdlePriv *priv;
543
544         priv = GET_PRIV (idle);
545
546         return priv->state;
547 }
548
549 void
550 empathy_idle_set_state (EmpathyIdle *idle,
551                         TpConnectionPresenceType   state)
552 {
553         EmpathyIdlePriv *priv;
554
555         priv = GET_PRIV (idle);
556
557         empathy_idle_set_presence (idle, state, priv->status);
558 }
559
560 const gchar *
561 empathy_idle_get_status (EmpathyIdle *idle)
562 {
563         EmpathyIdlePriv *priv;
564
565         priv = GET_PRIV (idle);
566
567         if (!priv->status) {
568                 return empathy_presence_get_default_message (priv->state);
569         }
570
571         return priv->status;
572 }
573
574 void
575 empathy_idle_set_status (EmpathyIdle *idle,
576                          const gchar *status)
577 {
578         EmpathyIdlePriv *priv;
579
580         priv = GET_PRIV (idle);
581
582         empathy_idle_set_presence (idle, priv->state, status);
583 }
584
585 TpConnectionPresenceType
586 empathy_idle_get_flash_state (EmpathyIdle *idle)
587 {
588         EmpathyIdlePriv *priv;
589
590         priv = GET_PRIV (idle);
591
592         return priv->flash_state;
593 }
594
595 void
596 empathy_idle_set_flash_state (EmpathyIdle *idle,
597                               TpConnectionPresenceType   state)
598 {
599         EmpathyIdlePriv *priv;
600
601         priv = GET_PRIV (idle);
602
603         priv->flash_state = state;
604
605         if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
606         }
607
608         g_object_notify (G_OBJECT (idle), "flash-state");
609 }
610
611 static void
612 empathy_idle_do_set_presence (EmpathyIdle *idle,
613                            TpConnectionPresenceType   state,
614                            const gchar *status)
615 {
616         McPresence mc_state = MC_PRESENCE_UNSET;
617         EmpathyIdlePriv *priv = GET_PRIV (idle);
618
619         switch (state) {
620                 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
621                         mc_state = MC_PRESENCE_OFFLINE;
622                         break;
623                 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
624                         mc_state = MC_PRESENCE_AVAILABLE;
625                         break;
626                 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
627                         mc_state = MC_PRESENCE_AWAY;
628                         break;
629                 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
630                         mc_state = MC_PRESENCE_EXTENDED_AWAY;
631                         break;
632                 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
633                         mc_state = MC_PRESENCE_HIDDEN;
634                         break;
635                 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
636                         mc_state = MC_PRESENCE_DO_NOT_DISTURB;
637                         break;
638                 default:
639                         g_assert_not_reached ();
640         }
641
642         mission_control_set_presence (priv->mc, mc_state, status, NULL, NULL);
643 }
644
645 void
646 empathy_idle_set_presence (EmpathyIdle *idle,
647                            TpConnectionPresenceType   state,
648                            const gchar *status)
649 {
650         EmpathyIdlePriv *priv;
651         const gchar     *default_status;
652
653         priv = GET_PRIV (idle);
654
655         DEBUG ("Changing presence to %s (%d)", status, state);
656
657         /* Do not set translated default messages */
658         default_status = empathy_presence_get_default_message (state);
659         if (!tp_strdiff (status, default_status)) {
660                 status = NULL;
661         }
662
663         if (!priv->nm_connected) {
664                 DEBUG ("NM not connected");
665
666                 priv->nm_saved_state = state;
667                 if (tp_strdiff (priv->status, status)) {
668                         g_free (priv->status);
669                         priv->status = NULL;
670                         if (!EMP_STR_EMPTY (status)) {
671                                 priv->status = g_strdup (status);
672                         }
673                         g_object_notify (G_OBJECT (idle), "status");
674                 }
675
676                 return;
677         }
678
679         empathy_idle_do_set_presence (idle, state, status);
680 }
681
682 gboolean
683 empathy_idle_get_auto_away (EmpathyIdle *idle)
684 {
685         EmpathyIdlePriv *priv = GET_PRIV (idle);
686
687         return priv->auto_away;
688 }
689
690 void
691 empathy_idle_set_auto_away (EmpathyIdle *idle,
692                             gboolean     auto_away)
693 {
694         EmpathyIdlePriv *priv = GET_PRIV (idle);
695
696         priv->auto_away = auto_away;
697
698         g_object_notify (G_OBJECT (idle), "auto-away");
699 }
700
701 gboolean
702 empathy_idle_get_use_nm (EmpathyIdle *idle)
703 {
704         EmpathyIdlePriv *priv = GET_PRIV (idle);
705
706         return priv->use_nm;
707 }
708
709 void
710 empathy_idle_set_use_nm (EmpathyIdle *idle,
711                          gboolean     use_nm)
712 {
713         EmpathyIdlePriv *priv = GET_PRIV (idle);
714
715         if (!priv->nm_proxy || use_nm == priv->use_nm) {
716                 return;
717         }
718
719         priv->use_nm = use_nm;
720
721         if (use_nm) {
722                 guint   nm_status;
723                 GError *error = NULL;
724
725                 dbus_g_proxy_call (priv->nm_proxy, "state",
726                                    &error,
727                                    G_TYPE_INVALID,
728                                    G_TYPE_UINT, &nm_status,
729                                    G_TYPE_INVALID);
730
731                 if (error) {
732                         DEBUG ("Couldn't get NM state: %s", error->message);
733                         g_clear_error (&error);
734                         nm_status = NM_STATE_ASLEEP;
735                 }
736
737                 idle_nm_state_change_cb (priv->nm_proxy, nm_status, idle);
738         } else {
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);
742                 }
743                 priv->nm_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
744         }
745
746         g_object_notify (G_OBJECT (idle), "use-nm");
747 }
748