14192
|
1 /*
|
|
2 * gaim
|
|
3 *
|
|
4 * Gaim is the legal property of its developers, whose names are too numerous
|
|
5 * to list here. Please refer to the COPYRIGHT file distributed with this
|
|
6 * source distribution.
|
|
7 *
|
|
8 * This program is free software; you can redistribute it and/or modify
|
|
9 * it under the terms of the GNU General Public License as published by
|
|
10 * the Free Software Foundation; either version 2 of the License, or
|
|
11 * (at your option) any later version.
|
|
12 *
|
|
13 * This program is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 * GNU General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU General Public License
|
|
19 * along with this program; if not, write to the Free Software
|
|
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
21 *
|
|
22 */
|
|
23 #include "internal.h"
|
|
24
|
|
25 #include "connection.h"
|
|
26 #include "debug.h"
|
|
27 #include "idle.h"
|
|
28 #include "log.h"
|
|
29 #include "prefs.h"
|
|
30 #include "savedstatuses.h"
|
|
31 #include "signals.h"
|
|
32
|
|
33 #define IDLEMARK 600 /* 10 minutes! */
|
|
34 #define IDLE_CHECK_INTERVAL 5 /* 5 seconds */
|
|
35
|
|
36 typedef enum
|
|
37 {
|
|
38 GAIM_IDLE_NOT_AWAY = 0,
|
|
39 GAIM_IDLE_AUTO_AWAY,
|
|
40 GAIM_IDLE_AWAY_BUT_NOT_AUTO_AWAY
|
|
41
|
|
42 } GaimAutoAwayState;
|
|
43
|
|
44 static GaimIdleUiOps *idle_ui_ops = NULL;
|
|
45
|
|
46 /**
|
|
47 * This is needed for the I'dle Mak'er plugin to work correctly. We
|
|
48 * use it to determine if we're the ones who set our accounts idle
|
|
49 * or if someone else did it (the I'dle Mak'er plugin, for example).
|
|
50 * Basically we just keep track of which accounts were set idle by us,
|
|
51 * and then we'll only set these specific accounts unidle when the
|
|
52 * user returns.
|
|
53 */
|
|
54 static GList *idled_accts = NULL;
|
|
55
|
|
56 static guint idle_timer = 0;
|
|
57
|
|
58 static time_t last_active_time = 0;
|
|
59
|
|
60 static void
|
|
61 set_account_idle(GaimAccount *account, int time_idle)
|
|
62 {
|
|
63 GaimPresence *presence;
|
|
64
|
|
65 presence = gaim_account_get_presence(account);
|
|
66
|
|
67 if (gaim_presence_is_idle(presence))
|
|
68 /* This account is already idle! */
|
|
69 return;
|
|
70
|
|
71 gaim_debug_info("idle", "Setting %s idle %d seconds\n",
|
|
72 gaim_account_get_username(account), time_idle);
|
|
73 gaim_presence_set_idle(presence, TRUE, time(NULL) - time_idle);
|
|
74 idled_accts = g_list_prepend(idled_accts, account);
|
|
75 }
|
|
76
|
|
77 static void
|
|
78 set_account_unidle(GaimAccount *account)
|
|
79 {
|
|
80 GaimPresence *presence;
|
|
81
|
|
82 presence = gaim_account_get_presence(account);
|
|
83
|
|
84 idled_accts = g_list_remove(idled_accts, account);
|
|
85
|
|
86 if (!gaim_presence_is_idle(presence))
|
|
87 /* This account is already unidle! */
|
|
88 return;
|
|
89
|
|
90 gaim_debug_info("idle", "Setting %s unidle\n",
|
|
91 gaim_account_get_username(account));
|
|
92 gaim_presence_set_idle(presence, FALSE, 0);
|
|
93 }
|
|
94
|
|
95 /*
|
|
96 * This function should be called when you think your idle state
|
|
97 * may have changed. Maybe you're over the 10-minute mark and
|
|
98 * Gaim should start reporting idle time to the server. Maybe
|
|
99 * you've returned from being idle. Maybe your auto-away message
|
|
100 * should be set.
|
|
101 *
|
|
102 * There is no harm to calling this many many times, other than
|
|
103 * it will be kinda slow. This is called every 5 seconds by a
|
|
104 * timer set when Gaim starts. It is also called when
|
|
105 * you send an IM, a chat, etc.
|
|
106 *
|
|
107 * This function has 3 sections.
|
|
108 * 1. Get your idle time. It will query XScreenSaver or Windows
|
|
109 * or use the Gaim idle time. Whatever.
|
|
110 * 2. Set or unset your auto-away message.
|
|
111 * 3. Report your current idle time to the IM server.
|
|
112 */
|
|
113 static gint
|
|
114 check_idleness()
|
|
115 {
|
|
116 time_t time_idle;
|
|
117 gboolean auto_away;
|
|
118 const gchar *idle_reporting;
|
|
119 gboolean report_idle;
|
|
120 GList *l;
|
|
121
|
|
122 gaim_signal_emit(gaim_blist_get_handle(), "update-idle");
|
|
123
|
|
124 idle_reporting = gaim_prefs_get_string("/core/away/idle_reporting");
|
|
125 report_idle = TRUE;
|
|
126 if (!strcmp(idle_reporting, "system") &&
|
|
127 (idle_ui_ops != NULL) && (idle_ui_ops->get_time_idle != NULL))
|
|
128 {
|
|
129 /* Use system idle time (mouse or keyboard movement, etc.) */
|
|
130 time_idle = idle_ui_ops->get_time_idle();
|
|
131 }
|
|
132 else if (!strcmp(idle_reporting, "gaim"))
|
|
133 {
|
|
134 /* Use 'Gaim idle' */
|
|
135 time_idle = time(NULL) - last_active_time;
|
|
136 }
|
|
137 else
|
|
138 {
|
|
139 /* Don't report idle time */
|
|
140 time_idle = 0;
|
|
141 report_idle = FALSE;
|
|
142 }
|
|
143
|
|
144 /* Auto-away stuff */
|
|
145 auto_away = gaim_prefs_get_bool("/core/away/away_when_idle");
|
|
146 if (auto_away &&
|
|
147 (time_idle > (60 * gaim_prefs_get_int("/core/away/mins_before_away"))))
|
|
148 {
|
|
149 gaim_savedstatus_set_idleaway(TRUE);
|
|
150 }
|
|
151 else if (time_idle < 60 * gaim_prefs_get_int("/core/away/mins_before_away"))
|
|
152 {
|
|
153 gaim_savedstatus_set_idleaway(FALSE);
|
|
154 }
|
|
155
|
|
156 /* Idle reporting stuff */
|
|
157 if (report_idle && (time_idle >= IDLEMARK))
|
|
158 {
|
|
159 for (l = gaim_connections_get_all(); l != NULL; l = l->next)
|
|
160 {
|
|
161 GaimConnection *gc = l->data;
|
|
162 set_account_idle(gaim_connection_get_account(gc), time_idle);
|
|
163 }
|
|
164 }
|
|
165 else if (!report_idle || (time_idle < IDLEMARK))
|
|
166 {
|
|
167 while (idled_accts != NULL)
|
|
168 set_account_unidle(idled_accts->data);
|
|
169 }
|
|
170
|
|
171 return TRUE;
|
|
172 }
|
|
173
|
|
174 static void
|
|
175 im_msg_sent_cb(GaimAccount *account, const char *receiver,
|
|
176 const char *message, void *data)
|
|
177 {
|
|
178 /* Check our idle time after an IM is sent */
|
|
179 check_idleness();
|
|
180 }
|
|
181
|
|
182 static void
|
|
183 signing_on_cb(GaimConnection *gc, void *data)
|
|
184 {
|
|
185 /* When signing on a new account, check if the account should be idle */
|
|
186 check_idleness();
|
|
187 }
|
|
188
|
|
189 static void
|
|
190 signing_off_cb(GaimConnection *gc, void *data)
|
|
191 {
|
|
192 GaimAccount *account;
|
|
193
|
|
194 account = gaim_connection_get_account(gc);
|
|
195 set_account_unidle(account);
|
|
196 }
|
|
197
|
|
198 void
|
|
199 gaim_idle_touch()
|
|
200 {
|
|
201 time(&last_active_time);
|
|
202 }
|
|
203
|
|
204 void
|
|
205 gaim_idle_set(time_t time)
|
|
206 {
|
|
207 last_active_time = time;
|
|
208 }
|
|
209
|
|
210 void
|
|
211 gaim_idle_set_ui_ops(GaimIdleUiOps *ops)
|
|
212 {
|
|
213 idle_ui_ops = ops;
|
|
214 }
|
|
215
|
|
216 GaimIdleUiOps *
|
|
217 gaim_idle_get_ui_ops(void)
|
|
218 {
|
|
219 return idle_ui_ops;
|
|
220 }
|
|
221
|
|
222 static void *
|
|
223 gaim_idle_get_handle()
|
|
224 {
|
|
225 static int handle;
|
|
226
|
|
227 return &handle;
|
|
228 }
|
|
229
|
|
230 void
|
|
231 gaim_idle_init()
|
|
232 {
|
|
233 /* Add the timer to check if we're idle */
|
|
234 idle_timer = gaim_timeout_add(IDLE_CHECK_INTERVAL * 1000, check_idleness, NULL);
|
|
235
|
|
236 gaim_signal_connect(gaim_conversations_get_handle(), "sent-im-msg",
|
|
237 gaim_idle_get_handle(),
|
|
238 GAIM_CALLBACK(im_msg_sent_cb), NULL);
|
|
239 gaim_signal_connect(gaim_connections_get_handle(), "signing-on",
|
|
240 gaim_idle_get_handle(),
|
|
241 GAIM_CALLBACK(signing_on_cb), NULL);
|
|
242 gaim_signal_connect(gaim_connections_get_handle(), "signing-off",
|
|
243 gaim_idle_get_handle(),
|
|
244 GAIM_CALLBACK(signing_off_cb), NULL);
|
|
245
|
|
246 gaim_idle_touch();
|
|
247 }
|
|
248
|
|
249 void
|
|
250 gaim_idle_uninit()
|
|
251 {
|
|
252 gaim_signals_disconnect_by_handle(gaim_idle_get_handle());
|
|
253
|
|
254 /* Remove the idle timer */
|
|
255 if (idle_timer > 0)
|
|
256 gaim_timeout_remove(idle_timer);
|
|
257 idle_timer = 0;
|
|
258 }
|