/* * Licensed under the GNU Lesser General Public License Version 3 * * This library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the license, or * (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library. If not, see . */ // generated automatically - do not change module gio.NetworkMonitorT; public import gio.AsyncResultIF; public import gio.Cancellable; public import gio.NetworkMonitor; public import gio.NetworkMonitorIF; public import gio.SocketConnectableIF; public import gio.c.functions; public import gio.c.types; public import glib.ErrorG; public import glib.GException; public import gobject.ObjectG; public import gobject.Signals; public import std.algorithm; /** * #GNetworkMonitor provides an easy-to-use cross-platform API * for monitoring network connectivity. On Linux, the implementation * is based on the kernel's netlink interface. * * Since: 2.32 */ public template NetworkMonitorT(TStruct) { /** Get the main Gtk struct */ public GNetworkMonitor* getNetworkMonitorStruct(bool transferOwnership = false) { if (transferOwnership) ownedRef = false; return cast(GNetworkMonitor*)getStruct(); } /** * Gets the default #GNetworkMonitor for the system. * * Returns: a #GNetworkMonitor * * Since: 2.32 */ public static NetworkMonitorIF getDefault() { auto p = g_network_monitor_get_default(); if(p is null) { return null; } return ObjectG.getDObject!(NetworkMonitor, NetworkMonitorIF)(cast(GNetworkMonitor*) p); } /** * Attempts to determine whether or not the host pointed to by * @connectable can be reached, without actually trying to connect to * it. * * This may return %TRUE even when #GNetworkMonitor:network-available * is %FALSE, if, for example, @monitor can determine that * @connectable refers to a host on a local network. * * If @monitor believes that an attempt to connect to @connectable * will succeed, it will return %TRUE. Otherwise, it will return * %FALSE and set @error to an appropriate error (such as * %G_IO_ERROR_HOST_UNREACHABLE). * * Note that although this does not attempt to connect to * @connectable, it may still block for a brief period of time (eg, * trying to do multicast DNS on the local network), so if you do not * want to block, you should use g_network_monitor_can_reach_async(). * * Params: * connectable = a #GSocketConnectable * cancellable = a #GCancellable, or %NULL * * Returns: %TRUE if @connectable is reachable, %FALSE if not. * * Since: 2.32 * * Throws: GException on failure. */ public bool canReach(SocketConnectableIF connectable, Cancellable cancellable) { GError* err = null; auto p = g_network_monitor_can_reach(getNetworkMonitorStruct(), (connectable is null) ? null : connectable.getSocketConnectableStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0; if (err !is null) { throw new GException( new ErrorG(err) ); } return p; } /** * Asynchronously attempts to determine whether or not the host * pointed to by @connectable can be reached, without actually * trying to connect to it. * * For more details, see g_network_monitor_can_reach(). * * When the operation is finished, @callback will be called. * You can then call g_network_monitor_can_reach_finish() * to get the result of the operation. * * Params: * connectable = a #GSocketConnectable * cancellable = a #GCancellable, or %NULL * callback = a #GAsyncReadyCallback to call when the * request is satisfied * userData = the data to pass to callback function */ public void canReachAsync(SocketConnectableIF connectable, Cancellable cancellable, GAsyncReadyCallback callback, void* userData) { g_network_monitor_can_reach_async(getNetworkMonitorStruct(), (connectable is null) ? null : connectable.getSocketConnectableStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData); } /** * Finishes an async network connectivity test. * See g_network_monitor_can_reach_async(). * * Params: * result = a #GAsyncResult * * Returns: %TRUE if network is reachable, %FALSE if not. * * Throws: GException on failure. */ public bool canReachFinish(AsyncResultIF result) { GError* err = null; auto p = g_network_monitor_can_reach_finish(getNetworkMonitorStruct(), (result is null) ? null : result.getAsyncResultStruct(), &err) != 0; if (err !is null) { throw new GException( new ErrorG(err) ); } return p; } /** * Gets a more detailed networking state than * g_network_monitor_get_network_available(). * * If #GNetworkMonitor:network-available is %FALSE, then the * connectivity state will be %G_NETWORK_CONNECTIVITY_LOCAL. * * If #GNetworkMonitor:network-available is %TRUE, then the * connectivity state will be %G_NETWORK_CONNECTIVITY_FULL (if there * is full Internet connectivity), %G_NETWORK_CONNECTIVITY_LIMITED (if * the host has a default route, but appears to be unable to actually * reach the full Internet), or %G_NETWORK_CONNECTIVITY_PORTAL (if the * host is trapped behind a "captive portal" that requires some sort * of login or acknowledgement before allowing full Internet access). * * Note that in the case of %G_NETWORK_CONNECTIVITY_LIMITED and * %G_NETWORK_CONNECTIVITY_PORTAL, it is possible that some sites are * reachable but others are not. In this case, applications can * attempt to connect to remote servers, but should gracefully fall * back to their "offline" behavior if the connection attempt fails. * * Returns: the network connectivity state * * Since: 2.44 */ public GNetworkConnectivity getConnectivity() { return g_network_monitor_get_connectivity(getNetworkMonitorStruct()); } /** * Checks if the network is available. "Available" here means that the * system has a default route available for at least one of IPv4 or * IPv6. It does not necessarily imply that the public Internet is * reachable. See #GNetworkMonitor:network-available for more details. * * Returns: whether the network is available * * Since: 2.32 */ public bool getNetworkAvailable() { return g_network_monitor_get_network_available(getNetworkMonitorStruct()) != 0; } /** * Checks if the network is metered. * See #GNetworkMonitor:network-metered for more details. * * Returns: whether the connection is metered * * Since: 2.46 */ public bool getNetworkMetered() { return g_network_monitor_get_network_metered(getNetworkMonitorStruct()) != 0; } protected class OnNetworkChangedDelegateWrapper { static OnNetworkChangedDelegateWrapper[] listeners; void delegate(bool, NetworkMonitorIF) dlg; gulong handlerId; this(void delegate(bool, NetworkMonitorIF) dlg) { this.dlg = dlg; this.listeners ~= this; } void remove(OnNetworkChangedDelegateWrapper source) { foreach(index, wrapper; listeners) { if (wrapper.handlerId == source.handlerId) { listeners[index] = null; listeners = std.algorithm.remove(listeners, index); break; } } } } /** * Emitted when the network configuration changes. If @available is * %TRUE, then some hosts may be reachable that were not reachable * before, while others that were reachable before may no longer be * reachable. If @available is %FALSE, then no remote hosts are * reachable. * * Params: * available = the current value of #GNetworkMonitor:network-available * * Since: 2.32 */ gulong addOnNetworkChanged(void delegate(bool, NetworkMonitorIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) { auto wrapper = new OnNetworkChangedDelegateWrapper(dlg); wrapper.handlerId = Signals.connectData( this, "network-changed", cast(GCallback)&callBackNetworkChanged, cast(void*)wrapper, cast(GClosureNotify)&callBackNetworkChangedDestroy, connectFlags); return wrapper.handlerId; } extern(C) static void callBackNetworkChanged(GNetworkMonitor* networkmonitorStruct, bool available, OnNetworkChangedDelegateWrapper wrapper) { wrapper.dlg(available, wrapper.outer); } extern(C) static void callBackNetworkChangedDestroy(OnNetworkChangedDelegateWrapper wrapper, GClosure* closure) { wrapper.remove(wrapper); } }