[nautilus/wip/antoniof/run-program-context-menu] files-view: Add action to run files as programs



commit 688b85c564ef8d233dfad99636c93a140c1be808
Author: António Fernandes <antoniof gnome org>
Date:   Thu Dec 17 01:10:04 2020 +0000

    files-view: Add action to run files as programs
    
    By default, when as executable text file is activated (by double-click,
    pressing <Return>, etc.), it opens as a regular text file (usually with
    a text editor).
    
    This is a safe and consistent behavior, but it makes runing scripts
    harder than it needs to be, requiring configuration by the user.
    
    Address this by introducing a new context menu action to run files as
    programs. For consistency, this menu action is shown for all launchable
    files: not only executable text files, but also executable binaries.
    (The code for this action was adapted from mime-actions.c.)
    
    Required for https://gitlab.gnome.org/GNOME/nautilus/-/issues/1700
    Discussed in https://gitlab.gnome.org/GNOME/nautilus/-/issues/443

 src/nautilus-files-view.c                          | 68 ++++++++++++++++++++++
 .../ui/nautilus-files-view-context-menus.ui        |  7 +++
 2 files changed, 75 insertions(+)
---
diff --git a/src/nautilus-files-view.c b/src/nautilus-files-view.c
index 678ad98b6..8a588936e 100644
--- a/src/nautilus-files-view.c
+++ b/src/nautilus-files-view.c
@@ -6625,6 +6625,70 @@ action_compress (GSimpleAction *action,
     nautilus_files_view_compress_dialog_new (view);
 }
 
+static gboolean
+can_run_in_terminal (GList *selection)
+{
+    NautilusFile *file;
+
+    if (g_list_length (selection) != 1)
+    {
+        return FALSE;
+    }
+
+    file = NAUTILUS_FILE (selection->data);
+
+    if (nautilus_file_is_launchable (file))
+    {
+        g_autofree gchar *activation_uri = NULL;
+        g_autofree gchar *executable_path = NULL;
+
+        activation_uri = nautilus_file_get_activation_uri (file);
+        executable_path = g_filename_from_uri (activation_uri, NULL, NULL);
+
+        if (executable_path != NULL)
+        {
+            return TRUE;
+        }
+    }
+
+    return FALSE;
+}
+
+static void
+action_run_in_terminal (GSimpleAction *action,
+                        GVariant      *state,
+                        gpointer       user_data)
+{
+    NautilusFilesView *view;
+    g_autolist (NautilusFile) selection = NULL;
+    g_autofree char *uri = NULL;
+    g_autofree char *executable_path = NULL;
+    g_autofree char *quoted_path = NULL;
+    GtkWindow *parent_window;
+    GdkScreen *screen;
+
+    g_assert (NAUTILUS_IS_FILES_VIEW (user_data));
+
+    view = NAUTILUS_FILES_VIEW (user_data);
+
+    selection = nautilus_view_get_selection (NAUTILUS_VIEW (view));
+
+    if (!can_run_in_terminal (selection))
+    {
+        return;
+    }
+
+    uri = nautilus_file_get_activation_uri (NAUTILUS_FILE (selection->data));
+    executable_path = g_filename_from_uri (uri, NULL, NULL);
+    quoted_path = g_shell_quote (executable_path);
+
+    parent_window = nautilus_files_view_get_containing_window (view);
+    screen = gtk_widget_get_screen (GTK_WIDGET (parent_window));
+
+    DEBUG ("Launching in terminal %s", quoted_path);
+
+    nautilus_launch_application_from_command (screen, quoted_path, TRUE, NULL);
+}
 
 #define BG_KEY_PRIMARY_COLOR      "primary-color"
 #define BG_KEY_SECONDARY_COLOR    "secondary-color"
@@ -7151,6 +7215,7 @@ const GActionEntry view_entries[] =
     { "compress", action_compress },
     { "properties", action_properties},
     { "current-directory-properties", action_current_dir_properties},
+    { "run-in-terminal", action_run_in_terminal },
     { "set-as-wallpaper", action_set_as_wallpaper },
     { "mount-volume", action_mount_volume },
     { "unmount-volume", action_unmount_volume },
@@ -7641,6 +7706,9 @@ real_update_actions_state (NautilusFilesView *view)
     action = g_action_map_lookup_action (G_ACTION_MAP (view_action_group),
                                          "open-item-new-window");
     g_simple_action_set_enabled (G_SIMPLE_ACTION (action), item_opens_in_view);
+    action = g_action_map_lookup_action (G_ACTION_MAP (view_action_group),
+                                         "run-in-terminal");
+    g_simple_action_set_enabled (G_SIMPLE_ACTION (action), can_run_in_terminal (selection));
     action = g_action_map_lookup_action (G_ACTION_MAP (view_action_group),
                                          "set-as-wallpaper");
     g_simple_action_set_enabled (G_SIMPLE_ACTION (action), can_set_wallpaper (selection));
diff --git a/src/resources/ui/nautilus-files-view-context-menus.ui 
b/src/resources/ui/nautilus-files-view-context-menus.ui
index 6438314a7..bc32dbc03 100644
--- a/src/resources/ui/nautilus-files-view-context-menus.ui
+++ b/src/resources/ui/nautilus-files-view-context-menus.ui
@@ -82,6 +82,13 @@
         <attribute name="hidden-when">action-disabled</attribute>
       </item>
     </section>
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">_Run as a Program</attribute>
+        <attribute name="action">view.run-in-terminal</attribute>
+        <attribute name="hidden-when">action-disabled</attribute>
+      </item>
+    </section>
     <section id="drive-section">
       <item>
         <attribute name="label" translatable="yes">_Mount</attribute>


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