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