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