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"
45 #include "gtk/old-gtk-compat.h"
47 struct _st_node_pres {
51 struct _tree_cfg_pres {
52 tap_param_dlg* stat_dlg;
62 /* the columns of the tree pane */
63 enum _stat_tree_columns {
71 /* used for converting numbers */
72 #define NUM_BUF_SIZE 32
74 /* creates the gtk representation for a stat_node
78 setup_gtk_node_pr(stat_node* node)
80 GtkTreeIter* parent = NULL;
82 node->pr = g_malloc(sizeof(st_node_pres));
84 if (node->st->pr->store) {
85 node->pr->iter = g_malloc0(sizeof(GtkTreeIter));
87 if ( node->parent && node->parent->pr ) {
88 parent = node->parent->pr->iter;
90 gtk_tree_store_append (node->st->pr->store, node->pr->iter, parent);
91 gtk_tree_store_set(node->st->pr->store, node->pr->iter,
92 TITLE_COLUMN, node->name, RATE_COLUMN, "", COUNT_COLUMN, "", -1);
98 draw_gtk_node(stat_node* node)
100 static gchar value[NUM_BUF_SIZE];
101 static gchar rate[NUM_BUF_SIZE];
102 static gchar percent[NUM_BUF_SIZE];
105 stats_tree_get_strs_from_node(node, value, rate,
108 if (node->st->pr->store && node->pr->iter) {
109 gtk_tree_store_set(node->st->pr->store, node->pr->iter,
112 PERCENT_COLUMN, percent,
116 if (node->children) {
117 for (child = node->children; child; child = child->next )
118 draw_gtk_node(child);
123 draw_gtk_tree(void *psp)
125 stats_tree *st = psp;
128 for (child = st->root.children; child; child = child->next ) {
129 draw_gtk_node(child);
131 if (child->pr->iter && st->pr->store) {
132 gtk_tree_view_expand_row(GTK_TREE_VIEW(st->pr->tree),
133 gtk_tree_model_get_path(GTK_TREE_MODEL(st->pr->store),
142 free_gtk_tree(GtkWindow *win _U_, stats_tree *st)
145 protect_thread_critical_region();
146 remove_tap_listener(st);
147 unprotect_thread_critical_region();
150 st->root.pr->iter = NULL;
152 st->cfg->in_use = FALSE;
158 clear_node_pr(stat_node* n)
161 for (c = n->children; c; c = c->next) {
166 gtk_tree_store_remove(n->st->pr->store, n->pr->iter);
176 for (c = st->root.children; c; c = c->next) {
183 /* initializes the stats_tree window */
185 init_gtk_tree(const char* optarg, void *userdata _U_)
187 gchar *abbr = stats_tree_get_abbr(optarg);
188 stats_tree* st = NULL;
189 stats_tree_cfg* cfg = NULL;
190 tree_pres* pr = g_malloc(sizeof(tree_pres));
192 gchar* window_name = NULL;
193 GString* error_string;
196 GtkWidget *main_vb, *bbox, *bt_close;
197 GtkTreeViewColumn* column;
198 GtkCellRenderer* renderer;
201 cfg = stats_tree_get_cfg_by_abbr(abbr);
203 if (cfg && cfg->in_use) {
205 report_failure("cannot open more than one tree of the same type at once");
210 init_strlen = strlen(cfg->pr->stat_dlg->init_string);
212 if (strncmp (optarg, cfg->pr->stat_dlg->init_string, init_strlen) == 0){
213 if (init_strlen == strlen(optarg)) {
214 st = stats_tree_new(cfg,pr,NULL);
216 st = stats_tree_new(cfg,pr,(char*)optarg+init_strlen+1);
220 st = stats_tree_new(cfg,pr,NULL);
223 report_failure("no such stats_tree (%s) in stats_tree registry",abbr);
230 report_failure("could not obtain stats_tree abbr from optarg");
237 window_name = g_strdup_printf("%s Stats Tree", cfg->name);
239 st->pr->win = window_new_with_geom(GTK_WINDOW_TOPLEVEL,window_name,window_name);
240 gtk_window_set_default_size(GTK_WINDOW(st->pr->win), 400, 400);
244 title=g_strdup_printf("%s with filter: %s",cfg->name,st->filter);
247 title=g_strdup_printf("%s", cfg->name);
250 gtk_window_set_title(GTK_WINDOW(st->pr->win), title);
253 main_vb = gtk_vbox_new(FALSE, 3);
254 gtk_container_set_border_width(GTK_CONTAINER(main_vb), 12);
255 gtk_container_add(GTK_CONTAINER(st->pr->win), main_vb);
257 scr_win = scrolled_window_new(NULL, NULL);
259 st->pr->store = gtk_tree_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING,
260 G_TYPE_STRING, G_TYPE_STRING);
262 st->pr->tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (st->pr->store));
263 g_object_unref(G_OBJECT(st->pr->store));
265 gtk_container_add( GTK_CONTAINER(scr_win), st->pr->tree);
268 renderer = gtk_cell_renderer_text_new ();
269 column = gtk_tree_view_column_new_with_attributes ("Topic / Item", renderer,
270 "text", TITLE_COLUMN,
272 gtk_tree_view_column_set_resizable (column,TRUE);
273 gtk_tree_view_column_set_sizing(column,GTK_TREE_VIEW_COLUMN_AUTOSIZE);
274 gtk_tree_view_append_column (GTK_TREE_VIEW (st->pr->tree), column);
276 renderer = gtk_cell_renderer_text_new ();
277 column = gtk_tree_view_column_new_with_attributes ("Count", renderer,
278 "text", COUNT_COLUMN,
281 gtk_tree_view_column_set_resizable (column,TRUE);
282 gtk_tree_view_column_set_sizing(column,GTK_TREE_VIEW_COLUMN_AUTOSIZE);
283 gtk_tree_view_append_column (GTK_TREE_VIEW (st->pr->tree), column);
285 renderer = gtk_cell_renderer_text_new ();
286 column = gtk_tree_view_column_new_with_attributes ("Rate (ms)", renderer,
289 gtk_tree_view_column_set_resizable (column,TRUE);
290 gtk_tree_view_column_set_sizing(column,GTK_TREE_VIEW_COLUMN_AUTOSIZE);
291 gtk_tree_view_append_column (GTK_TREE_VIEW (st->pr->tree), column);
293 renderer = gtk_cell_renderer_text_new ();
294 column = gtk_tree_view_column_new_with_attributes ("Percent", renderer,
295 "text", PERCENT_COLUMN,
297 gtk_tree_view_column_set_resizable(column,TRUE);
298 gtk_tree_view_column_set_sizing(column,GTK_TREE_VIEW_COLUMN_AUTOSIZE);
299 gtk_tree_view_append_column (GTK_TREE_VIEW (st->pr->tree), column);
301 gtk_container_add( GTK_CONTAINER(main_vb), scr_win);
303 error_string = register_tap_listener( cfg->tapname,
312 /* error, we failed to attach to the tap. clean up */
313 /* destroy_stat_tree_window(st); */
314 report_failure("stats_tree for: %s failed to attach to the tap: %s",cfg->name,error_string->str);
315 g_string_free(error_string, TRUE);
319 bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
320 gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
322 bt_close = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
323 window_set_cancel_button(st->pr->win, bt_close, window_cancel_button_cb);
325 g_signal_connect(GTK_WINDOW(st->pr->win), "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
326 g_signal_connect(GTK_WINDOW(st->pr->win), "destroy", G_CALLBACK(free_gtk_tree), st);
328 gtk_widget_show_all(st->pr->win);
329 window_present(st->pr->win);
331 cf_retap_packets(&cfile);
332 gdk_window_raise(gtk_widget_get_window(st->pr->win));
335 static tap_param tree_stat_params[] = {
336 { PARAM_FILTER, "Filter", NULL }
340 register_gtk_stats_tree_tap (gpointer k _U_, gpointer v, gpointer p _U_)
342 stats_tree_cfg* cfg = v;
344 cfg->pr = g_malloc(sizeof(tree_pres));
346 cfg->pr->stat_dlg = g_malloc(sizeof(tap_param_dlg));
348 cfg->pr->stat_dlg->win_title = g_strdup_printf("%s Stats Tree",cfg->name);
349 cfg->pr->stat_dlg->init_string = g_strdup_printf("%s,tree",cfg->abbr);
350 cfg->pr->stat_dlg->tap_init_cb = init_gtk_tree;
351 cfg->pr->stat_dlg->index = -1;
352 cfg->pr->stat_dlg->nparams = G_N_ELEMENTS(tree_stat_params);
353 cfg->pr->stat_dlg->params = tree_stat_params;
355 #ifdef MAIN_MENU_USE_UIMANAGER
357 register_dfilter_stat(cfg->pr->stat_dlg, cfg->name, cfg->stat_group);
362 free_tree_presentation(stats_tree* st)
368 register_tap_listener_stats_tree_stat(void)
371 stats_tree_presentation(register_gtk_stats_tree_tap,
377 free_tree_presentation,
383 #ifdef MAIN_MENU_USE_UIMANAGER
384 void gtk_stats_tree_cb(GtkAction *action, gpointer user_data _U_)
386 const gchar *action_name;
388 stats_tree_cfg* cfg = NULL;
390 action_name = gtk_action_get_name (action);
391 abbr = strrchr(action_name,'/');
395 abbr = g_strdup_printf("%s",action_name);
397 cfg = stats_tree_get_cfg_by_abbr(abbr);
399 tap_param_dlg_cb(action, cfg->pr->stat_dlg);
401 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
402 "Failed to find the stat tree named %s",