Ethereal-dev: [Ethereal-dev] show hostname in window title - feature/patch submission

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

From: Nathan Jennings <njen@xxxxxxxxxxxx>
Date: Tue, 09 Nov 2004 08:32:04 -0500
Hello,

I saw this request flow through a week or so ago. I had been meaning to do it for quite awhile now anyway...

The attached patch allows you to easily see which copy of Ethereal you're manipulating (by looking at the window title) if you have multiple Ethereal instances open on your desktop that are running on multiple machines. It doesn't handle differentiating multiple instances running on the same machine.

The patch also adds a user preference to show the local machine's host name in full or short format. The window titles it changes are the main window, the capture dialog and the capture info dialog.

I tested it on FreeBSD 4.9-RELEASE only.

-Nathan
Index: AUTHORS
===================================================================
--- AUTHORS	(revision 12488)
+++ AUTHORS	(working copy)
@@ -1931,7 +1931,7 @@
 	Support for saving RTP analysis data in CSV form
 }
 
-Nathan Jennings         <njen [AT] bellsouth.net> {
+Nathan Jennings         <njen [AT] triad.rr.com> {
 	Support for user-supplied interface descriptions
 	Support for hiding interfaces in drop-down list in capture
 	    dialog
Index: gtk/capture_info_dlg.c
===================================================================
--- gtk/capture_info_dlg.c	(revision 12488)
+++ gtk/capture_info_dlg.c	(working copy)
@@ -86,6 +86,7 @@
   GtkWidget         *counts_fr, *running_tb, *running_label, *bbox;
   capture_info_ui_t *info;
   gchar             *cap_w_title;
+  gchar             *hostname;
 
   info = g_malloc0(sizeof(capture_info_ui_t));
   info->counts[0].title = "Total";
@@ -113,7 +114,13 @@
   info->counts[11].title = "Other";
   info->counts[11].value_ptr = &(cinfo->counts->other);
 
-  cap_w_title = g_strdup_printf("Ethereal: Capture - Interface %s", iface);
+  /* if use host name in title preference is set */
+  if ((hostname = get_window_hostname()) != NULL) {
+    cap_w_title = g_strdup_printf("Ethereal: Capture - Interface %s %s", iface, hostname);
+    g_free(hostname);
+  }
+  else
+    cap_w_title = g_strdup_printf("Ethereal: Capture - Interface %s", iface);
 
   info->cap_w = dlg_window_new(cap_w_title);
   g_free(cap_w_title);
Index: gtk/gui_prefs.c
===================================================================
--- gtk/gui_prefs.c	(revision 12488)
+++ gtk/gui_prefs.c	(working copy)
@@ -56,6 +56,7 @@
 static void fileopen_selected_cb(GtkWidget *mybutton_rb _U_, gpointer parent_w);
 static gint recent_files_count_changed_cb(GtkWidget *recent_files_entry _U_, 
 					  GdkEvent *event _U_, gpointer parent_w);
+static void show_hostname_selected_cb(GtkWidget *mybutton_rb _U_, gpointer parent_w);
 
 #define SCROLLBAR_PLACEMENT_KEY		"scrollbar_placement"
 #define PLIST_SEL_BROWSE_KEY		"plist_sel_browse"
@@ -80,6 +81,7 @@
 #define GUI_ASK_UNSAVED_KEY     "ask_unsaved"
 #define GUI_WEBBROWSER_KEY	    "webbrowser"
 #define GUI_FIND_WRAP_KEY       "find_wrap"
+#define GUI_SHOW_HOSTNAME_KEY   "show_hostname"
 
 #define GUI_TOOLBAR_STYLE_KEY	"toolbar_style"
 
@@ -154,6 +156,13 @@
 	{ NULL,          NULL,                      0 }
 };
 
+static const enum_val_t gui_show_hostname_vals[] = {
+	{ "NO",    "No",         SHOW_HOSTNAME_NO },
+	{ "FULL",  "Full name",  SHOW_HOSTNAME_FULL },
+	{ "SHORT", "Short name", SHOW_HOSTNAME_SHORT },
+	{ NULL,    NULL,         0 }
+};
+
 /* Set to FALSE initially; set to TRUE if the user ever hits "OK" on
    the "Font..." dialog, so that we know that they (probably) changed
    the font, and therefore that the "apply" function needs to take care
@@ -173,9 +182,9 @@
 static char open_file_preview_str[128] = "";
 
 #if GTK_MAJOR_VERSION < 2
+#define GUI_TABLE_ROWS 11
+#else
 #define GUI_TABLE_ROWS 10
-#else
-#define GUI_TABLE_ROWS 9
 #endif
 
 GtkWidget*
@@ -193,6 +202,7 @@
 	GtkWidget *recent_files_count_max_te, *ask_unsaved_cb, *find_wrap_cb;
     GtkWidget *webbrowser_te;
 	GtkWidget *save_position_cb, *save_size_cb, *save_maximized_cb;
+    GtkWidget *show_hostname_rb;
 #if GTK_MAJOR_VERSION < 2
 	GtkWidget *expander_style_om, *line_style_om;
 #else
@@ -350,6 +360,12 @@
 	    OBJECT_SET_DATA(main_vb, GUI_WEBBROWSER_KEY, webbrowser_te);
     }
 
+	/* show host name in window title? */
+    show_hostname_rb = create_preference_radio_buttons(main_tb, pos++,
+        "Show host name in window title:", NULL, gui_show_hostname_vals,
+        prefs.gui_show_hostname);
+    OBJECT_SET_DATA(main_vb, GUI_SHOW_HOSTNAME_KEY, show_hostname_rb);
+    SIGNAL_CONNECT(show_hostname_rb, "clicked", show_hostname_selected_cb, main_vb);
 
 	/* Show 'em what we got */
 	gtk_widget_show_all(main_vb);
@@ -456,13 +472,16 @@
     prefs.gui_find_wrap = 
 	    gtk_toggle_button_get_active(OBJECT_GET_DATA(w, GUI_FIND_WRAP_KEY));
     
-    if(browser_needs_pref()) {
+    if (browser_needs_pref()) {
 		g_free(prefs.gui_webbrowser);
 	    prefs.gui_webbrowser = g_strdup(gtk_entry_get_text(
 		    GTK_ENTRY(OBJECT_GET_DATA(w, GUI_WEBBROWSER_KEY))));
     }
 
+    prefs.gui_show_hostname = fetch_preference_radio_buttons_val(
+        OBJECT_GET_DATA(w, GUI_SHOW_HOSTNAME_KEY), gui_show_hostname_vals);
 
+
 	/*
 	 * XXX - we need to have a way to fetch the preferences into
 	 * local storage and only set the permanent preferences if there
@@ -631,3 +650,14 @@
     }
     return;
 }
+
+static void
+show_hostname_selected_cb(GtkWidget *mybutton_rb _U_, gpointer parent_w)
+{
+	GtkWidget *show_hostname_rb;
+
+	show_hostname_rb = (GtkWidget *)OBJECT_GET_DATA(parent_w, GUI_SHOW_HOSTNAME_KEY);
+	prefs.gui_show_hostname = fetch_preference_radio_buttons_val(
+		show_hostname_rb, gui_show_hostname_vals);
+return;
+}
Index: gtk/capture_dlg.c
===================================================================
--- gtk/capture_dlg.c	(revision 12488)
+++ gtk/capture_dlg.c	(working copy)
@@ -527,6 +527,8 @@
   GtkWidget     *buffer_size_lb, *buffer_size_sb;
 #endif
   guint32       value;
+  gchar         *cap_title;
+  gchar         *hostname;
 
   if (cap_open_w != NULL) {
     /* There's already a "Capture Options" dialog box; reactivate it. */
@@ -566,8 +568,17 @@
     g_free(cant_get_if_list_errstr);
   }
 
-  cap_open_w = dlg_window_new("Ethereal: Capture Options");
+  /* if use host name in title preference is set */
+  if ((hostname = get_window_hostname()) != NULL) {
+    cap_title = g_strdup_printf("Ethereal: Capture Options %s", hostname);
+    g_free(hostname);
+  }
+  else
+    cap_title = g_strdup_printf("Ethereal: Capture Options");
 
+  cap_open_w = dlg_window_new(cap_title);
+  g_free(cap_title);
+
   tooltips = gtk_tooltips_new();
 
 #if GTK_MAJOR_VERSION < 2
Index: gtk/main.c
===================================================================
--- gtk/main.c	(revision 12488)
+++ gtk/main.c	(working copy)
@@ -2943,6 +2943,8 @@
     GList         *filter_list = NULL;
     GtkTooltips   *tooltips;
     GtkAccelGroup *accel;
+	gchar         *title;
+	gchar         *hostname;
     /* Display filter construct dialog has an Apply button, and "OK" not
        only sets our text widget, it activates it (i.e., it causes us to
        filter the capture). */
@@ -2952,8 +2954,17 @@
         TRUE
     };
 
