comparison libpurple/plugins/idle.c @ 15373:5fe8042783c1

Rename gtk/ and libgaim/ to pidgin/ and libpurple/
author Sean Egan <seanegan@gmail.com>
date Sat, 20 Jan 2007 02:32:10 +0000
parents
children 32c366eeeb99
comparison
equal deleted inserted replaced
15372:f79e0f4df793 15373:5fe8042783c1
1 /*
2 * idle.c - I'dle Mak'er plugin for Gaim
3 *
4 * This file is part of Gaim.
5 *
6 * Gaim is the legal property of its developers, whose names are too numerous
7 * to list here. Please refer to the COPYRIGHT file distributed with this
8 * source distribution.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25 #include "internal.h"
26
27 #include "connection.h"
28 #include "debug.h"
29 #include "notify.h"
30 #include "plugin.h"
31 #include "request.h"
32 #include "server.h"
33 #include "status.h"
34 #include "version.h"
35
36 /* This plugin no longer depends on gtk */
37 #define IDLE_PLUGIN_ID "core-idle"
38
39 static GList *idled_accts = NULL;
40
41 static gboolean
42 unidle_filter(GaimAccount *acct)
43 {
44 if (g_list_find(idled_accts, acct))
45 return TRUE;
46
47 return FALSE;
48 }
49
50 static gboolean
51 idleable_filter(GaimAccount *account)
52 {
53 GaimPlugin *prpl;
54
55 prpl = gaim_find_prpl(gaim_account_get_protocol_id(account));
56 g_return_val_if_fail(prpl != NULL, FALSE);
57
58 return (GAIM_PLUGIN_PROTOCOL_INFO(prpl)->set_idle != NULL);
59 }
60
61 static void
62 set_idle_time(GaimAccount *acct, int mins_idle)
63 {
64 time_t t;
65 GaimConnection *gc = gaim_account_get_connection(acct);
66 GaimPresence *presence = gaim_account_get_presence(acct);
67
68 if (!gc)
69 return;
70
71 gaim_debug_info("idle",
72 "setting idle time for %s to %d\n",
73 gaim_account_get_username(acct), mins_idle);
74
75 if (mins_idle)
76 t = time(NULL) - (60 * mins_idle); /* subtract seconds idle from current time */
77 else
78 t = 0; /* time idle is irrelevant */
79
80 gaim_presence_set_idle(presence, mins_idle ? TRUE : FALSE, t);
81 }
82
83 static void
84 idle_action_ok(void *ignored, GaimRequestFields *fields)
85 {
86 int tm = gaim_request_fields_get_integer(fields, "mins");
87 GaimAccount *acct = gaim_request_fields_get_account(fields, "acct");
88
89 /* only add the account to the GList if it's not already been idled */
90 if (!unidle_filter(acct))
91 {
92 gaim_debug_misc("idle",
93 "%s hasn't been idled yet; adding to list.\n",
94 gaim_account_get_username(acct));
95 idled_accts = g_list_append(idled_accts, acct);
96 }
97
98 set_idle_time(acct, tm);
99 }
100
101 static void
102 idle_all_action_ok(void *ignored, GaimRequestFields *fields)
103 {
104 GaimAccount *acct = NULL;
105 GList *list, *iter;
106 int tm = gaim_request_fields_get_integer(fields, "mins");
107 const char *prpl_id = NULL;
108
109 list = gaim_accounts_get_all_active();
110 for(iter = list; iter; iter = iter->next) {
111 acct = (GaimAccount *)(iter->data);
112
113 if(acct)
114 prpl_id = gaim_account_get_protocol_id(acct);
115
116 if(acct && idleable_filter(acct)) {
117 gaim_debug_misc("idle", "Idling %s.\n",
118 gaim_account_get_username(acct));
119
120 set_idle_time(acct, tm);
121
122 if(!g_list_find(idled_accts, acct))
123 idled_accts = g_list_append(idled_accts, acct);
124 }
125 }
126
127 g_list_free(list);
128 }
129
130 static void
131 unidle_action_ok(void *ignored, GaimRequestFields *fields)
132 {
133 GaimAccount *acct = gaim_request_fields_get_account(fields, "acct");
134
135 set_idle_time(acct, 0); /* unidle the account */
136
137 /* once the account has been unidled it shouldn't be in the list */
138 idled_accts = g_list_remove(idled_accts, acct);
139 }
140
141
142 static void
143 idle_action(GaimPluginAction *action)
144 {
145 /* Use the super fancy request API */
146
147 GaimRequestFields *request;
148 GaimRequestFieldGroup *group;
149 GaimRequestField *field;
150
151 group = gaim_request_field_group_new(NULL);
152
153 field = gaim_request_field_account_new("acct", _("Account"), NULL);
154 gaim_request_field_account_set_filter(field, idleable_filter);
155 gaim_request_field_account_set_show_all(field, FALSE);
156 gaim_request_field_group_add_field(group, field);
157
158 field = gaim_request_field_int_new("mins", _("Minutes"), 10);
159 gaim_request_field_group_add_field(group, field);
160
161 request = gaim_request_fields_new();
162 gaim_request_fields_add_group(request, group);
163
164 gaim_request_fields(action->plugin,
165 N_("I'dle Mak'er"),
166 _("Set Account Idle Time"),
167 NULL,
168 request,
169 _("_Set"), G_CALLBACK(idle_action_ok),
170 _("_Cancel"), NULL,
171 NULL);
172 }
173
174 static void
175 unidle_action(GaimPluginAction *action)
176 {
177 GaimRequestFields *request;
178 GaimRequestFieldGroup *group;
179 GaimRequestField *field;
180
181 if (idled_accts == NULL)
182 {
183 gaim_notify_info(NULL, NULL, _("None of your accounts are idle."), NULL);
184 return;
185 }
186
187 group = gaim_request_field_group_new(NULL);
188
189 field = gaim_request_field_account_new("acct", _("Account"), NULL);
190 gaim_request_field_account_set_filter(field, unidle_filter);
191 gaim_request_field_account_set_show_all(field, FALSE);
192 gaim_request_field_group_add_field(group, field);
193
194 request = gaim_request_fields_new();
195 gaim_request_fields_add_group(request, group);
196
197 gaim_request_fields(action->plugin,
198 N_("I'dle Mak'er"),
199 _("Unset Account Idle Time"),
200 NULL,
201 request,
202 _("_Unset"), G_CALLBACK(unidle_action_ok),
203 _("_Cancel"), NULL,
204 NULL);
205 }
206
207 static void
208 idle_all_action(GaimPluginAction *action)
209 {
210 GaimRequestFields *request;
211 GaimRequestFieldGroup *group;
212 GaimRequestField *field;
213
214 group = gaim_request_field_group_new(NULL);
215
216 field = gaim_request_field_int_new("mins", _("Minutes"), 10);
217 gaim_request_field_group_add_field(group, field);
218
219 request = gaim_request_fields_new();
220 gaim_request_fields_add_group(request, group);
221
222 gaim_request_fields(action->plugin,
223 N_("I'dle Mak'er"),
224 _("Set Idle Time for All Accounts"),
225 NULL,
226 request,
227 _("_Set"), G_CALLBACK(idle_all_action_ok),
228 _("_Cancel"), NULL,
229 NULL);
230 }
231
232 static void
233 unidle_all_action(GaimPluginAction *action)
234 {
235 GList *l;
236
237 /* freeing the list here will cause segfaults if the user idles an account
238 * after the list is freed */
239 for (l = idled_accts; l; l = l->next)
240 {
241 GaimAccount *account = l->data;
242 set_idle_time(account, 0);
243 }
244
245 g_list_free(idled_accts);
246 idled_accts = NULL;
247 }
248
249 static GList *
250 actions(GaimPlugin *plugin, gpointer context)
251 {
252 GList *l = NULL;
253 GaimPluginAction *act = NULL;
254
255 act = gaim_plugin_action_new(_("Set Account Idle Time"),
256 idle_action);
257 l = g_list_append(l, act);
258
259 act = gaim_plugin_action_new(_("Unset Account Idle Time"),
260 unidle_action);
261 l = g_list_append(l, act);
262
263 act = gaim_plugin_action_new(_("Set Idle Time for All Accounts"),
264 idle_all_action);
265 l = g_list_append(l, act);
266
267 act = gaim_plugin_action_new(
268 _("Unset Idle Time for All Idled Accounts"), unidle_all_action);
269 l = g_list_append(l, act);
270
271 return l;
272 }
273
274 static void
275 signing_off_cb(GaimConnection *gc, void *data)
276 {
277 GaimAccount *account;
278
279 account = gaim_connection_get_account(gc);
280 idled_accts = g_list_remove(idled_accts, account);
281 }
282
283 static gboolean
284 plugin_load(GaimPlugin *plugin)
285 {
286 gaim_signal_connect(gaim_connections_get_handle(), "signing-off",
287 plugin,
288 GAIM_CALLBACK(signing_off_cb), NULL);
289
290 return TRUE;
291 }
292
293 static gboolean
294 plugin_unload(GaimPlugin *plugin)
295 {
296 unidle_all_action(NULL);
297
298 return TRUE;
299 }
300
301 static GaimPluginInfo info =
302 {
303 GAIM_PLUGIN_MAGIC,
304 GAIM_MAJOR_VERSION,
305 GAIM_MINOR_VERSION,
306 GAIM_PLUGIN_STANDARD,
307 NULL,
308 0,
309 NULL,
310 GAIM_PRIORITY_DEFAULT,
311 IDLE_PLUGIN_ID,
312
313 /* This is a cultural reference. Dy'er Mak'er is a song by Led Zeppelin.
314 If that doesn't translate well into your language, drop the 's before translating. */
315 N_("I'dle Mak'er"),
316 VERSION,
317 N_("Allows you to hand-configure how long you've been idle"),
318 N_("Allows you to hand-configure how long you've been idle"),
319 "Eric Warmenhoven <eric@warmenhoven.org>",
320 GAIM_WEBSITE,
321 plugin_load,
322 plugin_unload,
323 NULL,
324 NULL,
325 NULL,
326 NULL,
327 actions
328 };
329
330 static void
331 init_plugin(GaimPlugin *plugin)
332 {
333 }
334
335
336 GAIM_INIT_PLUGIN(idle, init_plugin, info)
337