portal network monitor: use GetStatus when available

Version 3 of the portal interface adds a GetStatus
method that gets all data in a single roundtrip.
Use it when available.
This commit is contained in:
Matthias Clasen 2018-08-23 23:25:48 -04:00
parent a090d8605b
commit 5c5881b2ed

View File

@ -183,14 +183,81 @@ got_connectivity (GObject *source,
}
}
static void
got_status (GObject *source,
GAsyncResult *res,
gpointer data)
{
GDBusProxy *proxy = G_DBUS_PROXY (source);
GNetworkMonitorPortal *nm = G_NETWORK_MONITOR_PORTAL (data);
GError *error = NULL;
GVariant *ret;
gboolean should_emit_changed = FALSE;
GVariant *status;
gboolean available;
gboolean metered;
GNetworkConnectivity connectivity;
ret = g_dbus_proxy_call_finish (proxy, res, &error);
if (ret == NULL)
{
g_warning ("%s", error->message);
g_clear_error (&error);
return;
}
g_variant_get (ret, "(@a{sv})", &status);
g_variant_unref (ret);
g_variant_lookup (status, "available", "b", &available);
g_variant_lookup (status, "metered", "b", &metered);
g_variant_lookup (status, "connectivity", "u", &connectivity);
g_variant_unref (status);
g_object_freeze_notify (G_OBJECT (nm));
if (nm->priv->available != available)
{
nm->priv->available = available;
g_object_notify (G_OBJECT (nm), "network-available");
should_emit_changed = TRUE;
}
if (nm->priv->metered != metered)
{
nm->priv->metered = metered;
g_object_notify (G_OBJECT (nm), "network-metered");
should_emit_changed = TRUE;
}
if (nm->priv->connectivity != connectivity)
{
nm->priv->connectivity = connectivity;
g_object_notify (G_OBJECT (nm), "connectivity");
should_emit_changed = TRUE;
}
g_object_thaw_notify (G_OBJECT (nm));
if (should_emit_changed)
g_signal_emit_by_name (nm, "network-changed", available);
}
static void
update_properties (GDBusProxy *proxy,
GNetworkMonitorPortal *nm)
{
if (nm->priv->version == 3)
{
g_dbus_proxy_call (proxy, "GetStatus", NULL, 0, -1, NULL, got_status, nm);
}
else
{
g_dbus_proxy_call (proxy, "GetConnectivity", NULL, 0, -1, NULL, got_connectivity, nm);
g_dbus_proxy_call (proxy, "GetMetered", NULL, 0, -1, NULL, got_metered, nm);
g_dbus_proxy_call (proxy, "GetAvailable", NULL, 0, -1, NULL, got_available, nm);
}
}
static void
proxy_signal (GDBusProxy *proxy,
@ -209,7 +276,7 @@ proxy_signal (GDBusProxy *proxy,
g_variant_get (parameters, "(b)", &available);
g_signal_emit_by_name (nm, "network-changed", available);
}
else if (nm->priv->version == 2)
else if (nm->priv->version == 2 || nm->priv->version == 3)
{
update_properties (proxy, nm);
}