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.DBusServer;
26 
27 private import gio.Cancellable;
28 private import gio.DBusAuthObserver;
29 private import gio.DBusConnection;
30 private import gio.InitableIF;
31 private import gio.InitableT;
32 private import gio.c.functions;
33 public  import gio.c.types;
34 private import glib.ConstructionException;
35 private import glib.ErrorG;
36 private import glib.GException;
37 private import glib.Str;
38 private import gobject.ObjectG;
39 private import gobject.Signals;
40 public  import gtkc.giotypes;
41 private import std.algorithm;
42 
43 
44 /**
45  * #GDBusServer is a helper for listening to and accepting D-Bus
46  * connections. This can be used to create a new D-Bus server, allowing two
47  * peers to use the D-Bus protocol for their own specialized communication.
48  * A server instance provided in this way will not perform message routing or
49  * implement the org.freedesktop.DBus interface.
50  * 
51  * To just export an object on a well-known name on a message bus, such as the
52  * session or system bus, you should instead use g_bus_own_name().
53  * 
54  * An example of peer-to-peer communication with G-DBus can be found
55  * in [gdbus-example-peer.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-peer.c).
56  *
57  * Since: 2.26
58  */
59 public class DBusServer : ObjectG, InitableIF
60 {
61 	/** the main Gtk struct */
62 	protected GDBusServer* gDBusServer;
63 
64 	/** Get the main Gtk struct */
65 	public GDBusServer* getDBusServerStruct(bool transferOwnership = false)
66 	{
67 		if (transferOwnership)
68 			ownedRef = false;
69 		return gDBusServer;
70 	}
71 
72 	/** the main Gtk struct as a void* */
73 	protected override void* getStruct()
74 	{
75 		return cast(void*)gDBusServer;
76 	}
77 
78 	protected override void setStruct(GObject* obj)
79 	{
80 		gDBusServer = cast(GDBusServer*)obj;
81 		super.setStruct(obj);
82 	}
83 
84 	/**
85 	 * Sets our main struct and passes it to the parent class.
86 	 */
87 	public this (GDBusServer* gDBusServer, bool ownedRef = false)
88 	{
89 		this.gDBusServer = gDBusServer;
90 		super(cast(GObject*)gDBusServer, ownedRef);
91 	}
92 
93 	// add the Initable capabilities
94 	mixin InitableT!(GDBusServer);
95 
96 
97 	/** */
98 	public static GType getType()
99 	{
100 		return g_dbus_server_get_type();
101 	}
102 
103 	/**
104 	 * Creates a new D-Bus server that listens on the first address in
105 	 * @address that works.
106 	 *
107 	 * Once constructed, you can use g_dbus_server_get_client_address() to
108 	 * get a D-Bus address string that clients can use to connect.
109 	 *
110 	 * Connect to the #GDBusServer::new-connection signal to handle
111 	 * incoming connections.
112 	 *
113 	 * The returned #GDBusServer isn't active - you have to start it with
114 	 * g_dbus_server_start().
115 	 *
116 	 * #GDBusServer is used in this [example][gdbus-peer-to-peer].
117 	 *
118 	 * This is a synchronous failable constructor. See
119 	 * g_dbus_server_new() for the asynchronous version.
120 	 *
121 	 * Params:
122 	 *     address = A D-Bus address.
123 	 *     flags = Flags from the #GDBusServerFlags enumeration.
124 	 *     guid = A D-Bus GUID.
125 	 *     observer = A #GDBusAuthObserver or %NULL.
126 	 *     cancellable = A #GCancellable or %NULL.
127 	 *
128 	 * Returns: A #GDBusServer or %NULL if @error is set. Free with
129 	 *     g_object_unref().
130 	 *
131 	 * Since: 2.26
132 	 *
133 	 * Throws: GException on failure.
134 	 * Throws: ConstructionException GTK+ fails to create the object.
135 	 */
136 	public this(string address, GDBusServerFlags flags, string guid, DBusAuthObserver observer, Cancellable cancellable)
137 	{
138 		GError* err = null;
139 
140 		auto p = g_dbus_server_new_sync(Str.toStringz(address), flags, Str.toStringz(guid), (observer is null) ? null : observer.getDBusAuthObserverStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
141 
142 		if (err !is null)
143 		{
144 			throw new GException( new ErrorG(err) );
145 		}
146 
147 		if(p is null)
148 		{
149 			throw new ConstructionException("null returned by new_sync");
150 		}
151 
152 		this(cast(GDBusServer*) p, true);
153 	}
154 
155 	/**
156 	 * Gets a
157 	 * [D-Bus address](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses)
158 	 * string that can be used by clients to connect to @server.
159 	 *
160 	 * Returns: A D-Bus address string. Do not free, the string is owned
161 	 *     by @server.
162 	 *
163 	 * Since: 2.26
164 	 */
165 	public string getClientAddress()
166 	{
167 		return Str.toString(g_dbus_server_get_client_address(gDBusServer));
168 	}
169 
170 	/**
171 	 * Gets the flags for @server.
172 	 *
173 	 * Returns: A set of flags from the #GDBusServerFlags enumeration.
174 	 *
175 	 * Since: 2.26
176 	 */
177 	public GDBusServerFlags getFlags()
178 	{
179 		return g_dbus_server_get_flags(gDBusServer);
180 	}
181 
182 	/**
183 	 * Gets the GUID for @server.
184 	 *
185 	 * Returns: A D-Bus GUID. Do not free this string, it is owned by @server.
186 	 *
187 	 * Since: 2.26
188 	 */
189 	public string getGuid()
190 	{
191 		return Str.toString(g_dbus_server_get_guid(gDBusServer));
192 	}
193 
194 	/**
195 	 * Gets whether @server is active.
196 	 *
197 	 * Returns: %TRUE if server is active, %FALSE otherwise.
198 	 *
199 	 * Since: 2.26
200 	 */
201 	public bool isActive()
202 	{
203 		return g_dbus_server_is_active(gDBusServer) != 0;
204 	}
205 
206 	/**
207 	 * Starts @server.
208 	 *
209 	 * Since: 2.26
210 	 */
211 	public void start()
212 	{
213 		g_dbus_server_start(gDBusServer);
214 	}
215 
216 	/**
217 	 * Stops @server.
218 	 *
219 	 * Since: 2.26
220 	 */
221 	public void stop()
222 	{
223 		g_dbus_server_stop(gDBusServer);
224 	}
225 
226 	protected class OnNewConnectionDelegateWrapper
227 	{
228 		bool delegate(DBusConnection, DBusServer) dlg;
229 		gulong handlerId;
230 
231 		this(bool delegate(DBusConnection, DBusServer) dlg)
232 		{
233 			this.dlg = dlg;
234 			onNewConnectionListeners ~= this;
235 		}
236 
237 		void remove(OnNewConnectionDelegateWrapper source)
238 		{
239 			foreach(index, wrapper; onNewConnectionListeners)
240 			{
241 				if (wrapper.handlerId == source.handlerId)
242 				{
243 					onNewConnectionListeners[index] = null;
244 					onNewConnectionListeners = std.algorithm.remove(onNewConnectionListeners, index);
245 					break;
246 				}
247 			}
248 		}
249 	}
250 	OnNewConnectionDelegateWrapper[] onNewConnectionListeners;
251 
252 	/**
253 	 * Emitted when a new authenticated connection has been made. Use
254 	 * g_dbus_connection_get_peer_credentials() to figure out what
255 	 * identity (if any), was authenticated.
256 	 *
257 	 * If you want to accept the connection, take a reference to the
258 	 * @connection object and return %TRUE. When you are done with the
259 	 * connection call g_dbus_connection_close() and give up your
260 	 * reference. Note that the other peer may disconnect at any time -
261 	 * a typical thing to do when accepting a connection is to listen to
262 	 * the #GDBusConnection::closed signal.
263 	 *
264 	 * If #GDBusServer:flags contains %G_DBUS_SERVER_FLAGS_RUN_IN_THREAD
265 	 * then the signal is emitted in a new thread dedicated to the
266 	 * connection. Otherwise the signal is emitted in the
267 	 * [thread-default main context][g-main-context-push-thread-default]
268 	 * of the thread that @server was constructed in.
269 	 *
270 	 * You are guaranteed that signal handlers for this signal runs
271 	 * before incoming messages on @connection are processed. This means
272 	 * that it's suitable to call g_dbus_connection_register_object() or
273 	 * similar from the signal handler.
274 	 *
275 	 * Params:
276 	 *     connection = A #GDBusConnection for the new connection.
277 	 *
278 	 * Returns: %TRUE to claim @connection, %FALSE to let other handlers
279 	 *     run.
280 	 *
281 	 * Since: 2.26
282 	 */
283 	gulong addOnNewConnection(bool delegate(DBusConnection, DBusServer) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
284 	{
285 		auto wrapper = new OnNewConnectionDelegateWrapper(dlg);
286 		wrapper.handlerId = Signals.connectData(
287 			this,
288 			"new-connection",
289 			cast(GCallback)&callBackNewConnection,
290 			cast(void*)wrapper,
291 			cast(GClosureNotify)&callBackNewConnectionDestroy,
292 			connectFlags);
293 		return wrapper.handlerId;
294 	}
295 
296 	extern(C) static int callBackNewConnection(GDBusServer* dbusserverStruct, GDBusConnection* connection, OnNewConnectionDelegateWrapper wrapper)
297 	{
298 		return wrapper.dlg(ObjectG.getDObject!(DBusConnection)(connection), wrapper.outer);
299 	}
300 
301 	extern(C) static void callBackNewConnectionDestroy(OnNewConnectionDelegateWrapper wrapper, GClosure* closure)
302 	{
303 		wrapper.remove(wrapper);
304 	}
305 }