[gnome-text-editor] session: truncate bookmarks to 100



commit c297cfc2a6a8ed2890bd9b6f173c6f2922ecea47
Author: Christian Hergert <chergert redhat com>
Date:   Thu Oct 28 20:34:24 2021 -0700

    session: truncate bookmarks to 100
    
    This will truncate the oldest bookmarks beyond 100 so that we don't over
    fill our recents list.
    
    For those with a large collection of recent files, this will take two
    times to run before the changes are seen. The first run will truncate the
    bookmarks when the session is saved (at startup). The second run will
    see that truncated list.
    
    Related #192

 src/editor-session.c | 107 +++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 82 insertions(+), 25 deletions(-)
---
diff --git a/src/editor-session.c b/src/editor-session.c
index f7bc2f0..66120b6 100644
--- a/src/editor-session.c
+++ b/src/editor-session.c
@@ -32,6 +32,7 @@
 
 #define DEFAULT_AUTO_SAVE_TIMEOUT_SECONDS 3
 #define MAX_AUTO_SAVE_TIMEOUT_SECONDS (60*5)
+#define MAX_BOOKMARKS 100
 
 typedef struct
 {
@@ -55,6 +56,12 @@ typedef struct
   Position end;
 } Selection;
 
+typedef struct
+{
+  char *uri;
+  GDateTime *age;
+} Recent;
+
 G_DEFINE_TYPE (EditorSession, editor_session, G_TYPE_OBJECT)
 
 enum {
@@ -997,6 +1004,25 @@ _editor_session_remove_window (EditorSession *self,
   _editor_session_mark_dirty (self);
 }
 
+static int
+recent_compare (gconstpointer a,
+                gconstpointer b)
+{
+  const Recent *ra = a;
+  const Recent *rb = b;
+
+  if (ra->age == NULL && rb->age == NULL)
+    return strcmp (ra->uri, rb->uri);
+
+  if (ra->age == NULL)
+    return 1;
+
+  if (rb->age == NULL)
+    return -1;
+
+  return g_date_time_compare (ra->age, rb->age);
+}
+
 static void
 editor_session_update_recent_worker (GTask        *task,
                                      gpointer      source_object,
@@ -1005,6 +1031,9 @@ editor_session_update_recent_worker (GTask        *task,
 {
   EditorSessionSave *save = task_data;
   g_autoptr(GSettings) settings = NULL;
+  g_autoptr(GBookmarkFile) bookmarks = NULL;
+  g_autofree gchar *filename = NULL;
+  g_autoptr(GError) error = NULL;
 
   g_assert (G_IS_TASK (task));
   g_assert (EDITOR_IS_SESSION (source_object));
@@ -1021,46 +1050,74 @@ editor_session_update_recent_worker (GTask        *task,
       return;
     }
 
-  if (save->seen != NULL || save->forgot != NULL)
+  bookmarks = g_bookmark_file_new ();
+  filename = get_bookmarks_filename ();
+
+  if (!g_bookmark_file_load_from_file (bookmarks, filename, &error))
+    {
+      if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
+        g_warning ("Failed to load bookmarks file: %s", error->message);
+      g_clear_error (&error);
+    }
+
+  /* Truncate to recent items to avoid gigantic lists */
+  if (g_bookmark_file_get_size (bookmarks) > MAX_BOOKMARKS)
     {
-      g_autoptr(GBookmarkFile) bookmarks = g_bookmark_file_new ();
-      g_autofree gchar *filename = get_bookmarks_filename ();
-      g_autoptr(GError) error = NULL;
+      g_autoptr(GArray) ar = g_array_new (FALSE, FALSE, sizeof (Recent));
+      gchar **uris;
+      gsize length;
+
+      uris = g_bookmark_file_get_uris (bookmarks, &length);
 
-      if (!g_bookmark_file_load_from_file (bookmarks, filename, &error))
+      for (gsize i = 0; i < length; i++)
         {
-          if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
-            g_warning ("Failed to load bookmarks file: %s", error->message);
-          g_clear_error (&error);
+          Recent r;
+
+          r.age = g_bookmark_file_get_visited_date_time (bookmarks, uris[i], NULL);
+          r.uri = g_steal_pointer (&uris[i]);
+
+          g_array_append_val (ar, r);
         }
 
-      if (save->seen != NULL)
+      g_clear_pointer (&uris, g_free);
+
+      g_array_sort (ar, recent_compare);
+
+      for (gsize i = MAX_BOOKMARKS; i < ar->len; i++)
         {
-          for (guint i = 0; i < save->seen->len; i++)
-            {
-              GFile *file = g_ptr_array_index (save->seen, i);
-              g_autofree gchar *uri = g_file_get_uri (file);
-              g_bookmark_file_add_application (bookmarks, uri, NULL, NULL);
-            }
+          const Recent *r = &g_array_index (ar, Recent, i);
+
+          g_debug ("Removing %s from recents", r->uri);
+          g_bookmark_file_remove_item (bookmarks, r->uri, NULL);
         }
+    }
 
-      if (save->forgot != NULL)
+  if (save->seen != NULL)
+    {
+      for (guint i = 0; i < save->seen->len; i++)
         {
-          for (guint i = 0; i < save->forgot->len; i++)
-            {
-              GFile *file = g_ptr_array_index (save->forgot, i);
-              g_autofree gchar *uri = g_file_get_uri (file);
-              g_bookmark_file_remove_item (bookmarks, uri, NULL);
-            }
+          GFile *file = g_ptr_array_index (save->seen, i);
+          g_autofree gchar *uri = g_file_get_uri (file);
+          g_bookmark_file_add_application (bookmarks, uri, NULL, NULL);
         }
+    }
 
-      if (!g_bookmark_file_to_file (bookmarks, filename, &error))
+  if (save->forgot != NULL)
+    {
+      for (guint i = 0; i < save->forgot->len; i++)
         {
-          g_task_return_error (task, g_steal_pointer (&error));
-          return;
+          GFile *file = g_ptr_array_index (save->forgot, i);
+          g_autofree gchar *uri = g_file_get_uri (file);
+          g_bookmark_file_remove_item (bookmarks, uri, NULL);
         }
     }
 
+  if (!g_bookmark_file_to_file (bookmarks, filename, &error))
+    {
+      g_task_return_error (task, g_steal_pointer (&error));
+      return;
+    }
+
   g_task_return_boolean (task, TRUE);
 }
 


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