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