Rename the tap_dfilter_dlg.[ch] files to reflect that they can do more
[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
46 struct _st_node_pres {
47         GtkTreeIter*    iter;
48 };
49
50 struct _tree_cfg_pres {
51         tap_param_dlg* stat_dlg;
52 };
53
54 struct _tree_pres {
55         GString*        text;
56         GtkWidget*      win;
57         GtkTreeStore*   store;
58         GtkWidget*      tree;
59 };
60
61 /* the columns of the tree pane */
62 enum _stat_tree_columns {
63         TITLE_COLUMN,
64         COUNT_COLUMN,
65         RATE_COLUMN,
66         PERCENT_COLUMN,
67         N_COLUMNS
68 };
69
70 /* used for converting numbers */
71 #define NUM_BUF_SIZE  32
72
73 /* creates the gtk representation for a stat_node
74  * node: the node
75  */
76 static void
77 setup_gtk_node_pr(stat_node* node)
78 {
79         GtkTreeIter* parent =  NULL;
80
81         node->pr = g_malloc(sizeof(st_node_pres));
82
83         if (node->st->pr->store) {
84                 node->pr->iter = g_malloc0(sizeof(GtkTreeIter));
85
86                 if ( node->parent && node->parent->pr ) {
87                         parent = node->parent->pr->iter;
88                 }
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);
92         }
93 }
94
95
96 static void
97 draw_gtk_node(stat_node* node)
98 {
99         static gchar value[NUM_BUF_SIZE];
100         static gchar rate[NUM_BUF_SIZE];
101         static gchar percent[NUM_BUF_SIZE];
102         stat_node* child;
103
104         stats_tree_get_strs_from_node(node, value, rate,
105                                       percent);
106
107         if (node->st->pr->store && node->pr->iter) {
108                 gtk_tree_store_set(node->st->pr->store, node->pr->iter,
109                                    RATE_COLUMN, rate,
110                                    COUNT_COLUMN, value,
111                                    PERCENT_COLUMN, percent,
112                                    -1);
113         }
114
115         if (node->children) {
116                 for (child = node->children; child; child = child->next )
117                         draw_gtk_node(child);
118         }
119 }
120
121 static void
122 draw_gtk_tree(void *psp)
123 {
124         stats_tree *st = psp;
125         stat_node* child;
126
127         for (child = st->root.children; child; child = child->next ) {
128                 draw_gtk_node(child);
129
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),
133                                                                          child->pr->iter),
134                                                  FALSE);
135                 }
136         }
137
138 }
139
140 static void
141 free_gtk_tree(GtkWindow *win _U_, stats_tree *st)
142 {
143
144         protect_thread_critical_region();
145         remove_tap_listener(st);
146         unprotect_thread_critical_region();
147
148         if (st->root.pr)
149                 st->root.pr->iter = NULL;
150
151         st->cfg->in_use = FALSE;
152         stats_tree_free(st);
153
154 }
155
156 static void
157 clear_node_pr(stat_node* n)
158 {
159         stat_node* c;
160         for (c = n->children; c; c = c->next) {
161                 clear_node_pr(c);
162         }
163
164         if (n->pr->iter) {
165                 gtk_tree_store_remove(n->st->pr->store, n->pr->iter);
166                 n->pr->iter = NULL;
167         }
168 }
169
170 static void
171 reset_tap(void* p)
172 {
173         stats_tree* st = p;
174         stat_node* c;
175         for (c = st->root.children; c; c = c->next) {
176                 clear_node_pr(c);
177         }
178
179         st->cfg->init(st);
180 }
181
182 /* initializes the stats_tree window */
183 static void
184 init_gtk_tree(const char* optarg, void *userdata _U_)
185 {
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));
190         gchar* title = NULL;
191         gchar* window_name = NULL;
192         GString* error_string;
193         GtkWidget *scr_win;
194         size_t init_strlen;
195         GtkWidget *main_vb, *bbox, *bt_close;
196         GtkTreeViewColumn* column;
197         GtkCellRenderer* renderer;
198
199         if (abbr) {
200                 cfg = stats_tree_get_cfg_by_abbr(abbr);
201
202                 if (cfg && cfg->in_use) {
203                         /* XXX: ! */
204                         report_failure("cannot open more than one tree of the same type at once");
205                         return;
206                 }
207
208                 if (cfg != NULL) {
209                         init_strlen = strlen(cfg->pr->stat_dlg->init_string);
210
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);
214                                 } else {
215                                         st = stats_tree_new(cfg,pr,(char*)optarg+init_strlen+1);
216                                 }
217
218                         } else {
219                                 st = stats_tree_new(cfg,pr,NULL);
220                         }
221                 } else {
222                         report_failure("no such stats_tree (%s) in stats_tree registry",abbr);
223                         g_free(abbr);
224                         return;
225                 }
226                 g_free(abbr);
227
228         } else {
229                 report_failure("could not obtain stats_tree abbr from optarg");
230                 g_free(pr);
231                 return;
232         }
233
234         cfg->in_use = TRUE;
235
236         window_name = g_strdup_printf("%s Stats Tree", cfg->name);
237
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);
240         g_free(window_name);
241
242         if(st->filter){
243                 title=g_strdup_printf("%s with filter: %s",cfg->name,st->filter);
244         } else {
245                 st->filter=NULL;
246                 title=g_strdup_printf("%s", cfg->name);
247         }
248
249         gtk_window_set_title(GTK_WINDOW(st->pr->win), title);
250         g_free(title);
251
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);
255
256         scr_win = scrolled_window_new(NULL, NULL);
257
258         st->pr->store = gtk_tree_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING,
259                                             G_TYPE_STRING, G_TYPE_STRING);
260
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));
263
264         gtk_container_add( GTK_CONTAINER(scr_win), st->pr->tree);
265
266         /* the columns */
267         renderer = gtk_cell_renderer_text_new ();
268         column = gtk_tree_view_column_new_with_attributes ("Topic / Item", renderer,
269                                                            "text", TITLE_COLUMN,
270                                                            NULL);
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);
274
275         renderer = gtk_cell_renderer_text_new ();
276         column = gtk_tree_view_column_new_with_attributes ("Count", renderer,
277                                                            "text", COUNT_COLUMN,
278                                                            NULL);
279
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);
283
284         renderer = gtk_cell_renderer_text_new ();
285         column = gtk_tree_view_column_new_with_attributes ("Rate (ms)", renderer,
286                                                            "text", RATE_COLUMN,
287                                                            NULL);
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);
291
292         renderer = gtk_cell_renderer_text_new ();
293         column = gtk_tree_view_column_new_with_attributes ("Percent", renderer,
294                                                            "text", PERCENT_COLUMN,
295                                                            NULL);
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);
299
300         gtk_container_add( GTK_CONTAINER(main_vb), scr_win);
301
302         error_string = register_tap_listener( cfg->tapname,
303                                               st,
304                                               st->filter,
305                                               cfg->flags,
306                                               reset_tap,
307                                               stats_tree_packet,
308                                               draw_gtk_tree);
309
310         if (error_string) {
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);
315         }
316
317         /* Button row. */
318         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
319         gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 0);
320
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);
323
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);
326
327         gtk_widget_show_all(st->pr->win);
328         window_present(st->pr->win);
329
330         cf_retap_packets(&cfile);
331         gdk_window_raise(st->pr->win->window);
332 }
333
334
335 static void
336 register_gtk_stats_tree_tap (gpointer k _U_, gpointer v, gpointer p _U_)
337 {
338         stats_tree_cfg* cfg = v;
339
340         cfg->pr = g_malloc(sizeof(tree_pres));
341
342         cfg->pr->stat_dlg = g_malloc(sizeof(tap_param_dlg));
343
344         cfg->pr->stat_dlg->win_title = g_strdup_printf("%s Stats Tree",cfg->name);
345         cfg->pr->stat_dlg->init_string = g_strdup_printf("%s,tree",cfg->abbr);
346         cfg->pr->stat_dlg->tap_init_cb = init_gtk_tree;
347         cfg->pr->stat_dlg->index = -1;
348
349         register_dfilter_stat(cfg->pr->stat_dlg, cfg->name, cfg->stat_group);
350 }
351
352 static void
353 free_tree_presentation(stats_tree* st)
354 {
355         g_free(st->pr);
356 }
357
358 void
359 register_tap_listener_stats_tree_stat(void)
360 {
361
362         stats_tree_presentation(register_gtk_stats_tree_tap,
363                                 setup_gtk_node_pr,
364                                 NULL,
365                                 NULL,
366                                 NULL,
367                                 NULL,
368                                 free_tree_presentation,
369                                 NULL,
370                                 NULL,
371                                 NULL);
372 }