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