After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 757766 - There isn't shortcuts support to launch the scripts
There isn't shortcuts support to launch the scripts
Status: RESOLVED FIXED
Product: nautilus
Classification: Core
Component: Scripts facilities
unspecified
Other Linux
: Normal enhancement
: ---
Assigned To: Nautilus Maintainers
Nautilus Maintainers
Depends on:
Blocks:
 
 
Reported: 2015-11-08 09:23 UTC by devim
Modified: 2015-11-11 18:16 UTC
See Also:
GNOME target: ---
GNOME version: 3.15/3.16


Attachments
Patch that add custom shortcuts support to launch scripts (3.06 KB, patch)
2015-11-08 09:40 UTC, devim
none Details | Review
Previous patch with coding style and bugs fixed (3.00 KB, patch)
2015-11-09 12:20 UTC, devim
none Details | Review
[Fixed] Patch that add custom shortcuts support to launch scripts (3.07 KB, patch)
2015-11-10 06:37 UTC, devim
none Details | Review
[Fixed] Patch that add custom shortcuts support to launch scripts (3.08 KB, patch)
2015-11-10 22:00 UTC, devim
committed Details | Review

Description devim 2015-11-08 09:23:55 UTC
I think it's really a need to have shortcuts to run the scripts, because this could save time instead of clicking in the context menu.
Nautilus is a file manager, i.e. it's an important piece of the os, but it doesn't support custom shortcuts. If it was possible map custom shortcuts to run own scripts it would be useful (and awesome).
Comment 1 devim 2015-11-08 09:40:49 UTC
Created attachment 315075 [details] [review]
Patch that add custom shortcuts support to launch scripts
Comment 2 Cosimo Cecchi 2015-11-08 19:29:53 UTC
Review of attachment 315075 [details] [review]:

Thanks for the revised patch; please find my comments inline.

