Commit 8c645266 authored by Graeme Gott's avatar Graeme Gott

Use shared field code expansion.

parent 27b7ca91
......@@ -54,6 +54,7 @@ static std::string normalize(const gchar* string)
//-----------------------------------------------------------------------------
#if !LIBXFCE4UTIL_CHECK_VERSION(4,15,1)
static void replace_with_quoted_string(std::string& command, std::string::size_type& index, const gchar* unquoted)
{
if (!exo_str_is_empty(unquoted))
......@@ -91,11 +92,57 @@ static void replace_with_quoted_string(std::string& command, std::string::size_t
//-----------------------------------------------------------------------------
static void replace_and_free_with_quoted_string(std::string& command, std::string::size_type& index, gchar* unquoted)
static gchar* xfce_expand_desktop_entry_field_codes(const gchar* command, GSList* /*uri_list*/,
const gchar* icon, const gchar* name, const gchar* uri, gboolean requires_terminal)
{
replace_with_quoted_string(command, index, unquoted);
g_free(unquoted);
std::string expanded(command);
if (requires_terminal)
{
expanded.insert(0, "exo-open --launch TerminalEmulator ");
}
std::string::size_type length = expanded.length() - 1;
for (std::string::size_type i = 0; i < length; ++i)
{
if (G_UNLIKELY(command[i] == '%'))
{
switch (command[i + 1])
{
case 'i':
replace_with_quoted_string(expanded, i, "--icon ", icon);
break;
case 'c':
replace_with_quoted_string(expanded, i, name);
break;
case 'k':
replace_with_quoted_string(expanded, i, uri);
break;
case '%':
expanded.erase(i, 1);
break;
case 'f':
// unsupported, pass in a single file dropped on launcher
case 'F':
// unsupported, pass in a list of files dropped on launcher
case 'u':
// unsupported, pass in a single URL dropped on launcher
case 'U':
// unsupported, pass in a list of URLs dropped on launcher
default:
expanded.erase(i, 2);
break;
}
length = expanded.length() - 1;
}
}
return g_strdup(expanded.c_str());
}
#endif
//-----------------------------------------------------------------------------
......@@ -245,63 +292,27 @@ void Launcher::hide()
void Launcher::run(GdkScreen* screen) const
{
// Expand the field codes
const gchar* string = garcon_menu_item_get_command(m_item);
if (exo_str_is_empty(string))
{
return;
}
std::string command(string);
if (garcon_menu_item_requires_terminal(m_item))
{
command.insert(0, "exo-open --launch TerminalEmulator ");
}
// Expand the field codes
std::string::size_type length = command.length() - 1;
for (std::string::size_type i = 0; i < length; ++i)
{
if (G_UNLIKELY(command[i] == '%'))
{
switch (command[i + 1])
{
case 'i':
replace_with_quoted_string(command, i, "--icon ", garcon_menu_item_get_icon_name(m_item));
break;
case 'c':
replace_with_quoted_string(command, i, garcon_menu_item_get_name(m_item));
break;
case 'k':
replace_and_free_with_quoted_string(command, i, garcon_menu_item_get_uri(m_item));
break;
case '%':
command.erase(i, 1);
break;
case 'f':
// unsupported, pass in a single file dropped on launcher
case 'F':
// unsupported, pass in a list of files dropped on launcher
case 'u':
// unsupported, pass in a single URL dropped on launcher
case 'U':
// unsupported, pass in a list of URLs dropped on launcher
default:
command.erase(i, 2);
break;
}
length = command.length() - 1;
}
}
gchar* uri = garcon_menu_item_get_uri(m_item);
gchar* command = xfce_expand_desktop_entry_field_codes(string,
nullptr,
garcon_menu_item_get_icon_name(m_item),
garcon_menu_item_get_name(m_item),
uri,
garcon_menu_item_requires_terminal(m_item));
g_free(uri);
// Parse and spawn command
gchar** argv;
bool result = false;
GError* error = nullptr;
if (g_shell_parse_argv(command.c_str(), nullptr, &argv, &error))
if (g_shell_parse_argv(command, nullptr, &argv, &error))
{
result = xfce_spawn_on_screen(screen,
garcon_menu_item_get_path(m_item),
......@@ -315,67 +326,38 @@ void Launcher::run(GdkScreen* screen) const
if (G_UNLIKELY(!result))
{
xfce_dialog_show_error(nullptr, error, _("Failed to execute command \"%s\"."), string);
xfce_dialog_show_error(nullptr, error, _("Failed to execute command \"%s\"."), command);
g_error_free(error);
}
g_free(command);
}
//-----------------------------------------------------------------------------
void Launcher::run(GdkScreen* screen, DesktopAction* action) const
{
// Expand the field codes
const gchar* string = action->get_command();
if (exo_str_is_empty(string))
{
return;
}
std::string command(string);
// Expand the field codes
std::string::size_type length = command.length() - 1;
for (std::string::size_type i = 0; i < length; ++i)
{
if (G_UNLIKELY(command[i] == '%'))
{
switch (command[i + 1])
{
case 'i':
replace_with_quoted_string(command, i, "--icon ", action->get_icon());
break;
case 'c':
replace_with_quoted_string(command, i, action->get_name());
break;
case 'k':
replace_and_free_with_quoted_string(command, i, garcon_menu_item_get_uri(m_item));
break;
case '%':
command.erase(i, 1);
break;
case 'f':
// unsupported, pass in a single file dropped on launcher
case 'F':
// unsupported, pass in a list of files dropped on launcher
case 'u':
// unsupported, pass in a single URL dropped on launcher
case 'U':
// unsupported, pass in a list of URLs dropped on launcher
default:
command.erase(i, 2);
break;
}
length = command.length() - 1;
}
}
gchar* uri = garcon_menu_item_get_uri(m_item);
gchar* command = xfce_expand_desktop_entry_field_codes(string,
nullptr,
action->get_icon(),
action->get_name(),
uri,
false);
g_free(uri);
// Parse and spawn command
gchar** argv;
bool result = false;
GError* error = nullptr;
if (g_shell_parse_argv(command.c_str(), nullptr, &argv, &error))
if (g_shell_parse_argv(command, nullptr, &argv, &error))
{
result = xfce_spawn_on_screen(screen,
garcon_menu_item_get_path(m_item),
......@@ -389,9 +371,11 @@ void Launcher::run(GdkScreen* screen, DesktopAction* action) const
if (G_UNLIKELY(!result))
{
xfce_dialog_show_error(nullptr, error, _("Failed to execute command \"%s\"."), string);
xfce_dialog_show_error(nullptr, error, _("Failed to execute command \"%s\"."), command);
g_error_free(error);
}
g_free(command);
}
//-----------------------------------------------------------------------------
......
  • @gottcode: I had prepared a MR for that, which did not require to duplicate the code: Tamaranch/xfce4-whiskermenu-plugin@d65c88ae
    (cited in xfce/libxfce4util!2 (merged))

    Furthermore, it's possible that the version of libxfce4util changes to 4.15.2: I was waiting for an answer from @ochosi.

    Edited by Gaël Bonithon
  • Unfortunately, the duplicated code is still necessary, because I am not willing to have Whisker Menu depend on Xfce 4.16. I support older distros, so the minimum requirement is staying at Xfce 4.12.

    Bumping the ifdef to 4.15.2 is easy enough if that ends up being the version with the function.

  • Yes, in the meantime I read this more carefully (and also d1d23c4f), so I understand…

  • mentioned in merge request xfce/libxfce4util!2 (merged)

    Toggle commit list
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment