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.DBusAuthObserver;
26 
27 private import gio.Credentials;
28 private import gio.IOStream;
29 private import glib.ConstructionException;
30 private import glib.Str;
31 private import gobject.ObjectG;
32 private import gobject.Signals;
33 public  import gtkc.gdktypes;
34 private import gtkc.gio;
35 public  import gtkc.giotypes;
36 private import std.algorithm;
37 
38 
39 /**
40  * The #GDBusAuthObserver type provides a mechanism for participating
41  * in how a #GDBusServer (or a #GDBusConnection) authenticates remote
42  * peers. Simply instantiate a #GDBusAuthObserver and connect to the
43  * signals you are interested in. Note that new signals may be added
44  * in the future
45  * 
46  * ## Controlling Authentication # {#auth-observer}
47  * 
48  * For example, if you only want to allow D-Bus connections from
49  * processes owned by the same uid as the server, you would use a
50  * signal handler like the following:
51  * 
52  * |[<!-- language="C" -->
53  * static gboolean
54  * on_authorize_authenticated_peer (GDBusAuthObserver *observer,
55  * GIOStream         *stream,
56  * GCredentials      *credentials,
57  * gpointer           user_data)
58  * {
59  * gboolean authorized;
60  * 
61  * authorized = FALSE;
62  * if (credentials != NULL)
63  * {
64  * GCredentials *own_credentials;
65  * own_credentials = g_credentials_new ();
66  * if (g_credentials_is_same_user (credentials, own_credentials, NULL))
67  * authorized = TRUE;
68  * g_object_unref (own_credentials);
69  * }
70  * 
71  * return authorized;
72  * }
73  * ]|
74  *
75  * Since: 2.26
76  */
77 public class DBusAuthObserver : ObjectG
78 {
79 	/** the main Gtk struct */
80 	protected GDBusAuthObserver* gDBusAuthObserver;
81 
82 	/** Get the main Gtk struct */
83 	public GDBusAuthObserver* getDBusAuthObserverStruct()
84 	{
85 		return gDBusAuthObserver;
86 	}
87 
88 	/** the main Gtk struct as a void* */
89 	protected override void* getStruct()
90 	{
91 		return cast(void*)gDBusAuthObserver;
92 	}
93 
94 	protected override void setStruct(GObject* obj)
95 	{
96 		gDBusAuthObserver = cast(GDBusAuthObserver*)obj;
97 		super.setStruct(obj);
98 	}
99 
100 	/**
101 	 * Sets our main struct and passes it to the parent class.
102 	 */
103 	public this (GDBusAuthObserver* gDBusAuthObserver, bool ownedRef = false)
104 	{
105 		this.gDBusAuthObserver = gDBusAuthObserver;
106 		super(cast(GObject*)gDBusAuthObserver, ownedRef);
107 	}
108 
109 
110 	/** */
111 	public static GType getType()
112 	{
113 		return g_dbus_auth_observer_get_type();
114 	}
115 
116 	/**
117 	 * Creates a new #GDBusAuthObserver object.
118 	 *
119 	 * Return: A #GDBusAuthObserver. Free with g_object_unref().
120 	 *
121 	 * Since: 2.26
122 	 *
123 	 * Throws: ConstructionException GTK+ fails to create the object.
124 	 */
125 	public this()
126 	{
127 		auto p = g_dbus_auth_observer_new();
128 		
129 		if(p is null)
130 		{
131 			throw new ConstructionException("null returned by new");
132 		}
133 		
134 		this(cast(GDBusAuthObserver*) p, true);
135 	}
136 
137 	/**
138 	 * Emits the #GDBusAuthObserver::allow-mechanism signal on @observer.
139 	 *
140 	 * Params:
141 	 *     mechanism = The name of the mechanism, e.g. `DBUS_COOKIE_SHA1`.
142 	 *
143 	 * Return: %TRUE if @mechanism can be used to authenticate the other peer, %FALSE if not.
144 	 *
145 	 * Since: 2.34
146 	 */
147 	public bool allowMechanism(string mechanism)
148 	{
149 		return g_dbus_auth_observer_allow_mechanism(gDBusAuthObserver, Str.toStringz(mechanism)) != 0;
150 	}
151 
152 	/**
153 	 * Emits the #GDBusAuthObserver::authorize-authenticated-peer signal on @observer.
154 	 *
155 	 * Params:
156 	 *     stream = A #GIOStream for the #GDBusConnection.
157 	 *     credentials = Credentials received from the peer or %NULL.
158 	 *
159 	 * Return: %TRUE if the peer is authorized, %FALSE if not.
160 	 *
161 	 * Since: 2.26
162 	 */
163 	public bool authorizeAuthenticatedPeer(IOStream stream, Credentials credentials)
164 	{
165 		return g_dbus_auth_observer_authorize_authenticated_peer(gDBusAuthObserver, (stream is null) ? null : stream.getIOStreamStruct(), (credentials is null) ? null : credentials.getCredentialsStruct()) != 0;
166 	}
167 
168 	protected class OnAllowMechanismDelegateWrapper
169 	{
170 		bool delegate(string, DBusAuthObserver) dlg;
171 		gulong handlerId;
172 		ConnectFlags flags;
173 		this(bool delegate(string, DBusAuthObserver) dlg, gulong handlerId, ConnectFlags flags)
174 		{
175 			this.dlg = dlg;
176 			this.handlerId = handlerId;
177 			this.flags = flags;
178 		}
179 	}
180 	protected OnAllowMechanismDelegateWrapper[] onAllowMechanismListeners;
181 
182 	/**
183 	 * Emitted to check if @mechanism is allowed to be used.
184 	 *
185 	 * Params:
186 	 *     mechanism = The name of the mechanism, e.g. `DBUS_COOKIE_SHA1`.
187 	 *
188 	 * Return: %TRUE if @mechanism can be used to authenticate the other peer, %FALSE if not.
189 	 *
190 	 * Since: 2.34
191 	 */
192 	gulong addOnAllowMechanism(bool delegate(string, DBusAuthObserver) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
193 	{
194 		onAllowMechanismListeners ~= new OnAllowMechanismDelegateWrapper(dlg, 0, connectFlags);
195 		onAllowMechanismListeners[onAllowMechanismListeners.length - 1].handlerId = Signals.connectData(
196 			this,
197 			"allow-mechanism",
198 			cast(GCallback)&callBackAllowMechanism,
199 			cast(void*)onAllowMechanismListeners[onAllowMechanismListeners.length - 1],
200 			cast(GClosureNotify)&callBackAllowMechanismDestroy,
201 			connectFlags);
202 		return onAllowMechanismListeners[onAllowMechanismListeners.length - 1].handlerId;
203 	}
204 	
205 	extern(C) static int callBackAllowMechanism(GDBusAuthObserver* dbusauthobserverStruct, char* mechanism,OnAllowMechanismDelegateWrapper wrapper)
206 	{
207 		return wrapper.dlg(Str.toString(mechanism), wrapper.outer);
208 	}
209 	
210 	extern(C) static void callBackAllowMechanismDestroy(OnAllowMechanismDelegateWrapper wrapper, GClosure* closure)
211 	{
212 		wrapper.outer.internalRemoveOnAllowMechanism(wrapper);
213 	}
214 
215 	protected void internalRemoveOnAllowMechanism(OnAllowMechanismDelegateWrapper source)
216 	{
217 		foreach(index, wrapper; onAllowMechanismListeners)
218 		{
219 			if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId)
220 			{
221 				onAllowMechanismListeners[index] = null;
222 				onAllowMechanismListeners = std.algorithm.remove(onAllowMechanismListeners, index);
223 				break;
224 			}
225 		}
226 	}
227 	
228 
229 	protected class OnAuthorizeAuthenticatedPeerDelegateWrapper
230 	{
231 		bool delegate(IOStream, Credentials, DBusAuthObserver) dlg;
232 		gulong handlerId;
233 		ConnectFlags flags;
234 		this(bool delegate(IOStream, Credentials, DBusAuthObserver) dlg, gulong handlerId, ConnectFlags flags)
235 		{
236 			this.dlg = dlg;
237 			this.handlerId = handlerId;
238 			this.flags = flags;
239 		}
240 	}
241 	protected OnAuthorizeAuthenticatedPeerDelegateWrapper[] onAuthorizeAuthenticatedPeerListeners;
242 
243 	/**
244 	 * Emitted to check if a peer that is successfully authenticated
245 	 * is authorized.
246 	 *
247 	 * Params:
248 	 *     stream = A #GIOStream for the #GDBusConnection.
249 	 *     credentials = Credentials received from the peer or %NULL.
250 	 *
251 	 * Return: %TRUE if the peer is authorized, %FALSE if not.
252 	 *
253 	 * Since: 2.26
254 	 */
255 	gulong addOnAuthorizeAuthenticatedPeer(bool delegate(IOStream, Credentials, DBusAuthObserver) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
256 	{
257 		onAuthorizeAuthenticatedPeerListeners ~= new OnAuthorizeAuthenticatedPeerDelegateWrapper(dlg, 0, connectFlags);
258 		onAuthorizeAuthenticatedPeerListeners[onAuthorizeAuthenticatedPeerListeners.length - 1].handlerId = Signals.connectData(
259 			this,
260 			"authorize-authenticated-peer",
261 			cast(GCallback)&callBackAuthorizeAuthenticatedPeer,
262 			cast(void*)onAuthorizeAuthenticatedPeerListeners[onAuthorizeAuthenticatedPeerListeners.length - 1],
263 			cast(GClosureNotify)&callBackAuthorizeAuthenticatedPeerDestroy,
264 			connectFlags);
265 		return onAuthorizeAuthenticatedPeerListeners[onAuthorizeAuthenticatedPeerListeners.length - 1].handlerId;
266 	}
267 	
268 	extern(C) static int callBackAuthorizeAuthenticatedPeer(GDBusAuthObserver* dbusauthobserverStruct, GIOStream* stream, GCredentials* credentials,OnAuthorizeAuthenticatedPeerDelegateWrapper wrapper)
269 	{
270 		return wrapper.dlg(ObjectG.getDObject!(IOStream)(stream), ObjectG.getDObject!(Credentials)(credentials), wrapper.outer);
271 	}
272 	
273 	extern(C) static void callBackAuthorizeAuthenticatedPeerDestroy(OnAuthorizeAuthenticatedPeerDelegateWrapper wrapper, GClosure* closure)
274 	{
275 		wrapper.outer.internalRemoveOnAuthorizeAuthenticatedPeer(wrapper);
276 	}
277 
278 	protected void internalRemoveOnAuthorizeAuthenticatedPeer(OnAuthorizeAuthenticatedPeerDelegateWrapper source)
279 	{
280 		foreach(index, wrapper; onAuthorizeAuthenticatedPeerListeners)
281 		{
282 			if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId)
283 			{
284 				onAuthorizeAuthenticatedPeerListeners[index] = null;
285 				onAuthorizeAuthenticatedPeerListeners = std.algorithm.remove(onAuthorizeAuthenticatedPeerListeners, index);
286 				break;
287 			}
288 		}
289 	}
290 	
291 }