[gnome-logs/wip/current-boot: 1/2] Add GlEventViewRow



commit 5ce7595da4da2fd1ecbf4a63addf9da288515e8d
Author: David King <davidk gnome org>
Date:   Fri Nov 1 12:55:37 2013 +0000

    Add GlEventViewRow
    
    Add a dedicated object for listbox rows.

 Makefile.am           |    3 +
 src/gl-eventviewrow.c |  292 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/gl-eventviewrow.h |   63 +++++++++++
 3 files changed, 358 insertions(+), 0 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 74c4ee7..7b6d52b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -24,6 +24,7 @@ gnome_logs_SOURCES = \
        src/gl-categorylist.c \
        src/gl-eventtoolbar.c \
        src/gl-eventview.c \
+       src/gl-eventviewrow.c \
        src/gl-journal.c \
        src/gl-main.c \
        src/gl-util.c \
@@ -40,6 +41,7 @@ enum_data = \
 gnome_logs_enum_headers = \
        $(srcdir)/src/gl-eventtoolbar.h \
        $(srcdir)/src/gl-eventview.h \
+       $(srcdir)/src/gl-eventviewrow.h \
        $(srcdir)/src/gl-util.h
 
 noinst_gnome_logs_headers = \
@@ -47,6 +49,7 @@ noinst_gnome_logs_headers = \
        src/gl-categorylist.h \
        src/gl-eventtoolbar.h \
        src/gl-eventview.h \
+       src/gl-eventviewrow.h \
        src/gl-journal.h \
        src/gl-util.h \
        src/gl-window.h
