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