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