Fix winprefs plugin for PurpleBuddyList changes.
[pidgin-git.git] / libpurple / plugins / idle.c
blob46bd4b0c8dde5467b6d3ef2bbea69b7679c58f15
1 /*
2 * idle.c - I'dle Mak'er plugin for Purple
4 * This file is part of Purple.
6 * Purple 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.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
25 #include "internal.h"
27 #include "connection.h"
28 #include "debug.h"
29 #include "notify.h"
30 #include "plugins.h"
31 #include "presence.h"
32 #include "request.h"
33 #include "server.h"
34 #include "status.h"
35 #include "version.h"
37 /* This plugin no longer depends on gtk */
38 #define IDLE_PLUGIN_ID "core-idle"
40 static GList *idled_accts = NULL;
42 static gboolean
43 unidle_filter(PurpleAccount *acct)
45 if (g_list_find(idled_accts, acct))
46 return TRUE;
48 return FALSE;
51 static gboolean
52 idleable_filter(PurpleAccount *account)
54 PurpleProtocol *protocol;
56 protocol = purple_protocols_find(purple_account_get_protocol_id(account));
57 g_return_val_if_fail(protocol != NULL, FALSE);
59 return PURPLE_PROTOCOL_IMPLEMENTS(protocol, SERVER, set_idle);
62 static void
63 set_idle_time(PurpleAccount *acct, int mins_idle)
65 time_t t;
66 PurpleConnection *gc = purple_account_get_connection(acct);
67 PurplePresence *presence = purple_account_get_presence(acct);
69 if (!gc)
70 return;
72 purple_debug_info("idle",
73 "setting idle time for %s to %d\n",
74 purple_account_get_username(acct), mins_idle);
76 if (mins_idle)
77 t = time(NULL) - (60 * mins_idle); /* subtract seconds idle from current time */
78 else
79 t = 0; /* time idle is irrelevant */
81 purple_presence_set_idle(presence, mins_idle ? TRUE : FALSE, t);
84 static void
85 idle_action_ok(void *ignored, PurpleRequestFields *fields)
87 int tm = purple_request_fields_get_integer(fields, "mins");
88 PurpleAccount *acct = purple_request_fields_get_account(fields, "acct");
90 /* only add the account to the GList if it's not already been idled */
91 if (!unidle_filter(acct))
93 purple_debug_misc("idle",
94 "%s hasn't been idled yet; adding to list.\n",
95 purple_account_get_username(acct));
96 idled_accts = g_list_append(idled_accts, acct);
99 set_idle_time(acct, tm);
102 static void
103 idle_all_action_ok(void *ignored, PurpleRequestFields *fields)
105 PurpleAccount *acct = NULL;
106 GList *list, *iter;
107 int tm = purple_request_fields_get_integer(fields, "mins");
109 list = purple_accounts_get_all_active();
110 for(iter = list; iter; iter = iter->next) {
111 acct = (PurpleAccount *)(iter->data);
113 if(acct && idleable_filter(acct)) {
114 purple_debug_misc("idle", "Idling %s.\n",
115 purple_account_get_username(acct));
117 set_idle_time(acct, tm);
119 if(!g_list_find(idled_accts, acct))
120 idled_accts = g_list_append(idled_accts, acct);
124 g_list_free(list);
127 static void
128 unidle_action_ok(void *ignored, PurpleRequestFields *fields)
130 PurpleAccount *acct = purple_request_fields_get_account(fields, "acct");
132 set_idle_time(acct, 0); /* unidle the account */
134 /* once the account has been unidled it shouldn't be in the list */
135 idled_accts = g_list_remove(idled_accts, acct);
139 static void
140 idle_action(PurplePluginAction *action)
142 /* Use the super fancy request API */
144 PurpleRequestFields *request;
145 PurpleRequestFieldGroup *group;
146 PurpleRequestField *field;
148 group = purple_request_field_group_new(NULL);
150 field = purple_request_field_account_new("acct", _("Account"), NULL);
151 purple_request_field_account_set_filter(field, idleable_filter);
152 purple_request_field_account_set_show_all(field, FALSE);
153 purple_request_field_group_add_field(group, field);
155 field = purple_request_field_int_new("mins", _("Minutes"), 10, 0, 9999);
156 purple_request_field_group_add_field(group, field);
158 request = purple_request_fields_new();
159 purple_request_fields_add_group(request, group);
161 purple_request_fields(action->plugin,
162 N_("I'dle Mak'er"),
163 _("Set Account Idle Time"),
164 NULL,
165 request,
166 _("_Set"), G_CALLBACK(idle_action_ok),
167 _("_Cancel"), NULL,
168 NULL, NULL);
171 static void
172 unidle_action(PurplePluginAction *action)
174 PurpleRequestFields *request;
175 PurpleRequestFieldGroup *group;
176 PurpleRequestField *field;
178 if (idled_accts == NULL)
180 purple_notify_info(NULL, NULL, _("None of your accounts are idle."), NULL, NULL);
181 return;
184 group = purple_request_field_group_new(NULL);
186 field = purple_request_field_account_new("acct", _("Account"), NULL);
187 purple_request_field_account_set_filter(field, unidle_filter);
188 purple_request_field_account_set_show_all(field, FALSE);
189 purple_request_field_group_add_field(group, field);
191 request = purple_request_fields_new();
192 purple_request_fields_add_group(request, group);
194 purple_request_fields(action->plugin,
195 N_("I'dle Mak'er"),
196 _("Unset Account Idle Time"),
197 NULL,
198 request,
199 _("_Unset"), G_CALLBACK(unidle_action_ok),
200 _("_Cancel"), NULL,
201 NULL, NULL);
204 static void
205 idle_all_action(PurplePluginAction *action)
207 PurpleRequestFields *request;
208 PurpleRequestFieldGroup *group;
209 PurpleRequestField *field;
211 group = purple_request_field_group_new(NULL);
213 field = purple_request_field_int_new("mins", _("Minutes"), 10, 0, 9999);
214 purple_request_field_group_add_field(group, field);
216 request = purple_request_fields_new();
217 purple_request_fields_add_group(request, group);
219 purple_request_fields(action->plugin,
220 N_("I'dle Mak'er"),
221 _("Set Idle Time for All Accounts"),
222 NULL,
223 request,
224 _("_Set"), G_CALLBACK(idle_all_action_ok),
225 _("_Cancel"), NULL,
226 NULL, NULL);
229 static void
230 unidle_all_action(PurplePluginAction *action)
232 GList *l;
234 /* freeing the list here will cause segfaults if the user idles an account
235 * after the list is freed */
236 for (l = idled_accts; l; l = l->next)
238 PurpleAccount *account = l->data;
239 set_idle_time(account, 0);
242 g_list_free(idled_accts);
243 idled_accts = NULL;
246 static GList *
247 actions(PurplePlugin *plugin)
249 GList *l = NULL;
250 PurplePluginAction *act = NULL;
252 act = purple_plugin_action_new(_("Set Account Idle Time"),
253 idle_action);
254 l = g_list_append(l, act);
256 act = purple_plugin_action_new(_("Unset Account Idle Time"),
257 unidle_action);
258 l = g_list_append(l, act);
260 act = purple_plugin_action_new(_("Set Idle Time for All Accounts"),
261 idle_all_action);
262 l = g_list_append(l, act);
264 act = purple_plugin_action_new(
265 _("Unset Idle Time for All Idled Accounts"), unidle_all_action);
266 l = g_list_append(l, act);
268 return l;
271 static void
272 signing_off_cb(PurpleConnection *gc, void *data)
274 PurpleAccount *account;
276 account = purple_connection_get_account(gc);
277 idled_accts = g_list_remove(idled_accts, account);
280 static PurplePluginInfo *
281 plugin_query(GError **error)
283 const gchar * const authors[] = {
284 "Eric Warmenhoven <eric@warmenhoven.org>",
285 NULL
288 return purple_plugin_info_new(
289 "id", IDLE_PLUGIN_ID,
290 /* This is a cultural reference. Dy'er Mak'er is a song by Led Zeppelin.
291 If that doesn't translate well into your language, drop the 's before translating. */
292 "name", N_("I'dle Mak'er"),
293 "version", DISPLAY_VERSION,
294 "category", N_("Utility"),
295 "summary", N_("Allows you to hand-configure how long you've been idle"),
296 "description", N_("Allows you to hand-configure how long you've been idle"),
297 "authors", authors,
298 "website", PURPLE_WEBSITE,
299 "abi-version", PURPLE_ABI_VERSION,
300 "actions-cb", actions,
301 NULL
305 static gboolean
306 plugin_load(PurplePlugin *plugin, GError **error)
308 purple_signal_connect(purple_connections_get_handle(), "signing-off",
309 plugin,
310 PURPLE_CALLBACK(signing_off_cb), NULL);
312 return TRUE;
315 static gboolean
316 plugin_unload(PurplePlugin *plugin, GError **error)
318 unidle_all_action(NULL);
320 return TRUE;
323 PURPLE_PLUGIN_INIT(idle, plugin_query, plugin_load, plugin_unload);