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