diff --git a/gio/Makefile.am b/gio/Makefile.am index 58d51b8ff..dd5039999 100644 --- a/gio/Makefile.am +++ b/gio/Makefile.am @@ -354,6 +354,8 @@ $(xdp_dbus_built_sources) : $(srcdir)/org.freedesktop.portal.NetworkMonitor.xml $(NULL) portal_sources = \ + gportalsupport.c \ + gportalsupport.h \ gnetworkmonitorportal.c \ gnetworkmonitorportal.h \ $(xdp_dbus_built_sources) \ diff --git a/gio/gappinfo.c b/gio/gappinfo.c index 963607b2a..0ea80510f 100644 --- a/gio/gappinfo.c +++ b/gio/gappinfo.c @@ -31,6 +31,7 @@ #include "glibintl.h" #include #include +#include "gportalsupport.h" /** @@ -669,26 +670,6 @@ g_app_info_should_show (GAppInfo *appinfo) return (* iface->should_show) (appinfo); } -static gboolean -should_use_portal (void) -{ - const char *use_portal; - char *path; - - path = g_strdup_printf ("/run/user/%d/flatpak-info", getuid()); - if (g_file_test (path, G_FILE_TEST_EXISTS)) - use_portal = "1"; - else - { - use_portal = g_getenv ("GTK_USE_PORTAL"); - if (!use_portal) - use_portal = ""; - } - g_free (path); - - return g_str_equal (use_portal, "1"); -} - static gboolean launch_default_with_portal (const char *uri, GAppLaunchContext *context, @@ -754,7 +735,7 @@ g_app_info_launch_default_for_uri (const char *uri, GList l; gboolean res; - if (should_use_portal ()) + if (glib_should_use_portal ()) return launch_default_with_portal (uri, launch_context, error); /* g_file_query_default_handler() calls diff --git a/gio/gnetworkmonitorportal.c b/gio/gnetworkmonitorportal.c index 655f787c5..d56c7fb76 100644 --- a/gio/gnetworkmonitorportal.c +++ b/gio/gnetworkmonitorportal.c @@ -23,6 +23,7 @@ #include "giomodule-priv.h" #include "gnetworkmonitor.h" #include "xdp-dbus.h" +#include "gportalsupport.h" static void g_network_monitor_portal_iface_init (GNetworkMonitorInterface *iface); @@ -104,46 +105,6 @@ proxy_changed (XdpNetworkMonitor *proxy, g_signal_emit_by_name (nm, "network-changed", available); } - -static gboolean -should_use_portal (void) -{ - const char *use_portal; - char *path; - - path = g_strdup_printf ("/run/user/%d/flatpak-info", getuid()); - if (g_file_test (path, G_FILE_TEST_EXISTS)) - use_portal = "1"; - else - { - use_portal = g_getenv ("GTK_USE_PORTAL"); - if (!use_portal) - use_portal = ""; - } - g_free (path); - - return g_str_equal (use_portal, "1"); -} - -static gboolean -network_available_in_sandbox (void) -{ - char *path; - g_autoptr(GKeyFile) keyfile = g_key_file_new (); - - path = g_strdup_printf ("/run/user/%d/flatpak-info", getuid()); - if (g_key_file_load_from_file (keyfile, path, G_KEY_FILE_NONE, NULL)) - { - g_auto(GStrv) shared = NULL; - - shared = g_key_file_get_string_list (keyfile, "Context", "shared", NULL, NULL); - - return g_strv_contains ((const char * const *)shared, "network"); - } - - return TRUE; -} - static gboolean g_network_monitor_portal_initable_init (GInitable *initable, GCancellable *cancellable, @@ -153,7 +114,7 @@ g_network_monitor_portal_initable_init (GInitable *initable, XdpNetworkMonitor *proxy; gchar *name_owner = NULL; - if (!should_use_portal ()) + if (!glib_should_use_portal ()) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Not using portals"); return FALSE; @@ -181,7 +142,7 @@ g_network_monitor_portal_initable_init (GInitable *initable, g_signal_connect (G_OBJECT (proxy), "changed", G_CALLBACK (proxy_changed), nm); nm->priv->proxy = proxy; - nm->priv->network_available = network_available_in_sandbox (); + nm->priv->network_available = glib_network_available_in_sandbox (); return TRUE; } diff --git a/gio/gportalsupport.c b/gio/gportalsupport.c new file mode 100644 index 000000000..d7ba523c8 --- /dev/null +++ b/gio/gportalsupport.c @@ -0,0 +1,69 @@ +/* GIO - GLib Input, Output and Streaming Library + * + * Copyright 2016 Red Hat, Inc. + * + * This library 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 . + */ + +#include "config.h" + +#include + +#include "gportalsupport.h" + +gboolean +glib_should_use_portal (void) +{ + const char *use_portal; + char *path; + + path = g_strdup_printf ("/run/user/%d/flatpak-info", getuid()); + if (g_file_test (path, G_FILE_TEST_EXISTS)) + use_portal = "1"; + else + { + use_portal = g_getenv ("GTK_USE_PORTAL"); + if (!use_portal) + use_portal = ""; + } + g_free (path); + + return g_str_equal (use_portal, "1"); +} + +gboolean +glib_network_available_in_sandbox (void) +{ + char *path; + GKeyFile *keyfile; + gboolean available = TRUE; + + path = g_strdup_printf ("/run/user/%d/flatpak-info", getuid()); + + keyfile = g_key_file_new (); + if (g_key_file_load_from_file (keyfile, path, G_KEY_FILE_NONE, NULL)) + { + char **shared = NULL; + + shared = g_key_file_get_string_list (keyfile, "Context", "shared", NULL, NULL); + available = g_strv_contains ((const char * const *)shared, "network"); + g_strfreev (shared); + } + + g_key_file_free (keyfile); + g_free (path); + + return available; +} + diff --git a/gio/gportalsupport.h b/gio/gportalsupport.h new file mode 100644 index 000000000..a92e07ca7 --- /dev/null +++ b/gio/gportalsupport.h @@ -0,0 +1,30 @@ +/* GIO - GLib Input, Output and Streaming Library + * + * Copyright 2016 Red Hat, Inc. + * + * This library 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 . + */ + +#ifndef __G_PORTAL_SUPPORT_H__ + +#include + +G_BEGIN_DECLS + +gboolean glib_should_use_portal (void); +gboolean glib_network_available_in_sandbox (void); + +G_END_DECLS + +#endif