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