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