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 	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 	 * Return: 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 D-Bus address string that can be used by clients to connect
154 	 * to @server.
155 	 *
156 	 * Return: A D-Bus address string. Do not free, the string is owned
157 	 *     by @server.
158 	 *
159 	 * Since: 2.26
160 	 */
161 	public string getClientAddress()
162 	{
163 		return Str.toString(g_dbus_server_get_client_address(gDBusServer));
164 	}
165 
166 	/**
167 	 * Gets the flags for @server.
168 	 *
169 	 * Return: A set of flags from the #GDBusServerFlags enumeration.
170 	 *
171 	 * Since: 2.26
172 	 */
173 	public GDBusServerFlags getFlags()
174 	{
175 		return g_dbus_server_get_flags(gDBusServer);
176 	}
177 
178 	/**
179 	 * Gets the GUID for @server.
180 	 *
181 	 * Return: A D-Bus GUID. Do not free this string, it is owned by @server.
182 	 *
183 	 * Since: 2.26
184 	 */
185 	public string getGuid()
186 	{
187 		return Str.toString(g_dbus_server_get_guid(gDBusServer));
188 	}
189 
190 	/**
191 	 * Gets whether @server is active.
192 	 *
193 	 * Return: %TRUE if server is active, %FALSE otherwise.
194 	 *
195 	 * Since: 2.26
196 	 */
197 	public bool isActive()
198 	{
199 		return g_dbus_server_is_active(gDBusServer) != 0;
200 	}
201 
202 	/**
203 	 * Starts @server.
204 	 *
205 	 * Since: 2.26
206 	 */
207 	public void start()
208 	{
209 		g_dbus_server_start(gDBusServer);
210 	}
211 
212 	/**
213 	 * Stops @server.
214 	 *
215 	 * Since: 2.26
216 	 */
217 	public void stop()
218 	{
219 		g_dbus_server_stop(gDBusServer);
220 	}
221 
222 	int[string] connectedSignals;
223 
224 	bool delegate(DBusConnection, DBusServer)[] onNewConnectionListeners;
225 	/**
226 	 * Emitted when a new authenticated connection has been made. Use
227 	 * g_dbus_connection_get_peer_credentials() to figure out what
228 	 * identity (if any), was authenticated.
229 	 *
230 	 * If you want to accept the connection, take a reference to the
231 	 * @connection object and return %TRUE. When you are done with the
232 	 * connection call g_dbus_connection_close() and give up your
233 	 * reference. Note that the other peer may disconnect at any time -
234 	 * a typical thing to do when accepting a connection is to listen to
235 	 * the #GDBusConnection::closed signal.
236 	 *
237 	 * If #GDBusServer:flags contains %G_DBUS_SERVER_FLAGS_RUN_IN_THREAD
238 	 * then the signal is emitted in a new thread dedicated to the
239 	 * connection. Otherwise the signal is emitted in the
240 	 * [thread-default main context][g-main-context-push-thread-default]
241 	 * of the thread that @server was constructed in.
242 	 *
243 	 * You are guaranteed that signal handlers for this signal runs
244 	 * before incoming messages on @connection are processed. This means
245 	 * that it's suitable to call g_dbus_connection_register_object() or
246 	 * similar from the signal handler.
247 	 *
248 	 * Params:
249 	 *     connection = A #GDBusConnection for the new connection.
250 	 *
251 	 * Return: %TRUE to claim @connection, %FALSE to let other handlers
252 	 *     run.
253 	 *
254 	 * Since: 2.26
255 	 */
256 	void addOnNewConnection(bool delegate(DBusConnection, DBusServer) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
257 	{
258 		if ( "new-connection" !in connectedSignals )
259 		{
260 			Signals.connectData(
261 				this,
262 				"new-connection",
263 				cast(GCallback)&callBackNewConnection,
264 				cast(void*)this,
265 				null,
266 				connectFlags);
267 			connectedSignals["new-connection"] = 1;
268 		}
269 		onNewConnectionListeners ~= dlg;
270 	}
271 	extern(C) static int callBackNewConnection(GDBusServer* dbusserverStruct, GDBusConnection* connection, DBusServer _dbusserver)
272 	{
273 		foreach ( bool delegate(DBusConnection, DBusServer) dlg; _dbusserver.onNewConnectionListeners )
274 		{
275 			if ( dlg(ObjectG.getDObject!(DBusConnection)(connection), _dbusserver) )
276 			{
277 				return 1;
278 			}
279 		}
280 		
281 		return 0;
282 	}
283 }