Tue, 01 May 2007 04:49:01 +0000
Initialize docklet from pidgin_ui_init. This ensures docklet prefs are
created prior to prefs migration. Fixes ticket #405.
| 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 | #define IDLE_CHECK_INTERVAL 5 /* 5 seconds */ | |
| 35 | ||
| 36 | typedef enum | |
| 37 | { | |
| 15884 | 38 | PURPLE_IDLE_NOT_AWAY = 0, |
| 39 | PURPLE_IDLE_AUTO_AWAY, | |
| 40 | PURPLE_IDLE_AWAY_BUT_NOT_AUTO_AWAY | |
| 12272 | 41 | |
| 15884 | 42 | } PurpleAutoAwayState; |
| 12272 | 43 | |
| 15884 | 44 | static PurpleIdleUiOps *idle_ui_ops = NULL; |
| 12272 | 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). | |
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
50 | * 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
|
51 | * 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
|
52 | * user returns. |
| 12272 | 53 | */ |
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
54 | static GList *idled_accts = NULL; |
| 12272 | 55 | |
| 56 | static guint idle_timer = 0; | |
| 57 | ||
| 58 | static time_t last_active_time = 0; | |
| 59 | ||
| 60 | static void | |
| 15884 | 61 | set_account_idle(PurpleAccount *account, int time_idle) |
| 12272 | 62 | { |
| 15884 | 63 | PurplePresence *presence; |
| 12272 | 64 | |
| 15884 | 65 | presence = purple_account_get_presence(account); |
| 12272 | 66 | |
| 15884 | 67 | if (purple_presence_is_idle(presence)) |
| 12272 | 68 | /* This account is already idle! */ |
| 69 | return; | |
| 70 | ||
| 15884 | 71 | purple_debug_info("idle", "Setting %s idle %d seconds\n", |
| 72 | purple_account_get_username(account), time_idle); | |
| 73 | purple_presence_set_idle(presence, TRUE, time(NULL) - time_idle); | |
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
74 | idled_accts = g_list_prepend(idled_accts, account); |
| 12272 | 75 | } |
| 76 | ||
| 77 | static void | |
| 15884 | 78 | set_account_unidle(PurpleAccount *account) |
| 12272 | 79 | { |
| 15884 | 80 | PurplePresence *presence; |
| 12272 | 81 | |
| 15884 | 82 | presence = purple_account_get_presence(account); |
| 12272 | 83 | |
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
84 | idled_accts = g_list_remove(idled_accts, account); |
|
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
85 | |
| 15884 | 86 | if (!purple_presence_is_idle(presence)) |
| 12272 | 87 | /* This account is already unidle! */ |
| 88 | return; | |
| 89 | ||
| 15884 | 90 | purple_debug_info("idle", "Setting %s unidle\n", |
| 91 | purple_account_get_username(account)); | |
| 92 | purple_presence_set_idle(presence, FALSE, 0); | |
| 12272 | 93 | } |
| 94 | ||
| 95 | /* | |
| 96 | * This function should be called when you think your idle state | |
| 97 | * may have changed. Maybe you're over the 10-minute mark and | |
| 15884 | 98 | * Purple should start reporting idle time to the server. Maybe |
| 12272 | 99 | * you've returned from being idle. Maybe your auto-away message |
| 100 | * should be set. | |
| 101 | * | |
| 102 | * There is no harm to calling this many many times, other than | |
| 103 | * it will be kinda slow. This is called every 5 seconds by a | |
| 15884 | 104 | * timer set when Purple starts. It is also called when |
| 12272 | 105 | * you send an IM, a chat, etc. |
| 106 | * | |
| 107 | * This function has 3 sections. | |
| 108 | * 1. Get your idle time. It will query XScreenSaver or Windows | |
| 15884 | 109 | * or use the Purple idle time. Whatever. |
| 12272 | 110 | * 2. Set or unset your auto-away message. |
| 111 | * 3. Report your current idle time to the IM server. | |
| 112 | */ | |
| 113 | static gint | |
| 114 | check_idleness() | |
| 115 | { | |
| 116 | time_t time_idle; | |
| 117 | gboolean auto_away; | |
|
12573
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
118 | const gchar *idle_reporting; |
| 12272 | 119 | gboolean report_idle; |
| 120 | GList *l; | |
| 121 | ||
| 15884 | 122 | purple_signal_emit(purple_blist_get_handle(), "update-idle"); |
| 12272 | 123 | |
|
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
|
124 | idle_reporting = purple_prefs_get_string("/purple/away/idle_reporting"); |
|
12573
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
125 | report_idle = TRUE; |
|
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
126 | if (!strcmp(idle_reporting, "system") && |
|
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
127 | (idle_ui_ops != NULL) && (idle_ui_ops->get_time_idle != NULL)) |
|
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
128 | { |
|
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
129 | /* Use system idle time (mouse or keyboard movement, etc.) */ |
| 12272 | 130 | time_idle = idle_ui_ops->get_time_idle(); |
|
12573
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
131 | } |
| 15884 | 132 | else if (!strcmp(idle_reporting, "purple")) |
|
12573
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
133 | { |
| 15884 | 134 | /* Use 'Purple idle' */ |
| 12272 | 135 | time_idle = time(NULL) - last_active_time; |
|
12573
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
136 | } |
|
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
137 | else |
|
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
138 | { |
|
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
139 | /* Don't report idle time */ |
|
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
140 | time_idle = 0; |
|
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
141 | report_idle = FALSE; |
|
1fc347b54974
[gaim-migrate @ 14895]
Mark Doliner <markdoliner@pidgin.im>
parents:
12412
diff
changeset
|
142 | } |
| 12272 | 143 | |
| 144 | /* 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
|
145 | 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
|
146 | |
|
7b3992f19766
[gaim-migrate @ 17709]
Daniel Atallah <datallah@pidgin.im>
parents:
14254
diff
changeset
|
147 | /* If we're not reporting idle, we can still do auto-away. |
| 15884 | 148 | * 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
|
149 | if (!report_idle && auto_away) { |
|
7b3992f19766
[gaim-migrate @ 17709]
Daniel Atallah <datallah@pidgin.im>
parents:
14254
diff
changeset
|
150 | 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
|
151 | time_idle = idle_ui_ops->get_time_idle(); |
|
7b3992f19766
[gaim-migrate @ 17709]
Daniel Atallah <datallah@pidgin.im>
parents:
14254
diff
changeset
|
152 | else |
|
7b3992f19766
[gaim-migrate @ 17709]
Daniel Atallah <datallah@pidgin.im>
parents:
14254
diff
changeset
|
153 | time_idle = time(NULL) - last_active_time; |
|
7b3992f19766
[gaim-migrate @ 17709]
Daniel Atallah <datallah@pidgin.im>
parents:
14254
diff
changeset
|
154 | } |
|
7b3992f19766
[gaim-migrate @ 17709]
Daniel Atallah <datallah@pidgin.im>
parents:
14254
diff
changeset
|
155 | |
| 12272 | 156 | if (auto_away && |
|
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
|
157 | (time_idle > (60 * purple_prefs_get_int("/purple/away/mins_before_away")))) |
| 12272 | 158 | { |
| 15884 | 159 | purple_savedstatus_set_idleaway(TRUE); |
| 12272 | 160 | } |
|
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
|
161 | else if (time_idle < 60 * purple_prefs_get_int("/purple/away/mins_before_away")) |
| 12272 | 162 | { |
| 15884 | 163 | purple_savedstatus_set_idleaway(FALSE); |
| 12272 | 164 | } |
| 165 | ||
| 166 | /* Idle reporting stuff */ | |
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
167 | if (report_idle && (time_idle >= IDLEMARK)) |
| 12272 | 168 | { |
| 15884 | 169 | 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
|
170 | { |
| 15884 | 171 | PurpleConnection *gc = l->data; |
| 172 | set_account_idle(purple_connection_get_account(gc), time_idle); | |
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
173 | } |
| 12272 | 174 | } |
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
175 | else if (!report_idle || (time_idle < IDLEMARK)) |
| 12272 | 176 | { |
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
177 | while (idled_accts != NULL) |
|
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
178 | set_account_unidle(idled_accts->data); |
| 12272 | 179 | } |
| 180 | ||
| 181 | return TRUE; | |
| 182 | } | |
| 183 | ||
| 184 | static void | |
| 15884 | 185 | im_msg_sent_cb(PurpleAccount *account, const char *receiver, |
| 12272 | 186 | const char *message, void *data) |
| 187 | { | |
| 188 | /* Check our idle time after an IM is sent */ | |
| 189 | check_idleness(); | |
| 190 | } | |
| 191 | ||
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
192 | static void |
| 15884 | 193 | signing_on_cb(PurpleConnection *gc, void *data) |
| 14189 | 194 | { |
| 195 | /* When signing on a new account, check if the account should be idle */ | |
| 196 | check_idleness(); | |
| 197 | } | |
| 198 | ||
| 199 | static void | |
| 15884 | 200 | signing_off_cb(PurpleConnection *gc, void *data) |
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
201 | { |
| 15884 | 202 | PurpleAccount *account; |
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
203 | |
| 15884 | 204 | account = purple_connection_get_account(gc); |
| 14189 | 205 | set_account_unidle(account); |
|
12825
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
206 | } |
|
0989792c930b
[gaim-migrate @ 15173]
Mark Doliner <markdoliner@pidgin.im>
parents:
12573
diff
changeset
|
207 | |
| 12272 | 208 | void |
| 15884 | 209 | purple_idle_touch() |
| 12272 | 210 | { |
| 211 | time(&last_active_time); | |
| 212 | } | |
| 213 | ||
| 214 | void | |
| 15884 | 215 | purple_idle_set(time_t time) |
| 12272 | 216 | { |
| 217 | last_active_time = time; | |
| 218 | } | |
| 219 | ||
| 220 | void | |
| 15884 | 221 | purple_idle_set_ui_ops(PurpleIdleUiOps *ops) |
| 12272 | 222 | { |
| 223 | idle_ui_ops = ops; | |
| 224 | } | |
| 225 | ||
| 15884 | 226 | PurpleIdleUiOps * |
| 227 | purple_idle_get_ui_ops(void) | |
| 12272 | 228 | { |
| 229 | return idle_ui_ops; | |
| 230 | } | |
| 231 | ||
|
12412
8abe3226695e
[gaim-migrate @ 14719]
Richard Laager <rlaager@pidgin.im>
parents:
12272
diff
changeset
|
232 | static void * |
| 15884 | 233 | purple_idle_get_handle() |
| 12272 | 234 | { |
| 235 | static int handle; | |
| 236 | ||
| 237 | return &handle; | |
| 238 | } | |
| 239 | ||
| 240 | void | |
| 15884 | 241 | purple_idle_init() |
| 12272 | 242 | { |
| 243 | /* Add the timer to check if we're idle */ | |
| 15884 | 244 | idle_timer = purple_timeout_add(IDLE_CHECK_INTERVAL * 1000, check_idleness, NULL); |
| 12272 | 245 | |
| 15884 | 246 | purple_signal_connect(purple_conversations_get_handle(), "sent-im-msg", |
| 247 | purple_idle_get_handle(), | |
| 248 | PURPLE_CALLBACK(im_msg_sent_cb), NULL); | |
| 249 | purple_signal_connect(purple_connections_get_handle(), "signing-on", | |
| 250 | purple_idle_get_handle(), | |
| 251 | PURPLE_CALLBACK(signing_on_cb), NULL); | |
| 252 | purple_signal_connect(purple_connections_get_handle(), "signing-off", | |
| 253 | purple_idle_get_handle(), | |
| 254 | PURPLE_CALLBACK(signing_off_cb), NULL); | |
| 12272 | 255 | |
| 15884 | 256 | purple_idle_touch(); |
| 12272 | 257 | } |
| 258 | ||
| 259 | void | |
| 15884 | 260 | purple_idle_uninit() |
| 12272 | 261 | { |
| 15884 | 262 | purple_signals_disconnect_by_handle(purple_idle_get_handle()); |
| 12272 | 263 | |
| 264 | /* Remove the idle timer */ | |
| 265 | if (idle_timer > 0) | |
| 15884 | 266 | purple_timeout_remove(idle_timer); |
| 12272 | 267 | idle_timer = 0; |
| 268 | } |