]> git.0d.be Git - empathy.git/blob - libempathy/empathy-idle.c
Merge branch 'ft_rework'
[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         DEBUG ("Presence changed to '%s' (%d)", status, state);
95
96         g_free (priv->status);
97         priv->state = state;
98         priv->status = NULL;
99         if (!EMP_STR_EMPTY (status)) {
100                 priv->status = g_strdup (status);
101         }
102
103         g_object_notify (G_OBJECT (idle), "state");
104         g_object_notify (G_OBJECT (idle), "status");
105 }
106
107 static gboolean
108 idle_ext_away_cb (EmpathyIdle *idle)
109 {
110         EmpathyIdlePriv *priv;
111
112         priv = GET_PRIV (idle);
113
114         DEBUG ("Going to extended autoaway");
115         empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY);
116         priv->ext_away_timeout = 0;
117
118         return FALSE;
119 }
120
121 static void
122 idle_ext_away_stop (EmpathyIdle *idle)
123 {
124         EmpathyIdlePriv *priv;
125
126         priv = GET_PRIV (idle);
127
128         if (priv->ext_away_timeout) {
129                 g_source_remove (priv->ext_away_timeout);
130                 priv->ext_away_timeout = 0;
131         }
132 }
133
134 static void
135 idle_ext_away_start (EmpathyIdle *idle)
136 {
137         EmpathyIdlePriv *priv;
138
139         priv = GET_PRIV (idle);
140
141         if (priv->ext_away_timeout != 0) {
142                 return;
143         }
144         priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
145                                                         (GSourceFunc) idle_ext_away_cb,
146                                                         idle);
147 }
148
149 static void
150 idle_session_idle_changed_cb (DBusGProxy  *gs_proxy,
151                               gboolean     is_idle,
152                               EmpathyIdle *idle)
153 {
154         EmpathyIdlePriv *priv;
155
156         priv = GET_PRIV (idle);
157
158         DEBUG ("Session idle state changed, %s -> %s",
159                 priv->is_idle ? "yes" : "no",
160                 is_idle ? "yes" : "no");
161
162         if (!priv->auto_away ||
163             (priv->nm_saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET &&
164              (priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
165               priv->state == TP_CONNECTION_PRESENCE_TYPE_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;
169                 return;
170         }
171
172         if (is_idle && !priv->is_idle) {
173                 TpConnectionPresenceType new_state;
174                 /* We are now idle */
175
176                 idle_ext_away_start (idle);
177
178                 if (priv->nm_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
179                         /* We are disconnected, when coming back from away
180                          * we want to restore the presence before the
181                          * disconnection. */
182                         priv->away_saved_state = priv->nm_saved_state;
183                 } else {
184                         priv->away_saved_state = priv->state;
185                 }
186
187                 new_state = TP_CONNECTION_PRESENCE_TYPE_AWAY;
188                 if (priv->state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
189                         new_state = TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY;
190                 }
191
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 */
198
199                 idle_ext_away_stop (idle);
200
201                 if (priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_AWAY ||
202                     priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
203                         priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
204                         new_status = NULL;
205                 } else {
206                         new_status = priv->status;
207                 }
208
209                 DEBUG ("Restoring state to %d, reset status to %s",
210                         priv->away_saved_state, new_status);
211
212                 empathy_idle_set_presence (idle,
213                                            priv->away_saved_state,
214                                            new_status);
215
216                 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
217         }
218
219         priv->is_idle = is_idle;
220 }
221
222 static void
223 idle_nm_state_change_cb (DBusGProxy  *proxy,
224                          guint        state,
225                          EmpathyIdle *idle)
226 {
227         EmpathyIdlePriv *priv;
228         gboolean         old_nm_connected;
229         gboolean         new_nm_connected;
230
231         priv = GET_PRIV (idle);
232
233         if (!priv->use_nm
234             || priv->nm_saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
235                 return;
236         }
237
238         old_nm_connected = priv->nm_connected;
239         new_nm_connected = !(state == NM_STATE_CONNECTING ||
240                              state == NM_STATE_DISCONNECTED);
241         priv->nm_connected = TRUE; /* To be sure _set_state will work */
242
243         DEBUG ("New network state %d", state);
244
245         if (old_nm_connected && !new_nm_connected) {
246                 /* We are no more connected */
247                 DEBUG ("Disconnected: Save state %d (%s)",
248                                 priv->state, priv->status);
249                 priv->nm_saved_state = priv->state;
250                 g_free (priv->nm_saved_status);
251                 priv->nm_saved_status = g_strdup (priv->status);
252                 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
253         }
254         else if (!old_nm_connected && new_nm_connected) {
255                 /* We are now connected */
256                 DEBUG ("Reconnected: Restore state %d (%s)",
257                                 priv->nm_saved_state, priv->nm_saved_status);
258                 empathy_idle_set_presence (idle,
259                                 priv->nm_saved_state,
260                                 priv->nm_saved_status);
261                 priv->nm_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
262                 g_free (priv->nm_saved_status);
263                 priv->nm_saved_status = NULL;
264         }
265
266         priv->nm_connected = new_nm_connected;
267 }
268
269 static void
270 idle_finalize (GObject *object)
271 {
272         EmpathyIdlePriv *priv;
273
274         priv = GET_PRIV (object);
275
276         g_free (priv->status);
277         g_object_unref (priv->mc);
278
279         if (priv->gs_proxy) {
280                 g_object_unref (priv->gs_proxy);
281         }
282
283         idle_ext_away_stop (EMPATHY_IDLE (object));
284 }
285
286 static GObject *
287 idle_constructor (GType type,
288                   guint n_props,
289                   GObjectConstructParam *props)
290 {
291         GObject *retval;
292
293         if (idle_singleton) {
294                 retval = g_object_ref (idle_singleton);
295         } else {
296                 retval = G_OBJECT_CLASS (empathy_idle_parent_class)->constructor
297                         (type, n_props, props);
298
299                 idle_singleton = EMPATHY_IDLE (retval);
300                 g_object_add_weak_pointer (retval, (gpointer) &idle_singleton);
301         }
302
303         return retval;
304 }
305
306 static void
307 idle_get_property (GObject    *object,
308                    guint       param_id,
309                    GValue     *value,
310                    GParamSpec *pspec)
311 {
312         EmpathyIdlePriv *priv;
313         EmpathyIdle     *idle;
314
315         priv = GET_PRIV (object);
316         idle = EMPATHY_IDLE (object);
317
318         switch (param_id) {
319         case PROP_STATE:
320                 g_value_set_enum (value, empathy_idle_get_state (idle));
321                 break;
322         case PROP_STATUS:
323                 g_value_set_string (value, empathy_idle_get_status (idle));
324                 break;
325         case PROP_FLASH_STATE:
326                 g_value_set_enum (value, empathy_idle_get_flash_state (idle));
327                 break;
328         case PROP_AUTO_AWAY:
329                 g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
330                 break;
331         case PROP_USE_NM:
332                 g_value_set_boolean (value, empathy_idle_get_use_nm (idle));
333                 break;
334         default:
335                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
336                 break;
337         };
338 }
339
340 static void
341 idle_set_property (GObject      *object,
342                    guint         param_id,
343                    const GValue *value,
344                    GParamSpec   *pspec)
345 {
346         EmpathyIdlePriv *priv;
347         EmpathyIdle     *idle;
348
349         priv = GET_PRIV (object);
350         idle = EMPATHY_IDLE (object);
351
352         switch (param_id) {
353         case PROP_STATE:
354                 empathy_idle_set_state (idle, g_value_get_enum (value));
355                 break;
356         case PROP_STATUS:
357                 empathy_idle_set_status (idle, g_value_get_string (value));
358                 break;
359         case PROP_FLASH_STATE:
360                 empathy_idle_set_flash_state (idle, g_value_get_enum (value));
361                 break;
362         case PROP_AUTO_AWAY:
363                 empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
364                 break;
365         case PROP_USE_NM:
366                 empathy_idle_set_use_nm (idle, g_value_get_boolean (value));
367                 break;
368         default:
369                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
370                 break;
371         };
372 }
373
374 static void
375 empathy_idle_class_init (EmpathyIdleClass *klass)
376 {
377         GObjectClass *object_class = G_OBJECT_CLASS (klass);
378
379         object_class->finalize = idle_finalize;
380         object_class->constructor = idle_constructor;
381         object_class->get_property = idle_get_property;
382         object_class->set_property = idle_set_property;
383
384         g_object_class_install_property (object_class,
385                                          PROP_STATE,
386                                          g_param_spec_uint ("state",
387                                                             "state",
388                                                             "state",
389                                                             0, NUM_TP_CONNECTION_PRESENCE_TYPES,
390                                                             TP_CONNECTION_PRESENCE_TYPE_UNSET,
391                                                             G_PARAM_READWRITE));
392         g_object_class_install_property (object_class,
393                                          PROP_STATUS,
394                                          g_param_spec_string ("status",
395                                                               "status",
396                                                               "status",
397                                                               NULL,
398                                                               G_PARAM_READWRITE));
399         g_object_class_install_property (object_class,
400                                          PROP_FLASH_STATE,
401                                          g_param_spec_uint ("flash-state",
402                                                             "flash-state",
403                                                             "flash-state",
404                                                             0, NUM_TP_CONNECTION_PRESENCE_TYPES,
405                                                             TP_CONNECTION_PRESENCE_TYPE_UNSET,
406                                                             G_PARAM_READWRITE));
407
408          g_object_class_install_property (object_class,
409                                           PROP_AUTO_AWAY,
410                                           g_param_spec_boolean ("auto-away",
411                                                                 "Automatic set presence to away",
412                                                                 "Should it set presence to away if inactive",
413                                                                 FALSE,
414                                                                 G_PARAM_READWRITE));
415
416          g_object_class_install_property (object_class,
417                                           PROP_USE_NM,
418                                           g_param_spec_boolean ("use-nm",
419                                                                 "Use Network Manager",
420                                                                 "Set presence according to Network Manager",
421                                                                 FALSE,
422                                                                 G_PARAM_READWRITE));
423
424         g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
425 }
426
427 static TpConnectionPresenceType
428 empathy_idle_get_actual_presence (EmpathyIdle *idle, GError **error)
429 {
430         McPresence presence;
431         EmpathyIdlePriv *priv = GET_PRIV (idle);
432
433         presence = mission_control_get_presence_actual (priv->mc, error);
434
435         switch (presence) {
436         case MC_PRESENCE_OFFLINE:
437                 return TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
438         case MC_PRESENCE_AVAILABLE:
439                 return TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
440         case MC_PRESENCE_AWAY:
441                 return TP_CONNECTION_PRESENCE_TYPE_AWAY;
442         case MC_PRESENCE_EXTENDED_AWAY:
443                 return TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY;
444         case MC_PRESENCE_HIDDEN:
445                 return TP_CONNECTION_PRESENCE_TYPE_HIDDEN;
446         case MC_PRESENCE_DO_NOT_DISTURB:
447                 return TP_CONNECTION_PRESENCE_TYPE_BUSY;
448         default:
449                 return TP_CONNECTION_PRESENCE_TYPE_UNSET;
450         }
451 }
452
453 static void
454 empathy_idle_init (EmpathyIdle *idle)
455 {
456         DBusGConnection *system_bus;
457         GError          *error = NULL;
458         EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
459                 EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
460
461         idle->priv = priv;
462         priv->is_idle = FALSE;
463         priv->mc = empathy_mission_control_dup_singleton ();
464         priv->state = empathy_idle_get_actual_presence (idle, &error);
465         if (error) {
466                 DEBUG ("Error getting actual presence: %s", error->message);
467
468                 priv->state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
469                 g_clear_error (&error);
470         }
471         priv->status = mission_control_get_presence_message_actual (priv->mc, &error);
472         if (error || EMP_STR_EMPTY (priv->status)) {
473                 g_free (priv->status);
474                 priv->status = NULL;
475
476                 if (error) {
477                         DEBUG ("Error getting actual presence message: %s", error->message);
478                         g_clear_error (&error);
479                 }
480         }
481
482         dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
483                                      "PresenceChanged",
484                                      G_CALLBACK (idle_presence_changed_cb),
485                                      idle, NULL);
486
487         priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
488                                                     "org.gnome.ScreenSaver",
489                                                     "/org/gnome/ScreenSaver",
490                                                     "org.gnome.ScreenSaver");
491         if (priv->gs_proxy) {
492                 dbus_g_proxy_add_signal (priv->gs_proxy, "SessionIdleChanged",
493                                          G_TYPE_BOOLEAN,
494                                          G_TYPE_INVALID);
495                 dbus_g_proxy_connect_signal (priv->gs_proxy, "SessionIdleChanged",
496                                              G_CALLBACK (idle_session_idle_changed_cb),
497                                              idle, NULL);
498         } else {
499                 DEBUG ("Failed to get gs proxy");
500         }
501
502
503         system_bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
504         if (!system_bus) {
505                 DEBUG ("Failed to get system bus: %s",
506                         error ? error->message : "No error given");
507         } else {
508                 priv->nm_proxy = dbus_g_proxy_new_for_name (system_bus,
509                                                             "org.freedesktop.NetworkManager",
510                                                             "/org/freedesktop/NetworkManager",
511                                                             "org.freedesktop.NetworkManager");
512         }
513         if (priv->nm_proxy) {
514                 dbus_g_proxy_add_signal (priv->nm_proxy, "StateChange",
515                                          G_TYPE_UINT, G_TYPE_INVALID);
516                 dbus_g_proxy_connect_signal (priv->nm_proxy, "StateChange",
517                                              G_CALLBACK (idle_nm_state_change_cb),
518                                              idle, NULL);
519         } else {
520                 DEBUG ("Failed to get nm proxy");
521         }
522
523         priv->nm_connected = TRUE;
524 }
525
526 EmpathyIdle *
527 empathy_idle_dup_singleton (void)
528 {
529         return g_object_new (EMPATHY_TYPE_IDLE, NULL);
530 }
531
532 TpConnectionPresenceType
533 empathy_idle_get_state (EmpathyIdle *idle)
534 {
535         EmpathyIdlePriv *priv;
536
537         priv = GET_PRIV (idle);
538
539         return priv->state;
540 }
541
542 void
543 empathy_idle_set_state (EmpathyIdle *idle,
544                         TpConnectionPresenceType   state)
545 {
546         EmpathyIdlePriv *priv;
547
548         priv = GET_PRIV (idle);
549
550         empathy_idle_set_presence (idle, state, priv->status);
551 }
552
553 const gchar *
554 empathy_idle_get_status (EmpathyIdle *idle)
555 {
556         EmpathyIdlePriv *priv;
557
558         priv = GET_PRIV (idle);
559
560         if (!priv->status) {
561                 return empathy_presence_get_default_message (priv->state);
562         }
563
564         return priv->status;
565 }
566
567 void
568 empathy_idle_set_status (EmpathyIdle *idle,
569                          const gchar *status)
570 {
571         EmpathyIdlePriv *priv;
572
573         priv = GET_PRIV (idle);
574
575         empathy_idle_set_presence (idle, priv->state, status);
576 }
577
578 TpConnectionPresenceType
579 empathy_idle_get_flash_state (EmpathyIdle *idle)
580 {
581         EmpathyIdlePriv *priv;
582
583         priv = GET_PRIV (idle);
584
585         return priv->flash_state;
586 }
587
588 void
589 empathy_idle_set_flash_state (EmpathyIdle *idle,
590                               TpConnectionPresenceType   state)
591 {
592         EmpathyIdlePriv *priv;
593
594         priv = GET_PRIV (idle);
595
596         priv->flash_state = state;
597
598         if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
599         }
600
601         g_object_notify (G_OBJECT (idle), "flash-state");
602 }
603
604 static void
605 empathy_idle_do_set_presence (EmpathyIdle *idle,
606                            TpConnectionPresenceType   state,
607                            const gchar *status)
608 {
609         McPresence mc_state = MC_PRESENCE_UNSET;
610         EmpathyIdlePriv *priv = GET_PRIV (idle);
611
612         switch (state) {
613                 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
614                         mc_state = MC_PRESENCE_OFFLINE;
615                         break;
616                 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
617                         mc_state = MC_PRESENCE_AVAILABLE;
618                         break;
619                 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
620                         mc_state = MC_PRESENCE_AWAY;
621                         break;
622                 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
623                         mc_state = MC_PRESENCE_EXTENDED_AWAY;
624                         break;
625                 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
626                         mc_state = MC_PRESENCE_HIDDEN;
627                         break;
628                 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
629                         mc_state = MC_PRESENCE_DO_NOT_DISTURB;
630                         break;
631                 default:
632                         g_assert_not_reached ();
633         }
634
635         mission_control_set_presence (priv->mc, mc_state, status, NULL, NULL);
636 }
637
638 void
639 empathy_idle_set_presence (EmpathyIdle *idle,
640                            TpConnectionPresenceType   state,
641                            const gchar *status)
642 {
643         EmpathyIdlePriv *priv;
644         const gchar     *default_status;
645
646         priv = GET_PRIV (idle);
647
648         DEBUG ("Changing presence to %s (%d)", status, state);
649
650         /* Do not set translated default messages */
651         default_status = empathy_presence_get_default_message (state);
652         if (!tp_strdiff (status, default_status)) {
653                 status = NULL;
654         }
655
656         if (!priv->nm_connected) {
657                 DEBUG ("NM not connected");
658
659                 priv->nm_saved_state = state;
660                 if (tp_strdiff (priv->status, status)) {
661                         g_free (priv->status);
662                         priv->status = NULL;
663                         if (!EMP_STR_EMPTY (status)) {
664                                 priv->status = g_strdup (status);
665                         }
666                         g_object_notify (G_OBJECT (idle), "status");
667                 }
668
669                 return;
670         }
671
672         empathy_idle_do_set_presence (idle, state, status);
673 }
674
675 gboolean
676 empathy_idle_get_auto_away (EmpathyIdle *idle)
677 {
678         EmpathyIdlePriv *priv = GET_PRIV (idle);
679
680         return priv->auto_away;
681 }
682
683 void
684 empathy_idle_set_auto_away (EmpathyIdle *idle,
685                             gboolean     auto_away)
686 {
687         EmpathyIdlePriv *priv = GET_PRIV (idle);
688
689         priv->auto_away = auto_away;
690
691         g_object_notify (G_OBJECT (idle), "auto-away");
692 }
693
694 gboolean
695 empathy_idle_get_use_nm (EmpathyIdle *idle)
696 {
697         EmpathyIdlePriv *priv = GET_PRIV (idle);
698
699         return priv->use_nm;
700 }
701
702 void
703 empathy_idle_set_use_nm (EmpathyIdle *idle,
704                          gboolean     use_nm)
705 {
706         EmpathyIdlePriv *priv = GET_PRIV (idle);
707
708         if (!priv->nm_proxy || use_nm == priv->use_nm) {
709                 return;
710         }
711
712         priv->use_nm = use_nm;
713
714         if (use_nm) {
715                 guint   nm_status;
716                 GError *error = NULL;
717
718                 dbus_g_proxy_call (priv->nm_proxy, "state",
719                                    &error,
720                                    G_TYPE_INVALID,
721                                    G_TYPE_UINT, &nm_status,
722                                    G_TYPE_INVALID);
723
724                 if (error) {
725                         DEBUG ("Couldn't get NM state: %s", error->message);
726                         g_clear_error (&error);
727                         nm_status = NM_STATE_ASLEEP;
728                 }
729                 
730                 idle_nm_state_change_cb (priv->nm_proxy, nm_status, idle);
731         } else {
732                 priv->nm_connected = TRUE;
733                 if (priv->nm_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
734                         empathy_idle_set_state (idle, priv->nm_saved_state);
735                 }
736                 priv->nm_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
737         }
738
739         g_object_notify (G_OBJECT (idle), "use-nm");
740 }
741