Ethereal-dev: [Ethereal-dev] Copy functionality

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

From: "Greg Morris" <gmorris@xxxxxxxxxx>
Date: Tue, 09 May 2006 16:47:33 +0200
Attached are diffs to add copy functionality to the details window. Also includes a change in the copy function of the hex data window to allow copying just the text strings instead of all of the hex data.
 
Tested on:
Windows XP
SuSE Linux 10 GTK1
SuSE Linux 10 GTK2
 
Build and testing done against current SVN codebase.
 
Please comment/check-in,
Greg
Index: proto_draw.c
===================================================================
--- proto_draw.c	(revision 18083)
+++ proto_draw.c	(working copy)
@@ -806,7 +806,7 @@
 }
 
 void
-copy_hex_cb(GtkWidget * w _U_, gpointer data _U_)
+copy_hex_cb(GtkWidget * w _U_, gpointer data _U_, int data_type)
 {
 	GtkWidget *bv;
 	int len;
@@ -814,6 +814,7 @@
 	const guint8 *data_p = NULL;
 	GString *ASCII_representation = g_string_new("");
 	GString *byte_str = g_string_new("");
+	GString *text_str = g_string_new("");
 
 	bv = get_notebook_bv_ptr(byte_nb_ptr);
 	if (bv == NULL) {
@@ -823,15 +824,31 @@
 	}
 
 	data_p = get_byte_view_data_and_length(GTK_WIDGET(bv), &len);
-
-	g_string_sprintfa(byte_str,"%04x  ",i); /* Offset 0000 */
+        
+    g_string_sprintfa(byte_str,"%04x  ",i); /* Offset 0000 */
 	for (i=0; i<len; i++){
-	  g_string_sprintfa(ASCII_representation,"%c",isprint(*data_p) ? *data_p : '.');
-	  g_string_sprintfa(byte_str," %02x",*data_p++);
-	  if ((i+1)%16==0 && i!=0){
-	    g_string_sprintfa(byte_str,"  %s\n%04x  ",ASCII_representation->str,i+1);
-	    g_string_assign (ASCII_representation,"");
-	  }
+        if (data_type==1) {
+            if (isprint(*data_p)) {
+                g_string_sprintfa(ASCII_representation,"%c", *data_p);
+            }
+            else
+            {
+                if (*data_p==0x0a) {
+                    g_string_sprintfa(ASCII_representation,"\n");
+                }
+            }
+        }
+        else
+        {
+            g_string_sprintfa(ASCII_representation,"%c",isprint(*data_p) ? *data_p : '.');
+        }
+        g_string_sprintfa(byte_str," %02x",*data_p++);
+        if ((i+1)%16==0 && i!=0){
+            g_string_sprintfa(byte_str,"  %s\n%04x  ",ASCII_representation->str,i+1);
+            g_string_sprintfa(text_str,"%s",ASCII_representation->str);
+            
+            g_string_assign (ASCII_representation,"");
+        }
 	}
 
 	if(ASCII_representation->len){
@@ -839,10 +856,18 @@
 	    g_string_sprintfa(byte_str,"   ");
 	  }
 	  g_string_sprintfa(byte_str,"  %s\n",ASCII_representation->str);
+	  g_string_sprintfa(text_str,"%s",ASCII_representation->str);
 	}
 	/* Now that we have the byte data, copy it into the default clipboard */
+    if (data_type==1) {
+        copy_to_clipboard(text_str);
+    }
+    else
+    {
         copy_to_clipboard(byte_str);
+    }
 	g_string_free(byte_str, TRUE);                       /* Free the memory */
+	g_string_free(text_str, TRUE);                       /* Free the memory */
 	g_string_free(ASCII_representation, TRUE);           /* Free the memory */
 }
 
Index: proto_draw.h
===================================================================
--- proto_draw.h	(revision 18083)
+++ proto_draw.h	(working copy)
@@ -114,7 +114,7 @@
  * @param w unused
  * @param data unused
  */
