]> git.0d.be Git - empathy.git/blob - libempathy/empathy-idle.c
Merge back from master
[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                 DEBUG ("Restoring state to %d, reset status to %s",
219                         priv->away_saved_state, new_status);
220
221                 empathy_idle_set_presence (idle,
222                                            priv->away_saved_state,
223                                            new_status);
224
225                 priv->away_saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
226         }
227
228         priv->is_idle = is_idle;
229 }
230
231 static void
232 idle_state_change_cb (EmpathyConnectivity *connectivity,
233                       gboolean new_online,
234                       EmpathyIdle *idle)
235 {
236         EmpathyIdlePriv *priv;
237
238         priv = GET_PRIV (idle);
239
240         if (!new_online) {
241                 /* We are no longer connected */
242                 DEBUG ("Disconnected: Save state %d (%s)",
243                                 priv->state, priv->status);
244                 priv->saved_state = priv->state;
245                 g_free (priv->saved_status);
246                 priv->saved_status = g_strdup (priv->status);
247                 empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
248         }
249         else if (new_online
250                         && priv->saved_state != TP_CONNECTION_PRESENCE_TYPE_UNSET) {
251                 /* We are now connected */
252                 DEBUG ("Reconnected: Restore state %d (%s)",
253                                 priv->saved_state, priv->saved_status);
254                 empathy_idle_set_presence (idle,
255                                 priv->saved_state,
256                                 priv->saved_status);
257                 priv->saved_state = TP_CONNECTION_PRESENCE_TYPE_UNSET;
258                 g_free (priv->saved_status);
259                 priv->saved_status = NULL;
260         }
261 }
262
263 static void
264 idle_finalize (GObject *object)
265 {
266         EmpathyIdlePriv *priv;
267
268         priv = GET_PRIV (object);
269
270         g_free (priv->status);
271
272         if (priv->gs_proxy) {
273                 g_object_unref (priv->gs_proxy);
274         }
275
276         g_signal_handler_disconnect (priv->connectivity,
277                                      priv->state_change_signal_id);
278         priv->state_change_signal_id = 0;
279
280         g_object_unref (priv->connectivity);
281
282         idle_ext_away_stop (EMPATHY_IDLE (object));
283 }
284
285 static GObject *
286 idle_constructor (GType type,
287                   guint n_props,
288                   GObjectConstructParam *props)
289 {
290         GObject *retval;
291
292         if (idle_singleton) {
293                 retval = g_object_ref (idle_singleton);
294         } else {
295                 retval = G_OBJECT_CLASS (empathy_idle_parent_class)->constructor
296                         (type, n_props, props);
297
298                 idle_singleton = EMPATHY_IDLE (retval);
299                 g_object_add_weak_pointer (retval, (gpointer) &idle_singleton);
300         }
301
302         return retval;
303 }
304
305 static void
306 idle_get_property (GObject    *object,
307                    guint       param_id,
308                    GValue     *value,
309                    GParamSpec *pspec)
310 {
311         EmpathyIdlePriv *priv;
312         EmpathyIdle     *idle;
313
314         priv = GET_PRIV (object);
315         idle = EMPATHY_IDLE (object);
316
317         switch (param_id) {
318         case PROP_STATE:
319                 g_value_set_enum (value, empathy_idle_get_state (idle));
320                 break;
321         case PROP_STATUS:
322                 g_value_set_string (value, empathy_idle_get_status (idle));
323                 break;
324         case PROP_FLASH_STATE:
325                 g_value_set_enum (value, empathy_idle_get_flash_state (idle));
326                 break;
327         case PROP_AUTO_AWAY:
328                 g_value_set_boolean (value, empathy_idle_get_auto_away (idle));
329                 break;
330         default:
331                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
332                 break;
333         };
334 }
335
336 static void
337 idle_set_property (GObject      *object,
338                    guint         param_id,
339                    const GValue *value,
340                    GParamSpec   *pspec)
341 {
342         EmpathyIdlePriv *priv;
343         EmpathyIdle     *idle;
344
345         priv = GET_PRIV (object);
346         idle = EMPATHY_IDLE (object);
347
348         switch (param_id) {
349         case PROP_STATE:
350                 empathy_idle_set_state (idle, g_value_get_enum (value));
351                 break;
352         case PROP_STATUS:
353                 empathy_idle_set_status (idle, g_value_get_string (value));
354                 break;
355         case PROP_FLASH_STATE:
356                 empathy_idle_set_flash_state (idle, g_value_get_enum (value));
357                 break;
358         case PROP_AUTO_AWAY:
359                 empathy_idle_set_auto_away (idle, g_value_get_boolean (value));
360                 break;
361         default:
362                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
363                 break;
364         };
365 }
366
367 static void
368 empathy_idle_class_init (EmpathyIdleClass *klass)
369 {
370         GObjectClass *object_class = G_OBJECT_CLASS (klass);
371
372         object_class->finalize = idle_finalize;
373         object_class->constructor = idle_constructor;
374         object_class->get_property = idle_get_property;
375         object_class->set_property = idle_set_property;
376
377         g_object_class_install_property (object_class,
378                                          PROP_STATE,
379                                          g_param_spec_uint ("state",
380                                                             "state",
381                                                             "state",
382                                                             0, NUM_TP_CONNECTION_PRESENCE_TYPES,
383                                                             TP_CONNECTION_PRESENCE_TYPE_UNSET,
384                                                             G_PARAM_READWRITE));
385         g_object_class_install_property (object_class,
386                                          PROP_STATUS,
387                                          g_param_spec_string ("status",
388                                                               "status",
389                                                               "status",
390                                                               NULL,
391                                                               G_PARAM_READWRITE));
392         g_object_class_install_property (object_class,
393                                          PROP_FLASH_STATE,
394                                          g_param_spec_uint ("flash-state",
395                                                             "flash-state",
396                                                             "flash-state",
397                                                             0, NUM_TP_CONNECTION_PRESENCE_TYPES,
398                                                             TP_CONNECTION_PRESENCE_TYPE_UNSET,
399                                                             G_PARAM_READWRITE));
400
401          g_object_class_install_property (object_class,
402                                           PROP_AUTO_AWAY,
403                                           g_param_spec_boolean ("auto-away",
404                                                                 "Automatic set presence to away",
405                                                                 "Should it set presence to away if inactive",
406                                                                 FALSE,
407                                                                 G_PARAM_READWRITE));
408
409         g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
410 }
411
412 static void
413 empathy_idle_init (EmpathyIdle *idle)
414 {
415         EmpathyIdlePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (idle,
416                 EMPATHY_TYPE_IDLE, EmpathyIdlePriv);
417
418         idle->priv = priv;
419         priv->is_idle = FALSE;
420
421         priv->manager = empathy_account_manager_dup_singleton ();
422         priv->state = empathy_account_manager_get_global_presence (priv->manager,
423                 NULL, &priv->status);
424
425
426         g_signal_connect (priv->manager, "global-presence-changed",
427                 G_CALLBACK (idle_presence_changed_cb), idle);
428
429         priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
430                                                     "org.gnome.SessionManager",
431                                                     "/org/gnome/SessionManager/Presence",
432                                                     "org.gnome.SessionManager.Presence");
433         if (priv->gs_proxy) {
434                 dbus_g_proxy_add_signal (priv->gs_proxy, "StatusChanged",
435                                          G_TYPE_UINT, G_TYPE_INVALID);
436                 dbus_g_proxy_connect_signal (priv->gs_proxy, "StatusChanged",
437                                              G_CALLBACK (idle_session_status_changed_cb),
438                                              idle, NULL);
439         } else {
440                 DEBUG ("Failed to get gs proxy");
441         }
442
443         priv->connectivity = empathy_connectivity_dup_singleton ();
444         priv->state_change_signal_id = g_signal_connect (priv->connectivity,
445             "state-change", G_CALLBACK (idle_state_change_cb), idle);
446 }
447
448 EmpathyIdle *
449 empathy_idle_dup_singleton (void)
450 {
451         return g_object_new (EMPATHY_TYPE_IDLE, NULL);
452 }
453
454 TpConnectionPresenceType
455 empathy_idle_get_state (EmpathyIdle *idle)
456 {
457         EmpathyIdlePriv *priv;
458
459         priv = GET_PRIV (idle);
460
461         return priv->state;
462 }
463
464 void
465 empathy_idle_set_state (EmpathyIdle *idle,
466                         TpConnectionPresenceType   state)
467 {
468         EmpathyIdlePriv *priv;
469
470         priv = GET_PRIV (idle);
471
472         empathy_idle_set_presence (idle, state, priv->status);
473 }
474
475 const gchar *
476 empathy_idle_get_status (EmpathyIdle *idle)
477 {
478         EmpathyIdlePriv *priv;
479
480         priv = GET_PRIV (idle);
481
482         if (!priv->status) {
483                 return empathy_presence_get_default_message (priv->state);
484         }
485
486         return priv->status;
487 }
488
489 void
490 empathy_idle_set_status (EmpathyIdle *idle,
491                          const gchar *status)
492 {
493         EmpathyIdlePriv *priv;
494
495         priv = GET_PRIV (idle);
496
497         empathy_idle_set_presence (idle, priv->state, status);
498 }
499
500 TpConnectionPresenceType
501 empathy_idle_get_flash_state (EmpathyIdle *idle)
502 {
503         EmpathyIdlePriv *priv;
504
505         priv = GET_PRIV (idle);
506
507         return priv->flash_state;
508 }
509
510 void
511 empathy_idle_set_flash_state (EmpathyIdle *idle,
512                               TpConnectionPresenceType   state)
513 {
514         EmpathyIdlePriv *priv;
515
516         priv = GET_PRIV (idle);
517
518         priv->flash_state = state;
519
520         if (state == TP_CONNECTION_PRESENCE_TYPE_UNSET) {
521         }
522
523         g_object_notify (G_OBJECT (idle), "flash-state");
524 }
525
526 static void
527 empathy_idle_do_set_presence (EmpathyIdle *idle,
528                            TpConnectionPresenceType status_type,
529                            const gchar *status_message)
530 {
531         EmpathyIdlePriv *priv = GET_PRIV (idle);
532         const gchar *statuses[NUM_TP_CONNECTION_PRESENCE_TYPES] = {
533                 NULL,
534                 "offline",
535                 "available",
536                 "away",
537                 "xa",
538                 "hidden",
539                 "busy",
540                 NULL,
541                 NULL,
542         };
543         const gchar *status;
544
545         g_assert (status_type > 0 && status_type < NUM_TP_CONNECTION_PRESENCE_TYPES);
546
547         status = statuses[status_type];
548
549         g_return_if_fail (status != NULL);
550
551         empathy_account_manager_request_global_presence (priv->manager,
552                 status_type, status, status_message);
553 }
554
555 void
556 empathy_idle_set_presence (EmpathyIdle *idle,
557                            TpConnectionPresenceType   state,
558                            const gchar *status)
559 {
560         EmpathyIdlePriv *priv;
561         const gchar     *default_status;
562
563         priv = GET_PRIV (idle);
564
565         DEBUG ("Changing presence to %s (%d)", status, state);
566
567         /* Do not set translated default messages */
568         default_status = empathy_presence_get_default_message (state);
569         if (!tp_strdiff (status, default_status)) {
570                 status = NULL;
571         }
572
573         if (!empathy_connectivity_is_online (priv->connectivity)) {
574                 DEBUG ("Empathy is not online");
575
576                 if (tp_strdiff (priv->status, status)) {
577                         g_free (priv->status);
578                         priv->status = NULL;
579                         if (!EMP_STR_EMPTY (status)) {
580                                 priv->status = g_strdup (status);
581                         }
582                         g_object_notify (G_OBJECT (idle), "status");
583                 }
584         }
585
586         empathy_idle_do_set_presence (idle, state, status);
587 }
588
589 gboolean
590 empathy_idle_get_auto_away (EmpathyIdle *idle)
591 {
592         EmpathyIdlePriv *priv = GET_PRIV (idle);
593
594         return priv->auto_away;
595 }
596
597 void
598 empathy_idle_set_auto_away (EmpathyIdle *idle,
599                             gboolean     auto_away)
600 {
601         EmpathyIdlePriv *priv = GET_PRIV (idle);
602
603         priv->auto_away = auto_away;
604
605         g_object_notify (G_OBJECT (idle), "auto-away");
606 }
607