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