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