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 glib.c.functions;
39 private import gobject.ObjectG;
40 private import gobject.Signals;
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 GDBus can be found
55  * in [gdbus-example-peer.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-peer.c).
56  * 
57  * Note that a minimal #GDBusServer will accept connections from any
58  * peer. In many use-cases it will be necessary to add a #GDBusAuthObserver
59  * that only accepts connections that have successfully authenticated
60  * as the same user that is running the #GDBusServer. Since GLib 2.68 this can
61  * be achieved more simply by passing the
62  * %G_DBUS_SERVER_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER flag to the server.
63  *
64  * Since: 2.26
65  */
66 public class DBusServer : ObjectG, InitableIF
67 {
68 	/** the main Gtk struct */
69 	protected GDBusServer* gDBusServer;
70 
71 	/** Get the main Gtk struct */
72 	public GDBusServer* getDBusServerStruct(bool transferOwnership = false)
73 	{
74 		if (transferOwnership)
75 			ownedRef = false;
76 		return gDBusServer;
77 	}
78 
79 	/** the main Gtk struct as a void* */
80 	protected override void* getStruct()
81 	{
82 		return cast(void*)gDBusServer;
83 	}
84 
85 	/**
86 	 * Sets our main struct and passes it to the parent class.
87 	 */
88 	public this (GDBusServer* gDBusServer, bool ownedRef = false)
89 	{
90 		this.gDBusServer = gDBusServer;
91 		super(cast(GObject*)gDBusServer, ownedRef);
92 	}
93 
94 	// add the Initable capabilities
95 	mixin InitableT!(GDBusServer);
96 
97 
98 	/** */
99 	public static GType getType()
100 	{
101 		return g_dbus_server_get_type();
102 	}
103 
104 	/**
105 	 * Creates a new D-Bus server that listens on the first address in
106 	 * @address that works.
107 	 *
108 	 * Once constructed, you can use g_dbus_server_get_client_address() to
109 	 * get a D-Bus address string that clients can use to connect.
110 	 *
111 	 * To have control over the available authentication mechanisms and
112 	 * the users that are authorized to connect, it is strongly recommended
113 	 * to provide a non-%NULL #GDBusAuthObserver.
114 	 *
115 	 * Connect to the #GDBusServer::new-connection signal to handle
116 	 * incoming connections.
117 	 *
118 	 * The returned #GDBusServer isn't active - you have to start it with
119 	 * g_dbus_server_start().
120 	 *
121 	 * #GDBusServer is used in this [example][gdbus-peer-to-peer].
122 	 *
123 	 * This is a synchronous failable constructor. There is currently no
124 	 * asynchronous version.
125 	 *
126 	 * Params:
127 	 *     address = A D-Bus address.
128 	 *     flags = Flags from the #GDBusServerFlags enumeration.
129 	 *     guid = A D-Bus GUID.
130 	 *     observer = A #GDBusAuthObserver or %NULL.
131 	 *     cancellable = A #GCancellable or %NULL.
132 	 *
133 	 * Returns: A #GDBusServer or %NULL if @error is set. Free with
134 	 *     g_object_unref().
135 	 *
136 	 * Since: 2.26
137 	 *
138 	 * Throws: GException on failure.
139 	 * Throws: ConstructionException GTK+ fails to create the object.
140 	 */
141 	public this(string address, GDBusServerFlags flags, string guid, DBusAuthObserver observer, Cancellable cancellable)
142 	{
143 		GError* err = null;
144 
145 		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);
146 
147 		if (err !is null)
148 		{
149 			throw new GException( new ErrorG(err) );
150 		}
151 
152 		if(__p is null)
153 		{
154 			throw new ConstructionException("null returned by new_sync");
155 		}
156 
157 		this(cast(GDBusServer*) __p, true);
158 	}
159 
160 	/**
161 	 * Gets a
162 	 * [D-Bus address](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses)
163 	 * string that can be used by clients to connect to @server.
164 	 *
165 	 * Returns: A D-Bus address string. Do not free, the string is owned
166 	 *     by @server.
167 	 *
168 	 * Since: 2.26
169 	 */
170 	public string getClientAddress()
171 	{
172 		return Str.toString(g_dbus_server_get_client_address(gDBusServer));
173 	}
174 
175 	/**
176 	 * Gets the flags for @server.
177 	 *
178 	 * Returns: A set of flags from the #GDBusServerFlags enumeration.
179 	 *
180 	 * Since: 2.26
181 	 */
182 	public GDBusServerFlags getFlags()
183 	{
184 		return g_dbus_server_get_flags(gDBusServer);
185 	}
186 
187 	/**
188 	 * Gets the GUID for @server.
189 	 *
190 	 * Returns: A D-Bus GUID. Do not free this string, it is owned by @server.
191 	 *
192 	 * Since: 2.26
193 	 */
194 	public string getGuid()
195 	{
196 		return Str.toString(g_dbus_server_get_guid(gDBusServer));
197 	}
198 
199 	/**
200 	 * Gets whether @server is active.
201 	 *
202 	 * Returns: %TRUE if server is active, %FALSE otherwise.
203 	 *
204 	 * Since: 2.26
205 	 */
206 	public bool isActive()
207 	{
208 		return g_dbus_server_is_active(gDBusServer) != 0;
209 	}
210 
211 	/**
212 	 * Starts @server.
213 	 *
214 	 * Since: 2.26
215 	 */
216 	public void start()
217 	{
218 		g_dbus_server_start(gDBusServer);
219 	}
220 
221 	/**
222 	 * Stops @server.
223 	 *
224 	 * Since: 2.26
225 	 */
226 	public void stop()
227 	{
228 		g_dbus_server_stop(gDBusServer);
229 	}
230 
231 	/**
232 	 * Emitted when a new authenticated connection has been made. Use
233 	 * g_dbus_connection_get_peer_credentials() to figure out what
234 	 * identity (if any), was authenticated.
235 	 *
236 	 * If you want to accept the connection, take a reference to the
237 	 * @connection object and return %TRUE. When you are done with the
238 	 * connection call g_dbus_connection_close() and give up your
239 	 * reference. Note that the other peer may disconnect at any time -
240 	 * a typical thing to do when accepting a connection is to listen to
241 	 * the #GDBusConnection::closed signal.
242 	 *
243 	 * If #GDBusServer:flags contains %G_DBUS_SERVER_FLAGS_RUN_IN_THREAD
244 	 * then the signal is emitted in a new thread dedicated to the
245 	 * connection. Otherwise the signal is emitted in the
246 	 * [thread-default main context][g-main-context-push-thread-default]
247 	 * of the thread that @server was constructed in.
248 	 *
249 	 * You are guaranteed that signal handlers for this signal runs
250 	 * before incoming messages on @connection are processed. This means
251 	 * that it's suitable to call g_dbus_connection_register_object() or
252 	 * similar from the signal handler.
253 	 *
254 	 * Params:
255 	 *     connection = A #GDBusConnection for the new connection.
256 	 *
257 	 * Returns: %TRUE to claim @connection, %FALSE to let other handlers
258 	 *     run.
259 	 *
260 	 * Since: 2.26
261 	 */
262 	gulong addOnNewConnection(bool delegate(DBusConnection, DBusServer) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
263 	{
264 		return Signals.connect(this, "new-connection", dlg, connectFlags ^ ConnectFlags.SWAPPED);
265 	}
266 }