Handle -I in the options processing.
[obnox/wireshark/wip.git] / gtk / expert_comp_table.c
1 /* expert_comp_table.c
2  * expert_comp_table   2005 Greg Morris
3  * Portions copied from service_response_time_table.c by Ronnie Sahlberg
4  * Helper routines common to all composite expert statistics
5  * tap.
6  *
7  * $Id$
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998 Gerald Combs
12  * 
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * 
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <string.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <math.h>
36
37 #include <gtk/gtk.h>
38
39 #include "epan/packet_info.h"
40 #include "epan/strutil.h"
41
42 #include <epan/expert.h>
43
44 #include "../simple_dialog.h"
45 #include "../globals.h"
46 #include "../color.h"
47
48 #include "gtk/expert_comp_table.h"
49 #include "gtk/filter_utils.h"
50 #include "gtk/find_dlg.h"
51 #include "gtk/color_dlg.h"
52 #include "gtk/main.h"
53 #include "gtk/gui_utils.h"
54 #include "gtk/gtkglobals.h"
55 #include "gtk/webbrowser.h"
56
57 const char  *packet = "Packet:";
58
59 enum
60 {
61    GROUP_COLUMN,
62    PROTOCOL_COLUMN,
63    SUMMARY_COLUMN,
64    COUNT_COLUMN,
65    N_COLUMNS
66 };
67
68 static void
69 proto_data_func (GtkTreeViewColumn *column _U_,
70                            GtkCellRenderer   *renderer,
71                            GtkTreeModel      *model,
72                            GtkTreeIter       *iter,
73                            gpointer           user_data)
74 {       
75          gchar *str = NULL;
76          gchar *grp = NULL; /* type pointer, don't free */
77
78      /* The col to get data from is in userdata */
79      gint data_column = GPOINTER_TO_INT(user_data);
80
81      gtk_tree_model_get(model, iter, data_column, &str, -1);
82      gtk_tree_model_get(model, iter, GROUP_COLUMN, &grp, -1);
83          /* XXX should we check that str is non NULL and print a warning or do assert? */
84
85      g_object_set(renderer, "text", str, NULL);
86      if (grp == packet) {
87          /* it's a number right align */
88          g_object_set(renderer, "xalign", 1.0, NULL);
89      }
90      else {
91          g_object_set(renderer, "xalign", 0.0, NULL);
92      }
93      g_free(str);
94 }
95
96 static gint
97 proto_sort_func(GtkTreeModel *model,
98                                                         GtkTreeIter *a,
99                                                         GtkTreeIter *b,
100                                                         gpointer user_data)
101 {
102          gchar *str_a = NULL;
103          gchar *str_b = NULL;
104          gchar *grp = NULL; /* type pointer, don't free */
105          gint ret = 0;
106
107          /* The col to get data from is in userdata */
108          gint data_column = GPOINTER_TO_INT(user_data);
109
110      gtk_tree_model_get(model, a, data_column, &str_a, -1);
111      gtk_tree_model_get(model, b, data_column, &str_b, -1);
112      gtk_tree_model_get(model, a, GROUP_COLUMN, &grp, -1);
113
114         if (str_a == str_b) {
115                 ret = 0;
116         } 
117         else if (str_a == NULL || str_b == NULL) {
118                 ret = (str_a == NULL) ? -1 : 1;
119         } 
120         else {
121         if (grp == packet) {
122           gint int_a = atoi(str_a);
123           gint int_b = atoi(str_b);
124           if (int_a == int_b)
125               ret = 0;
126           else if (int_a < int_b)
127               ret = -1;
128           else 
129               ret = 1;
130         }
131         else
132                     ret = g_ascii_strcasecmp(str_a,str_b);
133         }
134         g_free(str_a);
135         g_free(str_b);
136         return ret;
137 }
138
139 static gint find_summary_data(error_equiv_table *err, const expert_info_t *expert_data)
140 {
141     guint i;
142     error_procedure_t *procedure;
143     
144     /* First time thru values will be 0 */
145     if (err->num_procs==0) {
146         return -1;
147     }
148     for (i=0;i<err->num_procs;i++) {
149         procedure = &g_array_index(err->procs_array, error_procedure_t, i);
150         if (strcmp(procedure->entries[0], expert_data->protocol) == 0 &&
151             strcmp(procedure->entries[1], expert_data->summary) == 0) {
152             return i;
153         }
154     }
155     return -1;
156 }
157
158 static void
159 error_select_filter_cb(GtkWidget *widget _U_, gpointer callback_data, guint callback_action)
160 {
161     int action, type, selection;
162     error_equiv_table *err = (error_equiv_table *)callback_data;
163     char str[512];
164     const char *current_filter;
165     error_procedure_t *procedure;
166
167     GtkTreeIter iter;
168     GtkTreeModel *model;
169     expert_info_t expert_data;
170     gchar *grp;
171
172     action=FILTER_ACTION(callback_action);
173     type=FILTER_ACTYPE(callback_action);
174
175
176     gtk_tree_selection_get_selected(err->select, &model, &iter);
177
178     gtk_tree_model_get (model, &iter, 
179                         GROUP_COLUMN,    &grp,
180                         PROTOCOL_COLUMN, &expert_data.protocol,
181                         SUMMARY_COLUMN,  &expert_data.summary,
182                         -1);
183     
184     if (strcmp(grp, packet)==0) {
185         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "You cannot filter or search for packet number. Click on a valid item header.");
186         g_free(expert_data.summary);
187         return;
188     }
189
190     /* XXX: find_summary_data doesn't (currently) reference expert_data.group.   */
191     /*      If "group" is required, then the message from GROUP_COLUMN will need */
192     /*       to be translated to the group number (or the actual group number    */
193     /*       will also need to be stored in the TreeModel).                      */
194     selection = find_summary_data(err, &expert_data);
195     /* g_free(expert_data.protocol); - const */
196     g_free(expert_data.summary);
197
198     if(selection>=(int)err->num_procs){
199         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "No items are selected");
200         return;
201     }
202     current_filter=gtk_entry_get_text(GTK_ENTRY(main_display_filter_widget));
203
204     /* Some expert data doesn't pass an expert item. Without this we cannot create a filter */
205     /* But allow for searching of internet for error string */
206     procedure = &g_array_index(err->procs_array, error_procedure_t, selection);
207
208     /* FIXME what is 7 ?*/
209     if (action != ACTION_WEB_LOOKUP && action != 7) {
210         char *msg;
211         if (0 /*procedure->fvalue_value==NULL*/) {
212             if (action != ACTION_FIND_FRAME && action != ACTION_FIND_NEXT && action != ACTION_FIND_PREVIOUS) {
213                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Wireshark cannot create a filter on this item - %s, try using find instead.", 
214                               procedure->entries[1]);
215                 return;
216             }
217         }
218         msg = g_malloc(escape_string_len(procedure->entries[1]));
219         escape_string(msg, procedure->entries[1]);
220         switch(type){
221         case ACTYPE_SELECTED:
222             /* if no expert item was passed */
223             if (procedure->fvalue_value==NULL) {
224                 g_snprintf(str, sizeof(str), "expert.message==%s", msg);
225             }
226             else
227             {
228                 /* expert item exists. Use it. */
229                 g_strlcpy(str, procedure->fvalue_value, sizeof(str));
230             }
231             break;
232         case ACTYPE_NOT_SELECTED:
233             /* if no expert item was passed */
234             if (procedure->fvalue_value==NULL) {
235                 g_snprintf(str, sizeof(str), "!(expert.message==%s)", msg);
236             }
237             else
238             {
239                 /* expert item exists. Use it. */
240                 g_snprintf(str, sizeof(str), "!(%s)", procedure->fvalue_value);
241             }
242             break;
243             /* the remaining cases will only exist if the expert item exists so no need to check */
244         case ACTYPE_AND_SELECTED:
245             if ((!current_filter) || (0 == strlen(current_filter)))
246                 g_snprintf(str, sizeof(str), "expert.message==%s", msg);
247             else
248                 g_snprintf(str, sizeof(str), "(%s) && (expert.message==%s)", current_filter, msg);
249             break;
250         case ACTYPE_OR_SELECTED:
251             if ((!current_filter) || (0 == strlen(current_filter)))
252                 g_snprintf(str, sizeof(str), "expert.message==%s", msg);
253             else
254                 g_snprintf(str, sizeof(str), "(%s) || (expert.message==%s)", current_filter, msg);
255             break;
256         case ACTYPE_AND_NOT_SELECTED:
257             if ((!current_filter) || (0 == strlen(current_filter)))
258                 g_snprintf(str, sizeof(str), "!(expert.message==%s)", msg);
259             else
260                 g_snprintf(str, sizeof(str), "(%s) && !(expert.message==%s)", current_filter, msg);
261             break;
262         case ACTYPE_OR_NOT_SELECTED:
263             if ((!current_filter) || (0 == strlen(current_filter)))
264                 g_snprintf(str, sizeof(str), "!(expert.message==%s)", msg);
265             else
266                 g_snprintf(str, sizeof(str), "(%s) || !(expert.message==%s)", current_filter, msg);
267             break;
268         default:
269             simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Can't find menu type - %u", type);
270         }
271         g_free(msg);
272     }
273
274     switch(action){
275     case ACTION_MATCH:
276         main_filter_packets(&cfile, str, FALSE);
277         break;
278     case ACTION_PREPARE:
279         gtk_entry_set_text(GTK_ENTRY(main_display_filter_widget), str);
280         break;
281     case ACTION_FIND_FRAME:
282         /* When trying to perform a find without expert item, we must pass
283          * the expert string to the find window. The user might need to modify
284          * the string and click on the text search to locate the packet in question.
285          * So regardless of the type we will just bring up the find window and allow
286          * the user to modify the search criteria and options.
287          */
288         find_frame_with_filter(str);
289         break;
290     case ACTION_FIND_NEXT:
291         /* In the case of find next, if there was no expert item, then most likely the expert
292          * string was modified to locate the text inside the message. So we can't just perform
293          * a find with the expert string or we will not really be performing a find next.
294          * In an effort to allow the user to modify the string and/or continue searching, we
295          * will just present the user with the find window again with the default expert string.
296          * A better aproach would be to attempt in capturing the last find string and utilize this 
297          * with a find next/previous. Also a better approach might be to just send a <Ctl-N> keystroke.
298          */
299         if (procedure->fvalue_value==NULL) {
300             find_frame_with_filter(str);
301         }
302         else
303         { 
304             /* We have an expert item so just continue search without find dialog. */
305             find_previous_next_frame_with_filter(str, FALSE);
306         }
307         break;
308     case ACTION_FIND_PREVIOUS:
309         /* In the case of find previous, if there was no expert item, then most likely the expert
310          * string was modified to locate the text inside the message. So we can't just perform
311          * a find with the expert string or we will not really be performing a find previous.
312          * In an effort to allow the user to modify the string and/or continue searching, we
313          * will just present the user with the find window again with the default expert string.
314          * A better aproach would be to attempt in capturing the last find string and utilize this 
315          * with a find next/previous. Also a better approach might be to just send a <Ctl-B> keystroke.
316          */
317         if (procedure->fvalue_value==NULL) {
318             find_frame_with_filter(str);
319         }
320         else
321         { 
322             /* We have an expert item so just continue search without find dialog. */
323             find_previous_next_frame_with_filter(str, TRUE);
324         }
325         break;
326     case ACTION_COLORIZE:
327         color_display_with_filter(str);
328         break;
329     case ACTION_WEB_LOOKUP:
330         /* Lookup expert string on internet. Default search via www.google.com */
331         g_snprintf(str, sizeof(str), "http://www.google.com/search?hl=en&q=%s+'%s'", procedure->entries[0], procedure->entries[1]);
332         browser_open_url(str);
333         break;
334     default:
335         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Can't find menu action - %u", action);
336     }
337 }
338
339 static gint
340 error_show_popup_menu_cb(void *widg _U_, GdkEvent *event, error_equiv_table *err)
341 {
342         GdkEventButton *bevent = (GdkEventButton *)event;
343
344         if(event->type==GDK_BUTTON_PRESS && bevent->button==3){
345                 gtk_menu_popup(GTK_MENU(err->menu), NULL, NULL, NULL, NULL, 
346                         bevent->button, bevent->time);
347         }
348
349         return FALSE;
350 }
351
352 static GtkItemFactoryEntry error_list_menu_items[] =
353 {
354         /* Match */
355         {"/Apply as Filter", NULL, NULL, 0, "<Branch>", NULL,},
356         {"/Apply as Filter/Selected", NULL,
357                 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_MATCH(ACTYPE_SELECTED, 0),
358                 NULL, NULL,},
359         {"/Apply as Filter/... not Selected", NULL,
360                 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_MATCH(ACTYPE_NOT_SELECTED, 0),
361                 NULL, NULL,},
362         {"/Apply as Filter/.. and Selected", NULL,
363                 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_MATCH(ACTYPE_AND_SELECTED, 0),
364                 NULL, NULL,},
365         {"/Apply as Filter/... or Selected", NULL,
366                 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_MATCH(ACTYPE_OR_SELECTED, 0),
367                 NULL, NULL,},
368         {"/Apply as Filter/... and not Selected", NULL,
369                 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_MATCH(ACTYPE_AND_NOT_SELECTED, 0),
370                 NULL, NULL,},
371         {"/Apply as Filter/... or not Selected", NULL,
372                 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_MATCH(ACTYPE_OR_NOT_SELECTED, 0),
373                 NULL, NULL,},
374
375         /* Prepare */
376         {"/Prepare a Filter", NULL, NULL, 0, "<Branch>", NULL,},
377         {"/Prepare a Filter/Selected", NULL,
378                 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_PREPARE(ACTYPE_SELECTED, 0),
379                 NULL, NULL,},
380         {"/Prepare a Filter/Not Selected", NULL,
381                 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_PREPARE(ACTYPE_NOT_SELECTED, 0),
382                 NULL, NULL,},
383         {"/Prepare a Filter/... and Selected", NULL,
384                 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_PREPARE(ACTYPE_AND_SELECTED, 0),
385                 NULL, NULL,},
386         {"/Prepare a Filter/... or Selected", NULL,
387                 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_PREPARE(ACTYPE_OR_SELECTED, 0),
388                 NULL, NULL,},
389         {"/Prepare a Filter/... and not Selected", NULL,
390                 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_PREPARE(ACTYPE_AND_NOT_SELECTED, 0),
391                 NULL, NULL,},
392         {"/Prepare a Filter/... or not Selected", NULL,
393                 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_PREPARE(ACTYPE_OR_NOT_SELECTED, 0),
394                 NULL, NULL,},
395
396         /* Find Frame */
397         {"/Find Frame", NULL, NULL, 0, "<Branch>", NULL,},
398         {"/Find Frame/Find Frame", NULL, NULL, 0, "<Branch>", NULL,},
399         {"/Find Frame/Find Frame/Selected", NULL,
400                 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_FIND_FRAME(ACTYPE_SELECTED, 0),
401                 NULL, NULL,},
402         {"/Find Frame/Find Frame/Not Selected", NULL,
403                 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_FIND_FRAME(ACTYPE_NOT_SELECTED, 0),
404                 NULL, NULL,},
405         /* Find Next */
406         {"/Find Frame/Find Next", NULL, NULL, 0, "<Branch>", NULL,},
407         {"/Find Frame/Find Next/Selected", NULL,
408                 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_FIND_NEXT(ACTYPE_SELECTED, 0),
409                 NULL, NULL,},
410         {"/Find Frame/Find Next/Not Selected", NULL,
411                 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_FIND_NEXT(ACTYPE_NOT_SELECTED, 0),
412                 NULL, NULL,},
413
414         /* Find Previous */
415         {"/Find Frame/Find Previous", NULL, NULL, 0, "<Branch>", NULL,},
416         {"/Find Frame/Find Previous/Selected", NULL,
417                 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_FIND_PREVIOUS(ACTYPE_SELECTED, 0),
418                 NULL, NULL,},
419         {"/Find Frame/Find Previous/Not Selected", NULL,
420                 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_FIND_PREVIOUS(ACTYPE_NOT_SELECTED, 0),
421                 NULL, NULL,},
422
423         /* Colorize Procedure */
424         {"/Colorize Procedure", NULL, NULL, 0, "<Branch>", NULL,},
425         {"/Colorize Procedure/Selected", NULL,
426                 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_COLORIZE(ACTYPE_SELECTED, 0),
427                 NULL, NULL,},
428         {"/Colorize Procedure/Not Selected", NULL,
429                 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_COLORIZE(ACTYPE_NOT_SELECTED, 0),
430                 NULL, NULL,},
431
432         /* Search Internet */
433         {"/Internet Search for Info Text", NULL,
434                 GTK_MENU_FUNC(error_select_filter_cb), CALLBACK_WEB_LOOKUP, NULL, NULL,}
435 };
436
437 #if 0
438 /* Prepare to change GtkItemFactory to GtkUIManager */
439 static void
440 apply_as_selected_cb(GtkAction *action)
441 {
442 }
443 static void
444 apply_as_not_selected_cb(GtkAction *action)
445 {
446 }
447 static void
448 apply_as_and_selected_cb(GtkAction *action)
449 {
450 }
451 static void
452 apply_as_or_selected_cb(GtkAction *action)
453 {
454 }
455 static void
456 apply_as_and_not_selected_cb(GtkAction *action)
457 {
458 }
459 static void
460 apply_as_or_not_selected_cb(GtkAction *action)
461 {
462 }
463
464 static void
465 prep_as_selected_cb(GtkAction *action)
466 {
467 }
468 static void
469 prep_as_not_selected_cb(GtkAction *action)
470 {
471 }
472 static void
473 prep_as_and_selected_cb(GtkAction *action)
474 {
475 }
476 static void
477 prep_as_or_selected_cb(GtkAction *action)
478 {
479 }
480 static void
481 prep_as_and_not_selected_cb(GtkAction *action)
482 {
483 }
484 static void
485 prep_as_or_not_selected_cb(GtkAction *action)
486 {
487 }
488
489 static void
490 find_selected_cb(GtkAction *action)
491 {
492 }
493 static void
494 find_not_selected_cb(GtkAction *action)
495 {
496 }
497 static void
498 find_prev_selected_cb(GtkAction *action)
499 {
500 }
501 static void
502 find_prev_not_selected_cb(GtkAction *action)
503 {
504 }
505 static void
506 find_next_selected_cb(GtkAction *action)
507 {
508 }
509 static void
510 find_next_not_selected_cb(GtkAction *action)
511 {
512 }
513 static void
514 color_next_selected_cb(GtkAction *action)
515 {
516 }
517 static void
518 color_next_not_selected_cb(GtkAction *action)
519 {
520 }
521 static const char *ui_description =
522 "<ui>"
523 "  <popup name='MainMenu'>"
524 "    <menu action='/Apply as Filter'>"
525 "      <menuitem action='/Apply as Filter/Selected'/>"
526 "      <menuitem action='/Apply as Filter/... not Selected'/>"
527 "      <menuitem action='/Apply as Filter/... and Selected'/>"
528 "      <menuitem action='/Apply as Filter/... or Selected'/>"
529 "      <menuitem action='/Apply as Filter/... and not Selected'/>"
530 "      <menuitem action='/Apply as Filter/... or not Selected'/>"
531 "    </menu>"
532 "    <menu action='/Prepare a Filter'>"
533 "      <menuitem action='/Prepare a Filter/Selected'/>"
534 "      <menuitem action='/Prepare a Filter/... not Selected'/>"
535 "      <menuitem action='/Prepare a Filter/... and Selected'/>"
536 "      <menuitem action='/Prepare a Filter/... or Selected'/>"
537 "      <menuitem action='/Prepare a Filter/... and not Selected'/>"
538 "      <menuitem action='/Prepare a Filter/... or not Selected'/>"
539 "    </menu>"
540 "    <menu action='/Find Frame'>"
541 "      <menu action='/Find Frame/Find Frame'>"
542 "        <menuitem action='/Find Frame/Selected'/>"
543 "        <menuitem action='/Find Frame/Not Selected'/>"
544 "      </menu>"
545 "      <menu action='/Find Frame/Find Next'>"
546 "        <menuitem action='/Find Next/Selected'/>"
547 "        <menuitem action='/Find Next/Not Selected'/>"
548 "      </menu>"
549 "      <menu action='/Find Frame/Find Previous'>"
550 "        <menuitem action='/Find Previous/Selected'/>"
551 "        <menuitem action='/Find Previous/Not Selected'/>"
552 "      </menu>"
553 "    </menu>"
554 "    <menu action='/Colorize Procedure'>"
555 "     <menuitem action='/Colorize Procedure/Selected'/>"
556 "     <menuitem action='/Colorize Procedure/Not Selected'/>"
557 "    </menu>"
558 "  </popup>"
559 "</ui>";
560
561
562 /* Normal items */
563 static const GtkActionEntry entries[] = {
564   { "/Apply as Filter", NULL, "Apply as Filter" },
565   { "/Prepare a Filter", NULL, "Prepare a Filter" },
566   { "/Find Frame", NULL, "Find Frame" },
567   { "/Find Frame/Find Frame", NULL, "Find Frame" },
568   { "/Find Frame/Find Next", NULL, "Find Next" },
569   { "/Find Frame/Find Previous", NULL, "Find Previous" },
570   { "/Colorize Procedure", NULL, "Colorize Procedure" },
571   { "/Apply as Filter/Selected", NULL, "Selected", NULL, "Selected", G_CALLBACK(apply_as_selected_cb) },
572   { "/Apply as Filter/... not Selected", NULL, "... not Selected", NULL, "... not Selected", G_CALLBACK(apply_as_not_selected_cb) },
573   { "/Apply as Filter/... and Selected", NULL, "... and Selected", NULL, "... and Selected", G_CALLBACK(apply_as_and_selected_cb) },
574   { "/Apply as Filter/... or Selected", NULL, "... or Selected", NULL, "... or Selected", G_CALLBACK(apply_as_or_selected_cb) },
575   { "/Apply as Filter/... and not Selected", NULL, "... and not Selected", NULL, "... and not Selected", G_CALLBACK(apply_as_and_not_selected_cb) },
576   { "/Apply as Filter/... or not Selected", NULL, "... or not Selected", NULL, "... or not Selected", G_CALLBACK(apply_as_or_not_selected_cb) },
577   { "/Prepare a Filter/Selected", NULL, "Selected", NULL, "selcted", G_CALLBACK(prep_as_selected_cb) },
578   { "/Prepare a Filter/... not Selected", NULL, "... not Selected", NULL, "... not Selected", G_CALLBACK(prep_as_not_selected_cb) },
579   { "/Prepare a Filter/... and Selected", NULL, "... and Selected", NULL, "... and Selected", G_CALLBACK(prep_as_and_selected_cb) },
580   { "/Prepare a Filter/... or Selected", NULL, "... or Selected", NULL, "... or Selected", G_CALLBACK(prep_as_or_selected_cb) },
581   { "/Prepare a Filter/... and not Selected", NULL, "... and not Selected", NULL, "... and not Selected", G_CALLBACK(prep_as_and_not_selected_cb) },
582   { "/Prepare a Filter/... or not Selected", NULL, "... or not Selected", NULL, "... or not Selected", G_CALLBACK(prep_as_or_not_selected_cb) },
583   { "/Find Frame/Selected", NULL, "Selected", NULL, "Selected", G_CALLBACK(find_selected_cb) },
584   { "/Find Frame/Not Selected", NULL, "Not Selected", NULL, "Not Selected", G_CALLBACK(find_not_selected_cb) },
585   { "/Find Previous/Selected", NULL, "Selected", NULL, "Selected", G_CALLBACK(find_prev_selected_cb) },
586   { "/Find Previous/Not Selected", NULL, "Not Selected", NULL, "Not Selected", G_CALLBACK(find_prev_not_selected_cb) },
587   { "/Find Next/Selected", NULL, "Selected", NULL, "Selected", G_CALLBACK(find_next_selected_cb) },
588   { "/Find Next/Not Selected", NULL, "Not Selected", NULL, "Not Selected", G_CALLBACK(find_next_not_selected_cb) },
589   { "/Colorize Procedure/Selected", NULL, "Selected", NULL, "Selected", G_CALLBACK(color_next_selected_cb) },
590   { "/Colorize Procedure/Not Selected", NULL, "Not Selected", NULL, "Not Selected", G_CALLBACK(color_next_not_selected_cb) },
591 };
592 #endif
593 static void
594 expert_goto_pkt_cb (GtkTreeSelection *selection, gpointer data _U_)
595 {
596         GtkTreeIter iter;
597         GtkTreeModel *model;
598         gchar *pkt;
599         gchar *grp;
600
601         if (gtk_tree_selection_get_selected (selection, &model, &iter))
602         {
603                 gtk_tree_model_get (model, &iter, 
604                                     PROTOCOL_COLUMN, &pkt,
605                                     GROUP_COLUMN,    &grp,
606                                     -1);
607
608                 if (strcmp(grp, packet)==0) {
609                     cf_goto_frame(&cfile, atoi(pkt));
610                 }
611                 g_free (pkt);
612         }
613 }
614
615 static void
616 error_create_popup_menu(error_equiv_table *err)
617 {
618     GtkItemFactory *item_factory;
619
620
621     err->select = gtk_tree_view_get_selection (GTK_TREE_VIEW (err->tree_view));
622     gtk_tree_selection_set_mode (err->select, GTK_SELECTION_SINGLE);
623     g_signal_connect (G_OBJECT (err->select), "changed",
624                   G_CALLBACK (expert_goto_pkt_cb),
625                   err);
626     item_factory = gtk_item_factory_new(GTK_TYPE_MENU, "<main>", NULL);
627
628     gtk_item_factory_create_items_ac(item_factory, sizeof(error_list_menu_items)/sizeof(error_list_menu_items[0]), error_list_menu_items, err, 2);
629
630     err->menu = gtk_item_factory_get_widget(item_factory, "<main>");
631     g_signal_connect(err->tree_view, "button_press_event", G_CALLBACK(error_show_popup_menu_cb), err);
632 }
633
634 void
635 init_error_table(error_equiv_table *err, guint num_procs, GtkWidget *vbox)
636 {
637     GtkTreeStore *store;
638     GtkWidget *tree;
639     GtkTreeViewColumn *column;
640     GtkCellRenderer *renderer;
641     GtkTreeSortable *sortable;
642
643     /* Create the store */
644     store = gtk_tree_store_new (4,       /* Total number of columns */
645                                G_TYPE_POINTER,   /* Group              */
646                                G_TYPE_STRING,   /* Protocol           */
647                                G_TYPE_STRING,   /* Summary            */
648                                G_TYPE_INT);     /* Count              */
649
650     /* Create a view */
651     tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
652     err->tree_view = GTK_TREE_VIEW(tree);
653     sortable = GTK_TREE_SORTABLE(store);
654
655 #if GTK_CHECK_VERSION(2,6,0)
656         /* Speed up the list display */
657         gtk_tree_view_set_fixed_height_mode(err->tree_view, TRUE);
658 #endif
659
660     gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW (tree), FALSE);
661
662     /* The view now holds a reference.  We can get rid of our own reference */
663     g_object_unref (G_OBJECT (store));
664
665     /* Create a cell renderer */
666     renderer = gtk_cell_renderer_text_new ();
667
668     /* Create the first column, associating the "text" attribute of the
669      * cell_renderer to the first column of the model */
670     column = gtk_tree_view_column_new_with_attributes ("Group", renderer, NULL);
671     gtk_tree_view_column_set_sort_column_id(column, GROUP_COLUMN);
672     gtk_tree_view_column_set_resizable(column, TRUE);
673         gtk_tree_view_column_set_cell_data_func(column, renderer, str_ptr_data_func, 
674                 GINT_TO_POINTER(GROUP_COLUMN), NULL);
675
676         gtk_tree_sortable_set_sort_func(sortable, GROUP_COLUMN, str_ptr_sort_func,
677                 GINT_TO_POINTER(GROUP_COLUMN), NULL);
678
679     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
680     gtk_tree_view_column_set_min_width(column, 80);
681     /* Add the column to the view. */
682     gtk_tree_view_append_column (GTK_TREE_VIEW (err->tree_view), column);
683  
684     /* Second column.. Protocol. */
685     renderer = gtk_cell_renderer_text_new ();
686     column = gtk_tree_view_column_new_with_attributes ("Protocol", renderer, "text", PROTOCOL_COLUMN, NULL);
687     gtk_tree_view_column_set_sort_column_id(column, PROTOCOL_COLUMN);
688     gtk_tree_view_column_set_resizable(column, TRUE);
689         gtk_tree_view_column_set_cell_data_func(column, renderer, proto_data_func, 
690                 GINT_TO_POINTER(PROTOCOL_COLUMN), NULL);
691
692         gtk_tree_sortable_set_sort_func(sortable, PROTOCOL_COLUMN, proto_sort_func,
693                 GINT_TO_POINTER(PROTOCOL_COLUMN), NULL);
694
695     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
696     gtk_tree_view_column_set_min_width(column, 40);
697         gtk_tree_view_column_set_fixed_width(column, 100);
698     gtk_tree_view_append_column (GTK_TREE_VIEW (err->tree_view), column);
699  
700     /* Third column.. Summary. */
701     renderer = gtk_cell_renderer_text_new ();
702     column = gtk_tree_view_column_new_with_attributes ("Summary", renderer, "text", SUMMARY_COLUMN, NULL);
703     gtk_tree_view_column_set_sort_column_id(column, SUMMARY_COLUMN);
704     gtk_tree_view_column_set_resizable(column, TRUE);
705     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
706     gtk_tree_view_column_set_min_width(column, 80);
707         gtk_tree_view_column_set_fixed_width(column, 230);
708     gtk_tree_view_append_column (GTK_TREE_VIEW (err->tree_view), column);
709  
710     /* Last column.. Count. */
711     renderer = gtk_cell_renderer_text_new ();
712     /* right align */
713     g_object_set(G_OBJECT(renderer), "xalign", 1.0, NULL);
714     column = gtk_tree_view_column_new_with_attributes ("Count", renderer, "text", COUNT_COLUMN, NULL);
715     gtk_tree_view_column_set_sort_column_id(column, COUNT_COLUMN);
716     gtk_tree_view_column_set_resizable(column, TRUE);
717     gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
718     gtk_tree_view_column_set_min_width(column, 80);
719     gtk_tree_view_append_column (GTK_TREE_VIEW (err->tree_view), column);
720  
721     err->scrolled_window=scrolled_window_new(NULL, NULL);
722
723     gtk_container_add(GTK_CONTAINER(err->scrolled_window), GTK_WIDGET (err->tree_view));
724
725     gtk_box_pack_start(GTK_BOX(vbox), err->scrolled_window, TRUE, TRUE, 0);
726
727     gtk_tree_view_set_search_column (err->tree_view, SUMMARY_COLUMN); /* Allow searching the summary */
728     gtk_tree_view_set_reorderable (err->tree_view, TRUE);   /* Allow user to reorder data with drag n drop */
729     
730     /* Now enable the sorting of each column */
731     gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(err->tree_view), TRUE);
732     gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW(err->tree_view), TRUE);
733
734     gtk_widget_show(err->scrolled_window);
735
736     err->num_procs=num_procs;
737
738     err->text = g_string_chunk_new(100);
739     err->procs_array = g_array_sized_new(FALSE, FALSE, sizeof(error_procedure_t), num_procs);
740
741     /* create popup menu for this table */
742     error_create_popup_menu(err);
743 }
744
745 void
746 init_error_table_row(error_equiv_table *err, const expert_info_t *expert_data)
747 {
748     guint old_num_procs=err->num_procs;
749     gint row=0;
750     error_procedure_t *procedure;
751     GtkTreeStore *store;
752     GtkTreeIter   new_iter;
753     gchar num[10];
754
755     /* we have discovered a new procedure. Extend the table accordingly */
756     row = find_summary_data(err, expert_data);
757     if(row==-1){
758         error_procedure_t new_procedure;
759         /* First time we have seen this event so initialize memory table */
760         row = old_num_procs; /* Number of expert events since this is a new event */
761
762         new_procedure.count=0; /* count of events for this item */
763         new_procedure.fvalue_value = NULL; /* Filter string value */
764
765         g_array_append_val(err->procs_array, new_procedure);
766         procedure = &g_array_index(err->procs_array, error_procedure_t, row);
767         
768         /* Create the item in our memory table */
769         procedure->entries[0]=(char *)g_string_chunk_insert_const(err->text, expert_data->protocol);    /* Protocol */
770         procedure->entries[1]=(char *)g_string_chunk_insert_const(err->text, expert_data->summary);     /* Summary */
771
772         /* Create a new item in our tree view */
773         store = GTK_TREE_STORE(gtk_tree_view_get_model(err->tree_view)); /* Get store */
774         gtk_tree_store_append (store, &procedure->iter, NULL);  /* Acquire an iterator */
775         
776         /* match_strval return a static constant  or null */        
777         gtk_tree_store_set (store, &procedure->iter,
778                     GROUP_COLUMN, match_strval(expert_data->group, expert_group_vals), 
779                     PROTOCOL_COLUMN, procedure->entries[0],
780                     SUMMARY_COLUMN,  procedure->entries[1], -1);
781         
782         /* If an expert item was passed then build the filter string */
783         if (expert_data->pitem) {
784             char *filter;
785             
786             g_assert(PITEM_FINFO(expert_data->pitem));
787             filter = proto_construct_match_selected_string(PITEM_FINFO(expert_data->pitem), NULL);
788             if (filter != NULL)
789                 procedure->fvalue_value = g_string_chunk_insert_const(err->text, filter);
790         }
791         /* Store the updated count of events */
792         err->num_procs = ++old_num_procs;
793     }
794
795     /* Update our memory table with event data */
796     procedure = &g_array_index(err->procs_array, error_procedure_t, row);
797     procedure->count++; /* increment the count of events for this item */
798
799     /* Update the tree with new count for this event */
800     store = GTK_TREE_STORE(gtk_tree_view_get_model(err->tree_view));
801     gtk_tree_store_set(store, &procedure->iter, 
802                        COUNT_COLUMN, procedure->count, 
803                                            -1);
804
805     g_snprintf(num, sizeof(num), "%d", expert_data->packet_num);
806 #if 0
807         This does not have a big performance improvment :(
808 if GTK_CHECK_VERSION(2,10,0)
809         gtk_tree_store_insert_with_values   (store,
810                        &new_iter,   /* *iter */
811                        &procedure->iter, /* *parent*/
812                        G_MAXINT,    /* position */
813
814 #else
815
816     /* FIXME gtk is plagued with slow algorithms 
817        gtk_tree_store_append call new_path and its nice recursive linear search....
818     */
819     if (procedure->count > 1000) {
820         /* If there's more than 1000 sub rows give up and prepend new rows, at least 
821            it will end in a reasonable time. Anyway with so many rows it's not
822            very useful and if sorted the right order is restored. 
823         */
824         gtk_tree_store_prepend(store, &new_iter, &procedure->iter);
825     }
826     else {
827         gtk_tree_store_append(store, &new_iter, &procedure->iter);
828     }
829     gtk_tree_store_set(store, &new_iter,
830 #endif
831                        GROUP_COLUMN,    packet,
832                        PROTOCOL_COLUMN, num,
833                        COUNT_COLUMN,    1,
834                        -1);
835 }
836
837 void
838 reset_error_table_data(error_equiv_table *err)
839 {
840     GtkTreeStore    *store;
841
842     store = GTK_TREE_STORE(gtk_tree_view_get_model(err->tree_view));
843     gtk_tree_store_clear(store);
844     err->num_procs = 0;
845     /* g_string_chunk_clear() is introduced in glib 2.14 */
846     g_string_chunk_free(err->text);
847     err->text = g_string_chunk_new(100);
848
849     g_array_set_size(err->procs_array, 0);
850 }
851
852 void
853 free_error_table_data(error_equiv_table *err)
854 {
855     err->num_procs=0;
856     g_string_chunk_free(err->text);
857     g_array_free(err->procs_array, TRUE);
858 }