Skip to content

Commit

Permalink
nemo-actions: Add flag to escape spaces in file paths.
Browse files Browse the repository at this point in the history
Also, don't unescape uris, they work fine, quoted or unquoted.

https://bugs.launchpad.net/linuxmint/+bug/1330225
  • Loading branch information
mtwebster committed Jun 18, 2014
1 parent fbb2bad commit 98c62e6
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
<keyword>Quote</keyword>
<keyword>Dependencies</keyword>
<keyword>Conditions</keyword>
<keyword>EscapeSpaces</keyword>
</context>

<context id="non-standard-key" style-ref="key">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
<keyword>Quote</keyword>
<keyword>Dependencies</keyword>
<keyword>Conditions</keyword>
<keyword>EscapeSpaces</keyword>
</context>

<context id="non-standard-key" style-ref="key">
Expand Down
16 changes: 13 additions & 3 deletions files/usr/share/nemo/actions/sample.nemo_action
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
[Nemo Action]

###################################
#############################################
#### DEBUGGING:
####
#### Run Nemo with the environment
#### variable NEMO_ACTION_VERBOSE
#### set to get useful log output
#### for debugging your actions
###################################
####
#### i.e. $ nemo --quit
#### $ NEMO_ACTION_VERBOSE=1 nemo
#############################################

# Whether this action is active. For troubleshooting.
# Optional - if this field is omitted, the action will be active
Expand Down Expand Up @@ -70,7 +73,7 @@ Extensions=any;
# Quote type to use (if any) - enclose paths/urls with quotes. Optional - defaults
# to no quotes.
# Can be: single, double, backtick
Quote=double
#Quote=double

# Dependencies - program executables required for this action to work. Nemo will
# Search in the path for these program(s) and not display the action if any are missing.
Expand All @@ -87,3 +90,10 @@ Quote=double

#Conditions=desktop;

# Escape Spaces - set to true to escape spaces in filenames and uris ($U, $F, $P, $D)
#
# Sometimes this may be preferred to getting raw filenames that must be enclosed in
# quotes.
#
# Optional - by default this is false
EscapeSpaces=true
82 changes: 64 additions & 18 deletions libnemo-private/nemo-action.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ static gpointer parent_class;
#define KEY_QUOTE_TYPE "Quote"
#define KEY_DEPENDENCIES "Dependencies"
#define KEY_CONDITIONS "Conditions"
#define KEY_WHITESPACE "EscapeSpaces"

enum
{
Expand All @@ -78,6 +79,7 @@ enum
PROP_ORIG_TT,
PROP_SEPARATOR,
PROP_QUOTE_TYPE,
PROP_ESCAPE_SPACE,
PROP_CONDITIONS
};

Expand Down Expand Up @@ -114,6 +116,7 @@ nemo_action_init (NemoAction *action)
action->dbus = NULL;
action->dbus_satisfied = TRUE;
action->escape_underscores = FALSE;
action->escape_space = FALSE;
action->log_output = g_getenv ("NEMO_ACTION_VERBOSE") != NULL;
}

Expand Down Expand Up @@ -232,6 +235,14 @@ nemo_action_class_init (NemoActionClass *klass)
"Special conditions, like a bool gsettings key, or 'desktop'",
G_PARAM_READWRITE)
);
g_object_class_install_property (object_class,
PROP_ESCAPE_SPACE,
g_param_spec_boolean ("escape-space",
"Escape spaces in file paths",
"Escape spaces in file paths",
FALSE,
G_PARAM_READWRITE)
);
}

static void
Expand Down Expand Up @@ -431,6 +442,13 @@ nemo_action_constructed (GObject *object)
&condition_count,
NULL);

gboolean escape_space;

escape_space = g_key_file_get_boolean (key_file,
ACTION_FILE_GROUP,
KEY_WHITESPACE,
NULL);

if (conditions && condition_count > 0) {
int j;
gchar *condition;
Expand Down Expand Up @@ -472,6 +490,7 @@ nemo_action_constructed (GObject *object)
"quote-type", quote_type,
"separator", separator,
"conditions", conditions,
"escape-space", escape_space,
NULL);

