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 <http://www.gnu.org/licenses/>.
21 #include "gnetworkmonitorportal.h"
22 #include "ginitable.h"
23 #include "giomodule-priv.h"
24 #include "gnetworkmonitor.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
);
35 PROP_NETWORK_AVAILABLE
,
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
,
59 g_network_monitor_portal_init (GNetworkMonitorPortal
*nm
)
61 nm
->priv
= g_network_monitor_portal_get_instance_private (nm
);
65 g_network_monitor_portal_get_property (GObject
*object
,
70 GNetworkMonitorPortal
*nm
= G_NETWORK_MONITOR_PORTAL (object
);
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
));
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
));
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
);
94 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
100 proxy_changed (GXdpNetworkMonitor
*proxy
,
102 GNetworkMonitorPortal
*nm
)
104 if (nm
->priv
->network_available
)
105 g_signal_emit_by_name (nm
, "network-changed", available
);
109 g_network_monitor_portal_initable_init (GInitable
*initable
,
110 GCancellable
*cancellable
,
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");
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",
133 name_owner
= g_dbus_proxy_get_name_owner (G_DBUS_PROXY (proxy
));
137 g_object_unref (proxy
);
140 G_DBUS_ERROR_NAME_HAS_NO_OWNER
,
141 "Desktop portal not found");
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 ();
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
);
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");
178 g_network_monitor_portal_iface_init (GNetworkMonitorInterface
*monitor_iface
)
183 g_network_monitor_portal_initable_iface_init (GInitableIface
*iface
)
185 iface
->init
= g_network_monitor_portal_initable_init
;