Ethereal-dev: Re: [Ethereal-dev] Color Filters - rollback on Cancel - given up

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: Richard Urwin <richard@xxxxxxxxxxxxxxx>
Date: Wed, 22 Sep 2004 22:29:23 +0100
On Tuesday 21 Sep 2004 7:45 pm, Ulf Lamping wrote:
> Richard Urwin wrote:
> >I was modifying the color filters dialog so that changes made and
> > then cancelled would not affect the packet display.
> >
> >I have given up. I need to get more understanding of the GTK
> > structures, and this requires time, effort and interest that I
> > don't have.
> >
> >If anyone wants to continue where I left off, and has understanding
> > of GTK, then I can supply the diffs as far as I've got. It should
> > be fairly straightforward.
>
> You can send me the diffs, but I cannot promise to finish your work
> (not much time) ...
>
> Regards, ULFL

Sorry, Ulf, I seem to have deleted the base release that I patched.
I think this correct, but there is one bit that I think may need 
merging. It would be best to do it all by hand, I think.

-- 
Richard Urwin
diff -u /home/richard/Documents/Downloads/ethereal-0.10.3/gtk/color_dlg.c ethereal/gtk/color_dlg.c
--- /home/richard/Documents/Downloads/ethereal-0.10.3/gtk/color_dlg.c	2004-03-18 04:05:34.000000000 +0000
+++ ethereal/gtk/color_dlg.c	2004-09-18 16:05:32.000000000 +0100
@@ -1,7 +1,7 @@
 /* color_dlg.c
  * Definitions for dialog boxes for color filters
  *
- * $Id: color_dlg.c,v 1.44 2004/03/13 15:15:23 ulfl Exp $
+ * $Id: color_dlg.c,v 1.51 2004/05/31 13:35:49 ulfl Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@xxxxxxxxxxxx>
@@ -46,6 +46,8 @@
 #include "compat_macros.h"
 #include "filter_prefs.h"
 #include "file_dlg.h"
+#include "gtkglobals.h"
+#include "prefs.h"
 
 static GtkWidget* colorize_dialog_new(char *filter);
 static void add_filter_to_list(gpointer filter_arg, gpointer list_arg);
@@ -83,7 +85,6 @@
 static void edit_color_filter_fg_cb(GtkButton *button, gpointer user_data);
 static void edit_color_filter_bg_cb(GtkButton *button, gpointer user_data);
 static void edit_color_filter_ok_cb(GtkButton *button, gpointer user_data);
-static void edit_color_filter_cancel_cb(GtkObject *object, gpointer user_data);
 
 static GtkWidget* color_sel_win_new(color_filter_t *colorf, gboolean);
 static void color_sel_ok_cb(GtkButton *button, gpointer user_data);
@@ -153,7 +154,7 @@
 {
   int count = 0;
 
-  g_slist_foreach(filter_list, count_this_mark, &count);
+  g_slist_foreach(temp_filter_list, count_this_mark, &count);
 
   return count;
 }
@@ -206,7 +207,7 @@
 #endif
   gchar *titles[] = { "Name", "String" };
 
-
+  init_temp_color_filters();	/* copy the color filters into a temp list */
   row_selected = -1; /* no row selected */
   tooltips = gtk_tooltips_new ();
 
@@ -363,7 +364,7 @@
 #endif
 
   num_of_filters = 0;
-  g_slist_foreach(filter_list, add_filter_to_list, color_filters);
+  g_slist_foreach(temp_filter_list, add_filter_to_list, color_filters);
 
 #if GTK_MAJOR_VERSION < 2
   gtk_clist_set_selection_mode    (GTK_CLIST (color_filters),GTK_SELECTION_EXTENDED);
@@ -583,8 +585,8 @@
   
 #endif
 
-  filter_list = g_slist_remove(filter_list, colorf);
-  filter_list = g_slist_insert(filter_list, colorf, filter_number + amount);
+  temp_filter_list = g_slist_remove(temp_filter_list, colorf);
+  temp_filter_list = g_slist_insert(temp_filter_list, colorf, filter_number + amount);
 }
 
 /* Move the selected filters up in the list */
@@ -752,7 +754,7 @@
     data.count = 0; 
     data.color_filters = color_filters;
 
-    g_slist_foreach(filter_list, clear_mark, NULL);
+    g_slist_foreach(temp_filter_list, clear_mark, NULL);
     gtk_tree_selection_selected_foreach(sel,remember_this_row, &data);
                                       
     if (data.count > 0)
@@ -841,7 +843,7 @@
                                         gpointer         user_data _U_)
 {
   /* Destroy any edit dialogs we have open. */
-  g_slist_foreach(filter_list, destroy_edit_dialog_cb, NULL);
+  g_slist_foreach(temp_filter_list, destroy_edit_dialog_cb, NULL);
 
   colorize_win = NULL;
 }
