| 1 |
/* config-xdp.c |
| 2 |
* |
| 3 |
* Copyright 2024 The Libproxy Team |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
* |
| 19 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <gio/gio.h> |
| 23 |
|
| 24 |
#include "config-xdp.h" |
| 25 |
|
| 26 |
#include "px-manager.h" |
| 27 |
#include "px-plugin-config.h" |
| 28 |
|
| 29 |
static void px_config_iface_init (PxConfigInterface *iface); |
| 30 |
|
| 31 |
struct _PxConfigXdp { |
| 32 |
GObject parent_instance; |
| 33 |
gboolean available; |
| 34 |
GDBusProxy *proxy_resolver; |
| 35 |
}; |
| 36 |
|
| 37 |
G_DEFINE_FINAL_TYPE_WITH_CODE (PxConfigXdp, |
| 38 |
px_config_xdp, |
| 39 |
G_TYPE_OBJECT, |
| 40 |
G_IMPLEMENT_INTERFACE (PX_TYPE_CONFIG, px_config_iface_init)) |
| 41 |
|
| 42 |
enum { |
| 43 |
PROP_0, |
| 44 |
PROP_CONFIG_OPTION |
| 45 |
}; |
| 46 |
|
| 47 |
static void |
| 48 |
px_config_xdp_init (PxConfigXdp *self) |
| 49 |
{ |
| 50 |
g_autoptr (GDBusConnection) connection = NULL; |
| 51 |
g_autoptr (GError) error = NULL; |
| 52 |
g_autofree char *path = g_build_filename (g_get_user_runtime_dir (), "flatpak-info", NULL); |
| 53 |
|
| 54 |
self->available = FALSE; |
| 55 |
|
| 56 |
/* Test for Flatpak or Snap Enivronments */ |
| 57 |
if (!g_file_test (path, G_FILE_TEST_EXISTS) && !g_getenv ("SNAP_NAME")) |
| 58 |
return; |
| 59 |
|
| 60 |
connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error); |
| 61 |
if (error) { |
| 62 |
g_debug ("Could not access dbus session: %s", error->message); |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
self->proxy_resolver = g_dbus_proxy_new_sync (connection, G_DBUS_PROXY_FLAGS_NONE, NULL, "org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop", "org.freedesktop.portal.ProxyResolver", NULL, &error); |
| 67 |
if (error) { |
| 68 |
g_warning ("Could not access proxy resolver: %s", error->message); |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
self->available = TRUE; |
| 73 |
} |
| 74 |
|
| 75 |
static void |
| 76 |
px_config_xdp_dispose (GObject *object) |
| 77 |
{ |
| 78 |
PxConfigXdp *self = PX_CONFIG_XDP (object); |
| 79 |
|
| 80 |
g_clear_object (&self->proxy_resolver); |
| 81 |
|
| 82 |
G_OBJECT_CLASS (px_config_xdp_parent_class)->dispose (object); |
| 83 |
} |
| 84 |
|
| 85 |
static void |
| 86 |
px_config_xdp_set_property (GObject *object, |
| 87 |
guint prop_id, |
| 88 |
const GValue *value, |
| 89 |
GParamSpec *pspec) |
| 90 |
{ |
| 91 |
switch (prop_id) { |
| 92 |
case PROP_CONFIG_OPTION: |
| 93 |
break; |
| 94 |
|
| 95 |
default: |
| 96 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 97 |
break; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
static void |
| 102 |
px_config_xdp_get_property (GObject *object, |
| 103 |
guint prop_id, |
| 104 |
GValue *value, |
| 105 |
GParamSpec *pspec) |
| 106 |
{ |
| 107 |
switch (prop_id) { |
| 108 |
case PROP_CONFIG_OPTION: |
| 109 |
break; |
| 110 |
|
| 111 |
default: |
| 112 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 113 |
break; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
static void |
| 118 |
px_config_xdp_class_init (PxConfigXdpClass *klass) |
| 119 |
{ |
| 120 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 121 |
|
| 122 |
object_class->dispose = px_config_xdp_dispose; |
| 123 |
object_class->set_property = px_config_xdp_set_property; |
| 124 |
object_class->get_property = px_config_xdp_get_property; |
| 125 |
|
| 126 |
g_object_class_override_property (object_class, PROP_CONFIG_OPTION, "config-option"); |
| 127 |
} |
| 128 |
|
| 129 |
static void |
| 130 |
px_config_xdp_get_config (PxConfig *config, |
| 131 |
GUri *uri, |
| 132 |
GStrvBuilder *builder) |
| 133 |
{ |
| 134 |
g_autoptr (GVariant) var = NULL; |
| 135 |
g_autoptr (GError) error = NULL; |
| 136 |
g_autoptr (GVariantIter) iter = NULL; |
| 137 |
PxConfigXdp *self = PX_CONFIG_XDP (config); |
| 138 |
g_autofree char *uri_str = NULL; |
| 139 |
const char *str; |
| 140 |
|
| 141 |
if (!self->available) |
| 142 |
return; |
| 143 |
|
| 144 |
uri_str = g_uri_to_string (uri); |
| 145 |
var = g_dbus_proxy_call_sync (self->proxy_resolver, "Lookup", g_variant_new ("(s)", uri_str), 0, -1, NULL, &error); |
| 146 |
if (error) { |
| 147 |
g_warning ("Could not query proxy: %s", error->message); |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
g_variant_get (var, "(as)", &iter); |
| 152 |
while (g_variant_iter_loop (iter, "s", &str)) { |
| 153 |
px_strv_builder_add_proxy (builder, str); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
static void |
| 158 |
px_config_iface_init (PxConfigInterface *iface) |
| 159 |
{ |
| 160 |
iface->name = "config-xdp"; |
| 161 |
iface->priority = PX_CONFIG_PRIORITY_DEFAULT; |
| 162 |
iface->get_config = px_config_xdp_get_config; |
| 163 |
} |