]> git.0d.be Git - empathy.git/blob - libempathy/empathy-idle.c
f9dd4fca891dd06c194e7419e4e752b42ad0127b
[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         McPresence      state;
49         gchar          *status;
50         McPresence      flash_state;
51         gboolean        auto_away;
52         gboolean        use_nm;
53
54         McPresence      away_saved_state;
55         McPresence      nm_saved_state;
56
57         gboolean        is_idle;
58         gboolean        nm_connected;
59         guint           ext_away_timeout;
60 } EmpathyIdlePriv;
61
62 typedef enum {
63         NM_STATE_UNKNOWN,
64         NM_STATE_ASLEEP,
65         NM_STATE_CONNECTING,
66         NM_STATE_CONNECTED,
67         NM_STATE_DISCONNECTED
68 } NMState;
69
70 enum {
71         PROP_0,
72         PROP_STATE,
73         PROP_STATUS,
74         PROP_FLASH_STATE,
75         PROP_AUTO_AWAY,
76         PROP_USE_NM
77 };
78
79 G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT);
80
81 static EmpathyIdle * idle_singleton = NULL;
82
83 static void
84 idle_presence_changed_cb (MissionControl *mc,
85                           McPresence      state,
86                           gchar          *status,
87                           EmpathyIdle    *idle)
88 {
89         EmpathyIdlePriv *priv;
90
91         priv = GET_PRIV (idle);
92
93         DEBUG ("Presence changed to '%s' (%d)", status, state);
94
95         g_free (priv->status);
96         priv->state = state;
97         priv->status = NULL;
98         if (!EMP_STR_EMPTY (status)) {
99                 priv->status = g_strdup (status);
100         }
101
102         g_object_notify (G_OBJECT (idle), "state");
103         g_object_notify (G_OBJECT (idle), "status");
104 }
105
106 static gboolean
107 idle_ext_away_cb (EmpathyIdle *idle)
108 {
109         EmpathyIdlePriv *priv;
110
111         priv = GET_PRIV (idle);
112
113         DEBUG ("Going to extended autoaway");
114         empathy_idle_set_state (idle, MC_PRESENCE_EXTENDED_AWAY);
115         priv->ext_away_timeout = 0;
116
117         return FALSE;
118 }
119
120 static void
121 idle_ext_away_stop (EmpathyIdle *idle)
122 {
123         EmpathyIdlePriv *priv;
124
125         priv = GET_PRIV (idle);
126
127         if (priv->ext_away_timeout) {
128                 g_source_remove (priv->ext_away_timeout);
129                 priv->ext_away_timeout = 0;
130         }
131 }
132
133 static void
134 idle_ext_away_start (EmpathyIdle *idle)
135 {
136         EmpathyIdlePriv *priv;
137
138         priv = GET_PRIV (idle);
139
140         if (priv->ext_away_timeout != 0) {
141                 return;
142         }
143         priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
144                                                         (GSourceFunc) idle_ext_away_cb,
145                                                         idle);
146 }
147
148 static void
149 idle_session_idle_changed_cb (DBusGProxy  *gs_proxy,
150                               gboolean     is_idle,
151                               EmpathyIdle *idle)
152 {
153         EmpathyIdlePriv *priv;
154
155         priv = GET_PRIV (idle);
156
157         DEBUG ("Session idle state changed, %s -> %s",
158                 priv->is_idle ? "yes" : "no",
159                 is_idle ? "yes" : "no");
160
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;
168                 return;
169         }
170
171         if (is_idle && !priv->is_idle) {
172                 McPresence new_state;
173                 /* We are now idle */
174
175                 idle_ext_away_start (idle);
176
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
180                          * disconnection. */
181                         priv->away_saved_state = priv->nm_saved_state;
182                 } else {
183                         priv->away_saved_state = priv->state;
184                 }
185
186                 new_state = MC_PRESENCE_AWAY;
187                 if (priv->state == MC_PRESENCE_EXTENDED_AWAY) {
188                         new_state = MC_PRESENCE_EXTENDED_AWAY;
189                 }
190
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 */
197
198                 idle_ext_away_stop (idle);
199
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;
203                         new_status = NULL;
204                 } else {
205                         new_status = priv->status;
206                 }
207
208                 DEBUG ("Restoring state to %d, reset status to %s",
209                         priv->away_saved_state, new_status);
210
211                 empathy_idle_set_presence (idle,
212                                            priv->away_saved_state,
213                                            new_status);
214
215                 priv->away_saved_state = MC_PRESENCE_UNSET;
216         }
217
218         priv->is_idle = is_idle;
219 }
220
221 static void
222 idle_nm_state_change_cb (DBusGProxy  *proxy,
223                          guint        state,
224                          EmpathyIdle *idle)
225 {
226         EmpathyIdlePriv *priv;
227         gboolean         old_nm_connected;
228         gboolean         new_nm_connected;
229
230         priv = GET_PRIV (idle);
231
232         if (!priv->use_nm) {
233                 return;
234         }
235
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 */
240
241         DEBUG ("New network state %d", state);
242
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);
248         }
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;
254         }
255
256         priv->nm_connected = new_nm_connected;
257 }
258
259 static void
260 idle_finalize (GObject *object)
261 {
262         EmpathyIdlePriv *priv;
263
264         priv = GET_PRIV (object);
265
266         g_free (priv->status);
267         g_object_unref (priv->mc);
268
269         if (priv->gs_proxy) {
270                 g_object_unref (priv->gs_proxy);
271         }
272
273         idle_ext_away_stop (EMPATHY_IDLE (object));
274 }
275
276 static GObject *
277 idle_constructor (GType type,
278                   guint n_props,
279                   GObjectConstructParam *props)
280 {
281         GObject *retval;
282
283         if (idle_singleton) {
284                 retval = g_object_ref (idle_singleton);
285         } else {
286                 retval = G_OBJECT_CLASS (empathy_idle_parent_class)->constructor
287                         (type, n_props, props);
288
289                 idle_singleton = EMPATHY_IDLE (retval);
290                 g_object_add_weak_pointer (retval, (gpointer *) &idle_singleton);
291         }
292
293         return retval;
294 }
295
296 static void
297 idle_get_property (GObject    *object,
298                    guint       param_id,
299                    GValue     *value,
300                    GParamSpec *pspec)
301 {
302         EmpathyIdlePriv *priv;
303         EmpathyIdle     *idle;
304
305         priv = GET_PRIV (object);
306         idle = EMPATHY_IDLE (object);
307
308         switch (param_id) {
309         case PROP_STATE:
310                 g_value_set_enum (value, empathy_idle_get_state (idle));
311                 break;
312         case PROP_STATUS:
313                 g_value_set_string (value, empathy_idle_get_status (idle));
314                 break;
315         case PROP_FLASH_STATE:
316                 g_value_set_enum (value, empathy_idle_get_flash_state (idle));
317                 break;
318         case PROP_AUTO_AWAY:
319                 g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
320                 break;
321         case PROP_USE_NM:
322                 g_value_set_boolean (value, empathy_idle_get_use_nm (idle));
323                 break;
324         default:
325                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
326                 break;
327         };
328 }
329
330 static void
331 idle_set_property (GObject      *object,
332                    guint         param_id,
333                    const GValue *value,
334                    GParamSpec   *pspec)
335 {
336         EmpathyIdlePriv *priv;
337         EmpathyIdle     *idle;
338
339         priv = GET_PRIV (object);
340         idle = EMPATHY_IDLE (object);
341
342         switch (param_id) {
343         case PROP_STATE:
344                 empathy_idle_set_state (idle, g_value_get_enum (value));
345                 break;
346         case PROP_STATUS:
347                 empathy_idle_set_status (idle, g_value_get_string (value));
348                 break;
349         case PROP_FLASH_STATE:
350                 empathy_idle_set_flash_state (idle, g_value_get_enum (value));
351                 break;
352         case PROP_AUTO_AWAY:
353                 empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
354                 break;
355         case PROP_USE_NM:
356                 empathy_idle_set_use_nm (idle, g_value_get_boolean (value));
357                 break;
358         default:
359                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
360                 break;
361         };
362 }
363
364 static void
365 empathy_idle_class_init (EmpathyIdleClass *klass)
366 {
367         GObjectClass *object_class = G_OBJECT_CLASS (klass);
368
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;
373
374         g_object_class_install_property (object_class,
375                                          PROP_STATE,
376                                          g_param_spec_enum ("state",
377                                                             "state",
378                                                             "state",
379                                                             MC_TYPE_PRESENCE,
380                                                             MC_PRESENCE_AVAILABLE,
381                                                             G_PARAM_READWRITE));
382         g_object_class_install_property (object_class,
383                                          PROP_STATUS,
384                                          g_param_spec_string ("status",
385                                                               "status",
386                                                               "status",
387                                                               NULL,
388                                                               G_PARAM_READWRITE));
389         g_object_class_install_property (object_class,
390                                          PROP_FLASH_STATE,
391                                          g_param_spec_enum ("flash-state",
392                                                             "flash-state",
393                                                             "flash-state",
394                                                             MC_TYPE_PRESENCE,
395                                                             MC_PRESENCE_UNSET,
396                                                             G_PARAM_READWRITE));
397
398          g_object_class_install_property (object_class,
399                                           PROP_AUTO_AWAY,
400                                           g_param_spec_boolean ("auto-away",
401                                                                 "Automatic set presence to away",
402                                                                 "Should it set presence to away if inactive",
403                                                                 FALSE,
404                                                                 G_PARAM_READWRITE));
405
406          g_object_class_install_property (object_class,
407                                           PROP_USE_NM,
408                                           g_param_spec_boolean ("use-nm",
409                                                                 "Use Network Manager",
410                                                                 "Set presence according to Network Manager",
411                                                                 FALSE,
412                                                                 G_PARAM_READWRITE));
413
414         g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
415 }
416
417 static void
418 empathy_idle_init (EmpathyIdle *idle)
419 {
420         DBusGConnection *system_bus;
421         GError          *error = NULL;
422         EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
423                 EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
424
425         idle->priv = priv;
426         priv->is_idle = FALSE;
427         priv->mc = empathy_mission_control_dup_singleton ();
428         priv->state = mission_control_get_presence_actual (priv->mc, &error);
429         if (error) {
430                 DEBUG ("Error getting actual presence: %s", error->message);
431
432                 priv->state = MC_PRESENCE_UNSET;
433                 g_clear_error (&error);
434         }
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);
438                 priv->status = NULL;
439
440                 if (error) {
441                         DEBUG ("Error getting actual presence message: %s", error->message);
442                         g_clear_error (&error);
443                 }
444         }
445
446         dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
447                                      "PresenceChanged",
448                                      G_CALLBACK (idle_presence_changed_cb),
449                                      idle, NULL);
450
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",
457                                          G_TYPE_BOOLEAN,
458                                          G_TYPE_INVALID);
459                 dbus_g_proxy_connect_signal (priv->gs_proxy, "SessionIdleChanged",
460                                              G_CALLBACK (idle_session_idle_changed_cb),
461                                              idle, NULL);
462         } else {
463                 DEBUG ("Failed to get gs proxy");
464         }
465
466
467         system_bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
468         if (!system_bus) {
469                 DEBUG ("Failed to get system bus: %s",
470                         error ? error->message : "No error given");
471         } else {
472                 priv->nm_proxy = dbus_g_proxy_new_for_name (system_bus,
473                                                             "org.freedesktop.NetworkManager",
474                                                             "/org/freedesktop/NetworkManager",
475                                                             "org.freedesktop.NetworkManager");
476         }
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),
482                                              idle, NULL);
483         } else {
484                 DEBUG ("Failed to get nm proxy");
485         }
486
487         priv->nm_connected = TRUE;
488 }
489
490 EmpathyIdle *
491 empathy_idle_dup_singleton (void)
492 {
493         return g_object_new (EMPATHY_TYPE_IDLE, NULL);
494 }
495
496 McPresence
497 empathy_idle_get_state (EmpathyIdle *idle)
498 {
499         EmpathyIdlePriv *priv;
500
501         priv = GET_PRIV (idle);
502
503         return priv->state;
504 }
505
506 void
507 empathy_idle_set_state (EmpathyIdle *idle,
508                         McPresence   state)
509 {
510         EmpathyIdlePriv *priv;
511
512         priv = GET_PRIV (idle);
513
514         empathy_idle_set_presence (idle, state, priv->status);
515 }
516
517 const gchar *
518 empathy_idle_get_status (EmpathyIdle *idle)
519 {
520         EmpathyIdlePriv *priv;
521
522         priv = GET_PRIV (idle);
523
524         if (!priv->status) {
525                 return empathy_presence_get_default_message (priv->state);
526         }
527
528         return priv->status;
529 }
530
531 void
532 empathy_idle_set_status (EmpathyIdle *idle,
533                          const gchar *status)
534 {
535         EmpathyIdlePriv *priv;
536
537         priv = GET_PRIV (idle);
538
539         empathy_idle_set_presence (idle, priv->state, status);
540 }
541
542 McPresence
543 empathy_idle_get_flash_state (EmpathyIdle *idle)
544 {
545         EmpathyIdlePriv *priv;
546
547         priv = GET_PRIV (idle);
548
549         return priv->flash_state;
550 }
551
552 void
553 empathy_idle_set_flash_state (EmpathyIdle *idle,
554                               McPresence   state)
555 {
556         EmpathyIdlePriv *priv;
557
558         priv = GET_PRIV (idle);
559
560         priv->flash_state = state;
561
562         if (state == MC_PRESENCE_UNSET) {
563         }
564
565         g_object_notify (G_OBJECT (idle), "flash-state");
566 }
567
568 void
569 empathy_idle_set_presence (EmpathyIdle *idle,
570                            McPresence   state,
571                            const gchar *status)
572 {
573         EmpathyIdlePriv *priv;
574         const gchar     *default_status;
575
576         priv = GET_PRIV (idle);
577
578         DEBUG ("Changing presence to %s (%d)", status, state);
579
580         /* Do not set translated default messages */
581         default_status = empathy_presence_get_default_message (state);
582         if (!tp_strdiff (status, default_status)) {
583                 status = NULL;
584         }
585
586         if (!priv->nm_connected) {
587                 DEBUG ("NM not connected");
588
589                 priv->nm_saved_state = state;
590                 if (tp_strdiff (priv->status, status)) {
591                         g_free (priv->status);
592                         priv->status = NULL;
593                         if (!EMP_STR_EMPTY (status)) {
594                                 priv->status = g_strdup (status);
595                         }
596                         g_object_notify (G_OBJECT (idle), "status");
597                 }
598
599                 return;
600         }
601
602         mission_control_set_presence (priv->mc, state, status, NULL, NULL);
603 }
604
605 gboolean
606 empathy_idle_get_auto_away (EmpathyIdle *idle)
607 {
608         EmpathyIdlePriv *priv = GET_PRIV (idle);
609
610         return priv->auto_away;
611 }
612
613 void
614 empathy_idle_set_auto_away (EmpathyIdle *idle,
615                             gboolean     auto_away)
616 {
617         EmpathyIdlePriv *priv = GET_PRIV (idle);
618
619         priv->auto_away = auto_away;
620
621         g_object_notify (G_OBJECT (idle), "auto-away");
622 }
623
624 gboolean
625 empathy_idle_get_use_nm (EmpathyIdle *idle)
626 {
627         EmpathyIdlePriv *priv = GET_PRIV (idle);
628
629         return priv->use_nm;
630 }
631
632 void
633 empathy_idle_set_use_nm (EmpathyIdle *idle,
634                          gboolean     use_nm)
635 {
636         EmpathyIdlePriv *priv = GET_PRIV (idle);
637
638         if (!priv->nm_proxy || use_nm == priv->use_nm) {
639                 return;
640         }
641
642         priv->use_nm = use_nm;
643
644         if (use_nm) {
645                 guint   nm_status;
646                 GError *error = NULL;
647
648                 dbus_g_proxy_call (priv->nm_proxy, "state",
649                                    &error,
650                                    G_TYPE_INVALID,
651                                    G_TYPE_UINT, &nm_status,
652                                    G_TYPE_INVALID);
653
654                 if (error) {
655                         DEBUG ("Couldn't get NM state: %s", error->message);
656                         g_clear_error (&error);
657                         nm_status = NM_STATE_ASLEEP;
658                 }
659                 
660                 idle_nm_state_change_cb (priv->nm_proxy, nm_status, idle);
661         } else {
662                 priv->nm_connected = TRUE;
663                 if (priv->nm_saved_state != MC_PRESENCE_UNSET) {
664                         empathy_idle_set_state (idle, priv->nm_saved_state);
665                 }
666                 priv->nm_saved_state = MC_PRESENCE_UNSET;
667         }
668
669         g_object_notify (G_OBJECT (idle), "use-nm");
670 }
671