::: src/nautilus-view.c
@@ +4229,3 @@
+static void
+nautilus_load_custom_accel_for_scripts(){
+	gchar *path = g_strconcat(g_get_home_dir(),SHORTCUTS_PATH,NULL);

Coding style: add a space before parenthesis for function calls (multiple occurrences)

@@ +4233,3 @@
+	GError *error = NULL;
+	if (g_file_get_contents(path, &contents, NULL, &error)) {
+		gchar **lines = g_strsplit(contents, "\n", -1);

lines is leaked

@@ +4239,3 @@
+		for(i=0; strstr(lines[i]," ") > 0; i++) {
+			gchar **result = g_strsplit(lines[i], " ", 2);
+	if (g_file_get_contents(path, &contents, NULL, &error)) {

You should declare the hash table with g_hash_table_new_full() and pass g_free() as GDestroyNotify functions for both the keys and the values.

@@ +4243,3 @@
+		}
+	} else
+		g_free(contents);

error is guaranteed to be != NULL if g_file_get_contents() returns FALSE

@@ +4268,3 @@
 
+	if(script_accels == NULL) script_accels = g_hash_table_new(g_str_hash,g_str_equal);
+	nautilus_load_custom_accel_for_scripts();

As far as I can see, here you're populating the hash table every time this function is called. Instead, it should only ever be parsed once.

@@ +4270,3 @@
+	nautilus_load_custom_accel_for_scripts();
+	
+    file_list = nautilus_directory_get_file_list (directory);

No indentation changes please
Comment 3 devim 2015-11-09 12:15:17 UTC
Ok, I fixed the patch as you suggested
Comment 4 devim 2015-11-09 12:20:21 UTC
Created attachment 315117 [details] [review]
Previous patch with coding style and bugs fixed
Comment 5 Cosimo Cecchi 2015-11-09 21:37:52 UTC
Review of attachment 315117 [details] [review]:

Thank you for the updated patch, we're getting there!
I have a few more minor comments.

::: src/nautilus-view.c
@@ +115,3 @@
 #define TEMPLATE_LIMIT 30
 
+#define SHORTCUTS_PATH "/.config/nautilus/shortcuts"

The file should be called "scripts-accels" or something like that. "shortcuts" is too generic.

@@ +4228,3 @@
 
+static void
+nautilus_load_custom_accel_for_scripts (){

Missing void parameter; also missing space before curly brace

@@ +4229,3 @@
+static void
+nautilus_load_custom_accel_for_scripts (){
+	gchar *path = g_strconcat (g_get_home_dir (), SHORTCUTS_PATH, NULL);

Should use g_build_filename() and g_get_user_config_dir()

@@ +4231,3 @@
+	gchar *path = g_strconcat (g_get_home_dir (), SHORTCUTS_PATH, NULL);
+	gchar *contents;
+	GError *error = NULL;

Nitpick: add space here

@@ +4235,3 @@
+		gchar **lines = g_strsplit (contents, "\n", -1);
+		g_free (contents);
+	gchar *contents;

Why do you need the max length at all?

@@ +4236,3 @@
+		g_free (contents);
+		const int max_len = 100;
+	GError *error = NULL;

Nitpick: move variable declarations at the top

@@ +4237,3 @@
+		const int max_len = 100;
+		int i;
+	GError *error = NULL;

Missing space before and after =, and between the two parameters of strstr().
Also I think we need a check for lines[i] != NULL too.

@@ +4239,3 @@
+		for(i=0; strstr (lines[i]," ") > 0; i++) {
+			gchar **result = g_strsplit (lines[i], " ", 2);
+	if (g_file_get_contents (path, &contents, NULL, &error)) {

I was imagining that the accel file was of the format

script_name accel
script_name2 another_accel

instead of

accel script_name
another_accel script_name2

Any reason to prefer one over the other? In any case, there should be a comment before this function that explains the expected format.

@@ +4267,3 @@
 	g_return_val_if_fail (NAUTILUS_IS_DIRECTORY (directory), NULL);
 
+	if(script_accels == NULL) {

Missing space after if
Comment 6 devim 2015-11-10 06:37:35 UTC
Created attachment 315169 [details] [review]
[Fixed] Patch that add custom shortcuts support to launch scripts

Ok, I followed your precious suggestions again, but I can't understand "Nitpick: add space here", where?

I think it's better to use:
accel script_name
another_accel script_name2

because the script names can have a big length difference, instead the accels have often a small length.
Example format 1:
<control>h script_with_a_very_long_long_long_long_long_long_name
F2 short_name

Example format 2:
script_with_a_very_long_long_long_long_long_long_name <control>h
short_name F2

I prefer the appearance of format 1.
Comment 7 Cosimo Cecchi 2015-11-10 17:58:15 UTC
Review of attachment 315169 [details] [review]:

Looking almost ready to go - see minor comments below.

::: src/nautilus-view.c
@@ +4193,3 @@
+    
+	gchar *shortcut = NULL;
+	if (shortcut = g_hash_table_lookup(script_accels,name)) {

Missing space before parenthesis, and missing space after comma between the parameters.

@@ +4194,3 @@
+	gchar *shortcut = NULL;
+	if (shortcut = g_hash_table_lookup(script_accels,name)) {
+		nautilus_application_add_accelerator(g_application_get_default(),detailed_action_name,shortcut);       

See above

@@ +4234,3 @@
+	gchar *contents;
+	GError *error = NULL;
+	const int max_len = 100;

Why do you need a max length?

@@ +4239,3 @@
+		gchar **lines = g_strsplit (contents, "\n", -1);
+		g_free (contents);
+	gchar *contents;

Missing space after "for"
Comment 8 devim 2015-11-10 22:00:35 UTC
Created attachment 315216 [details] [review]
[Fixed] Patch that add custom shortcuts support to launch scripts

Ok I fixed the spaces.

I use max length because I think it's better don't keep strings longer than required both to avoid a waste of memory and to avoid problems.

If the hashtable contains a long string (potentially very long) and someone in the future will add a new feature that will use this hashtable, maybe this could bring problems (like a buffer overflow).

So I used max_len because there isn't the need to keep strings with a potentially infinite length.
Comment 9 Cosimo Cecchi 2015-11-10 22:29:02 UTC
Review of attachment 315216 [details] [review]:

This looks good to me now, thanks for your work!
Comment 10 devim 2015-11-11 05:45:13 UTC
thank you for your support :)
Comment 11 Cosimo Cecchi 2015-11-11 18:13:43 UTC
Pushed to master.
Comment 12 Carlos Soriano 2015-11-11 18:16:38 UTC
commit: https://git.gnome.org/browse/nautilus/commit/?id=9fe1335

Cool, thanks Devim!