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