register PurpleAccountOption as a boxed type
[pidgin-git.git] / finch / gntpounce.c
blob2412a3b265fd6352f10dcaa944f99acfac6295a7
1 /*
2 * finch
4 * Finch is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 #include <internal.h>
25 #include NCURSES_HEADER
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"
40 #include "account.h"
41 #include "conversation.h"
42 #include "debug.h"
43 #include "notify.h"
44 #include "protocol.h"
45 #include "request.h"
46 #include "server.h"
47 #include "util.h"
49 #include "gntpounce.h"
52 typedef struct
54 /* Pounce data */
55 PurplePounce *pounce;
56 PurpleAccount *account;
58 /* The window */
59 GntWidget *window;
61 /* Pounce on Whom */
62 GntWidget *account_menu;
63 GntWidget *buddy_entry;
65 /* Pounce options */
66 GntWidget *on_away;
68 /* Pounce When Buddy... */
69 GntWidget *signon;
70 GntWidget *signoff;
71 GntWidget *away;
72 GntWidget *away_return;
73 GntWidget *idle;
74 GntWidget *idle_return;
75 GntWidget *typing;
76 GntWidget *typed;
77 GntWidget *stop_typing;
78 GntWidget *message_recv;
80 /* Action */
81 GntWidget *open_win;
82 GntWidget *popup;
83 GntWidget *popup_entry;
84 GntWidget *send_msg;
85 GntWidget *send_msg_entry;
86 GntWidget *exec_cmd;
87 GntWidget *exec_cmd_entry;
88 GntWidget *play_sound;
90 GntWidget *save_pounce;
92 /* Buttons */
93 GntWidget *save_button;
95 } PurpleGntPounceDialog;
97 typedef struct
99 GntWidget *window;
100 GntWidget *tree;
101 GntWidget *modify_button;
102 GntWidget *delete_button;
103 } PouncesManager;
105 static PouncesManager *pounces_manager = NULL;
107 /**************************************************************************
108 * Callbacks
109 **************************************************************************/
110 static gint
111 delete_win_cb(GntWidget *w, PurpleGntPounceDialog *dialog)
113 gnt_widget_destroy(dialog->window);
114 g_free(dialog);
116 return TRUE;
119 static void
120 cancel_cb(GntWidget *w, PurpleGntPounceDialog *dialog)
122 gnt_widget_destroy(dialog->window);
125 static void
126 add_pounce_to_treeview(GntTree *tree, PurplePounce *pounce)
128 PurpleAccount *account;
129 const char *pouncer;
130 const char *pouncee;
132 account = purple_pounce_get_pouncer(pounce);
133 pouncer = purple_account_get_username(account);
134 pouncee = purple_pounce_get_pouncee(pounce);
135 gnt_tree_add_row_last(tree, pounce,
136 gnt_tree_create_row(tree, pouncer, pouncee), NULL);
139 static void
140 populate_pounces_list(PouncesManager *dialog)
142 GList *pounces;
144 gnt_tree_remove_all(GNT_TREE(dialog->tree));
146 for (pounces = purple_pounces_get_all_for_ui(FINCH_UI); pounces != NULL;
147 pounces = g_list_delete_link(pounces, pounces))
149 add_pounce_to_treeview(GNT_TREE(dialog->tree), pounces->data);
153 static void
154 update_pounces(void)
156 /* Rebuild the pounces list if the pounces manager is open */
157 if (pounces_manager != NULL)
159 populate_pounces_list(pounces_manager);
163 static void
164 signed_on_off_cb(PurpleConnection *gc, gpointer user_data)
166 update_pounces();
169 static void
170 setup_buddy_list_suggestion(GntEntry *entry, gboolean offline)
172 PurpleBlistNode *node = purple_blist_get_default_root();
173 for (; node; node = purple_blist_node_next(node, offline)) {
174 if (!PURPLE_IS_BUDDY(node))
175 continue;
176 gnt_entry_add_suggest(entry, purple_buddy_get_name((PurpleBuddy*)node));
180 static void
181 save_pounce_cb(GntWidget *w, PurpleGntPounceDialog *dialog)
183 const char *name;
184 const char *message, *command, *reason;
185 PurplePounceEvent events = PURPLE_POUNCE_NONE;
186 PurplePounceOption options = PURPLE_POUNCE_OPTION_NONE;
188 name = gnt_entry_get_text(GNT_ENTRY(dialog->buddy_entry));
190 if (*name == '\0')
192 purple_notify_error(NULL, NULL,
193 _("Please enter a buddy to pounce."), NULL, NULL);
194 return;
197 /* Options */
198 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->on_away)))
199 options |= PURPLE_POUNCE_OPTION_AWAY;
201 /* Events */
202 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->signon)))
203 events |= PURPLE_POUNCE_SIGNON;
205 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->signoff)))
206 events |= PURPLE_POUNCE_SIGNOFF;
208 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->away)))
209 events |= PURPLE_POUNCE_AWAY;
211 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->away_return)))
212 events |= PURPLE_POUNCE_AWAY_RETURN;
214 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->idle)))
215 events |= PURPLE_POUNCE_IDLE;
217 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->idle_return)))
218 events |= PURPLE_POUNCE_IDLE_RETURN;
220 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->typing)))
221 events |= PURPLE_POUNCE_TYPING;
223 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->typed)))
224 events |= PURPLE_POUNCE_TYPED;
226 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->stop_typing)))
227 events |= PURPLE_POUNCE_TYPING_STOPPED;
229 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->message_recv)))
230 events |= PURPLE_POUNCE_MESSAGE_RECEIVED;
232 /* Data fields */
233 message = gnt_entry_get_text(GNT_ENTRY(dialog->send_msg_entry));
234 command = gnt_entry_get_text(GNT_ENTRY(dialog->exec_cmd_entry));
235 reason = gnt_entry_get_text(GNT_ENTRY(dialog->popup_entry));
237 if (*reason == '\0') reason = NULL;
238 if (*message == '\0') message = NULL;
239 if (*command == '\0') command = NULL;
241 if (dialog->pounce == NULL) {
242 dialog->pounce = purple_pounce_new(FINCH_UI, dialog->account,
243 name, events, options);
244 } else {
245 purple_pounce_set_events(dialog->pounce, events);
246 purple_pounce_set_options(dialog->pounce, options);
247 purple_pounce_set_pouncer(dialog->pounce, dialog->account);
248 purple_pounce_set_pouncee(dialog->pounce, name);
251 /* Actions */
252 purple_pounce_action_set_enabled(dialog->pounce, "open-window",
253 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->open_win)));
254 purple_pounce_action_set_enabled(dialog->pounce, "popup-notify",
255 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->popup)));
256 purple_pounce_action_set_enabled(dialog->pounce, "send-message",
257 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->send_msg)));
258 purple_pounce_action_set_enabled(dialog->pounce, "execute-command",
259 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->exec_cmd)));
260 purple_pounce_action_set_enabled(dialog->pounce, "play-beep",
261 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->play_sound)));
263 purple_pounce_action_set_attribute(dialog->pounce, "send-message",
264 "message", message);
265 purple_pounce_action_set_attribute(dialog->pounce, "execute-command",
266 "command", command);
267 purple_pounce_action_set_attribute(dialog->pounce, "popup-notify",
268 "reason", reason);
270 /* Set the defaults for next time. */
271 purple_prefs_set_bool("/finch/pounces/default_actions/open-window",
272 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->open_win)));
273 purple_prefs_set_bool("/finch/pounces/default_actions/popup-notify",
274 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->popup)));
275 purple_prefs_set_bool("/finch/pounces/default_actions/send-message",
276 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->send_msg)));
277 purple_prefs_set_bool("/finch/pounces/default_actions/execute-command",
278 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->exec_cmd)));
279 purple_prefs_set_bool("/finch/pounces/default_actions/play-beep",
280 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->play_sound)));
282 purple_pounce_set_save(dialog->pounce,
283 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->save_pounce)));
285 purple_pounce_set_pouncer(dialog->pounce,
286 (PurpleAccount *)gnt_combo_box_get_selected_data(GNT_COMBO_BOX(dialog->account_menu)));
288 update_pounces();
290 gnt_widget_destroy(dialog->window);
294 void
295 finch_pounce_editor_show(PurpleAccount *account, const char *name,
296 PurplePounce *cur_pounce)
298 PurpleGntPounceDialog *dialog;
299 GntWidget *window;
300 GntWidget *bbox;
301 GntWidget *hbox, *vbox;
302 GntWidget *button;
303 GntWidget *combo;
304 GList *list;
306 g_return_if_fail((cur_pounce != NULL) ||
307 (account != NULL) ||
308 (purple_accounts_get_all() != NULL));
310 dialog = g_new0(PurpleGntPounceDialog, 1);
312 if (cur_pounce != NULL) {
313 dialog->pounce = cur_pounce;
314 dialog->account = purple_pounce_get_pouncer(cur_pounce);
315 } else if (account != NULL) {
316 dialog->pounce = NULL;
317 dialog->account = account;
318 } else {
319 GList *connections = purple_connections_get_all();
320 PurpleConnection *gc;
322 if (connections != NULL) {
323 gc = (PurpleConnection *)connections->data;
324 dialog->account = purple_connection_get_account(gc);
325 } else
326 dialog->account = purple_accounts_get_all()->data;
328 dialog->pounce = NULL;
331 /* Create the window. */
332 dialog->window = window = gnt_vbox_new(FALSE);
333 gnt_box_set_pad(GNT_BOX(window), 0);
334 gnt_box_set_toplevel(GNT_BOX(window), TRUE);
335 gnt_box_set_alignment(GNT_BOX(window), GNT_ALIGN_LEFT);
336 gnt_box_set_title(GNT_BOX(window),
337 (cur_pounce == NULL
338 ? _("New Buddy Pounce") : _("Edit Buddy Pounce")));
340 g_signal_connect(G_OBJECT(window), "destroy",
341 G_CALLBACK(delete_win_cb), dialog);
343 gnt_box_add_widget(GNT_BOX(window), gnt_label_new_with_format(_("Pounce on Whom"), GNT_TEXT_FLAG_BOLD));
345 /* Account: */
346 gnt_box_add_widget(GNT_BOX(window), gnt_label_new(_("Account:")));
347 dialog->account_menu = combo = gnt_combo_box_new();
348 list = purple_accounts_get_all();
349 for (; list; list = list->next)
351 PurpleAccount *account;
352 char *text;
354 account = list->data;
355 text = g_strdup_printf("%s (%s)",
356 purple_account_get_username(account),
357 purple_account_get_protocol_name(account));
358 gnt_combo_box_add_data(GNT_COMBO_BOX(combo), account, text);
359 g_free(text);
361 if (dialog->account)
362 gnt_combo_box_set_selected(GNT_COMBO_BOX(combo), dialog->account);
364 gnt_box_add_widget(GNT_BOX(window), combo);
366 /* Buddy: */
367 hbox = gnt_hbox_new(FALSE);
368 gnt_box_add_widget(GNT_BOX(hbox), gnt_label_new(_("Buddy name:")));
370 dialog->buddy_entry = gnt_entry_new(NULL);
371 gnt_box_add_widget(GNT_BOX(hbox), dialog->buddy_entry);
373 setup_buddy_list_suggestion(GNT_ENTRY(dialog->buddy_entry), TRUE);
375 gnt_box_add_widget(GNT_BOX(window), hbox);
377 if (cur_pounce != NULL) {
378 gnt_entry_set_text(GNT_ENTRY(dialog->buddy_entry),
379 purple_pounce_get_pouncee(cur_pounce));
380 } else if (name != NULL) {
381 gnt_entry_set_text(GNT_ENTRY(dialog->buddy_entry), name);
384 /* Create the event frame */
385 gnt_box_add_widget(GNT_BOX(window), gnt_line_new(FALSE));
386 gnt_box_add_widget(GNT_BOX(window), gnt_label_new_with_format(_("Pounce When Buddy..."), GNT_TEXT_FLAG_BOLD));
388 dialog->signon = gnt_check_box_new(_("Signs on"));
389 dialog->signoff = gnt_check_box_new(_("Signs off"));
390 dialog->away = gnt_check_box_new(_("Goes away"));
391 dialog->away_return = gnt_check_box_new(_("Returns from away"));
392 dialog->idle = gnt_check_box_new(_("Becomes idle"));
393 dialog->idle_return = gnt_check_box_new(_("Is no longer idle"));
394 dialog->typing = gnt_check_box_new(_("Starts typing"));
395 dialog->typed = gnt_check_box_new(_("Pauses while typing"));
396 dialog->stop_typing = gnt_check_box_new(_("Stops typing"));
397 dialog->message_recv = gnt_check_box_new(_("Sends a message"));
399 hbox = gnt_hbox_new(TRUE);
400 gnt_box_set_pad(GNT_BOX(hbox), 2);
402 vbox = gnt_vbox_new(FALSE);
403 gnt_box_set_pad(GNT_BOX(vbox), 0);
404 gnt_box_add_widget(GNT_BOX(hbox), vbox);
406 gnt_box_add_widget(GNT_BOX(vbox), dialog->signon);
407 gnt_box_add_widget(GNT_BOX(vbox), dialog->away);
408 gnt_box_add_widget(GNT_BOX(vbox), dialog->idle);
409 gnt_box_add_widget(GNT_BOX(vbox), dialog->typing);
410 gnt_box_add_widget(GNT_BOX(vbox), dialog->stop_typing);
412 vbox = gnt_vbox_new(FALSE);
413 gnt_box_set_pad(GNT_BOX(vbox), 0);
414 gnt_box_add_widget(GNT_BOX(hbox), vbox);
416 gnt_box_add_widget(GNT_BOX(vbox), dialog->signoff);
417 gnt_box_add_widget(GNT_BOX(vbox), dialog->away_return);
418 gnt_box_add_widget(GNT_BOX(vbox), dialog->idle_return);
419 gnt_box_add_widget(GNT_BOX(vbox), dialog->typed);
420 gnt_box_add_widget(GNT_BOX(vbox), dialog->message_recv);
422 gnt_box_add_widget(GNT_BOX(window), hbox);
424 /* Create the "Action" frame. */
425 gnt_box_add_widget(GNT_BOX(window), gnt_line_new(FALSE));
426 gnt_box_add_widget(GNT_BOX(window), gnt_label_new_with_format(_("Action"), GNT_TEXT_FLAG_BOLD));
428 dialog->open_win = gnt_check_box_new(_("Open an IM window"));
429 dialog->popup = gnt_check_box_new(_("Pop up a notification"));
430 dialog->send_msg = gnt_check_box_new(_("Send a message"));
431 dialog->exec_cmd = gnt_check_box_new(_("Execute a command"));
432 dialog->play_sound = gnt_check_box_new(_("Play a sound"));
434 dialog->send_msg_entry = gnt_entry_new(NULL);
435 dialog->exec_cmd_entry = gnt_entry_new(NULL);
436 dialog->popup_entry = gnt_entry_new(NULL);
437 dialog->exec_cmd_entry = gnt_entry_new(NULL);
439 hbox = gnt_hbox_new(FALSE);
440 gnt_box_add_widget(GNT_BOX(hbox), dialog->open_win);
441 gnt_box_add_widget(GNT_BOX(window), hbox);
442 hbox = gnt_hbox_new(FALSE);
443 gnt_box_add_widget(GNT_BOX(hbox), dialog->popup);
444 gnt_box_add_widget(GNT_BOX(hbox), dialog->popup_entry);
445 gnt_box_add_widget(GNT_BOX(window), hbox);
446 hbox = gnt_hbox_new(FALSE);
447 gnt_box_add_widget(GNT_BOX(hbox), dialog->send_msg);
448 gnt_box_add_widget(GNT_BOX(hbox), dialog->send_msg_entry);
449 gnt_box_add_widget(GNT_BOX(window), hbox);
450 hbox = gnt_hbox_new(FALSE);
451 gnt_box_add_widget(GNT_BOX(hbox), dialog->exec_cmd);
452 gnt_box_add_widget(GNT_BOX(hbox), dialog->exec_cmd_entry);
453 gnt_box_add_widget(GNT_BOX(window), hbox);
454 hbox = gnt_hbox_new(FALSE);
455 gnt_box_add_widget(GNT_BOX(hbox), dialog->play_sound);
456 gnt_box_add_widget(GNT_BOX(window), hbox);
458 gnt_box_add_widget(GNT_BOX(window), gnt_line_new(FALSE));
459 gnt_box_add_widget(GNT_BOX(window), gnt_label_new_with_format(_("Options"), GNT_TEXT_FLAG_BOLD));
460 dialog->on_away = gnt_check_box_new(_("Pounce only when my status is not Available"));
461 gnt_box_add_widget(GNT_BOX(window), dialog->on_away);
462 dialog->save_pounce = gnt_check_box_new(_("Recurring"));
463 gnt_box_add_widget(GNT_BOX(window), dialog->save_pounce);
466 gnt_box_add_widget(GNT_BOX(window), gnt_line_new(FALSE));
467 /* Now the button box! */
468 bbox = gnt_hbox_new(FALSE);
470 /* Cancel button */
471 button = gnt_button_new(_("Cancel"));
472 gnt_box_add_widget(GNT_BOX(bbox), button);
473 g_signal_connect(G_OBJECT(button), "activate",
474 G_CALLBACK(cancel_cb), dialog);
476 /* Save button */
477 dialog->save_button = button = gnt_button_new(_("Save"));
478 gnt_box_add_widget(GNT_BOX(bbox), button);
479 g_signal_connect(G_OBJECT(button), "activate",
480 G_CALLBACK(save_pounce_cb), dialog);
482 gnt_box_add_widget(GNT_BOX(window), bbox);
485 /* Set the values of stuff. */
486 if (cur_pounce != NULL)
488 PurplePounceEvent events = purple_pounce_get_events(cur_pounce);
489 PurplePounceOption options = purple_pounce_get_options(cur_pounce);
490 const char *value;
492 /* Options */
493 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->on_away),
494 (options & PURPLE_POUNCE_OPTION_AWAY));
496 /* Events */
497 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->signon),
498 (events & PURPLE_POUNCE_SIGNON));
499 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->signoff),
500 (events & PURPLE_POUNCE_SIGNOFF));
501 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->away),
502 (events & PURPLE_POUNCE_AWAY));
503 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->away_return),
504 (events & PURPLE_POUNCE_AWAY_RETURN));
505 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->idle),
506 (events & PURPLE_POUNCE_IDLE));
507 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->idle_return),
508 (events & PURPLE_POUNCE_IDLE_RETURN));
509 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->typing),
510 (events & PURPLE_POUNCE_TYPING));
511 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->typed),
512 (events & PURPLE_POUNCE_TYPED));
513 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->stop_typing),
514 (events & PURPLE_POUNCE_TYPING_STOPPED));
515 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->message_recv),
516 (events & PURPLE_POUNCE_MESSAGE_RECEIVED));
518 /* Actions */
519 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->open_win),
520 purple_pounce_action_is_enabled(cur_pounce, "open-window"));
521 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->popup),
522 purple_pounce_action_is_enabled(cur_pounce, "popup-notify"));
523 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->send_msg),
524 purple_pounce_action_is_enabled(cur_pounce, "send-message"));
525 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->exec_cmd),
526 purple_pounce_action_is_enabled(cur_pounce, "execute-command"));
527 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->play_sound),
528 purple_pounce_action_is_enabled(cur_pounce, "play-beep"));
530 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->save_pounce),
531 purple_pounce_get_save(cur_pounce));
533 if ((value = purple_pounce_action_get_attribute(cur_pounce,
534 "send-message",
535 "message")) != NULL)
537 gnt_entry_set_text(GNT_ENTRY(dialog->send_msg_entry), value);
540 if ((value = purple_pounce_action_get_attribute(cur_pounce,
541 "popup-notify",
542 "reason")) != NULL)
544 gnt_entry_set_text(GNT_ENTRY(dialog->popup_entry), value);
547 if ((value = purple_pounce_action_get_attribute(cur_pounce,
548 "execute-command",
549 "command")) != NULL)
551 gnt_entry_set_text(GNT_ENTRY(dialog->exec_cmd_entry), value);
554 else
556 PurpleBuddy *buddy = NULL;
558 if (name != NULL)
559 buddy = purple_blist_find_buddy(account, name);
561 /* Set some defaults */
562 if (buddy == NULL) {
563 gnt_check_box_set_checked(
564 GNT_CHECK_BOX(dialog->signon), TRUE);
565 } else {
566 if (!PURPLE_BUDDY_IS_ONLINE(buddy)) {
567 gnt_check_box_set_checked(
568 GNT_CHECK_BOX(dialog->signon), TRUE);
569 } else {
570 gboolean default_set = FALSE;
571 PurplePresence *presence = purple_buddy_get_presence(buddy);
573 if (purple_presence_is_idle(presence))
575 gnt_check_box_set_checked(
576 GNT_CHECK_BOX(dialog->idle_return), TRUE);
578 default_set = TRUE;
581 if (!purple_presence_is_available(presence))
583 gnt_check_box_set_checked(
584 GNT_CHECK_BOX(dialog->away_return), TRUE);
586 default_set = TRUE;
589 if (!default_set)
591 gnt_check_box_set_checked(
592 GNT_CHECK_BOX(dialog->signon), TRUE);
597 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->open_win),
598 purple_prefs_get_bool("/finch/pounces/default_actions/open-window"));
599 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->popup),
600 purple_prefs_get_bool("/finch/pounces/default_actions/popup-notify"));
601 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->send_msg),
602 purple_prefs_get_bool("/finch/pounces/default_actions/send-message"));
603 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->exec_cmd),
604 purple_prefs_get_bool("/finch/pounces/default_actions/execute-command"));
605 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->play_sound),
606 purple_prefs_get_bool("/finch/pounces/default_actions/play-beep"));
609 gnt_widget_show(window);
614 static gboolean
615 pounces_manager_destroy_cb(GntWidget *widget, gpointer user_data)
617 PouncesManager *dialog = user_data;
619 dialog->window = NULL;
620 finch_pounces_manager_hide();
622 return FALSE;
626 static void
627 pounces_manager_add_cb(GntButton *button, gpointer user_data)
629 if (purple_accounts_get_all() == NULL) {
630 purple_notify_error(NULL, _("Cannot create pounce"),
631 _("You do not have any accounts."),
632 _("You must create an account first before you can create a pounce."), NULL);
633 return;
635 finch_pounce_editor_show(NULL, NULL, NULL);
639 static void
640 pounces_manager_modify_cb(GntButton *button, gpointer user_data)
642 PouncesManager *dialog = user_data;
643 PurplePounce *pounce = gnt_tree_get_selection_data(GNT_TREE(dialog->tree));
644 if (pounce)
645 finch_pounce_editor_show(NULL, NULL, pounce);
648 static void
649 pounces_manager_delete_confirm_cb(PurplePounce *pounce)
651 gnt_tree_remove(GNT_TREE(pounces_manager->tree), pounce);
653 purple_request_close_with_handle(pounce);
654 purple_pounce_destroy(pounce);
658 static void
659 pounces_manager_delete_cb(GntButton *button, gpointer user_data)
661 PouncesManager *dialog = user_data;
662 PurplePounce *pounce;
663 PurpleAccount *account;
664 const char *pouncer, *pouncee;
665 char *buf;
667 pounce = (PurplePounce *)gnt_tree_get_selection_data(GNT_TREE(dialog->tree));
668 if (pounce == NULL)
669 return;
671 account = purple_pounce_get_pouncer(pounce);
672 pouncer = purple_account_get_username(account);
673 pouncee = purple_pounce_get_pouncee(pounce);
674 buf = g_strdup_printf(_("Are you sure you want to delete the pounce on %s for %s?"), pouncee, pouncer);
675 purple_request_action(pounce, NULL, buf, NULL, 0,
676 purple_request_cpar_from_account(account), pounce, 2,
677 _("Delete"), pounces_manager_delete_confirm_cb,
678 _("Cancel"), NULL);
679 g_free(buf);
682 static void
683 pounces_manager_close_cb(GntButton *button, gpointer user_data)
685 finch_pounces_manager_hide();
689 void
690 finch_pounces_manager_show(void)
692 PouncesManager *dialog;
693 GntWidget *bbox;
694 GntWidget *button;
695 GntWidget *tree;
696 GntWidget *win;
698 if (pounces_manager != NULL) {
699 gnt_window_present(pounces_manager->window);
700 return;
703 pounces_manager = dialog = g_new0(PouncesManager, 1);
705 dialog->window = win = gnt_vbox_new(FALSE);
706 gnt_box_set_toplevel(GNT_BOX(win), TRUE);
707 gnt_box_set_title(GNT_BOX(win), _("Buddy Pounces"));
708 gnt_box_set_pad(GNT_BOX(win), 0);
710 g_signal_connect(G_OBJECT(win), "destroy",
711 G_CALLBACK(pounces_manager_destroy_cb), dialog);
713 /* List of saved buddy pounces */
714 dialog->tree = tree = GNT_WIDGET(gnt_tree_new_with_columns(2));
715 gnt_tree_set_column_titles(GNT_TREE(tree), "Account", "Pouncee", NULL);
716 gnt_tree_set_show_title(GNT_TREE(tree), TRUE);
718 gnt_box_add_widget(GNT_BOX(win), tree);
720 /* Button box. */
721 bbox = gnt_hbox_new(FALSE);
723 /* Add button */
724 button = gnt_button_new(_("Add"));
725 gnt_box_add_widget(GNT_BOX(bbox), button);
726 gnt_util_set_trigger_widget(tree, GNT_KEY_INS, button);
728 g_signal_connect(G_OBJECT(button), "activate",
729 G_CALLBACK(pounces_manager_add_cb), dialog);
731 /* Modify button */
732 button = gnt_button_new(_("Modify"));
733 dialog->modify_button = button;
734 gnt_box_add_widget(GNT_BOX(bbox), button);
736 g_signal_connect(G_OBJECT(button), "activate",
737 G_CALLBACK(pounces_manager_modify_cb), dialog);
739 /* Delete button */
740 button = gnt_button_new(_("Delete"));
741 dialog->delete_button = button;
742 gnt_box_add_widget(GNT_BOX(bbox), button);
743 gnt_util_set_trigger_widget(tree, GNT_KEY_DEL, button);
745 g_signal_connect(G_OBJECT(button), "activate",
746 G_CALLBACK(pounces_manager_delete_cb), dialog);
748 /* Close button */
749 button = gnt_button_new(_("Close"));
750 gnt_box_add_widget(GNT_BOX(bbox), button);
751 gnt_widget_show(button);
753 g_signal_connect(G_OBJECT(button), "activate",
754 G_CALLBACK(pounces_manager_close_cb), dialog);
756 gnt_box_add_widget(GNT_BOX(win), bbox);
758 gnt_widget_show(win);
759 populate_pounces_list(pounces_manager);
762 void
763 finch_pounces_manager_hide(void)
765 if (pounces_manager == NULL)
766 return;
768 if (pounces_manager->window != NULL)
769 gnt_widget_destroy(pounces_manager->window);
771 purple_signals_disconnect_by_handle(pounces_manager);
773 g_free(pounces_manager);
774 pounces_manager = NULL;
777 static void
778 pounce_cb(PurplePounce *pounce, PurplePounceEvent events, void *data)
780 PurpleIMConversation *im;
781 PurpleAccount *account;
782 PurpleBuddy *buddy;
783 const char *pouncee;
784 const char *alias;
786 pouncee = purple_pounce_get_pouncee(pounce);
787 account = purple_pounce_get_pouncer(pounce);
789 buddy = purple_blist_find_buddy(account, pouncee);
790 if (buddy != NULL)
792 alias = purple_buddy_get_alias(buddy);
793 if (alias == NULL)
794 alias = pouncee;
796 else
797 alias = pouncee;
799 if (purple_pounce_action_is_enabled(pounce, "open-window"))
801 if (!purple_conversations_find_im_with_account(pouncee, account))
802 purple_im_conversation_new(account, pouncee);
805 if (purple_pounce_action_is_enabled(pounce, "popup-notify"))
807 char *tmp = NULL;
808 const char *name_shown;
809 const char *reason;
810 struct {
811 PurplePounceEvent event;
812 const char *format;
813 } messages[] = {
814 {PURPLE_POUNCE_TYPING, _("%s has started typing to you (%s)")},
815 {PURPLE_POUNCE_TYPED, _("%s has paused while typing to you (%s)")},
816 {PURPLE_POUNCE_SIGNON, _("%s has signed on (%s)")},
817 {PURPLE_POUNCE_IDLE_RETURN, _("%s has returned from being idle (%s)")},
818 {PURPLE_POUNCE_AWAY_RETURN, _("%s has returned from being away (%s)")},
819 {PURPLE_POUNCE_TYPING_STOPPED, _("%s has stopped typing to you (%s)")},
820 {PURPLE_POUNCE_SIGNOFF, _("%s has signed off (%s)")},
821 {PURPLE_POUNCE_IDLE, _("%s has become idle (%s)")},
822 {PURPLE_POUNCE_AWAY, _("%s has gone away. (%s)")},
823 {PURPLE_POUNCE_MESSAGE_RECEIVED, _("%s has sent you a message. (%s)")},
824 {0, NULL}
826 int i;
827 reason = purple_pounce_action_get_attribute(pounce, "popup-notify",
828 "reason");
831 * Here we place the protocol name in the pounce dialog to lessen
832 * confusion about what protocol a pounce is for.
834 for (i = 0; messages[i].format != NULL; i++) {
835 if (messages[i].event & events) {
836 tmp = g_strdup_printf(messages[i].format, alias,
837 purple_account_get_protocol_name(account));
838 break;
841 if (tmp == NULL)
842 tmp = g_strdup(_("Unknown pounce event. Please report this!"));
845 * Ok here is where I change the second argument, title, from
846 * NULL to the account alias if we have it or the account
847 * name if that's all we have
849 if ((name_shown = purple_account_get_private_alias(account)) == NULL)
850 name_shown = purple_account_get_username(account);
852 if (reason == NULL)
854 purple_notify_info(NULL, name_shown, tmp, purple_date_format_full(NULL), NULL);
856 else
858 char *tmp2 = g_strdup_printf("%s\n\n%s", reason, purple_date_format_full(NULL));
859 purple_notify_info(NULL, name_shown, tmp, tmp2, NULL);
860 g_free(tmp2);
862 g_free(tmp);
865 if (purple_pounce_action_is_enabled(pounce, "send-message"))
867 const char *message;
869 message = purple_pounce_action_get_attribute(pounce, "send-message",
870 "message");
872 if (message != NULL)
874 PurpleMessage *pmsg;
876 im = purple_conversations_find_im_with_account(pouncee, account);
878 if (im == NULL)
879 im = purple_im_conversation_new(account, pouncee);
881 pmsg = purple_message_new_outgoing(pouncee, message, 0);
882 purple_serv_send_im(purple_account_get_connection(account), pmsg);
883 purple_conversation_write_message(PURPLE_CONVERSATION(im), pmsg);
887 if (purple_pounce_action_is_enabled(pounce, "execute-command"))
889 #ifdef _WIN32
890 purple_debug_error("gntpounce", "execute-command is not supported on this OS");
891 #else
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);
921 #endif
924 if (purple_pounce_action_is_enabled(pounce, "play-beep"))
926 beep();
930 static void
931 free_pounce(PurplePounce *pounce)
933 update_pounces();
936 static void
937 new_pounce(PurplePounce *pounce)
939 purple_pounce_action_register(pounce, "open-window");
940 purple_pounce_action_register(pounce, "popup-notify");
941 purple_pounce_action_register(pounce, "send-message");
942 purple_pounce_action_register(pounce, "execute-command");
943 purple_pounce_action_register(pounce, "play-beep");
945 update_pounces();
948 void *
949 finch_pounces_get_handle()
951 static int handle;
953 return &handle;
956 void
957 finch_pounces_init(void)
959 purple_pounces_register_handler(FINCH_UI, pounce_cb, new_pounce,
960 free_pounce);
962 purple_prefs_add_none("/finch/pounces");
963 purple_prefs_add_none("/finch/pounces/default_actions");
964 purple_prefs_add_bool("/finch/pounces/default_actions/open-window",
965 FALSE);
966 purple_prefs_add_bool("/finch/pounces/default_actions/popup-notify",
967 TRUE);
968 purple_prefs_add_bool("/finch/pounces/default_actions/send-message",
969 FALSE);
970 purple_prefs_add_bool("/finch/pounces/default_actions/execute-command",
971 FALSE);
972 purple_prefs_add_bool("/finch/pounces/default_actions/play-beep",
973 FALSE);
974 purple_prefs_add_none("/finch/pounces/dialog");
976 purple_signal_connect(purple_connections_get_handle(), "signed-on",
977 finch_pounces_get_handle(),
978 PURPLE_CALLBACK(signed_on_off_cb), NULL);
979 purple_signal_connect(purple_connections_get_handle(), "signed-off",
980 finch_pounces_get_handle(),
981 PURPLE_CALLBACK(signed_on_off_cb), NULL);
984 /* XXX: There's no such thing in pidgin. Perhaps there should be?
985 * For sure, we don't need purple_pounces_unregister_handler -
986 * it's wiped in purple_pounces_uninit.
988 void finch_pounces_uninit()
990 purple_signals_disconnect_by_handle(finch_pounces_get_handle());