1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Copyright (C) 2007 Collabora Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authors: Xavier Claessens <xclaesse@gmail.com>
*/
#include <config.h>
#include <glib/gi18n.h>
#include <dbus/dbus-glib.h>
#include <libtelepathy/tp-helpers.h>
#include "empathy-idle.h"
#include "gossip-utils.h"
#include "gossip-debug.h"
#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
EMPATHY_TYPE_IDLE, EmpathyIdlePriv))
#define DEBUG_DOMAIN "Idle"
/* Number of seconds before entering extended autoaway. */
#define EXT_AWAY_TIME (30*60)
enum {
LAST_SIGNAL
};
struct _EmpathyIdlePriv {
MissionControl *mc;
DBusGProxy *gs_proxy;
gboolean is_idle;
McPresence state;
McPresence slack_state;
gchar *status;
McPresence saved_state;
gchar *saved_status;
guint ext_away_timeout;
};
static void empathy_idle_class_init (EmpathyIdleClass *klass);
static void empathy_idle_init (EmpathyIdle *idle);
static void idle_finalize (GObject *object);
static void idle_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec);
static void idle_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec);
static void idle_presence_changed_cb (MissionControl *mc,
McPresence state,
EmpathyIdle *idle);
static void idle_session_idle_changed_cb (DBusGProxy *gs_proxy,
gboolean is_idle,
EmpathyIdle *idle);
static void idle_ext_away_start (EmpathyIdle *idle);
static void idle_ext_away_stop (EmpathyIdle *idle);
static gboolean idle_ext_away_cb (EmpathyIdle *idle);
enum {
PROP_0,
PROP_STATE,
PROP_STATUS,
PROP_SLACK_STATE
};
G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT)
static void
empathy_idle_class_init (EmpathyIdleClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = idle_finalize;
object_class->get_property = idle_get_property;
object_class->set_property = idle_set_property;
g_object_class_install_property (object_class,
PROP_STATE,
g_param_spec_uint ("state",
"state",
"state",
MC_PRESENCE_UNSET,
LAST_MC_PRESENCE,
MC_PRESENCE_AVAILABLE,
G_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_STATUS,
g_param_spec_string ("status",
"status",
"status",
NULL,
G_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_SLACK_STATE,
g_param_spec_uint ("slack-state",
"slack-state",
"slack-state",
MC_PRESENCE_UNSET,
LAST_MC_PRESENCE,
MC_PRESENCE_UNSET,
G_PARAM_READWRITE));
g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
}
static void
empathy_idle_init (EmpathyIdle *idle)
{
EmpathyIdlePriv *priv;
priv = GET_PRIV (idle);
priv->is_idle = FALSE;
priv->mc = gossip_mission_control_new ();
priv->state = mission_control_get_presence_actual (priv->mc, NULL);
priv->status = mission_control_get_presence_message_actual (priv->mc, NULL);
priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
"org.gnome.ScreenSaver",
"/org/gnome/ScreenSaver",
"org.gnome.ScreenSaver");
if (!priv->gs_proxy) {
gossip_debug (DEBUG_DOMAIN, "Failed to get gs proxy");
return;
}
dbus_g_proxy_add_signal (priv->gs_proxy, "SessionIdleChanged",
G_TYPE_BOOLEAN,
G_TYPE_INVALID);
dbus_g_proxy_connect_signal (priv->gs_proxy, "SessionIdleChanged",
G_CALLBACK (idle_session_idle_changed_cb),
idle, NULL);
dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
"PresenceStatusActual",
G_CALLBACK (idle_presence_changed_cb),
idle, NULL);
}
static void
idle_finalize (GObject *object)
{
EmpathyIdlePriv *priv;
priv = GET_PRIV (object);
g_free (priv->status);
g_free (priv->saved_status);
g_object_unref (priv->mc);
if (priv->gs_proxy) {
g_object_unref (priv->gs_proxy);
}
idle_ext_away_stop (EMPATHY_IDLE (object));
}
static void
idle_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec)
{
EmpathyIdlePriv *priv;
EmpathyIdle *idle;
priv = GET_PRIV (object);
idle = EMPATHY_IDLE (object);
switch (param_id) {
case PROP_STATE:
g_value_set_uint (value, empathy_idle_get_state (idle));
break;
case PROP_STATUS:
g_value_set_string (value, empathy_idle_get_status (idle));
break;
case PROP_SLACK_STATE:
g_value_set_uint (value, empathy_idle_get_slack_state (idle));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
};
}
static void
idle_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec)
{
EmpathyIdlePriv *priv;
EmpathyIdle *idle;
priv = GET_PRIV (object);
idle = EMPATHY_IDLE (object);
switch (param_id) {
case PROP_STATE:
empathy_idle_set_state (idle, g_value_get_uint (value));
break;
case PROP_STATUS:
empathy_idle_set_status (idle, g_value_get_string (value));
break;
case PROP_SLACK_STATE:
empathy_idle_set_slack_state (idle, g_value_get_uint (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
};
}
EmpathyIdle *
empathy_idle_new (void)
{
static EmpathyIdle *idle = NULL;
if (!idle) {
idle = g_object_new (EMPATHY_TYPE_IDLE, NULL);
g_object_add_weak_pointer (G_OBJECT (idle), (gpointer) &idle);
} else {
g_object_ref (idle);
}
return idle;
}
McPresence
empathy_idle_get_state (EmpathyIdle *idle)
{
EmpathyIdlePriv *priv;
priv = GET_PRIV (idle);
return priv->state;
}
void
empathy_idle_set_state (EmpathyIdle *idle,
McPresence state)
{
EmpathyIdlePriv *priv;
priv = GET_PRIV (idle);
mission_control_set_presence (priv->mc,
state,
priv->status,
NULL, NULL);
}
const gchar *
empathy_idle_get_status (EmpathyIdle *idle)
{
EmpathyIdlePriv *priv;
priv = GET_PRIV (idle);
return priv->status;
}
void
empathy_idle_set_status (EmpathyIdle *idle,
const gchar *status)
{
EmpathyIdlePriv *priv;
priv = GET_PRIV (idle);
mission_control_set_presence (priv->mc,
priv->state,
status,
NULL, NULL);
}
McPresence
empathy_idle_get_slack_state (EmpathyIdle *idle)
{
EmpathyIdlePriv *priv;
priv = GET_PRIV (idle);
return priv->slack_state;
}
void
empathy_idle_set_slack_state (EmpathyIdle *idle,
McPresence state)
{
EmpathyIdlePriv *priv;
priv = GET_PRIV (idle);
priv->slack_state = state;
g_object_notify (G_OBJECT (idle), "slack-state");
}
static void
idle_presence_changed_cb (MissionControl *mc,
McPresence state,
EmpathyIdle *idle)
{
EmpathyIdlePriv *priv;
priv = GET_PRIV (idle);
g_free (priv->status);
priv->state = state;
priv->status = mission_control_get_presence_message_actual (priv->mc, NULL);
if (G_STR_EMPTY (priv->status)) {
g_free (priv->status);
priv->status = g_strdup (gossip_presence_state_get_default_status (state));
}
g_object_notify (G_OBJECT (idle), "state");
g_object_notify (G_OBJECT (idle), "status");
}
static void
idle_session_idle_changed_cb (DBusGProxy *gs_proxy,
gboolean is_idle,
EmpathyIdle *idle)
{
EmpathyIdlePriv *priv;
priv = GET_PRIV (idle);
gossip_debug (DEBUG_DOMAIN, "Session idle state changed, %s -> %s",
priv->is_idle ? "yes" : "no",
is_idle ? "yes" : "no");
if (is_idle && !priv->is_idle) {
McPresence new_state;
/* We are now idle, set state to away */
if (priv->state <= MC_PRESENCE_OFFLINE ||
priv->state == MC_PRESENCE_HIDDEN) {
/* We are not online so nothing to do here */
return;
} else if (priv->state == MC_PRESENCE_AWAY ||
priv->state == MC_PRESENCE_EXTENDED_AWAY) {
/* User set away manually, when coming back we restore
* default presence. */
new_state = priv->state;
priv->saved_state = MC_PRESENCE_AVAILABLE;
priv->saved_status = NULL;
} else {
new_state = MC_PRESENCE_AWAY;
priv->saved_state = priv->state;
priv->saved_status = g_strdup (priv->status);
}
gossip_debug (DEBUG_DOMAIN, "Going to autoaway");
empathy_idle_set_state (idle, new_state);
idle_ext_away_start (idle);
} else if (!is_idle && priv->is_idle) {
/* We are no more idle, restore state */
idle_ext_away_stop (idle);
gossip_debug (DEBUG_DOMAIN, "Restoring state to %d %s",
priv->saved_state,
priv->saved_status);
mission_control_set_presence (priv->mc,
priv->saved_state,
priv->saved_status,
NULL, NULL);
g_free (priv->saved_status);
priv->saved_status = NULL;
}
priv->is_idle = is_idle;
}
static void
idle_ext_away_start (EmpathyIdle *idle)
{
EmpathyIdlePriv *priv;
priv = GET_PRIV (idle);
idle_ext_away_stop (idle);
priv->ext_away_timeout = g_timeout_add (EXT_AWAY_TIME * 1000,
(GSourceFunc) idle_ext_away_cb,
idle);
}
static void
idle_ext_away_stop (EmpathyIdle *idle)
{
EmpathyIdlePriv *priv;
priv = GET_PRIV (idle);
if (priv->ext_away_timeout) {
g_source_remove (priv->ext_away_timeout);
priv->ext_away_timeout = 0;
}
}
static gboolean
idle_ext_away_cb (EmpathyIdle *idle)
{
EmpathyIdlePriv *priv;
priv = GET_PRIV (idle);
gossip_debug (DEBUG_DOMAIN, "Going to extended autoaway");
mission_control_set_presence (priv->mc,
MC_PRESENCE_EXTENDED_AWAY,
priv->saved_status,
NULL, NULL);
priv->ext_away_timeout = 0;
return FALSE;
}
|