diff --git a/src/gl-eventviewrow.c b/src/gl-eventviewrow.c
new file mode 100644
index 0000000..79711d0
--- /dev/null
+++ b/src/gl-eventviewrow.c
@@ -0,0 +1,292 @@
+/*
+ *  GNOME Logs - View and search logs
+ *  Copyright (C) 2013  Red Hat, Inc.
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "gl-eventviewrow.h"
+
+#include <glib/gi18n.h>
+#include <glib-unix.h>
+#include <stdlib.h>
+#include <systemd/sd-journal.h>
+
+#include "gl-enums.h"
+
+enum
+{
+    PROP_0,
+    PROP_CLOCK_FORMAT,
+    PROP_RESULT,
+    PROP_STYLE,
+    N_PROPERTIES
+};
+
+typedef struct
+{
+    GlUtilClockFormat clock_format;
+    GlJournalResult *result;
+    GlEventViewRowStyle style;
+} GlEventViewRowPrivate;
+
+G_DEFINE_TYPE_WITH_PRIVATE (GlEventViewRow, gl_event_view_row, GTK_TYPE_LIST_BOX_ROW)
+
+static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
+
+static void
+gl_event_view_row_finalize (GObject *object)
+{
+    GlEventViewRow *row = GL_EVENT_VIEW_ROW (object);
+    GlEventViewRowPrivate *priv = gl_event_view_row_get_instance_private (row);
+
+    g_clear_pointer (&priv->result, gl_journal_result_unref);
+}
+
+static void
+gl_event_view_row_get_property (GObject *object,
+                                guint prop_id,
+                                GValue *value,
+                                GParamSpec *pspec)
+{
+    GlEventViewRow *row = GL_EVENT_VIEW_ROW (object);
+    GlEventViewRowPrivate *priv = gl_event_view_row_get_instance_private (row);
+
+    switch (prop_id)
+    {
+        case PROP_CLOCK_FORMAT:
+            g_value_set_enum (value, priv->clock_format);
+            break;
+        case PROP_RESULT:
+            g_value_set_boxed (value, priv->result);
+            break;
+        case PROP_STYLE:
+            g_value_set_enum (value, priv->style);
+            break;
+        default:
+            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+            break;
+    }
+}
+
+static void
+gl_event_view_row_set_property (GObject *object,
+                                guint prop_id,
+                                const GValue *value,
+                                GParamSpec *pspec)
+{
+    GlEventViewRow *row = GL_EVENT_VIEW_ROW (object);
+    GlEventViewRowPrivate *priv = gl_event_view_row_get_instance_private (row);
+
+    switch (prop_id)
+    {
+        case PROP_CLOCK_FORMAT:
+            priv->clock_format = g_value_get_enum (value);
+            break;
+        case PROP_RESULT:
+            priv->result = g_value_dup_boxed (value);
+            break;
+        case PROP_STYLE:
+            priv->style = g_value_get_enum (value);
+            break;
+        default:
+            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+            break;
+    }
+}
+
+static void
+gl_event_view_row_create_cmdline (GlEventViewRow *row)
+{
+    GlEventViewRowPrivate *priv;
+    GtkStyleContext *context;
+    GtkWidget *grid;
+    gchar *markup;
+    GtkWidget *label;
+    gchar *time;
+    gboolean rtl;
+    GtkWidget *image;
+    GlJournalResult *result;
+
+    priv = gl_event_view_row_get_instance_private (row);
+    result = priv->result;
+
+    rtl = (gtk_widget_get_default_direction () == GTK_TEXT_DIR_RTL);
+
+    context = gtk_widget_get_style_context (GTK_WIDGET (row));
+    gtk_style_context_add_class (context, "event");
+    grid = gtk_grid_new ();
+    gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
+    gtk_container_add (GTK_CONTAINER (row), grid);
+
+    markup = g_markup_printf_escaped ("<b>%s</b>",
+                                      result->comm ? result->comm : "");
+    label = gtk_label_new (NULL);
+    gtk_widget_set_direction (label, GTK_TEXT_DIR_LTR);
+    gtk_widget_set_hexpand (label, TRUE);
+    gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
+    gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
+    gtk_label_set_markup (GTK_LABEL (label), markup);
+    g_free (markup);
+    gtk_grid_attach (GTK_GRID (grid), label, rtl ? 1 : 0, 0, 1, 1);
+
+    label = gtk_label_new (result->message);
+    gtk_widget_set_direction (label, GTK_TEXT_DIR_LTR);
+    context = gtk_widget_get_style_context (GTK_WIDGET (label));
+    gtk_style_context_add_class (context, "event-monospace");
+    gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
+    gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
+    gtk_grid_attach (GTK_GRID (grid), label, 0, 1, 2, 1);
+
+    time = gl_util_timestamp_to_display (result->timestamp,
+                                         priv->clock_format);
+    label = gtk_label_new (time);
+    context = gtk_widget_get_style_context (GTK_WIDGET (label));
+    gtk_style_context_add_class (context, "dim-label");
+    gtk_style_context_add_class (context, "event-time");
+    gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
+    gtk_grid_attach (GTK_GRID (grid), label, rtl ? 0 : 1, 0, 1, 1);
+
+    image = gtk_image_new_from_icon_name (rtl ? "go-next-rtl-symbolic"
+                                              : "go-next-symbolic",
+                                          GTK_ICON_SIZE_MENU);
+    gtk_grid_attach (GTK_GRID (grid), image, 2, 0, 1, 2);
+
+    g_free (time);
+}
+
+static void
+gl_event_view_row_create_simple (GlEventViewRow *row)
+{
+    GlEventViewRowPrivate *priv;
+    GtkStyleContext *context;
+    GtkWidget *grid;
+    GtkWidget *label;
+    gchar *time;
+    gboolean rtl;
+    GtkWidget *image;
+    GlJournalResult *result;
+
+    priv = gl_event_view_row_get_instance_private (row);
+    result = priv->result;
+
+    rtl = (gtk_widget_get_default_direction () == GTK_TEXT_DIR_RTL);
+
+    context = gtk_widget_get_style_context (GTK_WIDGET (row));
+    gtk_style_context_add_class (context, "event");
+    grid = gtk_grid_new ();
+    gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
+    gtk_container_add (GTK_CONTAINER (row), grid);
+
+    label = gtk_label_new (result->message);
+    gtk_widget_set_direction (label, GTK_TEXT_DIR_LTR);
+    context = gtk_widget_get_style_context (GTK_WIDGET (label));
+    gtk_style_context_add_class (context, "event-monospace");
+    gtk_widget_set_hexpand (label, TRUE);
+    gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
+    gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
+    gtk_grid_attach (GTK_GRID (grid), label, 0, 1, 1, 1);
+
+    time = gl_util_timestamp_to_display (result->timestamp,
+                                         priv->clock_format);
+    label = gtk_label_new (time);
+    context = gtk_widget_get_style_context (GTK_WIDGET (label));
+    gtk_style_context_add_class (context, "dim-label");
+    gtk_style_context_add_class (context, "event-time");
+    gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
+    gtk_grid_attach (GTK_GRID (grid), label, rtl ? 1 : 0, 0, 1, 1);
+
+    image = gtk_image_new_from_icon_name (rtl ? "go-next-rtl-symbolic"
+                                              : "go-next-symbolic",
+                                          GTK_ICON_SIZE_MENU);
+    gtk_grid_attach (GTK_GRID (grid), image, 1, 0, 1, 2);
+
+    g_free (time);
+}
+
+static void
+gl_event_view_row_constructed (GObject *object)
+{
+    GlEventViewRow *row = GL_EVENT_VIEW_ROW (object);
+    GlEventViewRowPrivate *priv;
+
+    priv = gl_event_view_row_get_instance_private (row);
+
+    /* contruct-only properties have already been set. */
+    switch (priv->style)
+    {
+        case GL_EVENT_VIEW_ROW_STYLE_CMDLINE:
+            gl_event_view_row_create_cmdline (row);
+            break;
+        case GL_EVENT_VIEW_ROW_STYLE_SIMPLE:
+            gl_event_view_row_create_simple (row);
+            break;
+        default:
+            g_assert_not_reached ();
+    }
+
+    G_OBJECT_CLASS (gl_event_view_row_parent_class)->constructed (object);
+}
+
+static void
+gl_event_view_row_class_init (GlEventViewRowClass *klass)
+{
+    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+    gobject_class->constructed = gl_event_view_row_constructed;
+    gobject_class->finalize = gl_event_view_row_finalize;
+    gobject_class->get_property = gl_event_view_row_get_property;
+    gobject_class->set_property = gl_event_view_row_set_property;
+
+    obj_properties[PROP_CLOCK_FORMAT] = g_param_spec_enum ("clock-format", "Clock format",
+                                                           "Format of the clock in which to show timestamps",
+                                                           GL_TYPE_UTIL_CLOCK_FORMAT,
+                                                           GL_UTIL_CLOCK_FORMAT_24HR,
+                                                           G_PARAM_READWRITE |
+                                                           G_PARAM_CONSTRUCT_ONLY |
+                                                           G_PARAM_STATIC_STRINGS);
+
+    obj_properties[PROP_RESULT] = g_param_spec_boxed ("result", "Result",
+                                                      "Journal query result for this row",
+                                                      GL_TYPE_JOURNAL_RESULT,
+                                                      G_PARAM_READWRITE |
+                                                      G_PARAM_CONSTRUCT_ONLY |
+                                                      G_PARAM_STATIC_STRINGS);
+
+    obj_properties[PROP_STYLE] = g_param_spec_enum ("style", "Style",
+                                                    "Row display style",
+                                                    GL_TYPE_EVENT_VIEW_ROW_STYLE,
+                                                    GL_EVENT_VIEW_ROW_STYLE_CMDLINE,
+                                                    G_PARAM_READWRITE |
+                                                    G_PARAM_CONSTRUCT_ONLY |
+                                                    G_PARAM_STATIC_STRINGS);
+
+    g_object_class_install_properties (gobject_class, N_PROPERTIES,
+                                       obj_properties);
+}
+
+static void
+gl_event_view_row_init (GlEventViewRow *row)
+{
+    /* See gl_event_view_row_constructed (). */
+}
+
+GtkWidget *
+gl_event_view_row_new (GlJournalResult *result,
+                       GlEventViewRowStyle style,
+                       GlUtilClockFormat clock_format)
+{
+    return g_object_new (GL_TYPE_EVENT_VIEW_ROW, "style", style, "result",
+                         result, "clock-format", clock_format, NULL);
+}
diff --git a/src/gl-eventviewrow.h b/src/gl-eventviewrow.h
new file mode 100644
index 0000000..8b7d42c
--- /dev/null
+++ b/src/gl-eventviewrow.h
@@ -0,0 +1,63 @@
+/*
+ *  GNOME Logs - View and search logs
+ *  Copyright (C) 2013  Red Hat, Inc.
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GL_EVENT_VIEW_ROW_H_
+#define GL_EVENT_VIEW_ROW_H_
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#include "gl-journal.h"
+#include "gl-util.h"
+
+typedef struct
+{
+    /*< private >*/
+    GtkListBoxRow parent_instance;
+} GlEventViewRow;
+
+typedef struct
+{
+    /*< private >*/
+    GtkListBoxRowClass parent_class;
+} GlEventViewRowClass;
+
+/*
+ * GlEventViewRowStyle:
+ * @GL_EVENT_VIEW_ROW_STYLE_CMDLINE: show the command-line in bold, if it
+ * exists, as well as the log message
+ * @GL_EVENT_VIEW_ROW_STYLE_SIMPLE: show only the event message and timestamp
+ *
+ * The style for the row.
+ */
+typedef enum
+{
+    GL_EVENT_VIEW_ROW_STYLE_CMDLINE,
+    GL_EVENT_VIEW_ROW_STYLE_SIMPLE
+} GlEventViewRowStyle;
+
+#define GL_TYPE_EVENT_VIEW_ROW (gl_event_view_row_get_type ())
+#define GL_EVENT_VIEW_ROW(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GL_TYPE_EVENT_VIEW_ROW, 
GlEventViewRow))
+
+GType gl_event_view_row_get_type (void);
+GtkWidget * gl_event_view_row_new (GlJournalResult *result, GlEventViewRowStyle style, GlUtilClockFormat 
clock_format);
+
+G_END_DECLS
+
+#endif /* GL_EVENT_VIEW_ROW_H_ */


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