@@ -1122,11 +1131,17 @@
 static void
 color_ok_cb(GtkButton *button _U_, gpointer user_data _U_)
 {
+  /* copy temp list of filters back into global list */
+  apply_all_color_filters();
+  
+  /* destroy temp color filter list */
+  remove_temp_color_filters();
+  
   /* colorize list */
   colorize_packets(&cfile);
 
   /* Destroy the dialog box. */
   window_destroy(colorize_win);
 }
 
 /* Exit dialog without colorizing packets with the new list.
@@ -1134,14 +1149,20 @@
 static void
 color_cancel_cb(GtkWidget *widget _U_, gpointer user_data _U_)
 {
+  /* destroy temp color filter list */
+  remove_temp_color_filters();
+  
   /* Destroy the dialog box. */
   window_destroy(colorize_win);
 }
 
 /* Apply new list of color filters to the capture. */
 static void
 color_apply_cb(GtkButton *button _U_, gpointer user_data _U_)
 {
+  /* copy temp list of filters back into real list */
+  apply_all_color_filters();
+  
   colorize_packets(&cfile);
 }
 
@@ -1580,7 +1587,6 @@
 
 
   GTK_WIDGET_SET_FLAGS (color_sel_help, GTK_CAN_DEFAULT);
-  SIGNAL_CONNECT(color_sel_win, "destroy", color_sel_cancel_cb, color_sel_win);
 
   SIGNAL_CONNECT(color_sel_ok, "clicked", color_sel_ok_cb, color_sel_win);
   SIGNAL_CONNECT(color_sel_cancel, "clicked", color_sel_cancel_cb, color_sel_win);
diff -u /home/richard/Documents/Downloads/ethereal-0.10.3/gtk/color_filters.c ethereal/gtk/color_filters.c
--- /home/richard/Documents/Downloads/ethereal-0.10.3/gtk/color_filters.c	2004-03-18 04:05:34.000000000 +0000
+++ ethereal/gtk/color_filters.c	2004-09-18 16:05:27.000000000 +0100
@@ -1,7 +1,7 @@
 /* color_filters.c
  * Routines for color filters
  *
- * $Id: color_filters.c,v 1.8 2004/03/14 23:55:53 deniel Exp $
+ * $Id: color_filters.c,v 1.10 2004/04/16 19:36:36 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@xxxxxxxxxxxx>
@@ -43,12 +43,18 @@
 #include "file.h"
 #include <epan/dfilter/dfilter.h>
 #include "simple_dialog.h"
 
 static gboolean read_filters(void);
 static gboolean read_global_filters(void);
 
+/* There can be only one color dialog, so there is no need to generalize to
+* support multiple temp lists. In fact only the new_color_filter() function
+* can be called for either list. So we will duplicate that function. If multiple
+* temp lists are ever needed we can modify the temp version of that function to
+* take a handle parameter. */
+
 GSList *filter_list = NULL;
+GSList *temp_filter_list = NULL;
 GSList *removed_filter_list = NULL;
 
 /* Remove the specified filter from the list of existing color filters,
@@ -59,7 +65,19 @@
 void remove_color_filter(color_filter_t *colorf)
 {
 	/* Remove colorf from the list of color filters */
-	filter_list = g_slist_remove(filter_list, colorf);
+	temp_filter_list = g_slist_remove(temp_filter_list, colorf);
+	/* Add colorf to the list of removed color filters */
+	removed_filter_list = g_slist_prepend(removed_filter_list, colorf);
+}
+
+/* Remove the specified filter as an iterator
+* Since it's an iterator, we don't actually remove it, just add it to the
+* removed list. The calling code will then free the entire list,
+* so we can use this function for filters on either list. */
+static void
+remove_color_filter_it(gpointer filter_arg, gpointer unused _U_)
+{
+	color_filter_t *colorf = filter_arg;
 	/* Add colorf to the list of removed color filters */
 	removed_filter_list = g_slist_prepend(removed_filter_list, colorf);
 }
@@ -97,6 +115,55 @@
 	removed_filter_list = NULL;
 }
 
+/* copy the specified filter from temp list to global list as an iterator*/
+/* should be called from a foreach on the temp list */
+static void
+apply_color_filter_it(gpointer filter_arg, gpointer unused _U_)
+{
+	color_filter_t *colorf = filter_arg;
+	filter_list = g_slist_append(filter_list, colorf);
+}
+
+/* copy all filters from temp list to global list */
+void
+apply_all_color_filters(void)
+{
+	g_slist_foreach(filter_list, remove_color_filter_it, NULL);
+	g_slist_free(filter_list);
+	filter_list = NULL;
+	g_slist_foreach(temp_filter_list, apply_color_filter_it, NULL);
+}
+
+/* remove all color filters in the temp list */
+void
+remove_temp_color_filters(void)
+{
+	if (temp_filter_list != NULL)
+	{
+		g_slist_foreach(temp_filter_list, remove_color_filter_it, NULL);
+		g_slist_free(temp_filter_list);
+		temp_filter_list = NULL;
+	}
+}
+
+/* copy the specified filter from global list to temp list as an iterator*/
+/* should be called from a foreach on the temp list */
+static void
+copy_color_filter_it(gpointer filter_arg, gpointer unused _U_)
+{
+	color_filter_t *colorf = filter_arg;
+	temp_filter_list = g_slist_append(temp_filter_list, colorf);
+}
+
+/* initialize temp list as a copy of the global list */
+void
+init_temp_color_filters(void)
+{
+	remove_temp_color_filters();
+	g_slist_foreach(filter_list, copy_color_filter_it, NULL);
+}
+
+
 /* Initialize the filter structures (reading from file) for general running, including app startup */
 void
 colfilter_init(void)
