diff options
author | António Fernandes <antoniof@gnome.org> | 2020-12-17 01:10:04 +0000 |
---|---|---|
committer | António Fernandes <antoniof@gnome.org> | 2021-02-06 18:20:10 +0000 |
commit | 0584f6cb93b26d2052e3214ce5894656ea79be27 (patch) | |
tree | cad21914689dd1e0993c40ae9ce119155fa18577 /src | |
parent | 145fcbac1351b39be963bf8b125381e4e43bbb77 (diff) | |
download | nautilus-0584f6cb93b26d2052e3214ce5894656ea79be27.tar.gz |
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. (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
Diffstat (limited to 'src')
-rw-r--r-- | src/nautilus-files-view.c | 69 | ||||
-rw-r--r-- | src/resources/ui/nautilus-files-view-context-menus.ui | 7 |
2 files changed, 76 insertions, 0 deletions
diff --git a/src/nautilus-files-view.c b/src/nautilus-files-view.c index 6a1cc25b9..9282e96ca 100644 --- a/src/nautilus-files-view.c +++ b/src/nautilus-files-view.c @@ -6580,6 +6580,71 @@ 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) && + nautilus_file_contains_text (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" @@ -7123,6 +7188,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 }, @@ -7614,6 +7680,9 @@ real_update_actions_state (NautilusFilesView *view) "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)); action = g_action_map_lookup_action (G_ACTION_MAP (view_action_group), diff --git a/src/resources/ui/nautilus-files-view-context-menus.ui b/src/resources/ui/nautilus-files-view-context-menus.ui index 0b48f4fc1..0d71a927a 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> |