58451961954c6a1d336f3d2eeda7f41f554d6acf
[obnox/wireshark/wip.git] / gtk / proto_dlg.c
1 /* proto_dlg.c
2  *
3  * $Id: proto_dlg.c,v 1.33 2004/05/26 03:49:24 ulfl Exp $
4  *
5  * Laurent Deniel <laurent.deniel@free.fr>
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 2000 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (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 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <gtk/gtk.h>
31 #include <gdk/gdkkeysyms.h>
32 #include <string.h>
33
34 #include "prefs.h"
35 #include "globals.h"
36 #include "main.h"
37 #include "util.h"
38 #include "ui_util.h"
39 #include "dlg_utils.h"
40 #include "proto_dlg.h"
41 #include "simple_dialog.h"
42 #include "compat_macros.h"
43 #include "disabled_protos.h"
44 #include <epan/filesystem.h>
45
46 static gboolean proto_delete_cb(GtkWidget *, gpointer);
47 static void proto_ok_cb(GtkWidget *, gpointer);
48 static void proto_apply_cb(GtkWidget *, gpointer);
49 static void proto_save_cb(GtkWidget *, gpointer);
50 static void proto_cancel_cb(GtkWidget *, gpointer);
51 static void proto_destroy_cb(GtkWidget *, gpointer);
52
53 #if GTK_MAJOR_VERSION < 2
54 static void show_proto_selection(GtkCList *proto_list);
55 #else
56 static void show_proto_selection(GtkListStore *proto_store);
57 #endif
58 static gboolean set_proto_selection(GtkWidget *);
59 static gboolean revert_proto_selection(void);
60
61 static void toggle_all_cb(GtkWidget *button, gpointer parent_w);
62 static void enable_all_cb(GtkWidget *button, gpointer parent_w);
63 static void disable_all_cb(GtkWidget *button, gpointer parent_w);
64 #if GTK_MAJOR_VERSION < 2
65 static void proto_list_select_cb(GtkCList *proto_list, gint row, gint col, 
66                                  GdkEventButton *ev, gpointer gp);
67 static gboolean proto_list_keypress_cb(GtkWidget *pl, GdkEventKey *ev,
68                                    gpointer gp);
69 #else
70 static void status_toggled(GtkCellRendererToggle *, gchar *, gpointer);
71 #endif
72
73 static GtkWidget *proto_w = NULL;
74
75 /* list of protocols */
76 static GSList *protocol_list = NULL;
77
78 typedef struct protocol_data {
79   char     *name;
80   char     *abbrev;
81   int      hfinfo_index;
82   gboolean enabled;
83   gboolean was_enabled;
84 #if GTK_MAJOR_VERSION < 2
85   gint     row;
86 #else
87   GtkTreeIter iter;
88 #endif
89 } protocol_data_t;
90
91 #define DISABLED "Disabled"
92 #define STATUS_TXT(x) ((x) ? "" : DISABLED)
93
94 void
95 proto_cb(GtkWidget *w _U_, gpointer data _U_)
96 {
97
98   GtkWidget *main_vb, *bbox, *proto_list, *label, *proto_sw, *proto_frame,
99             *proto_vb, *button;
100   gchar *titles[] = { "Status", "Protocol", "Description" };
101 #if GTK_MAJOR_VERSION < 2
102   gint width;
103 #else
104   GtkListStore *proto_store;
105   GtkCellRenderer *proto_rend;
106   GtkTreeViewColumn *proto_col;
107 #endif
108
109
110   if (proto_w != NULL) {
111     reactivate_window(proto_w);
112     return;
113   }
114
115   proto_w = dlg_window_new("Ethereal: Enabled Protocols");
116   gtk_window_set_default_size(GTK_WINDOW(proto_w), DEF_WIDTH * 2/3, DEF_HEIGHT * 2/3);
117
118   /* Container for each row of widgets */
119
120   main_vb = gtk_vbox_new(FALSE, 0);
121   gtk_container_border_width(GTK_CONTAINER(main_vb), 1);
122   gtk_container_add(GTK_CONTAINER(proto_w), main_vb);
123   gtk_widget_show(main_vb);
124
125   /* Protocol selection list ("enable/disable" protocols) */
126
127   proto_frame = gtk_frame_new("Enabled Protocols");
128   gtk_box_pack_start(GTK_BOX(main_vb), proto_frame, TRUE, TRUE, 0);
129   gtk_container_border_width(GTK_CONTAINER(proto_frame), 5);
130   gtk_widget_show(proto_frame);
131
132   /* Protocol list */
133   
134   proto_vb = gtk_vbox_new(FALSE, 0);
135   gtk_container_border_width(GTK_CONTAINER(proto_vb), 1);
136   gtk_container_add(GTK_CONTAINER(proto_frame), proto_vb);
137   gtk_container_border_width(GTK_CONTAINER(proto_vb), 5);
138   gtk_widget_show(proto_vb);
139   
140   proto_sw = scrolled_window_new(NULL, NULL);
141 #if GTK_MAJOR_VERSION >= 2
142   gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(proto_sw), 
143                                    GTK_SHADOW_IN);
144 #endif
145   gtk_box_pack_start(GTK_BOX(proto_vb), proto_sw, TRUE, TRUE, 0);
146   gtk_widget_show(proto_sw);
147
148 #if GTK_MAJOR_VERSION < 2
149   proto_list = gtk_clist_new_with_titles(3, titles);
150   gtk_container_add(GTK_CONTAINER(proto_sw), proto_list);
151   gtk_clist_set_selection_mode(GTK_CLIST(proto_list), GTK_SELECTION_BROWSE);
152   gtk_clist_column_titles_passive(GTK_CLIST(proto_list));
153   gtk_clist_column_titles_show(GTK_CLIST(proto_list));
154   gtk_clist_set_column_auto_resize(GTK_CLIST(proto_list), 0, FALSE);
155   gtk_clist_set_column_auto_resize(GTK_CLIST(proto_list), 1, TRUE);
156   gtk_clist_set_column_auto_resize(GTK_CLIST(proto_list), 2, TRUE);
157   width = gdk_string_width(proto_list->style->font, DISABLED);
158   gtk_clist_set_column_width(GTK_CLIST(proto_list), 0, width);
159   SIGNAL_CONNECT(proto_list, "select-row", proto_list_select_cb, NULL);
160   SIGNAL_CONNECT(proto_list, "key-press-event", proto_list_keypress_cb, NULL);
161   show_proto_selection(GTK_CLIST(proto_list));
162 #else
163   proto_store = gtk_list_store_new(4, G_TYPE_BOOLEAN, G_TYPE_STRING,
164                                    G_TYPE_STRING, G_TYPE_POINTER);
165   show_proto_selection(proto_store);
166   /* default sort on "abbrev" column */
167   gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(proto_store), 1,
168                                        GTK_SORT_ASCENDING);
169   proto_list = tree_view_new(GTK_TREE_MODEL(proto_store));
170   gtk_container_add(GTK_CONTAINER(proto_sw), proto_list);
171   proto_rend = gtk_cell_renderer_toggle_new();
172   SIGNAL_CONNECT(proto_rend, "toggled", status_toggled, proto_store);
173   proto_col = gtk_tree_view_column_new_with_attributes(titles[0], proto_rend,
174                                                     "active", 0, NULL);
175   gtk_tree_view_column_set_sort_column_id(proto_col, 0);
176   gtk_tree_view_append_column(GTK_TREE_VIEW(proto_list), proto_col);
177   proto_rend = gtk_cell_renderer_text_new();
178   proto_col = gtk_tree_view_column_new_with_attributes(titles[1], proto_rend,
179                                                     "text", 1, NULL);
180   gtk_tree_view_column_set_sort_column_id(proto_col, 1);
181   gtk_tree_view_append_column(GTK_TREE_VIEW(proto_list), proto_col);
182   proto_rend = gtk_cell_renderer_text_new();
183   proto_col = gtk_tree_view_column_new_with_attributes(titles[2], proto_rend,
184                                                     "text", 2, NULL);
185   gtk_tree_view_column_set_sort_column_id(proto_col, 2);
186   gtk_tree_view_append_column(GTK_TREE_VIEW(proto_list), proto_col);
187   g_object_unref(G_OBJECT(proto_store));
188 #endif
189   gtk_widget_show(proto_list);
190
191   label = gtk_label_new("Disabling a protocol prevents higher "
192                         "layer protocols from being displayed");
193   gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
194   gtk_widget_show(label);
195   gtk_box_pack_start(GTK_BOX(proto_vb), label, FALSE, FALSE, 5);
196
197
198   bbox = gtk_hbutton_box_new();
199   gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END);
200   gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
201   gtk_box_pack_start(GTK_BOX(proto_vb), bbox, FALSE, FALSE, 0);
202   gtk_widget_show(bbox);
203
204   /* Enable All */
205   button = gtk_button_new_with_label("Enable All");
206   SIGNAL_CONNECT(button, "clicked", enable_all_cb, proto_list);
207   gtk_box_pack_start(GTK_BOX(bbox), button, TRUE, TRUE, 0);
208   gtk_widget_show(button);
209
210   /* Disable All */
211   button = gtk_button_new_with_label("Disable All");
212   SIGNAL_CONNECT(button, "clicked", disable_all_cb, proto_list);
213   gtk_box_pack_start(GTK_BOX(bbox), button, TRUE, TRUE, 0);
214   gtk_widget_show(button);
215
216   /* Invert */
217   button = gtk_button_new_with_label("Invert");
218   SIGNAL_CONNECT(button, "clicked", toggle_all_cb, proto_list);
219   gtk_box_pack_start(GTK_BOX(bbox), button, TRUE, TRUE, 0);
220   gtk_widget_show(button);
221
222
223   /* Button row */
224   bbox = dlg_button_row_new(GTK_STOCK_OK, GTK_STOCK_APPLY, GTK_STOCK_SAVE, GTK_STOCK_CANCEL, NULL);
225   gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
226   gtk_widget_show(bbox);
227
228   button = OBJECT_GET_DATA(bbox, GTK_STOCK_OK);
229   SIGNAL_CONNECT(button, "clicked", proto_ok_cb, proto_w);
230   gtk_widget_grab_default(button);
231
232   button = OBJECT_GET_DATA(bbox, GTK_STOCK_APPLY);
233   SIGNAL_CONNECT(button, "clicked", proto_apply_cb, proto_w);
234
235   button = OBJECT_GET_DATA(bbox, GTK_STOCK_SAVE);
236   SIGNAL_CONNECT(button, "clicked", proto_save_cb, proto_w);
237
238   button = OBJECT_GET_DATA(bbox, GTK_STOCK_CANCEL);
239   window_set_cancel_button(proto_w, button, proto_cancel_cb);
240
241   SIGNAL_CONNECT(proto_w, "delete_event", proto_delete_cb, NULL);
242   SIGNAL_CONNECT(proto_w, "destroy", proto_destroy_cb, NULL);
243
244   gtk_quit_add_destroy(gtk_main_level(), GTK_OBJECT(proto_w));
245
246   gtk_widget_show(proto_w);
247   window_present(proto_w);
248 } /* proto_cb */
249
250 #if GTK_MAJOR_VERSION < 2
251 static void
252 proto_list_select_cb(GtkCList *proto_list, gint row, gint col, 
253                      GdkEventButton *ev _U_, gpointer gp _U_) {
254   protocol_data_t *p = gtk_clist_get_row_data(proto_list, row);
255   
256   if (row < 0 || col < 0)
257     return;
258
259   if (p->enabled)
260     p->enabled = FALSE;
261   else
262     p->enabled = TRUE;
263
264   gtk_clist_set_text(proto_list, row, 0, STATUS_TXT(p->enabled) );
265 } /* proto_list_select_cb */
266
267 static gboolean
268 proto_list_keypress_cb(GtkWidget *pl, GdkEventKey *ev, gpointer gp _U_) {
269   GtkCList *proto_list = GTK_CLIST(pl);
270   
271   if (ev->keyval == GDK_space) {
272     proto_list_select_cb(proto_list, proto_list->focus_row, 0, NULL, gp);
273   }
274   return TRUE;
275 }
276
277 #else
278 static void
279 status_toggled(GtkCellRendererToggle *cell _U_, gchar *path_str, gpointer data)
280 {
281   GtkTreeModel    *model = (GtkTreeModel *)data;
282   GtkTreeIter      iter;
283   GtkTreePath     *path = gtk_tree_path_new_from_string(path_str);
284   protocol_data_t *p;
285
286   gtk_tree_model_get_iter(model, &iter, path);
287   gtk_tree_model_get(model, &iter, 3, &p, -1);
288
289   if (p->enabled)
290     p->enabled = FALSE;
291   else
292     p->enabled = TRUE;
293
294   gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0, p->enabled, -1);
295
296   gtk_tree_path_free(path);
297 } /* status toggled */
298 #endif
299
300 /* XXX - We need callbacks for Gtk2 */
301
302
303 /* Toggle All */
304 static void
305 toggle_all_cb(GtkWidget *button _U_, gpointer pl)
306 {
307   GSList *entry;
308 #if GTK_MAJOR_VERSION < 2
309   GtkCList *proto_list = GTK_CLIST(pl);
310 #else
311   GtkListStore *s = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(pl)));
312 #endif
313
314   for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
315     protocol_data_t *p = entry->data;
316
317     if (p->enabled)
318       p->enabled = FALSE;
319     else
320       p->enabled = TRUE;
321     
322 #if GTK_MAJOR_VERSION < 2
323     gtk_clist_set_text(proto_list, p->row, 0, STATUS_TXT(p->enabled) );
324 #else
325     gtk_list_store_set(s, &p->iter, 0, p->enabled, -1);
326 #endif
327   }
328 }
329
330 /* Enable/Disable All Helper */
331 static void
332 set_active_all(GtkWidget *w, gboolean new_state)
333 {
334
335 #if GTK_MAJOR_VERSION < 2
336   GtkCList *proto_list = GTK_CLIST(w);
337 #else
338   GtkListStore *s = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(w)));
339 #endif
340   GSList *entry;
341
342 #if GTK_MAJOR_VERSION < 2
343   gtk_clist_freeze(proto_list);
344 #endif
345   for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
346     protocol_data_t *p = entry->data;
347     
348     p->enabled = new_state;
349 #if GTK_MAJOR_VERSION < 2
350     gtk_clist_set_text(proto_list, p->row, 0, STATUS_TXT(new_state) );
351 #else
352     gtk_list_store_set(s, &p->iter, 0, new_state, -1);
353 #endif
354   }
355 #if GTK_MAJOR_VERSION < 2
356   gtk_clist_thaw(proto_list);
357 #endif
358 }
359
360 /* Enable All */
361 static void
362 enable_all_cb(GtkWidget *button _U_, gpointer pl)
363 {
364         set_active_all(pl, TRUE);
365 }
366
367 /* Disable All */
368 static void
369 disable_all_cb(GtkWidget *button _U_, gpointer pl)
370 {
371         set_active_all(pl, FALSE);
372 }
373
374 static void
375 proto_destroy_cb(GtkWidget *w _U_, gpointer data _U_)
376 {
377   GSList *entry;
378
379   if (proto_w)
380     window_destroy(proto_w);
381   proto_w = NULL;
382
383   /* remove protocol list */
384   if (protocol_list) {
385     for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
386       g_free(entry->data);
387     }
388     g_slist_free(protocol_list);
389     protocol_list = NULL;
390   }
391 }
392
393 /* Treat this as a cancel, by calling "proto_cancel_cb()".
394    XXX - that'll destroy the Protocols dialog; will that upset
395    a higher-level handler that says "OK, we've been asked to delete
396    this, so destroy it"? */
397 static gboolean
398 proto_delete_cb(GtkWidget *proto_w, gpointer dummy _U_)
399 {
400   proto_cancel_cb(NULL, proto_w);
401   return FALSE;
402 }
403
404 static void
405 proto_ok_cb(GtkWidget *ok_bt _U_, gpointer parent_w)
406 {
407   gboolean redissect;
408
409   redissect = set_proto_selection(GTK_WIDGET(parent_w));
410   window_destroy(GTK_WIDGET(parent_w));
411   if (redissect)
412     redissect_packets(&cfile);
413 }
414
415 static void
416 proto_apply_cb(GtkWidget *apply_bt _U_, gpointer parent_w)
417 {
418   if (set_proto_selection(GTK_WIDGET(parent_w)))
419     redissect_packets(&cfile);
420 }
421
422 static void
423 proto_save_cb(GtkWidget *save_bt _U_, gpointer parent_w)
424 {
425   gboolean must_redissect = FALSE;
426   char *pf_dir_path;
427   char *pf_path;
428   int pf_save_errno;
429
430   /* Create the directory that holds personal configuration files, if
431      necessary.  */
432   if (create_persconffile_dir(&pf_dir_path) == -1) {
433      simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
434       "Can't create directory\n\"%s\"\nfor disabled protocols file: %s.", pf_dir_path,
435       strerror(errno));
436      g_free(pf_dir_path);
437   } else {
438     /*
439      * make disabled/enabled protocol settings current
440      */
441     must_redissect = set_proto_selection(GTK_WIDGET(parent_w));
442
443     save_disabled_protos_list(&pf_path, &pf_save_errno);
444     if (pf_path != NULL) {
445         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
446             "Could not save to your disabled protocols file\n\"%s\": %s.",
447             pf_path, strerror(pf_save_errno));
448         g_free(pf_path);
449     }
450   }
451
452   if (must_redissect) {
453     /* Redissect all the packets, and re-evaluate the display filter. */
454     redissect_packets(&cfile);
455   }
456 }
457
458 static void
459 proto_cancel_cb(GtkWidget *cancel_bt _U_, gpointer parent_w)
460 {
461   gboolean redissect;
462
463   redissect = revert_proto_selection();
464   window_destroy(GTK_WIDGET(parent_w));
465   if (redissect)
466     redissect_packets(&cfile);
467 }
468
469 static gboolean
470 set_proto_selection(GtkWidget *parent_w _U_)
471 {
472   GSList *entry;
473   gboolean need_redissect = FALSE;
474
475   for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
476     protocol_data_t *p = entry->data;
477     protocol_t *protocol;
478
479     protocol = find_protocol_by_id(p->hfinfo_index);
480     if (proto_is_protocol_enabled(protocol) != p->enabled) {
481       proto_set_decoding(p->hfinfo_index, p->enabled);
482       need_redissect = TRUE;
483     }
484   }
485
486   return need_redissect;
487
488 } /* set_proto_selection */
489
490 static gboolean
491 revert_proto_selection(void)
492 {
493   GSList *entry;
494   gboolean need_redissect = FALSE;
495
496   /*
497    * Undo all the changes we've made to protocol enable flags.
498    */
499   for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
500     protocol_data_t *p = entry->data;
501     protocol_t *protocol;
502
503     protocol = find_protocol_by_id(p->hfinfo_index);
504     if (proto_is_protocol_enabled(protocol) != p->was_enabled) {
505       proto_set_decoding(p->hfinfo_index, p->was_enabled);
506       need_redissect = TRUE;
507     }
508   }
509
510   return need_redissect;
511
512 } /* revert_proto_selection */
513
514 gint
515 protocol_data_compare(gconstpointer a, gconstpointer b)
516 {
517   const protocol_data_t *ap = (const protocol_data_t *)a;
518   const protocol_data_t *bp = (const protocol_data_t *)b;
519
520   return strcmp(ap->abbrev, bp->abbrev);
521 }
522
523 static void
524 #if GTK_MAJOR_VERSION < 2
525 show_proto_selection(GtkCList *proto_list)
526 #else
527 show_proto_selection(GtkListStore *proto_store)
528 #endif
529 {
530   GSList *entry;
531   gint i;
532   void *cookie;
533   protocol_t *protocol;
534   protocol_data_t *p;
535 #if GTK_MAJOR_VERSION < 2
536   gchar *proto_text[3];
537 #endif
538
539   /* Iterate over all the protocols */
540
541   for (i = proto_get_first_protocol(&cookie); i != -1;
542        i = proto_get_next_protocol(&cookie)) {
543       if (proto_can_toggle_protocol(i)) {
544         p = g_malloc(sizeof(protocol_data_t));
545         protocol = find_protocol_by_id(i);
546         p->name = proto_get_protocol_name(i);
547         p->abbrev = proto_get_protocol_short_name(protocol);
548         p->hfinfo_index = i;
549         p->enabled = proto_is_protocol_enabled(protocol);
550         p->was_enabled = p->enabled;
551         protocol_list = g_slist_insert_sorted(protocol_list,
552                                             p, protocol_data_compare);
553       }
554   }
555
556   for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
557     p = entry->data;
558
559 #if GTK_MAJOR_VERSION < 2
560     /* XXX - The preferred way to do this would be to have a check box
561      * in the first column.  GtkClists don't let us put arbitrary widgets
562      * in a cell, so we use the word "Disabled" instead.  We should be
563      * able to use check boxes in Gtk2, however.
564      */        
565     proto_text[0] = STATUS_TXT (p->enabled);
566     proto_text[1] = p->abbrev;
567     proto_text[2] = p->name;
568     p->row = gtk_clist_append(proto_list, proto_text);
569     gtk_clist_set_row_data(proto_list, p->row, p);
570 #else
571     gtk_list_store_append(proto_store, &p->iter);
572     gtk_list_store_set(proto_store, &p->iter,
573                        0, p->enabled,
574                        1, p->abbrev,
575                        2, p->name,
576                        3, p,
577                       -1);
578 #endif
579   }
580
581 } /* show_proto_selection */