gtestutils: Fix a typo in a documentation comment
[glib.git] / gio / gproxyresolverportal.c
blobff0965dea1476143f9ac7c58565fbc2f002252b7
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright 2016 Red Hat, Inc.
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 of the License, or (at your option) any later version.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see
17 * <http://www.gnu.org/licenses/>.
20 #include "config.h"
22 #include "xdp-dbus.h"
23 #include "giomodule-priv.h"
24 #include "gportalsupport.h"
25 #include "gproxyresolverportal.h"
27 struct _GProxyResolverPortal {
28 GObject parent_instance;
30 GXdpProxyResolver *resolver;
31 gboolean network_available;
34 static void g_proxy_resolver_portal_iface_init (GProxyResolverInterface *iface);
36 G_DEFINE_TYPE_WITH_CODE (GProxyResolverPortal, g_proxy_resolver_portal, G_TYPE_OBJECT,
37 G_IMPLEMENT_INTERFACE (G_TYPE_PROXY_RESOLVER,
38 g_proxy_resolver_portal_iface_init)
39 _g_io_modules_ensure_extension_points_registered ();
40 g_io_extension_point_implement (G_PROXY_RESOLVER_EXTENSION_POINT_NAME,
41 g_define_type_id,
42 "portal",
43 90))
45 static void
46 g_proxy_resolver_portal_init (GProxyResolverPortal *resolver)
48 resolver->resolver = gxdp_proxy_resolver_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
49 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
50 "org.freedesktop.portal.Desktop",
51 "/org/freedesktop/portal/desktop",
52 NULL,
53 NULL);
55 resolver->network_available = glib_network_available_in_sandbox ();
58 static gboolean
59 g_proxy_resolver_portal_is_supported (GProxyResolver *object)
61 GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (object);
62 char *name_owner;
63 gboolean has_portal;
65 if (!glib_should_use_portal () || !resolver->resolver)
66 return FALSE;
68 name_owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (resolver->resolver));
69 has_portal = name_owner != NULL;
70 g_free (name_owner);
72 return has_portal;
75 static const char *no_proxy[2] = { "direct://", NULL };
77 static gchar **
78 g_proxy_resolver_portal_lookup (GProxyResolver *proxy_resolver,
79 const gchar *uri,
80 GCancellable *cancellable,
81 GError **error)
83 GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (proxy_resolver);
84 char **proxy = NULL;
86 if (!gxdp_proxy_resolver_call_lookup_sync (resolver->resolver,
87 uri,
88 &proxy,
89 cancellable,
90 error))
91 return NULL;
93 if (!resolver->network_available)
95 g_strfreev (proxy);
96 proxy = g_strdupv ((gchar **)no_proxy);
99 return proxy;
102 static void
103 lookup_done (GObject *source,
104 GAsyncResult *result,
105 gpointer data)
107 GTask *task = data;
108 GError *error = NULL;
109 gchar **proxies = NULL;
111 if (!gxdp_proxy_resolver_call_lookup_finish (GXDP_PROXY_RESOLVER (source),
112 &proxies,
113 result,
114 &error))
115 g_task_return_error (task, error);
116 else
117 g_task_return_pointer (task, proxies, NULL);
119 g_object_unref (task);
122 static void
123 g_proxy_resolver_portal_lookup_async (GProxyResolver *proxy_resolver,
124 const gchar *uri,
125 GCancellable *cancellable,
126 GAsyncReadyCallback callback,
127 gpointer user_data)
129 GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (proxy_resolver);
130 GTask *task;
132 task = g_task_new (proxy_resolver, cancellable, callback, user_data);
133 gxdp_proxy_resolver_call_lookup (resolver->resolver,
134 uri,
135 cancellable,
136 lookup_done,
137 g_object_ref (task));
138 g_object_unref (task);
141 static gchar **
142 g_proxy_resolver_portal_lookup_finish (GProxyResolver *proxy_resolver,
143 GAsyncResult *result,
144 GError **error)
146 GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (proxy_resolver);
147 GTask *task = G_TASK (result);
148 char **proxies;
150 proxies = g_task_propagate_pointer (task, error);
151 if (proxies == NULL)
152 return NULL;
154 if (!resolver->network_available)
156 g_strfreev (proxies);
157 proxies = g_strdupv ((gchar **)no_proxy);
160 return proxies;
163 static void
164 g_proxy_resolver_portal_finalize (GObject *object)
166 GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (object);
168 g_clear_object (&resolver->resolver);
170 G_OBJECT_CLASS (g_proxy_resolver_portal_parent_class)->finalize (object);
173 static void
174 g_proxy_resolver_portal_class_init (GProxyResolverPortalClass *resolver_class)
176 GObjectClass *object_class;
178 object_class = G_OBJECT_CLASS (resolver_class);
179 object_class->finalize = g_proxy_resolver_portal_finalize;
182 static void
183 g_proxy_resolver_portal_iface_init (GProxyResolverInterface *iface)
185 iface->is_supported = g_proxy_resolver_portal_is_supported;
186 iface->lookup = g_proxy_resolver_portal_lookup;
187 iface->lookup_async = g_proxy_resolver_portal_lookup_async;
188 iface->lookup_finish = g_proxy_resolver_portal_lookup_finish;