Thu, 01 Dec 2005 21:13:48 +0000
[gaim-migrate @ 14590]
Mostly whitespace, but a couple small typo fixes, and an A;B -> B;A; swap to
be consistent in plugins/notify.c
| 12272 | 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 | * If our accounts are marked as idle and have_set_idle is FALSE and | |
| 51 | * the user moves the mouse, then we will NOT unidle our accounts. | |
| 52 | */ | |
| 53 | static gboolean have_set_idle = FALSE; | |
| 54 | ||
| 55 | static guint idle_timer = 0; | |
| 56 | ||
| 57 | static time_t last_active_time = 0; | |
| 58 | ||
| 59 | static void | |
| 60 | set_account_autoaway(GaimConnection *gc) | |
| 61 | { | |
| 62 | GaimAccount *account; | |
| 63 | GaimPresence *presence; | |
| 64 | GaimStatus *status; | |
| 65 | ||
| 66 | if (gc->is_auto_away) | |
| 67 | /* This account is already auto-away! */ | |
| 68 | return; | |
| 69 | ||
| 70 | account = gaim_connection_get_account(gc); | |
| 71 | presence = gaim_account_get_presence(account); | |
| 72 | status = gaim_presence_get_active_status(presence); | |
| 73 | ||
| 74 | if (gaim_status_is_available(status)) | |
| 75 | { | |
| 76 | GaimSavedStatus *saved_status; | |
| 77 | ||
| 78 | gaim_debug_info("idle", "Making %s auto-away\n", | |
| 79 | gaim_account_get_username(account)); | |
| 80 | ||
| 81 | saved_status = gaim_savedstatus_get_idleaway(); | |
| 82 | gaim_savedstatus_activate_for_account(saved_status, account); | |
| 83 | ||
| 84 | gc->is_auto_away = GAIM_IDLE_AUTO_AWAY; | |
| 85 | } else { | |
| 86 | gc->is_auto_away = GAIM_IDLE_AWAY_BUT_NOT_AUTO_AWAY; | |
| 87 | } | |
| 88 | } | |
| 89 | ||
| 90 | static void | |
| 91 | unset_account_autoaway(GaimConnection *gc) | |
| 92 | { | |
| 93 | GaimAccount *account; | |
| 94 | GaimPresence *presence; | |
| 95 | GaimStatus *status; | |
| 96 | GaimSavedStatus *saved_status; | |
| 97 | ||
| 98 | account = gaim_connection_get_account(gc); | |
| 99 | presence = gaim_account_get_presence(account); | |
| 100 | status = gaim_presence_get_active_status(presence); | |
| 101 | ||
| 102 | if (!gc->is_auto_away) | |
| 103 | /* This account is already not auto-away! */ | |
| 104 | return; | |
| 105 | ||
| 106 | if (gc->is_auto_away == GAIM_IDLE_AWAY_BUT_NOT_AUTO_AWAY) { | |
| 107 | gc->is_auto_away = GAIM_IDLE_NOT_AWAY; | |
| 108 | } else { | |
| 109 | gc->is_auto_away = GAIM_IDLE_NOT_AWAY; | |
| 110 | ||
| 111 | gaim_debug_info("idle", "%s returning from auto-away\n", | |
| 112 | gaim_account_get_username(account)); | |
| 113 | ||
| 114 | /* Return our account to its previous status */ | |
| 115 | saved_status = gaim_savedstatus_get_current(); | |
| 116 | gaim_savedstatus_activate_for_account(saved_status, account); | |
| 117 | } | |
| 118 | } | |
| 119 | ||
| 120 | static void | |
| 121 | set_account_idle(GaimConnection *gc, int time_idle) | |
| 122 | { | |
| 123 | GaimAccount *account; | |
| 124 | GaimPresence *presence; | |
| 125 | ||
| 126 | account = gaim_connection_get_account(gc); | |
| 127 | presence = gaim_account_get_presence(account); | |
| 128 | ||
| 129 | if (gaim_presence_is_idle(presence)) | |
| 130 | /* This account is already idle! */ | |
| 131 | return; | |
| 132 | ||
| 133 | gaim_debug_info("idle", "Setting %s idle %d seconds\n", | |
| 134 | gaim_account_get_username(account), time_idle); | |
| 135 | gaim_presence_set_idle(presence, TRUE, time(NULL) - time_idle); | |
| 136 | } | |
| 137 | ||
| 138 | static void | |
| 139 | set_account_unidle(GaimConnection *gc) | |
| 140 | { | |
| 141 | GaimAccount *account; | |
| 142 | GaimPresence *presence; | |
| 143 | ||
| 144 | account = gaim_connection_get_account(gc); | |
| 145 | presence = gaim_account_get_presence(account); | |
| 146 | ||
| 147 | if (!gaim_presence_is_idle(presence)) | |
| 148 | /* This account is already unidle! */ | |
| 149 | return; | |
| 150 | ||
| 151 | gaim_debug_info("idle", "Setting %s unidle\n", | |
| 152 | gaim_account_get_username(account)); | |
| 153 | gaim_presence_set_idle(presence, FALSE, time(NULL)); | |
| 154 | } | |
| 155 | ||
| 156 | /* | |
| 157 | * This function should be called when you think your idle state | |
| 158 | * may have changed. Maybe you're over the 10-minute mark and | |
| 159 | * Gaim should start reporting idle time to the server. Maybe | |
| 160 | * you've returned from being idle. Maybe your auto-away message | |
| 161 | * should be set. | |
| 162 | * | |
| 163 | * There is no harm to calling this many many times, other than | |
| 164 | * it will be kinda slow. This is called every 5 seconds by a | |
| 165 | * timer set when Gaim starts. It is also called when | |
| 166 | * you send an IM, a chat, etc. | |
| 167 | * | |
| 168 | * This function has 3 sections. | |
| 169 | * 1. Get your idle time. It will query XScreenSaver or Windows | |
| 170 | * or use the Gaim idle time. Whatever. | |
| 171 | * 2. Set or unset your auto-away message. | |
| 172 | * 3. Report your current idle time to the IM server. | |
| 173 | */ | |
| 174 | static gint | |
| 175 | check_idleness() | |
| 176 | { | |
| 177 | time_t time_idle; | |
| 178 | gboolean auto_away; | |
| 179 | gboolean report_idle; | |
| 180 | GList *l; | |
| 181 | ||
| 182 | gaim_signal_emit(gaim_blist_get_handle(), "update-idle"); | |
| 183 | ||
| 184 | if (idle_ui_ops != NULL && idle_ui_ops->get_time_idle != NULL) | |
| 185 | time_idle = idle_ui_ops->get_time_idle(); | |
| 186 | else | |
| 187 | /* Use 'Gaim idle' */ | |
| 188 | time_idle = time(NULL) - last_active_time; | |
| 189 | ||
| 190 | /* Auto-away stuff */ | |
| 191 | auto_away = gaim_prefs_get_bool("/core/away/away_when_idle"); | |
| 192 | if (auto_away && | |
| 193 | (time_idle > (60 * gaim_prefs_get_int("/core/away/mins_before_away")))) | |
| 194 | { | |
| 195 | for (l = gaim_connections_get_all(); l != NULL; l = l->next) | |
| 196 | set_account_autoaway(l->data); | |
| 197 | } | |
| 198 | else if (time_idle < 60 * gaim_prefs_get_int("/core/away/mins_before_away")) | |
| 199 | { | |
| 200 | for (l = gaim_connections_get_all(); l != NULL; l = l->next) | |
| 201 | unset_account_autoaway(l->data); | |
| 202 | } | |
| 203 | ||
| 204 | /* Idle reporting stuff */ | |
| 205 | report_idle = gaim_prefs_get_bool("/core/away/report_idle"); | |
| 206 | if (report_idle && (time_idle >= IDLEMARK) && !have_set_idle) | |
| 207 | { | |
| 208 | for (l = gaim_connections_get_all(); l != NULL; l = l->next) | |
| 209 | set_account_idle(l->data, time_idle); | |
| 210 | have_set_idle = TRUE; | |
| 211 | } | |
| 212 | else if ((!report_idle || time_idle < IDLEMARK) && have_set_idle) | |
| 213 | { | |
| 214 | for (l = gaim_connections_get_all(); l != NULL; l = l->next) | |
| 215 | set_account_unidle(l->data); | |
| 216 | have_set_idle = FALSE; | |
| 217 | } | |
| 218 | ||
| 219 | return TRUE; | |
| 220 | } | |
| 221 | ||
| 222 | static void | |
| 223 | im_msg_sent_cb(GaimAccount *account, const char *receiver, | |
| 224 | const char *message, void *data) | |
| 225 | { | |
| 226 | /* Check our idle time after an IM is sent */ | |
| 227 | check_idleness(); | |
| 228 | } | |
| 229 | ||
| 230 | void | |
| 231 | gaim_idle_touch() | |
| 232 | { | |
| 233 | time(&last_active_time); | |
| 234 | } | |
| 235 | ||
| 236 | void | |
| 237 | gaim_idle_set(time_t time) | |
| 238 | { | |
| 239 | last_active_time = time; | |
| 240 | } | |
| 241 | ||
| 242 | void | |
| 243 | gaim_idle_set_ui_ops(GaimIdleUiOps *ops) | |
| 244 | { | |
| 245 | idle_ui_ops = ops; | |
| 246 | } | |
| 247 | ||
| 248 | GaimIdleUiOps * | |
| 249 | gaim_idle_get_ui_ops(void) | |
| 250 | { | |
| 251 | return idle_ui_ops; | |
| 252 | } | |
| 253 | ||
| 254 | void * | |
| 255 | gaim_idle_get_handle() | |
| 256 | { | |
| 257 | static int handle; | |
| 258 | ||
| 259 | return &handle; | |
| 260 | } | |
| 261 | ||
| 262 | void | |
| 263 | gaim_idle_init() | |
| 264 | { | |
| 265 | /* Add the timer to check if we're idle */ | |
| 266 | idle_timer = gaim_timeout_add(IDLE_CHECK_INTERVAL * 1000, check_idleness, NULL); | |
| 267 | ||
| 268 | gaim_signal_connect(gaim_conversations_get_handle(), "sent-im-msg", | |
| 269 | gaim_idle_get_handle(), | |
| 270 | GAIM_CALLBACK(im_msg_sent_cb), NULL); | |
| 271 | ||
| 272 | gaim_idle_touch(); | |
| 273 | } | |
| 274 | ||
| 275 | void | |
| 276 | gaim_idle_uninit() | |
| 277 | { | |
| 278 | gaim_signals_disconnect_by_handle(gaim_idle_get_handle()); | |
| 279 | ||
| 280 | /* Remove the idle timer */ | |
| 281 | if (idle_timer > 0) | |
| 282 | gaim_timeout_remove(idle_timer); | |
| 283 | idle_timer = 0; | |
| 284 | } |