@@ -107,22 +174,50 @@
WARNING: CHECK THIS OUT BY HAND - I DON'T TRUST THE CHANGE IN PARAMETERS
}
 
 /* Create a new filter */
-color_filter_t *
-new_color_filter(gchar *name,           /* The name of the filter to create */
-                 gchar *filter_string)  /* The string representing the filter */
+static color_filter_t *
+make_new_color_filter(gchar *name,           /* The name of the filter to create */
+                 gchar *filter_string,  /* The string representing the filter */
+                 GdkColor *bg_color,    /* The background color */
+                 GdkColor *fg_color)    /* The foreground color */
 {
 	color_filter_t *colorf;
-        GtkStyle       *style;
 
 	colorf = g_malloc(sizeof (color_filter_t));
 	colorf->filter_name = g_strdup(name);
 	colorf->filter_text = g_strdup(filter_string);
-        style = gtk_widget_get_style(packet_list);
-	gdkcolor_to_color_t(&colorf->bg_color, &style->base[GTK_STATE_NORMAL]);
-	gdkcolor_to_color_t(&colorf->fg_color, &style->text[GTK_STATE_NORMAL]);
+	gdkcolor_to_color_t(&colorf->bg_color, bg_color);
+	gdkcolor_to_color_t(&colorf->fg_color, fg_color);
 	colorf->c_colorfilter = NULL;
 	colorf->edit_dialog = NULL;
 	colorf->marked = FALSE;
+
+	return colorf;
+}
+
+/* Create a new filter in the temp list */
+color_filter_t *
+new_temp_color_filter(gchar *name,           /* The name of the filter to create */
+                 gchar *filter_string,  /* The string representing the filter */
+                 GdkColor *bg_color,    /* The background color */
+                 GdkColor *fg_color)    /* The foreground color */
+{
+	color_filter_t *colorf;
+
+	colorf = make_new_color_filter(name, filter_string, bg_color, fg_color);
+	filter_list = g_slist_append(temp_filter_list, colorf);
+        return colorf;
+}
+
+/* Create a new filter in the global list */
+color_filter_t *
+new_color_filter(gchar *name,           /* The name of the filter to create */
+                 gchar *filter_string,  /* The string representing the filter */
+                 GdkColor *bg_color,    /* The background color */
+                 GdkColor *fg_color)    /* The foreground color */
+{
+	color_filter_t *colorf;
+
+	colorf = make_new_color_filter(name, filter_string, bg_color, fg_color);
 	filter_list = g_slist_append(filter_list, colorf);
         return colorf;
 }

diff -u /home/richard/Documents/Downloads/ethereal-0.10.3/gtk/color_filters.h ethereal/gtk/color_filters.h
--- /home/richard/Documents/Downloads/ethereal-0.10.3/gtk/color_filters.h	2004-03-18 04:05:34.000000000 +0000
+++ ethereal/gtk/color_filters.h	2004-09-18 16:05:38.000000000 +0100
@@ -1,7 +1,7 @@
 /* color_filters.h
  * Definitions for color filters
  *
- * $Id: color_filters.h,v 1.5 2004/03/14 23:55:53 deniel Exp $
+ * $Id: color_filters.h,v 1.6 2004/04/16 19:36:36 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@xxxxxxxxxxxx>
@@ -32,8 +32,15 @@
 gboolean write_filters(void);
 gboolean revert_filters(void);
 
-color_filter_t *new_color_filter(gchar *name, gchar *filter_string);
+color_filter_t *new_color_filter(gchar *name, gchar *filter_string,
+    GdkColor *bg_color, GdkColor *fg_color);
+color_filter_t *new_temp_color_filter(gchar *name, gchar *filter_string,
+    GdkColor *bg_color, GdkColor *fg_color);
 void remove_color_filter(color_filter_t *colorf);
+void apply_all_color_filters(void);
+void copy_all_color_filters(void);
+void init_temp_color_filters(void);
+
 gboolean read_other_filters(gchar *path, gpointer arg);
 gboolean write_other_filters(gchar *path, gboolean only_marked);
 
--- /home/richard/Documents/Downloads/ethereal-0.10.3/color.h	2004-01-11 20:54:07.000000000 +0000
+++ ethereal/color.h	2004-06-05 19:13:49.000000000 +0100
@@ -58,6 +58,7 @@
 
 /* List of all color filters. */
 extern GSList *filter_list;
+extern GSList *temp_filter_list;
 extern GSList *removed_filter_list;
 
 void