[gnome-logs] Disable 'export' action when the logs are empty



commit 93bb0931b29942310df7edfb83ccd18d1a9b8a4b
Author: ankritisachan <ankritisachan gmail com>
Date:   Fri Apr 20 09:15:31 2018 +0530

    Disable 'export' action when the logs are empty
    
    To prevent creation of empty file on export, it is better to disable
    'export' action when there are no logs to display.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=795349

 src/gl-eventviewlist.c |   10 +++++++++
 src/gl-eventviewlist.h |    1 +
 src/gl-journal-model.c |   49 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/gl-window.c        |   28 +++++++++++++++++++++++++++
 src/gl-window.h        |    2 +
 5 files changed, 90 insertions(+), 0 deletions(-)
---
diff --git a/src/gl-eventviewlist.c b/src/gl-eventviewlist.c
index a7431be..c9acd17 100644
--- a/src/gl-eventviewlist.c
+++ b/src/gl-eventviewlist.c
@@ -64,6 +64,16 @@ static const gchar SETTINGS_SCHEMA[] = "org.gnome.Logs";
 static const gchar CLOCK_FORMAT[] = "clock-format";
 static const gchar SORT_ORDER[] = "sort-order";
 
+GlJournalModel *
+gl_event_view_list_get_journal_model (GlEventViewList *view)
+{
+    GlEventViewListPrivate *priv;
+
+    priv = gl_event_view_list_get_instance_private (view);
+
+    return priv->journal_model;
+}
+
 GtkWidget *
 gl_event_view_list_get_category_list (GlEventViewList *view)
 {
diff --git a/src/gl-eventviewlist.h b/src/gl-eventviewlist.h
index 8e88c43..a320264 100644
--- a/src/gl-eventviewlist.h
+++ b/src/gl-eventviewlist.h
@@ -44,6 +44,7 @@ GArray * gl_event_view_list_get_boot_ids (GlEventViewList *view);
 gchar * gl_event_view_list_get_output_logs (GlEventViewList *view);
 gchar * gl_event_view_list_get_boot_time (GlEventViewList *view,
                                           const gchar *boot_match);
+GlJournalModel * gl_event_view_list_get_journal_model (GlEventViewList *view);
 
 G_END_DECLS
 
diff --git a/src/gl-journal-model.c b/src/gl-journal-model.c
index ea68cea..b1a54ec 100644
--- a/src/gl-journal-model.c
+++ b/src/gl-journal-model.c
@@ -58,8 +58,23 @@ struct _GlJournalModel
     guint idle_source;
 
     guint compressed_entries_counter;
+
+    /* export variable is used to identify when total number of entries,
+     * to display, changes from 0 to a non zero number and only then
+     * emit 'ENABLE_EXPORT'signal once instead of emitting it repeatedly
+     * when new entries are continuously added */
+    gboolean export;
 };
 
+enum
+{
+    DISABLE_EXPORT,
+    ENABLE_EXPORT,
+    LAST_SIGNAL
+};
+
+static guint signals[LAST_SIGNAL] = { 0 };
+
 static void gl_journal_model_interface_init (GListModelInterface *iface);
 static GPtrArray *tokenize_search_string (gchar *search_text);
 static gboolean search_in_entry (GlJournalEntry *entry, GlJournalModel *model);
@@ -103,6 +118,18 @@ gl_journal_model_fetch_idle (gpointer user_data)
     g_assert (model->n_entries_to_fetch > 0);
 
     last = model->entries->len;
+
+    if (last == 0)
+    {
+        model->export = FALSE;
+        g_signal_emit (model, signals[DISABLE_EXPORT], 0);
+    }
+    else if (last > 0 && model->export == FALSE)
+    {
+        model->export = TRUE;
+        g_signal_emit (model, signals[ENABLE_EXPORT], 0);
+    }
+
     if ((entry = gl_journal_previous (model->journal)) && gl_query_check_journal_end (model->query, entry))
     {
         if (search_in_entry (entry, model))
@@ -229,6 +256,7 @@ gl_journal_model_init (GlJournalModel *model)
     model->batch_size = 50;
     model->journal = gl_journal_new ();
     model->entries = g_ptr_array_new_with_free_func (g_object_unref);
+    model->export = FALSE;
 
     gl_journal_model_fetch_more_entries (model, FALSE);
 }
