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
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
29 /* This plugin no longer depends on gtk */
30 #define IDLE_PLUGIN_ID "core-idle"
32 static GList
*idled_accts
= NULL
;
35 unidle_filter(PurpleAccount
*acct
)
37 if (g_list_find(idled_accts
, acct
))
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
);
55 set_idle_time(PurpleAccount
*acct
, int mins_idle
)
58 PurpleConnection
*gc
= purple_account_get_connection(acct
);
59 PurplePresence
*presence
= purple_account_get_presence(acct
);
64 purple_debug_info("idle",
65 "setting idle time for %s to %d\n",
66 purple_account_get_username(acct
), mins_idle
);
69 t
= time(NULL
) - (60 * mins_idle
); /* subtract seconds idle from current time */
71 t
= 0; /* time idle is irrelevant */
73 purple_presence_set_idle(presence
, mins_idle
? TRUE
: FALSE
, t
);
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
);
95 idle_all_action_ok(void *ignored
, PurpleRequestFields
*fields
)
97 PurpleAccount
*acct
= NULL
;
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
);
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
);
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
,
155 _("Set Account Idle Time"),
158 _("_Set"), G_CALLBACK(idle_action_ok
),
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
);
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
,
188 _("Unset Account Idle Time"),
191 _("_Unset"), G_CALLBACK(unidle_action_ok
),
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
,
213 _("Set Idle Time for All Accounts"),
216 _("_Set"), G_CALLBACK(idle_all_action_ok
),
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
);
232 actions(PurplePlugin
*plugin
)
235 PurplePluginAction
*act
= NULL
;
237 act
= purple_plugin_action_new(_("Set Account Idle Time"),
239 l
= g_list_append(l
, act
);
241 act
= purple_plugin_action_new(_("Unset Account Idle Time"),
243 l
= g_list_append(l
, act
);
245 act
= purple_plugin_action_new(_("Set Idle Time for All Accounts"),
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
);
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>",
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"),
283 "website", PURPLE_WEBSITE
,
284 "abi-version", PURPLE_ABI_VERSION
,
285 "actions-cb", actions
,
291 plugin_load(PurplePlugin
*plugin
, GError
**error
)
293 purple_signal_connect(purple_connections_get_handle(), "signing-off",
295 PURPLE_CALLBACK(signing_off_cb
), NULL
);
301 plugin_unload(PurplePlugin
*plugin
, GError
**error
)
303 unidle_all_action(NULL
);
308 PURPLE_PLUGIN_INIT(idle
, plugin_query
, plugin_load
, plugin_unload
);