Skip to content

Commit

Permalink
Add shim xdg-open implementation
Browse files Browse the repository at this point in the history
As many 3rd party applications rely on xdg-utils for interacting
with the desktop environment, it makes sense for runtimes to provide
the expected tools. However there's no good reason to ship the usual
shell scripts, as the desktop abstraction is already provided by the
portal layer when running in a sandbox. Just add a small command line
tool that directly calls out to the portal.
  • Loading branch information
fmuellner committed Mar 22, 2017
1 parent e2efcf6 commit 1cf98ef
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
bin_PROGRAMS = xdg-open

xdg_open_SOURCES = xdg-open.c

xdg_open_CFLAGS = $(FLATPAK_XDG_UTILS_CFLAGS)
xdg_open_LDADD = $(FLATPAK_XDG_UTILS_LIBS)
122 changes: 122 additions & 0 deletions src/xdg-open.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright © 2017 Red Hat, Inc
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Florian Müllner <[email protected]>
*/

#include <gio/gio.h>

#include <glib/gi18n.h>

#define PORTAL_BUS_NAME "org.freedesktop.portal.Desktop"
#define PORTAL_OBJECT_PATH "/org/freedesktop/portal/desktop"
#define PORTAL_IFACE_NAME "org.freedesktop.portal.OpenURI"

static char **uris = NULL;
static gboolean show_help = FALSE;
static gboolean show_version = FALSE;

static GOptionEntry entries[] = {
/* Compat options with "real" xdg-open */
{ "manual", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &show_help, NULL, NULL },
{ "version", 0, 0, G_OPTION_ARG_NONE, &show_version, N_("Show program version"), NULL },

{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &uris, NULL, NULL }
};

int
main (int argc, char *argv[])
{
GOptionContext *context;
GError *error = NULL;
GDBusConnection *connection;
GVariantBuilder opt_builder;

context = g_option_context_new ("{ file | URL }");

g_option_context_add_main_entries (context, entries, NULL);
g_option_context_parse (context, &argc, &argv, &error);

if (error != NULL)
{
g_printerr ("Error parsing commandline options: %s\n", error->message);
g_printerr ("\n");
g_printerr ("Try \"%s --help\" for more information.\n", g_get_prgname ());

g_error_free (error);
return 1;
}

if (show_version)
{
g_print ("%s\n", PACKAGE_VERSION);

return 0;
}

if (show_help || uris == NULL || g_strv_length (uris) > 1)
{
char *help = g_option_context_get_help (context, TRUE, NULL);
g_print ("%s\n", help);

g_free (help);
return 0;
}

connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);

if (connection == NULL)
{
if (error)
g_printerr ("Failed to connect to session bus: %s", error->message);
else
g_printerr ("Failed to connect to session bus");

g_clear_pointer (&error, g_error_free);
return 3;
}

g_variant_builder_init (&opt_builder, G_VARIANT_TYPE_VARDICT);

g_dbus_connection_call_sync (connection,
PORTAL_BUS_NAME,
PORTAL_OBJECT_PATH,
PORTAL_IFACE_NAME,
"OpenURI",
g_variant_new ("(ss@a{sv})",
"", *uris,
g_variant_builder_end (&opt_builder)),
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);

if (error)
{
g_printerr ("Failed to call portal: %s\n", error->message);

g_object_unref (connection);
g_error_free (error);

return 4;
}

g_object_unref (connection);

return 0;
}

0 comments on commit 1cf98ef

Please sign in to comment.