+    /* if use host name in title preference is set */
+    if ((hostname = get_window_hostname()) != NULL) {
+        title = g_strdup_printf("The Ethereal Network Analyzer %s", hostname);
+        g_free(hostname);
+    }
+	else
+        title = g_strdup_printf("The Ethereal Network Analyzer");
+
     /* Main window */
-    top_level = window_new(GTK_WINDOW_TOPLEVEL, "The Ethereal Network Analyzer");
+    top_level = window_new(GTK_WINDOW_TOPLEVEL, title);
+    g_free(title);
 
     tooltips = gtk_tooltips_new();
 
Index: gtk/ui_util.c
===================================================================
--- gtk/ui_util.c	(revision 12488)
+++ gtk/ui_util.c	(working copy)
@@ -30,12 +30,17 @@
 
 #ifdef _WIN32
 #include <windows.h>
+#include <winsock2.h>	/* gethostname */
 #endif
 
 #ifdef HAVE_IO_H
 # include <io.h>
 #endif
 
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>	/* gethostname */
+#endif
+
 #include <gtk/gtk.h>
 #include <gdk/gdkkeysyms.h>
 
@@ -570,7 +575,19 @@
 void
 set_main_window_name(gchar *window_name)
 {
-  gtk_window_set_title(GTK_WINDOW(top_level), window_name);
+  gchar *hostname;
+  gchar *title;
+
+  /* if use host name in title preference is set */
+  if ((hostname = get_window_hostname()) != NULL) {
+    title = g_strdup_printf("%s %s", window_name, hostname);
+    gtk_window_set_title(GTK_WINDOW(top_level), title);
+    g_free(title);
+    g_free(hostname);
+  }
+  else
+    gtk_window_set_title(GTK_WINDOW(top_level), window_name);
+
   gdk_window_set_icon_name(top_level->window, window_name);
 }
 
