]> git.0d.be Git - empathy.git/blob - libempathy/empathy-idle.c
f60fc0f85bfafb0effe99c7871085ee2f166ff9c
[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 #ifdef HAVE_NM
29 #include <nm-client.h>
30 #endif
31
32 #include <telepathy-glib/dbus.h>
33 #include <telepathy-glib/util.h>
34
35 #include "empathy-account-manager.h"
36 #include "empathy-idle.h"
37 #include "empathy-utils.h"
38
39 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
40 #include "empathy-debug.h"
41
42 /* Number of seconds before entering extended autoaway. */
43 #define EXT_AWAY_TIME (30*60)
44
45 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIdle)
46 typedef struct {
47         DBusGProxy     *gs_proxy;
48 #ifdef HAVE_NM
49         NMClient       *nm_client;
50 #endif
51
52         TpConnectionPresenceType      state;
53         gchar          *status;
54         TpConnectionPresenceType      flash_state;
55         gboolean        auto_away;
56         gboolean        use_nm;
57
58         TpConnectionPresenceType      away_saved_state;
59         TpConnectionPresenceType      nm_saved_state;
60         gchar          *nm_saved_status;
61
62         gboolean        is_idle;
63         gboolean        nm_connected;
64         guint           ext_away_timeout;
65
66         EmpathyAccountManager *manager;
67 } EmpathyIdlePriv;
68
69 typedef enum {
70         SESSION_STATUS_AVAILABLE,
71         SESSION_STATUS_INVISIBLE,
72         SESSION_STATUS_BUSY,
73         SESSION_STATUS_IDLE,
74         SESSION_STATUS_UNKNOWN
75 } SessionStatus;
76
77 enum {
78         PROP_0,
79         PROP_STATE,
80         PROP_STATUS,
81         PROP_FLASH_STATE,
82         PROP_AUTO_AWAY,
83         PROP_USE_NM
84 };
85
86 G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT);
87
88 static EmpathyIdle * idle_singleton = NULL;
89
90 static void
91 idle_presence_changed_cb (EmpathyAccountManager *manager,
92                           TpConnectionPresenceType state,
93                           gchar          *status,
94                           gchar          *status_message,
95                           EmpathyIdle    *idle)
96 {
97         EmpathyIdlePriv *priv;
98
99         priv = GET_PRIV (idle);
100
101         if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET)
102                 /* Assume our presence is offline if MC reports UNSET */
103                 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
104
105         DEBUG ("Presence changed to '%s' (%d)", status, state);
106
107         g_free (priv->status);
108         priv->state = state;
109         priv->status = NULL;
110         if (!EMP_STR_EMPTY (status)) {
111                 priv->status = g_strdup (status);
112         }
113
114         g_object_notify (G_OBJECT (idle), "state");
115         g_object_notify (G_OBJECT (idle), "status");
116 }
117
118 static gboolean
119 idle_ext_away_cb (EmpathyIdle *idle)
120 {
121         EmpathyIdlePriv *priv;
122
123         priv = GET_PRIV (idle);
124
125         DEBUG ("Going to extended autoaway");
126         empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY);
127         priv->ext_away_timeout = 0;
128
129         return FALSE;
130 }
131
132 static void
133 idle_ext_away_stop (EmpathyIdle *idle)
134 {
135         EmpathyIdlePriv *priv;
136
137         priv = GET_PRIV (idle);
138
139         if (priv->ext_away_timeout) {
140                 g_source_remove (priv->ext_away_timeout);
141                 priv->ext_away_timeout = 0;
142         }
143 }
144
145 static void
146 idle_ext_away_start (EmpathyIdle *idle)
147 {
148         EmpathyIdlePriv *priv;
149
150         priv = GET_PRIV (idle);
151
152         if (priv->ext_away_timeout != 0) {
153                 return;
154         }
155         priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
156                                                         (GSourceFunc) idle_ext_away_cb,
157                                                         idle);
158 }
159
160 static void
161 idle_session_status_changed_cb (DBusGProxy    *gs_proxy,
162                                 SessionStatus  status,
163                                 EmpathyIdle   *idle)
164 {
165         EmpathyIdlePriv *priv;
166         gboolean is_idle;
167
168         priv = GET_PRIV (idle);
169
170         is_idle = (status == SESSION_STATUS_IDLE);
171
172         DEBUG ("Session idle state changed, %s -> %s",
173                 priv->is_idle ? "yes" : "no",
174                 is_idle ? "yes" : "no");
175
176         if (!priv->auto_away ||
177             (priv->nm_saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET &&
178              (priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
179               priv->state == TP_CONNECTION_PRESENCE_TYPE_HIDDEN))) {
180                 /* We don't want to go auto away OR we explicitely asked to be
181                  * offline, nothing to do here */
182                 priv->is_idle = is_idle;
183                 return;
184         }
185
186         if (is_idle && !priv->is_idle) {
187                 TpConnectionPresenceType new_state;
188                 /* We are now idle */
189
190                 idle_ext_away_start (idle);
191
192                 if (priv->nm_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
193                         /* We are disconnected, when coming back from away
194                          * we want to restore the presence before the
195                          * disconnection. */
196                         priv->away_saved_state = priv->nm_saved_state;
197                 } else {
198                         priv->away_saved_state = priv->state;
199                 }
200
201                 new_state = TP_CONNECTION_PRESENCE_TYPE_AWAY;
202                 if (priv->state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
203                         new_state = TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY;
204                 }
205
206                 DEBUG ("Going to autoaway. Saved state=%d, new state=%d",
207                         priv->away_saved_state, new_state);
208                 empathy_idle_set_state (idle, new_state);
209         } else if (!is_idle && priv->is_idle) {
210                 const gchar *new_status;
211                 /* We are no more idle, restore state */
212
213                 idle_ext_away_stop (idle);
214
215                 if (priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_AWAY ||
216                     priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
217                         priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
218                         new_status = NULL;
219                 } else {
220                         new_status = priv->status;
221                 }
222
223                 DEBUG ("Restoring state to %d, reset status to %s",
224                         priv->away_saved_state, new_status);
225
226                 empathy_idle_set_presence (idle,
227                                            priv->away_saved_state,
228                                            new_status);
229
230                 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
231         }
232
233         priv->is_idle = is_idle;
234 }
235
236 #ifdef HAVE_NM
237 static void
238 idle_nm_state_change_cb (NMClient         *client,
239                          const GParamSpec *pspec,
240                          EmpathyIdle      *idle)
241 {
242         EmpathyIdlePriv *priv;
243         gboolean         old_nm_connected;
244         gboolean         new_nm_connected;
245         NMState          state;
246
247         priv = GET_PRIV (idle);
248
249         if (!priv->use_nm) {
250                 return;
251         }
252
253         state = nm_client_get_state (priv->nm_client);
254         old_nm_connected = priv->nm_connected;
255         new_nm_connected = !(state == NM_STATE_CONNECTING ||
256                              state == NM_STATE_DISCONNECTED);
257         priv->nm_connected = TRUE; /* To be sure _set_state will work */
258
259         DEBUG ("New network state %d", state);
260
261         if (old_nm_connected && !new_nm_connected) {
262                 /* We are no more connected */
263                 DEBUG ("Disconnected: Save state %d (%s)",
264                                 priv->state, priv->status);
265                 priv->nm_saved_state = priv->state;
266                 g_free (priv->nm_saved_status);
267                 priv->nm_saved_status = g_strdup (priv->status);
268                 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
269         }
270         else if (!old_nm_connected && new_nm_connected
271                         && priv->nm_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
272                 /* We are now connected */
273                 DEBUG ("Reconnected: Restore state %d (%s)",
274                                 priv->nm_saved_state, priv->nm_saved_status);
275                 empathy_idle_set_presence (idle,
276                                 priv->nm_saved_state,
277                                 priv->nm_saved_status);
278                 priv->nm_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
279                 g_free (priv->nm_saved_status);
280                 priv->nm_saved_status = NULL;
281         }
282
283         priv->nm_connected = new_nm_connected;
284 }
285 #endif
286
287 static void
288 idle_finalize (GObject *object)
289 {
290         EmpathyIdlePriv *priv;
291
292         priv = GET_PRIV (object);
293
294         g_free (priv->status);
295
296         if (priv->gs_proxy) {
297                 g_object_unref (priv->gs_proxy);
298         }
299
300         if (priv->manager != NULL)
301                 g_object_unref (priv->manager);
302
303 #ifdef HAVE_NM
304         if (priv->nm_client) {
305                 g_object_unref (priv->nm_client);
306         }
307 #endif
308
309         idle_ext_away_stop (EMPATHY_IDLE (object));
310 }
311
312 static GObject *
313 idle_constructor (GType type,
314                   guint n_props,
315                   GObjectConstructParam *props)
316 {
317         GObject *retval;
318
319         if (idle_singleton) {
320                 retval = g_object_ref (idle_singleton);
321         } else {
322                 retval = G_OBJECT_CLASS (empathy_idle_parent_class)->constructor
323                         (type, n_props, props);
324
325                 idle_singleton = EMPATHY_IDLE (retval);
326                 g_object_add_weak_pointer (retval, (gpointer) &idle_singleton);
327         }
328
329         return retval;
330 }
331
332 static void
333 idle_get_property (GObject    *object,
334                    guint       param_id,
335                    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                 g_value_set_enum (value, empathy_idle_get_state (idle));
347                 break;
348         case PROP_STATUS:
349                 g_value_set_string (value, empathy_idle_get_status (idle));
350                 break;
351         case PROP_FLASH_STATE:
352                 g_value_set_enum (value, empathy_idle_get_flash_state (idle));
353                 break;
354         case PROP_AUTO_AWAY:
355                 g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
356                 break;
357         case PROP_USE_NM:
358                 g_value_set_boolean (value, empathy_idle_get_use_nm (idle));
359                 break;
360         default:
361                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
362                 break;
363         };
364 }
365
366 static void
367 idle_set_property (GObject      *object,
368                    guint         param_id,
369                    const GValue *value,
370                    GParamSpec   *pspec)
371 {
372         EmpathyIdlePriv *priv;
373         EmpathyIdle     *idle;
374
375         priv = GET_PRIV (object);
376         idle = EMPATHY_IDLE (object);
377
378         switch (param_id) {
379         case PROP_STATE:
380                 empathy_idle_set_state (idle, g_value_get_enum (value));
381                 break;
382         case PROP_STATUS:
383                 empathy_idle_set_status (idle, g_value_get_string (value));
384                 break;
385         case PROP_FLASH_STATE:
386                 empathy_idle_set_flash_state (idle, g_value_get_enum (value));
387                 break;
388         case PROP_AUTO_AWAY:
389                 empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
390                 break;
391         case PROP_USE_NM:
392                 empathy_idle_set_use_nm (idle, g_value_get_boolean (value));
393                 break;
394         default:
395                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
396                 break;
397         };
398 }
399
400 static void
401 empathy_idle_class_init (EmpathyIdleClass *klass)
402 {
403         GObjectClass *object_class = G_OBJECT_CLASS (klass);
404
405         object_class->finalize = idle_finalize;
406         object_class->constructor = idle_constructor;
407         object_class->get_property = idle_get_property;
408         object_class->set_property = idle_set_property;
409
410         g_object_class_install_property (object_class,
411                                          PROP_STATE,
412                                          g_param_spec_uint ("state",
413                                                             "state",
414                                                             "state",
415                                                             0, NUM_TP_CONNECTION_PRESENCE_TYPES,
416                                                             TP_CONNECTION_PRESENCE_TYPE_UNSET,
417                                                             G_PARAM_READWRITE));
418         g_object_class_install_property (object_class,
419                                          PROP_STATUS,
420                                          g_param_spec_string ("status",
421                                                               "status",
422                                                               "status",
423                                                               NULL,
424                                                               G_PARAM_READWRITE));
425         g_object_class_install_property (object_class,
426                                          PROP_FLASH_STATE,
427                                          g_param_spec_uint ("flash-state",
428                                                             "flash-state",
429                                                             "flash-state",
430                                                             0, NUM_TP_CONNECTION_PRESENCE_TYPES,
431                                                             TP_CONNECTION_PRESENCE_TYPE_UNSET,
432                                                             G_PARAM_READWRITE));
433
434          g_object_class_install_property (object_class,
435                                           PROP_AUTO_AWAY,
436                                           g_param_spec_boolean ("auto-away",
437                                                                 "Automatic set presence to away",
438                                                                 "Should it set presence to away if inactive",
439                                                                 FALSE,
440                                                                 G_PARAM_READWRITE));
441
442          g_object_class_install_property (object_class,
443                                           PROP_USE_NM,
444                                           g_param_spec_boolean ("use-nm",
445                                                                 "Use Network Manager",
446                                                                 "Set presence according to Network Manager",
447                                                                 TRUE,
448                                                                 G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
449
450         g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
451 }
452
453 static void
454 empathy_idle_init (EmpathyIdle *idle)
455 {
456         EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
457                 EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
458
459         idle->priv = priv;
460         priv->is_idle = FALSE;
461
462         priv->manager = empathy_account_manager_dup_singleton ();
463         priv->state = empathy_account_manager_get_global_presence (priv->manager,
464                 NULL, &priv->status);
465
466
467         g_signal_connect (priv->manager, "global-presence-changed",
468                 G_CALLBACK (idle_presence_changed_cb), idle);
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 #ifdef HAVE_NM
485         priv->nm_client = nm_client_new ();
486         if (priv->nm_client) {
487                 g_signal_connect (priv->nm_client, "notify::" NM_CLIENT_STATE,
488                                   G_CALLBACK (idle_nm_state_change_cb),
489                                   idle);
490         } else {
491                 DEBUG ("Failed to get nm proxy");
492         }
493 #endif
494 }
495
496 EmpathyIdle *
497 empathy_idle_dup_singleton (void)
498 {
499         return g_object_new (EMPATHY_TYPE_IDLE, NULL);
500 }
501
502 TpConnectionPresenceType
503 empathy_idle_get_state (EmpathyIdle *idle)
504 {
505         EmpathyIdlePriv *priv;
506
507         priv = GET_PRIV (idle);
508
509         return priv->state;
510 }
511
512 void
513 empathy_idle_set_state (EmpathyIdle *idle,
514                         TpConnectionPresenceType   state)
515 {
516         EmpathyIdlePriv *priv;
517
518         priv = GET_PRIV (idle);
519
520         empathy_idle_set_presence (idle, state, priv->status);
521 }
522
523 const gchar *
524 empathy_idle_get_status (EmpathyIdle *idle)
525 {
526         EmpathyIdlePriv *priv;
527
528         priv = GET_PRIV (idle);
529
530         if (!priv->status) {
531                 return empathy_presence_get_default_message (priv->state);
532         }
533
534         return priv->status;
535 }
536
537 void
538 empathy_idle_set_status (EmpathyIdle *idle,
539                          const gchar *status)
540 {
541         EmpathyIdlePriv *priv;
542
543         priv = GET_PRIV (idle);
544
545         empathy_idle_set_presence (idle, priv->state, status);
546 }
547
548 TpConnectionPresenceType
549 empathy_idle_get_flash_state (EmpathyIdle *idle)
550 {
551         EmpathyIdlePriv *priv;
552
553         priv = GET_PRIV (idle);
554
555         return priv->flash_state;
556 }
557
558 void
559 empathy_idle_set_flash_state (EmpathyIdle *idle,
560                               TpConnectionPresenceType   state)
561 {
562         EmpathyIdlePriv *priv;
563
564         priv = GET_PRIV (idle);
565
566         priv->flash_state = state;
567
568         if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
569         }
570
571         g_object_notify (G_OBJECT (idle), "flash-state");
572 }
573
574 static void
575 empathy_idle_do_set_presence (EmpathyIdle *idle,
576                            TpConnectionPresenceType status_type,
577                            const gchar *status_message)
578 {
579         EmpathyIdlePriv *priv = GET_PRIV (idle);
580         const gchar *statusses[NUM_TP_CONNECTION_PRESENCE_TYPES] = {
581                 NULL,
582                 "offline",
583                 "available",
584                 "away",
585                 "xa",
586                 "hidden",
587                 "busy",
588                 NULL,
589                 NULL,
590         };
591         const gchar *status;
592
593         g_assert (status_type > 0 && status_type < NUM_TP_CONNECTION_PRESENCE_TYPES);
594
595         status = statusses[status_type];
596
597         g_return_if_fail (status != NULL);
598
599         empathy_account_manager_request_global_presence (priv->manager,
600                 status_type, status, status_message);
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 (!priv->nm_connected) {
622                 DEBUG ("NM not connected");
623
624                 priv->nm_saved_state = state;
625                 if (tp_strdiff (priv->status, status)) {
626                         g_free (priv->status);
627                         priv->status = NULL;
628                         if (!EMP_STR_EMPTY (status)) {
629                                 priv->status = g_strdup (status);
630                         }
631                         g_object_notify (G_OBJECT (idle), "status");
632                 }
633
634                 return;
635         }
636
637         empathy_idle_do_set_presence (idle, state, status);
638 }
639
640 gboolean
641 empathy_idle_get_auto_away (EmpathyIdle *idle)
642 {
643         EmpathyIdlePriv *priv = GET_PRIV (idle);
644
645         return priv->auto_away;
646 }
647
648 void
649 empathy_idle_set_auto_away (EmpathyIdle *idle,
650                             gboolean     auto_away)
651 {
652         EmpathyIdlePriv *priv = GET_PRIV (idle);
653
654         priv->auto_away = auto_away;
655
656         g_object_notify (G_OBJECT (idle), "auto-away");
657 }
658
659 gboolean
660 empathy_idle_get_use_nm (EmpathyIdle *idle)
661 {
662         EmpathyIdlePriv *priv = GET_PRIV (idle);
663
664         return priv->use_nm;
665 }
666
667 void
668 empathy_idle_set_use_nm (EmpathyIdle *idle,
669                          gboolean     use_nm)
670 {
671         EmpathyIdlePriv *priv = GET_PRIV (idle);
672
673 #ifdef HAVE_NM
674         if (!priv->nm_client || use_nm == priv->use_nm) {
675                 return;
676         }
677 #endif
678
679         priv->use_nm = use_nm;
680
681 #ifdef HAVE_NM
682         if (use_nm) {
683                 idle_nm_state_change_cb (priv->nm_client, NULL, idle);
684 #else
685         if (0) {
686 #endif
687         } else {
688                 priv->nm_connected = TRUE;
689                 if (priv->nm_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
690                         empathy_idle_set_state (idle, priv->nm_saved_state);
691                 }
692                 priv->nm_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
693         }
694
695         g_object_notify (G_OBJECT (idle), "use-nm");
696 }
697