comparison libpurple/idle.c @ 18648:667c209521db

Fix idle reporting as discussed on devel@. Poll as needed when we're not idle, and poll frequently (once every three seconds) when we are. Fixes #1357
author Sean Egan <seanegan@gmail.com>
date Fri, 27 Jul 2007 00:05:40 +0000
parents 59aec2d6ec43
children 994d36743cbb
comparison
equal deleted inserted replaced
18647:24e3cca6b03c 18648:667c209521db
27 #include "idle.h" 27 #include "idle.h"
28 #include "log.h" 28 #include "log.h"
29 #include "prefs.h" 29 #include "prefs.h"
30 #include "savedstatuses.h" 30 #include "savedstatuses.h"
31 #include "signals.h" 31 #include "signals.h"
32
33 #define IDLEMARK 600 /* 10 minutes! */
34 32
35 typedef enum 33 typedef enum
36 { 34 {
37 PURPLE_IDLE_NOT_AWAY = 0, 35 PURPLE_IDLE_NOT_AWAY = 0,
38 PURPLE_IDLE_AUTO_AWAY, 36 PURPLE_IDLE_AUTO_AWAY,
119 gboolean auto_away; 117 gboolean auto_away;
120 const gchar *idle_reporting; 118 const gchar *idle_reporting;
121 gboolean report_idle = TRUE; 119 gboolean report_idle = TRUE;
122 gint away_seconds = 0; 120 gint away_seconds = 0;
123 gint idle_recheck_interval = 0; 121 gint idle_recheck_interval = 0;
124 122 gint idle_poll_seconds = purple_prefs_get_int("/purple/away/mins_before_away") * 60;
125 purple_signal_emit(purple_blist_get_handle(), "update-idle"); 123 purple_signal_emit(purple_blist_get_handle(), "update-idle");
126 124
127 idle_reporting = purple_prefs_get_string("/purple/away/idle_reporting"); 125 idle_reporting = purple_prefs_get_string("/purple/away/idle_reporting");
128 auto_away = purple_prefs_get_bool("/purple/away/away_when_idle"); 126 auto_away = purple_prefs_get_bool("/purple/away/away_when_idle");
129 127
130 if (!strcmp(idle_reporting, "system") && 128 if (!strcmp(idle_reporting, "system") &&
131 (idle_ui_ops != NULL) && (idle_ui_ops->get_time_idle != NULL)) 129 (idle_ui_ops != NULL) && (idle_ui_ops->get_time_idle != NULL))
132 { 130 {
133 /* Use system idle time (mouse or keyboard movement, etc.) */ 131 /* Use system idle time (mouse or keyboard movement, etc.) */
134 time_idle = idle_ui_ops->get_time_idle(); 132 time_idle = idle_ui_ops->get_time_idle();
135 idle_recheck_interval = 60; 133 idle_recheck_interval = 1;
136 } 134 }
137 else if (!strcmp(idle_reporting, "purple")) 135 else if (!strcmp(idle_reporting, "purple"))
138 { 136 {
139 /* Use 'Purple idle' */ 137 /* Use 'Purple idle' */
140 time_idle = time(NULL) - last_active_time; 138 time_idle = time(NULL) - last_active_time;
151 if (auto_away) 149 if (auto_away)
152 { 150 {
153 if ((idle_ui_ops != NULL) && (idle_ui_ops->get_time_idle != NULL)) 151 if ((idle_ui_ops != NULL) && (idle_ui_ops->get_time_idle != NULL))
154 { 152 {
155 time_idle = idle_ui_ops->get_time_idle(); 153 time_idle = idle_ui_ops->get_time_idle();
156 idle_recheck_interval = 60; 154 idle_recheck_interval = 1;
157 } 155 }
158 else 156 else
159 { 157 {
160 time_idle = time(NULL) - last_active_time; 158 time_idle = time(NULL) - last_active_time;
161 idle_recheck_interval = 0; 159 idle_recheck_interval = 0;
171 time_until_next_idle_event = 0; 169 time_until_next_idle_event = 0;
172 return; 170 return;
173 } 171 }
174 } 172 }
175 173
176 time_until_next_idle_event = IDLEMARK - time_idle; 174 time_until_next_idle_event = idle_poll_seconds - time_idle;
177 if (time_until_next_idle_event < 0) 175 if (time_until_next_idle_event < 0)
178 { 176 {
179 /* If we're already idle, check again as appropriate. */ 177 /* If we're already idle, check again as appropriate. */
180 time_until_next_idle_event = idle_recheck_interval; 178 time_until_next_idle_event = idle_recheck_interval;
181 } 179 }
195 if (time_until_next_idle_event == 0 || (away_seconds - time_idle) < time_until_next_idle_event) 193 if (time_until_next_idle_event == 0 || (away_seconds - time_idle) < time_until_next_idle_event)
196 time_until_next_idle_event = away_seconds - time_idle; 194 time_until_next_idle_event = away_seconds - time_idle;
197 } 195 }
198 196
199 /* Idle reporting stuff */ 197 /* Idle reporting stuff */
200 if (report_idle && (time_idle >= IDLEMARK)) 198 if (report_idle && (time_idle >= idle_poll_seconds))
201 { 199 {
202 GList *l; 200 GList *l;
203 for (l = purple_connections_get_all(); l != NULL; l = l->next) 201 for (l = purple_connections_get_all(); l != NULL; l = l->next)
204 { 202 {
205 PurpleConnection *gc = l->data; 203 PurpleConnection *gc = l->data;
206 set_account_idle(purple_connection_get_account(gc), time_idle); 204 set_account_idle(purple_connection_get_account(gc), time_idle);
207 } 205 }
208 } 206 }
209 else if (!report_idle || (time_idle < IDLEMARK)) 207 else if (!report_idle || (time_idle < idle_poll_seconds ))
210 { 208 {
211 while (idled_accts != NULL) 209 while (idled_accts != NULL)
212 set_account_unidle(idled_accts->data); 210 set_account_unidle(idled_accts->data);
213 } 211 }
214 } 212 }
304 return &handle; 302 return &handle;
305 } 303 }
306 304
307 static gboolean _do_purple_idle_touch_cb(gpointer data) 305 static gboolean _do_purple_idle_touch_cb(gpointer data)
308 { 306 {
307 int idle_poll_minutes = purple_prefs_get_int("/purple/away/mins_before_away");
308
309 /* +1 more for g_timeout_add_seconds rounding. */
310 idle_timer = purple_timeout_add_seconds((idle_poll_minutes * 60) + 2, check_idleness_timer, NULL);
311
309 purple_idle_touch(); 312 purple_idle_touch();
310 313
311 return FALSE; 314 return FALSE;
312 } 315 }
313 316
314 317
315 void 318 void
316 purple_idle_init() 319 purple_idle_init()
317 { 320 {
318 /* Add the timer to check if we're idle.
319 * IDLEMARK + 1 as the boundary,
320 * +1 more for g_timeout_add_seconds rounding. */
321 idle_timer = purple_timeout_add_seconds((IDLEMARK + 2), check_idleness_timer, NULL);
322
323 purple_signal_connect(purple_conversations_get_handle(), "sent-im-msg", 321 purple_signal_connect(purple_conversations_get_handle(), "sent-im-msg",
324 purple_idle_get_handle(), 322 purple_idle_get_handle(),
325 PURPLE_CALLBACK(im_msg_sent_cb), NULL); 323 PURPLE_CALLBACK(im_msg_sent_cb), NULL);
326 purple_signal_connect(purple_connections_get_handle(), "signing-on", 324 purple_signal_connect(purple_connections_get_handle(), "signing-on",
327 purple_idle_get_handle(), 325 purple_idle_get_handle(),