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