annotate src/idle.c @ 14019:19a1812e6c46

[gaim-migrate @ 16615] Fix a crash. committer: Tailor Script <tailor@pidgin.im>
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Wed, 02 Aug 2006 06:24:38 +0000
parents 4997b8b66206
children 9a4b76c288aa
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12272
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
1 /*
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
2 * gaim
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
3 *
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
4 * Gaim is the legal property of its developers, whose names are too numerous
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
5 * to list here. Please refer to the COPYRIGHT file distributed with this
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
6 * source distribution.
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
7 *
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
8 * This program is free software; you can redistribute it and/or modify
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
9 * it under the terms of the GNU General Public License as published by
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
10 * the Free Software Foundation; either version 2 of the License, or
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
11 * (at your option) any later version.
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
12 *
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful,
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
16 * GNU General Public License for more details.
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
17 *
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
19 * along with this program; if not, write to the Free Software
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
21 *
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
22 */
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
23 #include "internal.h"
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
24
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
25 #include "connection.h"
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
26 #include "debug.h"
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
27 #include "idle.h"
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
28 #include "log.h"
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
29 #include "prefs.h"
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
30 #include "savedstatuses.h"
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
31 #include "signals.h"
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
32
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
33 #define IDLEMARK 600 /* 10 minutes! */
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
34 #define IDLE_CHECK_INTERVAL 5 /* 5 seconds */
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
35
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
36 typedef enum
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
37 {
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
38 GAIM_IDLE_NOT_AWAY = 0,
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
39 GAIM_IDLE_AUTO_AWAY,
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
40 GAIM_IDLE_AWAY_BUT_NOT_AUTO_AWAY
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
41
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
42 } GaimAutoAwayState;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
43
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
44 static GaimIdleUiOps *idle_ui_ops = NULL;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
45
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
46 /**
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
47 * This is needed for the I'dle Mak'er plugin to work correctly. We
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
48 * use it to determine if we're the ones who set our accounts idle
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
49 * or if someone else did it (the I'dle Mak'er plugin, for example).
12825
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
50 * Basically we just keep track of which accounts were set idle by us,
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
51 * and then we'll only set these specific accounts unidle when the
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
52 * user returns.
12272
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
53 */
12825
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
54 static GList *idled_accts = NULL;
12272
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
55
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
56 static guint idle_timer = 0;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
57
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
58 static time_t last_active_time = 0;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
59
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
60 static void
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
61 set_account_autoaway(GaimConnection *gc)
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
62 {
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
63 GaimAccount *account;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
64 GaimPresence *presence;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
65 GaimStatus *status;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
66
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
67 if (gc->is_auto_away)
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
68 /* This account is already auto-away! */
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
69 return;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
70
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
71 account = gaim_connection_get_account(gc);
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
72 presence = gaim_account_get_presence(account);
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
73 status = gaim_presence_get_active_status(presence);
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
74
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
75 if (gaim_status_is_available(status))
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
76 {
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
77 GaimSavedStatus *saved_status;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
78
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
79 gaim_debug_info("idle", "Making %s auto-away\n",
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
80 gaim_account_get_username(account));
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
81
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
82 saved_status = gaim_savedstatus_get_idleaway();
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
83 gaim_savedstatus_activate_for_account(saved_status, account);
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
84
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
85 gc->is_auto_away = GAIM_IDLE_AUTO_AWAY;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
86 } else {
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
87 gc->is_auto_away = GAIM_IDLE_AWAY_BUT_NOT_AUTO_AWAY;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
88 }
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
89 }
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
90
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
91 static void
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
92 unset_account_autoaway(GaimConnection *gc)
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
93 {
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
94 GaimAccount *account;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
95 GaimSavedStatus *saved_status;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
96
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
97 account = gaim_connection_get_account(gc);
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
98
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
99 if (!gc->is_auto_away)
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
100 /* This account is already not auto-away! */
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
101 return;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
102
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
103 if (gc->is_auto_away == GAIM_IDLE_AWAY_BUT_NOT_AUTO_AWAY) {
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
104 gc->is_auto_away = GAIM_IDLE_NOT_AWAY;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
105 } else {
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
106 gc->is_auto_away = GAIM_IDLE_NOT_AWAY;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
107
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
108 gaim_debug_info("idle", "%s returning from auto-away\n",
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
109 gaim_account_get_username(account));
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
110
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
111 /* Return our account to its previous status */
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
112 saved_status = gaim_savedstatus_get_current();
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
113 gaim_savedstatus_activate_for_account(saved_status, account);
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
114 }
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
115 }
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
116
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
117 static void
12825
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
118 set_account_idle(GaimAccount *account, int time_idle)
12272
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
119 {
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
120 GaimPresence *presence;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
121
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
122 presence = gaim_account_get_presence(account);
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
123
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
124 if (gaim_presence_is_idle(presence))
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
125 /* This account is already idle! */
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
126 return;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
127
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
128 gaim_debug_info("idle", "Setting %s idle %d seconds\n",
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
129 gaim_account_get_username(account), time_idle);
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
130 gaim_presence_set_idle(presence, TRUE, time(NULL) - time_idle);
12825
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
131 idled_accts = g_list_prepend(idled_accts, account);
12272
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
132 }
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
133
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
134 static void
12825
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
135 set_account_unidle(GaimAccount *account)
12272
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
136 {
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
137 GaimPresence *presence;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
138
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
139 presence = gaim_account_get_presence(account);
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
140
12825
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
141 idled_accts = g_list_remove(idled_accts, account);
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
142
12272
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
143 if (!gaim_presence_is_idle(presence))
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
144 /* This account is already unidle! */
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
145 return;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
146
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
147 gaim_debug_info("idle", "Setting %s unidle\n",
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
148 gaim_account_get_username(account));
13722
4997b8b66206 [gaim-migrate @ 16131]
Mark Doliner <mark@kingant.net>
parents: 13667
diff changeset
149 gaim_presence_set_idle(presence, FALSE, 0);
12272
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
150 }
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
151
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
152 /*
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
153 * This function should be called when you think your idle state
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
154 * may have changed. Maybe you're over the 10-minute mark and
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
155 * Gaim should start reporting idle time to the server. Maybe
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
156 * you've returned from being idle. Maybe your auto-away message
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
157 * should be set.
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
158 *
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
159 * There is no harm to calling this many many times, other than
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
160 * it will be kinda slow. This is called every 5 seconds by a
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
161 * timer set when Gaim starts. It is also called when
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
162 * you send an IM, a chat, etc.
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
163 *
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
164 * This function has 3 sections.
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
165 * 1. Get your idle time. It will query XScreenSaver or Windows
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
166 * or use the Gaim idle time. Whatever.
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
167 * 2. Set or unset your auto-away message.
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
168 * 3. Report your current idle time to the IM server.
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
169 */
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
170 static gint
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
171 check_idleness()
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
172 {
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
173 time_t time_idle;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
174 gboolean auto_away;
12573
200f22ca4890 [gaim-migrate @ 14895]
Mark Doliner <mark@kingant.net>
parents: 12412
diff changeset
175 const gchar *idle_reporting;
12272
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
176 gboolean report_idle;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
177 GList *l;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
178
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
179 gaim_signal_emit(gaim_blist_get_handle(), "update-idle");
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
180
12573
200f22ca4890 [gaim-migrate @ 14895]
Mark Doliner <mark@kingant.net>
parents: 12412
diff changeset
181 idle_reporting = gaim_prefs_get_string("/core/away/idle_reporting");
200f22ca4890 [gaim-migrate @ 14895]
Mark Doliner <mark@kingant.net>
parents: 12412
diff changeset
182 report_idle = TRUE;
200f22ca4890 [gaim-migrate @ 14895]
Mark Doliner <mark@kingant.net>
parents: 12412
diff changeset
183 if (!strcmp(idle_reporting, "system") &&
200f22ca4890 [gaim-migrate @ 14895]
Mark Doliner <mark@kingant.net>
parents: 12412
diff changeset
184 (idle_ui_ops != NULL) && (idle_ui_ops->get_time_idle != NULL))
200f22ca4890 [gaim-migrate @ 14895]
Mark Doliner <mark@kingant.net>
parents: 12412
diff changeset
185 {
200f22ca4890 [gaim-migrate @ 14895]
Mark Doliner <mark@kingant.net>
parents: 12412
diff changeset
186 /* Use system idle time (mouse or keyboard movement, etc.) */
12272
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
187 time_idle = idle_ui_ops->get_time_idle();
12573
200f22ca4890 [gaim-migrate @ 14895]
Mark Doliner <mark@kingant.net>
parents: 12412
diff changeset
188 }
200f22ca4890 [gaim-migrate @ 14895]
Mark Doliner <mark@kingant.net>
parents: 12412
diff changeset
189 else if (!strcmp(idle_reporting, "gaim"))
200f22ca4890 [gaim-migrate @ 14895]
Mark Doliner <mark@kingant.net>
parents: 12412
diff changeset
190 {
12272
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
191 /* Use 'Gaim idle' */
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
192 time_idle = time(NULL) - last_active_time;
12573
200f22ca4890 [gaim-migrate @ 14895]
Mark Doliner <mark@kingant.net>
parents: 12412
diff changeset
193 }
200f22ca4890 [gaim-migrate @ 14895]
Mark Doliner <mark@kingant.net>
parents: 12412
diff changeset
194 else
200f22ca4890 [gaim-migrate @ 14895]
Mark Doliner <mark@kingant.net>
parents: 12412
diff changeset
195 {
200f22ca4890 [gaim-migrate @ 14895]
Mark Doliner <mark@kingant.net>
parents: 12412
diff changeset
196 /* Don't report idle time */
200f22ca4890 [gaim-migrate @ 14895]
Mark Doliner <mark@kingant.net>
parents: 12412
diff changeset
197 time_idle = 0;
200f22ca4890 [gaim-migrate @ 14895]
Mark Doliner <mark@kingant.net>
parents: 12412
diff changeset
198 report_idle = FALSE;
200f22ca4890 [gaim-migrate @ 14895]
Mark Doliner <mark@kingant.net>
parents: 12412
diff changeset
199 }
12272
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
200
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
201 /* Auto-away stuff */
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
202 auto_away = gaim_prefs_get_bool("/core/away/away_when_idle");
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
203 if (auto_away &&
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
204 (time_idle > (60 * gaim_prefs_get_int("/core/away/mins_before_away"))))
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
205 {
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
206 for (l = gaim_connections_get_all(); l != NULL; l = l->next)
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
207 set_account_autoaway(l->data);
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
208 }
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
209 else if (time_idle < 60 * gaim_prefs_get_int("/core/away/mins_before_away"))
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
210 {
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
211 for (l = gaim_connections_get_all(); l != NULL; l = l->next)
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
212 unset_account_autoaway(l->data);
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
213 }
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
214
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
215 /* Idle reporting stuff */
12825
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
216 if (report_idle && (time_idle >= IDLEMARK))
12272
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
217 {
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
218 for (l = gaim_connections_get_all(); l != NULL; l = l->next)
12825
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
219 {
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
220 GaimConnection *gc = l->data;
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
221 set_account_idle(gaim_connection_get_account(gc), time_idle);
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
222 }
12272
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
223 }
12825
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
224 else if (!report_idle || (time_idle < IDLEMARK))
12272
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
225 {
12825
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
226 while (idled_accts != NULL)
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
227 set_account_unidle(idled_accts->data);
12272
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
228 }
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
229
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
230 return TRUE;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
231 }
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
232
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
233 static void
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
234 im_msg_sent_cb(GaimAccount *account, const char *receiver,
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
235 const char *message, void *data)
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
236 {
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
237 /* Check our idle time after an IM is sent */
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
238 check_idleness();
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
239 }
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
240
12825
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
241 static void
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
242 signing_off_cb(GaimConnection *gc, void *data)
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
243 {
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
244 GaimAccount *account;
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
245
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
246 account = gaim_connection_get_account(gc);
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
247 idled_accts = g_list_remove(idled_accts, account);
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
248 }
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
249
12272
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
250 void
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
251 gaim_idle_touch()
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
252 {
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
253 time(&last_active_time);
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
254 }
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
255
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
256 void
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
257 gaim_idle_set(time_t time)
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
258 {
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
259 last_active_time = time;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
260 }
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
261
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
262 void
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
263 gaim_idle_set_ui_ops(GaimIdleUiOps *ops)
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
264 {
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
265 idle_ui_ops = ops;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
266 }
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
267
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
268 GaimIdleUiOps *
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
269 gaim_idle_get_ui_ops(void)
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
270 {
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
271 return idle_ui_ops;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
272 }
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
273
12412
a88ca6da0b38 [gaim-migrate @ 14719]
Richard Laager <rlaager@wiktel.com>
parents: 12272
diff changeset
274 static void *
12272
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
275 gaim_idle_get_handle()
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
276 {
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
277 static int handle;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
278
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
279 return &handle;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
280 }
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
281
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
282 void
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
283 gaim_idle_init()
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
284 {
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
285 /* Add the timer to check if we're idle */
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
286 idle_timer = gaim_timeout_add(IDLE_CHECK_INTERVAL * 1000, check_idleness, NULL);
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
287
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
288 gaim_signal_connect(gaim_conversations_get_handle(), "sent-im-msg",
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
289 gaim_idle_get_handle(),
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
290 GAIM_CALLBACK(im_msg_sent_cb), NULL);
12825
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
291 gaim_signal_connect(gaim_connections_get_handle(), "signing-off",
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
292 gaim_idle_get_handle(),
bd80fb1e8406 [gaim-migrate @ 15173]
Mark Doliner <mark@kingant.net>
parents: 12573
diff changeset
293 GAIM_CALLBACK(signing_off_cb), NULL);
12272
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
294
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
295 gaim_idle_touch();
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
296 }
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
297
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
298 void
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
299 gaim_idle_uninit()
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
300 {
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
301 gaim_signals_disconnect_by_handle(gaim_idle_get_handle());
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
302
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
303 /* Remove the idle timer */
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
304 if (idle_timer > 0)
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
305 gaim_timeout_remove(idle_timer);
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
306 idle_timer = 0;
3ef381cdc47e [gaim-migrate @ 14574]
Mark Doliner <mark@kingant.net>
parents:
diff changeset
307 }