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