[gnome-logs] Move sorting from view to model



commit 4aecdf242964aa4c3817b565bb25c6708a29dbb5
Author: Pranav Ganorkar <pranavg189 gmail com>
Date:   Tue May 9 21:47:44 2017 +0530

    Move sorting from view to model
    
    The logs shown in GlEventViewList are now sorted from GlJournalModel.
    The logs can be sorted in ascending or descending order according
    to the timestamp. The sorting order can be controlled by setting
    the 'sort-order' GSettings key.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=767995

 src/gl-eventviewlist.c |   96 ++++++++---------------------------------------
 src/gl-journal-model.c |   35 +++++++++++++++--
 src/gl-journal-model.h |    6 +++
 3 files changed, 54 insertions(+), 83 deletions(-)
---
diff --git a/src/gl-eventviewlist.c b/src/gl-eventviewlist.c
index e9df282..6d96cf5 100644
--- a/src/gl-eventviewlist.c
+++ b/src/gl-eventviewlist.c
@@ -617,10 +617,16 @@ create_query_object (GlEventViewList *view)
     GlEventViewListPrivate *priv;
     GlQuery *query;
     GlCategoryList *list;
+    GSettings *settings;
+    gint sort_order;
 
     priv = gl_event_view_list_get_instance_private (view);
     list = GL_CATEGORY_LIST (priv->categories);
 
+    /* Get the sorting order from GSettings key */
+    settings = g_settings_new (SETTINGS_SCHEMA);
+    sort_order = g_settings_get_enum (settings, SORT_ORDER);
+
     /* Create new query object */
     query = gl_query_new ();
 
@@ -633,6 +639,10 @@ create_query_object (GlEventViewList *view)
 
     gl_query_set_search_type (query, priv->search_type);
 
+    gl_query_set_sort_order (query, sort_order);
+
+    g_object_unref (settings);
+
     return query;
 }
 
@@ -643,8 +653,6 @@ on_notify_category (GlCategoryList *list,
 {
     GlEventViewList *view;
     GlEventViewListPrivate *priv;
-    GSettings *settings;
-    gint sort_order;
     GlQuery *query;
 
     view = GL_EVENT_VIEW_LIST (user_data);
@@ -655,11 +663,6 @@ on_notify_category (GlCategoryList *list,
 
     /* Set the created query on the journal model */
     gl_journal_model_take_query (priv->journal_model, query);
-
-    settings = g_settings_new (SETTINGS_SCHEMA);
-    sort_order = g_settings_get_enum (settings, SORT_ORDER);
-    g_object_unref (settings);
-    gl_event_view_list_set_sort_order (view, sort_order);
 }
 
 void
@@ -680,89 +683,24 @@ gl_event_view_list_view_boot (GlEventViewList *view, const gchar *match)
     gl_search_popover_set_journal_timestamp_range_current_boot (popover);
 }
 
-static gint
-gl_event_view_sort_by_ascending_time (GtkListBoxRow *row1,
-                                      GtkListBoxRow *row2)
-{
-    GlJournalEntry *entry1;
-    GlJournalEntry *entry2;
-    guint64 time1;
-    guint64 time2;
-
-    entry1 = gl_event_view_row_get_entry (GL_EVENT_VIEW_ROW (row1));
-    entry2 = gl_event_view_row_get_entry (GL_EVENT_VIEW_ROW (row2));
-    time1 = gl_journal_entry_get_timestamp (entry1);
-    time2 = gl_journal_entry_get_timestamp (entry2);
-
-    if (time1 > time2)
-    {
-        return 1;
-    }
-    else if (time1 < time2)
-    {
-        return -1;
-    }
-    else
-    {
-        return 0;
-    }
-}
-
-static gint
-gl_event_view_sort_by_descending_time (GtkListBoxRow *row1,
-                                       GtkListBoxRow *row2)
-{
-    GlJournalEntry *entry1;
-    GlJournalEntry *entry2;
-    guint64 time1;
-    guint64 time2;
-
-    entry1 = gl_event_view_row_get_entry (GL_EVENT_VIEW_ROW (row1));
-    entry2 = gl_event_view_row_get_entry (GL_EVENT_VIEW_ROW (row2));
-    time1 = gl_journal_entry_get_timestamp (entry1);
-    time2 = gl_journal_entry_get_timestamp (entry2);
-
-    if (time1 > time2)
-    {
-        return -1;
-    }
-    else if (time1 < time2)
-    {
-        return 1;
-    }
-    else
-    {
-        return 0;
-    }
-}
-
 void
 gl_event_view_list_set_sort_order (GlEventViewList *view,
                                    GlSortOrder sort_order)
 {
     GlEventViewListPrivate *priv;
+    GlQuery *query;
 
     g_return_if_fail (GL_EVENT_VIEW_LIST (view));
 
     priv = gl_event_view_list_get_instance_private (view);
 
-    switch (sort_order)
-    {
-        case GL_SORT_ORDER_ASCENDING_TIME:
-            gtk_list_box_set_sort_func (GTK_LIST_BOX (priv->entries_box),
-                                        (GtkListBoxSortFunc) gl_event_view_sort_by_ascending_time,
-                                        NULL, NULL);
-            break;
-        case GL_SORT_ORDER_DESCENDING_TIME:
-            gtk_list_box_set_sort_func (GTK_LIST_BOX (priv->entries_box),
-                                        (GtkListBoxSortFunc) gl_event_view_sort_by_descending_time,
-                                        NULL, NULL);
-            break;
-        default:
-            g_assert_not_reached ();
-            break;
-    }
+    /* Create the query object */
+    query = create_query_object (view);
 
+    /* The sort order is taken from the GSettings key irrespective
+     * of current model sort order and the created query is passed
+     * on to model */
+    gl_journal_model_take_query (priv->journal_model, query);
 }
 
 static void
diff --git a/src/gl-journal-model.c b/src/gl-journal-model.c
index 165315f..4eb745b 100644
--- a/src/gl-journal-model.c
+++ b/src/gl-journal-model.c
@@ -85,7 +85,15 @@ gl_journal_model_fetch_idle (gpointer user_data)
         {
             model->n_entries_to_fetch--;
             g_ptr_array_add (model->entries, entry);
-            g_list_model_items_changed (G_LIST_MODEL (model), last, 0, 1);
+
+            if (model->query->order == GL_SORT_ORDER_ASCENDING_TIME)
+            {
+                g_list_model_items_changed (G_LIST_MODEL (model), 0, 0, 1);
+            }
+            else
+            {
+                g_list_model_items_changed (G_LIST_MODEL (model), last, 0, 1);
+            }
         }
     }
     else