@@ -1013,3 +1030,33 @@
         gtk_editable_copy_clipboard((GtkEditable *)text); /* Copy the byte data into the clipboard */
 #endif
 }
+
+/* Get host name for use in window titles. */
+gchar *
+get_window_hostname(void)
+{
+
+	char hostname[256];
+	char *p;
+	gchar *hnf;
+
+	/* no host name preference */
+	if (prefs.gui_show_hostname == SHOW_HOSTNAME_NO)
+		return NULL;
+
+	/* get it */
+	if (gethostname(hostname, 256) != 0)
+		return NULL;
+
+	/* omit domain */
+	if (prefs.gui_show_hostname == SHOW_HOSTNAME_SHORT) {
+		if ((p = strchr(hostname, '.')) != NULL)
+			*p = '\0';
+	}
+
+	hnf = g_strdup_printf("[%s]", hostname);
+	if (hnf == NULL)
+		return NULL;
+
+return hnf;
+}
Index: gtk/ui_util.h
===================================================================
--- gtk/ui_util.h	(revision 12488)
+++ gtk/ui_util.h	(working copy)
@@ -297,4 +297,12 @@
  */
 extern void copy_to_clipboard(GString *str);  
 
+/** Get the local machine's host name for use in window titles.
+ *  The host name format (full or short name) is based upon user preference.
+ *
+ * @return the newly created host name string
+ */
+extern gchar *get_window_hostname(void);
+
+
 #endif /* __GTKGUIUI_UTIL_H__ */