g_free (orig_label);
Expand Down Expand Up @@ -653,6 +672,9 @@ nemo_action_set_property (GObject *object,
case PROP_CONDITIONS:
nemo_action_set_conditions (action, g_value_get_pointer (value));
break;
case PROP_ESCAPE_SPACE:
action->escape_space = g_value_get_boolean (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
Expand Down Expand Up @@ -707,6 +729,9 @@ nemo_action_get_property (GObject *object,
case PROP_CONDITIONS:
g_value_set_pointer (value, action->conditions);
break;
case PROP_ESCAPE_SPACE:
g_value_set_boolean (value, action->escape_space);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
Expand Down Expand Up @@ -757,8 +782,24 @@ find_token_type (const gchar *str, TokenType *token_type)
return NULL;
}

static gchar *
get_path (NemoAction *action, NemoFile *file)
{
gchar *ret;

if (action->escape_space) {
gchar *path = nemo_file_get_path (file);
ret = eel_str_escape_spaces (path);
g_free (path);
} else {
ret = nemo_file_get_path (file);
}

return ret;
}

static GString *
_score_append (NemoAction *action, GString *str, const gchar *c)
score_append (NemoAction *action, GString *str, const gchar *c)
{
if (action->escape_underscores) {
gchar *escaped = eel_str_double_underscores (c);
Expand All @@ -776,7 +817,7 @@ insert_separator (NemoAction *action, GString *str)
if (action->separator == NULL)
str = g_string_append (str, " ");
else
str = _score_append (action, str, action->separator);
str = score_append (action, str, action->separator);

return str;
}
Expand All @@ -802,16 +843,24 @@ insert_quote (NemoAction *action, GString *str)
}

static gchar *
get_device_path (NemoFile *file)
get_device_path (NemoAction *action, NemoFile *file)
{
GMount *mount = nemo_file_get_mount (file);
GVolume *volume = g_mount_get_volume (mount);
gchar *id = g_volume_get_identifier (volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
gchar *ret = NULL;

if (action->escape_space) {
gchar *id = g_volume_get_identifier (volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
ret = eel_str_escape_spaces (id);
g_free (id);
} else {
ret = g_volume_get_identifier (volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
}

g_object_unref (mount);
g_object_unref (volume);

return id;
return ret;
}

static gchar *
Expand All @@ -829,9 +878,9 @@ get_insertion_string (NemoAction *action, TokenType token_type, GList *selection
if (!first)
str = insert_separator (action, str);
str = insert_quote (action, str);
gchar *path = nemo_file_get_path (NEMO_FILE (l->data));
gchar *path = get_path (action, NEMO_FILE (l->data));
if (path)
str = _score_append (action, str, path);
str = score_append (action, str, path);
g_free (path);
str = insert_quote (action, str);
first = FALSE;
Expand All @@ -847,11 +896,8 @@ get_insertion_string (NemoAction *action, TokenType token_type, GList *selection
str = insert_separator (action, str);
str = insert_quote (action, str);
gchar *uri = nemo_file_get_uri (NEMO_FILE (l->data));
gchar *escaped = g_uri_unescape_string (uri, NULL);
if (escaped)
str = _score_append (action, str, escaped);
str = score_append (action, str, uri);
g_free (uri);
g_free (escaped);
str = insert_quote (action, str);
first = FALSE;
}
Expand All @@ -863,24 +909,24 @@ get_insertion_string (NemoAction *action, TokenType token_type, GList *selection
;
default_parent_path:
;
gchar *path = nemo_file_get_path (parent);
gchar *path = get_path (action, parent);
if (path == NULL) {
gchar *name = nemo_file_get_display_name (parent);
if (g_strcmp0 (name, "x-nemo-desktop") == 0)
path = nemo_get_desktop_directory ();
else
path = g_strdup_printf ("");
path = g_strdup ("");
g_free (name);
}
str = insert_quote (action, str);
str = _score_append (action, str, path);
str = score_append (action, str, path);
str = insert_quote (action, str);
g_free (path);
break;
case TOKEN_FILE_DISPLAY_NAME:
if (g_list_length (selection) > 0) {
gchar *file_display_name = nemo_file_get_display_name (NEMO_FILE (selection->data));
str = _score_append (action, str, file_display_name);
str = score_append (action, str, file_display_name);
g_free (file_display_name);
} else {
goto default_parent_display_name;
Expand All @@ -898,7 +944,7 @@ get_insertion_string (NemoAction *action, TokenType token_type, GList *selection
parent_display_name = nemo_file_get_display_name (parent);
g_free (real_display_name);
str = insert_quote (action, str);
str = _score_append (action, str, parent_display_name);
str = score_append (action, str, parent_display_name);
str = insert_quote (action, str);
g_free (parent_display_name);
break;
Expand All @@ -908,9 +954,9 @@ get_insertion_string (NemoAction *action, TokenType token_type, GList *selection
if (!first)
str = insert_separator (action, str);
str = insert_quote (action, str);
gchar *dev = get_device_path (NEMO_FILE (l->data));
gchar *dev = get_device_path (action, NEMO_FILE (l->data));
if (dev)
str = _score_append (action, str, dev);
str = score_append (action, str, dev);
g_free (dev);
str = insert_quote (action, str);
first = FALSE;
Expand Down
1 change: 1 addition & 0 deletions libnemo-private/nemo-action.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ struct _NemoAction {
GList *dbus;
gboolean dbus_satisfied;
gboolean escape_underscores;
gboolean escape_space;
};

struct _NemoActionClass {
Expand Down

1 comment on commit 98c62e6

@rik-shaw
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied my comment from here: https://bugs.launchpad.net/linuxmint/+bug/1330225

I think that "Quote=double" works fine for our nemo actions. Note from #3 in that above bug that the user had "Quotes=double". It is Quote not Quotes. So, not sure why the "EscapeSpaces" is needed. In our testing with "Quote=double" it works for files with SPACES in name and ALSO for multiple files.

Please sign in to comment.