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.DBusUtilities;
26 
27 private import gio.AsyncResultIF;
28 private import gio.Cancellable;
29 private import gio.IOStream;
30 private import glib.ErrorG;
31 private import glib.GException;
32 private import glib.Str;
33 private import glib.Variant;
34 private import glib.VariantType;
35 private import gobject.ObjectG;
36 private import gobject.Value;
37 private import gtkc.gio;
38 public  import gtkc.giotypes;
39 
40 
41 /** */
42 public struct DBusUtilities
43 {
44 
45 	/**
46 	 * Escape @string so it can appear in a D-Bus address as the value
47 	 * part of a key-value pair.
48 	 *
49 	 * For instance, if @string is "/run/bus-for-:0",
50 	 * this function would return "/run/bus-for-%3A0",
51 	 * which could be used in a D-Bus address like
52 	 * "unix:nonce-tcp:host=127.0.0.1,port=42,noncefile=/run/bus-for-%3A0".
53 	 *
54 	 * Params:
55 	 *     str = an unescaped string to be included in a D-Bus address
56 	 *         as the value in a key-value pair
57 	 *
58 	 * Returns: a copy of @string with all
59 	 *     non-optionally-escaped bytes escaped
60 	 *
61 	 * Since: 2.36
62 	 */
63 	public static string addressEscapeValue(string str)
64 	{
65 		auto retStr = g_dbus_address_escape_value(Str.toStringz(str));
66 		
67 		scope(exit) Str.freeString(retStr);
68 		return Str.toString(retStr);
69 	}
70 
71 	/**
72 	 * Synchronously looks up the D-Bus address for the well-known message
73 	 * bus instance specified by @bus_type. This may involve using various
74 	 * platform specific mechanisms.
75 	 *
76 	 * The returned address will be in the
77 	 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
78 	 *
79 	 * Params:
80 	 *     busType = a #GBusType
81 	 *     cancellable = a #GCancellable or %NULL
82 	 *
83 	 * Returns: a valid D-Bus address string for @bus_type or %NULL if
84 	 *     @error is set
85 	 *
86 	 * Since: 2.26
87 	 *
88 	 * Throws: GException on failure.
89 	 */
90 	public static string addressGetForBusSync(GBusType busType, Cancellable cancellable)
91 	{
92 		GError* err = null;
93 		
94 		auto retStr = g_dbus_address_get_for_bus_sync(busType, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
95 		
96 		if (err !is null)
97 		{
98 			throw new GException( new ErrorG(err) );
99 		}
100 		
101 		scope(exit) Str.freeString(retStr);
102 		return Str.toString(retStr);
103 	}
104 
105 	/**
106 	 * Asynchronously connects to an endpoint specified by @address and
107 	 * sets up the connection so it is in a state to run the client-side
108 	 * of the D-Bus authentication conversation. @address must be in the
109 	 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
110 	 *
111 	 * When the operation is finished, @callback will be invoked. You can
112 	 * then call g_dbus_address_get_stream_finish() to get the result of
113 	 * the operation.
114 	 *
115 	 * This is an asynchronous failable function. See
116 	 * g_dbus_address_get_stream_sync() for the synchronous version.
117 	 *
118 	 * Params:
119 	 *     address = A valid D-Bus address.
120 	 *     cancellable = A #GCancellable or %NULL.
121 	 *     callback = A #GAsyncReadyCallback to call when the request is satisfied.
122 	 *     userData = Data to pass to @callback.
123 	 *
124 	 * Since: 2.26
125 	 */
126 	public static void addressGetStream(string address, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
127 	{
128 		g_dbus_address_get_stream(Str.toStringz(address), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
129 	}
130 
131 	/**
132 	 * Finishes an operation started with g_dbus_address_get_stream().
133 	 *
134 	 * Params:
135 	 *     res = A #GAsyncResult obtained from the GAsyncReadyCallback passed to g_dbus_address_get_stream().
136 	 *     outGuid = %NULL or return location to store the GUID extracted from @address, if any.
137 	 *
138 	 * Returns: A #GIOStream or %NULL if @error is set.
139 	 *
140 	 * Since: 2.26
141 	 *
142 	 * Throws: GException on failure.
143 	 */
144 	public static IOStream addressGetStreamFinish(AsyncResultIF res, out string outGuid)
145 	{
146 		char* outoutGuid = null;
147 		GError* err = null;
148 		
149 		auto p = g_dbus_address_get_stream_finish((res is null) ? null : res.getAsyncResultStruct(), &outoutGuid, &err);
150 		
151 		if (err !is null)
152 		{
153 			throw new GException( new ErrorG(err) );
154 		}
155 		
156 		outGuid = Str.toString(outoutGuid);
157 		
158 		if(p is null)
159 		{
160 			return null;
161 		}
162 		
163 		return ObjectG.getDObject!(IOStream)(cast(GIOStream*) p, true);
164 	}
165 
166 	/**
167 	 * Synchronously connects to an endpoint specified by @address and
168 	 * sets up the connection so it is in a state to run the client-side
169 	 * of the D-Bus authentication conversation. @address must be in the
170 	 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
171 	 *
172 	 * This is a synchronous failable function. See
173 	 * g_dbus_address_get_stream() for the asynchronous version.
174 	 *
175 	 * Params:
176 	 *     address = A valid D-Bus address.
177 	 *     outGuid = %NULL or return location to store the GUID extracted from @address, if any.
178 	 *     cancellable = A #GCancellable or %NULL.
179 	 *
180 	 * Returns: A #GIOStream or %NULL if @error is set.
181 	 *
182 	 * Since: 2.26
183 	 *
184 	 * Throws: GException on failure.
185 	 */
186 	public static IOStream addressGetStreamSync(string address, out string outGuid, Cancellable cancellable)
187 	{
188 		char* outoutGuid = null;
189 		GError* err = null;
190 		
191 		auto p = g_dbus_address_get_stream_sync(Str.toStringz(address), &outoutGuid, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
192 		
193 		if (err !is null)
194 		{
195 			throw new GException( new ErrorG(err) );
196 		}
197 		
198 		outGuid = Str.toString(outoutGuid);
199 		
200 		if(p is null)
201 		{
202 			return null;
203 		}
204 		
205 		return ObjectG.getDObject!(IOStream)(cast(GIOStream*) p, true);
206 	}
207 
208 	/**
209 	 * Generate a D-Bus GUID that can be used with
210 	 * e.g. g_dbus_connection_new().
211 	 *
212 	 * See the D-Bus specification regarding what strings are valid D-Bus
213 	 * GUID (for example, D-Bus GUIDs are not RFC-4122 compliant).
214 	 *
215 	 * Returns: A valid D-Bus GUID. Free with g_free().
216 	 *
217 	 * Since: 2.26
218 	 */
219 	public static string generateGuid()
220 	{
221 		auto retStr = g_dbus_generate_guid();
222 		
223 		scope(exit) Str.freeString(retStr);
224 		return Str.toString(retStr);
225 	}
226 
227 	/**
228 	 * Converts a #GValue to a #GVariant of the type indicated by the @type
229 	 * parameter.
230 	 *
231 	 * The conversion is using the following rules:
232 	 *
233 	 * - #G_TYPE_STRING: 's', 'o', 'g' or 'ay'
234 	 * - #G_TYPE_STRV: 'as', 'ao' or 'aay'
235 	 * - #G_TYPE_BOOLEAN: 'b'
236 	 * - #G_TYPE_UCHAR: 'y'
237 	 * - #G_TYPE_INT: 'i', 'n'
238 	 * - #G_TYPE_UINT: 'u', 'q'
239 	 * - #G_TYPE_INT64 'x'
240 	 * - #G_TYPE_UINT64: 't'
241 	 * - #G_TYPE_DOUBLE: 'd'
242 	 * - #G_TYPE_VARIANT: Any #GVariantType
243 	 *
244 	 * This can fail if e.g. @gvalue is of type #G_TYPE_STRING and @type
245 	 * is ['i'][G-VARIANT-TYPE-INT32:CAPS]. It will also fail for any #GType
246 	 * (including e.g. #G_TYPE_OBJECT and #G_TYPE_BOXED derived-types) not
247 	 * in the table above.
248 	 *
249 	 * Note that if @gvalue is of type #G_TYPE_VARIANT and its value is
250 	 * %NULL, the empty #GVariant instance (never %NULL) for @type is
251 	 * returned (e.g. 0 for scalar types, the empty string for string types,
252 	 * '/' for object path types, the empty array for any array type and so on).
253 	 *
254 	 * See the g_dbus_gvariant_to_gvalue() function for how to convert a
255 	 * #GVariant to a #GValue.
256 	 *
257 	 * Params:
258 	 *     gvalue = A #GValue to convert to a #GVariant
259 	 *     type = A #GVariantType
260 	 *
261 	 * Returns: A #GVariant (never floating) of #GVariantType @type holding
262 	 *     the data from @gvalue or %NULL in case of failure. Free with
263 	 *     g_variant_unref().
264 	 *
265 	 * Since: 2.30
266 	 */
267 	public static Variant gvalueToGvariant(Value gvalue, VariantType type)
268 	{
269 		auto p = g_dbus_gvalue_to_gvariant((gvalue is null) ? null : gvalue.getValueStruct(), (type is null) ? null : type.getVariantTypeStruct());
270 		
271 		if(p is null)
272 		{
273 			return null;
274 		}
275 		
276 		return new Variant(cast(GVariant*) p, true);
277 	}
278 
279 	/**
280 	 * Converts a #GVariant to a #GValue. If @value is floating, it is consumed.
281 	 *
282 	 * The rules specified in the g_dbus_gvalue_to_gvariant() function are
283 	 * used - this function is essentially its reverse form.
284 	 *
285 	 * The conversion never fails - a valid #GValue is always returned in
286 	 * @out_gvalue.
287 	 *
288 	 * Params:
289 	 *     value = A #GVariant.
290 	 *     outGvalue = Return location pointing to a zero-filled (uninitialized) #GValue.
291 	 *
292 	 * Since: 2.30
293 	 */
294 	public static void gvariantToGvalue(Variant value, out Value outGvalue)
295 	{
296 		GValue* outoutGvalue = gMalloc!GValue();
297 		
298 		g_dbus_gvariant_to_gvalue((value is null) ? null : value.getVariantStruct(), outoutGvalue);
299 		
300 		outGvalue = ObjectG.getDObject!(Value)(outoutGvalue, true);
301 	}
302 
303 	/**
304 	 * Checks if @string is a
305 	 * [D-Bus address](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
306 	 *
307 	 * This doesn't check if @string is actually supported by #GDBusServer
308 	 * or #GDBusConnection - use g_dbus_is_supported_address() to do more
309 	 * checks.
310 	 *
311 	 * Params:
312 	 *     str = A string.
313 	 *
314 	 * Returns: %TRUE if @string is a valid D-Bus address, %FALSE otherwise.
315 	 *
316 	 * Since: 2.26
317 	 */
318 	public static bool isAddress(string str)
319 	{
320 		return g_dbus_is_address(Str.toStringz(str)) != 0;
321 	}
322 
323 	/**
324 	 * Checks if @string is a D-Bus GUID.
325 	 *
326 	 * See the D-Bus specification regarding what strings are valid D-Bus
327 	 * GUID (for example, D-Bus GUIDs are not RFC-4122 compliant).
328 	 *
329 	 * Params:
330 	 *     str = The string to check.
331 	 *
332 	 * Returns: %TRUE if @string is a guid, %FALSE otherwise.
333 	 *
334 	 * Since: 2.26
335 	 */
336 	public static bool isGuid(string str)
337 	{
338 		return g_dbus_is_guid(Str.toStringz(str)) != 0;
339 	}
340 
341 	/**
342 	 * Checks if @string is a valid D-Bus interface name.
343 	 *
344 	 * Params:
345 	 *     str = The string to check.
346 	 *
347 	 * Returns: %TRUE if valid, %FALSE otherwise.
348 	 *
349 	 * Since: 2.26
350 	 */
351 	public static bool isInterfaceName(string str)
352 	{
353 		return g_dbus_is_interface_name(Str.toStringz(str)) != 0;
354 	}
355 
356 	/**
357 	 * Checks if @string is a valid D-Bus member (e.g. signal or method) name.
358 	 *
359 	 * Params:
360 	 *     str = The string to check.
361 	 *
362 	 * Returns: %TRUE if valid, %FALSE otherwise.
363 	 *
364 	 * Since: 2.26
365 	 */
366 	public static bool isMemberName(string str)
367 	{
368 		return g_dbus_is_member_name(Str.toStringz(str)) != 0;
369 	}
370 
371 	/**
372 	 * Checks if @string is a valid D-Bus bus name (either unique or well-known).
373 	 *
374 	 * Params:
375 	 *     str = The string to check.
376 	 *
377 	 * Returns: %TRUE if valid, %FALSE otherwise.
378 	 *
379 	 * Since: 2.26
380 	 */
381 	public static bool isName(string str)
382 	{
383 		return g_dbus_is_name(Str.toStringz(str)) != 0;
384 	}
385 
386 	/**
387 	 * Like g_dbus_is_address() but also checks if the library supports the
388 	 * transports in @string and that key/value pairs for each transport
389 	 * are valid. See the specification of the
390 	 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
391 	 *
392 	 * Params:
393 	 *     str = A string.
394 	 *
395 	 * Returns: %TRUE if @string is a valid D-Bus address that is
396 	 *     supported by this library, %FALSE if @error is set.
397 	 *
398 	 * Since: 2.26
399 	 *
400 	 * Throws: GException on failure.
401 	 */
402 	public static bool isSupportedAddress(string str)
403 	{
404 		GError* err = null;
405 		
406 		auto p = g_dbus_is_supported_address(Str.toStringz(str), &err) != 0;
407 		
408 		if (err !is null)
409 		{
410 			throw new GException( new ErrorG(err) );
411 		}
412 		
413 		return p;
414 	}
415 
416 	/**
417 	 * Checks if @string is a valid D-Bus unique bus name.
418 	 *
419 	 * Params:
420 	 *     str = The string to check.
421 	 *
422 	 * Returns: %TRUE if valid, %FALSE otherwise.
423 	 *
424 	 * Since: 2.26
425 	 */
426 	public static bool isUniqueName(string str)
427 	{
428 		return g_dbus_is_unique_name(Str.toStringz(str)) != 0;
429 	}
430 }