Index: epan/prefs.c
===================================================================
--- epan/prefs.c	(revision 12488)
+++ epan/prefs.c	(working copy)
@@ -1046,6 +1046,7 @@
     prefs.gui_ask_unsaved            = TRUE;
     prefs.gui_find_wrap              = TRUE;
     prefs.gui_webbrowser             = g_strdup("mozilla %s");
+    prefs.gui_show_hostname          = 0;
     prefs.gui_layout_type            = layout_type_5;
     prefs.gui_layout_content_1       = layout_pane_content_plist;
     prefs.gui_layout_content_2       = layout_pane_content_pdetails;
@@ -1360,6 +1361,7 @@
 #define PRS_GUI_TOOLBAR_MAIN_SHOW        "gui.toolbar_main_show"
 #define PRS_GUI_TOOLBAR_MAIN_STYLE       "gui.toolbar_main_style"
 #define PRS_GUI_WEBBROWSER               "gui.webbrowser"
+#define PRS_GUI_SHOW_HOSTNAME            "gui.show_hostname"
 #define PRS_GUI_LAYOUT_TYPE              "gui.layout_type"
 #define PRS_GUI_LAYOUT_CONTENT_1         "gui.layout_content_1"
 #define PRS_GUI_LAYOUT_CONTENT_2         "gui.layout_content_2"
@@ -1695,6 +1697,8 @@
   } else if (strcmp(pref_name, PRS_GUI_WEBBROWSER) == 0) {
     g_free(prefs.gui_webbrowser);
     prefs.gui_webbrowser = g_strdup(value);
+  } else if (strcmp(pref_name, PRS_GUI_SHOW_HOSTNAME) == 0) {
+    prefs.gui_show_hostname = strtol(value, NULL, 10);
   } else if (strcmp(pref_name, PRS_GUI_LAYOUT_TYPE) == 0) {
     prefs.gui_layout_type = strtoul(value, NULL, 10);
     if (prefs.gui_layout_type == layout_unused ||
@@ -2306,6 +2310,11 @@
   fprintf(pf, "# Ex: mozilla %%s\n");
   fprintf(pf, PRS_GUI_WEBBROWSER ": %s\n", prefs.gui_webbrowser);
 
+  fprintf(pf, "\n# Show host name in window title?\n");
+  fprintf(pf, "# A decimal number: 0 = no, 1 = full name, 2 = short name.\n");
+  fprintf(pf, PRS_GUI_SHOW_HOSTNAME ": %d\n",
+              prefs.gui_show_hostname);
+
   fprintf (pf, "\n######## User Interface: Layout ########\n");
 
   fprintf(pf, "\n# Layout type (1-6).\n");
@@ -2510,6 +2519,7 @@
   dest->gui_geometry_save_size = src->gui_geometry_save_size;
   dest->gui_geometry_save_maximized = src->gui_geometry_save_maximized;
   dest->gui_webbrowser = g_strdup(src->gui_webbrowser);
+  dest->gui_show_hostname = src->gui_show_hostname;
 /*  values for the capture dialog box */
   dest->capture_device = g_strdup(src->capture_device);
   dest->capture_devices_descr = g_strdup(src->capture_devices_descr);
Index: epan/prefs.h
===================================================================
--- epan/prefs.h	(revision 12488)
+++ epan/prefs.h	(working copy)
@@ -69,6 +69,13 @@
 #define TB_STYLE_BOTH		2
 
 /*
+ * Host name formats used in window titles.
+ */
+#define SHOW_HOSTNAME_NO	0
+#define SHOW_HOSTNAME_FULL	1
+#define SHOW_HOSTNAME_SHORT	2
+
+/*
  * Types of layout of summary/details/hex panes.
  */
 typedef enum {
@@ -134,6 +141,7 @@
   gboolean gui_ask_unsaved;
   gboolean gui_find_wrap;
   gchar   *gui_webbrowser;
+  gint     gui_show_hostname;
   layout_type_e gui_layout_type;
   layout_pane_content_e gui_layout_content_1;
   layout_pane_content_e gui_layout_content_2;