Replace functions which called once with their bodies
[pidgin-git.git] / libpurple / plugins / idle.c
blob894aa5af2f05ab3c495ddb81a47a4e80e2fca012
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 <purple.h>
29 /* This plugin no longer depends on gtk */
30 #define IDLE_PLUGIN_ID "core-idle"
32 static GList *idled_accts = NULL;
34 static gboolean
35 unidle_filter(PurpleAccount *acct)
37 if (g_list_find(idled_accts, acct))
38 return TRUE;
40 return FALSE;
43 static gboolean
44 idleable_filter(PurpleAccount *account)
46 PurpleProtocol *protocol;
48 protocol = purple_protocols_find(purple_account_get_protocol_id(account));
49 g_return_val_if_fail(protocol != NULL, FALSE);
51 return PURPLE_PROTOCOL_IMPLEMENTS(protocol, SERVER, set_idle);
54 static void
55 set_idle_time(PurpleAccount *acct, int mins_idle)
57 time_t t;
58 PurpleConnection *gc = purple_account_get_connection(acct);
59 PurplePresence *presence = purple_account_get_presence(acct);
61 if (!gc)
62 return;
64 purple_debug_info("idle",
65 "setting idle time for %s to %d\n",
66 purple_account_get_username(acct), mins_idle);
68 if (mins_idle)
69 t = time(NULL) - (60 * mins_idle); /* subtract seconds idle from current time */
70 else
71 t = 0; /* time idle is irrelevant */
73 purple_presence_set_idle(presence, mins_idle ? TRUE : FALSE, t);
76 static void
77 idle_action_ok(void *ignored, PurpleRequestFields *fields)
79 int tm = purple_request_fields_get_integer(fields, "mins");
80 PurpleAccount *acct = purple_request_fields_get_account(fields, "acct");
82 /* only add the account to the GList if it's not already been idled */
83 if (!unidle_filter(acct))
85 purple_debug_misc("idle",
86 "%s hasn't been idled yet; adding to list.\n",
87 purple_account_get_username(acct));
88 idled_accts = g_list_append(idled_accts, acct);
91 set_idle_time(acct, tm);
94 static void
95 idle_all_action_ok(void *ignored, PurpleRequestFields *fields)
97 PurpleAccount *acct = NULL;
98 GList *list, *iter;
99 int tm = purple_request_fields_get_integer(fields, "mins");
101 list = purple_accounts_get_all_active();
102 for(iter = list; iter; iter = iter->next) {
103 acct = (PurpleAccount *)(iter->data);
105 if(acct && idleable_filter(acct)) {
106 purple_debug_misc("idle", "Idling %s.\n",
107 purple_account_get_username(acct));
109 set_idle_time(acct, tm);
111 if(!g_list_find(idled_accts, acct))
112 idled_accts = g_list_append(idled_accts, acct);
116 g_list_free(list);
119 static void
120 unidle_action_ok(void *ignored, PurpleRequestFields *fields)
122 PurpleAccount *acct = purple_request_fields_get_account(fields, "acct");
124 set_idle_time(acct, 0); /* unidle the account */
126 /* once the account has been unidled it shouldn't be in the list */
127 idled_accts = g_list_remove(idled_accts, acct);
131 static void
132 idle_action(PurplePluginAction *action)
134 /* Use the super fancy request API */
136 PurpleRequestFields *request;
137 PurpleRequestFieldGroup *group;
138 PurpleRequestField *field;
140 group = purple_request_field_group_new(NULL);
142 field = purple_request_field_account_new("acct", _("Account"), NULL);
143 purple_request_field_account_set_filter(field, idleable_filter);
144 purple_request_field_account_set_show_all(field, FALSE);
145 purple_request_field_group_add_field(group, field);
147 field = purple_request_field_int_new("mins", _("Minutes"), 10, 0, 9999);
148 purple_request_field_group_add_field(group, field);
150 request = purple_request_fields_new();
151 purple_request_fields_add_group(request, group);
153 purple_request_fields(action->plugin,
154 N_("I'dle Mak'er"),
155 _("Set Account Idle Time"),
156 NULL,
157 request,
158 _("_Set"), G_CALLBACK(idle_action_ok),
159 _("_Cancel"), NULL,
160 NULL, NULL);
163 static void
164 unidle_action(PurplePluginAction *action)
166 PurpleRequestFields *request;
167 PurpleRequestFieldGroup *group;
168 PurpleRequestField *field;
170 if (idled_accts == NULL)
172 purple_notify_info(NULL, NULL, _("None of your accounts are idle."), NULL, NULL);
173 return;
176 group = purple_request_field_group_new(NULL);
178 field = purple_request_field_account_new("acct", _("Account"), NULL);
179 purple_request_field_account_set_filter(field, unidle_filter);
180 purple_request_field_account_set_show_all(field, FALSE);
181 purple_request_field_group_add_field(group, field);
183 request = purple_request_fields_new();
184 purple_request_fields_add_group(request, group);
186 purple_request_fields(action->plugin,
187 N_("I'dle Mak'er"),
188 _("Unset Account Idle Time"),
189 NULL,
190 request,
191 _("_Unset"), G_CALLBACK(unidle_action_ok),
192 _("_Cancel"), NULL,
193 NULL, NULL);
196 static void
197 idle_all_action(PurplePluginAction *action)
199 PurpleRequestFields *request;
200 PurpleRequestFieldGroup *group;
201 PurpleRequestField *field;
203 group = purple_request_field_group_new(NULL);
205 field = purple_request_field_int_new("mins", _("Minutes"), 10, 0, 9999);
206 purple_request_field_group_add_field(group, field);
208 request = purple_request_fields_new();
209 purple_request_fields_add_group(request, group);
211 purple_request_fields(action->plugin,
212 N_("I'dle Mak'er"),
213 _("Set Idle Time for All Accounts"),
214 NULL,
215 request,
216 _("_Set"), G_CALLBACK(idle_all_action_ok),
217 _("_Cancel"), NULL,
218 NULL, NULL);
221 static void
222 unidle_all_action(PurplePluginAction *action)
224 /* freeing the list here will cause segfaults if the user idles an account
225 * after the list is freed */
226 g_list_foreach(idled_accts, (GFunc)set_idle_time, GINT_TO_POINTER(0));
227 g_list_free(idled_accts);
228 idled_accts = NULL;
231 static GList *
232 actions(PurplePlugin *plugin)
234 GList *l = NULL;
235 PurplePluginAction *act = NULL;
237 act = purple_plugin_action_new(_("Set Account Idle Time"),
238 idle_action);
239 l = g_list_append(l, act);
241 act = purple_plugin_action_new(_("Unset Account Idle Time"),
242 unidle_action);
243 l = g_list_append(l, act);
245 act = purple_plugin_action_new(_("Set Idle Time for All Accounts"),
246 idle_all_action);
247 l = g_list_append(l, act);
249 act = purple_plugin_action_new(
250 _("Unset Idle Time for All Idled Accounts"), unidle_all_action);
251 l = g_list_append(l, act);
253 return l;
256 static void
257 signing_off_cb(PurpleConnection *gc, void *data)
259 PurpleAccount *account;
261 account = purple_connection_get_account(gc);
262 idled_accts = g_list_remove(idled_accts, account);
265 static PurplePluginInfo *
266 plugin_query(GError **error)
268 const gchar * const authors[] = {
269 "Eric Warmenhoven <eric@warmenhoven.org>",
270 NULL
273 return purple_plugin_info_new(
274 "id", IDLE_PLUGIN_ID,
275 /* This is a cultural reference. Dy'er Mak'er is a song by Led Zeppelin.
276 If that doesn't translate well into your language, drop the 's before translating. */
277 "name", N_("I'dle Mak'er"),
278 "version", DISPLAY_VERSION,
279 "category", N_("Utility"),
280 "summary", N_("Allows you to hand-configure how long you've been idle"),
281 "description", N_("Allows you to hand-configure how long you've been idle"),
282 "authors", authors,
283 "website", PURPLE_WEBSITE,
284 "abi-version", PURPLE_ABI_VERSION,
285 "actions-cb", actions,
286 NULL
290 static gboolean
291 plugin_load(PurplePlugin *plugin, GError **error)
293 purple_signal_connect(purple_connections_get_handle(), "signing-off",
294 plugin,
295 PURPLE_CALLBACK(signing_off_cb), NULL);
297 return TRUE;
300 static gboolean
301 plugin_unload(PurplePlugin *plugin, GError **error)
303 unidle_all_action(NULL);
305 return TRUE;
308 PURPLE_PLUGIN_INIT(idle, plugin_query, plugin_load, plugin_unload);