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"
36 /* This plugin no longer depends on gtk */
37 #define IDLE_PLUGIN_ID "core-idle"
39 static GList
*idled_accts
= NULL
;
42 unidle_filter(PurpleAccount
*acct
)
44 if (g_list_find(idled_accts
, acct
))
51 idleable_filter(PurpleAccount
*account
)
55 prpl
= purple_find_prpl(purple_account_get_protocol_id(account
));
56 g_return_val_if_fail(prpl
!= NULL
, FALSE
);
58 return (PURPLE_PLUGIN_PROTOCOL_INFO(prpl
)->set_idle
!= NULL
);
62 set_idle_time(PurpleAccount
*acct
, int mins_idle
)
65 PurpleConnection
*gc
= purple_account_get_connection(acct
);
66 PurplePresence
*presence
= purple_account_get_presence(acct
);
71 purple_debug_info("idle",
72 "setting idle time for %s to %d\n",
73 purple_account_get_username(acct
), mins_idle
);
76 t
= time(NULL
) - (60 * mins_idle
); /* subtract seconds idle from current time */
78 t
= 0; /* time idle is irrelevant */
80 purple_presence_set_idle(presence
, mins_idle
? TRUE
: FALSE
, t
);
84 idle_action_ok(void *ignored
, PurpleRequestFields
*fields
)
86 int tm
= purple_request_fields_get_integer(fields
, "mins");
87 PurpleAccount
*acct
= purple_request_fields_get_account(fields
, "acct");
89 /* only add the account to the GList if it's not already been idled */
90 if (!unidle_filter(acct
))
92 purple_debug_misc("idle",
93 "%s hasn't been idled yet; adding to list.\n",
94 purple_account_get_username(acct
));
95 idled_accts
= g_list_append(idled_accts
, acct
);
98 set_idle_time(acct
, tm
);
102 idle_all_action_ok(void *ignored
, PurpleRequestFields
*fields
)
104 PurpleAccount
*acct
= NULL
;
106 int tm
= purple_request_fields_get_integer(fields
, "mins");
108 list
= purple_accounts_get_all_active();
109 for(iter
= list
; iter
; iter
= iter
->next
) {
110 acct
= (PurpleAccount
*)(iter
->data
);
112 if(acct
&& idleable_filter(acct
)) {
113 purple_debug_misc("idle", "Idling %s.\n",
114 purple_account_get_username(acct
));
116 set_idle_time(acct
, tm
);
118 if(!g_list_find(idled_accts
, acct
))
119 idled_accts
= g_list_append(idled_accts
, acct
);
127 unidle_action_ok(void *ignored
, PurpleRequestFields
*fields
)
129 PurpleAccount
*acct
= purple_request_fields_get_account(fields
, "acct");
131 set_idle_time(acct
, 0); /* unidle the account */
133 /* once the account has been unidled it shouldn't be in the list */
134 idled_accts
= g_list_remove(idled_accts
, acct
);
139 idle_action(PurplePluginAction
*action
)
141 /* Use the super fancy request API */
143 PurpleRequestFields
*request
;
144 PurpleRequestFieldGroup
*group
;
145 PurpleRequestField
*field
;
147 group
= purple_request_field_group_new(NULL
);
149 field
= purple_request_field_account_new("acct", _("Account"), NULL
);
150 purple_request_field_account_set_filter(field
, idleable_filter
);
151 purple_request_field_account_set_show_all(field
, FALSE
);
152 purple_request_field_group_add_field(group
, field
);
154 field
= purple_request_field_int_new("mins", _("Minutes"), 10);
155 purple_request_field_group_add_field(group
, field
);
157 request
= purple_request_fields_new();
158 purple_request_fields_add_group(request
, group
);
160 purple_request_fields(action
->plugin
,
162 _("Set Account Idle Time"),
165 _("_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
);
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
),
206 idle_all_action(PurplePluginAction
*action
)
208 PurpleRequestFields
*request
;
209 PurpleRequestFieldGroup
*group
;
210 PurpleRequestField
*field
;
212 group
= purple_request_field_group_new(NULL
);
214 field
= purple_request_field_int_new("mins", _("Minutes"), 10);
215 purple_request_field_group_add_field(group
, field
);
217 request
= purple_request_fields_new();
218 purple_request_fields_add_group(request
, group
);
220 purple_request_fields(action
->plugin
,
222 _("Set Idle Time for All Accounts"),
225 _("_Set"), G_CALLBACK(idle_all_action_ok
),
232 unidle_all_action(PurplePluginAction
*action
)
236 /* freeing the list here will cause segfaults if the user idles an account
237 * after the list is freed */
238 for (l
= idled_accts
; l
; l
= l
->next
)
240 PurpleAccount
*account
= l
->data
;
241 set_idle_time(account
, 0);
244 g_list_free(idled_accts
);
249 actions(PurplePlugin
*plugin
, gpointer context
)
252 PurplePluginAction
*act
= NULL
;
254 act
= purple_plugin_action_new(_("Set Account Idle Time"),
256 l
= g_list_append(l
, act
);
258 act
= purple_plugin_action_new(_("Unset Account Idle Time"),
260 l
= g_list_append(l
, act
);
262 act
= purple_plugin_action_new(_("Set Idle Time for All Accounts"),
264 l
= g_list_append(l
, act
);
266 act
= purple_plugin_action_new(
267 _("Unset Idle Time for All Idled Accounts"), unidle_all_action
);
268 l
= g_list_append(l
, act
);
274 signing_off_cb(PurpleConnection
*gc
, void *data
)
276 PurpleAccount
*account
;
278 account
= purple_connection_get_account(gc
);
279 idled_accts
= g_list_remove(idled_accts
, account
);
283 plugin_load(PurplePlugin
*plugin
)
285 purple_signal_connect(purple_connections_get_handle(), "signing-off",
287 PURPLE_CALLBACK(signing_off_cb
), NULL
);
293 plugin_unload(PurplePlugin
*plugin
)
295 unidle_all_action(NULL
);
300 static PurplePluginInfo info
=
303 PURPLE_MAJOR_VERSION
,
304 PURPLE_MINOR_VERSION
,
305 PURPLE_PLUGIN_STANDARD
,
309 PURPLE_PRIORITY_DEFAULT
,
312 /* This is a cultural reference. Dy'er Mak'er is a song by Led Zeppelin.
313 If that doesn't translate well into your language, drop the 's before translating. */
316 N_("Allows you to hand-configure how long you've been idle"),
317 N_("Allows you to hand-configure how long you've been idle"),
318 "Eric Warmenhoven <eric@warmenhoven.org>",
336 init_plugin(PurplePlugin
*plugin
)
341 PURPLE_INIT_PLUGIN(idle
, init_plugin
, info
)