[gnome-text-editor] session: provide hint when opening files with session



commit dd35c10d4f57f6962fb5a052757acc77d63443c0
Author: Christian Hergert <chergert redhat com>
Date:   Thu Feb 10 14:00:01 2022 -0800

    session: provide hint when opening files with session
    
    We want to be able to propagate a hint to the session manager when opening
    files so that special care can be handled based on command-line arguments.

 src/editor-application.c | 64 ++++++++++++++++++++++++++++++++++++------------
 src/editor-session.c     |  3 ++-
 src/editor-session.h     |  3 ++-
 3 files changed, 52 insertions(+), 18 deletions(-)
---
diff --git a/src/editor-application.c b/src/editor-application.c
index 1d1bd01..8c6d151 100644
--- a/src/editor-application.c
+++ b/src/editor-application.c
@@ -34,6 +34,12 @@
 #define PORTAL_OBJECT_PATH "/org/freedesktop/portal/desktop"
 #define PORTAL_SETTINGS_INTERFACE "org.freedesktop.portal.Settings"
 
+typedef struct
+{
+  GPtrArray *files;
+  char *hint;
+} Restore;
+
 G_DEFINE_TYPE (EditorApplication, editor_application, ADW_TYPE_APPLICATION)
 
 enum {
@@ -45,29 +51,63 @@ enum {
 
 static GParamSpec *properties[N_PROPS];
 
+static void
+restore_free (Restore *restore)
+{
+  g_clear_pointer (&restore->files, g_ptr_array_unref);
+  g_clear_pointer (&restore->hint, g_free);
+  g_free (restore);
+}
+
+static Restore *
+restore_new (GFile      **files,
+             guint        n_files,
+             const char  *hint)
+{
+  Restore *restore;
+
+  g_assert (files != NULL || n_files == 0);
+
+  restore = g_new0 (Restore, 1);
+  restore->files = g_ptr_array_new_with_free_func (g_object_unref);
+  restore->hint = g_strdup (hint);
+
+  for (guint i = 0; i < n_files; i++)
+    g_ptr_array_add (restore->files, g_object_ref (files[i]));
+
+  return restore;
+}
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (Restore, restore_free)
+
 static void
 editor_application_restore_cb (GObject      *object,
                                GAsyncResult *result,
                                gpointer      user_data)
 {
   EditorSession *session = (EditorSession *)object;
-  g_autoptr(GPtrArray) files = user_data;
+  g_autoptr(Restore) restore = user_data;
   g_autoptr(GError) error = NULL;
 
   g_assert (G_IS_ASYNC_RESULT (result));
   g_assert (EDITOR_IS_SESSION (session));
+  g_assert (restore != NULL);
+  g_assert (restore->files != NULL);
 
   if (!editor_session_restore_finish (session, result, &error))
     {
       if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
         g_warning ("Failed to restore session: %s", error->message);
 
-      if (files == NULL || files->len == 0)
+      if (restore->files->len == 0)
         editor_session_create_window (session);
     }
 
-  if (files != NULL && files->len > 0)
-    editor_session_open_files (session, (GFile **)files->pdata, files->len);
+  if (restore->files->len > 0)
+    editor_session_open_files (session,
+                               (GFile **)(gpointer)restore->files->pdata,
+                               restore->files->len,
+                               restore->hint);
 
   g_application_release (g_application_get_default ());
 }
@@ -105,7 +145,7 @@ editor_application_activate (GApplication *application)
   editor_session_restore_async (self->session,
                                 NULL,
                                 editor_application_restore_cb,
-                                NULL);
+                                restore_new (NULL, 0, NULL));
 }
 
 static void
@@ -115,15 +155,9 @@ editor_application_open (GApplication  *application,
                          const gchar   *hint)
 {
   EditorApplication *self = (EditorApplication *)application;
-  g_autoptr(GPtrArray) ar = NULL;
 
   g_assert (EDITOR_IS_APPLICATION (self));
-  g_assert (files != NULL);
-  g_assert (n_files > 0);
-
-  ar = g_ptr_array_new_with_free_func (g_object_unref);
-  for (guint i = 0; i < n_files; i++)
-    g_ptr_array_add (ar, g_file_dup (files[i]));
+  g_assert (files != NULL || n_files == 0);
 
   /* If we're being asked to open files via this interface,
    * we want to ignore restoring the previous session because
@@ -140,9 +174,7 @@ editor_application_open (GApplication  *application,
 
   if (_editor_session_did_restore (self->session))
     {
-      editor_session_open_files (self->session,
-                                 (GFile **)ar->pdata,
-                                 ar->len);
+      editor_session_open_files (self->session, files, n_files, hint);
       return;
     }
 
@@ -151,7 +183,7 @@ editor_application_open (GApplication  *application,
   editor_session_restore_async (self->session,
                                 NULL,
                                 editor_application_restore_cb,
-                                g_steal_pointer (&ar));
+                                restore_new (files, n_files, hint));
 }
 
 static gboolean
diff --git a/src/editor-session.c b/src/editor-session.c
index e39c7b4..607d114 100644
--- a/src/editor-session.c
+++ b/src/editor-session.c
@@ -1418,7 +1418,8 @@ editor_session_open (EditorSession           *self,
 void
 editor_session_open_files (EditorSession  *self,
                            GFile         **files,
-                           gint            n_files)
+                           gint            n_files,
+                           const char     *hint)
 {
   g_return_if_fail (EDITOR_IS_SESSION (self));
 
diff --git a/src/editor-session.h b/src/editor-session.h
index ab5aa5b..719f51f 100644
--- a/src/editor-session.h
+++ b/src/editor-session.h
@@ -40,7 +40,8 @@ EditorPage   *editor_session_open                (EditorSession            *self
                                                   const GtkSourceEncoding  *encoding);
 void          editor_session_open_files          (EditorSession            *self,
                                                   GFile                   **files,
-                                                  gint                      n_files);
+                                                  gint                      n_files,
+                                                  const char               *hint);
 void          editor_session_add_page            (EditorSession            *self,
                                                   EditorWindow             *window,
                                                   EditorPage               *page);


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