Immediately quit routines if fwrite() fails - further writes will
[obnox/wireshark/wip.git] / gtk / stats_tree_stat.c
index ebde04b424aa1c4b939c41b17840a71b8d93e899..e2caf8c9d3f30f39ad14ea1fa30b310394517b29 100644 (file)
@@ -4,8 +4,8 @@
  *
  * $Id$
  *
- * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@ethereal.com>
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  * 
  * This program is free software; you can redistribute it and/or
 #include <gtk/gtk.h>
 
 #include <epan/stats_tree_priv.h>
+#include <epan/report_err.h>
 
 #include "simple_dialog.h"
 #include "globals.h"
-#include "tap_menu.h"
-#include "ui_util.h"
+#include "gui_utils.h"
 #include "dlg_utils.h"
 #include "compat_macros.h"
-#include "tap_dfilter_dlg.h"
+#include "../stat_menu.h"
 #include "../tap_dfilter_dlg.h"
 
 struct _st_node_pres {
@@ -131,10 +131,11 @@ static void draw_gtk_node(stat_node* node) {
        static gchar percent[NUM_BUF_SIZE];
        stat_node* child;
        
-       stats_tree_get_strs_from_node(node, value, rate, percent);
+       stats_tree_get_strs_from_node(node, value, rate,
+                                     percent);
        
 #if GTK_MAJOR_VERSION >= 2
-       if (node->st->pr->store) {
+       if (node->st->pr->store && node->pr->iter) {
                gtk_tree_store_set(node->st->pr->store, node->pr->iter,
                                                   RATE_COLUMN, rate,
                                                   COUNT_COLUMN, value,
@@ -166,10 +167,12 @@ static void draw_gtk_tree( void *psp  ) {
                draw_gtk_node(child);
 
 #if GTK_MAJOR_VERSION >= 2
-               gtk_tree_view_expand_row(GTK_TREE_VIEW(st->pr->tree),
+               if (child->pr->iter && st->pr->store) {
+                       gtk_tree_view_expand_row(GTK_TREE_VIEW(st->pr->tree),
                                                                 gtk_tree_model_get_path(GTK_TREE_MODEL(st->pr->store),
                                                                                                                 child->pr->iter),
                                                                 FALSE);
+               }
 #endif
        }
 
@@ -190,19 +193,49 @@ static void free_gtk_tree(GtkWindow *win _U_, stats_tree *st)
                st->root.pr->iter = NULL;
 #endif
        
+       st->cfg->in_use = FALSE;
        stats_tree_free(st);
        
 }
 
+static void clear_node_pr(stat_node* n) {
+       stat_node* c;
+       for (c = n->children; c; c = c->next) {
+               clear_node_pr(c);
+       }
+       
+#if GTK_MAJOR_VERSION >= 2
+       if (n->pr->iter) {
+               gtk_tree_store_remove(n->st->pr->store, n->pr->iter);
+               n->pr->iter = NULL;
+       }
+#else
+       if (n->pr->node) {
+               gtk_ctree_remove_node(GTK_CTREE(n->st->pr->ctree),n->pr->node);
+               n->pr->node = NULL;
+       }
+#endif
+
+}
+
+static void reset_tap(void* p) {
+    stats_tree* st = p;
+       stat_node* c;
+       for (c = st->root.children; c; c = c->next) {
+               clear_node_pr(c);
+       }
+       
+       st->cfg->init(st);
+}
 
 /* initializes the stats_tree window */
-static void init_gtk_tree(const char* optarg) {
+static void init_gtk_tree(const char* optarg, void *userdata _U_) {
        guint8* abbr = stats_tree_get_abbr(optarg);
        stats_tree* st = NULL;
        stats_tree_cfg* cfg = NULL;
        tree_pres* pr = g_malloc(sizeof(tree_pres));
-       guint8* title = NULL;
-       guint8* window_name = NULL;
+       gchar* title = NULL;
+       gchar* window_name = NULL;
        GString* error_string;
        GtkWidget *scr_win;
        guint init_strlen;
@@ -223,6 +256,12 @@ static void init_gtk_tree(const char* optarg) {
        if (abbr) {
                cfg = stats_tree_get_cfg_by_abbr(abbr);
                
+               if (cfg && cfg->in_use) {
+                       /* XXX: ! */
+                       report_failure("cannot open more than one tree of the same type at once");
+                       return;
+               }
+               
                if (cfg != NULL) {
                        init_strlen = strlen(cfg->pr->stat_dlg->init_string);
                        
@@ -230,21 +269,27 @@ static void init_gtk_tree(const char* optarg) {
                                if (init_strlen == strlen(optarg)) {
                                        st = stats_tree_new(cfg,pr,NULL);
                                } else { 
-                                       st = stats_tree_new(cfg,pr,((guint8*)optarg)+init_strlen+1);
+                                       st = stats_tree_new(cfg,pr,(char*)optarg+init_strlen+1);
                                }
                                
                        } else {
                                st = stats_tree_new(cfg,pr,NULL);
                        }
                } else {
-                       g_error("no such stats_tree (%s) found in stats_tree registry",abbr);
+                       report_failure("no such stats_tree (%s) in stats_tree registry",abbr);
+                       g_free(abbr);
+                       return;
                }
                g_free(abbr);
                
        } else {
-               g_error("could not obtain stats_tree abbr from optarg");                
+               report_failure("could not obtain stats_tree abbr from optarg");
+               g_free(pr);
+               return;
        }
 
+       cfg->in_use = TRUE;
+       
        window_name = g_strdup_printf("%s Stats Tree", cfg->name);
        
        st->pr->win = window_new_with_geom(GTK_WINDOW_TOPLEVEL,window_name,window_name);
@@ -327,17 +372,17 @@ static void init_gtk_tree(const char* optarg) {
        gtk_container_add( GTK_CONTAINER(main_vb), scr_win);
        
        error_string = register_tap_listener( cfg->tapname,
-                                                                                 st,
-                                                                                 st->filter,
-                                                                                 NULL,
-                                                                                 stats_tree_packet,
-                                                                                 draw_gtk_tree);
+                                             st,
+                                             st->filter,
+                                             reset_tap,
+                                             stats_tree_packet,
+                                             draw_gtk_tree);
        
        if (error_string) {
                /* error, we failed to attach to the tap. clean up */
                simple_dialog( ESD_TYPE_ERROR, ESD_BTN_OK, error_string->str );
                /* destroy_stat_tree_window(st); */
-               g_error("stats_tree for: %s failed to attach to the tap: %s",cfg->name,error_string->str);
+               report_failure("stats_tree for: %s failed to attach to the tap: %s",cfg->name,error_string->str);
                g_string_free(error_string, TRUE);
        }
                
@@ -358,20 +403,13 @@ static void init_gtk_tree(const char* optarg) {
        gtk_tree_view_set_model(GTK_TREE_VIEW(st->pr->tree),GTK_TREE_MODEL(st->pr->store));
 #endif
        
-       st->cfg->init(st);
-
-       cf_retap_packets(&cfile);
+       cf_retap_packets(&cfile, FALSE);
 }
 
 
 static void register_gtk_stats_tree_tap (gpointer k _U_, gpointer v, gpointer p _U_) {
        stats_tree_cfg* cfg = v;
-       guint8* s;
 
-       s = g_strdup_printf("%s,tree",cfg->abbr);
-       
-       register_tap_listener_cmd_arg(s, init_gtk_tree);
-       
        cfg->pr = g_malloc(sizeof(tree_pres));
        
        cfg->pr->stat_dlg = g_malloc(sizeof(tap_dfilter_dlg));
@@ -381,8 +419,8 @@ static void register_gtk_stats_tree_tap (gpointer k _U_, gpointer v, gpointer p
        cfg->pr->stat_dlg->tap_init_cb = init_gtk_tree;
        cfg->pr->stat_dlg->index = -1;
        
-       register_tap_menu_item(cfg->name, REGISTER_TAP_GROUP_NONE,
-                                                  gtk_tap_dfilter_dlg_cb, NULL, NULL, cfg->pr->stat_dlg);
+       register_dfilter_stat(cfg->pr->stat_dlg, cfg->name,
+           REGISTER_STAT_GROUP_NONE);
 }
 
 static void free_tree_presentation(stats_tree* st) {
@@ -394,7 +432,13 @@ register_tap_listener_stats_tree_stat(void)
 {
        
        stats_tree_presentation(register_gtk_stats_tree_tap,
-                                                       setup_gtk_node_pr, NULL,
+                                                       setup_gtk_node_pr,
+                            NULL,
+                                                       NULL,
                                                        NULL,
-                                                       NULL, NULL, free_tree_presentation, NULL, NULL, NULL);
+                            NULL,
+                            free_tree_presentation,
+                            NULL,
+                            NULL,
+                            NULL);
 }