]> git.0d.be Git - empathy.git/blob - libempathy/empathy-presence-manager.c
account-settings: allow to change the service
[empathy.git] / libempathy / empathy-presence-manager.c
1 /*
2  * Copyright (C) 2007-2011 Collabora Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors: Xavier Claessens <xclaesse@gmail.com>
19  */
20
21 #include "empathy-presence-manager.h"
22
23 #include <config.h>
24
25 #include <string.h>
26
27 #include <glib/gi18n-lib.h>
28 #include <dbus/dbus-glib.h>
29
30 #include <telepathy-glib/account-manager.h>
31 #include <telepathy-glib/dbus.h>
32 #include <telepathy-glib/util.h>
33
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 /* Number of seconds to consider an account in the "just connected" state
44  * for. */
45 #define ACCOUNT_IS_JUST_CONNECTED_SECONDS 10
46
47 struct _EmpathyPresenceManagerPrivate
48 {
49   DBusGProxy *gs_proxy;
50   EmpathyConnectivity *connectivity;
51
52   gboolean ready;
53
54   TpConnectionPresenceType state;
55   gchar *status;
56   gboolean auto_away;
57
58   TpConnectionPresenceType away_saved_state;
59   TpConnectionPresenceType saved_state;
60   gchar *saved_status;
61
62   gboolean is_idle;
63   guint ext_away_timeout;
64
65   TpAccountManager *manager;
66
67   /* pointer to a TpAccount --> glong of time of connection */
68   GHashTable *connect_times;
69
70   TpConnectionPresenceType requested_presence_type;
71   gchar *requested_status_message;
72 };
73
74 typedef enum
75 {
76   SESSION_STATUS_AVAILABLE,
77   SESSION_STATUS_INVISIBLE,
78   SESSION_STATUS_BUSY,
79   SESSION_STATUS_IDLE,
80   SESSION_STATUS_UNKNOWN
81 } SessionStatus;
82
83 enum
84 {
85   PROP_0,
86   PROP_STATE,
87   PROP_STATUS,
88   PROP_AUTO_AWAY
89 };
90
91 G_DEFINE_TYPE (EmpathyPresenceManager, empathy_presence_manager, G_TYPE_OBJECT);
92
93 static EmpathyPresenceManager * singleton = NULL;
94
95 static const gchar *presence_type_to_status[NUM_TP_CONNECTION_PRESENCE_TYPES] =
96 {
97   NULL,
98   "offline",
99   "available",
100   "away",
101   "xa",
102   "hidden",
103   "busy",
104   NULL,
105   NULL,
106 };
107
108 static void
109 most_available_presence_changed (TpAccountManager *manager,
110     TpConnectionPresenceType state,
111     gchar *status,
112     gchar *status_message,
113     EmpathyPresenceManager *self)
114 {
115   if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET)
116     /* Assume our presence is offline if MC reports UNSET */
117     state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
118
119   DEBUG ("Presence changed to '%s' (%d) \"%s\"", status, state,
120     status_message);
121
122   g_free (self->priv->status);
123   self->priv->state = state;
124   if (EMP_STR_EMPTY (status_message))
125     self->priv->status = NULL;
126   else
127     self->priv->status = g_strdup (status_message);
128
129   g_object_notify (G_OBJECT (self), "state");
130   g_object_notify (G_OBJECT (self), "status");
131 }
132
133 static gboolean
134 ext_away_cb (EmpathyPresenceManager *self)
135 {
136   DEBUG ("Going to extended autoaway");
137   empathy_presence_manager_set_state (self,
138       TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY);
139   self->priv->ext_away_timeout = 0;
140
141   return FALSE;
142 }
143
144 static void
145 next_away_stop (EmpathyPresenceManager *self)
146 {
147   if (self->priv->ext_away_timeout)
148     {
149       g_source_remove (self->priv->ext_away_timeout);
150       self->priv->ext_away_timeout = 0;
151     }
152 }
153
154 static void
155 ext_away_start (EmpathyPresenceManager *self)
156 {
157   if (self->priv->ext_away_timeout != 0)
158     return;
159
160   self->priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
161       (GSourceFunc) ext_away_cb, self);
162 }
163
164 static void
165 session_status_changed_cb (DBusGProxy *gs_proxy,
166     SessionStatus status,
167     EmpathyPresenceManager *self)
168 {
169   gboolean is_idle;
170
171   is_idle = (status == SESSION_STATUS_IDLE);
172
173   DEBUG ("Session idle state changed, %s -> %s",
174     self->priv->is_idle ? "yes" : "no",
175     is_idle ? "yes" : "no");
176
177   if (!self->priv->auto_away ||
178       (self->priv->saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET &&
179        (self->priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
180         self->priv->state == TP_CONNECTION_PRESENCE_TYPE_HIDDEN)))
181     {
182       /* We don't want to go auto away OR we explicitely asked to be
183        * offline, nothing to do here */
184       self->priv->is_idle = is_idle;
185       return;
186     }
187
188   if (is_idle && !self->priv->is_idle)
189     {
190       TpConnectionPresenceType new_state;
191       /* We are now idle */
192
193       ext_away_start (self);
194
195       if (self->priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET)
196         /* We are disconnected, when coming back from away
197          * we want to restore the presence before the
198          * disconnection. */
199         self->priv->away_saved_state = self->priv->saved_state;
200       else
201         self->priv->away_saved_state = self->priv->state;
202
203     new_state = TP_CONNECTION_PRESENCE_TYPE_AWAY;
204     if (self->priv->state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY)
205       new_state = TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY;
206
207     DEBUG ("Going to autoaway. Saved state=%d, new state=%d",
208       self->priv->away_saved_state, new_state);
209     empathy_presence_manager_set_state (self, new_state);
210   }
211   else if (!is_idle && self->priv->is_idle)
212     {
213       /* We are no more idle, restore state */
214
215       next_away_stop (self);
216
217       /* Only try and set the presence if the away saved state is not
218        * unset. This is an odd case because it means that the session
219        * didn't notify us of the state change to idle, and as a
220        * result, we couldn't save the current state at that time.
221        */
222       if (self->priv->away_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET)
223         {
224           DEBUG ("Restoring state to %d",
225             self->priv->away_saved_state);
226
227           empathy_presence_manager_set_state (self,
228               self->priv->away_saved_state);
229         }
230       else
231         {
232           DEBUG ("Away saved state is unset. This means that we "
233                  "weren't told when the session went idle. "
234                  "As a result, I'm not trying to set presence");
235         }
236
237       self->priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
238     }
239
240   self->priv->is_idle = is_idle;
241 }
242
243 static void
244 state_change_cb (EmpathyConnectivity *connectivity,
245     gboolean new_online,
246     EmpathyPresenceManager *self)
247 {
248   if (!new_online)
249     {
250       /* We are no longer connected */
251       DEBUG ("Disconnected: Save state %d (%s)",
252           self->priv->state, self->priv->status);
253       self->priv->saved_state = self->priv->state;
254       g_free (self->priv->saved_status);
255       self->priv->saved_status = g_strdup (self->priv->status);
256       empathy_presence_manager_set_state (self,
257           TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
258     }
259   else if (new_online
260       && self->priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET)
261     {
262       /* We are now connected */
263       DEBUG ("Reconnected: Restore state %d (%s)",
264           self->priv->saved_state, self->priv->saved_status);
265       empathy_presence_manager_set_presence (self,
266           self->priv->saved_state,
267           self->priv->saved_status);
268       self->priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
269       g_free (self->priv->saved_status);
270       self->priv->saved_status = NULL;
271     }
272 }
273
274 static void
275 presence_manager_dispose (GObject *object)
276 {
277   EmpathyPresenceManager *self = (EmpathyPresenceManager *) object;
278
279   tp_clear_object (&self->priv->gs_proxy);
280   tp_clear_object (&self->priv->manager);
281
282   tp_clear_object (&self->priv->connectivity);
283   tp_clear_pointer (&self->priv->connect_times, g_hash_table_unref);
284
285   next_away_stop (EMPATHY_PRESENCE_MANAGER (object));
286
287   G_OBJECT_CLASS (empathy_presence_manager_parent_class)->dispose (object);
288 }
289
290 static void
291 presence_manager_finalize (GObject *object)
292 {
293   EmpathyPresenceManager *self = (EmpathyPresenceManager *) object;
294
295   g_free (self->priv->status);
296   g_free (self->priv->requested_status_message);
297
298   G_OBJECT_CLASS (empathy_presence_manager_parent_class)->finalize (object);
299 }
300
301 static GObject *
302 presence_manager_constructor (GType type,
303     guint n_props,
304     GObjectConstructParam *props)
305 {
306   GObject *retval;
307
308   if (singleton)
309     {
310       retval = g_object_ref (singleton);
311     }
312   else
313     {
314       retval = G_OBJECT_CLASS (empathy_presence_manager_parent_class)->
315         constructor (type, n_props, props);
316
317       singleton = EMPATHY_PRESENCE_MANAGER (retval);
318       g_object_add_weak_pointer (retval, (gpointer) &singleton);
319   }
320
321   return retval;
322 }
323
324 static const gchar *
325 empathy_presence_manager_get_status (EmpathyPresenceManager *self)
326 {
327   if (G_UNLIKELY (!self->priv->ready))
328     g_critical (G_STRLOC ": %s called before AccountManager ready",
329         G_STRFUNC);
330
331   if (!self->priv->status)
332     return empathy_presence_get_default_message (self->priv->state);
333
334   return self->priv->status;
335 }
336
337 static void
338 presence_manager_get_property (GObject *object,
339     guint param_id,
340     GValue *value,
341     GParamSpec *pspec)
342 {
343   EmpathyPresenceManager *self = EMPATHY_PRESENCE_MANAGER (object);
344
345   switch (param_id)
346     {
347       case PROP_STATE:
348         g_value_set_enum (value, empathy_presence_manager_get_state (self));
349         break;
350       case PROP_STATUS:
351         g_value_set_string (value, empathy_presence_manager_get_status (self));
352         break;
353       case PROP_AUTO_AWAY:
354         g_value_set_boolean (value,
355             empathy_presence_manager_get_auto_away (self));
356         break;
357       default:
358         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
359         break;
360     };
361 }
362
363 static void
364 presence_manager_set_property (GObject *object,
365     guint param_id,
366     const GValue *value,
367     GParamSpec *pspec)
368 {
369   EmpathyPresenceManager *self = EMPATHY_PRESENCE_MANAGER (object);
370
371   switch (param_id)
372     {
373       case PROP_STATE:
374         empathy_presence_manager_set_state (self, g_value_get_enum (value));
375         break;
376       case PROP_STATUS:
377         empathy_presence_manager_set_status (self, g_value_get_string (value));
378         break;
379       case PROP_AUTO_AWAY:
380         empathy_presence_manager_set_auto_away (self,
381             g_value_get_boolean (value));
382         break;
383       default:
384         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
385         break;
386     };
387 }
388
389 static void
390 empathy_presence_manager_class_init (EmpathyPresenceManagerClass *klass)
391 {
392   GObjectClass *object_class = G_OBJECT_CLASS (klass);
393
394   object_class->dispose = presence_manager_dispose;
395   object_class->finalize = presence_manager_finalize;
396   object_class->constructor = presence_manager_constructor;
397   object_class->get_property = presence_manager_get_property;
398   object_class->set_property = presence_manager_set_property;
399
400   g_object_class_install_property (object_class,
401       PROP_STATE,
402       g_param_spec_uint ("state", "state", "state",
403         0, NUM_TP_CONNECTION_PRESENCE_TYPES,
404         TP_CONNECTION_PRESENCE_TYPE_UNSET,
405         G_PARAM_READWRITE));
406
407   g_object_class_install_property (object_class,
408       PROP_STATUS,
409       g_param_spec_string ("status","status", "status",
410         NULL,
411         G_PARAM_READWRITE));
412
413    g_object_class_install_property (object_class,
414        PROP_AUTO_AWAY,
415        g_param_spec_boolean ("auto-away", "Automatic set presence to away",
416          "Should it set presence to away if inactive",
417          FALSE,
418          G_PARAM_READWRITE));
419
420   g_type_class_add_private (object_class,
421       sizeof (EmpathyPresenceManagerPrivate));
422 }
423
424 static void
425 account_status_changed_cb (TpAccount  *account,
426     guint old_status,
427     guint new_status,
428     guint reason,
429     gchar *dbus_error_name,
430     GHashTable *details,
431     gpointer user_data)
432 {
433   EmpathyPresenceManager *self = EMPATHY_PRESENCE_MANAGER (user_data);
434   GTimeVal val;
435
436   if (new_status == TP_CONNECTION_STATUS_CONNECTED)
437     {
438       g_get_current_time (&val);
439       g_hash_table_insert (self->priv->connect_times, account,
440                GINT_TO_POINTER (val.tv_sec));
441     }
442   else if (new_status == TP_CONNECTION_STATUS_DISCONNECTED)
443     {
444       g_hash_table_remove (self->priv->connect_times, account);
445     }
446 }
447
448 static void
449 account_manager_ready_cb (GObject *source_object,
450         GAsyncResult *result,
451         gpointer user_data)
452 {
453   EmpathyPresenceManager *self = user_data;
454   TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
455   TpConnectionPresenceType state;
456   gchar *status, *status_message;
457   GList *accounts, *l;
458   GError *error = NULL;
459
460   /* In case we've been finalized before reading this callback */
461   if (singleton == NULL)
462     return;
463
464   self->priv->ready = TRUE;
465
466   if (!tp_proxy_prepare_finish (account_manager, result, &error))
467     {
468       DEBUG ("Failed to prepare account manager: %s", error->message);
469       g_error_free (error);
470       return;
471     }
472
473   state = tp_account_manager_get_most_available_presence (self->priv->manager,
474     &status, &status_message);
475
476   most_available_presence_changed (account_manager, state, status,
477     status_message, self);
478
479   accounts = tp_account_manager_get_valid_accounts (self->priv->manager);
480   for (l = accounts; l != NULL; l = l->next)
481     {
482       tp_g_signal_connect_object (l->data, "status-changed",
483           G_CALLBACK (account_status_changed_cb), self, 0);
484     }
485   g_list_free (accounts);
486
487   g_free (status);
488   g_free (status_message);
489 }
490
491 static void
492 empathy_presence_manager_init (EmpathyPresenceManager *self)
493 {
494   TpDBusDaemon *dbus;
495
496   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
497       EMPATHY_TYPE_PRESENCE_MANAGER, EmpathyPresenceManagerPrivate);
498
499   self->priv->is_idle = FALSE;
500
501   self->priv->manager = tp_account_manager_dup ();
502
503   tp_proxy_prepare_async (self->priv->manager, NULL,
504       account_manager_ready_cb, self);
505
506   tp_g_signal_connect_object (self->priv->manager,
507       "most-available-presence-changed",
508       G_CALLBACK (most_available_presence_changed), self, 0);
509
510   dbus = tp_dbus_daemon_dup (NULL);
511
512   self->priv->gs_proxy = dbus_g_proxy_new_for_name (
513       tp_proxy_get_dbus_connection (dbus),
514       "org.gnome.SessionManager",
515       "/org/gnome/SessionManager/Presence",
516       "org.gnome.SessionManager.Presence");
517
518   if (self->priv->gs_proxy)
519     {
520       dbus_g_proxy_add_signal (self->priv->gs_proxy, "StatusChanged",
521           G_TYPE_UINT, G_TYPE_INVALID);
522       dbus_g_proxy_connect_signal (self->priv->gs_proxy, "StatusChanged",
523           G_CALLBACK (session_status_changed_cb),
524           self, NULL);
525     }
526   else
527     {
528       DEBUG ("Failed to get gs proxy");
529     }
530
531   g_object_unref (dbus);
532
533   self->priv->connectivity = empathy_connectivity_dup_singleton ();
534
535   tp_g_signal_connect_object (self->priv->connectivity,
536       "state-change", G_CALLBACK (state_change_cb), self, 0);
537
538   self->priv->connect_times = g_hash_table_new (g_direct_hash, g_direct_equal);
539 }
540
541 EmpathyPresenceManager *
542 empathy_presence_manager_dup_singleton (void)
543 {
544   return g_object_new (EMPATHY_TYPE_PRESENCE_MANAGER, NULL);
545 }
546
547 TpConnectionPresenceType
548 empathy_presence_manager_get_state (EmpathyPresenceManager *self)
549 {
550   if (G_UNLIKELY (!self->priv->ready))
551     g_critical (G_STRLOC ": %s called before AccountManager ready",
552         G_STRFUNC);
553
554   return self->priv->state;
555 }
556
557 void
558 empathy_presence_manager_set_state (EmpathyPresenceManager *self,
559     TpConnectionPresenceType state)
560 {
561   empathy_presence_manager_set_presence (self, state, self->priv->status);
562 }
563
564 void
565 empathy_presence_manager_set_status (EmpathyPresenceManager *self,
566        const gchar *status)
567 {
568   empathy_presence_manager_set_presence (self, self->priv->state, status);
569 }
570
571 static void
572 empathy_presence_manager_do_set_presence (EmpathyPresenceManager *self,
573     TpConnectionPresenceType status_type,
574     const gchar *status_message)
575 {
576   const gchar *status;
577
578   g_assert (status_type > 0 && status_type < NUM_TP_CONNECTION_PRESENCE_TYPES);
579
580   status = presence_type_to_status[status_type];
581
582   g_return_if_fail (status != NULL);
583
584   /* We possibly should be sure that the account manager is prepared, but
585    * sometimes this isn't possible, like when exiting. In other words,
586    * we need a callback to empathy_presence_manager_set_presence to be sure the
587    * presence is set on all accounts successfully.
588    * However, in practice, this is fine as we've already prepared the
589    * account manager here in _init. */
590   tp_account_manager_set_all_requested_presences (self->priv->manager,
591     status_type, status, status_message);
592 }
593
594 void
595 empathy_presence_manager_set_presence (EmpathyPresenceManager *self,
596     TpConnectionPresenceType state,
597     const gchar *status)
598 {
599   const gchar     *default_status;
600
601   DEBUG ("Changing presence to %s (%d)", status, state);
602
603   g_free (self->priv->requested_status_message);
604   self->priv->requested_presence_type = state;
605   self->priv->requested_status_message = g_strdup (status);
606
607   /* Do not set translated default messages */
608   default_status = empathy_presence_get_default_message (state);
609   if (!tp_strdiff (status, default_status))
610     status = NULL;
611
612   if (state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE &&
613       !empathy_connectivity_is_online (self->priv->connectivity))
614     {
615       DEBUG ("Empathy is not online");
616
617       self->priv->saved_state = state;
618       if (tp_strdiff (self->priv->status, status))
619         {
620           g_free (self->priv->saved_status);
621           self->priv->saved_status = NULL;
622           if (!EMP_STR_EMPTY (status))
623             self->priv->saved_status = g_strdup (status);
624         }
625       return;
626     }
627
628   empathy_presence_manager_do_set_presence (self, state, status);
629 }
630
631 gboolean
632 empathy_presence_manager_get_auto_away (EmpathyPresenceManager *self)
633 {
634   return self->priv->auto_away;
635 }
636
637 void
638 empathy_presence_manager_set_auto_away (EmpathyPresenceManager *self,
639           gboolean     auto_away)
640 {
641   self->priv->auto_away = auto_away;
642
643   g_object_notify (G_OBJECT (self), "auto-away");
644 }
645
646 TpConnectionPresenceType
647 empathy_presence_manager_get_requested_presence (EmpathyPresenceManager *self,
648     gchar **status,
649     gchar **status_message)
650 {
651   if (status != NULL)
652     *status = g_strdup (presence_type_to_status[
653         self->priv->requested_presence_type]);
654
655   if (status_message != NULL)
656     *status_message = g_strdup (self->priv->requested_status_message);
657
658   return self->priv->requested_presence_type;
659 }
660
661 /* This function returns %TRUE if EmpathyPresenceManager considers the account
662  * @account as having just connected recently. Otherwise, it returns
663  * %FALSE. In doubt, %FALSE is returned. */
664 gboolean
665 empathy_presence_manager_account_is_just_connected (
666     EmpathyPresenceManager *self,
667     TpAccount *account)
668 {
669   GTimeVal val;
670   gpointer ptr;
671   glong t;
672
673   if (tp_account_get_connection_status (account, NULL)
674       != TP_CONNECTION_STATUS_CONNECTED)
675     return FALSE;
676
677   ptr = g_hash_table_lookup (self->priv->connect_times, account);
678
679   if (ptr == NULL)
680     return FALSE;
681
682   t = GPOINTER_TO_INT (ptr);
683
684   g_get_current_time (&val);
685
686   return (val.tv_sec - t) < ACCOUNT_IS_JUST_CONNECTED_SECONDS;
687 }