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