comparison libpurple/idle.c @ 17089:e7e033a38612

When using Purple idle, don't run a timer at all when idleaway. When using system idle, check every 60 seconds.
author Richard Laager <rlaager@wiktel.com>
date Sat, 19 May 2007 06:16:44 +0000
parents 6545d21b6f5b
children bf27ef68e81a
comparison
equal deleted inserted replaced
17088:6545d21b6f5b 17089:e7e033a38612
90 purple_account_get_username(account)); 90 purple_account_get_username(account));
91 purple_presence_set_idle(presence, FALSE, 0); 91 purple_presence_set_idle(presence, FALSE, 0);
92 } 92 }
93 93
94 94
95 static int no_away = 0;
95 static gint time_until_next_idle_event; 96 static gint time_until_next_idle_event;
96 /* 97 /*
97 * This function should be called when you think your idle state 98 * This function should be called when you think your idle state
98 * may have changed. Maybe you're over the 10-minute mark and 99 * may have changed. Maybe you're over the 10-minute mark and
99 * Purple should start reporting idle time to the server. Maybe 100 * Purple should start reporting idle time to the server. Maybe
119 gboolean auto_away; 120 gboolean auto_away;
120 const gchar *idle_reporting; 121 const gchar *idle_reporting;
121 gboolean report_idle; 122 gboolean report_idle;
122 GList *l; 123 GList *l;
123 gint away_seconds = 0; 124 gint away_seconds = 0;
124 static int no_away = 0; 125 gint idle_recheck_interval;
125 126
126 purple_signal_emit(purple_blist_get_handle(), "update-idle"); 127 purple_signal_emit(purple_blist_get_handle(), "update-idle");
127 128
128 idle_reporting = purple_prefs_get_string("/purple/away/idle_reporting"); 129 idle_reporting = purple_prefs_get_string("/purple/away/idle_reporting");
129 report_idle = TRUE; 130 report_idle = TRUE;
130 if (!strcmp(idle_reporting, "system") && 131 if (!strcmp(idle_reporting, "system") &&
131 (idle_ui_ops != NULL) && (idle_ui_ops->get_time_idle != NULL)) 132 (idle_ui_ops != NULL) && (idle_ui_ops->get_time_idle != NULL))
132 { 133 {
133 /* Use system idle time (mouse or keyboard movement, etc.) */ 134 /* Use system idle time (mouse or keyboard movement, etc.) */
134 time_idle = idle_ui_ops->get_time_idle(); 135 time_idle = idle_ui_ops->get_time_idle();
136 idle_recheck_interval = 60;
135 } 137 }
136 else if (!strcmp(idle_reporting, "purple")) 138 else if (!strcmp(idle_reporting, "purple"))
137 { 139 {
138 /* Use 'Purple idle' */ 140 /* Use 'Purple idle' */
139 time_idle = time(NULL) - last_active_time; 141 time_idle = time(NULL) - last_active_time;
142 idle_recheck_interval = 0;
140 } 143 }
141 else 144 else
142 { 145 {
143 /* Don't report idle time */ 146 /* Don't report idle time */
144 time_idle = 0; 147 time_idle = 0;
148 /* Auto-away stuff */ 151 /* Auto-away stuff */
149 auto_away = purple_prefs_get_bool("/purple/away/away_when_idle"); 152 auto_away = purple_prefs_get_bool("/purple/away/away_when_idle");
150 153
151 /* If we're not reporting idle, we can still do auto-away. 154 /* If we're not reporting idle, we can still do auto-away.
152 * First try "system" and if that isn't possible, use "purple" */ 155 * First try "system" and if that isn't possible, use "purple" */
153 if (!report_idle && auto_away) { 156 if (!report_idle)
154 if ((idle_ui_ops != NULL) && (idle_ui_ops->get_time_idle != NULL)) 157 {
155 time_idle = idle_ui_ops->get_time_idle(); 158 if (auto_away)
159 {
160 if ((idle_ui_ops != NULL) && (idle_ui_ops->get_time_idle != NULL))
161 {
162 time_idle = idle_ui_ops->get_time_idle();
163 idle_recheck_interval = 60;
164 }
165 else
166 {
167 time_idle = time(NULL) - last_active_time;
168 idle_recheck_interval = 0;
169 }
170 }
156 else 171 else
157 time_idle = time(NULL) - last_active_time; 172 {
173 if (!no_away)
174 {
175 purple_savedstatus_set_idleaway(FALSE);
176 no_away = 1;
177 }
178 time_until_next_idle_event = 0;
179 return;
180 }
158 } 181 }
159 182
160 time_until_next_idle_event = IDLEMARK - time_idle; 183 time_until_next_idle_event = IDLEMARK - time_idle;
161 if (time_until_next_idle_event < 0) 184 if (time_until_next_idle_event < 0)
162 { 185 {
163 /* If we're already idle, check again in a minute. */ 186 /* If we're already idle, check again as appropriate. */
164 time_until_next_idle_event = 60; 187 time_until_next_idle_event = idle_recheck_interval;
165 } 188 }
166 189
167 if (auto_away || !no_away) 190 if (auto_away || !no_away)
168 away_seconds = 60 * purple_prefs_get_int("/purple/away/mins_before_away"); 191 away_seconds = 60 * purple_prefs_get_int("/purple/away/mins_before_away");
169 192
174 } 197 }
175 else if (!no_away && time_idle < away_seconds) 198 else if (!no_away && time_idle < away_seconds)
176 { 199 {
177 purple_savedstatus_set_idleaway(FALSE); 200 purple_savedstatus_set_idleaway(FALSE);
178 no_away = 1; 201 no_away = 1;
179 if ((away_seconds - time_idle) < time_until_next_idle_event) 202 if (time_until_next_idle_event == 0 || (away_seconds - time_idle) < time_until_next_idle_event)
180 time_until_next_idle_event = away_seconds - time_idle; 203 time_until_next_idle_event = away_seconds - time_idle;
181 } 204 }
182 205
183 /* Idle reporting stuff */ 206 /* Idle reporting stuff */
184 if (report_idle && (time_idle >= IDLEMARK)) 207 if (report_idle && (time_idle >= IDLEMARK))
202 */ 225 */
203 static gint 226 static gint
204 check_idleness_timer() 227 check_idleness_timer()
205 { 228 {
206 check_idleness(); 229 check_idleness();
207 idle_timer = purple_timeout_add(1000 * (time_until_next_idle_event + 1), check_idleness_timer, NULL); 230 if (time_until_next_idle_event == 0)
231 idle_timer = 0;
232 else
233 idle_timer = purple_timeout_add(1000 * (time_until_next_idle_event + 1), check_idleness_timer, NULL);
208 return FALSE; 234 return FALSE;
209 } 235 }
210 236
211 static void 237 static void
212 im_msg_sent_cb(PurpleAccount *account, const char *receiver, 238 im_msg_sent_cb(PurpleAccount *account, const char *receiver,
230 256
231 account = purple_connection_get_account(gc); 257 account = purple_connection_get_account(gc);
232 set_account_unidle(account); 258 set_account_unidle(account);
233 } 259 }
234 260
261 static void
262 idle_reporting_cb(const char *name, PurplePrefType type, gconstpointer val, gpointer data)
263 {
264 if (idle_timer)
265 purple_timeout_remove(idle_timer);
266 idle_timer = 0;
267 check_idleness_timer();
268 }
269
235 void 270 void
236 purple_idle_touch() 271 purple_idle_touch()
237 { 272 {
238 time(&last_active_time); 273 time(&last_active_time);
274 if (!no_away)
275 {
276 if (idle_timer)
277 purple_timeout_remove(idle_timer);
278 idle_timer = 0;
279 check_idleness_timer();
280 }
239 } 281 }
240 282
241 void 283 void
242 purple_idle_set(time_t time) 284 purple_idle_set(time_t time)
243 { 285 {
278 PURPLE_CALLBACK(signing_on_cb), NULL); 320 PURPLE_CALLBACK(signing_on_cb), NULL);
279 purple_signal_connect(purple_connections_get_handle(), "signing-off", 321 purple_signal_connect(purple_connections_get_handle(), "signing-off",
280 purple_idle_get_handle(), 322 purple_idle_get_handle(),
281 PURPLE_CALLBACK(signing_off_cb), NULL); 323 PURPLE_CALLBACK(signing_off_cb), NULL);
282 324
325 purple_prefs_connect_callback(purple_idle_get_handle(), "/purple/away/idle_reporting",
326 idle_reporting_cb, NULL)
327
283 purple_idle_touch(); 328 purple_idle_touch();
284 } 329 }
285 330
286 void 331 void
287 purple_idle_uninit() 332 purple_idle_uninit()
288 { 333 {
289 purple_signals_disconnect_by_handle(purple_idle_get_handle()); 334 purple_signals_disconnect_by_handle(purple_idle_get_handle());
335 purple_prefs_disconnect_by_handle(purple_idle_get_handle());
290 336
291 /* Remove the idle timer */ 337 /* Remove the idle timer */
292 if (idle_timer > 0) 338 if (idle_timer > 0)
293 purple_timeout_remove(idle_timer); 339 purple_timeout_remove(idle_timer);
294 idle_timer = 0; 340 idle_timer = 0;