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 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() 66 { 67 return gDBusServer; 68 } 69 70 /** the main Gtk struct as a void* */ 71 protected override void* getStruct() 72 { 73 return cast(void*)gDBusServer; 74 } 75 76 protected override void setStruct(GObject* obj) 77 { 78 gDBusServer = cast(GDBusServer*)obj; 79 super.setStruct(obj); 80 } 81 82 /** 83 * Sets our main struct and passes it to the parent class. 84 */ 85 public this (GDBusServer* gDBusServer, bool ownedRef = false) 86 { 87 this.gDBusServer = gDBusServer; 88 super(cast(GObject*)gDBusServer, ownedRef); 89 } 90 91 // add the Initable capabilities 92 mixin InitableT!(GDBusServer); 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 (err !is null) 141 { 142 throw new GException( new ErrorG(err) ); 143 } 144 145 if(p is null) 146 { 147 throw new ConstructionException("null returned by new_sync"); 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 protected class OnNewConnectionDelegateWrapper 224 { 225 bool delegate(DBusConnection, DBusServer) dlg; 226 gulong handlerId; 227 ConnectFlags flags; 228 this(bool delegate(DBusConnection, DBusServer) dlg, gulong handlerId, ConnectFlags flags) 229 { 230 this.dlg = dlg; 231 this.handlerId = handlerId; 232 this.flags = flags; 233 } 234 } 235 protected OnNewConnectionDelegateWrapper[] onNewConnectionListeners; 236 237 /** 238 * Emitted when a new authenticated connection has been made. Use 239 * g_dbus_connection_get_peer_credentials() to figure out what 240 * identity (if any), was authenticated. 241 * 242 * If you want to accept the connection, take a reference to the 243 * @connection object and return %TRUE. When you are done with the 244 * connection call g_dbus_connection_close() and give up your 245 * reference. Note that the other peer may disconnect at any time - 246 * a typical thing to do when accepting a connection is to listen to 247 * the #GDBusConnection::closed signal. 248 * 249 * If #GDBusServer:flags contains %G_DBUS_SERVER_FLAGS_RUN_IN_THREAD 250 * then the signal is emitted in a new thread dedicated to the 251 * connection. Otherwise the signal is emitted in the 252 * [thread-default main context][g-main-context-push-thread-default] 253 * of the thread that @server was constructed in. 254 * 255 * You are guaranteed that signal handlers for this signal runs 256 * before incoming messages on @connection are processed. This means 257 * that it's suitable to call g_dbus_connection_register_object() or 258 * similar from the signal handler. 259 * 260 * Params: 261 * connection = A #GDBusConnection for the new connection. 262 * 263 * Return: %TRUE to claim @connection, %FALSE to let other handlers 264 * run. 265 * 266 * Since: 2.26 267 */ 268 gulong addOnNewConnection(bool delegate(DBusConnection, DBusServer) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 269 { 270 onNewConnectionListeners ~= new OnNewConnectionDelegateWrapper(dlg, 0, connectFlags); 271 onNewConnectionListeners[onNewConnectionListeners.length - 1].handlerId = Signals.connectData( 272 this, 273 "new-connection", 274 cast(GCallback)&callBackNewConnection, 275 cast(void*)onNewConnectionListeners[onNewConnectionListeners.length - 1], 276 cast(GClosureNotify)&callBackNewConnectionDestroy, 277 connectFlags); 278 return onNewConnectionListeners[onNewConnectionListeners.length - 1].handlerId; 279 } 280 281 extern(C) static int callBackNewConnection(GDBusServer* dbusserverStruct, GDBusConnection* connection,OnNewConnectionDelegateWrapper wrapper) 282 { 283 return wrapper.dlg(ObjectG.getDObject!(DBusConnection)(connection), wrapper.outer); 284 } 285 286 extern(C) static void callBackNewConnectionDestroy(OnNewConnectionDelegateWrapper wrapper, GClosure* closure) 287 { 288 wrapper.outer.internalRemoveOnNewConnection(wrapper); 289 } 290 291 protected void internalRemoveOnNewConnection(OnNewConnectionDelegateWrapper source) 292 { 293 foreach(index, wrapper; onNewConnectionListeners) 294 { 295 if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId) 296 { 297 onNewConnectionListeners[index] = null; 298 onNewConnectionListeners = std.algorithm.remove(onNewConnectionListeners, index); 299 break; 300 } 301 } 302 } 303 304 }