Mercurial > pidgin.yaz
annotate libgaim/idle.c @ 15147:4035dfc09faa
[gaim-migrate @ 17932]
API Changelog for [17929]
committer: Tailor Script <tailor@pidgin.im>
author | Evan Schoenberg <evan.s@dreskin.net> |
---|---|
date | Sun, 10 Dec 2006 02:58:30 +0000 |
parents | 762896fc8449 |
children | a6aad36ca735 |
rev | line source |
---|---|
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"); | |
14937
762896fc8449
[gaim-migrate @ 17709]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
146 |
762896fc8449
[gaim-migrate @ 17709]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
147 /* If we're not reporting idle, we can still do auto-away. |
762896fc8449
[gaim-migrate @ 17709]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
148 * First try "system" and if that isn't possible, use "gaim" */ |
762896fc8449
[gaim-migrate @ 17709]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
149 if (!report_idle && auto_away) { |
762896fc8449
[gaim-migrate @ 17709]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
150 if ((idle_ui_ops != NULL) && (idle_ui_ops->get_time_idle != NULL)) |
762896fc8449
[gaim-migrate @ 17709]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
151 time_idle = idle_ui_ops->get_time_idle(); |
762896fc8449
[gaim-migrate @ 17709]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
152 else |
762896fc8449
[gaim-migrate @ 17709]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
153 time_idle = time(NULL) - last_active_time; |
762896fc8449
[gaim-migrate @ 17709]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
154 } |
762896fc8449
[gaim-migrate @ 17709]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
155 |
14192 | 156 if (auto_away && |
157 (time_idle > (60 * gaim_prefs_get_int("/core/away/mins_before_away")))) | |
158 { | |
159 gaim_savedstatus_set_idleaway(TRUE); | |
160 } | |
161 else if (time_idle < 60 * gaim_prefs_get_int("/core/away/mins_before_away")) | |
162 { | |
163 gaim_savedstatus_set_idleaway(FALSE); | |
164 } | |
165 | |
166 /* Idle reporting stuff */ | |
167 if (report_idle && (time_idle >= IDLEMARK)) | |
168 { | |
169 for (l = gaim_connections_get_all(); l != NULL; l = l->next) | |
170 { | |
171 GaimConnection *gc = l->data; | |
172 set_account_idle(gaim_connection_get_account(gc), time_idle); | |
173 } | |
174 } | |
175 else if (!report_idle || (time_idle < IDLEMARK)) | |
176 { | |
177 while (idled_accts != NULL) | |
178 set_account_unidle(idled_accts->data); | |
179 } | |
180 | |
181 return TRUE; | |
182 } | |
183 | |
184 static void | |
185 im_msg_sent_cb(GaimAccount *account, const char *receiver, | |
186 const char *message, void *data) | |
187 { | |
188 /* Check our idle time after an IM is sent */ | |
189 check_idleness(); | |
190 } | |
191 | |
192 static void | |
193 signing_on_cb(GaimConnection *gc, void *data) | |
194 { | |
195 /* When signing on a new account, check if the account should be idle */ | |
196 check_idleness(); | |
197 } | |
198 | |
199 static void | |
200 signing_off_cb(GaimConnection *gc, void *data) | |
201 { | |
202 GaimAccount *account; | |
203 | |
204 account = gaim_connection_get_account(gc); | |
205 set_account_unidle(account); | |
206 } | |
207 | |
208 void | |
209 gaim_idle_touch() | |
210 { | |
211 time(&last_active_time); | |
212 } | |
213 | |
214 void | |
215 gaim_idle_set(time_t time) | |
216 { | |
217 last_active_time = time; | |
218 } | |
219 | |
220 void | |
221 gaim_idle_set_ui_ops(GaimIdleUiOps *ops) | |
222 { | |
223 idle_ui_ops = ops; | |
224 } | |
225 | |
226 GaimIdleUiOps * | |
227 gaim_idle_get_ui_ops(void) | |
228 { | |
229 return idle_ui_ops; | |
230 } | |
231 | |
232 static void * | |
233 gaim_idle_get_handle() | |
234 { | |
235 static int handle; | |
236 | |
237 return &handle; | |
238 } | |
239 | |
240 void | |
241 gaim_idle_init() | |
242 { | |
243 /* Add the timer to check if we're idle */ | |
244 idle_timer = gaim_timeout_add(IDLE_CHECK_INTERVAL * 1000, check_idleness, NULL); | |
245 | |
246 gaim_signal_connect(gaim_conversations_get_handle(), "sent-im-msg", | |
247 gaim_idle_get_handle(), | |
248 GAIM_CALLBACK(im_msg_sent_cb), NULL); | |
249 gaim_signal_connect(gaim_connections_get_handle(), "signing-on", | |
250 gaim_idle_get_handle(), | |
251 GAIM_CALLBACK(signing_on_cb), NULL); | |
252 gaim_signal_connect(gaim_connections_get_handle(), "signing-off", | |
253 gaim_idle_get_handle(), | |
254 GAIM_CALLBACK(signing_off_cb), NULL); | |
255 | |
256 gaim_idle_touch(); | |
257 } | |
258 | |
259 void | |
260 gaim_idle_uninit() | |
261 { | |
262 gaim_signals_disconnect_by_handle(gaim_idle_get_handle()); | |
263 | |
264 /* Remove the idle timer */ | |
265 if (idle_timer > 0) | |
266 gaim_timeout_remove(idle_timer); | |
267 idle_timer = 0; | |
268 } |