-extern void copy_hex_cb(GtkWidget * w, gpointer data);
+extern void copy_hex_cb(GtkWidget * w, gpointer data, int data_type);
 
 #if GTK_MAJOR_VERSION < 2
 /** Redraw a given byte view window.
Index: main.c
===================================================================
--- main.c	(revision 18083)
+++ main.c	(working copy)
@@ -431,8 +431,38 @@
         get_text_from_packet_list(data));
 }
 
+/* This function allows users to right click in the details window and copy the text
+ * information to the operating systems clipboard. 
+ *
+ * We first check to see if a string representation is setup in the tree and then
+ * read the string. If not available then we try to grab the value. If all else
+ * fails we display a message to the user to indicate the copy could not be completed.
+ */
+void
+copy_selected_plist_cb(GtkWidget *w _U_, gpointer data _U_)
+{
+	GString *gtk_text_str = g_string_new("");
+    char labelstring[256];
+    char *stringpointer = labelstring;
 
+    if (cfile.finfo_selected->rep->representation != 0) {
+        g_string_sprintfa(gtk_text_str, "%s", cfile.finfo_selected->rep->representation);   /* Get the represented data */
+    }
+    if (gtk_text_str->len == 0) {                                                           /* If no representation then... */
+        proto_item_fill_label(cfile.finfo_selected, stringpointer);                         /* Try to read the value */
+        g_string_sprintfa(gtk_text_str, "%s", stringpointer);
+    }
+    if (gtk_text_str->len == 0) {                                                           /* Could not get item so display error msg */
+        simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Could not acquire information to copy, try expanding or choosing another item");
+    }
+    else
+    {
+        copy_to_clipboard(gtk_text_str);                     /* Copy string to clipboard */
+    }
+    g_string_free(gtk_text_str, TRUE);                       /* Free the memory */
+}
 
+
 /* XXX: use a preference for this setting! */
 static guint dfilter_combo_max_recent = 10;
 
Index: main.h
===================================================================
--- main.h	(revision 18083)
+++ main.h	(working copy)
@@ -122,6 +122,13 @@
 /** "bitwise or" this with MATCH_SELECTED_E value for instant apply instead of prepare only */
 #define MATCH_SELECTED_APPLY_NOW    0x100
 
+/** User highlited item in details window and then right clicked and selected the copy option 
+ *
+ * @param widget parent widget
+ * @param data parent widget
+ */
+extern void copy_selected_plist_cb(GtkWidget *w _U_, gpointer data);
+
 /** User requested one of "Apply as Filter" or "Prepare a Filter" functions 
  *  by menu or context menu of protocol tree.
  *
Index: menu.c
===================================================================
--- menu.c	(revision 18083)
+++ menu.c	(working copy)
@@ -474,6 +474,10 @@
 
 static GtkItemFactoryEntry tree_view_menu_items[] =
 {
+
+    ITEM_FACTORY_ENTRY("/Copy", NULL, copy_selected_plist_cb, 0, NULL, NULL),
+    ITEM_FACTORY_ENTRY("/<separator>", NULL, NULL, 0, "<Separator>", NULL),
+
     ITEM_FACTORY_ENTRY("/Expand Subtrees", NULL, expand_tree_cb, 0, NULL, NULL),
     ITEM_FACTORY_ENTRY("/Expand All", NULL, expand_all_cb, 0, NULL, NULL),
     ITEM_FACTORY_ENTRY("/Collapse All", NULL, collapse_all_cb, 0, NULL, NULL),
@@ -529,8 +533,11 @@
 
 static GtkItemFactoryEntry hexdump_menu_items[] =
 {
-    ITEM_FACTORY_ENTRY("/Copy", NULL, copy_hex_cb,
+    ITEM_FACTORY_ENTRY("/Copy", NULL, NULL, 0, "<Branch>", NULL),
+    ITEM_FACTORY_ENTRY("/Copy/...All Information", NULL, copy_hex_cb,
                        0, NULL, NULL),
+    ITEM_FACTORY_ENTRY("/Copy/...Text Only", NULL, copy_hex_cb,
+                       1, NULL, NULL),
     ITEM_FACTORY_ENTRY("/Export Selected Packet Bytes...", NULL, savehex_cb,
                        0, NULL, NULL),
 };