Wireshark-dev: [Wireshark-dev] [Patch] Add "Copy as Filter" to the detail-pane context menu
From: Sake Blok <sake@xxxxxxxxxx>
Date: Sat, 24 Feb 2007 09:17:20 +0100
Hi,

Many times I find myself using prepare as filter, just to copy the filter
to another instance of wireshark with a second trace (think of selecting
the tcp.seq to match packets between two traces). As this distroys the filter
that was currently active, I wrote a little patch that adds a "Copy as Filter"
item to the context-menu in the detail-view.

Could someone review this patch and add it to the trunk?

Cheers,


Sake
Index: gtk/menu.c
===================================================================
--- gtk/menu.c	(revision 20910)
+++ gtk/menu.c	(working copy)
@@ -762,6 +762,8 @@
 
     ITEM_FACTORY_ENTRY("/<separator>", NULL, NULL, 0, "<Separator>", NULL),
 
+    ITEM_FACTORY_ENTRY("/Copy as Filter", NULL, copy_selected_ptree_cb, 0, NULL, NULL),
+
     ITEM_FACTORY_ENTRY("/Apply as Filter", NULL, NULL, 0, "<Branch>", NULL),
     ITEM_FACTORY_ENTRY("/Apply as Filter/_Selected", NULL, match_selected_ptree_cb,
                        MATCH_SELECTED_REPLACE|MATCH_SELECTED_APPLY_NOW, NULL, NULL),
Index: gtk/main.c
===================================================================
--- gtk/main.c	(revision 20910)
+++ gtk/main.c	(working copy)
@@ -327,6 +327,29 @@
 }
 
 
+/* This function allows users to right click in the details window and copy the
+ * currently selected item as a display filter to the operating systems clipboard.
+ */
+void
+copy_selected_ptree_cb(GtkWidget *w _U_, gpointer data _U_)
+{
+    GString *gtk_text_str = g_string_new("");
+    char *filter;
+
+    if (cfile.finfo_selected) {
+        filter = proto_construct_match_selected_string(cfile.finfo_selected,
+                                                       cfile.edt);
+        if (strlen(filter) == 0) {              /* If no representation then... */
+            simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
+                "Could not acquire information to copy, try expanding or choosing another item");
+	} else {                                 /* Copy string to clipboard */
+	    g_string_sprintfa(gtk_text_str, "%s", filter);
+	    copy_to_clipboard(gtk_text_str); 
+	}
+    }
+    g_string_free(gtk_text_str, TRUE);           /* Free the memory */
+}
+
 static void selected_ptree_info_answered_cb(gpointer dialog _U_, gint btn, gpointer data)
 {
     gchar *selected_proto_url;
Index: gtk/main.h
===================================================================
--- gtk/main.h	(revision 20910)
+++ gtk/main.h	(working copy)
@@ -142,6 +142,13 @@
  */
 extern void match_selected_ptree_cb(GtkWidget *widget, gpointer data, MATCH_SELECTED_E action);
 
+/** User requested "Copy as Filter" function by context menu of protocol tree.
+ *
+ * @param widget parent widget
+ * @param data parent widget
+ */
+extern void copy_selected_ptree_cb(GtkWidget *widget _U_, gpointer data);
+
 /** User requested one of "Apply as Filter" or "Prepare a Filter" functions
  *  by context menu of packet list.
  *