move tap_dfilter_dlg.h from / to /gtk (and use the callback mechanism in main.c instead)
[obnox/wireshark/wip.git] / gtk / stats_tree_stat.c
1
2 /* stats_tree_stat.c
3  * GTK Tap implementation of stats_tree
4  * 2005, Luis E. G. Ontanon
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  * 
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <string.h>
32 #include <gtk/gtk.h>
33
34 #include <epan/stats_tree_priv.h>
35 #include <epan/report_err.h>
36
37 #include "simple_dialog.h"
38 #include "globals.h"
39 #include "gui_utils.h"
40 #include "dlg_utils.h"
41 #include "../stat_menu.h"
42 #include "tap_dfilter_dlg.h"
43
44 struct _st_node_pres {
45         GtkTreeIter*    iter;
46 };
47
48 struct _tree_cfg_pres {
49         tap_dfilter_dlg* stat_dlg;
50 };
51
52 struct _tree_pres {
53         GString*        text;
54         GtkWidget*      win;
55         GtkTreeStore*   store;
56         GtkWidget*      tree;
57 };
58
59 /* the columns of the tree pane */
60 enum _stat_tree_columns {
61         TITLE_COLUMN,
62         COUNT_COLUMN,
63         RATE_COLUMN,
64         PERCENT_COLUMN,
65         N_COLUMNS
66 };
67
68 /* used for converting numbers */
69 #define NUM_BUF_SIZE  32
70
71 /* creates the gtk representation for a stat_node
72  * node: the node
73  */
74 static void setup_gtk_node_pr(stat_node* node) {
75         GtkTreeIter* parent =  NULL;
76
77         node->pr = g_malloc(sizeof(st_node_pres));
78
79         if (node->st->pr->store) {
80                 node->pr->iter = g_malloc0(sizeof(GtkTreeIter));
81
82                 if ( node->parent && node->parent->pr ) {
83                         parent = node->parent->pr->iter;
84                 }
85                 gtk_tree_store_append (node->st->pr->store, node->pr->iter, parent);
86                 gtk_tree_store_set(node->st->pr->store, node->pr->iter, TITLE_COLUMN, node->name, RATE_COLUMN, "", COUNT_COLUMN, "", -1);
87         }
88 }
89
90
91 static void draw_gtk_node(stat_node* node) {
92         static gchar value[NUM_BUF_SIZE];
93         static gchar rate[NUM_BUF_SIZE];
94         static gchar percent[NUM_BUF_SIZE];
95         stat_node* child;
96         
97         stats_tree_get_strs_from_node(node, value, rate,
98                                       percent);
99         
100         if (node->st->pr->store && node->pr->iter) {
101                 gtk_tree_store_set(node->st->pr->store, node->pr->iter,
102                                                    RATE_COLUMN, rate,
103                                                    COUNT_COLUMN, value,
104                                                    PERCENT_COLUMN, percent,
105                                                    -1);
106         }
107         
108         if (node->children) {
109                 for (child = node->children; child; child = child->next )
110                         draw_gtk_node(child);
111         }
112 }
113
114 static void draw_gtk_tree( void *psp  ) {
115         stats_tree *st = psp;
116         stat_node* child;
117
118         for (child = st->root.children; child; child = child->next ) {
119                 draw_gtk_node(child);
120
121                 if (child->pr->iter && st->pr->store) {
122                         gtk_tree_view_expand_row(GTK_TREE_VIEW(st->pr->tree),
123                                                                  gtk_tree_model_get_path(GTK_TREE_MODEL(st->pr->store),
124                                                                                                                  child->pr->iter),
125                                                                  FALSE);
126                 }
127         }
128
129 }
130
131 void protect_thread_critical_region(void);
132 void unprotect_thread_critical_region(void);
133
134 static void free_gtk_tree(GtkWindow *win _U_, stats_tree *st)
135 {
136         
137         protect_thread_critical_region();
138         remove_tap_listener(st);
139         unprotect_thread_critical_region();
140         
141         if (st->root.pr)
142                 st->root.pr->iter = NULL;
143         
144         st->cfg->in_use = FALSE;
145         stats_tree_free(st);
146         
147 }
148
149 static void clear_node_pr(stat_node* n) {
150         stat_node* c;
151         for (c = n->children; c; c = c->next) {
152                 clear_node_pr(c);
153         }
154         
155         if (n->pr->iter) {
156                 gtk_tree_store_remove(n->st->pr->store, n->pr->iter);
157                 n->pr->iter = NULL;
158         }
159 }
160
161 static void reset_tap(void* p) {
162     stats_tree* st = p;
163         stat_node* c;
164         for (c = st->root.children; c; c = c->next) {
165                 clear_node_pr(c);
166         }
167         
168         st->cfg->init(st);
169 }
170
171 /* initializes the stats_tree window */
172 static void init_gtk_tree(const char* optarg, void *userdata _U_) {
173         guint8* abbr = stats_tree_get_abbr(optarg);
174         stats_tree* st = NULL;
175         stats_tree_cfg* cfg = NULL;
176         tree_pres* pr = g_malloc(sizeof(tree_pres));
177         gchar* title = NULL;
178         gchar* window_name = NULL;
179         GString* error_string;
180         GtkWidget *scr_win;
181         guint init_strlen;
182         GtkWidget *main_vb, *bbox, *bt_close;
183         GtkTreeViewColumn* column;
184         GtkCellRenderer* renderer;
185         
186         if (abbr) {
187                 cfg = stats_tree_get_cfg_by_abbr(abbr);
188                 
189                 if (cfg && cfg->in_use) {
190                         /* XXX: ! */
191                         report_failure("cannot open more than one tree of the same type at once");
192                         return;
193                 }
194                 
195                 if (cfg != NULL) {
196                         init_strlen = strlen(cfg->pr->stat_dlg->init_string);
197                         
198                         if (strncmp (optarg, cfg->pr->stat_dlg->init_string, init_strlen) == 0){
199                                 if (init_strlen == strlen(optarg)) {
200                                         st = stats_tree_new(cfg,pr,NULL);
201                                 } else { 
202                                         st = stats_tree_new(cfg,pr,(char*)optarg+init_strlen+1);
203                                 }
204                                 
205                         } else {
206                                 st = stats_tree_new(cfg,pr,NULL);
207                         }
208                 } else {
209                         report_failure("no such stats_tree (%s) in stats_tree registry",abbr);
210                         g_free(abbr);
211                         return;
212                 }
213                 g_free(abbr);
214                 
215         } else {
216                 report_failure("could not obtain stats_tree abbr from optarg");
217                 g_free(pr);
218                 return;
219         }
220
221         cfg->in_use = TRUE;
222         
223         window_name = g_strdup_printf("%s Stats Tree", cfg->name);
224         
225         st->pr->win = window_new_with_geom(GTK_WINDOW_TOPLEVEL,window_name,window_name);
226         gtk_window_set_default_size(GTK_WINDOW(st->pr->win), 400, 400);
227         g_free(window_name);
228     
229         if(st->filter){
230                 title=g_strdup_printf("%s with filter: %s",cfg->name,st->filter);
231         } else {
232                 st->filter=NULL;
233                 title=g_strdup_printf("%s", cfg->name);
234         }
235         
236         gtk_window_set_title(GTK_WINDOW(st->pr->win), title);
237         g_free(title);
238
239         main_vb = gtk_vbox_new(FALSE, 3);
240         gtk_container_border_width(GTK_CONTAINER(main_vb), 12);
241         gtk_container_add(GTK_CONTAINER(st->pr->win), main_vb);
242
243         scr_win = scrolled_window_new(NULL, NULL);
244
245         st->pr->store = gtk_tree_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING,
246                                                                         G_TYPE_STRING, G_TYPE_STRING);
247         
248         st->pr->tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (st->pr->store));
249         g_object_unref(G_OBJECT(st->pr->store));
250
251         gtk_container_add( GTK_CONTAINER(scr_win), st->pr->tree);
252         
253         /* the columns */
254         renderer = gtk_cell_renderer_text_new ();
255         column = gtk_tree_view_column_new_with_attributes ("Topic / Item", renderer,
256                                                                                                            "text", TITLE_COLUMN,
257                                                                                                            NULL);
258         gtk_tree_view_column_set_resizable (column,TRUE);
259         gtk_tree_view_column_set_sizing(column,GTK_TREE_VIEW_COLUMN_AUTOSIZE);
260         gtk_tree_view_append_column (GTK_TREE_VIEW (st->pr->tree), column);
261         
262         renderer = gtk_cell_renderer_text_new ();
263         column = gtk_tree_view_column_new_with_attributes ("Count", renderer,
264                                                                                                            "text", COUNT_COLUMN,
265                                                                                                            NULL);
266         
267         gtk_tree_view_column_set_resizable (column,TRUE);
268         gtk_tree_view_column_set_sizing(column,GTK_TREE_VIEW_COLUMN_AUTOSIZE);
269         gtk_tree_view_append_column (GTK_TREE_VIEW (st->pr->tree), column);
270         
271         renderer = gtk_cell_renderer_text_new ();
272         column = gtk_tree_view_column_new_with_attributes ("Rate", renderer,
273                                                                                                            "text", RATE_COLUMN,
274                                                                                                            NULL);
275         gtk_tree_view_column_set_resizable (column,TRUE);
276         gtk_tree_view_column_set_sizing(column,GTK_TREE_VIEW_COLUMN_AUTOSIZE);
277         gtk_tree_view_append_column (GTK_TREE_VIEW (st->pr->tree), column);
278         
279         renderer = gtk_cell_renderer_text_new ();
280         column = gtk_tree_view_column_new_with_attributes ("Percent", renderer,
281                                                                                                            "text", PERCENT_COLUMN,
282                                                                                                            NULL);
283         gtk_tree_view_column_set_resizable(column,TRUE);
284         gtk_tree_view_column_set_sizing(column,GTK_TREE_VIEW_COLUMN_AUTOSIZE);
285         gtk_tree_view_append_column (GTK_TREE_VIEW (st->pr->tree), column);
286
287         gtk_container_add( GTK_CONTAINER(main_vb), scr_win);
288         
289         error_string = register_tap_listener( cfg->tapname,
290                                               st,
291                                               st->filter,
292                                               reset_tap,
293                                               stats_tree_packet,
294                                               draw_gtk_tree);
295         
296         if (error_string) {
297                 /* error, we failed to attach to the tap. clean up */
298                 simple_dialog( ESD_TYPE_ERROR, ESD_BTN_OK, error_string->str );
299                 /* destroy_stat_tree_window(st); */
300                 report_failure("stats_tree for: %s failed to attach to the tap: %s",cfg->name,error_string->str);
301                 g_string_free(error_string, TRUE);
302         }
303                 
304         /* Button row. */
305         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
306         gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
307
308         bt_close = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
309         window_set_cancel_button(st->pr->win, bt_close, window_cancel_button_cb);
310
311         g_signal_connect(GTK_WINDOW(st->pr->win), "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
312         g_signal_connect(GTK_WINDOW(st->pr->win), "destroy", G_CALLBACK(free_gtk_tree), st);
313         
314         gtk_widget_show_all(st->pr->win);
315         window_present(st->pr->win);
316         
317         cf_retap_packets(&cfile, FALSE);
318 }
319
320
321 static void register_gtk_stats_tree_tap (gpointer k _U_, gpointer v, gpointer p _U_) {
322         stats_tree_cfg* cfg = v;
323
324         cfg->pr = g_malloc(sizeof(tree_pres));
325         
326         cfg->pr->stat_dlg = g_malloc(sizeof(tap_dfilter_dlg));
327         
328         cfg->pr->stat_dlg->win_title = g_strdup_printf("%s Stats Tree",cfg->name);
329         cfg->pr->stat_dlg->init_string = g_strdup_printf("%s,tree",cfg->abbr);
330         cfg->pr->stat_dlg->tap_init_cb = init_gtk_tree;
331         cfg->pr->stat_dlg->index = -1;
332         
333         register_dfilter_stat(cfg->pr->stat_dlg, cfg->name,
334             REGISTER_STAT_GROUP_NONE);
335 }
336
337 static void free_tree_presentation(stats_tree* st) {
338         g_free(st->pr);
339 }
340
341 void
342 register_tap_listener_stats_tree_stat(void)
343 {
344         
345         stats_tree_presentation(register_gtk_stats_tree_tap,
346                                                         setup_gtk_node_pr,
347                             NULL,
348                                                         NULL,
349                                                         NULL,
350                             NULL,
351                             free_tree_presentation,
352                             NULL,
353                             NULL,
354                             NULL);
355 }