@@ -135,8 +143,6 @@ gl_journal_model_get_property (GObject    *object,
     }
 }
 
-
-
 static void
 gl_journal_model_stop_idle (GlJournalModel *model)
 {
@@ -191,7 +197,22 @@ gl_journal_model_get_item (GListModel *list,
     GlJournalModel *model = GL_JOURNAL_MODEL (list);
 
     if (position < model->entries->len)
-        return g_object_ref (g_ptr_array_index (model->entries, position));
+    {
+        if (model->query->order == GL_SORT_ORDER_ASCENDING_TIME &&
+            model->entries->len)
+        {
+            guint last = model->entries->len;
+
+            /* read the array in reverse direction */
+            return g_object_ref (g_ptr_array_index (model->entries,
+                                                    (last - 1) - position));
+        }
+        else
+        {
+            return g_object_ref (g_ptr_array_index (model->entries, position));
+        }
+
+    }
 
     return NULL;
 }
@@ -281,6 +302,12 @@ gl_query_set_search_type (GlQuery *query, GlQuerySearchType search_type)
     query->search_type = search_type;
 }
 
+void
+gl_query_set_sort_order (GlQuery *query, GlSortOrder order)
+{
+    query->order = order;
+}
+
 static gchar *
 gl_query_item_create_match_string (GlQueryItem *queryitem)
 {
diff --git a/src/gl-journal-model.h b/src/gl-journal-model.h
index 5823282..5a6c1ce 100644
--- a/src/gl-journal-model.h
+++ b/src/gl-journal-model.h
@@ -21,6 +21,8 @@
 
 #include <gio/gio.h>
 
+#include "gl-application.h"
+
 typedef enum
 {
     GL_QUERY_SEARCH_TYPE_SUBSTRING,
@@ -32,6 +34,7 @@ typedef struct GlQuery
 {
     GPtrArray *queryitems;   /* array of GlQueryItem structs */
     GlQuerySearchType search_type;    /* indicates if search field is passed as exact match */
+    GlSortOrder order; /* whether to iterate through journal in ascending or descending timestamp order */
     guint64 start_timestamp;
     guint64 end_timestamp;
 } GlQuery;
@@ -71,4 +74,7 @@ gchar *                 gl_journal_model_get_boot_time                  (GlJourn
 void                    gl_query_set_search_type                        (GlQuery *query,
                                                                          GlQuerySearchType search_type);
 
+void                    gl_query_set_sort_order                         (GlQuery *query,
+                                                                         GlSortOrder order);
+
 #endif


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