]> git.0d.be Git - empathy.git/blob - libempathy/empathy-idle.c
5f38e941bd196bfd2c095cb7d72ec0e7895be9f5
[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 old_online,
230                       gboolean new_online,
231                       EmpathyIdle *idle)
232 {
233         EmpathyIdlePriv *priv;
234
235         priv = GET_PRIV (idle);
236
237         if (old_online && !new_online) {
238                 /* We are no longer connected */
239                 DEBUG ("Disconnected: Save state %d (%s)",
240                                 priv->state, priv->status);
241                 priv->saved_state = priv->state;
242                 g_free (priv->saved_status);
243                 priv->saved_status = g_strdup (priv->status);
244                 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
245         }
246         else if (!old_online && new_online
247                         && priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
248                 /* We are now connected */
249                 DEBUG ("Reconnected: Restore state %d (%s)",
250                                 priv->saved_state, priv->saved_status);
251                 empathy_idle_set_presence (idle,
252                                 priv->saved_state,
253                                 priv->saved_status);
254                 priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
255                 g_free (priv->saved_status);
256                 priv->saved_status = NULL;
257         }
258 }
259
260 static void
261 idle_use_conn_cb (GObject *object,
262                   GParamSpec *pspec,
263                   EmpathyIdle *idle)
264 {
265         EmpathyIdlePriv *priv = GET_PRIV (idle);
266
267         if (!empathy_connectivity_get_use_conn (priv->connectivity)) {
268                 if (priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
269                         empathy_idle_set_state (idle, priv->saved_state);
270                 }
271
272                 priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
273         }
274 }
275
276 static void
277 idle_finalize (GObject *object)
278 {
279         EmpathyIdlePriv *priv;
280
281         priv = GET_PRIV (object);
282
283         g_free (priv->status);
284         g_object_unref (priv->mc);
285
286         if (priv->gs_proxy) {
287                 g_object_unref (priv->gs_proxy);
288         }
289
290         g_signal_handlers_disconnect_by_func (priv->connectivity,
291                                               idle_state_change_cb, object);
292         g_signal_handlers_disconnect_by_func (priv->connectivity,
293                                               idle_use_conn_cb, object);
294
295         g_object_unref (priv->connectivity);
296
297         idle_ext_away_stop (EMPATHY_IDLE (object));
298 }
299
300 static GObject *
301 idle_constructor (GType type,
302                   guint n_props,
303                   GObjectConstructParam *props)
304 {
305         GObject *retval;
306
307         if (idle_singleton) {
308                 retval = g_object_ref (idle_singleton);
309         } else {
310                 retval = G_OBJECT_CLASS (empathy_idle_parent_class)->constructor
311                         (type, n_props, props);
312
313                 idle_singleton = EMPATHY_IDLE (retval);
314                 g_object_add_weak_pointer (retval, (gpointer) &idle_singleton);
315         }
316
317         return retval;
318 }
319
320 static void
321 idle_get_property (GObject    *object,
322                    guint       param_id,
323                    GValue     *value,
324                    GParamSpec *pspec)
325 {
326         EmpathyIdlePriv *priv;
327         EmpathyIdle     *idle;
328
329         priv = GET_PRIV (object);
330         idle = EMPATHY_IDLE (object);
331
332         switch (param_id) {
333         case PROP_STATE:
334                 g_value_set_enum (value, empathy_idle_get_state (idle));
335                 break;
336         case PROP_STATUS:
337                 g_value_set_string (value, empathy_idle_get_status (idle));
338                 break;
339         case PROP_FLASH_STATE:
340                 g_value_set_enum (value, empathy_idle_get_flash_state (idle));
341                 break;
342         case PROP_AUTO_AWAY:
343                 g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
344                 break;
345         default:
346                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
347                 break;
348         };
349 }
350
351 static void
352 idle_set_property (GObject      *object,
353                    guint         param_id,
354                    const GValue *value,
355                    GParamSpec   *pspec)
356 {
357         EmpathyIdlePriv *priv;
358         EmpathyIdle     *idle;
359
360         priv = GET_PRIV (object);
361         idle = EMPATHY_IDLE (object);
362
363         switch (param_id) {
364         case PROP_STATE:
365                 empathy_idle_set_state (idle, g_value_get_enum (value));
366                 break;
367         case PROP_STATUS:
368                 empathy_idle_set_status (idle, g_value_get_string (value));
369                 break;
370         case PROP_FLASH_STATE:
371                 empathy_idle_set_flash_state (idle, g_value_get_enum (value));
372                 break;
373         case PROP_AUTO_AWAY:
374                 empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
375                 break;
376         default:
377                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
378                 break;
379         };
380 }
381
382 static void
383 empathy_idle_class_init (EmpathyIdleClass *klass)
384 {
385         GObjectClass *object_class = G_OBJECT_CLASS (klass);
386
387         object_class->finalize = idle_finalize;
388         object_class->constructor = idle_constructor;
389         object_class->get_property = idle_get_property;
390         object_class->set_property = idle_set_property;
391
392         g_object_class_install_property (object_class,
393                                          PROP_STATE,
394                                          g_param_spec_uint ("state",
395                                                             "state",
396                                                             "state",
397                                                             0, NUM_TP_CONNECTION_PRESENCE_TYPES,
398                                                             TP_CONNECTION_PRESENCE_TYPE_UNSET,
399                                                             G_PARAM_READWRITE));
400         g_object_class_install_property (object_class,
401                                          PROP_STATUS,
402                                          g_param_spec_string ("status",
403                                                               "status",
404                                                               "status",
405                                                               NULL,
406                                                               G_PARAM_READWRITE));
407         g_object_class_install_property (object_class,
408                                          PROP_FLASH_STATE,
409                                          g_param_spec_uint ("flash-state",
410                                                             "flash-state",
411                                                             "flash-state",
412                                                             0, NUM_TP_CONNECTION_PRESENCE_TYPES,
413                                                             TP_CONNECTION_PRESENCE_TYPE_UNSET,
414                                                             G_PARAM_READWRITE));
415
416          g_object_class_install_property (object_class,
417                                           PROP_AUTO_AWAY,
418                                           g_param_spec_boolean ("auto-away",
419                                                                 "Automatic set presence to away",
420                                                                 "Should it set presence to away if inactive",
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_OFFLINE;
450         }
451 }
452
453 static void
454 empathy_idle_init (EmpathyIdle *idle)
455 {
456         GError          *error = NULL;
457         EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
458                 EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
459
460         idle->priv = priv;
461         priv->is_idle = FALSE;
462         priv->mc = empathy_mission_control_dup_singleton ();
463         priv->state = empathy_idle_get_actual_presence (idle, &error);
464         if (error) {
465                 DEBUG ("Error getting actual presence: %s", error->message);
466
467                 /* Fallback to OFFLINE as that's what mission_control_get_presence_actual
468                 does. This also ensure to always display the status icon (there is no
469                 unset presence icon). */
470                 priv->state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
471                 g_clear_error (&error);
472         }
473         priv->status = mission_control_get_presence_message_actual (priv->mc, &error);
474         if (error || EMP_STR_EMPTY (priv->status)) {
475                 g_free (priv->status);
476                 priv->status = NULL;
477
478                 if (error) {
479                         DEBUG ("Error getting actual presence message: %s", error->message);
480                         g_clear_error (&error);
481                 }
482         }
483
484         dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
485                                      "PresenceChanged",
486                                      G_CALLBACK (idle_presence_changed_cb),
487                                      idle, NULL);
488
489         priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
490                                                     "org.gnome.SessionManager",
491                                                     "/org/gnome/SessionManager/Presence",
492                                                     "org.gnome.SessionManager.Presence");
493         if (priv->gs_proxy) {
494                 dbus_g_proxy_add_signal (priv->gs_proxy, "StatusChanged",
495                                          G_TYPE_UINT, G_TYPE_INVALID);
496                 dbus_g_proxy_connect_signal (priv->gs_proxy, "StatusChanged",
497                                              G_CALLBACK (idle_session_status_changed_cb),
498                                              idle, NULL);
499         } else {
500                 DEBUG ("Failed to get gs proxy");
501         }
502
503         priv->connectivity = empathy_connectivity_dup_singleton ();
504         g_signal_connect (priv->connectivity, "state-change",
505             G_CALLBACK (idle_state_change_cb), idle);
506         g_signal_connect (priv->connectivity, "notify::use-conn",
507             G_CALLBACK (idle_use_conn_cb), idle);
508 }
509
510 EmpathyIdle *
511 empathy_idle_dup_singleton (void)
512 {
513         return g_object_new (EMPATHY_TYPE_IDLE, NULL);
514 }
515
516 TpConnectionPresenceType
517 empathy_idle_get_state (EmpathyIdle *idle)
518 {
519         EmpathyIdlePriv *priv;
520
521         priv = GET_PRIV (idle);
522
523         return priv->state;
524 }
525
526 void
527 empathy_idle_set_state (EmpathyIdle *idle,
528                         TpConnectionPresenceType   state)
529 {
530         EmpathyIdlePriv *priv;
531
532         priv = GET_PRIV (idle);
533
534         empathy_idle_set_presence (idle, state, priv->status);
535 }
536
537 const gchar *
538 empathy_idle_get_status (EmpathyIdle *idle)
539 {
540         EmpathyIdlePriv *priv;
541
542         priv = GET_PRIV (idle);
543
544         if (!priv->status) {
545                 return empathy_presence_get_default_message (priv->state);
546         }
547
548         return priv->status;
549 }
550
551 void
552 empathy_idle_set_status (EmpathyIdle *idle,
553                          const gchar *status)
554 {
555         EmpathyIdlePriv *priv;
556
557         priv = GET_PRIV (idle);
558
559         empathy_idle_set_presence (idle, priv->state, status);
560 }
561
562 TpConnectionPresenceType
563 empathy_idle_get_flash_state (EmpathyIdle *idle)
564 {
565         EmpathyIdlePriv *priv;
566
567         priv = GET_PRIV (idle);
568
569         return priv->flash_state;
570 }
571
572 void
573 empathy_idle_set_flash_state (EmpathyIdle *idle,
574                               TpConnectionPresenceType   state)
575 {
576         EmpathyIdlePriv *priv;
577
578         priv = GET_PRIV (idle);
579
580         priv->flash_state = state;
581
582         if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
583         }
584
585         g_object_notify (G_OBJECT (idle), "flash-state");
586 }
587
588 static void
589 empathy_idle_do_set_presence (EmpathyIdle *idle,
590                            TpConnectionPresenceType   state,
591                            const gchar *status)
592 {
593         McPresence mc_state = MC_PRESENCE_UNSET;
594         EmpathyIdlePriv *priv = GET_PRIV (idle);
595
596         switch (state) {
597                 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
598                         mc_state = MC_PRESENCE_OFFLINE;
599                         break;
600                 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
601                         mc_state = MC_PRESENCE_AVAILABLE;
602                         break;
603                 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
604                         mc_state = MC_PRESENCE_AWAY;
605                         break;
606                 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
607                         mc_state = MC_PRESENCE_EXTENDED_AWAY;
608                         break;
609                 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
610                         mc_state = MC_PRESENCE_HIDDEN;
611                         break;
612                 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
613                         mc_state = MC_PRESENCE_DO_NOT_DISTURB;
614                         break;
615                 default:
616                         g_assert_not_reached ();
617         }
618
619         mission_control_set_presence (priv->mc, mc_state, status, NULL, NULL);
620 }
621
622 void
623 empathy_idle_set_presence (EmpathyIdle *idle,
624                            TpConnectionPresenceType   state,
625                            const gchar *status)
626 {
627         EmpathyIdlePriv *priv;
628         const gchar     *default_status;
629
630         priv = GET_PRIV (idle);
631
632         DEBUG ("Changing presence to %s (%d)", status, state);
633
634         /* Do not set translated default messages */
635         default_status = empathy_presence_get_default_message (state);
636         if (!tp_strdiff (status, default_status)) {
637                 status = NULL;
638         }
639
640         if (!empathy_connectivity_is_online (priv->connectivity)) {
641                 DEBUG ("Empathy is not online");
642
643                 if (tp_strdiff (priv->status, status)) {
644                         g_free (priv->status);
645                         priv->status = NULL;
646                         if (!EMP_STR_EMPTY (status)) {
647                                 priv->status = g_strdup (status);
648                         }
649                         g_object_notify (G_OBJECT (idle), "status");
650                 }
651         }
652
653         empathy_idle_do_set_presence (idle, state, status);
654 }
655
656 gboolean
657 empathy_idle_get_auto_away (EmpathyIdle *idle)
658 {
659         EmpathyIdlePriv *priv = GET_PRIV (idle);
660
661         return priv->auto_away;
662 }
663
664 void
665 empathy_idle_set_auto_away (EmpathyIdle *idle,
666                             gboolean     auto_away)
667 {
668         EmpathyIdlePriv *priv = GET_PRIV (idle);
669
670         priv->auto_away = auto_away;
671
672         g_object_notify (G_OBJECT (idle), "auto-away");
673 }
674