merge_all_tap_menus() has been moved to menus.c.
[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  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
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 #include <string.h>
30
31 #include <gtk/gtk.h>
32
33 #include <epan/stats_tree_priv.h>
34 #include <epan/report_err.h>
35
36 #include "../simple_dialog.h"
37 #include "../globals.h"
38 #include "../stat_menu.h"
39
40 #include "gtk/gui_utils.h"
41 #include "gtk/dlg_utils.h"
42 #include "gtk/tap_param_dlg.h"
43 #include "gtk/main.h"
44
45 #include "gtk/old-gtk-compat.h"
46
47 struct _st_node_pres {
48         GtkTreeIter*    iter;
49 };
50
51 struct _tree_cfg_pres {
52         tap_param_dlg* stat_dlg;
53 };
54
55 struct _tree_pres {
56         GString*        text;
57         GtkWidget*      win;
58         GtkTreeStore*   store;
59         GtkWidget*      tree;
60 };
61
62 /* the columns of the tree pane */
63 enum _stat_tree_columns {
64         TITLE_COLUMN,
65         COUNT_COLUMN,
66         RATE_COLUMN,
67         PERCENT_COLUMN,
68         N_COLUMNS
69 };
70
71 /* used for converting numbers */
72 #define NUM_BUF_SIZE  32
73
74 /* creates the gtk representation for a stat_node
75  * node: the node
76  */
77 static void
78 setup_gtk_node_pr(stat_node* node)
79 {
80         GtkTreeIter* parent =  NULL;
81
82         node->pr = g_malloc(sizeof(st_node_pres));
83
84         if (node->st->pr->store) {
85                 node->pr->iter = g_malloc0(sizeof(GtkTreeIter));
86
87                 if ( node->parent && node->parent->pr ) {
88                         parent = node->parent->pr->iter;
89                 }
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);
93         }
94 }
95
96
97 static void
98 draw_gtk_node(stat_node* node)
99 {
100         static gchar value[NUM_BUF_SIZE];
101         static gchar rate[NUM_BUF_SIZE];
102         static gchar percent[NUM_BUF_SIZE];
103         stat_node* child;
104
105         stats_tree_get_strs_from_node(node, value, rate,
106                                       percent);
107
108         if (node->st->pr->store && node->pr->iter) {
109                 gtk_tree_store_set(node->st->pr->store, node->pr->iter,
110                                    RATE_COLUMN, rate,
111                                    COUNT_COLUMN, value,
112                                    PERCENT_COLUMN, percent,
113                                    -1);
114         }
115
116         if (node->children) {
117                 for (child = node->children; child; child = child->next )
118                         draw_gtk_node(child);
119         }
120 }
121
122 static void
123 draw_gtk_tree(void *psp)
124 {
125         stats_tree *st = psp;
126         stat_node* child;
127
128         for (child = st->root.children; child; child = child->next ) {
129                 draw_gtk_node(child);
130
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),
134                                                                          child->pr->iter),
135                                                  FALSE);
136                 }
137         }
138
139 }
140
141 static void
142 free_gtk_tree(GtkWindow *win _U_, stats_tree *st)
143 {
144
145         protect_thread_critical_region();
146         remove_tap_listener(st);
147         unprotect_thread_critical_region();
148
149         if (st->root.pr)
150                 st->root.pr->iter = NULL;
151
152         st->cfg->in_use = FALSE;
153         stats_tree_free(st);
154
155 }
156
157 static void
158 clear_node_pr(stat_node* n)
159 {
160         stat_node* c;
161         for (c = n->children; c; c = c->next) {
162                 clear_node_pr(c);
163         }
164
165         if (n->pr->iter) {
166                 gtk_tree_store_remove(n->st->pr->store, n->pr->iter);
167                 n->pr->iter = NULL;
168         }
169 }
170
171 static void
172 reset_tap(void* p)
173 {
174         stats_tree* st = p;
175         stat_node* c;
176         for (c = st->root.children; c; c = c->next) {
177                 clear_node_pr(c);
178         }
179
180         st->cfg->init(st);
181 }
182
183 /* initializes the stats_tree window */
184 static void
185 init_gtk_tree(const char* optarg, void *userdata _U_)
186 {
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));
191         gchar* title = NULL;
192         gchar* window_name = NULL;
193         GString* error_string;
194         GtkWidget *scr_win;
195         size_t init_strlen;
196         GtkWidget *main_vb, *bbox, *bt_close;
197         GtkTreeViewColumn* column;
198         GtkCellRenderer* renderer;
199
200         if (abbr) {
201                 cfg = stats_tree_get_cfg_by_abbr(abbr);
202
203                 if (cfg && cfg->in_use) {
204                         /* XXX: ! */
205                         report_failure("cannot open more than one tree of the same type at once");
206                         return;
207                 }
208
209                 if (cfg != NULL) {
210                         init_strlen = strlen(cfg->pr->stat_dlg->init_string);
211
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);
215                                 } else {
216                                         st = stats_tree_new(cfg,pr,(char*)optarg+init_strlen+1);
217                                 }
218
219                         } else {
220                                 st = stats_tree_new(cfg,pr,NULL);
221                         }
222                 } else {
223                         report_failure("no such stats_tree (%s) in stats_tree registry",abbr);
224                         g_free(abbr);
225                         return;
226                 }
227                 g_free(abbr);
228
229         } else {
230                 report_failure("could not obtain stats_tree abbr from optarg");
231                 g_free(pr);
232                 return;
233         }
234
235         cfg->in_use = TRUE;
236
237         window_name = g_strdup_printf("%s Stats Tree", cfg->name);
238
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);
241         g_free(window_name);
242
243         if(st->filter){
244                 title=g_strdup_printf("%s with filter: %s",cfg->name,st->filter);
245         } else {
246                 st->filter=NULL;
247                 title=g_strdup_printf("%s", cfg->name);
248         }
249
250         gtk_window_set_title(GTK_WINDOW(st->pr->win), title);
251         g_free(title);
252
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);
256
257         scr_win = scrolled_window_new(NULL, NULL);
258
259         st->pr->store = gtk_tree_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING,
260                                             G_TYPE_STRING, G_TYPE_STRING);
261
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));
264
265         gtk_container_add( GTK_CONTAINER(scr_win), st->pr->tree);
266
267         /* the columns */
268         renderer = gtk_cell_renderer_text_new ();
269         column = gtk_tree_view_column_new_with_attributes ("Topic / Item", renderer,
270                                                            "text", TITLE_COLUMN,
271                                                            NULL);
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);
275
276         renderer = gtk_cell_renderer_text_new ();
277         column = gtk_tree_view_column_new_with_attributes ("Count", renderer,
278                                                            "text", COUNT_COLUMN,
279                                                            NULL);
280
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);
284
285         renderer = gtk_cell_renderer_text_new ();
286         column = gtk_tree_view_column_new_with_attributes ("Rate (ms)", renderer,
287                                                            "text", RATE_COLUMN,
288                                                            NULL);
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);
292
293         renderer = gtk_cell_renderer_text_new ();
294         column = gtk_tree_view_column_new_with_attributes ("Percent", renderer,
295                                                            "text", PERCENT_COLUMN,
296                                                            NULL);
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);
300
301         gtk_container_add( GTK_CONTAINER(main_vb), scr_win);
302
303         error_string = register_tap_listener( cfg->tapname,
304                                               st,
305                                               st->filter,
306                                               cfg->flags,
307                                               reset_tap,
308                                               stats_tree_packet,
309                                               draw_gtk_tree);
310
311         if (error_string) {
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);
316         }
317
318         /* Button row. */
319         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
320         gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
321
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);
324
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);
327
328         gtk_widget_show_all(st->pr->win);
329         window_present(st->pr->win);
330
331         cf_retap_packets(&cfile);
332         gdk_window_raise(gtk_widget_get_window(st->pr->win));
333 }
334
335 static tap_param tree_stat_params[] = {
336         { PARAM_FILTER, "Filter", NULL }
337 };
338
339 static void
340 register_gtk_stats_tree_tap (gpointer k _U_, gpointer v, gpointer p _U_)
341 {
342         stats_tree_cfg* cfg = v;
343
344         cfg->pr = g_malloc(sizeof(tree_pres));
345
346         cfg->pr->stat_dlg = g_malloc(sizeof(tap_param_dlg));
347
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;
354
355 #ifdef MAIN_MENU_USE_UIMANAGER
356 #else
357         register_dfilter_stat(cfg->pr->stat_dlg, cfg->name, cfg->stat_group);
358 #endif
359 }
360
361 static void
362 free_tree_presentation(stats_tree* st)
363 {
364         g_free(st->pr);
365 }
366
367 void
368 register_tap_listener_stats_tree_stat(void)
369 {
370
371         stats_tree_presentation(register_gtk_stats_tree_tap,
372                                 setup_gtk_node_pr,
373                                 NULL,
374                                 NULL,
375                                 NULL,
376                                 NULL,
377                                 free_tree_presentation,
378                                 NULL,
379                                 NULL,
380                                 NULL);
381 }
382
383 #ifdef MAIN_MENU_USE_UIMANAGER
384 void gtk_stats_tree_cb(GtkAction *action, gpointer user_data _U_)
385 {
386         const gchar *action_name; 
387         gchar *abbr;
388         stats_tree_cfg* cfg = NULL;
389
390         action_name = gtk_action_get_name (action);
391         abbr = strrchr(action_name,'/');
392         if(abbr){
393                 abbr = abbr+1;
394         }else{
395                 abbr = g_strdup_printf("%s",action_name);
396         }
397         cfg = stats_tree_get_cfg_by_abbr(abbr);
398         if(cfg){
399                 tap_param_dlg_cb(action, cfg->pr->stat_dlg);
400         }else{
401         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
402                       "Failed to find the stat tree named %s",
403                       abbr);
404                 return;
405         }
406
407 }
408 #endif