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
27 #include "connection.h"
37 /* This plugin no longer depends on gtk */
38 #define IDLE_PLUGIN_ID "core-idle"
40 static GList
*idled_accts
= NULL
;
43 unidle_filter(PurpleAccount
*acct
)
45 if (g_list_find(idled_accts
, acct
))
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
);
63 set_idle_time(PurpleAccount
*acct
, int mins_idle
)
66 PurpleConnection
*gc
= purple_account_get_connection(acct
);
67 PurplePresence
*presence
= purple_account_get_presence(acct
);
72 purple_debug_info("idle",
73 "setting idle time for %s to %d\n",
74 purple_account_get_username(acct
), mins_idle
);
77 t
= time(NULL
) - (60 * mins_idle
); /* subtract seconds idle from current time */
79 t
= 0; /* time idle is irrelevant */
81 purple_presence_set_idle(presence
, mins_idle
? TRUE
: FALSE
, t
);
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
);
103 idle_all_action_ok(void *ignored
, PurpleRequestFields
*fields
)
105 PurpleAccount
*acct
= NULL
;
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
);
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
);
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
,
163 _("Set Account Idle Time"),
166 _("_Set"), G_CALLBACK(idle_action_ok
),
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
);
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
,
196 _("Unset Account Idle Time"),
199 _("_Unset"), G_CALLBACK(unidle_action_ok
),
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
,
221 _("Set Idle Time for All Accounts"),
224 _("_Set"), G_CALLBACK(idle_all_action_ok
),
230 unidle_all_action(PurplePluginAction
*action
)
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
);
247 actions(PurplePlugin
*plugin
)
250 PurplePluginAction
*act
= NULL
;
252 act
= purple_plugin_action_new(_("Set Account Idle Time"),
254 l
= g_list_append(l
, act
);
256 act
= purple_plugin_action_new(_("Unset Account Idle Time"),
258 l
= g_list_append(l
, act
);
260 act
= purple_plugin_action_new(_("Set Idle Time for All Accounts"),
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
);
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>",
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"),
298 "website", PURPLE_WEBSITE
,
299 "abi-version", PURPLE_ABI_VERSION
,
300 "actions-cb", actions
,
306 plugin_load(PurplePlugin
*plugin
, GError
**error
)
308 purple_signal_connect(purple_connections_get_handle(), "signing-off",
310 PURPLE_CALLBACK(signing_off_cb
), NULL
);
316 plugin_unload(PurplePlugin
*plugin
, GError
**error
)
318 unidle_all_action(NULL
);
323 PURPLE_PLUGIN_INIT(idle
, plugin_query
, plugin_load
, plugin_unload
);