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 
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 
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 	int[string] connectedSignals;
169 
170 	bool delegate(string, DBusAuthObserver)[] onAllowMechanismListeners;
171 	/**
172 	 * Emitted to check if @mechanism is allowed to be used.
173 	 *
174 	 * Params:
175 	 *     mechanism = The name of the mechanism, e.g. `DBUS_COOKIE_SHA1`.
176 	 *
177 	 * Return: %TRUE if @mechanism can be used to authenticate the other peer, %FALSE if not.
178 	 *
179 	 * Since: 2.34
180 	 */
181 	void addOnAllowMechanism(bool delegate(string, DBusAuthObserver) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
182 	{
183 		if ( "allow-mechanism" !in connectedSignals )
184 		{
185 			Signals.connectData(
186 				this,
187 				"allow-mechanism",
188 				cast(GCallback)&callBackAllowMechanism,
189 				cast(void*)this,
190 				null,
191 				connectFlags);
192 			connectedSignals["allow-mechanism"] = 1;
193 		}
194 		onAllowMechanismListeners ~= dlg;
195 	}
196 	extern(C) static int callBackAllowMechanism(GDBusAuthObserver* dbusauthobserverStruct, char* mechanism, DBusAuthObserver _dbusauthobserver)
197 	{
198 		foreach ( bool delegate(string, DBusAuthObserver) dlg; _dbusauthobserver.onAllowMechanismListeners )
199 		{
200 			if ( dlg(Str.toString(mechanism), _dbusauthobserver) )
201 			{
202 				return 1;
203 			}
204 		}
205 		
206 		return 0;
207 	}
208 
209 	bool delegate(IOStream, Credentials, DBusAuthObserver)[] onAuthorizeAuthenticatedPeerListeners;
210 	/**
211 	 * Emitted to check if a peer that is successfully authenticated
212 	 * is authorized.
213 	 *
214 	 * Params:
215 	 *     stream = A #GIOStream for the #GDBusConnection.
216 	 *     credentials = Credentials received from the peer or %NULL.
217 	 *
218 	 * Return: %TRUE if the peer is authorized, %FALSE if not.
219 	 *
220 	 * Since: 2.26
221 	 */
222 	void addOnAuthorizeAuthenticatedPeer(bool delegate(IOStream, Credentials, DBusAuthObserver) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
223 	{
224 		if ( "authorize-authenticated-peer" !in connectedSignals )
225 		{
226 			Signals.connectData(
227 				this,
228 				"authorize-authenticated-peer",
229 				cast(GCallback)&callBackAuthorizeAuthenticatedPeer,
230 				cast(void*)this,
231 				null,
232 				connectFlags);
233 			connectedSignals["authorize-authenticated-peer"] = 1;
234 		}
235 		onAuthorizeAuthenticatedPeerListeners ~= dlg;
236 	}
237 	extern(C) static int callBackAuthorizeAuthenticatedPeer(GDBusAuthObserver* dbusauthobserverStruct, GIOStream* stream, GCredentials* credentials, DBusAuthObserver _dbusauthobserver)
238 	{
239 		foreach ( bool delegate(IOStream, Credentials, DBusAuthObserver) dlg; _dbusauthobserver.onAuthorizeAuthenticatedPeerListeners )
240 		{
241 			if ( dlg(ObjectG.getDObject!(IOStream)(stream), ObjectG.getDObject!(Credentials)(credentials), _dbusauthobserver) )
242 			{
243 				return 1;
244 			}
245 		}
246 		
247 		return 0;
248 	}
249 }