Immediately quit routines if fwrite() fails - further writes will
[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 #include <gtk/gtk.h>
37 #include "compat_macros.h"
38 #include "epan/packet_info.h"
39 #include "expert_comp_table.h"
40
41 #if (GTK_MAJOR_VERSION < 2)
42 #include "image/clist_ascend.xpm"
43 #include "image/clist_descend.xpm"
44 #endif
45
46 #include "simple_dialog.h"
47 #include "globals.h"
48 #include "gtk/find_dlg.h"
49 #include "color.h"
50 #include "gtk/color_dlg.h"
51 #include "main.h"
52 #include "gui_utils.h"
53 #include "gtkglobals.h"
54 #include "webbrowser.h"
55 #include <epan/expert.h>
56 #include <epan/emem.h>
57
58 #define GTK_MENU_FUNC(a) ((GtkItemFactoryCallback)(a))
59
60 #define SORT_ALPHABETICAL 0
61
62 #if (GTK_MAJOR_VERSION >= 2)
63 static gint
64 sort_iter_compare_func (GtkTreeModel *model,
65 GtkTreeIter *a,
66 GtkTreeIter *b,
67 gpointer userdata)
68 {
69     gint sortcol = GPOINTER_TO_INT(userdata);
70     gint ret = 0;
71     switch (sortcol)
72     {
73         case SORT_ALPHABETICAL:
74         {
75         gchar *name1, *name2;
76         gtk_tree_model_get(model, a, 0, &name1, -1);
77         gtk_tree_model_get(model, b, 0, &name2, -1);
78         if (name1 == NULL || name2 == NULL)
79         {
80             if (name1 == NULL && name2 == NULL)
81                 break; /* both equal => ret = 0 */
82             ret = (name1 == NULL) ? -1 : 1;
83         }
84         else
85         {
86             ret = g_ascii_strcasecmp(name1,name2);
87         }
88         g_free(name1);
89         g_free(name2);
90         }
91         break;
92         default:
93         g_return_val_if_reached(0);
94     }
95     return ret;
96 }
97 #endif
98
99 #if (GTK_MAJOR_VERSION < 2)
100
101 typedef struct column_arrows {
102         GtkWidget *table;
103         GtkWidget *ascend_pm;
104         GtkWidget *descend_pm;
105 } column_arrows;
106
107 static void
108 error_click_column_cb(GtkCList *clist, gint column, gpointer data)
109 {
110         column_arrows *col_arrows = (column_arrows *) data;
111         int i;
112
113         gtk_clist_freeze(clist);
114
115         for (i = 0; i < 4; i++) {
116                 gtk_widget_hide(col_arrows[i].ascend_pm);
117                 gtk_widget_hide(col_arrows[i].descend_pm);
118         }
119
120         if (column == clist->sort_column) {
121                 if (clist->sort_type == GTK_SORT_ASCENDING) {
122                         clist->sort_type = GTK_SORT_DESCENDING;
123                         gtk_widget_show(col_arrows[column].descend_pm);
124                 } else {
125                         clist->sort_type = GTK_SORT_ASCENDING;
126                         gtk_widget_show(col_arrows[column].ascend_pm);
127                 }
128         } else {
129                 if(column>=2){
130                         clist->sort_type = GTK_SORT_DESCENDING;
131                         gtk_widget_show(col_arrows[column].descend_pm);
132                 } else {
133                         clist->sort_type = GTK_SORT_ASCENDING;
134                         gtk_widget_show(col_arrows[column].ascend_pm);
135                 }
136                 gtk_clist_set_sort_column(clist, column);
137         }
138         gtk_clist_thaw(clist);
139
140         gtk_clist_sort(clist);
141 }
142
143 static gint
144 error_sort_column(GtkCList *clist, gconstpointer ptr1, gconstpointer ptr2)
145 {
146         char *text1 = NULL;
147         char *text2 = NULL;
148         float f1,f2;
149
150         const GtkCListRow *row1 = ptr1;
151         const GtkCListRow *row2 = ptr2;
152
153         text1 = GTK_CELL_TEXT (row1->cell[clist->sort_column])->text;
154         text2 = GTK_CELL_TEXT (row2->cell[clist->sort_column])->text;
155
156         switch(clist->sort_column){
157         case 0:
158         case 2:
159         case 1:
160                 return strcmp (text1, text2);
161         case 3:
162         case 4:
163         case 5:
164                 sscanf(text1,"%f",&f1);
165                 sscanf(text2,"%f",&f2);
166                 if(fabs(f1-f2)<0.000005)
167                         return 0;
168                 if(f1>f2)
169                         return 1;
170                 return -1;
171         }
172         g_assert_not_reached();
173         return 0;
174 }
175
176 #else
177 enum
178 {
179    GROUP_COLUMN,
180    PROTOCOL_COLUMN,
181    SUMMARY_COLUMN,
182    COUNT_COLUMN,
183    N_COLUMNS
184 };
185 #endif
186
187 static gint find_summary_data(error_equiv_table *err, const expert_info_t *expert_data)
188 {
189     gint i;
190     
191     /* First time thru values will be 0 */
192     if (err->num_procs==0) {
193         return -1;
194     }
195     for (i=0;i<err->num_procs;i++) {
196         if (strcmp(err->procedures[i].entries[2], expert_data->summary) == 0) {
197             return i;
198         }
199     }
200     return -1;
201 }
202
203 /* action is encoded as 
204    filter_action*256+filter_type
205
206    filter_action:
207         0: Match
208         1: Prepare
209         2: Find Frame
210         3:   Find Next
211         4:   Find Previous
212         5: Colorize Procedure
213     6: Lookup on Internet
214    filter_type:
215         0: Selected
216         1: Not Selected
217         2: And Selected
218         3: Or Selected
219         4: And Not Selected
220         5: Or Not Selected
221 */
222 static void
223 error_select_filter_cb(GtkWidget *widget _U_, gpointer callback_data, guint callback_action)
224 {
225         int action, type, selection;
226         error_equiv_table *err = (error_equiv_table *)callback_data;
227         char str[256];
228         const char *current_filter;
229
230 #if (GTK_MAJOR_VERSION >= 2)
231     GtkTreeIter iter;
232     GtkTreeModel *model;
233     const expert_info_t expert_data;
234 #endif
235
236     action=(callback_action>>8)&0xff;
237         type=callback_action&0xff;
238
239
240 #if (GTK_MAJOR_VERSION < 2)
241         selection=GPOINTER_TO_INT(g_list_nth_data(GTK_CLIST(err->table)->selection, 0));
242 #else
243     gtk_tree_selection_get_selected(err->select, &model, &iter);
244
245     gtk_tree_model_get (model, &iter, GROUP_COLUMN, &expert_data.group, -1);
246     gtk_tree_model_get (model, &iter, PROTOCOL_COLUMN, &expert_data.protocol, -1);
247     gtk_tree_model_get (model, &iter, SUMMARY_COLUMN, &expert_data.summary, -1);
248     
249     if (strcmp((char *)expert_data.group, "Packet:")==0) {
250                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "You cannot filter or search for packet number. Click on a valid item header.");
251         return;
252     }
253
254     selection = find_summary_data(err, &expert_data);
255 #endif
256
257         if(selection>=(int)err->num_procs){
258                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "No items are selected");
259                 return;
260         }
261 #if (GTK_MAJOR_VERSION < 2)
262         /* translate it back from row index to index in procedures array */
263     selection=GPOINTER_TO_INT(gtk_clist_get_row_data(err->table, selection));
264 #endif
265
266         current_filter=gtk_entry_get_text(GTK_ENTRY(main_display_filter_widget));
267
268     /* Some expert data doesn't pass an expert item. Without this we cannot create a filter */
269     /* But allow for searching of internet for error string */
270     if (action != 6 && action != 7) {
271         if (err->procedures[selection].fvalue_value==NULL) {
272             if (action != 2 && action != 3 && action != 4) {
273                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Wireshark cannot create a filter on this item - %s, try using find instead.", err->procedures[selection].entries[2]);
274                 return;
275             }
276         }
277         switch(type){
278         case 0:
279                 /* selected */
280             /* if no expert item was passed */
281             if (err->procedures[selection].fvalue_value==NULL) {
282                 g_snprintf(str, 255, "%s", err->procedures[selection].entries[2]);
283             }
284             else
285             {
286                 /* expert item exists. Use it. */
287                 g_snprintf(str, 255, "%s", err->procedures[selection].fvalue_value);
288             }
289                 break;
290         case 1:
291                 /* not selected */
292             /* if no expert item was passed */
293             if (err->procedures[selection].fvalue_value==NULL) {
294                 g_snprintf(str, 255, "!%s", err->procedures[selection].entries[2]);
295             }
296             else
297             {
298                 /* expert item exists. Use it. */
299                 g_snprintf(str, 255, "!(%s)", err->procedures[selection].fvalue_value);
300             }
301                 break;
302             /* the remaining cases will only exist if the expert item exists so no need to check */
303         case 2:
304                 /* and selected */
305                 g_snprintf(str, 255, "(%s) && (%s)", current_filter, err->procedures[selection].fvalue_value);
306                 break;
307         case 3:
308                 /* or selected */
309                 g_snprintf(str, 255, "(%s) || (%s)", current_filter, err->procedures[selection].fvalue_value);
310                 break;
311         case 4:
312                 /* and not selected */
313                 g_snprintf(str, 255, "(%s) && !(%s)", current_filter, err->procedures[selection].fvalue_value);
314                 break;
315         case 5:
316                 /* or not selected */
317                 g_snprintf(str, 255, "(%s) || !(%s)", current_filter, err->procedures[selection].fvalue_value);
318                 break;
319         default:
320             simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Can't find menu type - %u", type);
321         }
322     }
323
324         switch(action){
325         case 0:
326                 /* match */
327                 main_filter_packets(&cfile, str, FALSE);
328         break;
329         case 1:
330                 /* prepare */
331         gtk_entry_set_text(GTK_ENTRY(main_display_filter_widget), str);
332                 break;
333         case 2:
334                 /* find frame */
335         /* When trying to perform a find without expert item, we must pass
336          * the expert string to the find window. The user might need to modify
337          * the string and click on the text search to locate the packet in question.
338          * So regardless of the type we will just bring up the find window and allow
339          * the user to modify the search criteria and options.
340          */
341             find_frame_with_filter(str);
342                 break;
343         case 3:
344                 /* find next */
345         /* In the case of find next, if there was no expert item, then most likely the expert
346          * string was modified to locate the text inside the message. So we can't just perform
347          * a find with the expert string or we will not really be performing a find next.
348          * In an effort to allow the user to modify the string and/or continue searching, we
349          * will just present the user with the find window again with the default expert string.
350          * A better aproach would be to attempt in capturing the last find string and utilize this 
351          * with a find next/previous. Also a better approach might be to just send a <Ctl-N> keystroke.
352          */
353         if (err->procedures[selection].fvalue_value==NULL) {
354             find_frame_with_filter(str);
355         }
356         else
357         { 
358             /* We have an expert item so just continue search without find dialog. */
359                     find_previous_next_frame_with_filter(str, FALSE);
360         }
361                 break;
362         case 4:
363                 /* find previous */
364         /* In the case of find previous, if there was no expert item, then most likely the expert
365          * string was modified to locate the text inside the message. So we can't just perform
366          * a find with the expert string or we will not really be performing a find previous.
367          * In an effort to allow the user to modify the string and/or continue searching, we
368          * will just present the user with the find window again with the default expert string.
369          * A better aproach would be to attempt in capturing the last find string and utilize this 
370          * with a find next/previous. Also a better approach might be to just send a <Ctl-B> keystroke.
371          */
372         if (err->procedures[selection].fvalue_value==NULL) {
373             find_frame_with_filter(str);
374         }
375         else
376         { 
377             /* We have an expert item so just continue search without find dialog. */
378                     find_previous_next_frame_with_filter(str, TRUE);
379         }
380                 break;
381         case 5:
382                 /* colorize procedure */
383                 color_display_with_filter(str);
384                 break;
385         case 6:
386                 /* Lookup expert string on internet. Default search via www.google.com */
387                 g_snprintf(str, 255, "http://www.google.com/search?hl=en&q=%s+'%s'", err->procedures[selection].entries[1], err->procedures[selection].entries[2]);
388         browser_open_url(str);
389                 break;
390 #if (GTK_MAJOR_VERSION < 2)
391     case 7:
392         /* Goto the first occurance (packet) in the trace */
393         cf_goto_frame(&cfile, err->procedures[selection].packet_num);
394         break;
395 #endif
396     default:
397         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Can't find menu action - %u", action);
398         }
399
400 }
401
402 static gint
403 error_show_popup_menu_cb(void *widg _U_, GdkEvent *event, error_equiv_table *err)
404 {
405         GdkEventButton *bevent = (GdkEventButton *)event;
406
407         if(event->type==GDK_BUTTON_PRESS && bevent->button==3){
408                 gtk_menu_popup(GTK_MENU(err->menu), NULL, NULL, NULL, NULL, 
409                         bevent->button, bevent->time);
410         }
411
412         return FALSE;
413 }
414
415 static GtkItemFactoryEntry error_list_menu_items[] =
416 {
417         /* Match */
418         ITEM_FACTORY_ENTRY("/Apply as Filter", NULL, NULL, 0, "<Branch>", NULL),
419         ITEM_FACTORY_ENTRY("/Apply as Filter/Selected", NULL,
420                 error_select_filter_cb, 0*256+0, NULL, NULL),
421         ITEM_FACTORY_ENTRY("/Apply as Filter/... not Selected", NULL,
422                 error_select_filter_cb, 0*256+1, NULL, NULL),
423         ITEM_FACTORY_ENTRY("/Apply as Filter/.. and Selected", NULL,
424                 error_select_filter_cb, 0*256+2, NULL, NULL),
425         ITEM_FACTORY_ENTRY("/Apply as Filter/... or Selected", NULL,
426                 error_select_filter_cb, 0*256+3, NULL, NULL),
427         ITEM_FACTORY_ENTRY("/Apply as Filter/... and not Selected", NULL,
428                 error_select_filter_cb, 0*256+4, NULL, NULL),
429         ITEM_FACTORY_ENTRY("/Apply as Filter/... or not Selected", NULL,
430                 error_select_filter_cb, 0*256+5, NULL, NULL),
431
432         /* Prepare */
433         ITEM_FACTORY_ENTRY("/Prepare a Filter", NULL, NULL, 0, "<Branch>", NULL),
434         ITEM_FACTORY_ENTRY("/Prepare a Filter/Selected", NULL,
435                 error_select_filter_cb, 1*256+0, NULL, NULL),
436         ITEM_FACTORY_ENTRY("/Prepare a Filter/Not Selected", NULL,
437                 error_select_filter_cb, 1*256+1, NULL, NULL),
438         ITEM_FACTORY_ENTRY("/Prepare a Filter/... and Selected", NULL,
439                 error_select_filter_cb, 1*256+2, NULL, NULL),
440         ITEM_FACTORY_ENTRY("/Prepare a Filter/... or Selected", NULL,
441                 error_select_filter_cb, 1*256+3, NULL, NULL),
442         ITEM_FACTORY_ENTRY("/Prepare a Filter/... and not Selected", NULL,
443                 error_select_filter_cb, 1*256+4, NULL, NULL),
444         ITEM_FACTORY_ENTRY("/Prepare a Filter/... or not Selected", NULL,
445                 error_select_filter_cb, 1*256+5, NULL, NULL),
446
447         /* Find Frame */
448         ITEM_FACTORY_ENTRY("/Find Frame", NULL, NULL, 0, "<Branch>", NULL),
449         ITEM_FACTORY_ENTRY("/Find Frame/Find Frame", NULL, NULL, 0, "<Branch>", NULL),
450         ITEM_FACTORY_ENTRY("/Find Frame/Find Frame/Selected", NULL,
451                 error_select_filter_cb, 2*256+0, NULL, NULL),
452         ITEM_FACTORY_ENTRY("/Find Frame/Find Frame/Not Selected", NULL,
453                 error_select_filter_cb, 2*256+1, NULL, NULL),
454         /* Find Next */
455         ITEM_FACTORY_ENTRY("/Find Frame/Find Next", NULL, NULL, 0, "<Branch>", NULL),
456         ITEM_FACTORY_ENTRY("/Find Frame/Find Next/Selected", NULL,
457                 error_select_filter_cb, 3*256+0, NULL, NULL),
458         ITEM_FACTORY_ENTRY("/Find Frame/Find Next/Not Selected", NULL,
459                 error_select_filter_cb, 3*256+1, NULL, NULL),
460
461         /* Find Previous */
462         ITEM_FACTORY_ENTRY("/Find Frame/Find Previous", NULL, NULL, 0, "<Branch>", NULL),
463         ITEM_FACTORY_ENTRY("/Find Frame/Find Previous/Selected", NULL,
464                 error_select_filter_cb, 4*256+0, NULL, NULL),
465         ITEM_FACTORY_ENTRY("/Find Frame/Find Previous/Not Selected", NULL,
466                 error_select_filter_cb, 4*256+1, NULL, NULL),
467
468         /* Colorize Procedure */
469         ITEM_FACTORY_ENTRY("/Colorize Procedure", NULL, NULL, 0, "<Branch>", NULL),
470         ITEM_FACTORY_ENTRY("/Colorize Procedure/Selected", NULL,
471                 error_select_filter_cb, 5*256+0, NULL, NULL),
472         ITEM_FACTORY_ENTRY("/Colorize Procedure/Not Selected", NULL,
473                 error_select_filter_cb, 5*256+1, NULL, NULL),
474
475         /* Search Internet */
476         ITEM_FACTORY_ENTRY("/Internet Search for Info Text", NULL,
477                 error_select_filter_cb, 6*256+0, NULL, NULL),
478 #if (GTK_MAJOR_VERSION < 2)
479         /* Go to first packet matching this entry */
480         ITEM_FACTORY_ENTRY("/Goto First Occurrence", NULL,
481                 error_select_filter_cb, 7*256+0, NULL, NULL),
482 #endif
483 };
484
485 #if (GTK_MAJOR_VERSION >= 2)
486 static void
487 expert_goto_pkt_cb (GtkTreeSelection *selection, gpointer data _U_)
488 {
489         GtkTreeIter iter;
490         GtkTreeModel *model;
491         gchar *pkt;
492         gchar *grp;
493
494         if (gtk_tree_selection_get_selected (selection, &model, &iter))
495         {
496                 gtk_tree_model_get (model, &iter, PROTOCOL_COLUMN, &pkt, -1);
497                 gtk_tree_model_get (model, &iter, GROUP_COLUMN, &grp, -1);
498
499                 if (strcmp(grp, "Packet:")==0) {
500                     cf_goto_frame(&cfile, atoi(pkt));
501                 }
502                 g_free (pkt);
503                 g_free (grp);
504         }
505 }
506 #endif
507
508 static void
509 error_create_popup_menu(error_equiv_table *err)
510 {
511         GtkItemFactory *item_factory;
512
513
514 #if (GTK_MAJOR_VERSION >= 2)
515     err->select = gtk_tree_view_get_selection (GTK_TREE_VIEW (err->tree_view));
516     gtk_tree_selection_set_mode (err->select, GTK_SELECTION_SINGLE);
517     g_signal_connect (G_OBJECT (err->select), "changed",
518                   G_CALLBACK (expert_goto_pkt_cb),
519                   err);
520 #endif
521         item_factory = gtk_item_factory_new(GTK_TYPE_MENU, "<main>", NULL);
522
523         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);
524
525         err->menu = gtk_item_factory_get_widget(item_factory, "<main>");
526 #if (GTK_MAJOR_VERSION >= 2)
527         SIGNAL_CONNECT(err->tree_view, "button_press_event", error_show_popup_menu_cb, err);
528 #else
529         SIGNAL_CONNECT(err->table, "button_press_event", error_show_popup_menu_cb, err);
530 #endif
531 }
532
533 void
534 init_error_table(error_equiv_table *err, guint16 num_procs, GtkWidget *vbox)
535 {
536     guint16 i, j;
537 #if (GTK_MAJOR_VERSION < 2)
538     column_arrows *col_arrows;
539     GdkBitmap *ascend_bm, *descend_bm;
540     GdkPixmap *ascend_pm, *descend_pm;
541     GtkStyle *win_style;
542     GtkWidget *column_lb;
543
544     const char *default_titles[] = { "Group", "Protocol", "Summary", "Count"};
545
546     err->scrolled_window=scrolled_window_new(NULL, NULL);
547     gtk_box_pack_start(GTK_BOX(vbox), err->scrolled_window, TRUE, TRUE, 0);
548
549     err->table=(GtkCList *)gtk_clist_new(4);
550
551     gtk_widget_show(GTK_WIDGET(err->table));
552     gtk_widget_show(err->scrolled_window);
553
554     col_arrows = (column_arrows *) g_malloc(sizeof(column_arrows) * 4);
555     win_style = gtk_widget_get_style(err->scrolled_window);
556     ascend_pm = gdk_pixmap_create_from_xpm_d(err->scrolled_window->window,
557             &ascend_bm,
558             &win_style->bg[GTK_STATE_NORMAL],
559             (gchar **)clist_ascend_xpm);
560     descend_pm = gdk_pixmap_create_from_xpm_d(err->scrolled_window->window,
561             &descend_bm,
562             &win_style->bg[GTK_STATE_NORMAL],
563             (gchar **)clist_descend_xpm);
564     for (i = 0; i < 4; i++) {
565         col_arrows[i].table = gtk_table_new(2, 2, FALSE);
566         gtk_table_set_col_spacings(GTK_TABLE(col_arrows[i].table), 5);
567         column_lb = gtk_label_new(default_titles[i]);
568         gtk_table_attach(GTK_TABLE(col_arrows[i].table), column_lb, 0, 1, 0, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
569         gtk_widget_show(column_lb);
570
571         col_arrows[i].ascend_pm = gtk_pixmap_new(ascend_pm, ascend_bm);
572         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].ascend_pm, 1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
573         col_arrows[i].descend_pm = gtk_pixmap_new(descend_pm, descend_bm);
574         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].descend_pm, 1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
575         if (i == 3) {
576             gtk_widget_show(col_arrows[i].descend_pm);
577         }
578         gtk_clist_set_column_widget(GTK_CLIST(err->table), i, col_arrows[i].table);
579         gtk_widget_show(col_arrows[i].table);
580     }
581     gtk_clist_column_titles_show(GTK_CLIST(err->table));
582
583     gtk_clist_set_compare_func(err->table, error_sort_column);
584     gtk_clist_set_sort_column(err->table, 3);
585     gtk_clist_set_sort_type(err->table, GTK_SORT_DESCENDING);
586
587
588     /*XXX instead of this we should probably have some code to
589         dynamically adjust the width of the columns */
590     gtk_clist_set_column_width(err->table, 0, 75);
591     gtk_clist_set_column_width(err->table, 1, 75);
592     gtk_clist_set_column_width(err->table, 2, 400);
593     gtk_clist_set_column_width(err->table, 3, 50);
594
595
596     gtk_clist_set_shadow_type(err->table, GTK_SHADOW_IN);
597     gtk_clist_column_titles_show(err->table);
598     gtk_container_add(GTK_CONTAINER(err->scrolled_window), (GtkWidget *)err->table);
599
600     SIGNAL_CONNECT(err->table, "click-column", error_click_column_cb, col_arrows);
601
602     gtk_widget_show(GTK_WIDGET(err->table));
603 #else
604     GtkTreeStore *store;
605     GtkWidget *tree;
606     GtkTreeViewColumn *column;
607     GtkCellRenderer *renderer;
608     GtkTreeSortable *sortable;
609
610     /* Create the store */
611     store = gtk_tree_store_new (4,       /* Total number of columns */
612                                G_TYPE_STRING,   /* Group              */
613                                G_TYPE_STRING,   /* Protocol           */
614                                G_TYPE_STRING,   /* Summary            */
615                                G_TYPE_INT);     /* Count              */
616
617     /* Create a view */
618     tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
619     err->tree_view = GTK_TREE_VIEW(tree);
620     sortable = GTK_TREE_SORTABLE(store);
621
622     /* Setup the sortable columns */
623     gtk_tree_sortable_set_sort_func(sortable, SORT_ALPHABETICAL, sort_iter_compare_func, GINT_TO_POINTER(SORT_ALPHABETICAL), NULL);
624     gtk_tree_sortable_set_sort_column_id(sortable, SORT_ALPHABETICAL, GTK_SORT_ASCENDING);
625     gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW (tree), FALSE);
626
627     /* The view now holds a reference.  We can get rid of our own reference */
628     g_object_unref (G_OBJECT (store));
629
630     /* Create a cell render */
631     renderer = gtk_cell_renderer_text_new ();
632
633     /* Create the first column, associating the "text" attribute of the
634      * cell_renderer to the first column of the model */
635     column = gtk_tree_view_column_new_with_attributes ("Group", renderer, "text", GROUP_COLUMN, NULL);
636     gtk_tree_view_column_set_sort_column_id(column, 0);
637     gtk_tree_view_column_set_resizable(column, TRUE);
638     /* Add the column to the view. */
639     gtk_tree_view_append_column (GTK_TREE_VIEW (err->tree_view), column);
640  
641     /* Second column.. Protocol. */
642     renderer = gtk_cell_renderer_text_new ();
643     column = gtk_tree_view_column_new_with_attributes ("Protocol", renderer, "text", PROTOCOL_COLUMN, NULL);
644     gtk_tree_view_column_set_sort_column_id(column, 1);
645     gtk_tree_view_column_set_resizable(column, TRUE);
646     gtk_tree_view_append_column (GTK_TREE_VIEW (err->tree_view), column);
647  
648     /* Third column.. Summary. */
649     renderer = gtk_cell_renderer_text_new ();
650     column = gtk_tree_view_column_new_with_attributes ("Summary", renderer, "text", SUMMARY_COLUMN, NULL);
651     gtk_tree_view_column_set_sort_column_id(column, 2);
652     gtk_tree_view_column_set_resizable(column, TRUE);
653     gtk_tree_view_append_column (GTK_TREE_VIEW (err->tree_view), column);
654  
655     /* Last column.. Count. */
656     column = gtk_tree_view_column_new_with_attributes ("Count", renderer, "text", COUNT_COLUMN, NULL);
657     gtk_tree_view_column_set_sort_column_id(column, 3);
658     gtk_tree_view_column_set_resizable(column, TRUE);
659     gtk_tree_view_append_column (GTK_TREE_VIEW (err->tree_view), column);
660  
661     err->scrolled_window=scrolled_window_new(NULL, NULL);
662
663     gtk_container_add(GTK_CONTAINER(err->scrolled_window), GTK_WIDGET (err->tree_view));
664
665     gtk_box_pack_start(GTK_BOX(vbox), err->scrolled_window, TRUE, TRUE, 0);
666
667     gtk_tree_view_set_search_column (err->tree_view, SUMMARY_COLUMN); /* Allow searching the summary */
668     gtk_tree_view_set_reorderable (err->tree_view, TRUE);   /* Allow user to reorder data with drag n drop */
669     
670     /* Now enable the sorting of each column */
671     gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(err->tree_view), TRUE);
672     gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW(err->tree_view), TRUE);
673 #endif
674
675     gtk_widget_show(err->scrolled_window);
676
677     err->num_procs=num_procs;
678     err->procedures=g_malloc(sizeof(error_procedure_t)*(num_procs+1));
679     for(i=0;i<num_procs;i++){
680         for(j=0;j<3;j++){
681             err->procedures[i].entries[j]=NULL; /* reset all values */
682         }
683     }
684
685     /* create popup menu for this table */
686     error_create_popup_menu(err);
687 }
688
689 void
690 init_error_table_row(error_equiv_table *err, const expert_info_t *expert_data)
691 {
692     guint16 old_num_procs=err->num_procs;
693     guint16 j;
694     gint row=0;
695
696 #if (GTK_MAJOR_VERSION >= 2)
697     GtkTreeStore *store;
698 #endif
699
700     /* we have discovered a new procedure. Extend the table accordingly */
701     row = find_summary_data(err, expert_data);
702     if(row==-1){
703         /* First time we have seen this event so initialize memory table */
704 #if (GTK_MAJOR_VERSION < 2)
705         row = 0;
706         old_num_procs++;
707
708         err->procedures=g_realloc(err->procedures, (sizeof(error_procedure_t)*(old_num_procs+1)));
709         err->procedures[err->num_procs].count=0;
710         for(j=0;j<4;j++)
711         {
712             err->procedures[err->num_procs].entries[j]=NULL;
713         }
714         err->procedures[err->num_procs].packet_num = (guint32)expert_data->packet_num;                        /* First packet num */
715         err->procedures[err->num_procs].entries[0]=(char *)g_strdup(val_to_str(expert_data->group, expert_group_vals,"Unknown group (%u)"));   /* Group */
716         err->procedures[err->num_procs].entries[1]=(char *)g_strdup(expert_data->protocol);    /* Protocol */
717         err->procedures[err->num_procs].entries[2]=(char *)g_strdup(expert_data->summary);     /* Summary */
718         err->procedures[err->num_procs].entries[3]=(char *)g_strdup_printf("%d", err->procedures[row].count);     /* Count */
719         err->procedures[err->num_procs].fvalue_value = NULL;
720     }
721     /* Store the updated count of events */
722     err->num_procs = old_num_procs;
723 #else
724         row = old_num_procs; /* Number of expert events since this is a new event */
725         err->procedures=g_realloc(err->procedures, (sizeof(error_procedure_t)*(old_num_procs+1)));
726         err->procedures[row].count=0; /* count of events for this item */
727         err->procedures[row].fvalue_value = NULL; /* Filter string value */
728         for(j=0;j<4;j++){
729             err->procedures[row].entries[j]=NULL;
730         }
731         
732         /* Create the item in our memory table */
733         err->procedures[row].entries[0]=(char *)g_strdup(val_to_str(expert_data->group, expert_group_vals,"Unknown group (%u)"));  /* Group */
734         err->procedures[row].entries[1]=(char *)g_strdup(expert_data->protocol);    /* Protocol */
735         err->procedures[row].entries[2]=(char *)g_strdup(expert_data->summary);     /* Summary */
736
737         /* Create a new item in our tree view */
738         store = GTK_TREE_STORE(gtk_tree_view_get_model(err->tree_view)); /* Get store */
739         gtk_tree_store_append (store, &err->procedures[row].iter, NULL);  /* Acquire an iterator */
740         
741         gtk_tree_store_set (store, &err->procedures[row].iter,
742                     GROUP_COLUMN, (char *)g_strdup(val_to_str(expert_data->group, expert_group_vals,"Unknown group (%u)")),
743                     PROTOCOL_COLUMN, (char *)g_strdup(expert_data->protocol),
744                     SUMMARY_COLUMN, (char *)g_strdup(expert_data->summary), -1);
745
746         /* If an expert item was passed then build the filter string */
747         if (expert_data->pitem) {
748             char *filter;
749
750             filter = proto_construct_match_selected_string(expert_data->pitem->finfo, NULL);
751             if (filter != NULL)
752                 err->procedures[row].fvalue_value = g_strdup(filter);
753         }
754         /* Store the updated count of events */
755         err->num_procs = ++old_num_procs;
756     }
757
758     /* Update our memory table with event data */
759     err->procedures[row].count++; /* increment the count of events for this item */
760
761     /* Store the updated count for this event item */
762     err->procedures[row].entries[3]=(char *)g_strdup_printf("%d", err->procedures[row].count);     /* Count */
763
764     /* Update the tree with new count for this event */
765     store = GTK_TREE_STORE(gtk_tree_view_get_model(err->tree_view));
766     gtk_tree_store_set(store, &err->procedures[row].iter, COUNT_COLUMN, err->procedures[row].count, -1);
767 #endif
768 }
769
770 void
771 add_error_table_data(error_equiv_table *err, const expert_info_t *expert_data)
772 {
773         error_procedure_t *errp;
774     gint index;
775 #if (GTK_MAJOR_VERSION < 2)
776     gint row;
777 #else
778     GtkTreeStore    *store;
779     GtkTreeIter      new_iter;
780 #endif
781
782     index = find_summary_data(err,expert_data);
783
784     /* We should never encounter a condition where we cannot find the expert data. If
785      * we do then we will just abort.
786      */
787     if (index == -1) {
788         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Could not find expert data. Aborting");
789         return;
790     }
791         errp=&err->procedures[index];
792
793 #if (GTK_MAJOR_VERSION < 2)
794         if (errp->count==0){
795                 row=gtk_clist_append(err->table, err->procedures[index].entries);
796                 gtk_clist_set_row_data(err->table, row, (gpointer) index);
797         }
798     errp->count++;
799     err->procedures[index].entries[3] = (char *)g_strdup_printf("%d", errp->count);
800 #else
801
802     store = GTK_TREE_STORE(gtk_tree_view_get_model(err->tree_view));
803
804     gtk_tree_store_append(store, &new_iter, &errp->iter);
805
806     gtk_tree_store_set(store, &new_iter,
807                            GROUP_COLUMN, "Packet:",
808                            PROTOCOL_COLUMN, (char *)g_strdup_printf("%d", expert_data->packet_num),
809                            -1);
810 #endif
811 }
812
813
814 #if (GTK_MAJOR_VERSION < 2)
815 void
816 draw_error_table_data(error_equiv_table *err)
817 {
818         int i,j;
819         char *strp;
820
821         for(i=0;i<err->num_procs;i++){
822                 /* ignore procedures with no calls (they don't have CList rows) */
823                 if(err->procedures[i].count==0){
824                         continue;
825                 }
826
827                 j=gtk_clist_find_row_from_data(err->table, (gpointer)i);
828                 strp=g_strdup_printf("%d", err->procedures[i].count);
829                 gtk_clist_set_text(err->table, j, 3, strp);
830                 err->procedures[i].entries[3]=(char *)strp;
831
832
833         }
834         gtk_clist_sort(err->table);
835 }
836 #endif
837
838 void
839 reset_error_table_data(error_equiv_table *err)
840 {
841         guint16 i;
842 #if (GTK_MAJOR_VERSION >= 2)
843     GtkTreeStore    *store;
844 #endif
845
846         for(i=0;i<err->num_procs;i++){
847                 err->procedures[i].entries[0] = NULL;
848                 err->procedures[i].entries[1] = NULL;
849                 err->procedures[i].entries[2] = NULL;
850                 err->procedures[i].entries[3] = NULL;
851 #if (GTK_MAJOR_VERSION < 2)
852         err->procedures[i].packet_num=0;
853 #else
854         err->procedures[i].count=0;
855 #endif
856
857         }
858
859 #if (GTK_MAJOR_VERSION < 2)
860         gtk_clist_clear(err->table);
861 #else
862     store = GTK_TREE_STORE(gtk_tree_view_get_model(err->tree_view));
863     gtk_tree_store_clear(store);
864 #endif
865     err->num_procs = 0;
866 }
867
868 void
869 free_error_table_data(error_equiv_table *err)
870 {
871         guint16 i,j;
872
873         for(i=0;i<err->num_procs;i++){
874                 for(j=0;j<4;j++){
875                         if(err->procedures[i].entries[j]){
876                                 err->procedures[i].entries[j]=NULL;
877                         }
878             err->procedures[i].fvalue_value=NULL;
879
880 #if (GTK_MAJOR_VERSION < 2)
881             err->procedures[i].packet_num=0;
882 #else
883             err->procedures[i].count=0;
884 #endif
885                 }
886         }
887         err->procedures=NULL;
888         err->num_procs=0;
889 }