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