2 * GTK Tap implementation of stats_tree
3 * 2005, Luis E. G. Ontanon
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
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.
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.
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.
33 #include <epan/stats_tree_priv.h>
34 #include <epan/report_err.h>
36 #include "../simple_dialog.h"
37 #include "../globals.h"
38 #include "../stat_menu.h"
40 #include "gtk/gui_utils.h"
41 #include "gtk/dlg_utils.h"
42 #include "gtk/tap_param_dlg.h"
46 struct _st_node_pres {
50 struct _tree_cfg_pres {
51 tap_param_dlg* stat_dlg;
61 /* the columns of the tree pane */
62 enum _stat_tree_columns {
70 /* used for converting numbers */
71 #define NUM_BUF_SIZE 32
73 /* creates the gtk representation for a stat_node
77 setup_gtk_node_pr(stat_node* node)
79 GtkTreeIter* parent = NULL;
81 node->pr = g_malloc(sizeof(st_node_pres));
83 if (node->st->pr->store) {
84 node->pr->iter = g_malloc0(sizeof(GtkTreeIter));
86 if ( node->parent && node->parent->pr ) {
87 parent = node->parent->pr->iter;
89 gtk_tree_store_append (node->st->pr->store, node->pr->iter, parent);
90 gtk_tree_store_set(node->st->pr->store, node->pr->iter,
91 TITLE_COLUMN, node->name, RATE_COLUMN, "", COUNT_COLUMN, "", -1);
97 draw_gtk_node(stat_node* node)
99 static gchar value[NUM_BUF_SIZE];
100 static gchar rate[NUM_BUF_SIZE];
101 static gchar percent[NUM_BUF_SIZE];
104 stats_tree_get_strs_from_node(node, value, rate,
107 if (node->st->pr->store && node->pr->iter) {
108 gtk_tree_store_set(node->st->pr->store, node->pr->iter,
111 PERCENT_COLUMN, percent,
115 if (node->children) {
116 for (child = node->children; child; child = child->next )
117 draw_gtk_node(child);
122 draw_gtk_tree(void *psp)
124 stats_tree *st = psp;
127 for (child = st->root.children; child; child = child->next ) {
128 draw_gtk_node(child);
130 if (child->pr->iter && st->pr->store) {
131 gtk_tree_view_expand_row(GTK_TREE_VIEW(st->pr->tree),
132 gtk_tree_model_get_path(GTK_TREE_MODEL(st->pr->store),
141 free_gtk_tree(GtkWindow *win _U_, stats_tree *st)
144 protect_thread_critical_region();
145 remove_tap_listener(st);
146 unprotect_thread_critical_region();
149 st->root.pr->iter = NULL;
151 st->cfg->in_use = FALSE;
157 clear_node_pr(stat_node* n)
160 for (c = n->children; c; c = c->next) {
165 gtk_tree_store_remove(n->st->pr->store, n->pr->iter);
175 for (c = st->root.children; c; c = c->next) {
182 /* initializes the stats_tree window */
184 init_gtk_tree(const char* optarg, void *userdata _U_)
186 gchar *abbr = stats_tree_get_abbr(optarg);
187 stats_tree* st = NULL;
188 stats_tree_cfg* cfg = NULL;
189 tree_pres* pr = g_malloc(sizeof(tree_pres));
191 gchar* window_name = NULL;
192 GString* error_string;
195 GtkWidget *main_vb, *bbox, *bt_close;
196 GtkTreeViewColumn* column;
197 GtkCellRenderer* renderer;
200 cfg = stats_tree_get_cfg_by_abbr(abbr);
202 if (cfg && cfg->in_use) {
204 report_failure("cannot open more than one tree of the same type at once");
209 init_strlen = strlen(cfg->pr->stat_dlg->init_string);
211 if (strncmp (optarg, cfg->pr->stat_dlg->init_string, init_strlen) == 0){
212 if (init_strlen == strlen(optarg)) {
213 st = stats_tree_new(cfg,pr,NULL);
215 st = stats_tree_new(cfg,pr,(char*)optarg+init_strlen+1);
219 st = stats_tree_new(cfg,pr,NULL);
222 report_failure("no such stats_tree (%s) in stats_tree registry",abbr);
229 report_failure("could not obtain stats_tree abbr from optarg");
236 window_name = g_strdup_printf("%s Stats Tree", cfg->name);
238 st->pr->win = window_new_with_geom(GTK_WINDOW_TOPLEVEL,window_name,window_name);
239 gtk_window_set_default_size(GTK_WINDOW(st->pr->win), 400, 400);
243 title=g_strdup_printf("%s with filter: %s",cfg->name,st->filter);
246 title=g_strdup_printf("%s", cfg->name);
249 gtk_window_set_title(GTK_WINDOW(st->pr->win), title);
252 main_vb = gtk_vbox_new(FALSE, 3);
253 gtk_container_set_border_width(GTK_CONTAINER(main_vb), 12);
254 gtk_container_add(GTK_CONTAINER(st->pr->win), main_vb);
256 scr_win = scrolled_window_new(NULL, NULL);
258 st->pr->store = gtk_tree_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING,
259 G_TYPE_STRING, G_TYPE_STRING);
261 st->pr->tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (st->pr->store));
262 g_object_unref(G_OBJECT(st->pr->store));
264 gtk_container_add( GTK_CONTAINER(scr_win), st->pr->tree);
267 renderer = gtk_cell_renderer_text_new ();
268 column = gtk_tree_view_column_new_with_attributes ("Topic / Item", renderer,
269 "text", TITLE_COLUMN,
271 gtk_tree_view_column_set_resizable (column,TRUE);
272 gtk_tree_view_column_set_sizing(column,GTK_TREE_VIEW_COLUMN_AUTOSIZE);
273 gtk_tree_view_append_column (GTK_TREE_VIEW (st->pr->tree), column);
275 renderer = gtk_cell_renderer_text_new ();
276 column = gtk_tree_view_column_new_with_attributes ("Count", renderer,
277 "text", COUNT_COLUMN,
280 gtk_tree_view_column_set_resizable (column,TRUE);
281 gtk_tree_view_column_set_sizing(column,GTK_TREE_VIEW_COLUMN_AUTOSIZE);
282 gtk_tree_view_append_column (GTK_TREE_VIEW (st->pr->tree), column);
284 renderer = gtk_cell_renderer_text_new ();
285 column = gtk_tree_view_column_new_with_attributes ("Rate (ms)", renderer,
288 gtk_tree_view_column_set_resizable (column,TRUE);
289 gtk_tree_view_column_set_sizing(column,GTK_TREE_VIEW_COLUMN_AUTOSIZE);
290 gtk_tree_view_append_column (GTK_TREE_VIEW (st->pr->tree), column);
292 renderer = gtk_cell_renderer_text_new ();
293 column = gtk_tree_view_column_new_with_attributes ("Percent", renderer,
294 "text", PERCENT_COLUMN,
296 gtk_tree_view_column_set_resizable(column,TRUE);
297 gtk_tree_view_column_set_sizing(column,GTK_TREE_VIEW_COLUMN_AUTOSIZE);
298 gtk_tree_view_append_column (GTK_TREE_VIEW (st->pr->tree), column);
300 gtk_container_add( GTK_CONTAINER(main_vb), scr_win);
302 error_string = register_tap_listener( cfg->tapname,
311 /* error, we failed to attach to the tap. clean up */
312 /* destroy_stat_tree_window(st); */
313 report_failure("stats_tree for: %s failed to attach to the tap: %s",cfg->name,error_string->str);
314 g_string_free(error_string, TRUE);
318 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
319 gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
321 bt_close = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
322 window_set_cancel_button(st->pr->win, bt_close, window_cancel_button_cb);
324 g_signal_connect(GTK_WINDOW(st->pr->win), "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
325 g_signal_connect(GTK_WINDOW(st->pr->win), "destroy", G_CALLBACK(free_gtk_tree), st);
327 gtk_widget_show_all(st->pr->win);
328 window_present(st->pr->win);
330 cf_retap_packets(&cfile);
331 gdk_window_raise(st->pr->win->window);
334 static tap_param tree_stat_params[] = {
335 { PARAM_FILTER, "Filter", NULL }
339 register_gtk_stats_tree_tap (gpointer k _U_, gpointer v, gpointer p _U_)
341 stats_tree_cfg* cfg = v;
343 cfg->pr = g_malloc(sizeof(tree_pres));
345 cfg->pr->stat_dlg = g_malloc(sizeof(tap_param_dlg));
347 cfg->pr->stat_dlg->win_title = g_strdup_printf("%s Stats Tree",cfg->name);
348 cfg->pr->stat_dlg->init_string = g_strdup_printf("%s,tree",cfg->abbr);
349 cfg->pr->stat_dlg->tap_init_cb = init_gtk_tree;
350 cfg->pr->stat_dlg->index = -1;
351 cfg->pr->stat_dlg->nparams = G_N_ELEMENTS(tree_stat_params);
352 cfg->pr->stat_dlg->params = tree_stat_params;
354 register_dfilter_stat(cfg->pr->stat_dlg, cfg->name, cfg->stat_group);
358 free_tree_presentation(stats_tree* st)
364 register_tap_listener_stats_tree_stat(void)
367 stats_tree_presentation(register_gtk_stats_tree_tap,
373 free_tree_presentation,