]> git.0d.be Git - empathy.git/blob - libempathy/empathy-idle.c
Removed EmpathyAccount{,Manager}.
[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/dbus.h>
30 #include <telepathy-glib/util.h>
31
32 #include "empathy-account-manager.h"
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 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIdle)
44 typedef struct {
45         DBusGProxy     *gs_proxy;
46         EmpathyConnectivity *connectivity;
47         gulong state_change_signal_id;
48
49         TpConnectionPresenceType      state;
50         gchar          *status;
51         TpConnectionPresenceType      flash_state;
52         gboolean        auto_away;
53
54         TpConnectionPresenceType      away_saved_state;
55         TpConnectionPresenceType      saved_state;
56         gchar          *saved_status;
57
58         gboolean        is_idle;
59         guint           ext_away_timeout;
60
61         EmpathyAccountManager *manager;
62 } EmpathyIdlePriv;
63
64 typedef enum {
65         SESSION_STATUS_AVAILABLE,
66         SESSION_STATUS_INVISIBLE,
67         SESSION_STATUS_BUSY,
68         SESSION_STATUS_IDLE,
69         SESSION_STATUS_UNKNOWN
70 } SessionStatus;
71
72 enum {
73         PROP_0,
74         PROP_STATE,
75         PROP_STATUS,
76         PROP_FLASH_STATE,
77         PROP_AUTO_AWAY
78 };
79
80 G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT);
81
82 static EmpathyIdle * idle_singleton = NULL;
83
84 static void
85 idle_presence_changed_cb (EmpathyAccountManager *manager,
86                           TpConnectionPresenceType state,
87                           gchar          *status,
88                           gchar          *status_message,
89                           EmpathyIdle    *idle)
90 {
91         EmpathyIdlePriv *priv;
92
93         priv = GET_PRIV (idle);
94
95         if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET)
96                 /* Assume our presence is offline if MC reports UNSET */
97                 state = TP_CONNECTION_PRESENCE_TYPE_OFFLINE;
98
99         DEBUG ("Presence changed to '%s' (%d) \"%s\"", status, state,
100                 status_message);
101
102         g_free (priv->status);
103         priv->state = state;
104         if (EMP_STR_EMPTY (status_message))
105                 priv->status = NULL;
106         else
107                 priv->status = g_strdup (status_message);
108
109         g_object_notify (G_OBJECT (idle), "state");
110         g_object_notify (G_OBJECT (idle), "status");
111 }
112
113 static gboolean
114 idle_ext_away_cb (EmpathyIdle *idle)
115 {
116         EmpathyIdlePriv *priv;
117
118         priv = GET_PRIV (idle);
119
120         DEBUG ("Going to extended autoaway");
121         empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY);
122         priv->ext_away_timeout = 0;
123
124         return FALSE;
125 }
126
127 static void
128 idle_ext_away_stop (EmpathyIdle *idle)
129 {
130         EmpathyIdlePriv *priv;
131
132         priv = GET_PRIV (idle);
133
134         if (priv->ext_away_timeout) {
135                 g_source_remove (priv->ext_away_timeout);
136                 priv->ext_away_timeout = 0;
137         }
138 }
139
140 static void
141 idle_ext_away_start (EmpathyIdle *idle)
142 {
143         EmpathyIdlePriv *priv;
144
145         priv = GET_PRIV (idle);
146
147         if (priv->ext_away_timeout != 0) {
148                 return;
149         }
150         priv->ext_away_timeout = g_timeout_add_seconds (EXT_AWAY_TIME,
151                                                         (GSourceFunc) idle_ext_away_cb,
152                                                         idle);
153 }
154
155 static void
156 idle_session_status_changed_cb (DBusGProxy    *gs_proxy,
157                                 SessionStatus  status,
158                                 EmpathyIdle   *idle)
159 {
160         EmpathyIdlePriv *priv;
161         gboolean is_idle;
162
163         priv = GET_PRIV (idle);
164
165         is_idle = (status == SESSION_STATUS_IDLE);
166
167         DEBUG ("Session idle state changed, %s -> %s",
168                 priv->is_idle ? "yes" : "no",
169                 is_idle ? "yes" : "no");
170
171         if (!priv->auto_away ||
172             (priv->saved_state == TP_CONNECTION_PRESENCE_TYPE_UNSET &&
173              (priv->state <= TP_CONNECTION_PRESENCE_TYPE_OFFLINE ||
174               priv->state == TP_CONNECTION_PRESENCE_TYPE_HIDDEN))) {
175                 /* We don't want to go auto away OR we explicitely asked to be
176                  * offline, nothing to do here */
177                 priv->is_idle = is_idle;
178                 return;
179         }
180
181         if (is_idle && !priv->is_idle) {
182                 TpConnectionPresenceType new_state;
183                 /* We are now idle */
184
185                 idle_ext_away_start (idle);
186
187                 if (priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
188                         /* We are disconnected, when coming back from away
189                          * we want to restore the presence before the
190                          * disconnection. */
191                         priv->away_saved_state = priv->saved_state;
192                 } else {
193                         priv->away_saved_state = priv->state;
194                 }
195
196                 new_state = TP_CONNECTION_PRESENCE_TYPE_AWAY;
197                 if (priv->state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
198                         new_state = TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY;
199                 }
200
201                 DEBUG ("Going to autoaway. Saved state=%d, new state=%d",
202                         priv->away_saved_state, new_state);
203                 empathy_idle_set_state (idle, new_state);
204         } else if (!is_idle && priv->is_idle) {
205                 const gchar *new_status;
206                 /* We are no more idle, restore state */
207
208                 idle_ext_away_stop (idle);
209
210                 if (priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_AWAY ||
211                     priv->away_saved_state == TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY) {
212                         priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_AVAILABLE;
213                         new_status = NULL;
214                 } else {
215                         new_status = priv->status;
216                 }
217
218                 /* Only try and set the presence if the away saved state is not
219                  * unset. This is an odd case because it means that the session
220                  * didn't notify us of the state change to idle, and as a
221                  * result, we couldn't save the current state at that time.
222                  */
223                 if (priv->away_saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
224                         DEBUG ("Restoring state to %d, reset status to %s",
225                                 priv->away_saved_state, new_status);
226
227                         empathy_idle_set_presence (idle,
228                                                    priv->away_saved_state,
229                                                    new_status);
230                 } else {
231                         DEBUG ("Away saved state is unset. This means that we "
232                                "weren't told when the session went idle. "
233                                "As a result, I'm not trying to set presence");
234                 }
235
236                 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
237         }
238
239         priv->is_idle = is_idle;
240 }
241
242 static void
243 idle_state_change_cb (EmpathyConnectivity *connectivity,
244                       gboolean new_online,
245                       EmpathyIdle *idle)
246 {
247         EmpathyIdlePriv *priv;
248
249         priv = GET_PRIV (idle);
250
251         if (!new_online) {
252                 /* We are no longer connected */
253                 DEBUG ("Disconnected: Save state %d (%s)",
254                                 priv->state, priv->status);
255                 priv->saved_state = priv->state;
256                 g_free (priv->saved_status);
257                 priv->saved_status = g_strdup (priv->status);
258                 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
259         }
260         else if (new_online
261                         && priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
262                 /* We are now connected */
263                 DEBUG ("Reconnected: Restore state %d (%s)",
264                                 priv->saved_state, priv->saved_status);
265                 empathy_idle_set_presence (idle,
266                                 priv->saved_state,
267                                 priv->saved_status);
268                 priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
269                 g_free (priv->saved_status);
270                 priv->saved_status = NULL;
271         }
272 }
273
274 static void
275 idle_finalize (GObject *object)
276 {
277         EmpathyIdlePriv *priv;
278
279         priv = GET_PRIV (object);
280
281         g_free (priv->status);
282
283         if (priv->gs_proxy) {
284                 g_object_unref (priv->gs_proxy);
285         }
286
287         g_signal_handler_disconnect (priv->connectivity,
288                                      priv->state_change_signal_id);
289         priv->state_change_signal_id = 0;
290
291         g_object_unref (priv->connectivity);
292
293         idle_ext_away_stop (EMPATHY_IDLE (object));
294 }
295
296 static GObject *
297 idle_constructor (GType type,
298                   guint n_props,
299                   GObjectConstructParam *props)
300 {
301         GObject *retval;
302
303         if (idle_singleton) {
304                 retval = g_object_ref (idle_singleton);
305         } else {
306                 retval = G_OBJECT_CLASS (empathy_idle_parent_class)->constructor
307                         (type, n_props, props);
308
309                 idle_singleton = EMPATHY_IDLE (retval);
310                 g_object_add_weak_pointer (retval, (gpointer) &idle_singleton);
311         }
312
313         return retval;
314 }
315
316 static void
317 idle_get_property (GObject    *object,
318                    guint       param_id,
319                    GValue     *value,
320                    GParamSpec *pspec)
321 {
322         EmpathyIdlePriv *priv;
323         EmpathyIdle     *idle;
324
325         priv = GET_PRIV (object);
326         idle = EMPATHY_IDLE (object);
327
328         switch (param_id) {
329         case PROP_STATE:
330                 g_value_set_enum (value, empathy_idle_get_state (idle));
331                 break;
332         case PROP_STATUS:
333                 g_value_set_string (value, empathy_idle_get_status (idle));
334                 break;
335         case PROP_FLASH_STATE:
336                 g_value_set_enum (value, empathy_idle_get_flash_state (idle));
337                 break;
338         case PROP_AUTO_AWAY:
339                 g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
340                 break;
341         default:
342                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
343                 break;
344         };
345 }
346
347 static void
348 idle_set_property (GObject      *object,
349                    guint         param_id,
350                    const GValue *value,
351                    GParamSpec   *pspec)
352 {
353         EmpathyIdlePriv *priv;
354         EmpathyIdle     *idle;
355
356         priv = GET_PRIV (object);
357         idle = EMPATHY_IDLE (object);
358
359         switch (param_id) {
360         case PROP_STATE:
361                 empathy_idle_set_state (idle, g_value_get_enum (value));
362                 break;
363         case PROP_STATUS:
364                 empathy_idle_set_status (idle, g_value_get_string (value));
365                 break;
366         case PROP_FLASH_STATE:
367                 empathy_idle_set_flash_state (idle, g_value_get_enum (value));
368                 break;
369         case PROP_AUTO_AWAY:
370                 empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
371                 break;
372         default:
373                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
374                 break;
375         };
376 }
377
378 static void
379 empathy_idle_class_init (EmpathyIdleClass *klass)
380 {
381         GObjectClass *object_class = G_OBJECT_CLASS (klass);
382
383         object_class->finalize = idle_finalize;
384         object_class->constructor = idle_constructor;
385         object_class->get_property = idle_get_property;
386         object_class->set_property = idle_set_property;
387
388         g_object_class_install_property (object_class,
389                                          PROP_STATE,
390                                          g_param_spec_uint ("state",
391                                                             "state",
392                                                             "state",
393                                                             0, NUM_TP_CONNECTION_PRESENCE_TYPES,
394                                                             TP_CONNECTION_PRESENCE_TYPE_UNSET,
395                                                             G_PARAM_READWRITE));
396         g_object_class_install_property (object_class,
397                                          PROP_STATUS,
398                                          g_param_spec_string ("status",
399                                                               "status",
400                                                               "status",
401                                                               NULL,
402                                                               G_PARAM_READWRITE));
403         g_object_class_install_property (object_class,
404                                          PROP_FLASH_STATE,
405                                          g_param_spec_uint ("flash-state",
406                                                             "flash-state",
407                                                             "flash-state",
408                                                             0, NUM_TP_CONNECTION_PRESENCE_TYPES,
409                                                             TP_CONNECTION_PRESENCE_TYPE_UNSET,
410                                                             G_PARAM_READWRITE));
411
412          g_object_class_install_property (object_class,
413                                           PROP_AUTO_AWAY,
414                                           g_param_spec_boolean ("auto-away",
415                                                                 "Automatic set presence to away",
416                                                                 "Should it set presence to away if inactive",
417                                                                 FALSE,
418                                                                 G_PARAM_READWRITE));
419
420         g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
421 }
422
423 static void
424 account_manager_ready_cb (EmpathyAccountManager *account_manager,
425                           GParamSpec *pspec,
426                           EmpathyIdle *idle)
427 {
428         EmpathyIdlePriv *priv;
429         TpConnectionPresenceType state;
430         gchar *status, *status_message;
431
432         priv = GET_PRIV (idle);
433
434         state = empathy_account_manager_get_global_presence (priv->manager,
435                 &status, &status_message);
436
437         idle_presence_changed_cb (account_manager, state, status,
438                 status_message, idle);
439
440         g_free (status);
441         g_free (status_message);
442 }
443
444 static void
445 empathy_idle_init (EmpathyIdle *idle)
446 {
447         EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
448                 EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
449
450         idle->priv = priv;
451         priv->is_idle = FALSE;
452
453         priv->manager = empathy_account_manager_dup_singleton ();
454
455         if (empathy_account_manager_is_ready (priv->manager)) {
456                 priv->state = empathy_account_manager_get_global_presence (priv->manager,
457                         NULL, &priv->status);
458         } else {
459                 g_signal_connect (priv->manager, "notify::ready",
460                         G_CALLBACK (account_manager_ready_cb), idle);
461         }
462
463
464         g_signal_connect (priv->manager, "global-presence-changed",
465                 G_CALLBACK (idle_presence_changed_cb), idle);
466
467         priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
468                                                     "org.gnome.SessionManager",
469                                                     "/org/gnome/SessionManager/Presence",
470                                                     "org.gnome.SessionManager.Presence");
471         if (priv->gs_proxy) {
472                 dbus_g_proxy_add_signal (priv->gs_proxy, "StatusChanged",
473                                          G_TYPE_UINT, G_TYPE_INVALID);
474                 dbus_g_proxy_connect_signal (priv->gs_proxy, "StatusChanged",
475                                              G_CALLBACK (idle_session_status_changed_cb),
476                                              idle, NULL);
477         } else {
478                 DEBUG ("Failed to get gs proxy");
479         }
480
481         priv->connectivity = empathy_connectivity_dup_singleton ();
482         priv->state_change_signal_id = g_signal_connect (priv->connectivity,
483             "state-change", G_CALLBACK (idle_state_change_cb), idle);
484 }
485
486 EmpathyIdle *
487 empathy_idle_dup_singleton (void)
488 {
489         return g_object_new (EMPATHY_TYPE_IDLE, NULL);
490 }
491
492 TpConnectionPresenceType
493 empathy_idle_get_state (EmpathyIdle *idle)
494 {
495         EmpathyIdlePriv *priv;
496
497         priv = GET_PRIV (idle);
498
499         return priv->state;
500 }
501
502 void
503 empathy_idle_set_state (EmpathyIdle *idle,
504                         TpConnectionPresenceType   state)
505 {
506         EmpathyIdlePriv *priv;
507
508         priv = GET_PRIV (idle);
509
510         empathy_idle_set_presence (idle, state, priv->status);
511 }
512
513 const gchar *
514 empathy_idle_get_status (EmpathyIdle *idle)
515 {
516         EmpathyIdlePriv *priv;
517
518         priv = GET_PRIV (idle);
519
520         if (!priv->status) {
521                 return empathy_presence_get_default_message (priv->state);
522         }
523
524         return priv->status;
525 }
526
527 void
528 empathy_idle_set_status (EmpathyIdle *idle,
529                          const gchar *status)
530 {
531         EmpathyIdlePriv *priv;
532
533         priv = GET_PRIV (idle);
534
535         empathy_idle_set_presence (idle, priv->state, status);
536 }
537
538 TpConnectionPresenceType
539 empathy_idle_get_flash_state (EmpathyIdle *idle)
540 {
541         EmpathyIdlePriv *priv;
542
543         priv = GET_PRIV (idle);
544
545         return priv->flash_state;
546 }
547
548 void
549 empathy_idle_set_flash_state (EmpathyIdle *idle,
550                               TpConnectionPresenceType   state)
551 {
552         EmpathyIdlePriv *priv;
553
554         priv = GET_PRIV (idle);
555
556         priv->flash_state = state;
557
558         if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
559         }
560
561         g_object_notify (G_OBJECT (idle), "flash-state");
562 }
563
564 static void
565 empathy_idle_do_set_presence (EmpathyIdle *idle,
566                            TpConnectionPresenceType status_type,
567                            const gchar *status_message)
568 {
569         EmpathyIdlePriv *priv = GET_PRIV (idle);
570         const gchar *statuses[NUM_TP_CONNECTION_PRESENCE_TYPES] = {
571                 NULL,
572                 "offline",
573                 "available",
574                 "away",
575                 "xa",
576                 "hidden",
577                 "busy",
578                 NULL,
579                 NULL,
580         };
581         const gchar *status;
582
583         g_assert (status_type > 0 && status_type < NUM_TP_CONNECTION_PRESENCE_TYPES);
584
585         status = statuses[status_type];
586
587         g_return_if_fail (status != NULL);
588
589         empathy_account_manager_request_global_presence (priv->manager,
590                 status_type, status, status_message);
591 }
592
593 void
594 empathy_idle_set_presence (EmpathyIdle *idle,
595                            TpConnectionPresenceType   state,
596                            const gchar *status)
597 {
598         EmpathyIdlePriv *priv;
599         const gchar     *default_status;
600
601         priv = GET_PRIV (idle);
602
603         DEBUG ("Changing presence to %s (%d)", status, state);
604
605         /* Do not set translated default messages */
606         default_status = empathy_presence_get_default_message (state);
607         if (!tp_strdiff (status, default_status)) {
608                 status = NULL;
609         }
610
611         if (state != TP_CONNECTION_PRESENCE_TYPE_OFFLINE &&
612                         !empathy_connectivity_is_online (priv->connectivity)) {
613                 DEBUG ("Empathy is not online");
614
615                 priv->saved_state = state;
616                 if (tp_strdiff (priv->status, status)) {
617                         g_free (priv->saved_status);
618                         priv->saved_status = NULL;
619                         if (!EMP_STR_EMPTY (status)) {
620                                 priv->saved_status = g_strdup (status);
621                         }
622                 }
623                 return;
624         }
625
626         empathy_idle_do_set_presence (idle, state, status);
627 }
628
629 gboolean
630 empathy_idle_get_auto_away (EmpathyIdle *idle)
631 {
632         EmpathyIdlePriv *priv = GET_PRIV (idle);
633
634         return priv->auto_away;
635 }
636
637 void
638 empathy_idle_set_auto_away (EmpathyIdle *idle,
639                             gboolean     auto_away)
640 {
641         EmpathyIdlePriv *priv = GET_PRIV (idle);
642
643         priv->auto_away = auto_away;
644
645         g_object_notify (G_OBJECT (idle), "auto-away");
646 }
647