comparison gtk/gtkpounce.c @ 14191:009db0b357b5

This is a hand-crafted commit to migrate across subversion revisions 16854:16861, due to some vagaries of the way the original renames were done. Witness that monotone can do in one revision what svn had to spread across several.
author Ethan Blanton <elb@pidgin.im>
date Sat, 16 Dec 2006 04:59:55 +0000
parents
children 33ac3c591b4a
comparison
equal deleted inserted replaced
14190:366be2ce35a7 14191:009db0b357b5
1 /**
2 * @file gtkpounce.c GTK+ Buddy Pounce API
3 * @ingroup gtkui
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 "internal.h"
27 #include "gtkgaim.h"
28
29 #include "account.h"
30 #include "conversation.h"
31 #include "debug.h"
32 #include "notify.h"
33 #include "prpl.h"
34 #include "request.h"
35 #include "server.h"
36 #include "sound.h"
37 #include "util.h"
38
39 #include "gtkblist.h"
40 #include "gtkdialogs.h"
41 #include "gtkpounce.h"
42 #include "gaimstock.h"
43 #include "gtkutils.h"
44
45 /**
46 * These are used for the GtkTreeView when you're scrolling through
47 * all your saved pounces.
48 */
49 enum
50 {
51 /* Hidden column containing the GaimPounce */
52 POUNCES_MANAGER_COLUMN_POUNCE,
53 POUNCES_MANAGER_COLUMN_ICON,
54 POUNCES_MANAGER_COLUMN_TARGET,
55 POUNCES_MANAGER_COLUMN_ACCOUNT,
56 POUNCES_MANAGER_COLUMN_RECURRING,
57 POUNCES_MANAGER_NUM_COLUMNS
58 };
59
60 typedef struct
61 {
62 /* Pounce data */
63 GaimPounce *pounce;
64 GaimAccount *account;
65
66 /* The window */
67 GtkWidget *window;
68
69 /* Pounce on Whom */
70 GtkWidget *account_menu;
71 GtkWidget *buddy_entry;
72
73 /* Pounce options */
74 GtkWidget *on_away;
75
76 /* Pounce When Buddy... */
77 GtkWidget *signon;
78 GtkWidget *signoff;
79 GtkWidget *away;
80 GtkWidget *away_return;
81 GtkWidget *idle;
82 GtkWidget *idle_return;
83 GtkWidget *typing;
84 GtkWidget *typed;
85 GtkWidget *stop_typing;
86 GtkWidget *message_recv;
87
88 /* Action */
89 GtkWidget *open_win;
90 GtkWidget *popup;
91 GtkWidget *popup_entry;
92 GtkWidget *send_msg;
93 GtkWidget *send_msg_entry;
94 GtkWidget *exec_cmd;
95 GtkWidget *exec_cmd_entry;
96 GtkWidget *exec_cmd_browse;
97 GtkWidget *play_sound;
98 GtkWidget *play_sound_entry;
99 GtkWidget *play_sound_browse;
100 GtkWidget *play_sound_test;
101
102 GtkWidget *save_pounce;
103
104 /* Buttons */
105 GtkWidget *save_button;
106
107 } GaimGtkPounceDialog;
108
109 typedef struct
110 {
111 GtkWidget *window;
112 GtkListStore *model;
113 GtkWidget *treeview;
114 GtkWidget *modify_button;
115 GtkWidget *delete_button;
116 } PouncesManager;
117
118 static PouncesManager *pounces_manager = NULL;
119
120 /**************************************************************************
121 * Callbacks
122 **************************************************************************/
123 static gint
124 delete_win_cb(GtkWidget *w, GdkEventAny *e, GaimGtkPounceDialog *dialog)
125 {
126 gtk_widget_destroy(dialog->window);
127 g_free(dialog);
128
129 return TRUE;
130 }
131
132 static void
133 cancel_cb(GtkWidget *w, GaimGtkPounceDialog *dialog)
134 {
135 delete_win_cb(NULL, NULL, dialog);
136 }
137
138 static void
139 pounce_update_entry_fields(void *user_data, const char *filename)
140 {
141 GtkWidget *entry = (GtkWidget *)user_data;
142
143 gtk_entry_set_text(GTK_ENTRY(entry), filename);
144 }
145
146 static void
147 filesel(GtkWidget *widget, gpointer data)
148 {
149 GtkWidget *entry;
150 const gchar *name;
151
152 entry = (GtkWidget *)data;
153 name = gtk_entry_get_text(GTK_ENTRY(entry));
154
155 gaim_request_file(entry, _("Select a file"), name, FALSE,
156 G_CALLBACK(pounce_update_entry_fields), NULL, entry);
157 }
158
159 static void
160 pounce_test_sound(GtkWidget *w, GtkWidget *entry)
161 {
162 const char *filename;
163
164 filename = gtk_entry_get_text(GTK_ENTRY(entry));
165
166 if (filename != NULL && *filename != '\0')
167 gaim_sound_play_file(filename, NULL);
168 else
169 gaim_sound_play_event(GAIM_SOUND_POUNCE_DEFAULT, NULL);
170 }
171
172 static void
173 add_pounce_to_treeview(GtkListStore *model, GaimPounce *pounce)
174 {
175 GtkTreeIter iter;
176 GaimAccount *account;
177 GaimPounceEvent events;
178 gboolean recurring;
179 const char *pouncer;
180 const char *pouncee;
181 GdkPixbuf *pixbuf;
182
183 account = gaim_pounce_get_pouncer(pounce);
184
185 if (gaim_account_is_disconnected(account))
186 return;
187
188 events = gaim_pounce_get_events(pounce);
189
190 pixbuf = gaim_gtk_create_prpl_icon(account, 0.5);
191
192 pouncer = gaim_account_get_username(account);
193 pouncee = gaim_pounce_get_pouncee(pounce);
194 recurring = gaim_pounce_get_save(pounce);
195
196 gtk_list_store_append(model, &iter);
197 gtk_list_store_set(model, &iter,
198 POUNCES_MANAGER_COLUMN_POUNCE, pounce,
199 POUNCES_MANAGER_COLUMN_ICON, pixbuf,
200 POUNCES_MANAGER_COLUMN_TARGET, pouncee,
201 POUNCES_MANAGER_COLUMN_ACCOUNT, pouncer,
202 POUNCES_MANAGER_COLUMN_RECURRING, recurring,
203 -1);
204
205 if (pixbuf != NULL)
206 g_object_unref(pixbuf);
207 }
208
209 static void
210 populate_pounces_list(PouncesManager *dialog)
211 {
212 const GList *pounces;
213
214 gtk_list_store_clear(dialog->model);
215
216 for (pounces = gaim_pounces_get_all(); pounces != NULL;
217 pounces = g_list_next(pounces))
218 {
219 add_pounce_to_treeview(dialog->model, pounces->data);
220 }
221 }
222
223 static void
224 update_pounces(void)
225 {
226 /* Rebuild the pounces list if the pounces manager is open */
227 if (pounces_manager != NULL)
228 {
229 populate_pounces_list(pounces_manager);
230 }
231 }
232
233 static void
234 signed_on_off_cb(GaimConnection *gc, gpointer user_data)
235 {
236 update_pounces();
237 }
238
239 static void
240 save_pounce_cb(GtkWidget *w, GaimGtkPounceDialog *dialog)
241 {
242 const char *name;
243 const char *message, *command, *sound, *reason;
244 GaimPounceEvent events = GAIM_POUNCE_NONE;
245 GaimPounceOption options = GAIM_POUNCE_OPTION_NONE;
246
247 name = gtk_entry_get_text(GTK_ENTRY(dialog->buddy_entry));
248
249 if (*name == '\0')
250 {
251 gaim_notify_error(NULL, NULL,
252 _("Please enter a buddy to pounce."), NULL);
253 return;
254 }
255
256 /* Options */
257 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->on_away)))
258 options |= GAIM_POUNCE_OPTION_AWAY;
259
260 /* Events */
261 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->signon)))
262 events |= GAIM_POUNCE_SIGNON;
263
264 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->signoff)))
265 events |= GAIM_POUNCE_SIGNOFF;
266
267 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->away)))
268 events |= GAIM_POUNCE_AWAY;
269
270 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->away_return)))
271 events |= GAIM_POUNCE_AWAY_RETURN;
272
273 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->idle)))
274 events |= GAIM_POUNCE_IDLE;
275
276 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->idle_return)))
277 events |= GAIM_POUNCE_IDLE_RETURN;
278
279 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->typing)))
280 events |= GAIM_POUNCE_TYPING;
281
282 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->typed)))
283 events |= GAIM_POUNCE_TYPED;
284
285 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->stop_typing)))
286 events |= GAIM_POUNCE_TYPING_STOPPED;
287
288 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->message_recv)))
289 events |= GAIM_POUNCE_MESSAGE_RECEIVED;
290
291 /* Data fields */
292 message = gtk_entry_get_text(GTK_ENTRY(dialog->send_msg_entry));
293 command = gtk_entry_get_text(GTK_ENTRY(dialog->exec_cmd_entry));
294 sound = gtk_entry_get_text(GTK_ENTRY(dialog->play_sound_entry));
295 reason = gtk_entry_get_text(GTK_ENTRY(dialog->popup_entry));
296
297 if (*reason == '\0') reason = NULL;
298 if (*message == '\0') message = NULL;
299 if (*command == '\0') command = NULL;
300 if (*sound == '\0') sound = NULL;
301
302 if (dialog->pounce == NULL)
303 {
304 dialog->pounce = gaim_pounce_new(GAIM_GTK_UI, dialog->account,
305 name, events, options);
306 }
307 else {
308 gaim_pounce_set_events(dialog->pounce, events);
309 gaim_pounce_set_options(dialog->pounce, options);
310 gaim_pounce_set_pouncer(dialog->pounce, dialog->account);
311 gaim_pounce_set_pouncee(dialog->pounce, name);
312 }
313
314 /* Actions */
315 gaim_pounce_action_set_enabled(dialog->pounce, "open-window",
316 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->open_win)));
317 gaim_pounce_action_set_enabled(dialog->pounce, "popup-notify",
318 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->popup)));
319 gaim_pounce_action_set_enabled(dialog->pounce, "send-message",
320 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->send_msg)));
321 gaim_pounce_action_set_enabled(dialog->pounce, "execute-command",
322 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->exec_cmd)));
323 gaim_pounce_action_set_enabled(dialog->pounce, "play-sound",
324 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->play_sound)));
325
326 gaim_pounce_action_set_attribute(dialog->pounce, "send-message",
327 "message", message);
328 gaim_pounce_action_set_attribute(dialog->pounce, "execute-command",
329 "command", command);
330 gaim_pounce_action_set_attribute(dialog->pounce, "play-sound",
331 "filename", sound);
332 gaim_pounce_action_set_attribute(dialog->pounce, "popup-notify",
333 "reason", reason);
334
335 /* Set the defaults for next time. */
336 gaim_prefs_set_bool("/gaim/gtk/pounces/default_actions/open-window",
337 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->open_win)));
338 gaim_prefs_set_bool("/gaim/gtk/pounces/default_actions/popup-notify",
339 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->popup)));
340 gaim_prefs_set_bool("/gaim/gtk/pounces/default_actions/send-message",
341 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->send_msg)));
342 gaim_prefs_set_bool("/gaim/gtk/pounces/default_actions/execute-command",
343 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->exec_cmd)));
344 gaim_prefs_set_bool("/gaim/gtk/pounces/default_actions/play-sound",
345 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->play_sound)));
346
347 gaim_pounce_set_save(dialog->pounce,
348 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->save_pounce)));
349
350 update_pounces();
351
352 delete_win_cb(NULL, NULL, dialog);
353 }
354
355 static void
356 pounce_choose_cb(GtkWidget *item, GaimAccount *account,
357 GaimGtkPounceDialog *dialog)
358 {
359 dialog->account = account;
360 }
361
362 static void
363 buddy_changed_cb(GtkEntry *entry, GaimGtkPounceDialog *dialog)
364 {
365 if (dialog->save_button == NULL)
366 return;
367
368 gtk_widget_set_sensitive(dialog->save_button,
369 *gtk_entry_get_text(entry) != '\0');
370 }
371
372 static void
373 message_recv_toggle(GtkButton *message_recv, GtkWidget *send_msg)
374 {
375 gboolean active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(message_recv));
376
377 gtk_widget_set_sensitive(send_msg, !active);
378 if (active)
379 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(send_msg), FALSE);
380 }
381
382 static void
383 pounce_dnd_recv(GtkWidget *widget, GdkDragContext *dc, gint x, gint y,
384 GtkSelectionData *sd, guint info, guint t, gpointer data)
385 {
386 GaimGtkPounceDialog *dialog;
387
388 if (sd->target == gdk_atom_intern("GAIM_BLIST_NODE", FALSE))
389 {
390 GaimBlistNode *node = NULL;
391 GaimBuddy *buddy;
392
393 memcpy(&node, sd->data, sizeof(node));
394
395 if (GAIM_BLIST_NODE_IS_CONTACT(node))
396 buddy = gaim_contact_get_priority_buddy((GaimContact *)node);
397 else if (GAIM_BLIST_NODE_IS_BUDDY(node))
398 buddy = (GaimBuddy *)node;
399 else
400 return;
401
402 dialog = (GaimGtkPounceDialog *)data;
403
404 gtk_entry_set_text(GTK_ENTRY(dialog->buddy_entry), buddy->name);
405 dialog->account = buddy->account;
406 gaim_gtk_account_option_menu_set_selected(dialog->account_menu, buddy->account);
407
408 gtk_drag_finish(dc, TRUE, (dc->action == GDK_ACTION_MOVE), t);
409 }
410 else if (sd->target == gdk_atom_intern("application/x-im-contact", FALSE))
411 {
412 char *protocol = NULL;
413 char *username = NULL;
414 GaimAccount *account;
415
416 if (gaim_gtk_parse_x_im_contact((const char *)sd->data, FALSE, &account,
417 &protocol, &username, NULL))
418 {
419 if (account == NULL)
420 {
421 gaim_notify_error(NULL, NULL,
422 _("You are not currently signed on with an account that "
423 "can add that buddy."), NULL);
424 }
425 else
426 {
427 dialog = (GaimGtkPounceDialog *)data;
428
429 gtk_entry_set_text(GTK_ENTRY(dialog->buddy_entry), username);
430 dialog->account = account;
431 gaim_gtk_account_option_menu_set_selected(dialog->account_menu, account);
432 }
433 }
434
435 g_free(username);
436 g_free(protocol);
437
438 gtk_drag_finish(dc, TRUE, (dc->action == GDK_ACTION_MOVE), t);
439 }
440 }
441
442 static const GtkTargetEntry dnd_targets[] =
443 {
444 {"GAIM_BLIST_NODE", GTK_TARGET_SAME_APP, 0},
445 {"application/x-im-contact", 0, 1}
446 };
447
448 void
449 gaim_gtk_pounce_editor_show(GaimAccount *account, const char *name,
450 GaimPounce *cur_pounce)
451 {
452 GaimGtkPounceDialog *dialog;
453 GtkWidget *window;
454 GtkWidget *label;
455 GtkWidget *bbox;
456 GtkWidget *vbox1, *vbox2;
457 GtkWidget *hbox;
458 GtkWidget *button;
459 GtkWidget *frame;
460 GtkWidget *table;
461 GtkSizeGroup *sg;
462 GPtrArray *sound_widgets;
463 GPtrArray *exec_widgets;
464
465 g_return_if_fail((cur_pounce != NULL) || (account != NULL) ||
466 (gaim_connections_get_all() != NULL));
467
468 dialog = g_new0(GaimGtkPounceDialog, 1);
469
470 if (cur_pounce != NULL)
471 {
472 dialog->pounce = cur_pounce;
473 dialog->account = gaim_pounce_get_pouncer(cur_pounce);
474 }
475 else if (account != NULL)
476 {
477 dialog->pounce = NULL;
478 dialog->account = account;
479 }
480 else
481 {
482 GaimConnection *gc;
483
484 gc = (GaimConnection *)gaim_connections_get_all()->data;
485
486 dialog->pounce = NULL;
487 dialog->account = gaim_connection_get_account(gc);
488 }
489
490 sg = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
491
492 /* Create the window. */
493 dialog->window = window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
494 gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
495 gtk_window_set_role(GTK_WINDOW(window), "buddy_pounce");
496 gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
497 gtk_window_set_title(GTK_WINDOW(window),
498 (cur_pounce == NULL
499 ? _("New Buddy Pounce") : _("Edit Buddy Pounce")));
500
501 gtk_container_set_border_width(GTK_CONTAINER(window), GAIM_HIG_BORDER);
502
503 g_signal_connect(G_OBJECT(window), "delete_event",
504 G_CALLBACK(delete_win_cb), dialog);
505
506 /* Create the parent vbox for everything. */
507 vbox1 = gtk_vbox_new(FALSE, GAIM_HIG_BORDER);
508 gtk_container_add(GTK_CONTAINER(window), vbox1);
509 gtk_widget_show(vbox1);
510
511 /* Create the vbox that will contain all the prefs stuff. */
512 vbox2 = gtk_vbox_new(FALSE, GAIM_HIG_BOX_SPACE);
513 gtk_box_pack_start(GTK_BOX(vbox1), vbox2, TRUE, TRUE, 0);
514
515 /* Create the "Pounce on Whom" frame. */
516 frame = gaim_gtk_make_frame(vbox2, _("Pounce on Whom"));
517
518 /* Account: */
519 hbox = gtk_hbox_new(FALSE, GAIM_HIG_BOX_SPACE);
520 gtk_box_pack_start(GTK_BOX(frame), hbox, FALSE, FALSE, 0);
521 gtk_widget_show(hbox);
522
523 label = gtk_label_new_with_mnemonic(_("_Account:"));
524 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
525 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
526 gtk_widget_show(label);
527 gtk_size_group_add_widget(sg, label);
528
529 dialog->account_menu =
530 gaim_gtk_account_option_menu_new(dialog->account, FALSE,
531 G_CALLBACK(pounce_choose_cb),
532 NULL, dialog);
533
534 gtk_box_pack_start(GTK_BOX(hbox), dialog->account_menu, FALSE, FALSE, 0);
535 gtk_widget_show(dialog->account_menu);
536 gaim_set_accessible_label (dialog->account_menu, label);
537
538 /* Buddy: */
539 hbox = gtk_hbox_new(FALSE, GAIM_HIG_BOX_SPACE);
540 gtk_box_pack_start(GTK_BOX(frame), hbox, FALSE, FALSE, 0);
541 gtk_widget_show(hbox);
542
543 label = gtk_label_new_with_mnemonic(_("_Buddy name:"));
544 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
545 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
546 gtk_widget_show(label);
547 gtk_size_group_add_widget(sg, label);
548
549 dialog->buddy_entry = gtk_entry_new();
550
551 gaim_gtk_setup_screenname_autocomplete(dialog->buddy_entry, dialog->account_menu, FALSE);
552
553 gtk_box_pack_start(GTK_BOX(hbox), dialog->buddy_entry, TRUE, TRUE, 0);
554 gtk_widget_show(dialog->buddy_entry);
555
556 g_signal_connect(G_OBJECT(dialog->buddy_entry), "changed",
557 G_CALLBACK(buddy_changed_cb), dialog);
558 gaim_set_accessible_label (dialog->buddy_entry, label);
559
560 if (cur_pounce != NULL) {
561 gtk_entry_set_text(GTK_ENTRY(dialog->buddy_entry),
562 gaim_pounce_get_pouncee(cur_pounce));
563 }
564 else if (name != NULL) {
565 gtk_entry_set_text(GTK_ENTRY(dialog->buddy_entry), name);
566 }
567
568 /* Create the "Pounce When Buddy..." frame. */
569 frame = gaim_gtk_make_frame(vbox2, _("Pounce When Buddy..."));
570
571 table = gtk_table_new(5, 2, FALSE);
572 gtk_container_add(GTK_CONTAINER(frame), table);
573 gtk_table_set_col_spacings(GTK_TABLE(table), GAIM_HIG_BORDER);
574 gtk_widget_show(table);
575
576 dialog->signon =
577 gtk_check_button_new_with_mnemonic(_("Si_gns on"));
578 dialog->signoff =
579 gtk_check_button_new_with_mnemonic(_("Signs o_ff"));
580 dialog->away =
581 gtk_check_button_new_with_mnemonic(_("Goes a_way"));
582 dialog->away_return =
583 gtk_check_button_new_with_mnemonic(_("Ret_urns from away"));
584 dialog->idle =
585 gtk_check_button_new_with_mnemonic(_("Becomes _idle"));
586 dialog->idle_return =
587 gtk_check_button_new_with_mnemonic(_("Is no longer i_dle"));
588 dialog->typing =
589 gtk_check_button_new_with_mnemonic(_("Starts _typing"));
590 dialog->typed =
591 gtk_check_button_new_with_mnemonic(_("P_auses while typing"));
592 dialog->stop_typing =
593 gtk_check_button_new_with_mnemonic(_("Stops t_yping"));
594 dialog->message_recv =
595 gtk_check_button_new_with_mnemonic(_("Sends a _message"));
596
597 gtk_table_attach(GTK_TABLE(table), dialog->message_recv, 0, 1, 0, 1,
598 GTK_FILL, 0, 0, 0);
599 gtk_table_attach(GTK_TABLE(table), dialog->signon, 0, 1, 1, 2,
600 GTK_FILL, 0, 0, 0);
601 gtk_table_attach(GTK_TABLE(table), dialog->signoff, 0, 1, 2, 3,
602 GTK_FILL, 0, 0, 0);
603 gtk_table_attach(GTK_TABLE(table), dialog->away, 0, 1, 3, 4,
604 GTK_FILL, 0, 0, 0);
605 gtk_table_attach(GTK_TABLE(table), dialog->away_return, 0, 1, 4, 5,
606 GTK_FILL, 0, 0, 0);
607 gtk_table_attach(GTK_TABLE(table), dialog->idle, 1, 2, 0, 1,
608 GTK_FILL, 0, 0, 0);
609 gtk_table_attach(GTK_TABLE(table), dialog->idle_return, 1, 2, 1, 2,
610 GTK_FILL, 0, 0, 0);
611 gtk_table_attach(GTK_TABLE(table), dialog->typing, 1, 2, 2, 3,
612 GTK_FILL, 0, 0, 0);
613 gtk_table_attach(GTK_TABLE(table), dialog->typed, 1, 2, 3, 4,
614 GTK_FILL, 0, 0, 0);
615 gtk_table_attach(GTK_TABLE(table), dialog->stop_typing, 1, 2, 4, 5,
616 GTK_FILL, 0, 0, 0);
617
618 gtk_widget_show(dialog->signon);
619 gtk_widget_show(dialog->signoff);
620 gtk_widget_show(dialog->away);
621 gtk_widget_show(dialog->away_return);
622 gtk_widget_show(dialog->idle);
623 gtk_widget_show(dialog->idle_return);
624 gtk_widget_show(dialog->typing);
625 gtk_widget_show(dialog->typed);
626 gtk_widget_show(dialog->stop_typing);
627 gtk_widget_show(dialog->message_recv);
628
629 /* Create the "Action" frame. */
630 frame = gaim_gtk_make_frame(vbox2, _("Action"));
631
632 table = gtk_table_new(3, 5, FALSE);
633 gtk_container_add(GTK_CONTAINER(frame), table);
634 gtk_table_set_col_spacings(GTK_TABLE(table), GAIM_HIG_BORDER);
635 gtk_widget_show(table);
636
637 dialog->open_win
638 = gtk_check_button_new_with_mnemonic(_("Ope_n an IM window"));
639 dialog->popup
640 = gtk_check_button_new_with_mnemonic(_("_Pop up a notification"));
641 dialog->send_msg
642 = gtk_check_button_new_with_mnemonic(_("Send a _message"));
643 dialog->exec_cmd
644 = gtk_check_button_new_with_mnemonic(_("E_xecute a command"));
645 dialog->play_sound
646 = gtk_check_button_new_with_mnemonic(_("P_lay a sound"));
647
648 dialog->send_msg_entry = gtk_entry_new();
649 dialog->exec_cmd_entry = gtk_entry_new();
650 dialog->popup_entry = gtk_entry_new();
651 dialog->exec_cmd_browse = gtk_button_new_with_mnemonic(_("Brows_e..."));
652 dialog->play_sound_entry = gtk_entry_new();
653 dialog->play_sound_browse = gtk_button_new_with_mnemonic(_("Br_owse..."));
654 dialog->play_sound_test = gtk_button_new_with_mnemonic(_("Pre_view"));
655
656 gtk_widget_set_sensitive(dialog->send_msg_entry, FALSE);
657 gtk_widget_set_sensitive(dialog->exec_cmd_entry, FALSE);
658 gtk_widget_set_sensitive(dialog->popup_entry, FALSE);
659 gtk_widget_set_sensitive(dialog->exec_cmd_browse, FALSE);
660 gtk_widget_set_sensitive(dialog->play_sound_entry, FALSE);
661 gtk_widget_set_sensitive(dialog->play_sound_browse, FALSE);
662 gtk_widget_set_sensitive(dialog->play_sound_test, FALSE);
663
664 sg = gtk_size_group_new(GTK_SIZE_GROUP_VERTICAL);
665 gtk_size_group_add_widget(sg, dialog->open_win);
666 gtk_size_group_add_widget(sg, dialog->popup);
667 gtk_size_group_add_widget(sg, dialog->popup_entry);
668 gtk_size_group_add_widget(sg, dialog->send_msg);
669 gtk_size_group_add_widget(sg, dialog->send_msg_entry);
670 gtk_size_group_add_widget(sg, dialog->exec_cmd);
671 gtk_size_group_add_widget(sg, dialog->exec_cmd_entry);
672 gtk_size_group_add_widget(sg, dialog->exec_cmd_browse);
673 gtk_size_group_add_widget(sg, dialog->play_sound);
674 gtk_size_group_add_widget(sg, dialog->play_sound_entry);
675 gtk_size_group_add_widget(sg, dialog->play_sound_browse);
676 gtk_size_group_add_widget(sg, dialog->play_sound_test);
677
678 gtk_table_attach(GTK_TABLE(table), dialog->open_win, 0, 1, 0, 1,
679 GTK_FILL, 0, 0, 0);
680 gtk_table_attach(GTK_TABLE(table), dialog->popup, 0, 1, 1, 2,
681 GTK_FILL, 0, 0, 0);
682 gtk_table_attach(GTK_TABLE(table), dialog->popup_entry, 1, 4, 1, 2,
683 GTK_FILL, 0, 0, 0);
684 gtk_table_attach(GTK_TABLE(table), dialog->send_msg, 0, 1, 2, 3,
685 GTK_FILL, 0, 0, 0);
686 gtk_table_attach(GTK_TABLE(table), dialog->send_msg_entry, 1, 4, 2, 3,
687 GTK_FILL, 0, 0, 0);
688 gtk_table_attach(GTK_TABLE(table), dialog->exec_cmd, 0, 1, 3, 4,
689 GTK_FILL, 0, 0, 0);
690 gtk_table_attach(GTK_TABLE(table), dialog->exec_cmd_entry, 1, 2, 3, 4,
691 GTK_FILL, 0, 0, 0);
692 gtk_table_attach(GTK_TABLE(table), dialog->exec_cmd_browse, 2, 3, 3, 4,
693 GTK_FILL | GTK_EXPAND, 0, 0, 0);
694 gtk_table_attach(GTK_TABLE(table), dialog->play_sound, 0, 1, 4, 5,
695 GTK_FILL, 0, 0, 0);
696 gtk_table_attach(GTK_TABLE(table), dialog->play_sound_entry, 1, 2, 4, 5,
697 GTK_FILL, 0, 0, 0);
698 gtk_table_attach(GTK_TABLE(table), dialog->play_sound_browse, 2, 3, 4, 5,
699 GTK_FILL | GTK_EXPAND, 0, 0, 0);
700 gtk_table_attach(GTK_TABLE(table), dialog->play_sound_test, 3, 4, 4, 5,
701 GTK_FILL | GTK_EXPAND, 0, 0, 0);
702
703 gtk_table_set_row_spacings(GTK_TABLE(table), GAIM_HIG_BOX_SPACE / 2);
704
705 gtk_widget_show(dialog->open_win);
706 gtk_widget_show(dialog->popup);
707 gtk_widget_show(dialog->popup_entry);
708 gtk_widget_show(dialog->send_msg);
709 gtk_widget_show(dialog->send_msg_entry);
710 gtk_widget_show(dialog->exec_cmd);
711 gtk_widget_show(dialog->exec_cmd_entry);
712 gtk_widget_show(dialog->exec_cmd_browse);
713 gtk_widget_show(dialog->play_sound);
714 gtk_widget_show(dialog->play_sound_entry);
715 gtk_widget_show(dialog->play_sound_browse);
716 gtk_widget_show(dialog->play_sound_test);
717
718 g_signal_connect(G_OBJECT(dialog->message_recv), "clicked",
719 G_CALLBACK(message_recv_toggle),
720 dialog->send_msg);
721
722 g_signal_connect(G_OBJECT(dialog->send_msg), "clicked",
723 G_CALLBACK(gaim_gtk_toggle_sensitive),
724 dialog->send_msg_entry);
725
726 g_signal_connect(G_OBJECT(dialog->popup), "clicked",
727 G_CALLBACK(gaim_gtk_toggle_sensitive),
728 dialog->popup_entry);
729
730 exec_widgets = g_ptr_array_new();
731 g_ptr_array_add(exec_widgets,dialog->exec_cmd_entry);
732 g_ptr_array_add(exec_widgets,dialog->exec_cmd_browse);
733
734 g_signal_connect(G_OBJECT(dialog->exec_cmd), "clicked",
735 G_CALLBACK(gaim_gtk_toggle_sensitive_array),
736 exec_widgets);
737 g_signal_connect(G_OBJECT(dialog->exec_cmd_browse), "clicked",
738 G_CALLBACK(filesel),
739 dialog->exec_cmd_entry);
740
741 sound_widgets = g_ptr_array_new();
742 g_ptr_array_add(sound_widgets,dialog->play_sound_entry);
743 g_ptr_array_add(sound_widgets,dialog->play_sound_browse);
744 g_ptr_array_add(sound_widgets,dialog->play_sound_test);
745
746 g_signal_connect(G_OBJECT(dialog->play_sound), "clicked",
747 G_CALLBACK(gaim_gtk_toggle_sensitive_array),
748 sound_widgets);
749 g_signal_connect(G_OBJECT(dialog->play_sound_browse), "clicked",
750 G_CALLBACK(filesel),
751 dialog->play_sound_entry);
752 g_signal_connect(G_OBJECT(dialog->play_sound_test), "clicked",
753 G_CALLBACK(pounce_test_sound),
754 dialog->play_sound_entry);
755
756 g_signal_connect(G_OBJECT(dialog->send_msg_entry), "activate",
757 G_CALLBACK(save_pounce_cb), dialog);
758 g_signal_connect(G_OBJECT(dialog->popup_entry), "activate",
759 G_CALLBACK(save_pounce_cb), dialog);
760 g_signal_connect(G_OBJECT(dialog->exec_cmd_entry), "activate",
761 G_CALLBACK(save_pounce_cb), dialog);
762 g_signal_connect(G_OBJECT(dialog->play_sound_entry), "activate",
763 G_CALLBACK(save_pounce_cb), dialog);
764
765 /* Create the "Options" frame. */
766 frame = gaim_gtk_make_frame(vbox2, _("Options"));
767
768 table = gtk_table_new(2, 1, FALSE);
769 gtk_container_add(GTK_CONTAINER(frame), table);
770 gtk_table_set_col_spacings(GTK_TABLE(table), GAIM_HIG_BORDER);
771 gtk_widget_show(table);
772
773 dialog->on_away =
774 gtk_check_button_new_with_mnemonic(_("P_ounce only when my status is not available"));
775 gtk_table_attach(GTK_TABLE(table), dialog->on_away, 0, 1, 0, 1,
776 GTK_FILL, 0, 0, 0);
777
778 dialog->save_pounce = gtk_check_button_new_with_mnemonic(
779 _("_Recurring"));
780 gtk_table_attach(GTK_TABLE(table), dialog->save_pounce, 0, 1, 1, 2,
781 GTK_FILL, 0, 0, 0);
782
783 gtk_widget_show(dialog->on_away);
784 gtk_widget_show(dialog->save_pounce);
785
786 /* Now the button box! */
787 bbox = gtk_hbutton_box_new();
788 gtk_box_set_spacing(GTK_BOX(bbox), GAIM_HIG_BOX_SPACE);
789 gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END);
790 gtk_box_pack_end(GTK_BOX(vbox1), bbox, FALSE, FALSE, 0);
791 gtk_widget_show(bbox);
792
793 /* Cancel button */
794 button = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
795 gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0);
796 gtk_widget_show(button);
797
798 g_signal_connect(G_OBJECT(button), "clicked",
799 G_CALLBACK(cancel_cb), dialog);
800
801 /* Save button */
802 dialog->save_button = button = gtk_button_new_from_stock(GTK_STOCK_SAVE);
803 gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0);
804 gtk_widget_show(button);
805
806 g_signal_connect(G_OBJECT(button), "clicked",
807 G_CALLBACK(save_pounce_cb), dialog);
808
809 if (*gtk_entry_get_text(GTK_ENTRY(dialog->buddy_entry)) == '\0')
810 gtk_widget_set_sensitive(button, FALSE);
811
812 /* Setup drag-and-drop */
813 gtk_drag_dest_set(window,
814 GTK_DEST_DEFAULT_MOTION |
815 GTK_DEST_DEFAULT_DROP,
816 dnd_targets,
817 sizeof(dnd_targets) / sizeof(GtkTargetEntry),
818 GDK_ACTION_COPY);
819 gtk_drag_dest_set(dialog->buddy_entry,
820 GTK_DEST_DEFAULT_MOTION |
821 GTK_DEST_DEFAULT_DROP,
822 dnd_targets,
823 sizeof(dnd_targets) / sizeof(GtkTargetEntry),
824 GDK_ACTION_COPY);
825
826 g_signal_connect(G_OBJECT(window), "drag_data_received",
827 G_CALLBACK(pounce_dnd_recv), dialog);
828 g_signal_connect(G_OBJECT(dialog->buddy_entry), "drag_data_received",
829 G_CALLBACK(pounce_dnd_recv), dialog);
830
831 /* Set the values of stuff. */
832 if (cur_pounce != NULL)
833 {
834 GaimPounceEvent events = gaim_pounce_get_events(cur_pounce);
835 GaimPounceOption options = gaim_pounce_get_options(cur_pounce);
836 const char *value;
837
838 /* Options */
839 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->on_away),
840 (options & GAIM_POUNCE_OPTION_AWAY));
841
842 /* Events */
843 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->signon),
844 (events & GAIM_POUNCE_SIGNON));
845 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->signoff),
846 (events & GAIM_POUNCE_SIGNOFF));
847 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->away),
848 (events & GAIM_POUNCE_AWAY));
849 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->away_return),
850 (events & GAIM_POUNCE_AWAY_RETURN));
851 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->idle),
852 (events & GAIM_POUNCE_IDLE));
853 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->idle_return),
854 (events & GAIM_POUNCE_IDLE_RETURN));
855 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->typing),
856 (events & GAIM_POUNCE_TYPING));
857 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->typed),
858 (events & GAIM_POUNCE_TYPED));
859 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->stop_typing),
860 (events & GAIM_POUNCE_TYPING_STOPPED));
861 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->message_recv),
862 (events & GAIM_POUNCE_MESSAGE_RECEIVED));
863
864 /* Actions */
865 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->open_win),
866 gaim_pounce_action_is_enabled(cur_pounce, "open-window"));
867 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->popup),
868 gaim_pounce_action_is_enabled(cur_pounce, "popup-notify"));
869 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->send_msg),
870 gaim_pounce_action_is_enabled(cur_pounce, "send-message"));
871 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->exec_cmd),
872 gaim_pounce_action_is_enabled(cur_pounce, "execute-command"));
873 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->play_sound),
874 gaim_pounce_action_is_enabled(cur_pounce, "play-sound"));
875
876 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->save_pounce),
877 gaim_pounce_get_save(cur_pounce));
878
879 if ((value = gaim_pounce_action_get_attribute(cur_pounce,
880 "send-message",
881 "message")) != NULL)
882 {
883 gtk_entry_set_text(GTK_ENTRY(dialog->send_msg_entry), value);
884 }
885
886 if ((value = gaim_pounce_action_get_attribute(cur_pounce,
887 "popup-notify",
888 "reason")) != NULL)
889 {
890 gtk_entry_set_text(GTK_ENTRY(dialog->popup_entry), value);
891 }
892
893 if ((value = gaim_pounce_action_get_attribute(cur_pounce,
894 "execute-command",
895 "command")) != NULL)
896 {
897 gtk_entry_set_text(GTK_ENTRY(dialog->exec_cmd_entry), value);
898 }
899
900 if ((value = gaim_pounce_action_get_attribute(cur_pounce,
901 "play-sound",
902 "filename")) != NULL)
903 {
904 gtk_entry_set_text(GTK_ENTRY(dialog->play_sound_entry), value);
905 }
906 }
907 else
908 {
909 GaimBuddy *buddy = NULL;
910
911 if (name != NULL)
912 buddy = gaim_find_buddy(account, name);
913
914 /* Set some defaults */
915 if (buddy == NULL)
916 {
917 gtk_toggle_button_set_active(
918 GTK_TOGGLE_BUTTON(dialog->signon), TRUE);
919 }
920 else
921 {
922 if (!GAIM_BUDDY_IS_ONLINE(buddy))
923 {
924 gtk_toggle_button_set_active(
925 GTK_TOGGLE_BUTTON(dialog->signon), TRUE);
926 }
927 else
928 {
929 gboolean default_set = FALSE;
930 GaimPresence *presence = gaim_buddy_get_presence(buddy);
931
932 if (gaim_presence_is_idle(presence))
933 {
934 gtk_toggle_button_set_active(
935 GTK_TOGGLE_BUTTON(dialog->idle_return), TRUE);
936
937 default_set = TRUE;
938 }
939
940 if (!gaim_presence_is_available(presence))
941 {
942 gtk_toggle_button_set_active(
943 GTK_TOGGLE_BUTTON(dialog->away_return), TRUE);
944
945 default_set = TRUE;
946 }
947
948 if (!default_set)
949 {
950 gtk_toggle_button_set_active(
951 GTK_TOGGLE_BUTTON(dialog->signon), TRUE);
952 }
953 }
954 }
955
956 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->open_win),
957 gaim_prefs_get_bool("/gaim/gtk/pounces/default_actions/open-window"));
958 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->popup),
959 gaim_prefs_get_bool("/gaim/gtk/pounces/default_actions/popup-notify"));
960 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->send_msg),
961 gaim_prefs_get_bool("/gaim/gtk/pounces/default_actions/send-message"));
962 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->exec_cmd),
963 gaim_prefs_get_bool("/gaim/gtk/pounces/default_actions/execute-command"));
964 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->play_sound),
965 gaim_prefs_get_bool("/gaim/gtk/pounces/default_actions/play-sound"));
966 }
967
968 gtk_widget_show_all(vbox2);
969 gtk_widget_show(window);
970 }
971
972 static gboolean
973 pounces_manager_configure_cb(GtkWidget *widget, GdkEventConfigure *event, PouncesManager *dialog)
974 {
975 if (GTK_WIDGET_VISIBLE(widget)) {
976 gaim_prefs_set_int("/gaim/gtk/pounces/dialog/width", event->width);
977 gaim_prefs_set_int("/gaim/gtk/pounces/dialog/height", event->height);
978 }
979
980 return FALSE;
981 }
982
983 static gboolean
984 pounces_manager_find_pounce(GtkTreeIter *iter, GaimPounce *pounce)
985 {
986 GtkTreeModel *model = GTK_TREE_MODEL(pounces_manager->model);
987 GaimPounce *p;
988
989 if (!gtk_tree_model_get_iter_first(model, iter))
990 return FALSE;
991
992 gtk_tree_model_get(model, iter, POUNCES_MANAGER_COLUMN_POUNCE, &p, -1);
993 if (pounce == p)
994 return TRUE;
995
996 while (gtk_tree_model_iter_next(model, iter))
997 {
998 gtk_tree_model_get(model, iter, POUNCES_MANAGER_COLUMN_POUNCE, &p, -1);
999 if (pounce == p)
1000 return TRUE;
1001 }
1002
1003 return FALSE;
1004 }
1005
1006 static gboolean
1007 pounces_manager_destroy_cb(GtkWidget *widget, GdkEvent *event, gpointer user_data)
1008 {
1009 PouncesManager *dialog = user_data;
1010
1011 dialog->window = NULL;
1012 gaim_gtk_pounces_manager_hide();
1013
1014 return FALSE;
1015 }
1016
1017 #if !GTK_CHECK_VERSION(2,2,0)
1018 static void
1019 count_selected_helper(GtkTreeModel *model, GtkTreePath *path,
1020 GtkTreeIter *iter, gpointer user_data)
1021 {
1022 (*(gint *)user_data)++;
1023 }
1024 #endif
1025
1026 static void
1027 pounces_manager_connection_cb(GaimConnection *gc, GtkWidget *add_button)
1028 {
1029 gtk_widget_set_sensitive(add_button, (gaim_connections_get_all() != NULL));
1030 }
1031
1032 static void
1033 pounces_manager_add_cb(GtkButton *button, gpointer user_data)
1034 {
1035 gaim_gtk_pounce_editor_show(NULL, NULL, NULL);
1036 }
1037
1038 static void
1039 pounces_manager_modify_foreach(GtkTreeModel *model, GtkTreePath *path,
1040 GtkTreeIter *iter, gpointer user_data)
1041 {
1042 GaimPounce *pounce;
1043
1044 gtk_tree_model_get(model, iter, POUNCES_MANAGER_COLUMN_POUNCE, &pounce, -1);
1045 gaim_gtk_pounce_editor_show(NULL, NULL, pounce);
1046 }
1047
1048 static void
1049 pounces_manager_modify_cb(GtkButton *button, gpointer user_data)
1050 {
1051 PouncesManager *dialog = user_data;
1052 GtkTreeSelection *selection;
1053
1054 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dialog->treeview));
1055
1056 gtk_tree_selection_selected_foreach(selection, pounces_manager_modify_foreach, user_data);
1057 }
1058
1059 static void
1060 pounces_manager_delete_confirm_cb(GaimPounce *pounce)
1061 {
1062 GtkTreeIter iter;
1063
1064 if (pounces_manager && pounces_manager_find_pounce(&iter, pounce))
1065 gtk_list_store_remove(pounces_manager->model, &iter);
1066
1067 gaim_request_close_with_handle(pounce);
1068 gaim_pounce_destroy(pounce);
1069 }
1070
1071 static void
1072 pounces_manager_delete_foreach(GtkTreeModel *model, GtkTreePath *path,
1073 GtkTreeIter *iter, gpointer user_data)
1074 {
1075 GaimPounce *pounce;
1076 GaimAccount *account;
1077 const char *pouncer, *pouncee;
1078 char *buf;
1079
1080 gtk_tree_model_get(model, iter, POUNCES_MANAGER_COLUMN_POUNCE, &pounce, -1);
1081 account = gaim_pounce_get_pouncer(pounce);
1082 pouncer = gaim_account_get_username(account);
1083 pouncee = gaim_pounce_get_pouncee(pounce);
1084
1085 buf = g_strdup_printf(_("Are you sure you want to delete the pounce on %s for %s?"), pouncee, pouncer);
1086 gaim_request_action(pounce, NULL, buf, NULL, 0, pounce, 2,
1087 _("Delete"), pounces_manager_delete_confirm_cb,
1088 _("Cancel"), NULL);
1089 g_free(buf);
1090 }
1091
1092 static void
1093 pounces_manager_delete_cb(GtkButton *button, gpointer user_data)
1094 {
1095 PouncesManager *dialog = user_data;
1096 GtkTreeSelection *selection;
1097
1098 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dialog->treeview));
1099
1100 gtk_tree_selection_selected_foreach(selection, pounces_manager_delete_foreach, user_data);
1101 }
1102
1103 static void
1104 pounces_manager_close_cb(GtkButton *button, gpointer user_data)
1105 {
1106 gaim_gtk_pounces_manager_hide();
1107 }
1108
1109 static void
1110 pounce_selected_cb(GtkTreeSelection *sel, gpointer user_data)
1111 {
1112 PouncesManager *dialog = user_data;
1113 int num_selected = 0;
1114
1115 #if GTK_CHECK_VERSION(2,2,0)
1116 num_selected = gtk_tree_selection_count_selected_rows(sel);
1117 #else
1118 gtk_tree_selection_selected_foreach(sel, count_selected_helper, &num_selected);
1119 #endif
1120
1121 gtk_widget_set_sensitive(dialog->modify_button, (num_selected > 0));
1122 gtk_widget_set_sensitive(dialog->delete_button, (num_selected > 0));
1123 }
1124
1125 static gboolean
1126 pounce_double_click_cb(GtkTreeView *treeview, GdkEventButton *event, gpointer user_data)
1127 {
1128 PouncesManager *dialog = user_data;
1129 GtkTreePath *path;
1130 GtkTreeIter iter;
1131 GaimPounce *pounce;
1132
1133 /* Figure out which node was clicked */
1134 if (!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(dialog->treeview), event->x, event->y, &path, NULL, NULL, NULL))
1135 return FALSE;
1136 gtk_tree_model_get_iter(GTK_TREE_MODEL(dialog->model), &iter, path);
1137 gtk_tree_path_free(path);
1138 gtk_tree_model_get(GTK_TREE_MODEL(dialog->model), &iter, POUNCES_MANAGER_COLUMN_POUNCE, &pounce, -1);
1139
1140 if ((pounce != NULL) && (event->button == 1) &&
1141 (event->type == GDK_2BUTTON_PRESS))
1142 {
1143 gaim_gtk_pounce_editor_show(NULL, NULL, pounce);
1144 return TRUE;
1145 }
1146
1147 return FALSE;
1148 }
1149
1150 static void
1151 pounces_manager_recurring_cb(GtkCellRendererToggle *renderer, gchar *path_str,
1152 gpointer user_data)
1153 {
1154 PouncesManager *dialog = user_data;
1155 GaimPounce *pounce;
1156 gboolean recurring;
1157 GtkTreeModel *model = GTK_TREE_MODEL(dialog->model);
1158 GtkTreeIter iter;
1159
1160 gtk_tree_model_get_iter_from_string(model, &iter, path_str);
1161 gtk_tree_model_get(model, &iter,
1162 POUNCES_MANAGER_COLUMN_POUNCE, &pounce,
1163 POUNCES_MANAGER_COLUMN_RECURRING, &recurring,
1164 -1);
1165
1166 gaim_pounce_set_save(pounce, !recurring);
1167
1168 update_pounces();
1169 }
1170
1171 static gboolean
1172 search_func(GtkTreeModel *model, gint column, const gchar *key, GtkTreeIter *iter, gpointer search_data)
1173 {
1174 gboolean result;
1175 char *haystack;
1176
1177 gtk_tree_model_get(model, iter, column, &haystack, -1);
1178
1179 result = (gaim_strcasestr(haystack, key) == NULL);
1180
1181 g_free(haystack);
1182
1183 return result;
1184 }
1185
1186 static GtkWidget *
1187 create_pounces_list(PouncesManager *dialog)
1188 {
1189 GtkWidget *sw;
1190 GtkWidget *treeview;
1191 GtkTreeSelection *sel;
1192 GtkTreeViewColumn *column;
1193 GtkCellRenderer *renderer;
1194
1195 /* Create the scrolled window */
1196 sw = gtk_scrolled_window_new(0, 0);
1197 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
1198 GTK_POLICY_AUTOMATIC,
1199 GTK_POLICY_AUTOMATIC);
1200 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw),
1201 GTK_SHADOW_IN);
1202 gtk_widget_show(sw);
1203
1204 /* Create the list model */
1205 dialog->model = gtk_list_store_new(POUNCES_MANAGER_NUM_COLUMNS,
1206 G_TYPE_POINTER,
1207 GDK_TYPE_PIXBUF,
1208 G_TYPE_STRING,
1209 G_TYPE_STRING,
1210 G_TYPE_BOOLEAN
1211 );
1212
1213 /* Create the treeview */
1214 treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(dialog->model));
1215 dialog->treeview = treeview;
1216 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview), TRUE);
1217
1218 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
1219 gtk_tree_selection_set_mode(sel, GTK_SELECTION_MULTIPLE);
1220 g_signal_connect(G_OBJECT(sel), "changed",
1221 G_CALLBACK(pounce_selected_cb), dialog);
1222
1223 /* Handle double-clicking */
1224 g_signal_connect(G_OBJECT(treeview), "button_press_event",
1225 G_CALLBACK(pounce_double_click_cb), dialog);
1226
1227
1228 gtk_container_add(GTK_CONTAINER(sw), treeview);
1229 gtk_widget_show(treeview);
1230
1231 /* Pouncee Column */
1232 column = gtk_tree_view_column_new();
1233 gtk_tree_view_column_set_title(column, _("Pounce Target"));
1234 gtk_tree_view_column_set_resizable(column, TRUE);
1235 gtk_tree_view_column_set_min_width(column, 200);
1236 gtk_tree_view_column_set_sort_column_id(column,
1237 POUNCES_MANAGER_COLUMN_TARGET);
1238 gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
1239
1240 /* Icon */
1241 renderer = gtk_cell_renderer_pixbuf_new();
1242 gtk_tree_view_column_pack_start(column, renderer, FALSE);
1243 gtk_tree_view_column_add_attribute(column, renderer, "pixbuf",
1244 POUNCES_MANAGER_COLUMN_ICON);
1245
1246 /* Pouncee */
1247 renderer = gtk_cell_renderer_text_new();
1248 gtk_tree_view_column_pack_start(column, renderer, TRUE);
1249 gtk_tree_view_column_add_attribute(column, renderer, "text",
1250 POUNCES_MANAGER_COLUMN_TARGET);
1251
1252
1253 /* Account Column */
1254 column = gtk_tree_view_column_new();
1255 gtk_tree_view_column_set_title(column, _("Account"));
1256 gtk_tree_view_column_set_resizable(column, TRUE);
1257 gtk_tree_view_column_set_min_width(column, 200);
1258 gtk_tree_view_column_set_sort_column_id(column,
1259 POUNCES_MANAGER_COLUMN_ACCOUNT);
1260 gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
1261 renderer = gtk_cell_renderer_text_new();
1262 gtk_tree_view_column_pack_start(column, renderer, TRUE);
1263 gtk_tree_view_column_add_attribute(column, renderer, "text",
1264 POUNCES_MANAGER_COLUMN_ACCOUNT);
1265
1266 /* Recurring Column */
1267 renderer = gtk_cell_renderer_toggle_new();
1268 column = gtk_tree_view_column_new_with_attributes(_("Recurring"), renderer,
1269 "active", POUNCES_MANAGER_COLUMN_RECURRING, NULL);
1270 gtk_tree_view_column_set_sort_column_id(column,
1271 POUNCES_MANAGER_COLUMN_RECURRING);
1272 gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
1273 g_signal_connect(G_OBJECT(renderer), "toggled",
1274 G_CALLBACK(pounces_manager_recurring_cb), dialog);
1275
1276 /* Enable CTRL+F searching */
1277 gtk_tree_view_set_search_column(GTK_TREE_VIEW(treeview), POUNCES_MANAGER_COLUMN_TARGET);
1278 gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(treeview), search_func, NULL, NULL);
1279
1280 /* Sort the pouncee column by default */
1281 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(dialog->model),
1282 POUNCES_MANAGER_COLUMN_TARGET,
1283 GTK_SORT_ASCENDING);
1284
1285 /* Populate list */
1286 populate_pounces_list(dialog);
1287
1288 return sw;
1289 }
1290
1291 void
1292 gaim_gtk_pounces_manager_show(void)
1293 {
1294 PouncesManager *dialog;
1295 GtkWidget *bbox;
1296 GtkWidget *button;
1297 GtkWidget *list;
1298 GtkWidget *vbox;
1299 GtkWidget *win;
1300 int width, height;
1301
1302 if (pounces_manager != NULL) {
1303 gtk_window_present(GTK_WINDOW(pounces_manager->window));
1304 return;
1305 }
1306
1307 pounces_manager = dialog = g_new0(PouncesManager, 1);
1308
1309 width = gaim_prefs_get_int("/gaim/gtk/pounces/dialog/width");
1310 height = gaim_prefs_get_int("/gaim/gtk/pounces/dialog/height");
1311
1312 dialog->window = win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1313 gtk_window_set_default_size(GTK_WINDOW(win), width, height);
1314 gtk_window_set_role(GTK_WINDOW(win), "pounces");
1315 gtk_window_set_title(GTK_WINDOW(win), _("Buddy Pounces"));
1316 gtk_container_set_border_width(GTK_CONTAINER(win), GAIM_HIG_BORDER);
1317
1318 g_signal_connect(G_OBJECT(win), "delete_event",
1319 G_CALLBACK(pounces_manager_destroy_cb), dialog);
1320 g_signal_connect(G_OBJECT(win), "configure_event",
1321 G_CALLBACK(pounces_manager_configure_cb), dialog);
1322
1323 /* Setup the vbox */
1324 vbox = gtk_vbox_new(FALSE, GAIM_HIG_BORDER);
1325 gtk_container_add(GTK_CONTAINER(win), vbox);
1326 gtk_widget_show(vbox);
1327
1328 /* List of saved buddy pounces */
1329 list = create_pounces_list(dialog);
1330 gtk_box_pack_start(GTK_BOX(vbox), list, TRUE, TRUE, 0);
1331
1332 /* Button box. */
1333 bbox = gtk_hbutton_box_new();
1334 gtk_box_set_spacing(GTK_BOX(bbox), GAIM_HIG_BOX_SPACE);
1335 gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END);
1336 gtk_box_pack_end(GTK_BOX(vbox), bbox, FALSE, TRUE, 0);
1337 gtk_widget_show(bbox);
1338
1339 /* Add button */
1340 button = gtk_button_new_from_stock(GTK_STOCK_ADD);
1341 gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0);
1342 gtk_widget_set_sensitive(button, (gaim_connections_get_all() != NULL));
1343 gaim_signal_connect(gaim_connections_get_handle(), "signed-on",
1344 pounces_manager, GAIM_CALLBACK(pounces_manager_connection_cb), button);
1345 gaim_signal_connect(gaim_connections_get_handle(), "signed-off",
1346 pounces_manager, GAIM_CALLBACK(pounces_manager_connection_cb), button);
1347 gtk_widget_show(button);
1348
1349 g_signal_connect(G_OBJECT(button), "clicked",
1350 G_CALLBACK(pounces_manager_add_cb), dialog);
1351
1352 /* Modify button */
1353 button = gtk_button_new_from_stock(GAIM_STOCK_MODIFY);
1354 dialog->modify_button = button;
1355 gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0);
1356 gtk_widget_set_sensitive(button, FALSE);
1357 gtk_widget_show(button);
1358
1359 g_signal_connect(G_OBJECT(button), "clicked",
1360 G_CALLBACK(pounces_manager_modify_cb), dialog);
1361
1362 /* Delete button */
1363 button = gtk_button_new_from_stock(GTK_STOCK_DELETE);
1364 dialog->delete_button = button;
1365 gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0);
1366 gtk_widget_set_sensitive(button, FALSE);
1367 gtk_widget_show(button);
1368
1369 g_signal_connect(G_OBJECT(button), "clicked",
1370 G_CALLBACK(pounces_manager_delete_cb), dialog);
1371
1372 /* Close button */
1373 button = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
1374 gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0);
1375 gtk_widget_show(button);
1376
1377 g_signal_connect(G_OBJECT(button), "clicked",
1378 G_CALLBACK(pounces_manager_close_cb), dialog);
1379
1380 gtk_widget_show(win);
1381 }
1382
1383 void
1384 gaim_gtk_pounces_manager_hide(void)
1385 {
1386 if (pounces_manager == NULL)
1387 return;
1388
1389 if (pounces_manager->window != NULL)
1390 gtk_widget_destroy(pounces_manager->window);
1391
1392 gaim_signals_disconnect_by_handle(pounces_manager);
1393
1394 g_free(pounces_manager);
1395 pounces_manager = NULL;
1396 }
1397
1398 static void
1399 pounce_cb(GaimPounce *pounce, GaimPounceEvent events, void *data)
1400 {
1401 GaimConversation *conv;
1402 GaimAccount *account;
1403 GaimBuddy *buddy;
1404 const char *pouncee;
1405 const char *alias;
1406
1407 pouncee = gaim_pounce_get_pouncee(pounce);
1408 account = gaim_pounce_get_pouncer(pounce);
1409
1410 buddy = gaim_find_buddy(account, pouncee);
1411 if (buddy != NULL)
1412 {
1413 alias = gaim_buddy_get_alias(buddy);
1414 if (alias == NULL)
1415 alias = pouncee;
1416 }
1417 else
1418 alias = pouncee;
1419
1420 if (gaim_pounce_action_is_enabled(pounce, "open-window"))
1421 {
1422 conv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM, pouncee, account);
1423
1424 if (conv == NULL)
1425 conv = gaim_conversation_new(GAIM_CONV_TYPE_IM, account, pouncee);
1426 }
1427
1428 if (gaim_pounce_action_is_enabled(pounce, "popup-notify"))
1429 {
1430 char *tmp;
1431 const char *name_shown;
1432 const char *reason;
1433 reason = gaim_pounce_action_get_attribute(pounce, "popup-notify",
1434 "reason");
1435
1436 /*
1437 * Here we place the protocol name in the pounce dialog to lessen
1438 * confusion about what protocol a pounce is for.
1439 */
1440 tmp = g_strdup_printf(
1441 (events & GAIM_POUNCE_TYPING) ?
1442 _("%s has started typing to you (%s)") :
1443 (events & GAIM_POUNCE_TYPED) ?
1444 _("%s has paused while typing to you (%s)") :
1445 (events & GAIM_POUNCE_SIGNON) ?
1446 _("%s has signed on (%s)") :
1447 (events & GAIM_POUNCE_IDLE_RETURN) ?
1448 _("%s has returned from being idle (%s)") :
1449 (events & GAIM_POUNCE_AWAY_RETURN) ?
1450 _("%s has returned from being away (%s)") :
1451 (events & GAIM_POUNCE_TYPING_STOPPED) ?
1452 _("%s has stopped typing to you (%s)") :
1453 (events & GAIM_POUNCE_SIGNOFF) ?
1454 _("%s has signed off (%s)") :
1455 (events & GAIM_POUNCE_IDLE) ?
1456 _("%s has become idle (%s)") :
1457 (events & GAIM_POUNCE_AWAY) ?
1458 _("%s has gone away. (%s)") :
1459 (events & GAIM_POUNCE_MESSAGE_RECEIVED) ?
1460 _("%s has sent you a message. (%s)") :
1461 _("Unknown pounce event. Please report this!"),
1462 alias, gaim_account_get_protocol_name(account));
1463
1464 /*
1465 * Ok here is where I change the second argument, title, from
1466 * NULL to the account alias if we have it or the account
1467 * name if that's all we have
1468 */
1469 if ((name_shown = gaim_account_get_alias(account)) == NULL)
1470 name_shown = gaim_account_get_username(account);
1471
1472 if (reason == NULL)
1473 {
1474 gaim_notify_info(NULL, name_shown, tmp, gaim_date_format_full(NULL));
1475 }
1476 else
1477 {
1478 char *tmp2 = g_strdup_printf("%s\n\n%s", reason, gaim_date_format_full(NULL));
1479 gaim_notify_info(NULL, name_shown, tmp, tmp2);
1480 g_free(tmp2);
1481 }
1482 g_free(tmp);
1483 }
1484
1485 if (gaim_pounce_action_is_enabled(pounce, "send-message"))
1486 {
1487 const char *message;
1488
1489 message = gaim_pounce_action_get_attribute(pounce, "send-message",
1490 "message");
1491
1492 if (message != NULL)
1493 {
1494 conv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM, pouncee, account);
1495
1496 if (conv == NULL)
1497 conv = gaim_conversation_new(GAIM_CONV_TYPE_IM, account, pouncee);
1498
1499 gaim_conversation_write(conv, NULL, message,
1500 GAIM_MESSAGE_SEND, time(NULL));
1501
1502 serv_send_im(account->gc, (char *)pouncee, (char *)message, 0);
1503 }
1504 }
1505
1506 if (gaim_pounce_action_is_enabled(pounce, "execute-command"))
1507 {
1508 const char *command;
1509
1510 command = gaim_pounce_action_get_attribute(pounce,
1511 "execute-command", "command");
1512
1513 if (command != NULL)
1514 {
1515 #ifndef _WIN32
1516 char *localecmd = g_locale_from_utf8(command, -1, NULL,
1517 NULL, NULL);
1518
1519 if (localecmd != NULL)
1520 {
1521 int pid = fork();
1522
1523 if (pid == 0) {
1524 char *args[4];
1525
1526 args[0] = "sh";
1527 args[1] = "-c";
1528 args[2] = (char *)localecmd;
1529 args[3] = NULL;
1530
1531 execvp(args[0], args);
1532
1533 _exit(0);
1534 }
1535 g_free(localecmd);
1536 }
1537 #else /* !_WIN32 */
1538 PROCESS_INFORMATION pi;
1539 BOOL retval;
1540 gchar *message = NULL;
1541
1542 memset(&pi, 0, sizeof(pi));
1543
1544 if (G_WIN32_HAVE_WIDECHAR_API ()) {
1545 STARTUPINFOW si;
1546 wchar_t *wc_cmd = g_utf8_to_utf16(command,
1547 -1, NULL, NULL, NULL);
1548
1549 memset(&si, 0 , sizeof(si));
1550 si.cb = sizeof(si);
1551
1552 retval = CreateProcessW(NULL, wc_cmd, NULL,
1553 NULL, 0, 0, NULL, NULL,
1554 &si, &pi);
1555 g_free(wc_cmd);
1556 } else {
1557 STARTUPINFOA si;
1558 char *l_cmd = g_locale_from_utf8(command,
1559 -1, NULL, NULL, NULL);
1560
1561 memset(&si, 0 , sizeof(si));
1562 si.cb = sizeof(si);
1563
1564 retval = CreateProcessA(NULL, l_cmd, NULL,
1565 NULL, 0, 0, NULL, NULL,
1566 &si, &pi);
1567 g_free(l_cmd);
1568 }
1569
1570 if (retval) {
1571 CloseHandle(pi.hProcess);
1572 CloseHandle(pi.hThread);
1573 } else {
1574 message = g_win32_error_message(GetLastError());
1575 }
1576
1577 gaim_debug_info("pounce",
1578 "Pounce execute command called for: "
1579 "%s\n%s%s%s",
1580 command,
1581 retval ? "" : "Error: ",
1582 retval ? "" : message,
1583 retval ? "" : "\n");
1584 g_free(message);
1585 #endif /* !_WIN32 */
1586 }
1587 }
1588
1589 if (gaim_pounce_action_is_enabled(pounce, "play-sound"))
1590 {
1591 const char *sound;
1592
1593 sound = gaim_pounce_action_get_attribute(pounce,
1594 "play-sound", "filename");
1595
1596 if (sound != NULL)
1597 gaim_sound_play_file(sound, account);
1598 else
1599 gaim_sound_play_event(GAIM_SOUND_POUNCE_DEFAULT, account);
1600 }
1601 }
1602
1603 static void
1604 free_pounce(GaimPounce *pounce)
1605 {
1606 update_pounces();
1607 }
1608
1609 static void
1610 new_pounce(GaimPounce *pounce)
1611 {
1612 gaim_pounce_action_register(pounce, "open-window");
1613 gaim_pounce_action_register(pounce, "popup-notify");
1614 gaim_pounce_action_register(pounce, "send-message");
1615 gaim_pounce_action_register(pounce, "execute-command");
1616 gaim_pounce_action_register(pounce, "play-sound");
1617
1618 update_pounces();
1619 }
1620
1621 void *
1622 gaim_gtk_pounces_get_handle() {
1623 static int handle;
1624
1625 return &handle;
1626 }
1627
1628 void
1629 gaim_gtk_pounces_init(void)
1630 {
1631 gaim_pounces_register_handler(GAIM_GTK_UI, pounce_cb, new_pounce,
1632 free_pounce);
1633
1634 gaim_prefs_add_none("/gaim/gtk/pounces");
1635 gaim_prefs_add_none("/gaim/gtk/pounces/default_actions");
1636 gaim_prefs_add_bool("/gaim/gtk/pounces/default_actions/open-window",
1637 FALSE);
1638 gaim_prefs_add_bool("/gaim/gtk/pounces/default_actions/popup-notify",
1639 TRUE);
1640 gaim_prefs_add_bool("/gaim/gtk/pounces/default_actions/send-message",
1641 FALSE);
1642 gaim_prefs_add_bool("/gaim/gtk/pounces/default_actions/execute-command",
1643 FALSE);
1644 gaim_prefs_add_bool("/gaim/gtk/pounces/default_actions/play-sound",
1645 FALSE);
1646 gaim_prefs_add_none("/gaim/gtk/pounces/dialog");
1647 gaim_prefs_add_int("/gaim/gtk/pounces/dialog/width", 520);
1648 gaim_prefs_add_int("/gaim/gtk/pounces/dialog/height", 321);
1649
1650 gaim_signal_connect(gaim_connections_get_handle(), "signed-on",
1651 gaim_gtk_pounces_get_handle(),
1652 GAIM_CALLBACK(signed_on_off_cb), NULL);
1653 gaim_signal_connect(gaim_connections_get_handle(), "signed-off",
1654 gaim_gtk_pounces_get_handle(),
1655 GAIM_CALLBACK(signed_on_off_cb), NULL);
1656 }