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