Canonicalize the line endings, and set svn:eol-style to native to keep
[obnox/wireshark/wip.git] / gtk / proto_dlg.c
1 /* proto_dlg.c
2  *
3  * $Id$
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_event_cb(GtkWidget *, GdkEvent *, 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);
117
118   /* Container for each row of widgets */
119
120   main_vb = gtk_vbox_new(FALSE, 6);
121   gtk_container_border_width(GTK_CONTAINER(main_vb), 6);
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_widget_show(proto_frame);
130
131   /* Protocol list */
132   
133   proto_vb = gtk_vbox_new(FALSE, 0);
134   gtk_container_add(GTK_CONTAINER(proto_frame), proto_vb);
135   gtk_container_border_width(GTK_CONTAINER(proto_vb), 5);
136   gtk_widget_show(proto_vb);
137   
138   proto_sw = scrolled_window_new(NULL, NULL);
139 #if GTK_MAJOR_VERSION >= 2
140   gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(proto_sw), 
141                                    GTK_SHADOW_IN);
142 #endif
143   gtk_box_pack_start(GTK_BOX(proto_vb), proto_sw, TRUE, TRUE, 0);
144   gtk_widget_show(proto_sw);
145
146 #if GTK_MAJOR_VERSION < 2
147   proto_list = gtk_clist_new_with_titles(3, titles);
148   gtk_container_add(GTK_CONTAINER(proto_sw), proto_list);
149   gtk_clist_set_selection_mode(GTK_CLIST(proto_list), GTK_SELECTION_BROWSE);
150   gtk_clist_column_titles_passive(GTK_CLIST(proto_list));
151   gtk_clist_column_titles_show(GTK_CLIST(proto_list));
152   gtk_clist_set_column_auto_resize(GTK_CLIST(proto_list), 0, FALSE);
153   gtk_clist_set_column_auto_resize(GTK_CLIST(proto_list), 1, TRUE);
154   gtk_clist_set_column_auto_resize(GTK_CLIST(proto_list), 2, TRUE);
155   width = gdk_string_width(proto_list->style->font, DISABLED);
156   gtk_clist_set_column_width(GTK_CLIST(proto_list), 0, width);
157   SIGNAL_CONNECT(proto_list, "select-row", proto_list_select_cb, NULL);
158   SIGNAL_CONNECT(proto_list, "key-press-event", proto_list_keypress_cb, NULL);
159   show_proto_selection(GTK_CLIST(proto_list));
160 #else
161   proto_store = gtk_list_store_new(4, G_TYPE_BOOLEAN, G_TYPE_STRING,
162                                    G_TYPE_STRING, G_TYPE_POINTER);
163   show_proto_selection(proto_store);
164   /* default sort on "abbrev" column */
165   gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(proto_store), 1,
166                                        GTK_SORT_ASCENDING);
167   proto_list = tree_view_new(GTK_TREE_MODEL(proto_store));
168   gtk_container_add(GTK_CONTAINER(proto_sw), proto_list);
169   proto_rend = gtk_cell_renderer_toggle_new();
170   SIGNAL_CONNECT(proto_rend, "toggled", status_toggled, proto_store);
171   proto_col = gtk_tree_view_column_new_with_attributes(titles[0], proto_rend,
172                                                     "active", 0, NULL);
173   gtk_tree_view_column_set_sort_column_id(proto_col, 0);
174   gtk_tree_view_append_column(GTK_TREE_VIEW(proto_list), proto_col);
175   proto_rend = gtk_cell_renderer_text_new();
176   proto_col = gtk_tree_view_column_new_with_attributes(titles[1], proto_rend,
177                                                     "text", 1, NULL);
178   gtk_tree_view_column_set_sort_column_id(proto_col, 1);
179   gtk_tree_view_append_column(GTK_TREE_VIEW(proto_list), proto_col);
180   proto_rend = gtk_cell_renderer_text_new();
181   proto_col = gtk_tree_view_column_new_with_attributes(titles[2], proto_rend,
182                                                     "text", 2, NULL);
183   gtk_tree_view_column_set_sort_column_id(proto_col, 2);
184   gtk_tree_view_append_column(GTK_TREE_VIEW(proto_list), proto_col);
185   g_object_unref(G_OBJECT(proto_store));
186 #endif
187   gtk_widget_show(proto_list);
188
189   label = gtk_label_new("Disabling a protocol prevents higher "
190                         "layer protocols from being displayed");
191   gtk_misc_set_alignment(GTK_MISC(label), 0.5, 0.5);
192   gtk_widget_show(label);
193   gtk_box_pack_start(GTK_BOX(proto_vb), label, FALSE, FALSE, 5);
194
195
196   bbox = gtk_hbutton_box_new();
197   gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END);
198   gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
199   gtk_box_pack_start(GTK_BOX(proto_vb), bbox, FALSE, FALSE, 0);
200   gtk_widget_show(bbox);
201
202   /* Enable All */
203   button = gtk_button_new_with_label("Enable All");
204   SIGNAL_CONNECT(button, "clicked", enable_all_cb, proto_list);
205   gtk_box_pack_start(GTK_BOX(bbox), button, TRUE, TRUE, 0);
206   gtk_widget_show(button);
207
208   /* Disable All */
209   button = gtk_button_new_with_label("Disable All");
210   SIGNAL_CONNECT(button, "clicked", disable_all_cb, proto_list);
211   gtk_box_pack_start(GTK_BOX(bbox), button, TRUE, TRUE, 0);
212   gtk_widget_show(button);
213
214   /* Invert */
215   button = gtk_button_new_with_label("Invert");
216   SIGNAL_CONNECT(button, "clicked", toggle_all_cb, proto_list);
217   gtk_box_pack_start(GTK_BOX(bbox), button, TRUE, TRUE, 0);
218   gtk_widget_show(button);
219
220
221   /* Button row */
222   bbox = dlg_button_row_new(GTK_STOCK_OK, GTK_STOCK_APPLY, GTK_STOCK_SAVE, GTK_STOCK_CANCEL, NULL);
223   gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
224   gtk_widget_show(bbox);
225
226   button = OBJECT_GET_DATA(bbox, GTK_STOCK_OK);
227   SIGNAL_CONNECT(button, "clicked", proto_ok_cb, proto_w);
228   gtk_widget_grab_default(button);
229
230   button = OBJECT_GET_DATA(bbox, GTK_STOCK_APPLY);
231   SIGNAL_CONNECT(button, "clicked", proto_apply_cb, proto_w);
232
233   button = OBJECT_GET_DATA(bbox, GTK_STOCK_SAVE);
234   SIGNAL_CONNECT(button, "clicked", proto_save_cb, proto_w);
235
236   button = OBJECT_GET_DATA(bbox, GTK_STOCK_CANCEL);
237   window_set_cancel_button(proto_w, button, proto_cancel_cb);
238
239   SIGNAL_CONNECT(proto_w, "delete_event", proto_delete_event_cb, NULL);
240   SIGNAL_CONNECT(proto_w, "destroy", proto_destroy_cb, NULL);
241
242   gtk_quit_add_destroy(gtk_main_level(), GTK_OBJECT(proto_w));
243
244   gtk_widget_show(proto_w);
245   window_present(proto_w);
246 } /* proto_cb */
247
248 #if GTK_MAJOR_VERSION < 2
249 static void
250 proto_list_select_cb(GtkCList *proto_list, gint row, gint col, 
251                      GdkEventButton *ev _U_, gpointer gp _U_) {
252   protocol_data_t *p = gtk_clist_get_row_data(proto_list, row);
253   
254   if (row < 0 || col < 0)
255     return;
256
257   if (p->enabled)
258     p->enabled = FALSE;
259   else
260     p->enabled = TRUE;
261
262   gtk_clist_set_text(proto_list, row, 0, STATUS_TXT(p->enabled) );
263 } /* proto_list_select_cb */
264
265 static gboolean
266 proto_list_keypress_cb(GtkWidget *pl, GdkEventKey *ev, gpointer gp _U_) {
267   GtkCList *proto_list = GTK_CLIST(pl);
268   
269   if (ev->keyval == GDK_space) {
270     proto_list_select_cb(proto_list, proto_list->focus_row, 0, NULL, gp);
271   }
272   return TRUE;
273 }
274
275 #else
276 static void
277 status_toggled(GtkCellRendererToggle *cell _U_, gchar *path_str, gpointer data)
278 {
279   GtkTreeModel    *model = (GtkTreeModel *)data;
280   GtkTreeIter      iter;
281   GtkTreePath     *path = gtk_tree_path_new_from_string(path_str);
282   protocol_data_t *p;
283
284   gtk_tree_model_get_iter(model, &iter, path);
285   gtk_tree_model_get(model, &iter, 3, &p, -1);
286
287   if (p->enabled)
288     p->enabled = FALSE;
289   else
290     p->enabled = TRUE;
291
292   gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0, p->enabled, -1);
293
294   gtk_tree_path_free(path);
295 } /* status toggled */
296 #endif
297
298 /* XXX - We need callbacks for Gtk2 */
299
300
301 /* Toggle All */
302 static void
303 toggle_all_cb(GtkWidget *button _U_, gpointer pl)
304 {
305   GSList *entry;
306 #if GTK_MAJOR_VERSION < 2
307   GtkCList *proto_list = GTK_CLIST(pl);
308 #else
309   GtkListStore *s = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(pl)));
310 #endif
311
312   for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
313     protocol_data_t *p = entry->data;
314
315     if (p->enabled)
316       p->enabled = FALSE;
317     else
318       p->enabled = TRUE;
319     
320 #if GTK_MAJOR_VERSION < 2
321     gtk_clist_set_text(proto_list, p->row, 0, STATUS_TXT(p->enabled) );
322 #else
323     gtk_list_store_set(s, &p->iter, 0, p->enabled, -1);
324 #endif
325   }
326 }
327
328 /* Enable/Disable All Helper */
329 static void
330 set_active_all(GtkWidget *w, gboolean new_state)
331 {
332
333 #if GTK_MAJOR_VERSION < 2
334   GtkCList *proto_list = GTK_CLIST(w);
335 #else
336   GtkListStore *s = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(w)));
337 #endif
338   GSList *entry;
339
340 #if GTK_MAJOR_VERSION < 2
341   gtk_clist_freeze(proto_list);
342 #endif
343   for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
344     protocol_data_t *p = entry->data;
345     
346     p->enabled = new_state;
347 #if GTK_MAJOR_VERSION < 2
348     gtk_clist_set_text(proto_list, p->row, 0, STATUS_TXT(new_state) );
349 #else
350     gtk_list_store_set(s, &p->iter, 0, new_state, -1);
351 #endif
352   }
353 #if GTK_MAJOR_VERSION < 2
354   gtk_clist_thaw(proto_list);
355 #endif
356 }
357
358 /* Enable All */
359 static void
360 enable_all_cb(GtkWidget *button _U_, gpointer pl)
361 {
362         set_active_all(pl, TRUE);
363 }
364
365 /* Disable All */
366 static void
367 disable_all_cb(GtkWidget *button _U_, gpointer pl)
368 {
369         set_active_all(pl, FALSE);
370 }
371
372 static void
373 proto_destroy_cb(GtkWidget *w _U_, gpointer data _U_)
374 {
375   GSList *entry;
376
377   proto_w = NULL;
378
379   /* remove protocol list */
380   if (protocol_list) {
381     for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
382       g_free(entry->data);
383     }
384     g_slist_free(protocol_list);
385     protocol_list = NULL;
386   }
387 }
388
389 /* Treat this as a cancel, by calling "proto_cancel_cb()".
390    XXX - that'll destroy the Protocols dialog; will that upset
391    a higher-level handler that says "OK, we've been asked to delete
392    this, so destroy it"? */
393 static gboolean
394 proto_delete_event_cb(GtkWidget *proto_w, GdkEvent *event _U_,
395                       gpointer dummy _U_)
396 {
397   proto_cancel_cb(NULL, proto_w);
398   return FALSE;
399 }
400
401 static void
402 proto_ok_cb(GtkWidget *ok_bt _U_, gpointer parent_w)
403 {
404   gboolean redissect;
405
406   redissect = set_proto_selection(GTK_WIDGET(parent_w));
407   window_destroy(GTK_WIDGET(parent_w));
408   if (redissect)
409     redissect_packets(&cfile);
410 }
411
412 static void
413 proto_apply_cb(GtkWidget *apply_bt _U_, gpointer parent_w)
414 {
415   if (set_proto_selection(GTK_WIDGET(parent_w)))
416     redissect_packets(&cfile);
417 }
418
419 static void
420 proto_save_cb(GtkWidget *save_bt _U_, gpointer parent_w)
421 {
422   gboolean must_redissect = FALSE;
423   char *pf_dir_path;
424   char *pf_path;
425   int pf_save_errno;
426
427   /* Create the directory that holds personal configuration files, if
428      necessary.  */
429   if (create_persconffile_dir(&pf_dir_path) == -1) {
430      simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
431       "Can't create directory\n\"%s\"\nfor disabled protocols file: %s.", pf_dir_path,
432       strerror(errno));
433      g_free(pf_dir_path);
434   } else {
435     /*
436      * make disabled/enabled protocol settings current
437      */
438     must_redissect = set_proto_selection(GTK_WIDGET(parent_w));
439
440     save_disabled_protos_list(&pf_path, &pf_save_errno);
441     if (pf_path != NULL) {
442         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
443             "Could not save to your disabled protocols file\n\"%s\": %s.",
444             pf_path, strerror(pf_save_errno));
445         g_free(pf_path);
446     }
447   }
448
449   if (must_redissect) {
450     /* Redissect all the packets, and re-evaluate the display filter. */
451     redissect_packets(&cfile);
452   }
453 }
454
455 static void
456 proto_cancel_cb(GtkWidget *cancel_bt _U_, gpointer parent_w)
457 {
458   gboolean redissect;
459
460   redissect = revert_proto_selection();
461   window_destroy(GTK_WIDGET(parent_w));
462   if (redissect)
463     redissect_packets(&cfile);
464 }
465
466 static gboolean
467 set_proto_selection(GtkWidget *parent_w _U_)
468 {
469   GSList *entry;
470   gboolean need_redissect = FALSE;
471
472   for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
473     protocol_data_t *p = entry->data;
474     protocol_t *protocol;
475
476     protocol = find_protocol_by_id(p->hfinfo_index);
477     if (proto_is_protocol_enabled(protocol) != p->enabled) {
478       proto_set_decoding(p->hfinfo_index, p->enabled);
479       need_redissect = TRUE;
480     }
481   }
482
483   return need_redissect;
484
485 } /* set_proto_selection */
486
487 static gboolean
488 revert_proto_selection(void)
489 {
490   GSList *entry;
491   gboolean need_redissect = FALSE;
492
493   /*
494    * Undo all the changes we've made to protocol enable flags.
495    */
496   for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
497     protocol_data_t *p = entry->data;
498     protocol_t *protocol;
499
500     protocol = find_protocol_by_id(p->hfinfo_index);
501     if (proto_is_protocol_enabled(protocol) != p->was_enabled) {
502       proto_set_decoding(p->hfinfo_index, p->was_enabled);
503       need_redissect = TRUE;
504     }
505   }
506
507   return need_redissect;
508
509 } /* revert_proto_selection */
510
511 gint
512 protocol_data_compare(gconstpointer a, gconstpointer b)
513 {
514   const protocol_data_t *ap = (const protocol_data_t *)a;
515   const protocol_data_t *bp = (const protocol_data_t *)b;
516
517   return strcmp(ap->abbrev, bp->abbrev);
518 }
519
520 static void
521 #if GTK_MAJOR_VERSION < 2
522 show_proto_selection(GtkCList *proto_list)
523 #else
524 show_proto_selection(GtkListStore *proto_store)
525 #endif
526 {
527   GSList *entry;
528   gint i;
529   void *cookie;
530   protocol_t *protocol;
531   protocol_data_t *p;
532 #if GTK_MAJOR_VERSION < 2
533   gchar *proto_text[3];
534 #endif
535
536   /* Iterate over all the protocols */
537
538   for (i = proto_get_first_protocol(&cookie); i != -1;
539        i = proto_get_next_protocol(&cookie)) {
540       if (proto_can_toggle_protocol(i)) {
541         p = g_malloc(sizeof(protocol_data_t));
542         protocol = find_protocol_by_id(i);
543         p->name = proto_get_protocol_name(i);
544         p->abbrev = proto_get_protocol_short_name(protocol);
545         p->hfinfo_index = i;
546         p->enabled = proto_is_protocol_enabled(protocol);
547         p->was_enabled = p->enabled;
548         protocol_list = g_slist_insert_sorted(protocol_list,
549                                             p, protocol_data_compare);
550       }
551   }
552
553   for (entry = protocol_list; entry != NULL; entry = g_slist_next(entry)) {
554     p = entry->data;
555
556 #if GTK_MAJOR_VERSION < 2
557     /* XXX - The preferred way to do this would be to have a check box
558      * in the first column.  GtkClists don't let us put arbitrary widgets
559      * in a cell, so we use the word "Disabled" instead.  We should be
560      * able to use check boxes in Gtk2, however.
561      */        
562     proto_text[0] = STATUS_TXT (p->enabled);
563     proto_text[1] = p->abbrev;
564     proto_text[2] = p->name;
565     p->row = gtk_clist_append(proto_list, proto_text);
566     gtk_clist_set_row_data(proto_list, p->row, p);
567 #else
568     gtk_list_store_append(proto_store, &p->iter);
569     gtk_list_store_set(proto_store, &p->iter,
570                        0, p->enabled,
571                        1, p->abbrev,
572                        2, p->name,
573                        3, p,
574                       -1);
575 #endif
576   }
577
578 } /* show_proto_selection */