Mon, 14 May 2007 04:02:56 +0000
Run the first timer at IDLEMARK + 1, to ensure we don't hit it right on IDLEMARK.
| 12272 | 1 | /* |
| 15884 | 2 | * purple |
| 12272 | 3 | * |
| 15884 | 4 | * Purple is the legal property of its developers, whose names are too numerous |
| 12272 | 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 | ||
| 35 | typedef enum | |
| 36 | { | |
| 15884 | 37 | PURPLE_IDLE_NOT_AWAY = 0, |
| 38 | PURPLE_IDLE_AUTO_AWAY, | |
| 39 | PURPLE_IDLE_AWAY_BUT_NOT_AUTO_AWAY | |
| 12272 | 40 | |
| 15884 | 41 | } PurpleAutoAwayState; |
| 12272 | 42 | |
| 15884 | 43 | static PurpleIdleUiOps *idle_ui_ops = NULL; |
| 12272 | 44 | |
| 45 | /** | |
| 46 | * This is needed for the I'dle Mak'er plugin to work correctly. We | |
| 47 | * use it to determine if we're the ones who set our accounts idle | |
| 48 | * or if someone else did it (the I'dle Mak'er plugin, for example). | |
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
49 | * Basically we just keep track of which accounts were set idle by us, |
|
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
50 | * and then we'll only set these specific accounts unidle when the |
|
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
51 | * user returns. |
| 12272 | 52 | */ |
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
53 | static GList *idled_accts = NULL; |
| 12272 | 54 | |
| 55 | static guint idle_timer = 0; | |
| 56 | ||
| 57 | static time_t last_active_time = 0; | |
| 58 | ||
| 59 | static void | |
| 15884 | 60 | set_account_idle(PurpleAccount *account, int time_idle) |
| 12272 | 61 | { |
| 15884 | 62 | PurplePresence *presence; |
| 12272 | 63 | |
| 15884 | 64 | presence = purple_account_get_presence(account); |
| 12272 | 65 | |
| 15884 | 66 | if (purple_presence_is_idle(presence)) |
| 12272 | 67 | /* This account is already idle! */ |
| 68 | return; | |
| 69 | ||
| 15884 | 70 | purple_debug_info("idle", "Setting %s idle %d seconds\n", |
| 71 | purple_account_get_username(account), time_idle); | |
| 72 | purple_presence_set_idle(presence, TRUE, time(NULL) - time_idle); | |
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
73 | idled_accts = g_list_prepend(idled_accts, account); |
| 12272 | 74 | } |
| 75 | ||
| 76 | static void | |
| 15884 | 77 | set_account_unidle(PurpleAccount *account) |
| 12272 | 78 | { |
| 15884 | 79 | PurplePresence *presence; |
| 12272 | 80 | |
| 15884 | 81 | presence = purple_account_get_presence(account); |
| 12272 | 82 | |
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
83 | idled_accts = g_list_remove(idled_accts, account); |
|
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
84 | |
| 15884 | 85 | if (!purple_presence_is_idle(presence)) |
| 12272 | 86 | /* This account is already unidle! */ |
| 87 | return; | |
| 88 | ||
| 15884 | 89 | purple_debug_info("idle", "Setting %s unidle\n", |
| 90 | purple_account_get_username(account)); | |
| 91 | purple_presence_set_idle(presence, FALSE, 0); | |
| 12272 | 92 | } |
| 93 | ||
| 17130 | 94 | |
| 95 | static gint time_until_next_idle_event; | |
| 12272 | 96 | /* |
| 97 | * This function should be called when you think your idle state | |
| 98 | * may have changed. Maybe you're over the 10-minute mark and | |
| 15884 | 99 | * Purple should start reporting idle time to the server. Maybe |
| 12272 | 100 | * you've returned from being idle. Maybe your auto-away message |
| 101 | * should be set. | |
| 102 | * | |
| 103 | * There is no harm to calling this many many times, other than | |
| 104 | * it will be kinda slow. This is called every 5 seconds by a | |
| 15884 | 105 | * timer set when Purple starts. It is also called when |
| 12272 | 106 | * you send an IM, a chat, etc. |
| 107 | * | |
| 108 | * This function has 3 sections. | |
| 109 | * 1. Get your idle time. It will query XScreenSaver or Windows | |
| 15884 | 110 | * or use the Purple idle time. Whatever. |
| 12272 | 111 | * 2. Set or unset your auto-away message. |
| 112 | * 3. Report your current idle time to the IM server. | |
| 113 | */ | |
| 17130 | 114 | |
| 115 | static void | |
| 116 | check_idleness(void) | |
| 12272 | 117 | { |
| 118 | time_t time_idle; | |
| 119 | gboolean auto_away; | |
|
12573
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
120 | const gchar *idle_reporting; |
| 12272 | 121 | gboolean report_idle; |
| 122 | GList *l; | |
| 17130 | 123 | gint away_seconds = 0; |
| 124 | static int no_away = 0; | |
| 12272 | 125 | |
| 15884 | 126 | purple_signal_emit(purple_blist_get_handle(), "update-idle"); |
| 12272 | 127 | |
|
16478
19107605c565
Works for me! Renames prefs: /core to /purple, /gaim/gtk to /pidgin, /gaim/gnt to /finch
Sean Egan <seanegan@pidgin.im>
parents:
15884
diff
changeset
|
128 | idle_reporting = purple_prefs_get_string("/purple/away/idle_reporting"); |
|
12573
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
129 | report_idle = TRUE; |
|
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
130 | if (!strcmp(idle_reporting, "system") && |
|
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
131 | (idle_ui_ops != NULL) && (idle_ui_ops->get_time_idle != NULL)) |
|
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
132 | { |
|
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
133 | /* Use system idle time (mouse or keyboard movement, etc.) */ |
| 12272 | 134 | time_idle = idle_ui_ops->get_time_idle(); |
|
12573
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
135 | } |
| 15884 | 136 | else if (!strcmp(idle_reporting, "purple")) |
|
12573
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
137 | { |
| 15884 | 138 | /* Use 'Purple idle' */ |
| 12272 | 139 | time_idle = time(NULL) - last_active_time; |
|
12573
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
140 | } |
|
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
141 | else |
|
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
142 | { |
|
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
143 | /* Don't report idle time */ |
|
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
144 | time_idle = 0; |
|
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
145 | report_idle = FALSE; |
|
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
146 | } |
| 12272 | 147 | |
| 148 | /* Auto-away stuff */ | |
|
16478
19107605c565
Works for me! Renames prefs: /core to /purple, /gaim/gtk to /pidgin, /gaim/gnt to /finch
Sean Egan <seanegan@pidgin.im>
parents:
15884
diff
changeset
|
149 | auto_away = purple_prefs_get_bool("/purple/away/away_when_idle"); |
|
14999
7b3992f19766
[gaim-migrate @ 17709]
Daniel Atallah <datallah@pidgin.im>
parents:
14254
diff
changeset
|
150 | |
|
7b3992f19766
[gaim-migrate @ 17709]
Daniel Atallah <datallah@pidgin.im>
parents:
14254
diff
changeset
|
151 | /* If we're not reporting idle, we can still do auto-away. |
| 15884 | 152 | * First try "system" and if that isn't possible, use "purple" */ |
|
14999
7b3992f19766
[gaim-migrate @ 17709]
Daniel Atallah <datallah@pidgin.im>
parents:
14254
diff
changeset
|
153 | if (!report_idle && auto_away) { |
|
7b3992f19766
[gaim-migrate @ 17709]
Daniel Atallah <datallah@pidgin.im>
parents:
14254
diff
changeset
|
154 | if ((idle_ui_ops != NULL) && (idle_ui_ops->get_time_idle != NULL)) |
|
7b3992f19766
[gaim-migrate @ 17709]
Daniel Atallah <datallah@pidgin.im>
parents:
14254
diff
changeset
|
155 | time_idle = idle_ui_ops->get_time_idle(); |
|
7b3992f19766
[gaim-migrate @ 17709]
Daniel Atallah <datallah@pidgin.im>
parents:
14254
diff
changeset
|
156 | else |
|
7b3992f19766
[gaim-migrate @ 17709]
Daniel Atallah <datallah@pidgin.im>
parents:
14254
diff
changeset
|
157 | time_idle = time(NULL) - last_active_time; |
|
7b3992f19766
[gaim-migrate @ 17709]
Daniel Atallah <datallah@pidgin.im>
parents:
14254
diff
changeset
|
158 | } |
|
7b3992f19766
[gaim-migrate @ 17709]
Daniel Atallah <datallah@pidgin.im>
parents:
14254
diff
changeset
|
159 | |
| 17130 | 160 | time_until_next_idle_event = IDLEMARK - time_idle; /* reasonable start upperbound */ |
| 161 | ||
| 162 | if (auto_away || !no_away) | |
| 163 | away_seconds = 60 * purple_prefs_get_int("/purple/away/mins_before_away"); | |
| 164 | ||
| 165 | if (auto_away && time_idle > away_seconds) | |
| 12272 | 166 | { |
| 15884 | 167 | purple_savedstatus_set_idleaway(TRUE); |
| 17130 | 168 | no_away = 0; |
| 17131 | 169 | if (time_idle < away_seconds && (away_seconds - time_idle) < time_until_next_idle_event) |
| 17130 | 170 | time_until_next_idle_event = away_seconds - time_idle; |
| 12272 | 171 | } |
| 17130 | 172 | else if (!no_away && time_idle < away_seconds) |
| 12272 | 173 | { |
| 15884 | 174 | purple_savedstatus_set_idleaway(FALSE); |
| 17130 | 175 | no_away = 1; |
| 176 | if (time_idle < away_seconds && (away_seconds - time_idle) < time_until_next_idle_event) | |
| 177 | time_until_next_idle_event = away_seconds - time_idle; | |
| 12272 | 178 | } |
| 179 | ||
| 180 | /* Idle reporting stuff */ | |
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
181 | if (report_idle && (time_idle >= IDLEMARK)) |
| 12272 | 182 | { |
| 15884 | 183 | for (l = purple_connections_get_all(); l != NULL; l = l->next) |
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
184 | { |
| 15884 | 185 | PurpleConnection *gc = l->data; |
| 186 | set_account_idle(purple_connection_get_account(gc), time_idle); | |
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
187 | } |
| 12272 | 188 | } |
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
189 | else if (!report_idle || (time_idle < IDLEMARK)) |
| 12272 | 190 | { |
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
191 | while (idled_accts != NULL) |
|
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
192 | set_account_unidle(idled_accts->data); |
| 12272 | 193 | } |
| 17130 | 194 | |
| 195 | if (time_until_next_idle_event < 0) | |
| 196 | time_until_next_idle_event = IDLEMARK; | |
| 197 | } | |
| 12272 | 198 | |
| 17130 | 199 | |
| 200 | /* | |
| 201 | * Check idle and set the timer to fire at the next idle-worth event | |
| 202 | */ | |
| 203 | static gint | |
| 204 | check_idleness_timer() | |
| 205 | { | |
| 206 | check_idleness(); | |
| 207 | idle_timer = purple_timeout_add(1000 * (time_until_next_idle_event + 1), check_idleness_timer, NULL); | |
| 208 | return FALSE; | |
| 12272 | 209 | } |
| 210 | ||
| 211 | static void | |
| 15884 | 212 | im_msg_sent_cb(PurpleAccount *account, const char *receiver, |
| 12272 | 213 | const char *message, void *data) |
| 214 | { | |
| 215 | /* Check our idle time after an IM is sent */ | |
| 216 | check_idleness(); | |
| 217 | } | |
| 218 | ||
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
219 | static void |
| 15884 | 220 | signing_on_cb(PurpleConnection *gc, void *data) |
| 14189 | 221 | { |
| 222 | /* When signing on a new account, check if the account should be idle */ | |
| 223 | check_idleness(); | |
| 224 | } | |
| 225 | ||
| 226 | static void | |
| 15884 | 227 | signing_off_cb(PurpleConnection *gc, void *data) |
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
228 | { |
| 15884 | 229 | PurpleAccount *account; |
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
230 | |
| 15884 | 231 | account = purple_connection_get_account(gc); |
| 14189 | 232 | set_account_unidle(account); |
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
233 | } |
|
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
234 | |
| 12272 | 235 | void |
| 15884 | 236 | purple_idle_touch() |
| 12272 | 237 | { |
| 238 | time(&last_active_time); | |
| 239 | } | |
| 240 | ||
| 241 | void | |
| 15884 | 242 | purple_idle_set(time_t time) |
| 12272 | 243 | { |
| 244 | last_active_time = time; | |
| 245 | } | |
| 246 | ||
| 247 | void | |
| 15884 | 248 | purple_idle_set_ui_ops(PurpleIdleUiOps *ops) |
| 12272 | 249 | { |
| 250 | idle_ui_ops = ops; | |
| 251 | } | |
| 252 | ||
| 15884 | 253 | PurpleIdleUiOps * |
| 254 | purple_idle_get_ui_ops(void) | |
| 12272 | 255 | { |
| 256 | return idle_ui_ops; | |
| 257 | } | |
| 258 | ||
|
12412
8abe3226695e
[gaim-migrate @ 14719]
Richard Laager <rlaager@pidgin.im>
parents:
12272
diff
changeset
|
259 | static void * |
| 15884 | 260 | purple_idle_get_handle() |
| 12272 | 261 | { |
| 262 | static int handle; | |
| 263 | ||
| 264 | return &handle; | |
| 265 | } | |
| 266 | ||
| 267 | void | |
| 15884 | 268 | purple_idle_init() |
| 12272 | 269 | { |
| 270 | /* Add the timer to check if we're idle */ | |
|
17132
3b628eff9b3b
Run the first timer at IDLEMARK + 1, to ensure we don't hit it right on IDLEMARK.
Richard Laager <rlaager@pidgin.im>
parents:
17131
diff
changeset
|
271 | idle_timer = purple_timeout_add(1000 * (IDLEMARK + 1), check_idleness_timer, NULL); |
| 12272 | 272 | |
| 15884 | 273 | purple_signal_connect(purple_conversations_get_handle(), "sent-im-msg", |
| 274 | purple_idle_get_handle(), | |
| 275 | PURPLE_CALLBACK(im_msg_sent_cb), NULL); | |
| 276 | purple_signal_connect(purple_connections_get_handle(), "signing-on", | |
| 277 | purple_idle_get_handle(), | |
| 278 | PURPLE_CALLBACK(signing_on_cb), NULL); | |
| 279 | purple_signal_connect(purple_connections_get_handle(), "signing-off", | |
| 280 | purple_idle_get_handle(), | |
| 281 | PURPLE_CALLBACK(signing_off_cb), NULL); | |
| 12272 | 282 | |
| 15884 | 283 | purple_idle_touch(); |
| 12272 | 284 | } |
| 285 | ||
| 286 | void | |
| 15884 | 287 | purple_idle_uninit() |
| 12272 | 288 | { |
| 15884 | 289 | purple_signals_disconnect_by_handle(purple_idle_get_handle()); |
| 12272 | 290 | |
| 291 | /* Remove the idle timer */ | |
| 292 | if (idle_timer > 0) | |
| 15884 | 293 | purple_timeout_remove(idle_timer); |
| 12272 | 294 | idle_timer = 0; |
| 295 | } |