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