@@ -344,6 +372,27 @@ gl_journal_model_class_init (GlJournalModelClass *class)
                                                      G_PARAM_READABLE | default_flags);
 
     g_object_class_install_properties (object_class, N_PROPERTIES, properties);
+
+    signals[DISABLE_EXPORT] = g_signal_new ("disable-export",
+                                           G_TYPE_FROM_CLASS (class),
+                                           G_SIGNAL_RUN_LAST,
+                                           0,
+                                           NULL,
+                                           NULL,
+                                           NULL,
+                                           G_TYPE_NONE,
+                                           0);
+
+    signals[ENABLE_EXPORT]  = g_signal_new ("enable-export",
+                                           G_TYPE_FROM_CLASS (class),
+                                           G_SIGNAL_RUN_LAST,
+                                           0,
+                                           NULL,
+                                           NULL,
+                                           NULL,
+                                           G_TYPE_NONE,
+                                           0);
+
 }
 
 static void
diff --git a/src/gl-window.c b/src/gl-window.c
index c604285..b6dbee2 100644
--- a/src/gl-window.c
+++ b/src/gl-window.c
@@ -363,6 +363,26 @@ gl_window_class_init (GlWindowClass *klass)
                                              on_ignore_button_clicked);
 }
 
+void
+disable_export (GlWindow *window)
+{
+    GAction *action_export;
+
+    action_export = g_action_map_lookup_action (G_ACTION_MAP (window),
+                                                "export");
+    g_simple_action_set_enabled (G_SIMPLE_ACTION (action_export), FALSE);
+}
+
+void
+enable_export (GlWindow *window)
+{
+    GAction *action_export;
+
+    action_export = g_action_map_lookup_action (G_ACTION_MAP (window),
+                                                "export");
+    g_simple_action_set_enabled (G_SIMPLE_ACTION (action_export), TRUE);
+}
+
 static void
 gl_window_init (GlWindow *window)
 {
@@ -380,6 +400,7 @@ gl_window_init (GlWindow *window)
     GVariant *variant;
     GSettings *settings;
     gboolean ignore;
+    GlJournalModel *model;
 
     gtk_widget_init_template (GTK_WIDGET (window));
 
@@ -410,6 +431,13 @@ gl_window_init (GlWindow *window)
     g_signal_connect (GL_CATEGORY_LIST (categories), "notify::category",
                       G_CALLBACK (on_category_list_changed), window);
 
+    model = gl_event_view_list_get_journal_model (event_list);
+
+    g_signal_connect_swapped (model, "disable_export",
+                              G_CALLBACK (disable_export), window);
+    g_signal_connect_swapped (model, "enable_export",
+                              G_CALLBACK (enable_export), window);
+
     provider = gtk_css_provider_new ();
     g_signal_connect (provider, "parsing-error",
                       G_CALLBACK (gl_util_on_css_provider_parsing_error),
diff --git a/src/gl-window.h b/src/gl-window.h
index 3c052e7..4744569 100644
--- a/src/gl-window.h
+++ b/src/gl-window.h
@@ -30,6 +30,8 @@ G_DECLARE_FINAL_TYPE (GlWindow, gl_window, GL, WINDOW, GtkApplicationWindow)
 
 GtkWidget * gl_window_new (GtkApplication *application);
 void gl_window_set_sort_order (GlWindow *window, GlSortOrder sort_order);
+void disable_export (GlWindow *window);
+void enable_export (GlWindow *window);
 
 G_END_DECLS
 


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