]> git.0d.be Git - empathy.git/blob - libempathy/empathy-idle.c
empathy-connectivity: only send the new state in the state-change signal
[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 #include "empathy-connectivity.h"
36
37 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
38 #include "empathy-debug.h"
39
40 /* Number of seconds before entering extended autoaway. */
41 #define EXT_AWAY_TIME (30*60)
42
43 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIdle)
44 typedef struct {
45         MissionControl *mc;
46         DBusGProxy     *gs_proxy;
47         EmpathyConnectivity *connectivity;
48
49         TpConnectionPresenceType      state;
50         gchar          *status;
51         TpConnectionPresenceType      flash_state;
52         gboolean        auto_away;
53
54         TpConnectionPresenceType      away_saved_state;
55         TpConnectionPresenceType      saved_state;
56         gchar          *saved_status;
57
58         gboolean        is_idle;
59         guint           ext_away_timeout;
60 } EmpathyIdlePriv;
61
62 typedef enum {
63         SESSION_STATUS_AVAILABLE,
64         SESSION_STATUS_INVISIBLE,
65         SESSION_STATUS_BUSY,
66         SESSION_STATUS_IDLE,
67         SESSION_STATUS_UNKNOWN
68 } SessionStatus;
69
70 enum {
71         PROP_0,
72         PROP_STATE,
73         PROP_STATUS,
74         PROP_FLASH_STATE,
75         PROP_AUTO_AWAY
76 };
77
78 G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT);
79
80 static EmpathyIdle * idle_singleton = NULL;
81
82 static void
83 idle_presence_changed_cb (MissionControl *mc,
84                           TpConnectionPresenceType state,
85                           gchar          *status,
86                           EmpathyIdle    *idle)
87 {
88         EmpathyIdlePriv *priv;
89
90         priv = GET_PRIV (idle);
91
92         if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET)
93                 /* Assume our presence is offline if MC reports UNSET */
94                 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
95
96         DEBUG ("Presence changed to '%s' (%d)", status, state);
97
98         g_free (priv->status);
99         priv->state = state;
100         priv->status = NULL;
101         if (!EMP_STR_EMPTY (status)) {
102                 priv->status = g_strdup (status);
103         }
104
105         g_object_notify (G_OBJECT (idle), "state");
106         g_object_notify (G_OBJECT (idle), "status");
107 }
108
109 static gboolean
110 idle_ext_away_cb (EmpathyIdle *idle)
111 {
112         EmpathyIdlePriv *priv;
113
114         priv = GET_PRIV (idle);
115
116         DEBUG ("Going to extended autoaway");
117         empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY);
118         priv->ext_away_timeout = 0;
119
120         return FALSE;
121 }
122
123 static void
124 idle_ext_away_stop (EmpathyIdle *idle)
125 {
126         EmpathyIdlePriv *priv;
127
128         priv = GET_PRIV (idle);
129
130         if (priv->ext_away_timeout) {
131                 g_source_remove (priv->ext_away_timeout);
132                 priv->ext_away_timeout = 0;
133         }
134 }
135
136 static void
137 idle_ext_away_start (EmpathyIdle *idle)
138 {
139         EmpathyIdlePriv *priv;
140
141         priv = GET_PRIV (idle);
142
143         if (priv->ext_away_timeout != 0) {
144                 return;
145         }
146         priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
147                                                         (GSourceFunc) idle_ext_away_cb,
148                                                         idle);
149 }
150
151 static void
152 idle_session_status_changed_cb (DBusGProxy    *gs_proxy,
153                                 SessionStatus  status,
154                                 EmpathyIdle   *idle)
155 {
156         EmpathyIdlePriv *priv;
157         gboolean is_idle;
158
159         priv = GET_PRIV (idle);
160
161         is_idle = (status == SESSION_STATUS_IDLE);
162
163         DEBUG ("Session idle state changed, %s -> %s",
164                 priv->is_idle ? "yes" : "no",
165                 is_idle ? "yes" : "no");
166
167         if (!priv->auto_away ||
168             (priv->saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET &&
169              (priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
170               priv->state == TP_CONNECTION_PRESENCE_TYPE_HIDDEN))) {
171                 /* We don't want to go auto away OR we explicitely asked to be
172                  * offline, nothing to do here */
173                 priv->is_idle = is_idle;
174                 return;
175         }
176
177         if (is_idle && !priv->is_idle) {
178                 TpConnectionPresenceType new_state;
179                 /* We are now idle */
180
181                 idle_ext_away_start (idle);
182
183                 if (priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
184                         /* We are disconnected, when coming back from away
185                          * we want to restore the presence before the
186                          * disconnection. */
187                         priv->away_saved_state = priv->saved_state;
188                 } else {
189                         priv->away_saved_state = priv->state;
190                 }
191
192                 new_state = TP_CONNECTION_PRESENCE_TYPE_AWAY;
193                 if (priv->state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
194                         new_state = TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY;
195                 }
196
197                 DEBUG ("Going to autoaway. Saved state=%d, new state=%d",
198                         priv->away_saved_state, new_state);
199                 empathy_idle_set_state (idle, new_state);
200         } else if (!is_idle && priv->is_idle) {
201                 const gchar *new_status;
202                 /* We are no more idle, restore state */
203
204                 idle_ext_away_stop (idle);
205
206                 if (priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_AWAY ||
207                     priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
208                         priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
209                         new_status = NULL;
210                 } else {
211                         new_status = priv->status;
212                 }
213
214                 DEBUG ("Restoring state to %d, reset status to %s",
215                         priv->away_saved_state, new_status);
216
217                 empathy_idle_set_presence (idle,
218                                            priv->away_saved_state,
219                                            new_status);
220
221                 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
222         }
223
224         priv->is_idle = is_idle;
225 }
226
227 static void
228 idle_state_change_cb (EmpathyConnectivity *connectivity,
229                       gboolean new_online,
230                       EmpathyIdle *idle)
231 {
232         EmpathyIdlePriv *priv;
233
234         priv = GET_PRIV (idle);
235
236         if (!new_online) {
237                 /* We are no longer connected */
238                 DEBUG ("Disconnected: Save state %d (%s)",
239                                 priv->state, priv->status);
240                 priv->saved_state = priv->state;
241                 g_free (priv->saved_status);
242                 priv->saved_status = g_strdup (priv->status);
243                 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
244         }
245         else if (new_online
246                         && priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
247                 /* We are now connected */
248                 DEBUG ("Reconnected: Restore state %d (%s)",
249                                 priv->saved_state, priv->saved_status);
250                 empathy_idle_set_presence (idle,
251                                 priv->saved_state,
252                                 priv->saved_status);
253                 priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
254                 g_free (priv->saved_status);
255                 priv->saved_status = NULL;
256         }
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         g_signal_handlers_disconnect_by_func (priv->connectivity,
274                                               idle_state_change_cb, object);
275
276         g_object_unref (priv->connectivity);
277
278         idle_ext_away_stop (EMPATHY_IDLE (object));
279 }
280
281 static GObject *
282 idle_constructor (GType type,
283                   guint n_props,
284                   GObjectConstructParam *props)
285 {
286         GObject *retval;
287
288         if (idle_singleton) {
289                 retval = g_object_ref (idle_singleton);
290         } else {
291                 retval = G_OBJECT_CLASS (empathy_idle_parent_class)->constructor
292                         (type, n_props, props);
293
294                 idle_singleton = EMPATHY_IDLE (retval);
295                 g_object_add_weak_pointer (retval, (gpointer) &idle_singleton);
296         }
297
298         return retval;
299 }
300
301 static void
302 idle_get_property (GObject    *object,
303                    guint       param_id,
304                    GValue     *value,
305                    GParamSpec *pspec)
306 {
307         EmpathyIdlePriv *priv;
308         EmpathyIdle     *idle;
309
310         priv = GET_PRIV (object);
311         idle = EMPATHY_IDLE (object);
312
313         switch (param_id) {
314         case PROP_STATE:
315                 g_value_set_enum (value, empathy_idle_get_state (idle));
316                 break;
317         case PROP_STATUS:
318                 g_value_set_string (value, empathy_idle_get_status (idle));
319                 break;
320         case PROP_FLASH_STATE:
321                 g_value_set_enum (value, empathy_idle_get_flash_state (idle));
322                 break;
323         case PROP_AUTO_AWAY:
324                 g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
325                 break;
326         default:
327                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
328                 break;
329         };
330 }
331
332 static void
333 idle_set_property (GObject      *object,
334                    guint         param_id,
335                    const GValue *value,
336                    GParamSpec   *pspec)
337 {
338         EmpathyIdlePriv *priv;
339         EmpathyIdle     *idle;
340
341         priv = GET_PRIV (object);
342         idle = EMPATHY_IDLE (object);
343
344         switch (param_id) {
345         case PROP_STATE:
346                 empathy_idle_set_state (idle, g_value_get_enum (value));
347                 break;
348         case PROP_STATUS:
349                 empathy_idle_set_status (idle, g_value_get_string (value));
350                 break;
351         case PROP_FLASH_STATE:
352                 empathy_idle_set_flash_state (idle, g_value_get_enum (value));
353                 break;
354         case PROP_AUTO_AWAY:
355                 empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
356                 break;
357         default:
358                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
359                 break;
360         };
361 }
362
363 static void
364 empathy_idle_class_init (EmpathyIdleClass *klass)
365 {
366         GObjectClass *object_class = G_OBJECT_CLASS (klass);
367
368         object_class->finalize = idle_finalize;
369         object_class->constructor = idle_constructor;
370         object_class->get_property = idle_get_property;
371         object_class->set_property = idle_set_property;
372
373         g_object_class_install_property (object_class,
374                                          PROP_STATE,
375                                          g_param_spec_uint ("state",
376                                                             "state",
377                                                             "state",
378                                                             0, NUM_TP_CONNECTION_PRESENCE_TYPES,
379                                                             TP_CONNECTION_PRESENCE_TYPE_UNSET,
380                                                             G_PARAM_READWRITE));
381         g_object_class_install_property (object_class,
382                                          PROP_STATUS,
383                                          g_param_spec_string ("status",
384                                                               "status",
385                                                               "status",
386                                                               NULL,
387                                                               G_PARAM_READWRITE));
388         g_object_class_install_property (object_class,
389                                          PROP_FLASH_STATE,
390                                          g_param_spec_uint ("flash-state",
391                                                             "flash-state",
392                                                             "flash-state",
393                                                             0, NUM_TP_CONNECTION_PRESENCE_TYPES,
394                                                             TP_CONNECTION_PRESENCE_TYPE_UNSET,
395                                                             G_PARAM_READWRITE));
396
397          g_object_class_install_property (object_class,
398                                           PROP_AUTO_AWAY,
399                                           g_param_spec_boolean ("auto-away",
400                                                                 "Automatic set presence to away",
401                                                                 "Should it set presence to away if inactive",
402                                                                 FALSE,
403                                                                 G_PARAM_READWRITE));
404
405         g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
406 }
407
408 static TpConnectionPresenceType
409 empathy_idle_get_actual_presence (EmpathyIdle *idle, GError **error)
410 {
411         McPresence presence;
412         EmpathyIdlePriv *priv = GET_PRIV (idle);
413
414         presence = mission_control_get_presence_actual (priv->mc, error);
415
416         switch (presence) {
417         case MC_PRESENCE_OFFLINE:
418                 return TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
419         case MC_PRESENCE_AVAILABLE:
420                 return TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
421         case MC_PRESENCE_AWAY:
422                 return TP_CONNECTION_PRESENCE_TYPE_AWAY;
423         case MC_PRESENCE_EXTENDED_AWAY:
424                 return TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY;
425         case MC_PRESENCE_HIDDEN:
426                 return TP_CONNECTION_PRESENCE_TYPE_HIDDEN;
427         case MC_PRESENCE_DO_NOT_DISTURB:
428                 return TP_CONNECTION_PRESENCE_TYPE_BUSY;
429         default:
430                 return TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
431         }
432 }
433
434 static void
435 empathy_idle_init (EmpathyIdle *idle)
436 {
437         GError          *error = NULL;
438         EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
439                 EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
440
441         idle->priv = priv;
442         priv->is_idle = FALSE;
443         priv->mc = empathy_mission_control_dup_singleton ();
444         priv->state = empathy_idle_get_actual_presence (idle, &error);
445         if (error) {
446                 DEBUG ("Error getting actual presence: %s", error->message);
447
448                 /* Fallback to OFFLINE as that's what mission_control_get_presence_actual
449                 does. This also ensure to always display the status icon (there is no
450                 unset presence icon). */
451                 priv->state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
452                 g_clear_error (&error);
453         }
454         priv->status = mission_control_get_presence_message_actual (priv->mc, &error);
455         if (error || EMP_STR_EMPTY (priv->status)) {
456                 g_free (priv->status);
457                 priv->status = NULL;
458
459                 if (error) {
460                         DEBUG ("Error getting actual presence message: %s", error->message);
461                         g_clear_error (&error);
462                 }
463         }
464
465         dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
466                                      "PresenceChanged",
467                                      G_CALLBACK (idle_presence_changed_cb),
468                                      idle, NULL);
469
470         priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
471                                                     "org.gnome.SessionManager",
472                                                     "/org/gnome/SessionManager/Presence",
473                                                     "org.gnome.SessionManager.Presence");
474         if (priv->gs_proxy) {
475                 dbus_g_proxy_add_signal (priv->gs_proxy, "StatusChanged",
476                                          G_TYPE_UINT, G_TYPE_INVALID);
477                 dbus_g_proxy_connect_signal (priv->gs_proxy, "StatusChanged",
478                                              G_CALLBACK (idle_session_status_changed_cb),
479                                              idle, NULL);
480         } else {
481                 DEBUG ("Failed to get gs proxy");
482         }
483
484         priv->connectivity = empathy_connectivity_dup_singleton ();
485         g_signal_connect (priv->connectivity, "state-change",
486             G_CALLBACK (idle_state_change_cb), idle);
487 }
488
489 EmpathyIdle *
490 empathy_idle_dup_singleton (void)
491 {
492         return g_object_new (EMPATHY_TYPE_IDLE, NULL);
493 }
494
495 TpConnectionPresenceType
496 empathy_idle_get_state (EmpathyIdle *idle)
497 {
498         EmpathyIdlePriv *priv;
499
500         priv = GET_PRIV (idle);
501
502         return priv->state;
503 }
504
505 void
506 empathy_idle_set_state (EmpathyIdle *idle,
507                         TpConnectionPresenceType   state)
508 {
509         EmpathyIdlePriv *priv;
510
511         priv = GET_PRIV (idle);
512
513         empathy_idle_set_presence (idle, state, priv->status);
514 }
515
516 const gchar *
517 empathy_idle_get_status (EmpathyIdle *idle)
518 {
519         EmpathyIdlePriv *priv;
520
521         priv = GET_PRIV (idle);
522
523         if (!priv->status) {
524                 return empathy_presence_get_default_message (priv->state);
525         }
526
527         return priv->status;
528 }
529
530 void
531 empathy_idle_set_status (EmpathyIdle *idle,
532                          const gchar *status)
533 {
534         EmpathyIdlePriv *priv;
535
536         priv = GET_PRIV (idle);
537
538         empathy_idle_set_presence (idle, priv->state, status);
539 }
540
541 TpConnectionPresenceType
542 empathy_idle_get_flash_state (EmpathyIdle *idle)
543 {
544         EmpathyIdlePriv *priv;
545
546         priv = GET_PRIV (idle);
547
548         return priv->flash_state;
549 }
550
551 void
552 empathy_idle_set_flash_state (EmpathyIdle *idle,
553                               TpConnectionPresenceType   state)
554 {
555         EmpathyIdlePriv *priv;
556
557         priv = GET_PRIV (idle);
558
559         priv->flash_state = state;
560
561         if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
562         }
563
564         g_object_notify (G_OBJECT (idle), "flash-state");
565 }
566
567 static void
568 empathy_idle_do_set_presence (EmpathyIdle *idle,
569                            TpConnectionPresenceType   state,
570                            const gchar *status)
571 {
572         McPresence mc_state = MC_PRESENCE_UNSET;
573         EmpathyIdlePriv *priv = GET_PRIV (idle);
574
575         switch (state) {
576                 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
577                         mc_state = MC_PRESENCE_OFFLINE;
578                         break;
579                 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
580                         mc_state = MC_PRESENCE_AVAILABLE;
581                         break;
582                 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
583                         mc_state = MC_PRESENCE_AWAY;
584                         break;
585                 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
586                         mc_state = MC_PRESENCE_EXTENDED_AWAY;
587                         break;
588                 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
589                         mc_state = MC_PRESENCE_HIDDEN;
590                         break;
591                 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
592                         mc_state = MC_PRESENCE_DO_NOT_DISTURB;
593                         break;
594                 default:
595                         g_assert_not_reached ();
596         }
597
598         mission_control_set_presence (priv->mc, mc_state, status, NULL, NULL);
599 }
600
601 void
602 empathy_idle_set_presence (EmpathyIdle *idle,
603                            TpConnectionPresenceType   state,
604                            const gchar *status)
605 {
606         EmpathyIdlePriv *priv;
607         const gchar     *default_status;
608
609         priv = GET_PRIV (idle);
610
611         DEBUG ("Changing presence to %s (%d)", status, state);
612
613         /* Do not set translated default messages */
614         default_status = empathy_presence_get_default_message (state);
615         if (!tp_strdiff (status, default_status)) {
616                 status = NULL;
617         }
618
619         if (!empathy_connectivity_is_online (priv->connectivity)) {
620                 DEBUG ("Empathy is not online");
621
622                 if (tp_strdiff (priv->status, status)) {
623                         g_free (priv->status);
624                         priv->status = NULL;
625                         if (!EMP_STR_EMPTY (status)) {
626                                 priv->status = g_strdup (status);
627                         }
628                         g_object_notify (G_OBJECT (idle), "status");
629                 }
630         }
631
632         empathy_idle_do_set_presence (idle, state, status);
633 }
634
635 gboolean
636 empathy_idle_get_auto_away (EmpathyIdle *idle)
637 {
638         EmpathyIdlePriv *priv = GET_PRIV (idle);
639
640         return priv->auto_away;
641 }
642
643 void
644 empathy_idle_set_auto_away (EmpathyIdle *idle,
645                             gboolean     auto_away)
646 {
647         EmpathyIdlePriv *priv = GET_PRIV (idle);
648
649         priv->auto_away = auto_away;
650
651         g_object_notify (G_OBJECT (idle), "auto-away");
652 }
653