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