Add portal helpers

These are private helper functions that will be used in
the following commits to get information about whether
we are running in a flatpak sandbox, etc.

We allow the use of GTK_USE_PORTAL=1 in the environment
to force the use of portals. This can be useful for
testing and debugging portal interaction.

https://bugzilla.gnome.org/show_bug.cgi?id=768498
This commit is contained in:
Matthias Clasen 2016-07-06 23:34:55 -04:00
parent 4586434346
commit 78ef32110a
3 changed files with 116 additions and 0 deletions

View File

@ -424,6 +424,8 @@ libgio_2_0_la_SOURCES = \
gpollableutils.c \ gpollableutils.c \
gpollfilemonitor.c \ gpollfilemonitor.c \
gpollfilemonitor.h \ gpollfilemonitor.h \
gportalsupport.c \
gportalsupport.h \
gproxy.c \ gproxy.c \
gproxyaddress.c \ gproxyaddress.c \
gproxyaddressenumerator.c \ gproxyaddressenumerator.c \

84
gio/gportalsupport.c Normal file
View File

@ -0,0 +1,84 @@
/* 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 <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "gportalsupport.h"
static gboolean flatpak_info_read;
static gboolean use_portal;
static gboolean network_available;
static void
read_flatpak_info (void)
{
char *path;
if (flatpak_info_read)
return;
flatpak_info_read = TRUE;
path = g_build_filename (g_get_user_runtime_dir (), "flatpak-info", NULL);
if (g_file_test (path, G_FILE_TEST_EXISTS))
{
GKeyFile *keyfile;
use_portal = TRUE;
network_available = FALSE;
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);
if (shared)
{
network_available = g_strv_contains ((const char * const *)shared, "network");
g_strfreev (shared);
}
}
}
else
{
const char *var;
var = g_getenv ("GTK_USE_PORTAL");
if (var && var[0] == '1')
use_portal = TRUE;
network_available = TRUE;
}
g_free (path);
}
gboolean
glib_should_use_portal (void)
{
read_flatpak_info ();
return use_portal;
}
gboolean
glib_network_available_in_sandbox (void)
{
read_flatpak_info ();
return network_available;
}

30
gio/gportalsupport.h Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef __G_PORTAL_SUPPORT_H__
#include <glib.h>
G_BEGIN_DECLS
gboolean glib_should_use_portal (void);
gboolean glib_network_available_in_sandbox (void);
G_END_DECLS
#endif