comparison finch/gntpounce.c @ 15818:0e3a8505ebbe

renamed gaim-text to finch
author Sean Egan <seanegan@gmail.com>
date Sun, 18 Mar 2007 19:38:15 +0000
parents
children 32c366eeeb99
comparison
equal deleted inserted replaced
15817:317e7613e581 15818:0e3a8505ebbe
1 /**
2 * @file gntpounce.c GNT Buddy Pounce API
3 * @ingroup gntui
4 *
5 * gaim
6 *
7 * Gaim is the legal property of its developers, whose names are too numerous
8 * to list here. Please refer to the COPYRIGHT file distributed with this
9 * source distribution.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26 #include <gnt.h>
27 #include <gntbox.h>
28 #include <gntbutton.h>
29 #include <gntcheckbox.h>
30 #include <gntcombobox.h>
31 #include <gntentry.h>
32 #include <gntlabel.h>
33 #include <gntline.h>
34 #include <gnttree.h>
35 #include "internal.h"
36 #include "gntgaim.h"
37
38 #include "account.h"
39 #include "conversation.h"
40 #include "debug.h"
41 #include "notify.h"
42 #include "prpl.h"
43 #include "request.h"
44 #include "server.h"
45 #include "util.h"
46
47 #include "gntpounce.h"
48
49
50 typedef struct
51 {
52 /* Pounce data */
53 GaimPounce *pounce;
54 GaimAccount *account;
55
56 /* The window */
57 GntWidget *window;
58
59 /* Pounce on Whom */
60 GntWidget *account_menu;
61 GntWidget *buddy_entry;
62
63 /* Pounce options */
64 GntWidget *on_away;
65
66 /* Pounce When Buddy... */
67 GntWidget *signon;
68 GntWidget *signoff;
69 GntWidget *away;
70 GntWidget *away_return;
71 GntWidget *idle;
72 GntWidget *idle_return;
73 GntWidget *typing;
74 GntWidget *typed;
75 GntWidget *stop_typing;
76 GntWidget *message_recv;
77
78 /* Action */
79 GntWidget *open_win;
80 GntWidget *popup;
81 GntWidget *popup_entry;
82 GntWidget *send_msg;
83 GntWidget *send_msg_entry;
84 GntWidget *exec_cmd;
85 GntWidget *exec_cmd_entry;
86 GntWidget *play_sound;
87
88 GntWidget *save_pounce;
89
90 /* Buttons */
91 GntWidget *save_button;
92
93 } GaimGntPounceDialog;
94
95 typedef struct
96 {
97 GntWidget *window;
98 GntWidget *tree;
99 GntWidget *modify_button;
100 GntWidget *delete_button;
101 } PouncesManager;
102
103 static PouncesManager *pounces_manager = NULL;
104
105 /**************************************************************************
106 * Callbacks
107 **************************************************************************/
108 static gint
109 delete_win_cb(GntWidget *w, GaimGntPounceDialog *dialog)
110 {
111 gnt_widget_destroy(dialog->window);
112 g_free(dialog);
113
114 return TRUE;
115 }
116
117 static void
118 cancel_cb(GntWidget *w, GaimGntPounceDialog *dialog)
119 {
120 gnt_widget_destroy(dialog->window);
121 }
122
123 static void
124 add_pounce_to_treeview(GntTree *tree, GaimPounce *pounce)
125 {
126 GaimAccount *account;
127 const char *pouncer;
128 const char *pouncee;
129
130 account = gaim_pounce_get_pouncer(pounce);
131 pouncer = gaim_account_get_username(account);
132 pouncee = gaim_pounce_get_pouncee(pounce);
133 gnt_tree_add_row_last(tree, pounce,
134 gnt_tree_create_row(tree, pouncer, pouncee), NULL);
135 }
136
137 static void
138 populate_pounces_list(PouncesManager *dialog)
139 {
140 const GList *pounces;
141
142 gnt_tree_remove_all(GNT_TREE(dialog->tree));
143
144 for (pounces = gaim_pounces_get_all(); pounces != NULL;
145 pounces = g_list_next(pounces))
146 {
147 add_pounce_to_treeview(GNT_TREE(dialog->tree), pounces->data);
148 }
149 }
150
151 static void
152 update_pounces(void)
153 {
154 /* Rebuild the pounces list if the pounces manager is open */
155 if (pounces_manager != NULL)
156 {
157 populate_pounces_list(pounces_manager);
158 }
159 }
160
161 static void
162 signed_on_off_cb(GaimConnection *gc, gpointer user_data)
163 {
164 update_pounces();
165 }
166
167 static void
168 save_pounce_cb(GntWidget *w, GaimGntPounceDialog *dialog)
169 {
170 const char *name;
171 const char *message, *command, *reason;
172 GaimPounceEvent events = GAIM_POUNCE_NONE;
173 GaimPounceOption options = GAIM_POUNCE_OPTION_NONE;
174
175 name = gnt_entry_get_text(GNT_ENTRY(dialog->buddy_entry));
176
177 if (*name == '\0')
178 {
179 gaim_notify_error(NULL, NULL,
180 _("Please enter a buddy to pounce."), NULL);
181 return;
182 }
183
184 /* Options */
185 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->on_away)))
186 options |= GAIM_POUNCE_OPTION_AWAY;
187
188 /* Events */
189 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->signon)))
190 events |= GAIM_POUNCE_SIGNON;
191
192 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->signoff)))
193 events |= GAIM_POUNCE_SIGNOFF;
194
195 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->away)))
196 events |= GAIM_POUNCE_AWAY;
197
198 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->away_return)))
199 events |= GAIM_POUNCE_AWAY_RETURN;
200
201 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->idle)))
202 events |= GAIM_POUNCE_IDLE;
203
204 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->idle_return)))
205 events |= GAIM_POUNCE_IDLE_RETURN;
206
207 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->typing)))
208 events |= GAIM_POUNCE_TYPING;
209
210 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->typed)))
211 events |= GAIM_POUNCE_TYPED;
212
213 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->stop_typing)))
214 events |= GAIM_POUNCE_TYPING_STOPPED;
215
216 if (gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->message_recv)))
217 events |= GAIM_POUNCE_MESSAGE_RECEIVED;
218
219 /* Data fields */
220 message = gnt_entry_get_text(GNT_ENTRY(dialog->send_msg_entry));
221 command = gnt_entry_get_text(GNT_ENTRY(dialog->exec_cmd_entry));
222 reason = gnt_entry_get_text(GNT_ENTRY(dialog->popup_entry));
223
224 if (*reason == '\0') reason = NULL;
225 if (*message == '\0') message = NULL;
226 if (*command == '\0') command = NULL;
227
228 if (dialog->pounce == NULL) {
229 dialog->pounce = gaim_pounce_new(GAIM_GNT_UI, dialog->account,
230 name, events, options);
231 } else {
232 gaim_pounce_set_events(dialog->pounce, events);
233 gaim_pounce_set_options(dialog->pounce, options);
234 gaim_pounce_set_pouncer(dialog->pounce, dialog->account);
235 gaim_pounce_set_pouncee(dialog->pounce, name);
236 }
237
238 /* Actions */
239 gaim_pounce_action_set_enabled(dialog->pounce, "open-window",
240 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->open_win)));
241 gaim_pounce_action_set_enabled(dialog->pounce, "popup-notify",
242 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->popup)));
243 gaim_pounce_action_set_enabled(dialog->pounce, "send-message",
244 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->send_msg)));
245 gaim_pounce_action_set_enabled(dialog->pounce, "execute-command",
246 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->exec_cmd)));
247 gaim_pounce_action_set_enabled(dialog->pounce, "play-beep",
248 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->play_sound)));
249
250 gaim_pounce_action_set_attribute(dialog->pounce, "send-message",
251 "message", message);
252 gaim_pounce_action_set_attribute(dialog->pounce, "execute-command",
253 "command", command);
254 gaim_pounce_action_set_attribute(dialog->pounce, "popup-notify",
255 "reason", reason);
256
257 /* Set the defaults for next time. */
258 gaim_prefs_set_bool("/gaim/gnt/pounces/default_actions/open-window",
259 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->open_win)));
260 gaim_prefs_set_bool("/gaim/gnt/pounces/default_actions/popup-notify",
261 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->popup)));
262 gaim_prefs_set_bool("/gaim/gnt/pounces/default_actions/send-message",
263 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->send_msg)));
264 gaim_prefs_set_bool("/gaim/gnt/pounces/default_actions/execute-command",
265 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->exec_cmd)));
266 gaim_prefs_set_bool("/gaim/gnt/pounces/default_actions/play-beep",
267 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->play_sound)));
268
269 gaim_pounce_set_save(dialog->pounce,
270 gnt_check_box_get_checked(GNT_CHECK_BOX(dialog->save_pounce)));
271
272 gaim_pounce_set_pouncer(dialog->pounce,
273 (GaimAccount *)gnt_combo_box_get_selected_data(GNT_COMBO_BOX(dialog->account_menu)));
274
275 update_pounces();
276
277 gnt_widget_destroy(dialog->window);
278 }
279
280
281 void
282 finch_pounce_editor_show(GaimAccount *account, const char *name,
283 GaimPounce *cur_pounce)
284 {
285 GaimGntPounceDialog *dialog;
286 GntWidget *window;
287 GntWidget *bbox;
288 GntWidget *hbox;
289 GntWidget *button;
290 GntWidget *combo;
291 GList *list;
292
293 g_return_if_fail((cur_pounce != NULL) ||
294 (account != NULL) ||
295 (gaim_accounts_get_all() != NULL));
296
297 dialog = g_new0(GaimGntPounceDialog, 1);
298
299 if (cur_pounce != NULL) {
300 dialog->pounce = cur_pounce;
301 dialog->account = gaim_pounce_get_pouncer(cur_pounce);
302 } else if (account != NULL) {
303 dialog->pounce = NULL;
304 dialog->account = account;
305 } else {
306 GList *connections = gaim_connections_get_all();
307 GaimConnection *gc;
308
309 if (connections != NULL) {
310 gc = (GaimConnection *)connections->data;
311 dialog->account = gaim_connection_get_account(gc);
312 } else
313 dialog->account = gaim_accounts_get_all()->data;
314
315 dialog->pounce = NULL;
316 }
317
318 /* Create the window. */
319 dialog->window = window = gnt_vbox_new(FALSE);
320 gnt_box_set_pad(GNT_BOX(window), 0);
321 gnt_box_set_toplevel(GNT_BOX(window), TRUE);
322 gnt_box_set_alignment(GNT_BOX(window), GNT_ALIGN_LEFT);
323 gnt_box_set_title(GNT_BOX(window),
324 (cur_pounce == NULL
325 ? _("New Buddy Pounce") : _("Edit Buddy Pounce")));
326
327 g_signal_connect(G_OBJECT(window), "destroy",
328 G_CALLBACK(delete_win_cb), dialog);
329
330 gnt_box_add_widget(GNT_BOX(window), gnt_label_new_with_format(_("Pounce Who"), GNT_TEXT_FLAG_BOLD));
331
332 /* Account: */
333 gnt_box_add_widget(GNT_BOX(window), gnt_label_new(_("Account:")));
334 dialog->account_menu = combo = gnt_combo_box_new();
335 list = gaim_accounts_get_all();
336 for (; list; list = list->next)
337 {
338 GaimAccount *account;
339 char *text;
340
341 account = list->data;
342 text = g_strdup_printf("%s (%s)",
343 gaim_account_get_username(account),
344 gaim_account_get_protocol_name(account));
345 gnt_combo_box_add_data(GNT_COMBO_BOX(combo), account, text);
346 g_free(text);
347 }
348 if (dialog->account)
349 gnt_combo_box_set_selected(GNT_COMBO_BOX(combo), dialog->account);
350
351 gnt_box_add_widget(GNT_BOX(window), combo);
352
353 /* Buddy: */
354 hbox = gnt_hbox_new(FALSE);
355 gnt_box_add_widget(GNT_BOX(hbox), gnt_label_new(_("Buddy name:")));
356
357 dialog->buddy_entry = gnt_entry_new(NULL);
358 gnt_box_add_widget(GNT_BOX(hbox), dialog->buddy_entry);
359
360 gnt_box_add_widget(GNT_BOX(window), hbox);
361
362 if (cur_pounce != NULL) {
363 gnt_entry_set_text(GNT_ENTRY(dialog->buddy_entry),
364 gaim_pounce_get_pouncee(cur_pounce));
365 } else if (name != NULL) {
366 gnt_entry_set_text(GNT_ENTRY(dialog->buddy_entry), name);
367 }
368
369 gnt_box_add_widget(GNT_BOX(window), gnt_line_new(FALSE));
370 gnt_box_add_widget(GNT_BOX(window), gnt_label_new_with_format(_("Pounce When Buddy..."), GNT_TEXT_FLAG_BOLD));
371
372 dialog->signon = gnt_check_box_new(_("Signs on"));
373 dialog->signoff = gnt_check_box_new(_("Signs off"));
374 dialog->away = gnt_check_box_new(_("Goes away"));
375 dialog->away_return = gnt_check_box_new(_("Returns from away"));
376 dialog->idle = gnt_check_box_new(_("Becomes idle"));
377 dialog->idle_return = gnt_check_box_new(_("Is no longer idle"));
378 dialog->typing = gnt_check_box_new(_("Starts typing"));
379 dialog->typed = gnt_check_box_new(_("Pauses while typing"));
380 dialog->stop_typing = gnt_check_box_new(_("Stops typing"));
381 dialog->message_recv = gnt_check_box_new(_("Sends a message"));
382
383 hbox = gnt_hbox_new(FALSE);
384 gnt_box_set_pad(GNT_BOX(hbox), 2);
385 gnt_box_add_widget(GNT_BOX(hbox), dialog->signon);
386 gnt_box_add_widget(GNT_BOX(hbox), dialog->signoff);
387 gnt_box_add_widget(GNT_BOX(window), hbox);
388 hbox = gnt_hbox_new(FALSE);
389 gnt_box_set_pad(GNT_BOX(hbox), 2);
390 gnt_box_add_widget(GNT_BOX(hbox), dialog->away);
391 gnt_box_add_widget(GNT_BOX(hbox), dialog->away_return);
392 gnt_box_add_widget(GNT_BOX(window), hbox);
393 hbox = gnt_hbox_new(FALSE);
394 gnt_box_set_pad(GNT_BOX(hbox), 2);
395 gnt_box_add_widget(GNT_BOX(hbox), dialog->idle);
396 gnt_box_add_widget(GNT_BOX(hbox), dialog->idle_return);
397 gnt_box_add_widget(GNT_BOX(window), hbox);
398 hbox = gnt_hbox_new(FALSE);
399 gnt_box_set_pad(GNT_BOX(hbox), 2);
400 gnt_box_add_widget(GNT_BOX(hbox), dialog->typing);
401 gnt_box_add_widget(GNT_BOX(hbox), dialog->typed);
402 gnt_box_add_widget(GNT_BOX(window), hbox);
403 hbox = gnt_hbox_new(FALSE);
404 gnt_box_set_pad(GNT_BOX(hbox), 2);
405 gnt_box_add_widget(GNT_BOX(hbox), dialog->stop_typing);
406 gnt_box_add_widget(GNT_BOX(hbox), dialog->message_recv);
407 gnt_box_add_widget(GNT_BOX(window), hbox);
408
409 /* Create the "Action" frame. */
410 gnt_box_add_widget(GNT_BOX(window), gnt_line_new(FALSE));
411 gnt_box_add_widget(GNT_BOX(window), gnt_label_new_with_format(_("Action"), GNT_TEXT_FLAG_BOLD));
412
413 dialog->open_win = gnt_check_box_new(_("Open an IM window"));
414 dialog->popup = gnt_check_box_new(_("Pop up a notification"));
415 dialog->send_msg = gnt_check_box_new(_("Send a message"));
416 dialog->exec_cmd = gnt_check_box_new(_("Execute a command"));
417 dialog->play_sound = gnt_check_box_new(_("Play a sound"));
418
419 dialog->send_msg_entry = gnt_entry_new(NULL);
420 dialog->exec_cmd_entry = gnt_entry_new(NULL);
421 dialog->popup_entry = gnt_entry_new(NULL);
422 dialog->exec_cmd_entry = gnt_entry_new(NULL);
423
424 hbox = gnt_hbox_new(FALSE);
425 gnt_box_add_widget(GNT_BOX(hbox), dialog->open_win);
426 gnt_box_add_widget(GNT_BOX(window), hbox);
427 hbox = gnt_hbox_new(FALSE);
428 gnt_box_add_widget(GNT_BOX(hbox), dialog->popup);
429 gnt_box_add_widget(GNT_BOX(hbox), dialog->popup_entry);
430 gnt_box_add_widget(GNT_BOX(window), hbox);
431 hbox = gnt_hbox_new(FALSE);
432 gnt_box_add_widget(GNT_BOX(hbox), dialog->send_msg);
433 gnt_box_add_widget(GNT_BOX(hbox), dialog->send_msg_entry);
434 gnt_box_add_widget(GNT_BOX(window), hbox);
435 hbox = gnt_hbox_new(FALSE);
436 gnt_box_add_widget(GNT_BOX(hbox), dialog->exec_cmd);
437 gnt_box_add_widget(GNT_BOX(hbox), dialog->exec_cmd_entry);
438 gnt_box_add_widget(GNT_BOX(window), hbox);
439 hbox = gnt_hbox_new(FALSE);
440 gnt_box_add_widget(GNT_BOX(hbox), dialog->play_sound);
441 gnt_box_add_widget(GNT_BOX(window), hbox);
442
443 gnt_box_add_widget(GNT_BOX(window), gnt_line_new(FALSE));
444 gnt_box_add_widget(GNT_BOX(window), gnt_label_new_with_format(_("Options"), GNT_TEXT_FLAG_BOLD));
445 dialog->on_away = gnt_check_box_new(_("Pounce only when my status is not available"));
446 gnt_box_add_widget(GNT_BOX(window), dialog->on_away);
447 dialog->save_pounce = gnt_check_box_new(_("Recurring"));
448 gnt_box_add_widget(GNT_BOX(window), dialog->save_pounce);
449
450
451 gnt_box_add_widget(GNT_BOX(window), gnt_line_new(FALSE));
452 /* Now the button box! */
453 bbox = gnt_hbox_new(TRUE);
454
455 /* Cancel button */
456 button = gnt_button_new(_("Cancel"));
457 gnt_box_add_widget(GNT_BOX(bbox), button);
458 g_signal_connect(G_OBJECT(button), "activate",
459 G_CALLBACK(cancel_cb), dialog);
460
461 /* Save button */
462 dialog->save_button = button = gnt_button_new(_("Save"));
463 gnt_box_add_widget(GNT_BOX(bbox), button);
464 g_signal_connect(G_OBJECT(button), "activate",
465 G_CALLBACK(save_pounce_cb), dialog);
466
467 gnt_box_add_widget(GNT_BOX(window), bbox);
468
469
470 /* Set the values of stuff. */
471 if (cur_pounce != NULL)
472 {
473 GaimPounceEvent events = gaim_pounce_get_events(cur_pounce);
474 GaimPounceOption options = gaim_pounce_get_options(cur_pounce);
475 const char *value;
476
477 /* Options */
478 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->on_away),
479 (options & GAIM_POUNCE_OPTION_AWAY));
480
481 /* Events */
482 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->signon),
483 (events & GAIM_POUNCE_SIGNON));
484 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->signoff),
485 (events & GAIM_POUNCE_SIGNOFF));
486 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->away),
487 (events & GAIM_POUNCE_AWAY));
488 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->away_return),
489 (events & GAIM_POUNCE_AWAY_RETURN));
490 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->idle),
491 (events & GAIM_POUNCE_IDLE));
492 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->idle_return),
493 (events & GAIM_POUNCE_IDLE_RETURN));
494 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->typing),
495 (events & GAIM_POUNCE_TYPING));
496 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->typed),
497 (events & GAIM_POUNCE_TYPED));
498 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->stop_typing),
499 (events & GAIM_POUNCE_TYPING_STOPPED));
500 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->message_recv),
501 (events & GAIM_POUNCE_MESSAGE_RECEIVED));
502
503 /* Actions */
504 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->open_win),
505 gaim_pounce_action_is_enabled(cur_pounce, "open-window"));
506 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->popup),
507 gaim_pounce_action_is_enabled(cur_pounce, "popup-notify"));
508 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->send_msg),
509 gaim_pounce_action_is_enabled(cur_pounce, "send-message"));
510 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->exec_cmd),
511 gaim_pounce_action_is_enabled(cur_pounce, "execute-command"));
512 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->play_sound),
513 gaim_pounce_action_is_enabled(cur_pounce, "play-beep"));
514
515 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->save_pounce),
516 gaim_pounce_get_save(cur_pounce));
517
518 if ((value = gaim_pounce_action_get_attribute(cur_pounce,
519 "send-message",
520 "message")) != NULL)
521 {
522 gnt_entry_set_text(GNT_ENTRY(dialog->send_msg_entry), value);
523 }
524
525 if ((value = gaim_pounce_action_get_attribute(cur_pounce,
526 "popup-notify",
527 "reason")) != NULL)
528 {
529 gnt_entry_set_text(GNT_ENTRY(dialog->popup_entry), value);
530 }
531
532 if ((value = gaim_pounce_action_get_attribute(cur_pounce,
533 "execute-command",
534 "command")) != NULL)
535 {
536 gnt_entry_set_text(GNT_ENTRY(dialog->exec_cmd_entry), value);
537 }
538 }
539 else
540 {
541 GaimBuddy *buddy = NULL;
542
543 if (name != NULL)
544 buddy = gaim_find_buddy(account, name);
545
546 /* Set some defaults */
547 if (buddy == NULL) {
548 gnt_check_box_set_checked(
549 GNT_CHECK_BOX(dialog->signon), TRUE);
550 } else {
551 if (!GAIM_BUDDY_IS_ONLINE(buddy)) {
552 gnt_check_box_set_checked(
553 GNT_CHECK_BOX(dialog->signon), TRUE);
554 } else {
555 gboolean default_set = FALSE;
556 GaimPresence *presence = gaim_buddy_get_presence(buddy);
557
558 if (gaim_presence_is_idle(presence))
559 {
560 gnt_check_box_set_checked(
561 GNT_CHECK_BOX(dialog->idle_return), TRUE);
562
563 default_set = TRUE;
564 }
565
566 if (!gaim_presence_is_available(presence))
567 {
568 gnt_check_box_set_checked(
569 GNT_CHECK_BOX(dialog->away_return), TRUE);
570
571 default_set = TRUE;
572 }
573
574 if (!default_set)
575 {
576 gnt_check_box_set_checked(
577 GNT_CHECK_BOX(dialog->signon), TRUE);
578 }
579 }
580 }
581
582 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->open_win),
583 gaim_prefs_get_bool("/gaim/gnt/pounces/default_actions/open-window"));
584 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->popup),
585 gaim_prefs_get_bool("/gaim/gnt/pounces/default_actions/popup-notify"));
586 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->send_msg),
587 gaim_prefs_get_bool("/gaim/gnt/pounces/default_actions/send-message"));
588 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->exec_cmd),
589 gaim_prefs_get_bool("/gaim/gnt/pounces/default_actions/execute-command"));
590 gnt_check_box_set_checked(GNT_CHECK_BOX(dialog->play_sound),
591 gaim_prefs_get_bool("/gaim/gnt/pounces/default_actions/play-beep"));
592 }
593
594 gnt_widget_show(window);
595 }
596
597
598
599 static gboolean
600 pounces_manager_destroy_cb(GntWidget *widget, gpointer user_data)
601 {
602 PouncesManager *dialog = user_data;
603
604 dialog->window = NULL;
605 finch_pounces_manager_hide();
606
607 return FALSE;
608 }
609
610
611 static void
612 pounces_manager_add_cb(GntButton *button, gpointer user_data)
613 {
614 finch_pounce_editor_show(NULL, NULL, NULL);
615 }
616
617
618 static void
619 pounces_manager_modify_cb(GntButton *button, gpointer user_data)
620 {
621 PouncesManager *dialog = user_data;
622 GaimPounce *pounce = gnt_tree_get_selection_data(GNT_TREE(dialog->tree));
623 finch_pounce_editor_show(NULL, NULL, pounce);
624 }
625
626 static void
627 pounces_manager_delete_confirm_cb(GaimPounce *pounce)
628 {
629 gnt_tree_remove(GNT_TREE(pounces_manager->tree), pounce);
630
631 gaim_request_close_with_handle(pounce);
632 gaim_pounce_destroy(pounce);
633 }
634
635
636 static void
637 pounces_manager_delete_cb(GntButton *button, gpointer user_data)
638 {
639 PouncesManager *dialog = user_data;
640 GaimPounce *pounce;
641 GaimAccount *account;
642 const char *pouncer, *pouncee;
643 char *buf;
644
645 pounce = (GaimPounce *)gnt_tree_get_selection_data(GNT_TREE(dialog->tree));
646 account = gaim_pounce_get_pouncer(pounce);
647 pouncer = gaim_account_get_username(account);
648 pouncee = gaim_pounce_get_pouncee(pounce);
649 buf = g_strdup_printf(_("Are you sure you want to delete the pounce on %s for %s?"), pouncee, pouncer);
650 gaim_request_action(pounce, NULL, buf, NULL, 0, pounce, 2,
651 _("Delete"), pounces_manager_delete_confirm_cb,
652 _("Cancel"), NULL);
653 g_free(buf);
654 }
655
656 static void
657 pounces_manager_close_cb(GntButton *button, gpointer user_data)
658 {
659 finch_pounces_manager_hide();
660 }
661
662
663 void
664 finch_pounces_manager_show(void)
665 {
666 PouncesManager *dialog;
667 GntWidget *bbox;
668 GntWidget *button;
669 GntWidget *tree;
670 GntWidget *win;
671
672 if (pounces_manager != NULL) {
673 return;
674 }
675
676 pounces_manager = dialog = g_new0(PouncesManager, 1);
677
678 dialog->window = win = gnt_vbox_new(FALSE);
679 gnt_box_set_toplevel(GNT_BOX(win), TRUE);
680 gnt_box_set_title(GNT_BOX(win), _("Buddy Pounces"));
681 gnt_box_set_pad(GNT_BOX(win), 0);
682
683 g_signal_connect(G_OBJECT(win), "destroy",
684 G_CALLBACK(pounces_manager_destroy_cb), dialog);
685
686 /* List of saved buddy pounces */
687 dialog->tree = tree = GNT_WIDGET(gnt_tree_new_with_columns(2));
688 gnt_tree_set_column_titles(GNT_TREE(tree), "Account", "Pouncee", NULL);
689 gnt_tree_set_show_title(GNT_TREE(tree), TRUE);
690
691 gnt_box_add_widget(GNT_BOX(win), tree);
692
693 /* Button box. */
694 bbox = gnt_hbox_new(TRUE);
695
696 /* Add button */
697 button = gnt_button_new(_("Add"));
698 gnt_box_add_widget(GNT_BOX(bbox), button);
699
700 g_signal_connect(G_OBJECT(button), "activate",
701 G_CALLBACK(pounces_manager_add_cb), dialog);
702
703 /* Modify button */
704 button = gnt_button_new(_("Modify"));
705 dialog->modify_button = button;
706 gnt_box_add_widget(GNT_BOX(bbox), button);
707
708 g_signal_connect(G_OBJECT(button), "activate",
709 G_CALLBACK(pounces_manager_modify_cb), dialog);
710
711 /* Delete button */
712 button = gnt_button_new(_("Delete"));
713 dialog->delete_button = button;
714 gnt_box_add_widget(GNT_BOX(bbox), button);
715
716 g_signal_connect(G_OBJECT(button), "activate",
717 G_CALLBACK(pounces_manager_delete_cb), dialog);
718
719 /* Close button */
720 button = gnt_button_new(_("Close"));
721 gnt_box_add_widget(GNT_BOX(bbox), button);
722 gnt_widget_show(button);
723
724 g_signal_connect(G_OBJECT(button), "activate",
725 G_CALLBACK(pounces_manager_close_cb), dialog);
726
727 gnt_box_add_widget(GNT_BOX(win), bbox);
728
729 gnt_widget_show(win);
730 populate_pounces_list(pounces_manager);
731 }
732
733 void
734 finch_pounces_manager_hide(void)
735 {
736 if (pounces_manager == NULL)
737 return;
738
739 if (pounces_manager->window != NULL)
740 gnt_widget_destroy(pounces_manager->window);
741
742 gaim_signals_disconnect_by_handle(pounces_manager);
743
744 g_free(pounces_manager);
745 pounces_manager = NULL;
746 }
747
748 static void
749 pounce_cb(GaimPounce *pounce, GaimPounceEvent events, void *data)
750 {
751 GaimConversation *conv;
752 GaimAccount *account;
753 GaimBuddy *buddy;
754 const char *pouncee;
755 const char *alias;
756
757 pouncee = gaim_pounce_get_pouncee(pounce);
758 account = gaim_pounce_get_pouncer(pounce);
759
760 buddy = gaim_find_buddy(account, pouncee);
761 if (buddy != NULL)
762 {
763 alias = gaim_buddy_get_alias(buddy);
764 if (alias == NULL)
765 alias = pouncee;
766 }
767 else
768 alias = pouncee;
769
770 if (gaim_pounce_action_is_enabled(pounce, "open-window"))
771 {
772 conv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM, pouncee, account);
773
774 if (conv == NULL)
775 conv = gaim_conversation_new(GAIM_CONV_TYPE_IM, account, pouncee);
776 }
777
778 if (gaim_pounce_action_is_enabled(pounce, "popup-notify"))
779 {
780 char *tmp;
781 const char *name_shown;
782 const char *reason;
783 reason = gaim_pounce_action_get_attribute(pounce, "popup-notify",
784 "reason");
785
786 /*
787 * Here we place the protocol name in the pounce dialog to lessen
788 * confusion about what protocol a pounce is for.
789 */
790 tmp = g_strdup_printf(
791 (events & GAIM_POUNCE_TYPING) ?
792 _("%s has started typing to you (%s)") :
793 (events & GAIM_POUNCE_TYPED) ?
794 _("%s has paused while typing to you (%s)") :
795 (events & GAIM_POUNCE_SIGNON) ?
796 _("%s has signed on (%s)") :
797 (events & GAIM_POUNCE_IDLE_RETURN) ?
798 _("%s has returned from being idle (%s)") :
799 (events & GAIM_POUNCE_AWAY_RETURN) ?
800 _("%s has returned from being away (%s)") :
801 (events & GAIM_POUNCE_TYPING_STOPPED) ?
802 _("%s has stopped typing to you (%s)") :
803 (events & GAIM_POUNCE_SIGNOFF) ?
804 _("%s has signed off (%s)") :
805 (events & GAIM_POUNCE_IDLE) ?
806 _("%s has become idle (%s)") :
807 (events & GAIM_POUNCE_AWAY) ?
808 _("%s has gone away. (%s)") :
809 (events & GAIM_POUNCE_MESSAGE_RECEIVED) ?
810 _("%s has sent you a message. (%s)") :
811 _("Unknown pounce event. Please report this!"),
812 alias, gaim_account_get_protocol_name(account));
813
814 /*
815 * Ok here is where I change the second argument, title, from
816 * NULL to the account alias if we have it or the account
817 * name if that's all we have
818 */
819 if ((name_shown = gaim_account_get_alias(account)) == NULL)
820 name_shown = gaim_account_get_username(account);
821
822 if (reason == NULL)
823 {
824 gaim_notify_info(NULL, name_shown, tmp, gaim_date_format_full(NULL));
825 }
826 else
827 {
828 char *tmp2 = g_strdup_printf("%s\n\n%s", reason, gaim_date_format_full(NULL));
829 gaim_notify_info(NULL, name_shown, tmp, tmp2);
830 g_free(tmp2);
831 }
832 g_free(tmp);
833 }
834
835 if (gaim_pounce_action_is_enabled(pounce, "send-message"))
836 {
837 const char *message;
838
839 message = gaim_pounce_action_get_attribute(pounce, "send-message",
840 "message");
841
842 if (message != NULL)
843 {
844 conv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM, pouncee, account);
845
846 if (conv == NULL)
847 conv = gaim_conversation_new(GAIM_CONV_TYPE_IM, account, pouncee);
848
849 gaim_conversation_write(conv, NULL, message,
850 GAIM_MESSAGE_SEND, time(NULL));
851
852 serv_send_im(account->gc, (char *)pouncee, (char *)message, 0);
853 }
854 }
855
856 if (gaim_pounce_action_is_enabled(pounce, "execute-command"))
857 {
858 const char *command;
859
860 command = gaim_pounce_action_get_attribute(pounce,
861 "execute-command", "command");
862
863 if (command != NULL)
864 {
865 char *localecmd = g_locale_from_utf8(command, -1, NULL,
866 NULL, NULL);
867
868 if (localecmd != NULL)
869 {
870 int pid = fork();
871
872 if (pid == 0) {
873 char *args[4];
874
875 args[0] = "sh";
876 args[1] = "-c";
877 args[2] = (char *)localecmd;
878 args[3] = NULL;
879
880 execvp(args[0], args);
881
882 _exit(0);
883 }
884 g_free(localecmd);
885 }
886 }
887 }
888
889 if (gaim_pounce_action_is_enabled(pounce, "play-beep"))
890 {
891 beep();
892 }
893 }
894
895 static void
896 free_pounce(GaimPounce *pounce)
897 {
898 update_pounces();
899 }
900
901 static void
902 new_pounce(GaimPounce *pounce)
903 {
904 gaim_pounce_action_register(pounce, "open-window");
905 gaim_pounce_action_register(pounce, "popup-notify");
906 gaim_pounce_action_register(pounce, "send-message");
907 gaim_pounce_action_register(pounce, "execute-command");
908 gaim_pounce_action_register(pounce, "play-beep");
909
910 update_pounces();
911 }
912
913 void *
914 finch_pounces_get_handle()
915 {
916 static int handle;
917
918 return &handle;
919 }
920
921 void
922 finch_pounces_init(void)
923 {
924 gaim_pounces_register_handler(GAIM_GNT_UI, pounce_cb, new_pounce,
925 free_pounce);
926
927 gaim_prefs_add_none("/gaim/gnt/pounces");
928 gaim_prefs_add_none("/gaim/gnt/pounces/default_actions");
929 gaim_prefs_add_bool("/gaim/gnt/pounces/default_actions/open-window",
930 FALSE);
931 gaim_prefs_add_bool("/gaim/gnt/pounces/default_actions/popup-notify",
932 TRUE);
933 gaim_prefs_add_bool("/gaim/gnt/pounces/default_actions/send-message",
934 FALSE);
935 gaim_prefs_add_bool("/gaim/gnt/pounces/default_actions/execute-command",
936 FALSE);
937 gaim_prefs_add_bool("/gaim/gnt/pounces/default_actions/play-beep",
938 FALSE);
939 gaim_prefs_add_none("/gaim/gnt/pounces/dialog");
940
941 gaim_signal_connect(gaim_connections_get_handle(), "signed-on",
942 finch_pounces_get_handle(),
943 GAIM_CALLBACK(signed_on_off_cb), NULL);
944 gaim_signal_connect(gaim_connections_get_handle(), "signed-off",
945 finch_pounces_get_handle(),
946 GAIM_CALLBACK(signed_on_off_cb), NULL);
947 }
948
949 /* XXX: There's no such thing in pidgin. Perhaps there should be? */
950 void finch_pounces_uninit()
951 {
952 gaim_pounces_register_handler(GAIM_GNT_UI, NULL, NULL, NULL);
953
954 gaim_signals_disconnect_by_handle(finch_pounces_get_handle());
955 }
956