merge of 'c965c78fc4196475e8fd004312be08c074958764'
[pidgin-git.git] / finch / gntpounce.c
blob449d525323ad4c5cdeb2dc9b6f3cdbd08364e38e
1 /**
2 * @file gntpounce.c GNT Buddy Pounce API
3 * @ingroup finch
4 */
6 /* finch
8 * Finch is the legal property of its developers, whose names are too numerous
9 * to list here. Please refer to the COPYRIGHT file distributed with this
10 * source distribution.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
27 #include <gnt.h>
28 #include <gntbox.h>
29 #include <gntbutton.h>
30 #include <gntcheckbox.h>
31 #include <gntcombobox.h>
32 #include <gntentry.h>
33 #include <gntlabel.h>
34 #include <gntline.h>
35 #include <gnttree.h>
36 #include <gntutils.h>
38 #include "finch.h"
39 #include <internal.h>
41 #include "account.h"
42 #include "conversation.h"
43 #include "debug.h"
44 #include "notify.h"
45 #include "prpl.h"
46 #include "request.h"
47 #include "server.h"
48 #include "util.h"
50 #include "gntpounce.h"
53 typedef struct
55 /* Pounce data */
56 PurplePounce *pounce;
57 PurpleAccount *account;
59 /* The window */
60 GntWidget *window;
62 /* Pounce on Whom */
63 GntWidget *account_menu;
64 GntWidget *buddy_entry;
66 /* Pounce options */
67 GntWidget *on_away;
69 /* Pounce When Buddy... */
70 GntWidget *signon;
71 GntWidget *signoff;
72 GntWidget *away;
73 GntWidget *away_return;
74 GntWidget *idle;
75 GntWidget *idle_return;
76 GntWidget *typing;
77 GntWidget *typed;
78 GntWidget *stop_typing;
79 GntWidget *message_recv;
81 /* Action */
82 GntWidget *open_win;
83 GntWidget *popup;
84 GntWidget *popup_entry;
85 GntWidget *send_msg;
86 GntWidget *send_msg_entry;
87 GntWidget *exec_cmd;
88 GntWidget *exec_cmd_entry;
89 GntWidget *play_sound;
91 GntWidget *save_pounce;
93 /* Buttons */
94 GntWidget *save_button;
96 } PurpleGntPounceDialog;
98 typedef struct
100 GntWidget *window;
101 GntWidget *tree;
102 GntWidget *modify_button;
103 GntWidget *delete_button;
104 } PouncesManager;
106 static PouncesManager *pounces_manager = NULL;
108 /**************************************************************************
109 * Callbacks
110 **************************************************************************/
111 static gint
112 delete_win_cb(GntWidget *w, PurpleGntPounceDialog *dialog)
114 gnt_widget_destroy(dialog->window);
115 g_free(dialog);
117 return TRUE;
120 static void
121 cancel_cb(GntWidget *w, PurpleGntPounceDialog *dialog)
123 gnt_widget_destroy(dialog->window);
126 static void
127 add_pounce_to_treeview(GntTree *tree, PurplePounce *pounce)
129 PurpleAccount *account;
130 const char *pouncer;
131 const char *pouncee;
133 account = purple_pounce_get_pouncer(pounce);
134 pouncer = purple_account_get_username(account);
135 pouncee = purple_pounce_get_pouncee(pounce);
136 gnt_tree_add_row_last(tree, pounce,
137 gnt_tree_create_row(tree, pouncer, pouncee), NULL);
140 static void
141 populate_pounces_list(PouncesManager *dialog)
143 GList *pounces;
145 gnt_tree_remove_all(GNT_TREE(dialog->tree));
147 for (pounces = purple_pounces_get_all_for_ui(FINCH_UI); pounces != NULL;
148 pounces = g_list_delete_link(pounces, pounces))
150 add_pounce_to_treeview(GNT_TREE(dialog->tree), pounces->data);
154 static void
155 update_pounces(void)
157 /* Rebuild the pounces list if the pounces manager is open */
158 if (pounces_manager != NULL)
160 populate_pounces_list(pounces_manager);
164 static void
165 signed_on_off_cb(PurpleConnection *gc, gpointer user_data)
167 update_pounces();
170 static void
171 setup_buddy_list_suggestion(GntEntry *entry, gboolean offline)
173 PurpleBlistNode *node = purple_blist_get_root();
174 for (; node; node = purple_blist_node_next(node, offline)) {
175 if (!PURPLE_BLIST_NODE_IS_BUDDY(node))
176 continue;
177 gnt_entry_add_suggest(entry, purple_buddy_get_name((PurpleBuddy*)node));
181 static void
182 save_pounce_cb(GntWidget *w, PurpleGntPounceDialog *dialog)
184 const char *name;
185 const char *message, *command, *reason;
186 PurplePounceEvent events = PURPLE_POUNCE_NONE;
187 PurplePounceOption options = PURPLE_POUNCE_OPTION_NONE;
189 name = gnt_entry_get_text(GNT_ENTRY(dialog->buddy_entry));
191 if (*name == '\0')
193 purple_notify_error(NULL, NULL,
194 _("Please enter a buddy to pounce."), NULL);
195 return;
198 /* Options */
199 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->on_away)))
200 options |= PURPLE_POUNCE_OPTION_AWAY;
202 /* Events */
203 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->signon)))
204 events |= PURPLE_POUNCE_SIGNON;
206 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->signoff)))
207 events |= PURPLE_POUNCE_SIGNOFF;
209 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->away)))
210 events |= PURPLE_POUNCE_AWAY;
212 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->away_return)))
213 events |= PURPLE_POUNCE_AWAY_RETURN;
215 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->idle)))
216 events |= PURPLE_POUNCE_IDLE;
218 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->idle_return)))
219 events |= PURPLE_POUNCE_IDLE_RETURN;
221 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->typing)))
222 events |= PURPLE_POUNCE_TYPING;
224 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->typed)))
225 events |= PURPLE_POUNCE_TYPED;
227 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->stop_typing)))
228 events |= PURPLE_POUNCE_TYPING_STOPPED;
230 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->message_recv)))
231 events |= PURPLE_POUNCE_MESSAGE_RECEIVED;
233 /* Data fields */
234 message = gnt_entry_get_text(GNT_ENTRY(dialog->send_msg_entry));
235 command = gnt_entry_get_text(GNT_ENTRY(dialog->exec_cmd_entry));
236 reason = gnt_entry_get_text(GNT_ENTRY(dialog->popup_entry));
238 if (*reason == '\0') reason = NULL;
239 if (*message == '\0') message = NULL;
240 if (*command == '\0') command = NULL;
242 if (dialog->pounce == NULL) {
243 dialog->pounce = purple_pounce_new(FINCH_UI, dialog->account,
244 name, events, options);
245 } else {
246 purple_pounce_set_events(dialog->pounce, events);
247 purple_pounce_set_options(dialog->pounce, options);
248 purple_pounce_set_pouncer(dialog->pounce, dialog->account);
249 purple_pounce_set_pouncee(dialog->pounce, name);
252 /* Actions */
253 purple_pounce_action_set_enabled(dialog->pounce, "open-window",
254 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->open_win)));
255 purple_pounce_action_set_enabled(dialog->pounce, "popup-notify",
256 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->popup)));
257 purple_pounce_action_set_enabled(dialog->pounce, "send-message",
258 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->send_msg)));
259 purple_pounce_action_set_enabled(dialog->pounce, "execute-command",
260 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->exec_cmd)));
261 purple_pounce_action_set_enabled(dialog->pounce, "play-beep",
262 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->play_sound)));
264 purple_pounce_action_set_attribute(dialog->pounce, "send-message",
265 "message", message);
266 purple_pounce_action_set_attribute(dialog->pounce, "execute-command",
267 "command", command);
268 purple_pounce_action_set_attribute(dialog->pounce, "popup-notify",
269 "reason", reason);
271 /* Set the defaults for next time. */
272 purple_prefs_set_bool("/finch/pounces/default_actions/open-window",
273 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->open_win)));
274 purple_prefs_set_bool("/finch/pounces/default_actions/popup-notify",
275 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->popup)));
276 purple_prefs_set_bool("/finch/pounces/default_actions/send-message",
277 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->send_msg)));
278 purple_prefs_set_bool("/finch/pounces/default_actions/execute-command",
279 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->exec_cmd)));
280 purple_prefs_set_bool("/finch/pounces/default_actions/play-beep",
281 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->play_sound)));
283 purple_pounce_set_save(dialog->pounce,
284 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->save_pounce)));
286 purple_pounce_set_pouncer(dialog->pounce,
287 (PurpleAccount *)gnt_combo_box_get_selected_data(GNT_COMBO_BOX(dialog->account_menu)));
289 update_pounces();
291 gnt_widget_destroy(dialog->window);
295 void
296 finch_pounce_editor_show(PurpleAccount *account, const char *name,
297 PurplePounce *cur_pounce)
299 PurpleGntPounceDialog *dialog;
300 GntWidget *window;
301 GntWidget *bbox;
302 GntWidget *hbox, *vbox;
303 GntWidget *button;
304 GntWidget *combo;
305 GList *list;
307 g_return_if_fail((cur_pounce != NULL) ||
308 (account != NULL) ||
309 (purple_accounts_get_all() != NULL));
311 dialog = g_new0(PurpleGntPounceDialog, 1);
313 if (cur_pounce != NULL) {
314 dialog->pounce = cur_pounce;
315 dialog->account = purple_pounce_get_pouncer(cur_pounce);
316 } else if (account != NULL) {
317 dialog->pounce = NULL;
318 dialog->account = account;
319 } else {
320 GList *connections = purple_connections_get_all();
321 PurpleConnection *gc;
323 if (connections != NULL) {
324 gc = (PurpleConnection *)connections->data;
325 dialog->account = purple_connection_get_account(gc);
326 } else
327 dialog->account = purple_accounts_get_all()->data;
329 dialog->pounce = NULL;
332 /* Create the window. */
333 dialog->window = window = gnt_vbox_new(FALSE);
334 gnt_box_set_pad(GNT_BOX(window), 0);
335 gnt_box_set_toplevel(GNT_BOX(window), TRUE);
336 gnt_box_set_alignment(GNT_BOX(window), GNT_ALIGN_LEFT);
337 gnt_box_set_title(GNT_BOX(window),
338 (cur_pounce == NULL
339 ? _("New Buddy Pounce") : _("Edit Buddy Pounce")));
341 g_signal_connect(G_OBJECT(window), "destroy",
342 G_CALLBACK(delete_win_cb), dialog);
344 gnt_box_add_widget(GNT_BOX(window), gnt_label_new_with_format(_("Pounce Who"), GNT_TEXT_FLAG_BOLD));
346 /* Account: */
347 gnt_box_add_widget(GNT_BOX(window), gnt_label_new(_("Account:")));
348 dialog->account_menu = combo = gnt_combo_box_new();
349 list = purple_accounts_get_all();
350 for (; list; list = list->next)
352 PurpleAccount *account;
353 char *text;
355 account = list->data;
356 text = g_strdup_printf("%s (%s)",
357 purple_account_get_username(account),
358 purple_account_get_protocol_name(account));
359 gnt_combo_box_add_data(GNT_COMBO_BOX(combo), account, text);
360 g_free(text);
362 if (dialog->account)
363 gnt_combo_box_set_selected(GNT_COMBO_BOX(combo), dialog->account);
365 gnt_box_add_widget(GNT_BOX(window), combo);
367 /* Buddy: */
368 hbox = gnt_hbox_new(FALSE);
369 gnt_box_add_widget(GNT_BOX(hbox), gnt_label_new(_("Buddy name:")));
371 dialog->buddy_entry = gnt_entry_new(NULL);
372 gnt_box_add_widget(GNT_BOX(hbox), dialog->buddy_entry);
374 setup_buddy_list_suggestion(GNT_ENTRY(dialog->buddy_entry), TRUE);
376 gnt_box_add_widget(GNT_BOX(window), hbox);
378 if (cur_pounce != NULL) {
379 gnt_entry_set_text(GNT_ENTRY(dialog->buddy_entry),
380 purple_pounce_get_pouncee(cur_pounce));
381 } else if (name != NULL) {
382 gnt_entry_set_text(GNT_ENTRY(dialog->buddy_entry), name);
385 /* Create the event frame */
386 gnt_box_add_widget(GNT_BOX(window), gnt_line_new(FALSE));
387 gnt_box_add_widget(GNT_BOX(window), gnt_label_new_with_format(_("Pounce When Buddy..."), GNT_TEXT_FLAG_BOLD));
389 dialog->signon = gnt_check_box_new(_("Signs on"));
390 dialog->signoff = gnt_check_box_new(_("Signs off"));
391 dialog->away = gnt_check_box_new(_("Goes away"));
392 dialog->away_return = gnt_check_box_new(_("Returns from away"));
393 dialog->idle = gnt_check_box_new(_("Becomes idle"));
394 dialog->idle_return = gnt_check_box_new(_("Is no longer idle"));
395 dialog->typing = gnt_check_box_new(_("Starts typing"));
396 dialog->typed = gnt_check_box_new(_("Pauses while typing"));
397 dialog->stop_typing = gnt_check_box_new(_("Stops typing"));
398 dialog->message_recv = gnt_check_box_new(_("Sends a message"));
400 hbox = gnt_hbox_new(TRUE);
401 gnt_box_set_pad(GNT_BOX(hbox), 2);
403 vbox = gnt_vbox_new(FALSE);
404 gnt_box_set_pad(GNT_BOX(vbox), 0);
405 gnt_box_add_widget(GNT_BOX(hbox), vbox);
407 gnt_box_add_widget(GNT_BOX(vbox), dialog->signon);
408 gnt_box_add_widget(GNT_BOX(vbox), dialog->away);
409 gnt_box_add_widget(GNT_BOX(vbox), dialog->idle);
410 gnt_box_add_widget(GNT_BOX(vbox), dialog->typing);
411 gnt_box_add_widget(GNT_BOX(vbox), dialog->stop_typing);
413 vbox = gnt_vbox_new(FALSE);
414 gnt_box_set_pad(GNT_BOX(vbox), 0);
415 gnt_box_add_widget(GNT_BOX(hbox), vbox);
417 gnt_box_add_widget(GNT_BOX(vbox), dialog->signoff);
418 gnt_box_add_widget(GNT_BOX(vbox), dialog->away_return);
419 gnt_box_add_widget(GNT_BOX(vbox), dialog->idle_return);
420 gnt_box_add_widget(GNT_BOX(vbox), dialog->typed);
421 gnt_box_add_widget(GNT_BOX(vbox), dialog->message_recv);
423 gnt_box_add_widget(GNT_BOX(window), hbox);
425 /* Create the "Action" frame. */
426 gnt_box_add_widget(GNT_BOX(window), gnt_line_new(FALSE));
427 gnt_box_add_widget(GNT_BOX(window), gnt_label_new_with_format(_("Action"), GNT_TEXT_FLAG_BOLD));
429 dialog->open_win = gnt_check_box_new(_("Open an IM window"));
430 dialog->popup = gnt_check_box_new(_("Pop up a notification"));
431 dialog->send_msg = gnt_check_box_new(_("Send a message"));
432 dialog->exec_cmd = gnt_check_box_new(_("Execute a command"));
433 dialog->play_sound = gnt_check_box_new(_("Play a sound"));
435 dialog->send_msg_entry = gnt_entry_new(NULL);
436 dialog->exec_cmd_entry = gnt_entry_new(NULL);
437 dialog->popup_entry = gnt_entry_new(NULL);
438 dialog->exec_cmd_entry = gnt_entry_new(NULL);
440 hbox = gnt_hbox_new(FALSE);
441 gnt_box_add_widget(GNT_BOX(hbox), dialog->open_win);
442 gnt_box_add_widget(GNT_BOX(window), hbox);
443 hbox = gnt_hbox_new(FALSE);
444 gnt_box_add_widget(GNT_BOX(hbox), dialog->popup);
445 gnt_box_add_widget(GNT_BOX(hbox), dialog->popup_entry);
446 gnt_box_add_widget(GNT_BOX(window), hbox);
447 hbox = gnt_hbox_new(FALSE);
448 gnt_box_add_widget(GNT_BOX(hbox), dialog->send_msg);
449 gnt_box_add_widget(GNT_BOX(hbox), dialog->send_msg_entry);
450 gnt_box_add_widget(GNT_BOX(window), hbox);
451 hbox = gnt_hbox_new(FALSE);
452 gnt_box_add_widget(GNT_BOX(hbox), dialog->exec_cmd);
453 gnt_box_add_widget(GNT_BOX(hbox), dialog->exec_cmd_entry);
454 gnt_box_add_widget(GNT_BOX(window), hbox);
455 hbox = gnt_hbox_new(FALSE);
456 gnt_box_add_widget(GNT_BOX(hbox), dialog->play_sound);
457 gnt_box_add_widget(GNT_BOX(window), hbox);
459 gnt_box_add_widget(GNT_BOX(window), gnt_line_new(FALSE));
460 gnt_box_add_widget(GNT_BOX(window), gnt_label_new_with_format(_("Options"), GNT_TEXT_FLAG_BOLD));
461 dialog->on_away = gnt_check_box_new(_("Pounce only when my status is not Available"));
462 gnt_box_add_widget(GNT_BOX(window), dialog->on_away);
463 dialog->save_pounce = gnt_check_box_new(_("Recurring"));
464 gnt_box_add_widget(GNT_BOX(window), dialog->save_pounce);
467 gnt_box_add_widget(GNT_BOX(window), gnt_line_new(FALSE));
468 /* Now the button box! */
469 bbox = gnt_hbox_new(FALSE);
471 /* Cancel button */
472 button = gnt_button_new(_("Cancel"));
473 gnt_box_add_widget(GNT_BOX(bbox), button);
474 g_signal_connect(G_OBJECT(button), "activate",
475 G_CALLBACK(cancel_cb), dialog);
477 /* Save button */
478 dialog->save_button = button = gnt_button_new(_("Save"));
479 gnt_box_add_widget(GNT_BOX(bbox), button);
480 g_signal_connect(G_OBJECT(button), "activate",
481 G_CALLBACK(save_pounce_cb), dialog);
483 gnt_box_add_widget(GNT_BOX(window), bbox);
486 /* Set the values of stuff. */
487 if (cur_pounce != NULL)
489 PurplePounceEvent events = purple_pounce_get_events(cur_pounce);
490 PurplePounceOption options = purple_pounce_get_options(cur_pounce);
491 const char *value;
493 /* Options */
494 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->on_away),
495 (options & PURPLE_POUNCE_OPTION_AWAY));
497 /* Events */
498 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->signon),
499 (events & PURPLE_POUNCE_SIGNON));
500 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->signoff),
501 (events & PURPLE_POUNCE_SIGNOFF));
502 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->away),
503 (events & PURPLE_POUNCE_AWAY));
504 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->away_return),
505 (events & PURPLE_POUNCE_AWAY_RETURN));
506 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->idle),
507 (events & PURPLE_POUNCE_IDLE));
508 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->idle_return),
509 (events & PURPLE_POUNCE_IDLE_RETURN));
510 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->typing),
511 (events & PURPLE_POUNCE_TYPING));
512 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->typed),
513 (events & PURPLE_POUNCE_TYPED));
514 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->stop_typing),
515 (events & PURPLE_POUNCE_TYPING_STOPPED));
516 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->message_recv),
517 (events & PURPLE_POUNCE_MESSAGE_RECEIVED));
519 /* Actions */
520 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->open_win),
521 purple_pounce_action_is_enabled(cur_pounce, "open-window"));
522 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->popup),
523 purple_pounce_action_is_enabled(cur_pounce, "popup-notify"));
524 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->send_msg),
525 purple_pounce_action_is_enabled(cur_pounce, "send-message"));
526 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->exec_cmd),
527 purple_pounce_action_is_enabled(cur_pounce, "execute-command"));
528 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->play_sound),
529 purple_pounce_action_is_enabled(cur_pounce, "play-beep"));
531 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->save_pounce),
532 purple_pounce_get_save(cur_pounce));
534 if ((value = purple_pounce_action_get_attribute(cur_pounce,
535 "send-message",
536 "message")) != NULL)
538 gnt_entry_set_text(GNT_ENTRY(dialog->send_msg_entry), value);
541 if ((value = purple_pounce_action_get_attribute(cur_pounce,
542 "popup-notify",
543 "reason")) != NULL)
545 gnt_entry_set_text(GNT_ENTRY(dialog->popup_entry), value);
548 if ((value = purple_pounce_action_get_attribute(cur_pounce,
549 "execute-command",
550 "command")) != NULL)
552 gnt_entry_set_text(GNT_ENTRY(dialog->exec_cmd_entry), value);
555 else
557 PurpleBuddy *buddy = NULL;
559 if (name != NULL)
560 buddy = purple_find_buddy(account, name);
562 /* Set some defaults */
563 if (buddy == NULL) {
564 gnt_check_box_set_checked(
565 GNT_CHECK_BOX(dialog->signon), TRUE);
566 } else {
567 if (!PURPLE_BUDDY_IS_ONLINE(buddy)) {
568 gnt_check_box_set_checked(
569 GNT_CHECK_BOX(dialog->signon), TRUE);
570 } else {
571 gboolean default_set = FALSE;
572 PurplePresence *presence = purple_buddy_get_presence(buddy);
574 if (purple_presence_is_idle(presence))
576 gnt_check_box_set_checked(
577 GNT_CHECK_BOX(dialog->idle_return), TRUE);
579 default_set = TRUE;
582 if (!purple_presence_is_available(presence))
584 gnt_check_box_set_checked(
585 GNT_CHECK_BOX(dialog->away_return), TRUE);
587 default_set = TRUE;
590 if (!default_set)
592 gnt_check_box_set_checked(
593 GNT_CHECK_BOX(dialog->signon), TRUE);
598 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->open_win),
599 purple_prefs_get_bool("/finch/pounces/default_actions/open-window"));
600 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->popup),
601 purple_prefs_get_bool("/finch/pounces/default_actions/popup-notify"));
602 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->send_msg),
603 purple_prefs_get_bool("/finch/pounces/default_actions/send-message"));
604 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->exec_cmd),
605 purple_prefs_get_bool("/finch/pounces/default_actions/execute-command"));
606 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->play_sound),
607 purple_prefs_get_bool("/finch/pounces/default_actions/play-beep"));
610 gnt_widget_show(window);
615 static gboolean
616 pounces_manager_destroy_cb(GntWidget *widget, gpointer user_data)
618 PouncesManager *dialog = user_data;
620 dialog->window = NULL;
621 finch_pounces_manager_hide();
623 return FALSE;
627 static void
628 pounces_manager_add_cb(GntButton *button, gpointer user_data)
630 if (purple_accounts_get_all() == NULL) {
631 purple_notify_error(NULL, _("Cannot create pounce"),
632 _("You do not have any accounts."),
633 _("You must create an account first before you can create a pounce."));
634 return;
636 finch_pounce_editor_show(NULL, NULL, NULL);
640 static void
641 pounces_manager_modify_cb(GntButton *button, gpointer user_data)
643 PouncesManager *dialog = user_data;
644 PurplePounce *pounce = gnt_tree_get_selection_data(GNT_TREE(dialog->tree));
645 if (pounce)
646 finch_pounce_editor_show(NULL, NULL, pounce);
649 static void
650 pounces_manager_delete_confirm_cb(PurplePounce *pounce)
652 gnt_tree_remove(GNT_TREE(pounces_manager->tree), pounce);
654 purple_request_close_with_handle(pounce);
655 purple_pounce_destroy(pounce);
659 static void
660 pounces_manager_delete_cb(GntButton *button, gpointer user_data)
662 PouncesManager *dialog = user_data;
663 PurplePounce *pounce;
664 PurpleAccount *account;
665 const char *pouncer, *pouncee;
666 char *buf;
668 pounce = (PurplePounce *)gnt_tree_get_selection_data(GNT_TREE(dialog->tree));
669 if (pounce == NULL)
670 return;
672 account = purple_pounce_get_pouncer(pounce);
673 pouncer = purple_account_get_username(account);
674 pouncee = purple_pounce_get_pouncee(pounce);
675 buf = g_strdup_printf(_("Are you sure you want to delete the pounce on %s for %s?"), pouncee, pouncer);
676 purple_request_action(pounce, NULL, buf, NULL, 0,
677 account, pouncee, NULL,
678 pounce, 2,
679 _("Delete"), pounces_manager_delete_confirm_cb,
680 _("Cancel"), NULL);
681 g_free(buf);
684 static void
685 pounces_manager_close_cb(GntButton *button, gpointer user_data)
687 finch_pounces_manager_hide();
691 void
692 finch_pounces_manager_show(void)
694 PouncesManager *dialog;
695 GntWidget *bbox;
696 GntWidget *button;
697 GntWidget *tree;
698 GntWidget *win;
700 if (pounces_manager != NULL) {
701 gnt_window_present(pounces_manager->window);
702 return;
705 pounces_manager = dialog = g_new0(PouncesManager, 1);
707 dialog->window = win = gnt_vbox_new(FALSE);
708 gnt_box_set_toplevel(GNT_BOX(win), TRUE);
709 gnt_box_set_title(GNT_BOX(win), _("Buddy Pounces"));
710 gnt_box_set_pad(GNT_BOX(win), 0);
712 g_signal_connect(G_OBJECT(win), "destroy",
713 G_CALLBACK(pounces_manager_destroy_cb), dialog);
715 /* List of saved buddy pounces */
716 dialog->tree = tree = GNT_WIDGET(gnt_tree_new_with_columns(2));
717 gnt_tree_set_column_titles(GNT_TREE(tree), "Account", "Pouncee", NULL);
718 gnt_tree_set_show_title(GNT_TREE(tree), TRUE);
720 gnt_box_add_widget(GNT_BOX(win), tree);
722 /* Button box. */
723 bbox = gnt_hbox_new(FALSE);
725 /* Add button */
726 button = gnt_button_new(_("Add"));
727 gnt_box_add_widget(GNT_BOX(bbox), button);
728 gnt_util_set_trigger_widget(tree, GNT_KEY_INS, button);
730 g_signal_connect(G_OBJECT(button), "activate",
731 G_CALLBACK(pounces_manager_add_cb), dialog);
733 /* Modify button */
734 button = gnt_button_new(_("Modify"));
735 dialog->modify_button = button;
736 gnt_box_add_widget(GNT_BOX(bbox), button);
738 g_signal_connect(G_OBJECT(button), "activate",
739 G_CALLBACK(pounces_manager_modify_cb), dialog);
741 /* Delete button */
742 button = gnt_button_new(_("Delete"));
743 dialog->delete_button = button;
744 gnt_box_add_widget(GNT_BOX(bbox), button);
745 gnt_util_set_trigger_widget(tree, GNT_KEY_DEL, button);
747 g_signal_connect(G_OBJECT(button), "activate",
748 G_CALLBACK(pounces_manager_delete_cb), dialog);
750 /* Close button */
751 button = gnt_button_new(_("Close"));
752 gnt_box_add_widget(GNT_BOX(bbox), button);
753 gnt_widget_show(button);
755 g_signal_connect(G_OBJECT(button), "activate",
756 G_CALLBACK(pounces_manager_close_cb), dialog);
758 gnt_box_add_widget(GNT_BOX(win), bbox);
760 gnt_widget_show(win);
761 populate_pounces_list(pounces_manager);
764 void
765 finch_pounces_manager_hide(void)
767 if (pounces_manager == NULL)
768 return;
770 if (pounces_manager->window != NULL)
771 gnt_widget_destroy(pounces_manager->window);
773 purple_signals_disconnect_by_handle(pounces_manager);
775 g_free(pounces_manager);
776 pounces_manager = NULL;
779 static void
780 pounce_cb(PurplePounce *pounce, PurplePounceEvent events, void *data)
782 PurpleConversation *conv;
783 PurpleAccount *account;
784 PurpleBuddy *buddy;
785 const char *pouncee;
786 const char *alias;
788 pouncee = purple_pounce_get_pouncee(pounce);
789 account = purple_pounce_get_pouncer(pounce);
791 buddy = purple_find_buddy(account, pouncee);
792 if (buddy != NULL)
794 alias = purple_buddy_get_alias(buddy);
795 if (alias == NULL)
796 alias = pouncee;
798 else
799 alias = pouncee;
801 if (purple_pounce_action_is_enabled(pounce, "open-window"))
803 conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, pouncee, account);
805 if (conv == NULL)
806 conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, account, pouncee);
809 if (purple_pounce_action_is_enabled(pounce, "popup-notify"))
811 char *tmp = NULL;
812 const char *name_shown;
813 const char *reason;
814 struct {
815 PurplePounceEvent event;
816 const char *format;
817 } messages[] = {
818 {PURPLE_POUNCE_TYPING, _("%s has started typing to you (%s)")},
819 {PURPLE_POUNCE_TYPED, _("%s has paused while typing to you (%s)")},
820 {PURPLE_POUNCE_SIGNON, _("%s has signed on (%s)")},
821 {PURPLE_POUNCE_IDLE_RETURN, _("%s has returned from being idle (%s)")},
822 {PURPLE_POUNCE_AWAY_RETURN, _("%s has returned from being away (%s)")},
823 {PURPLE_POUNCE_TYPING_STOPPED, _("%s has stopped typing to you (%s)")},
824 {PURPLE_POUNCE_SIGNOFF, _("%s has signed off (%s)")},
825 {PURPLE_POUNCE_IDLE, _("%s has become idle (%s)")},
826 {PURPLE_POUNCE_AWAY, _("%s has gone away. (%s)")},
827 {PURPLE_POUNCE_MESSAGE_RECEIVED, _("%s has sent you a message. (%s)")},
828 {0, NULL}
830 int i;
831 reason = purple_pounce_action_get_attribute(pounce, "popup-notify",
832 "reason");
835 * Here we place the protocol name in the pounce dialog to lessen
836 * confusion about what protocol a pounce is for.
838 for (i = 0; messages[i].format != NULL; i++) {
839 if (messages[i].event & events) {
840 tmp = g_strdup_printf(messages[i].format, alias,
841 purple_account_get_protocol_name(account));
842 break;
845 if (tmp == NULL)
846 tmp = g_strdup(_("Unknown pounce event. Please report this!"));
849 * Ok here is where I change the second argument, title, from
850 * NULL to the account alias if we have it or the account
851 * name if that's all we have
853 if ((name_shown = purple_account_get_alias(account)) == NULL)
854 name_shown = purple_account_get_username(account);
856 if (reason == NULL)
858 purple_notify_info(NULL, name_shown, tmp, purple_date_format_full(NULL));
860 else
862 char *tmp2 = g_strdup_printf("%s\n\n%s", reason, purple_date_format_full(NULL));
863 purple_notify_info(NULL, name_shown, tmp, tmp2);
864 g_free(tmp2);
866 g_free(tmp);
869 if (purple_pounce_action_is_enabled(pounce, "send-message"))
871 const char *message;
873 message = purple_pounce_action_get_attribute(pounce, "send-message",
874 "message");
876 if (message != NULL)
878 conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, pouncee, account);
880 if (conv == NULL)
881 conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, account, pouncee);
883 purple_conversation_write(conv, NULL, message,
884 PURPLE_MESSAGE_SEND, time(NULL));
886 serv_send_im(purple_account_get_connection(account), (char *)pouncee, (char *)message, 0);
890 if (purple_pounce_action_is_enabled(pounce, "execute-command"))
892 const char *command;
894 command = purple_pounce_action_get_attribute(pounce,
895 "execute-command", "command");
897 if (command != NULL)
899 char *localecmd = g_locale_from_utf8(command, -1, NULL,
900 NULL, NULL);
902 if (localecmd != NULL)
904 int pid = fork();
906 if (pid == 0) {
907 char *args[4];
909 args[0] = "sh";
910 args[1] = "-c";
911 args[2] = (char *)localecmd;
912 args[3] = NULL;
914 execvp(args[0], args);
916 _exit(0);
918 g_free(localecmd);
923 if (purple_pounce_action_is_enabled(pounce, "play-beep"))
925 beep();
929 static void
930 free_pounce(PurplePounce *pounce)
932 update_pounces();
935 static void
936 new_pounce(PurplePounce *pounce)
938 purple_pounce_action_register(pounce, "open-window");
939 purple_pounce_action_register(pounce, "popup-notify");
940 purple_pounce_action_register(pounce, "send-message");
941 purple_pounce_action_register(pounce, "execute-command");
942 purple_pounce_action_register(pounce, "play-beep");
944 update_pounces();
947 void *
948 finch_pounces_get_handle()
950 static int handle;
952 return &handle;
955 void
956 finch_pounces_init(void)
958 purple_pounces_register_handler(FINCH_UI, pounce_cb, new_pounce,
959 free_pounce);
961 purple_prefs_add_none("/finch/pounces");
962 purple_prefs_add_none("/finch/pounces/default_actions");
963 purple_prefs_add_bool("/finch/pounces/default_actions/open-window",
964 FALSE);
965 purple_prefs_add_bool("/finch/pounces/default_actions/popup-notify",
966 TRUE);
967 purple_prefs_add_bool("/finch/pounces/default_actions/send-message",
968 FALSE);
969 purple_prefs_add_bool("/finch/pounces/default_actions/execute-command",
970 FALSE);
971 purple_prefs_add_bool("/finch/pounces/default_actions/play-beep",
972 FALSE);
973 purple_prefs_add_none("/finch/pounces/dialog");
975 purple_signal_connect(purple_connections_get_handle(), "signed-on",
976 finch_pounces_get_handle(),
977 PURPLE_CALLBACK(signed_on_off_cb), NULL);
978 purple_signal_connect(purple_connections_get_handle(), "signed-off",
979 finch_pounces_get_handle(),
980 PURPLE_CALLBACK(signed_on_off_cb), NULL);
983 /* XXX: There's no such thing in pidgin. Perhaps there should be? */
984 void finch_pounces_uninit()
986 purple_pounces_unregister_handler(FINCH_UI);
988 purple_signals_disconnect_by_handle(finch_pounces_get_handle());