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