[gnome-text-editor] session: add --ignore-session command line option



commit 8c98cefd240cacfc4ccb0fb3beba87e5c8d61107
Author: Christian Hergert <chergert redhat com>
Date:   Thu Aug 5 11:46:24 2021 -0700

    session: add --ignore-session command line option
    
    This is a bit of an escape hatch to avoid loading pages when restarting
    text editor. There are other things we can do better long term, but this
    at least gives us some flexibility to recover.
    
    Fixes #134

 po/POTFILES.in               |  1 +
 src/editor-application.c     | 26 ++++++++++++++++++++++++++
 src/editor-session-private.h |  3 +++
 src/editor-session.c         | 23 +++++++++++++++++++++--
 4 files changed, 51 insertions(+), 2 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index ce42552..9a80b2a 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -4,6 +4,7 @@ data/org.gnome.TextEditor.appdata.xml.in.in
 data/org.gnome.TextEditor.desktop.in.in
 data/org.gnome.TextEditor.gschema.xml
 src/editor-animation.c
+src/editor-application.c
 src/editor-application-actions.c
 src/editor-document.c
 src/editor-info-bar.c
diff --git a/src/editor-application.c b/src/editor-application.c
index 2919e5d..3c93560 100644
--- a/src/editor-application.c
+++ b/src/editor-application.c
@@ -22,6 +22,8 @@
 
 #include "config.h"
 
+#include <glib/gi18n.h>
+
 #include "editor-application-private.h"
 #include "editor-session-private.h"
 #include "editor-utils-private.h"
@@ -177,6 +179,22 @@ editor_application_startup (GApplication *application)
   gtk_window_set_default_icon_name (PACKAGE_ICON_NAME);
 }
 
+static gint
+editor_application_handle_local_options (GApplication *app,
+                                         GVariantDict *options)
+{
+  EditorApplication *self = (EditorApplication *)app;
+  gboolean ignore_session;
+
+  g_assert (EDITOR_IS_APPLICATION (self));
+  g_assert (options != NULL);
+
+  if (g_variant_dict_lookup (options, "ignore-session", "b", &ignore_session))
+    _editor_session_set_restore_pages (self->session, FALSE);
+
+  return G_APPLICATION_CLASS (editor_application_parent_class)->handle_local_options (app, options);
+}
+
 static void
 editor_application_constructed (GObject *object)
 {
@@ -213,13 +231,21 @@ editor_application_class_init (EditorApplicationClass *klass)
   application_class->open = editor_application_open;
   application_class->startup = editor_application_startup;
   application_class->shutdown = editor_application_shutdown;
+  application_class->handle_local_options = editor_application_handle_local_options;
 }
 
+static const GOptionEntry entries[] = {
+  { "ignore-session", 0, 0, G_OPTION_ARG_NONE, NULL, N_("Do not restore session at startup") },
+  { 0 }
+};
+
 static void
 editor_application_init (EditorApplication *self)
 {
   self->session = _editor_session_new ();
   editor_session_set_auto_save (self->session, TRUE);
+
+  g_application_add_main_option_entries (G_APPLICATION (self), entries);
 }
 
 EditorApplication *
diff --git a/src/editor-session-private.h b/src/editor-session-private.h
index 6a002da..750c1cf 100644
--- a/src/editor-session-private.h
+++ b/src/editor-session-private.h
@@ -50,6 +50,7 @@ struct _EditorSession
 
   guint               auto_save : 1;
   guint               did_restore : 1;
+  guint               restore_pages : 1;
   guint               dirty : 1;
 };
 
@@ -78,5 +79,7 @@ void           _editor_session_forget                 (EditorSession  *self,
                                                        GFile          *file,
                                                        const gchar    *draft_id);
 void           _editor_session_mark_dirty             (EditorSession  *self);
+void           _editor_session_set_restore_pages      (EditorSession  *self,
+                                                       gboolean        restore_pages);
 
 G_END_DECLS
diff --git a/src/editor-session.c b/src/editor-session.c
index b2ddc84..ed56b7c 100644
--- a/src/editor-session.c
+++ b/src/editor-session.c
@@ -546,6 +546,7 @@ editor_session_class_init (EditorSessionClass *klass)
 static void
 editor_session_init (EditorSession *self)
 {
+  self->restore_pages = TRUE;
   self->auto_save_delay = DEFAULT_AUTO_SAVE_TIMEOUT_SECONDS;
   self->seen = g_hash_table_new_full ((GHashFunc) g_file_hash,
                                       (GEqualFunc) g_file_equal,
@@ -1613,7 +1614,8 @@ editor_session_restore_v1 (EditorSession *self,
   if ((drafts = g_variant_lookup_value (state, "drafts", G_VARIANT_TYPE ("aa{sv}"))))
     editor_session_restore_v1_drafts (self, drafts);
 
-  if (!(windows = g_variant_lookup_value (state, "windows", G_VARIANT_TYPE ("aa{sv}"))) ||
+  if (!self->restore_pages ||
+      !(windows = g_variant_lookup_value (state, "windows", G_VARIANT_TYPE ("aa{sv}"))) ||
       g_variant_n_children (windows) == 0)
     goto failure;
 
@@ -1675,7 +1677,8 @@ editor_session_restore_v1 (EditorSession *self,
   return;
 
 failure:
-  g_warning ("Failed to restore session");
+  if (!self->restore_pages)
+    g_debug ("Failed to restore session or nothing to restore");
 
   editor_session_create_window (self);
 }
@@ -2204,3 +2207,19 @@ _editor_session_mark_dirty (EditorSession *self)
                                                   editor_session_auto_save_timeout_cb,
                                                   self);
 }
+
+void
+_editor_session_set_restore_pages (EditorSession *self,
+                                   gboolean       restore_pages)
+{
+  g_return_if_fail (EDITOR_IS_SESSION (self));
+
+  if (self->did_restore)
+    {
+      g_warning ("Calling %s() after restoring hsa no effect. Ignoring request.",
+                 G_STRFUNC);
+      return;
+    }
+
+  self->restore_pages = !!restore_pages;
+}


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