comparison libpurple/idle.c @ 20453:69febfa6d307

propagate from branch 'im.pidgin.pidgin' (head d3e5a5add3f39caa08b46c83177328e51c2d961a) to branch 'im.pidgin.cpw.khc.msnp14' (head a8f6c999b039b4097aa70cd8d2597f3127615435)
author Carlos Silva <typ0@pidgin.im>
date Sat, 16 Jun 2007 04:00:32 +0000
parents 5c34a0a3c362 40d51793f2d7
children
comparison
equal deleted inserted replaced
20452:5c34a0a3c362 20453:69febfa6d307
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 gboolean no_away = FALSE;
96 static gint time_until_next_idle_event; 96 static gint time_until_next_idle_event;
97 /* 97 /*
98 * This function should be called when you think your idle state 98 * This function should be called when you think your idle state
99 * 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
100 * Purple should start reporting idle time to the server. Maybe 100 * Purple should start reporting idle time to the server. Maybe
116 check_idleness(void) 116 check_idleness(void)
117 { 117 {
118 time_t time_idle; 118 time_t time_idle;
119 gboolean auto_away; 119 gboolean auto_away;
120 const gchar *idle_reporting; 120 const gchar *idle_reporting;
121 gboolean report_idle; 121 gboolean report_idle = TRUE;
122 GList *l;
123 gint away_seconds = 0; 122 gint away_seconds = 0;
124 gint idle_recheck_interval = 0; 123 gint idle_recheck_interval = 0;
125 124
126 purple_signal_emit(purple_blist_get_handle(), "update-idle"); 125 purple_signal_emit(purple_blist_get_handle(), "update-idle");
127 126
128 idle_reporting = purple_prefs_get_string("/purple/away/idle_reporting"); 127 idle_reporting = purple_prefs_get_string("/purple/away/idle_reporting");
129 report_idle = TRUE; 128 auto_away = purple_prefs_get_bool("/purple/away/away_when_idle");
129
130 if (!strcmp(idle_reporting, "system") && 130 if (!strcmp(idle_reporting, "system") &&
131 (idle_ui_ops != NULL) && (idle_ui_ops->get_time_idle != NULL)) 131 (idle_ui_ops != NULL) && (idle_ui_ops->get_time_idle != NULL))
132 { 132 {
133 /* Use system idle time (mouse or keyboard movement, etc.) */ 133 /* Use system idle time (mouse or keyboard movement, etc.) */
134 time_idle = idle_ui_ops->get_time_idle(); 134 time_idle = idle_ui_ops->get_time_idle();
143 else 143 else
144 { 144 {
145 /* Don't report idle time */ 145 /* Don't report idle time */
146 time_idle = 0; 146 time_idle = 0;
147 report_idle = FALSE; 147 report_idle = FALSE;
148 } 148
149 149 /* If we're not reporting idle, we can still do auto-away.
150 /* Auto-away stuff */ 150 * First try "system" and if that isn't possible, use "purple" */
151 auto_away = purple_prefs_get_bool("/purple/away/away_when_idle");
152
153 /* If we're not reporting idle, we can still do auto-away.
154 * First try "system" and if that isn't possible, use "purple" */
155 if (!report_idle)
156 {
157 if (auto_away) 151 if (auto_away)
158 { 152 {
159 if ((idle_ui_ops != NULL) && (idle_ui_ops->get_time_idle != NULL)) 153 if ((idle_ui_ops != NULL) && (idle_ui_ops->get_time_idle != NULL))
160 { 154 {
161 time_idle = idle_ui_ops->get_time_idle(); 155 time_idle = idle_ui_ops->get_time_idle();
170 else 164 else
171 { 165 {
172 if (!no_away) 166 if (!no_away)
173 { 167 {
174 purple_savedstatus_set_idleaway(FALSE); 168 purple_savedstatus_set_idleaway(FALSE);
175 no_away = 1; 169 no_away = TRUE;
176 } 170 }
177 time_until_next_idle_event = 0; 171 time_until_next_idle_event = 0;
178 return; 172 return;
179 } 173 }
180 } 174 }
190 away_seconds = 60 * purple_prefs_get_int("/purple/away/mins_before_away"); 184 away_seconds = 60 * purple_prefs_get_int("/purple/away/mins_before_away");
191 185
192 if (auto_away && time_idle > away_seconds) 186 if (auto_away && time_idle > away_seconds)
193 { 187 {
194 purple_savedstatus_set_idleaway(TRUE); 188 purple_savedstatus_set_idleaway(TRUE);
195 no_away = 0; 189 no_away = FALSE;
196 } 190 }
197 else if (!no_away && time_idle < away_seconds) 191 else if (!no_away && time_idle < away_seconds)
198 { 192 {
199 no_away = 1; 193 no_away = TRUE;
200 purple_savedstatus_set_idleaway(FALSE); 194 purple_savedstatus_set_idleaway(FALSE);
201 if (time_until_next_idle_event == 0 || (away_seconds - time_idle) < time_until_next_idle_event) 195 if (time_until_next_idle_event == 0 || (away_seconds - time_idle) < time_until_next_idle_event)
202 time_until_next_idle_event = away_seconds - time_idle; 196 time_until_next_idle_event = away_seconds - time_idle;
203 } 197 }
204 198
205 /* Idle reporting stuff */ 199 /* Idle reporting stuff */
206 if (report_idle && (time_idle >= IDLEMARK)) 200 if (report_idle && (time_idle >= IDLEMARK))
207 { 201 {
202 const GList *l;
208 for (l = purple_connections_get_all(); l != NULL; l = l->next) 203 for (l = purple_connections_get_all(); l != NULL; l = l->next)
209 { 204 {
210 PurpleConnection *gc = l->data; 205 PurpleConnection *gc = l->data;
211 set_account_idle(purple_connection_get_account(gc), time_idle); 206 set_account_idle(purple_connection_get_account(gc), time_idle);
212 } 207 }
227 { 222 {
228 check_idleness(); 223 check_idleness();
229 if (time_until_next_idle_event == 0) 224 if (time_until_next_idle_event == 0)
230 idle_timer = 0; 225 idle_timer = 0;
231 else 226 else
232 idle_timer = purple_timeout_add(1000 * (time_until_next_idle_event + 1), check_idleness_timer, NULL); 227 {
228 /* +1 for the boundary,
229 * +1 more for g_timeout_add_seconds rounding. */
230 idle_timer = purple_timeout_add_seconds(time_until_next_idle_event + 2, check_idleness_timer, NULL);
231 }
233 return FALSE; 232 return FALSE;
234 } 233 }
235 234
236 static void 235 static void
237 im_msg_sent_cb(PurpleAccount *account, const char *receiver, 236 im_msg_sent_cb(PurpleAccount *account, const char *receiver,
303 static int handle; 302 static int handle;
304 303
305 return &handle; 304 return &handle;
306 } 305 }
307 306
307 static gboolean _do_purple_idle_touch_cb(gpointer data)
308 {
309 purple_idle_touch();
310
311 return FALSE;
312 }
313
314
308 void 315 void
309 purple_idle_init() 316 purple_idle_init()
310 { 317 {
311 /* Add the timer to check if we're idle */ 318 /* Add the timer to check if we're idle.
312 idle_timer = purple_timeout_add(1000 * (IDLEMARK + 1), check_idleness_timer, NULL); 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);
313 322
314 purple_signal_connect(purple_conversations_get_handle(), "sent-im-msg", 323 purple_signal_connect(purple_conversations_get_handle(), "sent-im-msg",
315 purple_idle_get_handle(), 324 purple_idle_get_handle(),
316 PURPLE_CALLBACK(im_msg_sent_cb), NULL); 325 PURPLE_CALLBACK(im_msg_sent_cb), NULL);
317 purple_signal_connect(purple_connections_get_handle(), "signing-on", 326 purple_signal_connect(purple_connections_get_handle(), "signing-on",
322 PURPLE_CALLBACK(signing_off_cb), NULL); 331 PURPLE_CALLBACK(signing_off_cb), NULL);
323 332
324 purple_prefs_connect_callback(purple_idle_get_handle(), "/purple/away/idle_reporting", 333 purple_prefs_connect_callback(purple_idle_get_handle(), "/purple/away/idle_reporting",
325 idle_reporting_cb, NULL); 334 idle_reporting_cb, NULL);
326 335
327 purple_idle_touch(); 336 /* Initialize the idleness asynchronously so it doesn't check idleness,
337 * and potentially try to change the status before the UI is initialized */
338 g_idle_add(_do_purple_idle_touch_cb, NULL);
339
328 } 340 }
329 341
330 void 342 void
331 purple_idle_uninit() 343 purple_idle_uninit()
332 { 344 {