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