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 gio.c.functions;
30 public  import gio.c.types;
31 private import glib.ConstructionException;
32 private import glib.Str;
33 private import gobject.ObjectG;
34 private import gobject.Signals;
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(bool transferOwnership = false)
84 	{
85 		if (transferOwnership)
86 			ownedRef = false;
87 		return gDBusAuthObserver;
88 	}
89 
90 	/** the main Gtk struct as a void* */
91 	protected override void* getStruct()
92 	{
93 		return cast(void*)gDBusAuthObserver;
94 	}
95 
96 	/**
97 	 * Sets our main struct and passes it to the parent class.
98 	 */
99 	public this (GDBusAuthObserver* gDBusAuthObserver, bool ownedRef = false)
100 	{
101 		this.gDBusAuthObserver = gDBusAuthObserver;
102 		super(cast(GObject*)gDBusAuthObserver, ownedRef);
103 	}
104 
105 
106 	/** */
107 	public static GType getType()
108 	{
109 		return g_dbus_auth_observer_get_type();
110 	}
111 
112 	/**
113 	 * Creates a new #GDBusAuthObserver object.
114 	 *
115 	 * Returns: A #GDBusAuthObserver. Free with g_object_unref().
116 	 *
117 	 * Since: 2.26
118 	 *
119 	 * Throws: ConstructionException GTK+ fails to create the object.
120 	 */
121 	public this()
122 	{
123 		auto p = g_dbus_auth_observer_new();
124 
125 		if(p is null)
126 		{
127 			throw new ConstructionException("null returned by new");
128 		}
129 
130 		this(cast(GDBusAuthObserver*) p, true);
131 	}
132 
133 	/**
134 	 * Emits the #GDBusAuthObserver::allow-mechanism signal on @observer.
135 	 *
136 	 * Params:
137 	 *     mechanism = The name of the mechanism, e.g. `DBUS_COOKIE_SHA1`.
138 	 *
139 	 * Returns: %TRUE if @mechanism can be used to authenticate the other peer, %FALSE if not.
140 	 *
141 	 * Since: 2.34
142 	 */
143 	public bool allowMechanism(string mechanism)
144 	{
145 		return g_dbus_auth_observer_allow_mechanism(gDBusAuthObserver, Str.toStringz(mechanism)) != 0;
146 	}
147 
148 	/**
149 	 * Emits the #GDBusAuthObserver::authorize-authenticated-peer signal on @observer.
150 	 *
151 	 * Params:
152 	 *     stream = A #GIOStream for the #GDBusConnection.
153 	 *     credentials = Credentials received from the peer or %NULL.
154 	 *
155 	 * Returns: %TRUE if the peer is authorized, %FALSE if not.
156 	 *
157 	 * Since: 2.26
158 	 */
159 	public bool authorizeAuthenticatedPeer(IOStream stream, Credentials credentials)
160 	{
161 		return g_dbus_auth_observer_authorize_authenticated_peer(gDBusAuthObserver, (stream is null) ? null : stream.getIOStreamStruct(), (credentials is null) ? null : credentials.getCredentialsStruct()) != 0;
162 	}
163 
164 	protected class OnAllowMechanismDelegateWrapper
165 	{
166 		bool delegate(string, DBusAuthObserver) dlg;
167 		gulong handlerId;
168 
169 		this(bool delegate(string, DBusAuthObserver) dlg)
170 		{
171 			this.dlg = dlg;
172 			onAllowMechanismListeners ~= this;
173 		}
174 
175 		void remove(OnAllowMechanismDelegateWrapper source)
176 		{
177 			foreach(index, wrapper; onAllowMechanismListeners)
178 			{
179 				if (wrapper.handlerId == source.handlerId)
180 				{
181 					onAllowMechanismListeners[index] = null;
182 					onAllowMechanismListeners = std.algorithm.remove(onAllowMechanismListeners, index);
183 					break;
184 				}
185 			}
186 		}
187 	}
188 	OnAllowMechanismDelegateWrapper[] onAllowMechanismListeners;
189 
190 	/**
191 	 * Emitted to check if @mechanism is allowed to be used.
192 	 *
193 	 * Params:
194 	 *     mechanism = The name of the mechanism, e.g. `DBUS_COOKIE_SHA1`.
195 	 *
196 	 * Returns: %TRUE if @mechanism can be used to authenticate the other peer, %FALSE if not.
197 	 *
198 	 * Since: 2.34
199 	 */
200 	gulong addOnAllowMechanism(bool delegate(string, DBusAuthObserver) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
201 	{
202 		auto wrapper = new OnAllowMechanismDelegateWrapper(dlg);
203 		wrapper.handlerId = Signals.connectData(
204 			this,
205 			"allow-mechanism",
206 			cast(GCallback)&callBackAllowMechanism,
207 			cast(void*)wrapper,
208 			cast(GClosureNotify)&callBackAllowMechanismDestroy,
209 			connectFlags);
210 		return wrapper.handlerId;
211 	}
212 
213 	extern(C) static int callBackAllowMechanism(GDBusAuthObserver* dbusauthobserverStruct, char* mechanism, OnAllowMechanismDelegateWrapper wrapper)
214 	{
215 		return wrapper.dlg(Str.toString(mechanism), wrapper.outer);
216 	}
217 
218 	extern(C) static void callBackAllowMechanismDestroy(OnAllowMechanismDelegateWrapper wrapper, GClosure* closure)
219 	{
220 		wrapper.remove(wrapper);
221 	}
222 
223 	protected class OnAuthorizeAuthenticatedPeerDelegateWrapper
224 	{
225 		bool delegate(IOStream, Credentials, DBusAuthObserver) dlg;
226 		gulong handlerId;
227 
228 		this(bool delegate(IOStream, Credentials, DBusAuthObserver) dlg)
229 		{
230 			this.dlg = dlg;
231 			onAuthorizeAuthenticatedPeerListeners ~= this;
232 		}
233 
234 		void remove(OnAuthorizeAuthenticatedPeerDelegateWrapper source)
235 		{
236 			foreach(index, wrapper; onAuthorizeAuthenticatedPeerListeners)
237 			{
238 				if (wrapper.handlerId == source.handlerId)
239 				{
240 					onAuthorizeAuthenticatedPeerListeners[index] = null;
241 					onAuthorizeAuthenticatedPeerListeners = std.algorithm.remove(onAuthorizeAuthenticatedPeerListeners, index);
242 					break;
243 				}
244 			}
245 		}
246 	}
247 	OnAuthorizeAuthenticatedPeerDelegateWrapper[] onAuthorizeAuthenticatedPeerListeners;
248 
249 	/**
250 	 * Emitted to check if a peer that is successfully authenticated
251 	 * is authorized.
252 	 *
253 	 * Params:
254 	 *     stream = A #GIOStream for the #GDBusConnection.
255 	 *     credentials = Credentials received from the peer or %NULL.
256 	 *
257 	 * Returns: %TRUE if the peer is authorized, %FALSE if not.
258 	 *
259 	 * Since: 2.26
260 	 */
261 	gulong addOnAuthorizeAuthenticatedPeer(bool delegate(IOStream, Credentials, DBusAuthObserver) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
262 	{
263 		auto wrapper = new OnAuthorizeAuthenticatedPeerDelegateWrapper(dlg);
264 		wrapper.handlerId = Signals.connectData(
265 			this,
266 			"authorize-authenticated-peer",
267 			cast(GCallback)&callBackAuthorizeAuthenticatedPeer,
268 			cast(void*)wrapper,
269 			cast(GClosureNotify)&callBackAuthorizeAuthenticatedPeerDestroy,
270 			connectFlags);
271 		return wrapper.handlerId;
272 	}
273 
274 	extern(C) static int callBackAuthorizeAuthenticatedPeer(GDBusAuthObserver* dbusauthobserverStruct, GIOStream* stream, GCredentials* credentials, OnAuthorizeAuthenticatedPeerDelegateWrapper wrapper)
275 	{
276 		return wrapper.dlg(ObjectG.getDObject!(IOStream)(stream), ObjectG.getDObject!(Credentials)(credentials), wrapper.outer);
277 	}
278 
279 	extern(C) static void callBackAuthorizeAuthenticatedPeerDestroy(OnAuthorizeAuthenticatedPeerDelegateWrapper wrapper, GClosure* closure)
280 	{
281 		wrapper.remove(wrapper);
282 	}
283 }