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 public  import gtkc.gdktypes;
39 private import gtkc.gio;
40 public  import gtkc.giotypes;
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 
96 	public static GType getType()
97 	{
98 		return g_dbus_server_get_type();
99 	}
100 
101 	/**
102 	 * Creates a new D-Bus server that listens on the first address in
103 	 * @address that works.
104 	 *
105 	 * Once constructed, you can use g_dbus_server_get_client_address() to
106 	 * get a D-Bus address string that clients can use to connect.
107 	 *
108 	 * Connect to the #GDBusServer::new-connection signal to handle
109 	 * incoming connections.
110 	 *
111 	 * The returned #GDBusServer isn't active - you have to start it with
112 	 * g_dbus_server_start().
113 	 *
114 	 * #GDBusServer is used in this [example][gdbus-peer-to-peer].
115 	 *
116 	 * This is a synchronous failable constructor. See
117 	 * g_dbus_server_new() for the asynchronous version.
118 	 *
119 	 * Params:
120 	 *     address = A D-Bus address.
121 	 *     flags = Flags from the #GDBusServerFlags enumeration.
122 	 *     guid = A D-Bus GUID.
123 	 *     observer = A #GDBusAuthObserver or %NULL.
124 	 *     cancellable = A #GCancellable or %NULL.
125 	 *
126 	 * Return: A #GDBusServer or %NULL if @error is set. Free with
127 	 *     g_object_unref().
128 	 *
129 	 * Since: 2.26
130 	 *
131 	 * Throws: GException on failure.
132 	 * Throws: ConstructionException GTK+ fails to create the object.
133 	 */
134 	public this(string address, GDBusServerFlags flags, string guid, DBusAuthObserver observer, Cancellable cancellable)
135 	{
136 		GError* err = null;
137 		
138 		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);
139 		
140 		if(p is null)
141 		{
142 			throw new ConstructionException("null returned by new_sync");
143 		}
144 		
145 		if (err !is null)
146 		{
147 			throw new GException( new ErrorG(err) );
148 		}
149 		
150 		this(cast(GDBusServer*) p, true);
151 	}
152 
153 	/**
154 	 * Gets a D-Bus address string that can be used by clients to connect
155 	 * to @server.
156 	 *
157 	 * Return: 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 	 * Return: 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 	 * Return: 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 	 * Return: %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 	int[string] connectedSignals;
224 
225 	bool delegate(DBusConnection, DBusServer)[] onNewConnectionListeners;
226 	/**
227 	 * Emitted when a new authenticated connection has been made. Use
228 	 * g_dbus_connection_get_peer_credentials() to figure out what
229 	 * identity (if any), was authenticated.
230 	 *
231 	 * If you want to accept the connection, take a reference to the
232 	 * @connection object and return %TRUE. When you are done with the
233 	 * connection call g_dbus_connection_close() and give up your
234 	 * reference. Note that the other peer may disconnect at any time -
235 	 * a typical thing to do when accepting a connection is to listen to
236 	 * the #GDBusConnection::closed signal.
237 	 *
238 	 * If #GDBusServer:flags contains %G_DBUS_SERVER_FLAGS_RUN_IN_THREAD
239 	 * then the signal is emitted in a new thread dedicated to the
240 	 * connection. Otherwise the signal is emitted in the
241 	 * [thread-default main context][g-main-context-push-thread-default]
242 	 * of the thread that @server was constructed in.
243 	 *
244 	 * You are guaranteed that signal handlers for this signal runs
245 	 * before incoming messages on @connection are processed. This means
246 	 * that it's suitable to call g_dbus_connection_register_object() or
247 	 * similar from the signal handler.
248 	 *
249 	 * Params:
250 	 *     connection = A #GDBusConnection for the new connection.
251 	 *
252 	 * Return: %TRUE to claim @connection, %FALSE to let other handlers
253 	 *     run.
254 	 *
255 	 * Since: 2.26
256 	 */
257 	void addOnNewConnection(bool delegate(DBusConnection, DBusServer) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
258 	{
259 		if ( "new-connection" !in connectedSignals )
260 		{
261 			Signals.connectData(
262 				this,
263 				"new-connection",
264 				cast(GCallback)&callBackNewConnection,
265 				cast(void*)this,
266 				null,
267 				connectFlags);
268 			connectedSignals["new-connection"] = 1;
269 		}
270 		onNewConnectionListeners ~= dlg;
271 	}
272 	extern(C) static int callBackNewConnection(GDBusServer* dbusserverStruct, GDBusConnection* connection, DBusServer _dbusserver)
273 	{
274 		foreach ( bool delegate(DBusConnection, DBusServer) dlg; _dbusserver.onNewConnectionListeners )
275 		{
276 			if ( dlg(ObjectG.getDObject!(DBusConnection)(connection), _dbusserver) )
277 			{
278 				return 1;
279 			}
280 		}
281 		
282 		return 0;
283 	}
284 }