1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD 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 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module gio.NetworkMonitorT; 26 27 public import gio.AsyncResultIF; 28 public import gio.Cancellable; 29 public import gio.NetworkMonitorIF; 30 public import gio.SocketConnectableIF; 31 public import gio.c.functions; 32 public import gio.c.types; 33 public import glib.ErrorG; 34 public import glib.GException; 35 public import gobject.ObjectG; 36 public import gobject.Signals; 37 public import gtkc.giotypes; 38 public import std.algorithm; 39 40 41 /** 42 * #GNetworkMonitor provides an easy-to-use cross-platform API 43 * for monitoring network connectivity. On Linux, the available 44 * implementations are based on the kernel's netlink interface and 45 * on NetworkManager. 46 * 47 * There is also an implementation for use inside Flatpak sandboxes. 48 * 49 * Since: 2.32 50 */ 51 public template NetworkMonitorT(TStruct) 52 { 53 /** Get the main Gtk struct */ 54 public GNetworkMonitor* getNetworkMonitorStruct(bool transferOwnership = false) 55 { 56 if (transferOwnership) 57 ownedRef = false; 58 return cast(GNetworkMonitor*)getStruct(); 59 } 60 61 62 /** 63 * Gets the default #GNetworkMonitor for the system. 64 * 65 * Returns: a #GNetworkMonitor 66 * 67 * Since: 2.32 68 */ 69 public static NetworkMonitorIF getDefault() 70 { 71 auto p = g_network_monitor_get_default(); 72 73 if(p is null) 74 { 75 return null; 76 } 77 78 return ObjectG.getDObject!(NetworkMonitorIF)(cast(GNetworkMonitor*) p); 79 } 80 81 /** 82 * Attempts to determine whether or not the host pointed to by 83 * @connectable can be reached, without actually trying to connect to 84 * it. 85 * 86 * This may return %TRUE even when #GNetworkMonitor:network-available 87 * is %FALSE, if, for example, @monitor can determine that 88 * @connectable refers to a host on a local network. 89 * 90 * If @monitor believes that an attempt to connect to @connectable 91 * will succeed, it will return %TRUE. Otherwise, it will return 92 * %FALSE and set @error to an appropriate error (such as 93 * %G_IO_ERROR_HOST_UNREACHABLE). 94 * 95 * Note that although this does not attempt to connect to 96 * @connectable, it may still block for a brief period of time (eg, 97 * trying to do multicast DNS on the local network), so if you do not 98 * want to block, you should use g_network_monitor_can_reach_async(). 99 * 100 * Params: 101 * connectable = a #GSocketConnectable 102 * cancellable = a #GCancellable, or %NULL 103 * 104 * Returns: %TRUE if @connectable is reachable, %FALSE if not. 105 * 106 * Since: 2.32 107 * 108 * Throws: GException on failure. 109 */ 110 public bool canReach(SocketConnectableIF connectable, Cancellable cancellable) 111 { 112 GError* err = null; 113 114 auto p = g_network_monitor_can_reach(getNetworkMonitorStruct(), (connectable is null) ? null : connectable.getSocketConnectableStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0; 115 116 if (err !is null) 117 { 118 throw new GException( new ErrorG(err) ); 119 } 120 121 return p; 122 } 123 124 /** 125 * Asynchronously attempts to determine whether or not the host 126 * pointed to by @connectable can be reached, without actually 127 * trying to connect to it. 128 * 129 * For more details, see g_network_monitor_can_reach(). 130 * 131 * When the operation is finished, @callback will be called. 132 * You can then call g_network_monitor_can_reach_finish() 133 * to get the result of the operation. 134 * 135 * Params: 136 * connectable = a #GSocketConnectable 137 * cancellable = a #GCancellable, or %NULL 138 * callback = a #GAsyncReadyCallback to call when the 139 * request is satisfied 140 * userData = the data to pass to callback function 141 */ 142 public void canReachAsync(SocketConnectableIF connectable, Cancellable cancellable, GAsyncReadyCallback callback, void* userData) 143 { 144 g_network_monitor_can_reach_async(getNetworkMonitorStruct(), (connectable is null) ? null : connectable.getSocketConnectableStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData); 145 } 146 147 /** 148 * Finishes an async network connectivity test. 149 * See g_network_monitor_can_reach_async(). 150 * 151 * Params: 152 * result = a #GAsyncResult 153 * 154 * Returns: %TRUE if network is reachable, %FALSE if not. 155 * 156 * Throws: GException on failure. 157 */ 158 public bool canReachFinish(AsyncResultIF result) 159 { 160 GError* err = null; 161 162 auto p = g_network_monitor_can_reach_finish(getNetworkMonitorStruct(), (result is null) ? null : result.getAsyncResultStruct(), &err) != 0; 163 164 if (err !is null) 165 { 166 throw new GException( new ErrorG(err) ); 167 } 168 169 return p; 170 } 171 172 /** 173 * Gets a more detailed networking state than 174 * g_network_monitor_get_network_available(). 175 * 176 * If #GNetworkMonitor:network-available is %FALSE, then the 177 * connectivity state will be %G_NETWORK_CONNECTIVITY_LOCAL. 178 * 179 * If #GNetworkMonitor:network-available is %TRUE, then the 180 * connectivity state will be %G_NETWORK_CONNECTIVITY_FULL (if there 181 * is full Internet connectivity), %G_NETWORK_CONNECTIVITY_LIMITED (if 182 * the host has a default route, but appears to be unable to actually 183 * reach the full Internet), or %G_NETWORK_CONNECTIVITY_PORTAL (if the 184 * host is trapped behind a "captive portal" that requires some sort 185 * of login or acknowledgement before allowing full Internet access). 186 * 187 * Note that in the case of %G_NETWORK_CONNECTIVITY_LIMITED and 188 * %G_NETWORK_CONNECTIVITY_PORTAL, it is possible that some sites are 189 * reachable but others are not. In this case, applications can 190 * attempt to connect to remote servers, but should gracefully fall 191 * back to their "offline" behavior if the connection attempt fails. 192 * 193 * Returns: the network connectivity state 194 * 195 * Since: 2.44 196 */ 197 public GNetworkConnectivity getConnectivity() 198 { 199 return g_network_monitor_get_connectivity(getNetworkMonitorStruct()); 200 } 201 202 /** 203 * Checks if the network is available. "Available" here means that the 204 * system has a default route available for at least one of IPv4 or 205 * IPv6. It does not necessarily imply that the public Internet is 206 * reachable. See #GNetworkMonitor:network-available for more details. 207 * 208 * Returns: whether the network is available 209 * 210 * Since: 2.32 211 */ 212 public bool getNetworkAvailable() 213 { 214 return g_network_monitor_get_network_available(getNetworkMonitorStruct()) != 0; 215 } 216 217 /** 218 * Checks if the network is metered. 219 * See #GNetworkMonitor:network-metered for more details. 220 * 221 * Returns: whether the connection is metered 222 * 223 * Since: 2.46 224 */ 225 public bool getNetworkMetered() 226 { 227 return g_network_monitor_get_network_metered(getNetworkMonitorStruct()) != 0; 228 } 229 230 protected class OnNetworkChangedDelegateWrapper 231 { 232 void delegate(bool, NetworkMonitorIF) dlg; 233 gulong handlerId; 234 235 this(void delegate(bool, NetworkMonitorIF) dlg) 236 { 237 this.dlg = dlg; 238 onNetworkChangedListeners ~= this; 239 } 240 241 void remove(OnNetworkChangedDelegateWrapper source) 242 { 243 foreach(index, wrapper; onNetworkChangedListeners) 244 { 245 if (wrapper.handlerId == source.handlerId) 246 { 247 onNetworkChangedListeners[index] = null; 248 onNetworkChangedListeners = std.algorithm.remove(onNetworkChangedListeners, index); 249 break; 250 } 251 } 252 } 253 } 254 OnNetworkChangedDelegateWrapper[] onNetworkChangedListeners; 255 256 /** 257 * Emitted when the network configuration changes. If @available is 258 * %TRUE, then some hosts may be reachable that were not reachable 259 * before, while others that were reachable before may no longer be 260 * reachable. If @available is %FALSE, then no remote hosts are 261 * reachable. 262 * 263 * Params: 264 * available = the current value of #GNetworkMonitor:network-available 265 * 266 * Since: 2.32 267 */ 268 gulong addOnNetworkChanged(void delegate(bool, NetworkMonitorIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 269 { 270 auto wrapper = new OnNetworkChangedDelegateWrapper(dlg); 271 wrapper.handlerId = Signals.connectData( 272 this, 273 "network-changed", 274 cast(GCallback)&callBackNetworkChanged, 275 cast(void*)wrapper, 276 cast(GClosureNotify)&callBackNetworkChangedDestroy, 277 connectFlags); 278 return wrapper.handlerId; 279 } 280 281 extern(C) static void callBackNetworkChanged(GNetworkMonitor* networkmonitorStruct, bool available, OnNetworkChangedDelegateWrapper wrapper) 282 { 283 wrapper.dlg(available, wrapper.outer); 284 } 285 286 extern(C) static void callBackNetworkChangedDestroy(OnNetworkChangedDelegateWrapper wrapper, GClosure* closure) 287 { 288 wrapper.remove(wrapper); 289 } 290 }