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