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