Mercurial > pidgin
annotate src/idle.c @ 12729:d3232d64fafd
[gaim-migrate @ 15073]
This is a better order
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Thu, 05 Jan 2006 04:32:25 +0000 |
parents | 200f22ca4890 |
children | bd80fb1e8406 |
rev | line source |
---|---|
12272 | 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 * If our accounts are marked as idle and have_set_idle is FALSE and | |
51 * the user moves the mouse, then we will NOT unidle our accounts. | |
52 */ | |
53 static gboolean have_set_idle = FALSE; | |
54 | |
55 static guint idle_timer = 0; | |
56 | |
57 static time_t last_active_time = 0; | |
58 | |
59 static void | |
60 set_account_autoaway(GaimConnection *gc) | |
61 { | |
62 GaimAccount *account; | |
63 GaimPresence *presence; | |
64 GaimStatus *status; | |
65 | |
66 if (gc->is_auto_away) | |
67 /* This account is already auto-away! */ | |
68 return; | |
69 | |
70 account = gaim_connection_get_account(gc); | |
71 presence = gaim_account_get_presence(account); | |
72 status = gaim_presence_get_active_status(presence); | |
73 | |
74 if (gaim_status_is_available(status)) | |
75 { | |
76 GaimSavedStatus *saved_status; | |
77 | |
78 gaim_debug_info("idle", "Making %s auto-away\n", | |
79 gaim_account_get_username(account)); | |
80 | |
81 saved_status = gaim_savedstatus_get_idleaway(); | |
82 gaim_savedstatus_activate_for_account(saved_status, account); | |
83 | |
84 gc->is_auto_away = GAIM_IDLE_AUTO_AWAY; | |
85 } else { | |
86 gc->is_auto_away = GAIM_IDLE_AWAY_BUT_NOT_AUTO_AWAY; | |
87 } | |
88 } | |
89 | |
90 static void | |
91 unset_account_autoaway(GaimConnection *gc) | |
92 { | |
93 GaimAccount *account; | |
94 GaimPresence *presence; | |
95 GaimStatus *status; | |
96 GaimSavedStatus *saved_status; | |
97 | |
98 account = gaim_connection_get_account(gc); | |
99 presence = gaim_account_get_presence(account); | |
100 status = gaim_presence_get_active_status(presence); | |
101 | |
102 if (!gc->is_auto_away) | |
103 /* This account is already not auto-away! */ | |
104 return; | |
105 | |
106 if (gc->is_auto_away == GAIM_IDLE_AWAY_BUT_NOT_AUTO_AWAY) { | |
107 gc->is_auto_away = GAIM_IDLE_NOT_AWAY; | |
108 } else { | |
109 gc->is_auto_away = GAIM_IDLE_NOT_AWAY; | |
110 | |
111 gaim_debug_info("idle", "%s returning from auto-away\n", | |
112 gaim_account_get_username(account)); | |
113 | |
114 /* Return our account to its previous status */ | |
115 saved_status = gaim_savedstatus_get_current(); | |
116 gaim_savedstatus_activate_for_account(saved_status, account); | |
117 } | |
118 } | |
119 | |
120 static void | |
121 set_account_idle(GaimConnection *gc, int time_idle) | |
122 { | |
123 GaimAccount *account; | |
124 GaimPresence *presence; | |
125 | |
126 account = gaim_connection_get_account(gc); | |
127 presence = gaim_account_get_presence(account); | |
128 | |
129 if (gaim_presence_is_idle(presence)) | |
130 /* This account is already idle! */ | |
131 return; | |
132 | |
133 gaim_debug_info("idle", "Setting %s idle %d seconds\n", | |
134 gaim_account_get_username(account), time_idle); | |
135 gaim_presence_set_idle(presence, TRUE, time(NULL) - time_idle); | |
136 } | |
137 | |
138 static void | |
139 set_account_unidle(GaimConnection *gc) | |
140 { | |
141 GaimAccount *account; | |
142 GaimPresence *presence; | |
143 | |
144 account = gaim_connection_get_account(gc); | |
145 presence = gaim_account_get_presence(account); | |
146 | |
147 if (!gaim_presence_is_idle(presence)) | |
148 /* This account is already unidle! */ | |
149 return; | |
150 | |
151 gaim_debug_info("idle", "Setting %s unidle\n", | |
152 gaim_account_get_username(account)); | |
153 gaim_presence_set_idle(presence, FALSE, time(NULL)); | |
154 } | |
155 | |
156 /* | |
157 * This function should be called when you think your idle state | |
158 * may have changed. Maybe you're over the 10-minute mark and | |
159 * Gaim should start reporting idle time to the server. Maybe | |
160 * you've returned from being idle. Maybe your auto-away message | |
161 * should be set. | |
162 * | |
163 * There is no harm to calling this many many times, other than | |
164 * it will be kinda slow. This is called every 5 seconds by a | |
165 * timer set when Gaim starts. It is also called when | |
166 * you send an IM, a chat, etc. | |
167 * | |
168 * This function has 3 sections. | |
169 * 1. Get your idle time. It will query XScreenSaver or Windows | |
170 * or use the Gaim idle time. Whatever. | |
171 * 2. Set or unset your auto-away message. | |
172 * 3. Report your current idle time to the IM server. | |
173 */ | |
174 static gint | |
175 check_idleness() | |
176 { | |
177 time_t time_idle; | |
178 gboolean auto_away; | |
12573 | 179 const gchar *idle_reporting; |
12272 | 180 gboolean report_idle; |
181 GList *l; | |
182 | |
183 gaim_signal_emit(gaim_blist_get_handle(), "update-idle"); | |
184 | |
12573 | 185 idle_reporting = gaim_prefs_get_string("/core/away/idle_reporting"); |
186 report_idle = TRUE; | |
187 if (!strcmp(idle_reporting, "system") && | |
188 (idle_ui_ops != NULL) && (idle_ui_ops->get_time_idle != NULL)) | |
189 { | |
190 /* Use system idle time (mouse or keyboard movement, etc.) */ | |
12272 | 191 time_idle = idle_ui_ops->get_time_idle(); |
12573 | 192 } |
193 else if (!strcmp(idle_reporting, "gaim")) | |
194 { | |
12272 | 195 /* Use 'Gaim idle' */ |
196 time_idle = time(NULL) - last_active_time; | |
12573 | 197 } |
198 else | |
199 { | |
200 /* Don't report idle time */ | |
201 time_idle = 0; | |
202 report_idle = FALSE; | |
203 } | |
12272 | 204 |
205 /* Auto-away stuff */ | |
206 auto_away = gaim_prefs_get_bool("/core/away/away_when_idle"); | |
207 if (auto_away && | |
208 (time_idle > (60 * gaim_prefs_get_int("/core/away/mins_before_away")))) | |
209 { | |
210 for (l = gaim_connections_get_all(); l != NULL; l = l->next) | |
211 set_account_autoaway(l->data); | |
212 } | |
213 else if (time_idle < 60 * gaim_prefs_get_int("/core/away/mins_before_away")) | |
214 { | |
215 for (l = gaim_connections_get_all(); l != NULL; l = l->next) | |
216 unset_account_autoaway(l->data); | |
217 } | |
218 | |
219 /* Idle reporting stuff */ | |
220 if (report_idle && (time_idle >= IDLEMARK) && !have_set_idle) | |
221 { | |
222 for (l = gaim_connections_get_all(); l != NULL; l = l->next) | |
223 set_account_idle(l->data, time_idle); | |
224 have_set_idle = TRUE; | |
225 } | |
226 else if ((!report_idle || time_idle < IDLEMARK) && have_set_idle) | |
227 { | |
228 for (l = gaim_connections_get_all(); l != NULL; l = l->next) | |
229 set_account_unidle(l->data); | |
230 have_set_idle = FALSE; | |
231 } | |
232 | |
233 return TRUE; | |
234 } | |
235 | |
236 static void | |
237 im_msg_sent_cb(GaimAccount *account, const char *receiver, | |
238 const char *message, void *data) | |
239 { | |
240 /* Check our idle time after an IM is sent */ | |
241 check_idleness(); | |
242 } | |
243 | |
244 void | |
245 gaim_idle_touch() | |
246 { | |
247 time(&last_active_time); | |
248 } | |
249 | |
250 void | |
251 gaim_idle_set(time_t time) | |
252 { | |
253 last_active_time = time; | |
254 } | |
255 | |
256 void | |
257 gaim_idle_set_ui_ops(GaimIdleUiOps *ops) | |
258 { | |
259 idle_ui_ops = ops; | |
260 } | |
261 | |
262 GaimIdleUiOps * | |
263 gaim_idle_get_ui_ops(void) | |
264 { | |
265 return idle_ui_ops; | |
266 } | |
267 | |
12412
a88ca6da0b38
[gaim-migrate @ 14719]
Richard Laager <rlaager@wiktel.com>
parents:
12272
diff
changeset
|
268 static void * |
12272 | 269 gaim_idle_get_handle() |
270 { | |
271 static int handle; | |
272 | |
273 return &handle; | |
274 } | |
275 | |
276 void | |
277 gaim_idle_init() | |
278 { | |
279 /* Add the timer to check if we're idle */ | |
280 idle_timer = gaim_timeout_add(IDLE_CHECK_INTERVAL * 1000, check_idleness, NULL); | |
281 | |
282 gaim_signal_connect(gaim_conversations_get_handle(), "sent-im-msg", | |
283 gaim_idle_get_handle(), | |
284 GAIM_CALLBACK(im_msg_sent_cb), NULL); | |
285 | |
286 gaim_idle_touch(); | |
287 } | |
288 | |
289 void | |
290 gaim_idle_uninit() | |
291 { | |
292 gaim_signals_disconnect_by_handle(gaim_idle_get_handle()); | |
293 | |
294 /* Remove the idle timer */ | |
295 if (idle_timer > 0) | |
296 gaim_timeout_remove(idle_timer); | |
297 idle_timer = 0; | |
298 } |