[gnome-builder] editor: implement IdeApplicationAddin.open



commit 8c16aac2e42e3745148968ccae25c07449ee3c81
Author: Daniel Buch <boogiewasthere gmail com>
Date:   Sat Jan 12 14:46:33 2019 -0800

    editor: implement IdeApplicationAddin.open
    
    This implements the open vfunc for the editor application addin. This
    allows opening files with the editor.
    
    Some work may need to be done in the future to handle content-types
    to avoid what will be opened.

 src/plugins/editor/gbp-editor-application-addin.c | 73 +++++++++++++++++++++--
 1 file changed, 67 insertions(+), 6 deletions(-)
---
diff --git a/src/plugins/editor/gbp-editor-application-addin.c 
b/src/plugins/editor/gbp-editor-application-addin.c
index 7a1fff061..244391bd9 100644
--- a/src/plugins/editor/gbp-editor-application-addin.c
+++ b/src/plugins/editor/gbp-editor-application-addin.c
@@ -63,6 +63,20 @@ get_common_ancestor (GPtrArray *files)
   return g_steal_pointer (&ancestor);
 }
 
+static GFile *
+get_common_ancestor_array (GFile **files,
+                           gint    n_files)
+{
+  g_autoptr (GPtrArray) fileptrs = g_ptr_array_sized_new (n_files);
+
+  g_assert (files != NULL || n_files == 0);
+
+  for (guint i = 0; i < n_files; i++)
+    g_ptr_array_add (fileptrs, files[i]);
+
+  return get_common_ancestor (fileptrs);
+}
+
 static void
 gbp_editor_application_addin_add_option_entries (IdeApplicationAddin *addin,
                                                  IdeApplication      *app)
@@ -84,22 +98,23 @@ gbp_editor_application_addin_open_all_cb (GObject      *object,
                                           GAsyncResult *result,
                                           gpointer      user_data)
 {
-  IdeWorkbench *workbench = (IdeWorkbench *)object;
+  IdeWorkbench *workbench = (IdeWorkbench *) object;
   g_autoptr(GApplicationCommandLine) cmdline = user_data;
   g_autoptr(GError) error = NULL;
 
+  g_assert (IDE_IS_MAIN_THREAD ());
   g_assert (IDE_IS_WORKBENCH (workbench));
   g_assert (G_IS_ASYNC_RESULT (result));
-  g_assert (G_IS_APPLICATION_COMMAND_LINE (cmdline));
+  g_assert (!cmdline || G_IS_APPLICATION_COMMAND_LINE (cmdline));
 
   if (!ide_workbench_open_finish (workbench, result, &error))
     {
-      g_application_command_line_printerr (cmdline, "%s\n", error->message);
-      g_application_command_line_set_exit_status (cmdline, EXIT_FAILURE);
-      return;
+      if (error != NULL && cmdline != NULL)
+        g_application_command_line_printerr (cmdline, "%s\n", error->message);
     }
 
-  g_application_command_line_set_exit_status (cmdline, EXIT_SUCCESS);
+  if (cmdline != NULL)
+    g_application_command_line_set_exit_status (cmdline, error == NULL ? EXIT_SUCCESS : EXIT_FAILURE);
 }
 
 static void
@@ -178,11 +193,57 @@ gbp_editor_application_addin_handle_command_line (IdeApplicationAddin     *addin
                                 g_object_ref (cmdline));
 }
 
+static void
+gbp_editor_application_addin_open (IdeApplicationAddin  *addin,
+                                   IdeApplication       *application,
+                                   GFile               **files,
+                                   gint                  n_files,
+                                   const gchar          *hint)
+{
+  g_autoptr(IdeWorkbench) workbench = NULL;
+  IdeEditorWorkspace *workspace;
+  g_autoptr(GFile) workdir = NULL;
+  IdeContext *context;
+
+  workbench = ide_workbench_new ();
+  ide_application_add_workbench (application, workbench);
+
+  workdir = get_common_ancestor_array (files, n_files);
+  context = ide_workbench_get_context (workbench);
+
+  /* Setup the working directory to top-most common ancestor of the
+   * files. That way we can still get somewhat localized search results
+   * and other workspace features.
+   */
+  if (workdir != NULL)
+    ide_context_set_workdir (context, workdir);
+
+  workspace = ide_editor_workspace_new (application);
+  ide_workbench_add_workspace (workbench, IDE_WORKSPACE (workspace));
+
+  /* Since we are opening a toplevel window, we want to restore it using
+   * the same window sizing as the primary IDE window.
+   */
+  _ide_window_settings_register (GTK_WINDOW (workspace));
+
+  ide_workbench_focus_workspace (workbench, IDE_WORKSPACE (workspace));
+
+  ide_workbench_open_all_async (workbench,
+                                files,
+                                n_files,
+                                "editor",
+                                NULL,
+                                gbp_editor_application_addin_open_all_cb,
+                                NULL);
+
+}
+
 static void
 cmdline_addin_iface_init (IdeApplicationAddinInterface *iface)
 {
   iface->add_option_entries = gbp_editor_application_addin_add_option_entries;
   iface->handle_command_line = gbp_editor_application_addin_handle_command_line;
+  iface->open = gbp_editor_application_addin_open;
 }
 
 G_DEFINE_TYPE_WITH_CODE (GbpEditorApplicationAddin, gbp_editor_application_addin, G_TYPE_OBJECT,


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