[gssdp/wip/modern-gtk: 5/6] wip: Move window menu to sniffer ui




commit 8e6e0ca1b6bbfcd9fd653b65a313dbf3d8df6278
Author: Jens Georg <mail jensge org>
Date:   Tue Oct 6 23:22:33 2020 +0200

    wip: Move window menu to sniffer ui

 tools/main-window.c  | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/main-window.ui |   4 +-
 2 files changed, 116 insertions(+), 2 deletions(-)
---
diff --git a/tools/main-window.c b/tools/main-window.c
index 3340f67..689af7d 100644
--- a/tools/main-window.c
+++ b/tools/main-window.c
@@ -4,8 +4,24 @@
 
 #include <glib.h>
 
+#include <libsoup/soup.h>
+
+typedef enum
+{
+        PACKET_STORE_COLUMN_TIME,
+        PACKET_STORE_COLUMN_IP,
+        PACKET_STORE_COLUMN_INTERFACE,
+        PACKET_STORE_COLUMN_PACKET_TYPE,
+        PACKET_STORE_COLUMN_TARGET,
+        PACKET_STORE_COLUMN_HEADERS,
+        PACKET_STORE_COLUMN_RAW_ARRIVAL_TIME
+} PACKET_STORE_COLUMNS;
+
 struct _GSSDPDeviceSnifferMainWindow {
         GtkApplicationWindow parent_instance;
+
+        GtkWidget *packet_treeview;
+        GtkWidget *packet_textview;
 };
 
 G_DEFINE_TYPE (GSSDPDeviceSnifferMainWindow,
@@ -21,6 +37,12 @@ gssdp_device_sniffer_main_window_class_init (
         gtk_widget_class_set_template_from_resource (
                 widget_class,
                 "/org/gupnp/GSSDP/MainWindow.ui");
+        gtk_widget_class_bind_template_child (widget_class,
+                                              GSSDPDeviceSnifferMainWindow,
+                                              packet_treeview);
+        gtk_widget_class_bind_template_child (widget_class,
+                                              GSSDPDeviceSnifferMainWindow,
+                                              packet_textview);
 }
 
 static void
@@ -53,6 +75,91 @@ on_realize (GtkWidget *self, gpointer user_data)
                                      window_height);
 }
 
+static void
+packet_header_to_string (const char *header_name,
+                         const char *header_val,
+                         GString **text)
+{
+        g_string_append_printf (*text, "%s: %s\n", header_name, header_val);
+}
+
+static void
+clear_textbuffer (GtkTextBuffer *textbuffer)
+{
+        GtkTextIter start, end;
+
+        gtk_text_buffer_get_bounds (textbuffer, &start, &end);
+        gtk_text_buffer_delete (textbuffer, &start, &end);
+}
+
+static void
+update_packet_details (GSSDPDeviceSnifferMainWindow *self,
+                       const char *text,
+                       int len)
+{
+        GtkTextBuffer *textbuffer;
+
+        textbuffer = gtk_text_view_get_buffer (
+                GTK_TEXT_VIEW (self->packet_textview));
+
+        clear_textbuffer (textbuffer);
+        gtk_text_buffer_insert_at_cursor (textbuffer, text, len);
+}
+
+static void
+display_packet (GSSDPDeviceSnifferMainWindow *self,
+                GDateTime *arrival_time,
+                SoupMessageHeaders *packet_headers)
+{
+        GString *text;
+        char *time = NULL;
+
+        time = g_date_time_format_iso8601 (arrival_time);
+        text = g_string_new ("Received on: ");
+        g_string_append (text, time);
+        g_string_append (text, "\nHeaders:\n\n");
+        g_free (time);
+
+        soup_message_headers_foreach (
+                packet_headers,
+                (SoupMessageHeadersForeachFunc) packet_header_to_string,
+                &text);
+
+        update_packet_details (self, text->str, text->len);
+        g_string_free (text, TRUE);
+}
+
+static void
+on_packet_selected (GtkTreeSelection *selection, gpointer user_data)
+{
+        GtkTreeModel *model;
+        GtkTreeIter iter;
+        GDateTime *arrival_time;
+
+        if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
+                SoupMessageHeaders *packet_headers;
+
+                gtk_tree_model_get (model,
+                                    &iter,
+                                    PACKET_STORE_COLUMN_HEADERS,
+                                    &packet_headers,
+                                    PACKET_STORE_COLUMN_RAW_ARRIVAL_TIME,
+                                    &arrival_time,
+                                    -1);
+                display_packet (GSSDP_DEVICE_SNIFFER_MAIN_WINDOW (user_data),
+                                arrival_time,
+                                packet_headers);
+                g_boxed_free (SOUP_TYPE_MESSAGE_HEADERS, packet_headers);
+                g_date_time_unref (arrival_time);
+        }
+
+        else
+                update_packet_details (
+                        GSSDP_DEVICE_SNIFFER_MAIN_WINDOW (user_data),
+                        "",
+                        0);
+}
+
 static void
 gssdp_device_sniffer_main_window_init (GSSDPDeviceSnifferMainWindow *self)
 {
@@ -62,4 +169,11 @@ gssdp_device_sniffer_main_window_init (GSSDPDeviceSnifferMainWindow *self)
                           "realize",
                           G_CALLBACK (on_realize),
                           NULL);
+
+        GtkTreeSelection *selection = gtk_tree_view_get_selection (
+                GTK_TREE_VIEW (self->packet_treeview));
+        g_signal_connect (G_OBJECT (selection),
+                          "changed",
+                          G_CALLBACK (on_packet_selected),
+                          self);
 }
diff --git a/tools/main-window.ui b/tools/main-window.ui
index d749021..99d88be 100644
--- a/tools/main-window.ui
+++ b/tools/main-window.ui
@@ -179,7 +179,7 @@
                 <property name="can_focus">True</property>
                 <property name="shadow_type">in</property>
                 <child>
-                  <object class="GtkTreeView" id="packet-treeview">
+                  <object class="GtkTreeView" id="packet_treeview">
                     <property name="visible">True</property>
                     <property name="can_focus">True</property>
                     <property name="model">packet_store</property>
@@ -256,7 +256,7 @@
                 <property name="can_focus">True</property>
                 <property name="shadow_type">in</property>
                 <child>
-                  <object class="GtkTextView" id="packet-details-textview">
+                  <object class="GtkTextView" id="packet_textview">
                     <property name="visible">True</property>
                     <property name="can_focus">True</property>
                     <property name="editable">False</property>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]