Add myself to the DOAP for GLib
[glib.git] / gio / gnetworkmonitorportal.c
blob2686834702f6b34f44c28e8092c3c568c6263a03
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.1 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 <http://www.gnu.org/licenses/>.
19 #include "config.h"
21 #include "gnetworkmonitorportal.h"
22 #include "ginitable.h"
23 #include "giomodule-priv.h"
24 #include "gnetworkmonitor.h"
25 #include "xdp-dbus.h"
26 #include "gportalsupport.h"
29 static void g_network_monitor_portal_iface_init (GNetworkMonitorInterface *iface);
30 static void g_network_monitor_portal_initable_iface_init (GInitableIface *iface);
32 enum
34 PROP_0,
35 PROP_NETWORK_AVAILABLE,
36 PROP_NETWORK_METERED,
37 PROP_CONNECTIVITY
40 struct _GNetworkMonitorPortalPrivate
42 GXdpNetworkMonitor *proxy;
43 gboolean network_available;
46 G_DEFINE_TYPE_WITH_CODE (GNetworkMonitorPortal, g_network_monitor_portal, G_TYPE_NETWORK_MONITOR_BASE,
47 G_ADD_PRIVATE (GNetworkMonitorPortal)
48 G_IMPLEMENT_INTERFACE (G_TYPE_NETWORK_MONITOR,
49 g_network_monitor_portal_iface_init)
50 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
51 g_network_monitor_portal_initable_iface_init)
52 _g_io_modules_ensure_extension_points_registered ();
53 g_io_extension_point_implement (G_NETWORK_MONITOR_EXTENSION_POINT_NAME,
54 g_define_type_id,
55 "portal",
56 40))
58 static void
59 g_network_monitor_portal_init (GNetworkMonitorPortal *nm)
61 nm->priv = g_network_monitor_portal_get_instance_private (nm);
64 static void
65 g_network_monitor_portal_get_property (GObject *object,
66 guint prop_id,
67 GValue *value,
68 GParamSpec *pspec)
70 GNetworkMonitorPortal *nm = G_NETWORK_MONITOR_PORTAL (object);
72 switch (prop_id)
74 case PROP_NETWORK_AVAILABLE:
75 g_value_set_boolean (value,
76 nm->priv->network_available &&
77 gxdp_network_monitor_get_available (nm->priv->proxy));
78 break;
80 case PROP_NETWORK_METERED:
81 g_value_set_boolean (value,
82 nm->priv->network_available &&
83 gxdp_network_monitor_get_metered (nm->priv->proxy));
84 break;
86 case PROP_CONNECTIVITY:
87 g_value_set_enum (value,
88 nm->priv->network_available
89 ? gxdp_network_monitor_get_connectivity (nm->priv->proxy)
90 : G_NETWORK_CONNECTIVITY_LOCAL);
91 break;
93 default:
94 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
95 break;
99 static void
100 proxy_changed (GXdpNetworkMonitor *proxy,
101 gboolean available,
102 GNetworkMonitorPortal *nm)
104 if (nm->priv->network_available)
105 g_signal_emit_by_name (nm, "network-changed", available);
108 static gboolean
109 g_network_monitor_portal_initable_init (GInitable *initable,
110 GCancellable *cancellable,
111 GError **error)
113 GNetworkMonitorPortal *nm = G_NETWORK_MONITOR_PORTAL (initable);
114 GXdpNetworkMonitor *proxy;
115 gchar *name_owner = NULL;
117 if (!glib_should_use_portal ())
119 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Not using portals");
120 return FALSE;
123 proxy = gxdp_network_monitor_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
124 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START
125 | G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES,
126 "org.freedesktop.portal.Desktop",
127 "/org/freedesktop/portal/desktop",
128 cancellable,
129 error);
130 if (!proxy)
131 return FALSE;
133 name_owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (proxy));
135 if (!name_owner)
137 g_object_unref (proxy);
138 g_set_error (error,
139 G_DBUS_ERROR,
140 G_DBUS_ERROR_NAME_HAS_NO_OWNER,
141 "Desktop portal not found");
142 return FALSE;
145 g_free (name_owner);
147 g_signal_connect (proxy, "changed", G_CALLBACK (proxy_changed), nm);
148 nm->priv->proxy = proxy;
149 nm->priv->network_available = glib_network_available_in_sandbox ();
151 return TRUE;
154 static void
155 g_network_monitor_portal_finalize (GObject *object)
157 GNetworkMonitorPortal *nm = G_NETWORK_MONITOR_PORTAL (object);
159 g_clear_object (&nm->priv->proxy);
161 G_OBJECT_CLASS (g_network_monitor_portal_parent_class)->finalize (object);
164 static void
165 g_network_monitor_portal_class_init (GNetworkMonitorPortalClass *class)
167 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
169 gobject_class->finalize = g_network_monitor_portal_finalize;
170 gobject_class->get_property = g_network_monitor_portal_get_property;
172 g_object_class_override_property (gobject_class, PROP_NETWORK_AVAILABLE, "network-available");
173 g_object_class_override_property (gobject_class, PROP_NETWORK_METERED, "network-metered");
174 g_object_class_override_property (gobject_class, PROP_CONNECTIVITY, "connectivity");
177 static void
178 g_network_monitor_portal_iface_init (GNetworkMonitorInterface *monitor_iface)
182 static void
183 g_network_monitor_portal_initable_iface_init (GInitableIface *iface)
185 iface->init = g_network_monitor_portal_initable_init;