]> git.0d.be Git - empathy.git/blob - libempathy/empathy-idle.c
idle: added a comment to explain _is_just_connected
[empathy.git] / libempathy / empathy-idle.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25
26 #include <glib/gi18n-lib.h>
27 #include <dbus/dbus-glib.h>
28
29 #include <telepathy-glib/account-manager.h>
30 #include <telepathy-glib/dbus.h>
31 #include <telepathy-glib/util.h>
32
33 #include "empathy-idle.h"
34 #include "empathy-utils.h"
35 #include "empathy-connectivity.h"
36
37 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
38 #include "empathy-debug.h"
39
40 /* Number of seconds before entering extended autoaway. */
41 #define EXT_AWAY_TIME (30*60)
42
43 /* Number of seconds to consider an account in the "just connected" state
44  * for. */
45 #define ACCOUNT_IS_JUST_CONNECTED_SECONDS 10
46
47 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIdle)
48 typedef struct {
49         DBusGProxy     *gs_proxy;
50         EmpathyConnectivity *connectivity;
51         gulong state_change_signal_id;
52
53         TpConnectionPresenceType      state;
54         gchar          *status;
55         TpConnectionPresenceType      flash_state;
56         gboolean        auto_away;
57
58         TpConnectionPresenceType      away_saved_state;
59         TpConnectionPresenceType      saved_state;
60         gchar          *saved_status;
61
62         gboolean        is_idle;
63         guint           ext_away_timeout;
64
65         TpAccountManager *manager;
66
67         /* pointer to a TpAccount --> glong of time of connection */
68         GHashTable *connect_times;
69
70         TpConnectionPresenceType requested_presence_type;
71         gchar *requested_status_message;
72
73 } EmpathyIdlePriv;
74
75 typedef enum {
76         SESSION_STATUS_AVAILABLE,
77         SESSION_STATUS_INVISIBLE,
78         SESSION_STATUS_BUSY,
79         SESSION_STATUS_IDLE,
80         SESSION_STATUS_UNKNOWN
81 } SessionStatus;
82
83 enum {
84         PROP_0,
85         PROP_STATE,
86         PROP_STATUS,
87         PROP_FLASH_STATE,
88         PROP_AUTO_AWAY
89 };
90
91 G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT);
92
93 static EmpathyIdle * idle_singleton = NULL;
94
95 static const gchar *presence_type_to_status[NUM_TP_CONNECTION_PRESENCE_TYPES] = {
96         NULL,
97         "offline",
98         "available",
99         "away",
100         "xa",
101         "hidden",
102         "busy",
103         NULL,
104         NULL,
105 };
106
107 static void
108 idle_presence_changed_cb (TpAccountManager *manager,
109                           TpConnectionPresenceType state,
110                           gchar          *status,
111                           gchar          *status_message,
112                           EmpathyIdle    *idle)
113 {
114         EmpathyIdlePriv *priv;
115
116         priv = GET_PRIV (idle);
117
118         if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET)
119                 /* Assume our presence is offline if MC reports UNSET */
120                 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
121
122         DEBUG ("Presence changed to '%s' (%d) \"%s\"", status, state,
123                 status_message);
124
125         g_free (priv->status);
126         priv->state = state;
127         if (EMP_STR_EMPTY (status_message))
128                 priv->status = NULL;
129         else
130                 priv->status = g_strdup (status_message);
131
132         g_object_notify (G_OBJECT (idle), "state");
133         g_object_notify (G_OBJECT (idle), "status");
134 }
135
136 static gboolean
137 idle_ext_away_cb (EmpathyIdle *idle)
138 {
139         EmpathyIdlePriv *priv;
140
141         priv = GET_PRIV (idle);
142
143         DEBUG ("Going to extended autoaway");
144         empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY);
145         priv->ext_away_timeout = 0;
146
147         return FALSE;
148 }
149
150 static void
151 idle_ext_away_stop (EmpathyIdle *idle)
152 {
153         EmpathyIdlePriv *priv;
154
155         priv = GET_PRIV (idle);
156
157         if (priv->ext_away_timeout) {
158                 g_source_remove (priv->ext_away_timeout);
159                 priv->ext_away_timeout = 0;
160         }
161 }
162
163 static void
164 idle_ext_away_start (EmpathyIdle *idle)
165 {
166         EmpathyIdlePriv *priv;
167
168         priv = GET_PRIV (idle);
169
170         if (priv->ext_away_timeout != 0) {
171                 return;
172         }
173         priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
174                                                         (GSourceFunc) idle_ext_away_cb,
175                                                         idle);
176 }
177
178 static void
179 idle_session_status_changed_cb (DBusGProxy    *gs_proxy,
180                                 SessionStatus  status,
181                                 EmpathyIdle   *idle)
182 {
183         EmpathyIdlePriv *priv;
184         gboolean is_idle;
185
186         priv = GET_PRIV (idle);
187
188         is_idle = (status == SESSION_STATUS_IDLE);
189
190         DEBUG ("Session idle state changed, %s -> %s",
191                 priv->is_idle ? "yes" : "no",
192                 is_idle ? "yes" : "no");
193
194         if (!priv->auto_away ||
195             (priv->saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET &&
196              (priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
197               priv->state == TP_CONNECTION_PRESENCE_TYPE_HIDDEN))) {
198                 /* We don't want to go auto away OR we explicitely asked to be
199                  * offline, nothing to do here */
200                 priv->is_idle = is_idle;
201                 return;
202         }
203
204         if (is_idle && !priv->is_idle) {
205                 TpConnectionPresenceType new_state;
206                 /* We are now idle */
207
208                 idle_ext_away_start (idle);
209
210                 if (priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
211                         /* We are disconnected, when coming back from away
212                          * we want to restore the presence before the
213                          * disconnection. */
214                         priv->away_saved_state = priv->saved_state;
215                 } else {
216                         priv->away_saved_state = priv->state;
217                 }
218
219                 new_state = TP_CONNECTION_PRESENCE_TYPE_AWAY;
220                 if (priv->state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
221                         new_state = TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY;
222                 }
223
224                 DEBUG ("Going to autoaway. Saved state=%d, new state=%d",
225                         priv->away_saved_state, new_state);
226                 empathy_idle_set_state (idle, new_state);
227         } else if (!is_idle && priv->is_idle) {
228                 const gchar *new_status;
229                 /* We are no more idle, restore state */
230
231                 idle_ext_away_stop (idle);
232
233                 if (priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_AWAY ||
234                     priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
235                         priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
236                         new_status = NULL;
237                 } else {
238                         new_status = priv->status;
239                 }
240
241                 /* Only try and set the presence if the away saved state is not
242                  * unset. This is an odd case because it means that the session
243                  * didn't notify us of the state change to idle, and as a
244                  * result, we couldn't save the current state at that time.
245                  */
246                 if (priv->away_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
247                         DEBUG ("Restoring state to %d, reset status to %s",
248                                 priv->away_saved_state, new_status);
249
250                         empathy_idle_set_presence (idle,
251                                                    priv->away_saved_state,
252                                                    new_status);
253                 } else {
254                         DEBUG ("Away saved state is unset. This means that we "
255                                "weren't told when the session went idle. "
256                                "As a result, I'm not trying to set presence");
257                 }
258
259                 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
260         }
261
262         priv->is_idle = is_idle;
263 }
264
265 static void
266 idle_state_change_cb (EmpathyConnectivity *connectivity,
267                       gboolean new_online,
268                       EmpathyIdle *idle)
269 {
270         EmpathyIdlePriv *priv;
271
272         priv = GET_PRIV (idle);
273
274         if (!new_online) {
275                 /* We are no longer connected */
276                 DEBUG ("Disconnected: Save state %d (%s)",
277                                 priv->state, priv->status);
278                 priv->saved_state = priv->state;
279                 g_free (priv->saved_status);
280                 priv->saved_status = g_strdup (priv->status);
281                 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
282         }
283         else if (new_online
284                         && priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
285                 /* We are now connected */
286                 DEBUG ("Reconnected: Restore state %d (%s)",
287                                 priv->saved_state, priv->saved_status);
288                 empathy_idle_set_presence (idle,
289                                 priv->saved_state,
290                                 priv->saved_status);
291                 priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
292                 g_free (priv->saved_status);
293                 priv->saved_status = NULL;
294         }
295 }
296
297 static void
298 idle_finalize (GObject *object)
299 {
300         EmpathyIdlePriv *priv;
301
302         priv = GET_PRIV (object);
303
304         g_free (priv->status);
305         g_free (priv->requested_status_message);
306
307         if (priv->gs_proxy) {
308                 g_object_unref (priv->gs_proxy);
309         }
310
311         g_signal_handler_disconnect (priv->connectivity,
312                                      priv->state_change_signal_id);
313         priv->state_change_signal_id = 0;
314
315         g_object_unref (priv->connectivity);
316
317         g_hash_table_destroy (priv->connect_times);
318         priv->connect_times = NULL;
319
320         idle_ext_away_stop (EMPATHY_IDLE (object));
321 }
322
323 static GObject *
324 idle_constructor (GType type,
325                   guint n_props,
326                   GObjectConstructParam *props)
327 {
328         GObject *retval;
329
330         if (idle_singleton) {
331                 retval = g_object_ref (idle_singleton);
332         } else {
333                 retval = G_OBJECT_CLASS (empathy_idle_parent_class)->constructor
334                         (type, n_props, props);
335
336                 idle_singleton = EMPATHY_IDLE (retval);
337                 g_object_add_weak_pointer (retval, (gpointer) &idle_singleton);
338         }
339
340         return retval;
341 }
342
343 static void
344 idle_get_property (GObject    *object,
345                    guint       param_id,
346                    GValue     *value,
347                    GParamSpec *pspec)
348 {
349         EmpathyIdlePriv *priv;
350         EmpathyIdle     *idle;
351
352         priv = GET_PRIV (object);
353         idle = EMPATHY_IDLE (object);
354
355         switch (param_id) {
356         case PROP_STATE:
357                 g_value_set_enum (value, empathy_idle_get_state (idle));
358                 break;
359         case PROP_STATUS:
360                 g_value_set_string (value, empathy_idle_get_status (idle));
361                 break;
362         case PROP_FLASH_STATE:
363                 g_value_set_enum (value, empathy_idle_get_flash_state (idle));
364                 break;
365         case PROP_AUTO_AWAY:
366                 g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
367                 break;
368         default:
369                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
370                 break;
371         };
372 }
373
374 static void
375 idle_set_property (GObject      *object,
376                    guint         param_id,
377                    const GValue *value,
378                    GParamSpec   *pspec)
379 {
380         EmpathyIdlePriv *priv;
381         EmpathyIdle     *idle;
382
383         priv = GET_PRIV (object);
384         idle = EMPATHY_IDLE (object);
385
386         switch (param_id) {
387         case PROP_STATE:
388                 empathy_idle_set_state (idle, g_value_get_enum (value));
389                 break;
390         case PROP_STATUS:
391                 empathy_idle_set_status (idle, g_value_get_string (value));
392                 break;
393         case PROP_FLASH_STATE:
394                 empathy_idle_set_flash_state (idle, g_value_get_enum (value));
395                 break;
396         case PROP_AUTO_AWAY:
397                 empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
398                 break;
399         default:
400                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
401                 break;
402         };
403 }
404
405 static void
406 empathy_idle_class_init (EmpathyIdleClass *klass)
407 {
408         GObjectClass *object_class = G_OBJECT_CLASS (klass);
409
410         object_class->finalize = idle_finalize;
411         object_class->constructor = idle_constructor;
412         object_class->get_property = idle_get_property;
413         object_class->set_property = idle_set_property;
414
415         g_object_class_install_property (object_class,
416                                          PROP_STATE,
417                                          g_param_spec_uint ("state",
418                                                             "state",
419                                                             "state",
420                                                             0, NUM_TP_CONNECTION_PRESENCE_TYPES,
421                                                             TP_CONNECTION_PRESENCE_TYPE_UNSET,
422                                                             G_PARAM_READWRITE));
423         g_object_class_install_property (object_class,
424                                          PROP_STATUS,
425                                          g_param_spec_string ("status",
426                                                               "status",
427                                                               "status",
428                                                               NULL,
429                                                               G_PARAM_READWRITE));
430         g_object_class_install_property (object_class,
431                                          PROP_FLASH_STATE,
432                                          g_param_spec_uint ("flash-state",
433                                                             "flash-state",
434                                                             "flash-state",
435                                                             0, NUM_TP_CONNECTION_PRESENCE_TYPES,
436                                                             TP_CONNECTION_PRESENCE_TYPE_UNSET,
437                                                             G_PARAM_READWRITE));
438
439          g_object_class_install_property (object_class,
440                                           PROP_AUTO_AWAY,
441                                           g_param_spec_boolean ("auto-away",
442                                                                 "Automatic set presence to away",
443                                                                 "Should it set presence to away if inactive",
444                                                                 FALSE,
445                                                                 G_PARAM_READWRITE));
446
447         g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
448 }
449
450 static void
451 account_status_changed_cb (TpAccount  *account,
452                            guint       old_status,
453                            guint       new_status,
454                            guint       reason,
455                            gchar      *dbus_error_name,
456                            GHashTable *details,
457                            gpointer    user_data)
458 {
459         EmpathyIdle *idle = EMPATHY_IDLE (user_data);
460         EmpathyIdlePriv *priv = GET_PRIV (idle);
461         GTimeVal val;
462
463         if (new_status == TP_CONNECTION_STATUS_CONNECTED) {
464                 g_get_current_time (&val);
465                 g_hash_table_insert (priv->connect_times, account,
466                                      GINT_TO_POINTER (val.tv_sec));
467         } else if (new_status == TP_CONNECTION_STATUS_DISCONNECTED) {
468                 g_hash_table_remove (priv->connect_times, account);
469         }
470 }
471
472 static void
473 account_manager_ready_cb (GObject *source_object,
474                           GAsyncResult *result,
475                           gpointer user_data)
476 {
477         EmpathyIdle *idle = EMPATHY_IDLE (user_data);
478         TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
479         EmpathyIdlePriv *priv = GET_PRIV (idle);
480         TpConnectionPresenceType state;
481         gchar *status, *status_message;
482         GList *accounts, *l;
483         GError *error = NULL;
484
485         if (!tp_account_manager_prepare_finish (account_manager, result, &error)) {
486                 DEBUG ("Failed to prepare account manager: %s", error->message);
487                 g_error_free (error);
488                 return;
489         }
490
491         state = tp_account_manager_get_most_available_presence (priv->manager,
492                 &status, &status_message);
493
494         idle_presence_changed_cb (account_manager, state, status,
495                 status_message, idle);
496
497         accounts = tp_account_manager_get_valid_accounts (priv->manager);
498         for (l = accounts; l != NULL; l = l->next) {
499                 empathy_signal_connect_weak (l->data, "status-changed",
500                                              G_CALLBACK (account_status_changed_cb),
501                                              G_OBJECT (idle));
502         }
503         g_list_free (accounts);
504
505         g_free (status);
506         g_free (status_message);
507 }
508
509 static void
510 empathy_idle_init (EmpathyIdle *idle)
511 {
512         EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
513                 EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
514
515         idle->priv = priv;
516         priv->is_idle = FALSE;
517
518         priv->manager = tp_account_manager_dup ();
519
520         tp_account_manager_prepare_async (priv->manager, NULL,
521             account_manager_ready_cb, idle);
522
523         g_signal_connect (priv->manager, "most-available-presence-changed",
524                 G_CALLBACK (idle_presence_changed_cb), idle);
525
526         priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
527                                                     "org.gnome.SessionManager",
528                                                     "/org/gnome/SessionManager/Presence",
529                                                     "org.gnome.SessionManager.Presence");
530         if (priv->gs_proxy) {
531                 dbus_g_proxy_add_signal (priv->gs_proxy, "StatusChanged",
532                                          G_TYPE_UINT, G_TYPE_INVALID);
533                 dbus_g_proxy_connect_signal (priv->gs_proxy, "StatusChanged",
534                                              G_CALLBACK (idle_session_status_changed_cb),
535                                              idle, NULL);
536         } else {
537                 DEBUG ("Failed to get gs proxy");
538         }
539
540         priv->connectivity = empathy_connectivity_dup_singleton ();
541         priv->state_change_signal_id = g_signal_connect (priv->connectivity,
542             "state-change", G_CALLBACK (idle_state_change_cb), idle);
543
544         priv->connect_times = g_hash_table_new (g_direct_hash, g_direct_equal);
545 }
546
547 EmpathyIdle *
548 empathy_idle_dup_singleton (void)
549 {
550         return g_object_new (EMPATHY_TYPE_IDLE, NULL);
551 }
552
553 TpConnectionPresenceType
554 empathy_idle_get_state (EmpathyIdle *idle)
555 {
556         EmpathyIdlePriv *priv;
557
558         priv = GET_PRIV (idle);
559
560         return priv->state;
561 }
562
563 void
564 empathy_idle_set_state (EmpathyIdle *idle,
565                         TpConnectionPresenceType   state)
566 {
567         EmpathyIdlePriv *priv;
568
569         priv = GET_PRIV (idle);
570
571         empathy_idle_set_presence (idle, state, priv->status);
572 }
573
574 const gchar *
575 empathy_idle_get_status (EmpathyIdle *idle)
576 {
577         EmpathyIdlePriv *priv;
578
579         priv = GET_PRIV (idle);
580
581         if (!priv->status) {
582                 return empathy_presence_get_default_message (priv->state);
583         }
584
585         return priv->status;
586 }
587
588 void
589 empathy_idle_set_status (EmpathyIdle *idle,
590                          const gchar *status)
591 {
592         EmpathyIdlePriv *priv;
593
594         priv = GET_PRIV (idle);
595
596         empathy_idle_set_presence (idle, priv->state, status);
597 }
598
599 TpConnectionPresenceType
600 empathy_idle_get_flash_state (EmpathyIdle *idle)
601 {
602         EmpathyIdlePriv *priv;
603
604         priv = GET_PRIV (idle);
605
606         return priv->flash_state;
607 }
608
609 void
610 empathy_idle_set_flash_state (EmpathyIdle *idle,
611                               TpConnectionPresenceType   state)
612 {
613         EmpathyIdlePriv *priv;
614
615         priv = GET_PRIV (idle);
616
617         priv->flash_state = state;
618
619         if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
620         }
621
622         g_object_notify (G_OBJECT (idle), "flash-state");
623 }
624
625 static void
626 empathy_idle_do_set_presence (EmpathyIdle *idle,
627                            TpConnectionPresenceType status_type,
628                            const gchar *status_message)
629 {
630         EmpathyIdlePriv *priv = GET_PRIV (idle);
631         const gchar *status;
632
633         g_assert (status_type > 0 && status_type < NUM_TP_CONNECTION_PRESENCE_TYPES);
634
635         status = presence_type_to_status[status_type];
636
637         g_return_if_fail (status != NULL);
638
639         /* We possibly should be sure that the account manager is prepared, but
640          * sometimes this isn't possible, like when exiting. In other words,
641          * we need a callback to empathy_idle_set_presence to be sure the
642          * presence is set on all accounts successfully.
643          * However, in practice, this is fine as we've already prepared the
644          * account manager here in _init. */
645         tp_account_manager_set_all_requested_presences (priv->manager,
646                 status_type, status, status_message);
647 }
648
649 void
650 empathy_idle_set_presence (EmpathyIdle *idle,
651                            TpConnectionPresenceType   state,
652                            const gchar *status)
653 {
654         EmpathyIdlePriv *priv;
655         const gchar     *default_status;
656
657         priv = GET_PRIV (idle);
658
659         DEBUG ("Changing presence to %s (%d)", status, state);
660
661         g_free (priv->requested_status_message);
662         priv->requested_presence_type = state;
663         priv->requested_status_message = g_strdup (status);
664
665         /* Do not set translated default messages */
666         default_status = empathy_presence_get_default_message (state);
667         if (!tp_strdiff (status, default_status)) {
668                 status = NULL;
669         }
670
671         if (state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE &&
672                         !empathy_connectivity_is_online (priv->connectivity)) {
673                 DEBUG ("Empathy is not online");
674
675                 priv->saved_state = state;
676                 if (tp_strdiff (priv->status, status)) {
677                         g_free (priv->saved_status);
678                         priv->saved_status = NULL;
679                         if (!EMP_STR_EMPTY (status)) {
680                                 priv->saved_status = g_strdup (status);
681                         }
682                 }
683                 return;
684         }
685
686         empathy_idle_do_set_presence (idle, state, status);
687 }
688
689 gboolean
690 empathy_idle_get_auto_away (EmpathyIdle *idle)
691 {
692         EmpathyIdlePriv *priv = GET_PRIV (idle);
693
694         return priv->auto_away;
695 }
696
697 void
698 empathy_idle_set_auto_away (EmpathyIdle *idle,
699                             gboolean     auto_away)
700 {
701         EmpathyIdlePriv *priv = GET_PRIV (idle);
702
703         priv->auto_away = auto_away;
704
705         g_object_notify (G_OBJECT (idle), "auto-away");
706 }
707
708 TpConnectionPresenceType
709 empathy_idle_get_requested_presence (EmpathyIdle *idle,
710     gchar **status,
711     gchar **status_message)
712 {
713         EmpathyIdlePriv *priv = GET_PRIV (idle);
714
715         if (status != NULL) {
716                 *status = g_strdup (presence_type_to_status[priv->requested_presence_type]);
717         }
718
719         if (status_message != NULL) {
720                 *status_message = g_strdup (priv->requested_status_message);
721         }
722
723         return priv->requested_presence_type;
724 }
725
726 /* This function returns %TRUE if EmpathyIdle considers the account
727  * @account as having just connected recently. Otherwise, it returns
728  * %FALSE. In doubt, %FALSE is returned. */
729 gboolean
730 empathy_idle_account_is_just_connected (EmpathyIdle *idle,
731                                         TpAccount *account)
732 {
733         EmpathyIdlePriv *priv = GET_PRIV (idle);
734         GTimeVal val;
735         gpointer ptr;
736         glong t;
737
738         if (tp_account_get_connection_status (account, NULL)
739             != TP_CONNECTION_STATUS_CONNECTED) {
740                 return FALSE;
741         }
742
743         ptr = g_hash_table_lookup (priv->connect_times, account);
744
745         if (ptr == NULL) {
746                 return FALSE;
747         }
748
749         t = GPOINTER_TO_INT (ptr);
750
751         g_get_current_time (&val);
752
753         return (val.tv_sec - t) < ACCOUNT_IS_JUST_CONNECTED_SECONDS;
754 }