1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
28 #include "gdbusutils.h"
34 * @title: D-Bus Utilities
35 * @short_description: Various utilities related to D-Bus.
38 * Various utility routines related to D-Bus.
42 is_valid_bus_name_character (gint c,
43 gboolean allow_hyphen)
46 (c >= '0' && c <= '9') ||
47 (c >= 'A' && c <= 'Z') ||
48 (c >= 'a' && c <= 'z') ||
50 (allow_hyphen && c == '-');
54 is_valid_initial_bus_name_character (gint c,
55 gboolean allow_initial_digit,
56 gboolean allow_hyphen)
58 if (allow_initial_digit)
59 return is_valid_bus_name_character (c, allow_hyphen);
62 (c >= 'A' && c <= 'Z') ||
63 (c >= 'a' && c <= 'z') ||
65 (allow_hyphen && c == '-');
69 is_valid_name (const gchar *start,
71 gboolean allow_initial_digit,
72 gboolean allow_hyphen)
92 if (G_UNLIKELY (!is_valid_initial_bus_name_character (*s, allow_initial_digit, allow_hyphen)))
96 else if (G_UNLIKELY (!is_valid_bus_name_character (*s, allow_hyphen)))
103 if (G_UNLIKELY (!has_dot))
114 * @string: The string to check.
116 * Checks if @string is a valid D-Bus bus name (either unique or well-known).
118 * Returns: %TRUE if valid, %FALSE otherwise.
123 g_dbus_is_name (const gchar *string)
129 g_return_val_if_fail (string != NULL, FALSE);
133 len = strlen (string);
134 if (G_UNLIKELY (len == 0 || len > 255))
140 /* handle unique name */
141 if (!is_valid_name (s + 1, len - 1, TRUE, TRUE))
146 else if (G_UNLIKELY (*s == '.'))
148 /* can't start with a . */
151 else if (G_UNLIKELY (!is_valid_initial_bus_name_character (*s, FALSE, TRUE)))
154 ret = is_valid_name (s + 1, len - 1, FALSE, TRUE);
161 * g_dbus_is_unique_name:
162 * @string: The string to check.
164 * Checks if @string is a valid D-Bus unique bus name.
166 * Returns: %TRUE if valid, %FALSE otherwise.
171 g_dbus_is_unique_name (const gchar *string)
176 g_return_val_if_fail (string != NULL, FALSE);
180 len = strlen (string);
181 if (G_UNLIKELY (len == 0 || len > 255))
184 if (G_UNLIKELY (*string != ':'))
187 if (G_UNLIKELY (!is_valid_name (string + 1, len - 1, TRUE, TRUE)))
197 * g_dbus_is_member_name:
198 * @string: The string to check.
200 * Checks if @string is a valid D-Bus member (e.g. signal or method) name.
202 * Returns: %TRUE if valid, %FALSE otherwise.
207 g_dbus_is_member_name (const gchar *string)
213 if (G_UNLIKELY (string == NULL))
216 if (G_UNLIKELY (!is_valid_initial_bus_name_character (string[0], FALSE, FALSE)))
219 for (n = 1; string[n] != '\0'; n++)
221 if (G_UNLIKELY (!is_valid_bus_name_character (string[n], FALSE)))
234 * g_dbus_is_interface_name:
235 * @string: The string to check.
237 * Checks if @string is a valid D-Bus interface name.
239 * Returns: %TRUE if valid, %FALSE otherwise.
244 g_dbus_is_interface_name (const gchar *string)
250 g_return_val_if_fail (string != NULL, FALSE);
254 len = strlen (string);
255 if (G_UNLIKELY (len == 0 || len > 255))
259 if (G_UNLIKELY (*s == '.'))
261 /* can't start with a . */
264 else if (G_UNLIKELY (!is_valid_initial_bus_name_character (*s, FALSE, FALSE)))
267 ret = is_valid_name (s + 1, len - 1, FALSE, FALSE);
273 /* ---------------------------------------------------------------------------------------------------- */
275 /* TODO: maybe move to glib? if so, it should conform to http://en.wikipedia.org/wiki/Guid and/or
276 * http://tools.ietf.org/html/rfc4122 - specifically it should have hyphens then.
280 * g_dbus_generate_guid:
282 * Generate a D-Bus GUID that can be used with
283 * e.g. g_dbus_connection_new().
285 * See the D-Bus specification regarding what strings are valid D-Bus
286 * GUID (for example, D-Bus GUIDs are not RFC-4122 compliant).
288 * Returns: A valid D-Bus GUID. Free with g_free().
293 g_dbus_generate_guid (void)
301 s = g_string_new (NULL);
303 r1 = g_random_int ();
304 r2 = g_random_int ();
305 r3 = g_random_int ();
306 g_get_current_time (&now);
308 g_string_append_printf (s, "%08x", r1);
309 g_string_append_printf (s, "%08x", r2);
310 g_string_append_printf (s, "%08x", r3);
311 g_string_append_printf (s, "%08x", (guint32) now.tv_sec);
313 return g_string_free (s, FALSE);
318 * @string: The string to check.
320 * Checks if @string is a D-Bus GUID.
322 * See the D-Bus specification regarding what strings are valid D-Bus
323 * GUID (for example, D-Bus GUIDs are not RFC-4122 compliant).
325 * Returns: %TRUE if @string is a guid, %FALSE otherwise.
330 g_dbus_is_guid (const gchar *string)
335 g_return_val_if_fail (string != NULL, FALSE);
339 for (n = 0; n < 32; n++)
341 if (!g_ascii_isxdigit (string[n]))
344 if (string[32] != '\0')
353 /* ---------------------------------------------------------------------------------------------------- */
356 * g_dbus_gvariant_to_gvalue:
357 * @value: A #GVariant.
358 * @out_gvalue: Return location pointing to a zero-filled (uninitialized) #GValue.
360 * Converts a #GVariant to a #GValue. If @value is floating, it is consumed.
362 * The rules specified in the g_dbus_gvalue_to_gvariant() function are
363 * used - this function is essentially its reverse form.
365 * The conversion never fails - a valid #GValue is always returned in
371 g_dbus_gvariant_to_gvalue (GVariant *value,
374 const GVariantType *type;
377 g_return_if_fail (value != NULL);
378 g_return_if_fail (out_gvalue != NULL);
380 memset (out_gvalue, '\0', sizeof (GValue));
382 switch (g_variant_classify (value))
384 case G_VARIANT_CLASS_BOOLEAN:
385 g_value_init (out_gvalue, G_TYPE_BOOLEAN);
386 g_value_set_boolean (out_gvalue, g_variant_get_boolean (value));
389 case G_VARIANT_CLASS_BYTE:
390 g_value_init (out_gvalue, G_TYPE_UCHAR);
391 g_value_set_uchar (out_gvalue, g_variant_get_byte (value));
394 case G_VARIANT_CLASS_INT16:
395 g_value_init (out_gvalue, G_TYPE_INT);
396 g_value_set_int (out_gvalue, g_variant_get_int16 (value));
399 case G_VARIANT_CLASS_UINT16:
400 g_value_init (out_gvalue, G_TYPE_UINT);
401 g_value_set_uint (out_gvalue, g_variant_get_uint16 (value));
404 case G_VARIANT_CLASS_INT32:
405 g_value_init (out_gvalue, G_TYPE_INT);
406 g_value_set_int (out_gvalue, g_variant_get_int32 (value));
409 case G_VARIANT_CLASS_UINT32:
410 g_value_init (out_gvalue, G_TYPE_UINT);
411 g_value_set_uint (out_gvalue, g_variant_get_uint32 (value));
414 case G_VARIANT_CLASS_INT64:
415 g_value_init (out_gvalue, G_TYPE_INT64);
416 g_value_set_int64 (out_gvalue, g_variant_get_int64 (value));
419 case G_VARIANT_CLASS_UINT64:
420 g_value_init (out_gvalue, G_TYPE_UINT64);
421 g_value_set_uint64 (out_gvalue, g_variant_get_uint64 (value));
424 case G_VARIANT_CLASS_HANDLE:
425 g_value_init (out_gvalue, G_TYPE_INT);
426 g_value_set_int (out_gvalue, g_variant_get_int32 (value));
429 case G_VARIANT_CLASS_DOUBLE:
430 g_value_init (out_gvalue, G_TYPE_DOUBLE);
431 g_value_set_double (out_gvalue, g_variant_get_double (value));
434 case G_VARIANT_CLASS_STRING:
435 g_value_init (out_gvalue, G_TYPE_STRING);
436 g_value_set_string (out_gvalue, g_variant_get_string (value, NULL));
439 case G_VARIANT_CLASS_OBJECT_PATH:
440 g_value_init (out_gvalue, G_TYPE_STRING);
441 g_value_set_string (out_gvalue, g_variant_get_string (value, NULL));
444 case G_VARIANT_CLASS_SIGNATURE:
445 g_value_init (out_gvalue, G_TYPE_STRING);
446 g_value_set_string (out_gvalue, g_variant_get_string (value, NULL));
449 case G_VARIANT_CLASS_ARRAY:
450 type = g_variant_get_type (value);
451 switch (g_variant_type_peek_string (type)[1])
453 case G_VARIANT_CLASS_BYTE:
454 g_value_init (out_gvalue, G_TYPE_STRING);
455 g_value_set_string (out_gvalue, g_variant_get_bytestring (value));
458 case G_VARIANT_CLASS_STRING:
459 g_value_init (out_gvalue, G_TYPE_STRV);
460 array = g_variant_dup_strv (value, NULL);
461 g_value_take_boxed (out_gvalue, array);
464 case G_VARIANT_CLASS_ARRAY:
465 switch (g_variant_type_peek_string (type)[2])
467 case G_VARIANT_CLASS_BYTE:
468 g_value_init (out_gvalue, G_TYPE_STRV);
469 array = g_variant_dup_bytestring_array (value, NULL);
470 g_value_take_boxed (out_gvalue, array);
474 g_value_init (out_gvalue, G_TYPE_VARIANT);
475 g_value_set_variant (out_gvalue, value);
481 g_value_init (out_gvalue, G_TYPE_VARIANT);
482 g_value_set_variant (out_gvalue, value);
487 case G_VARIANT_CLASS_VARIANT:
488 case G_VARIANT_CLASS_MAYBE:
489 case G_VARIANT_CLASS_TUPLE:
490 case G_VARIANT_CLASS_DICT_ENTRY:
491 g_value_init (out_gvalue, G_TYPE_VARIANT);
492 g_value_set_variant (out_gvalue, value);
499 * g_dbus_gvalue_to_gvariant:
500 * @gvalue: A #GValue to convert to a #GVariant.
501 * @type: A #GVariantType.
503 * Converts a #GValue to a #GVariant of the type indicated by the @type parameter.
505 * The conversion is using the following rules:
506 * <table frame='all'>
507 * <title>#GValue / #GVariant conversion rules</title>
508 * <tgroup cols='2' align='left' colsep='1' rowsep='1'>
511 * <entry>If the #GType for @gvalue is...</entry>
512 * <entry>... then @type must be</entry>
517 * <entry>#G_TYPE_STRING</entry>
518 * <entry><link linkend="G-VARIANT-TYPE-STRING:CAPS">'s'</link>, <link linkend="G-VARIANT-TYPE-OBJECT-PATH:CAPS">'o'</link>, <link linkend="G-VARIANT-TYPE-SIGNATURE:CAPS">'g'</link> or <link linkend="G-VARIANT-TYPE-BYTESTRING:CAPS">'ay'</link></entry>
521 * <entry>#G_TYPE_STRV</entry>
522 * <entry><link linkend="G-VARIANT-TYPE-STRING-ARRAY:CAPS">'as'</link> or <link linkend="G-VARIANT-TYPE-BYTESTRING-ARRAY:CAPS">'aay'</link></entry>
525 * <entry>#G_TYPE_BOOLEAN</entry>
526 * <entry><link linkend="G-VARIANT-TYPE-BOOLEAN:CAPS">'b'</link></entry>
529 * <entry>#G_TYPE_UCHAR</entry>
530 * <entry><link linkend="G-VARIANT-TYPE-BYTE:CAPS">'y'</link></entry>
533 * <entry>#G_TYPE_INT</entry>
534 * <entry><link linkend="G-VARIANT-TYPE-INT32:CAPS">'i'</link> or <link linkend="G-VARIANT-TYPE-INT16:CAPS">'n'</link></entry>
537 * <entry>#G_TYPE_UINT</entry>
538 * <entry><link linkend="G-VARIANT-TYPE-UINT32:CAPS">'u'</link> or <link linkend="G-VARIANT-TYPE-UINT16:CAPS">'q'</link></entry>
541 * <entry>#G_TYPE_INT64</entry>
542 * <entry><link linkend="G-VARIANT-TYPE-INT64:CAPS">'x'</link></entry>
545 * <entry>#G_TYPE_UINT64</entry>
546 * <entry><link linkend="G-VARIANT-TYPE-UINT64:CAPS">'t'</link></entry>
549 * <entry>#G_TYPE_INT</entry>
550 * <entry><link linkend="G-VARIANT-TYPE-HANDLE:CAPS">'h'</link></entry>
553 * <entry>#G_TYPE_DOUBLE</entry>
554 * <entry><link linkend="G-VARIANT-TYPE-DOUBLE:CAPS">'d'</link></entry>
557 * <entry>#G_TYPE_VARIANT</entry>
558 * <entry>Any #GVariantType</entry>
563 * This can fail if e.g. @gvalue is of type #G_TYPE_STRING and @type
564 * is <link linkend="G-VARIANT-TYPE-INT32:CAPS">'i'</link>. It will
565 * also fail for any #GType (including e.g. #G_TYPE_OBJECT and
566 * #G_TYPE_BOXED derived-types) not in the table above.
568 * Note that if @gvalue is of type #G_TYPE_VARIANT and its value is
569 * %NULL, the <emphasis>empty</emphasis> #GVariant instance (never
570 * %NULL) for @type is returned (e.g. 0 for scalar types, the empty
571 * string for string types, <literal>'/'</literal> for object path
572 * types, the empty array for any array type and so on).
574 * See the g_dbus_gvariant_to_gvalue() function for how to convert a
575 * #GVariant to a #GValue.
577 * Returns: A #GVariant (never floating) of #GVariantType
578 * @type holding the data from @gvalue or %NULL in case of
579 * failure. Free with g_variant_unref().
584 g_dbus_gvalue_to_gvariant (const GValue *gvalue,
585 const GVariantType *type)
589 const gchar * const *as;
590 const gchar *empty_strv[1] = {NULL};
592 g_return_val_if_fail (gvalue != NULL, NULL);
593 g_return_val_if_fail (type != NULL, NULL);
597 /* @type can easily be e.g. "s" with the GValue holding a GVariant - for example this
598 * can happen when using the org.gtk.GDBus.C.ForceGVariant annotation with the
599 * gdbus-codegen(1) tool.
601 if (G_VALUE_TYPE (gvalue) == G_TYPE_VARIANT)
603 ret = g_value_dup_variant (gvalue);
607 switch (g_variant_type_peek_string (type)[0])
609 case G_VARIANT_CLASS_BOOLEAN:
610 ret = g_variant_ref_sink (g_variant_new_boolean (g_value_get_boolean (gvalue)));
613 case G_VARIANT_CLASS_BYTE:
614 ret = g_variant_ref_sink (g_variant_new_byte (g_value_get_uchar (gvalue)));
617 case G_VARIANT_CLASS_INT16:
618 ret = g_variant_ref_sink (g_variant_new_int16 (g_value_get_int (gvalue)));
621 case G_VARIANT_CLASS_UINT16:
622 ret = g_variant_ref_sink (g_variant_new_uint16 (g_value_get_uint (gvalue)));
625 case G_VARIANT_CLASS_INT32:
626 ret = g_variant_ref_sink (g_variant_new_int32 (g_value_get_int (gvalue)));
629 case G_VARIANT_CLASS_UINT32:
630 ret = g_variant_ref_sink (g_variant_new_uint32 (g_value_get_uint (gvalue)));
633 case G_VARIANT_CLASS_INT64:
634 ret = g_variant_ref_sink (g_variant_new_int64 (g_value_get_int64 (gvalue)));
637 case G_VARIANT_CLASS_UINT64:
638 ret = g_variant_ref_sink (g_variant_new_uint64 (g_value_get_uint64 (gvalue)));
641 case G_VARIANT_CLASS_HANDLE:
642 ret = g_variant_ref_sink (g_variant_new_handle (g_value_get_int (gvalue)));
645 case G_VARIANT_CLASS_DOUBLE:
646 ret = g_variant_ref_sink (g_variant_new_double (g_value_get_double (gvalue)));
649 case G_VARIANT_CLASS_STRING:
650 s = g_value_get_string (gvalue);
653 ret = g_variant_ref_sink (g_variant_new_string (s));
656 case G_VARIANT_CLASS_OBJECT_PATH:
657 s = g_value_get_string (gvalue);
660 ret = g_variant_ref_sink (g_variant_new_object_path (s));
663 case G_VARIANT_CLASS_SIGNATURE:
664 s = g_value_get_string (gvalue);
667 ret = g_variant_ref_sink (g_variant_new_signature (s));
670 case G_VARIANT_CLASS_ARRAY:
671 switch (g_variant_type_peek_string (type)[1])
673 case G_VARIANT_CLASS_BYTE:
674 s = g_value_get_string (gvalue);
677 ret = g_variant_ref_sink (g_variant_new_bytestring (s));
680 case G_VARIANT_CLASS_STRING:
681 as = g_value_get_boxed (gvalue);
684 ret = g_variant_ref_sink (g_variant_new_strv (as, -1));
687 case G_VARIANT_CLASS_ARRAY:
688 switch (g_variant_type_peek_string (type)[2])
690 case G_VARIANT_CLASS_BYTE:
691 as = g_value_get_boxed (gvalue);
694 ret = g_variant_ref_sink (g_variant_new_bytestring_array (as, -1));
698 ret = g_value_dup_variant (gvalue);
704 ret = g_value_dup_variant (gvalue);
710 case G_VARIANT_CLASS_VARIANT:
711 case G_VARIANT_CLASS_MAYBE:
712 case G_VARIANT_CLASS_TUPLE:
713 case G_VARIANT_CLASS_DICT_ENTRY:
714 ret = g_value_dup_variant (gvalue);
719 /* Could be that the GValue is holding a NULL GVariant - in that case,
720 * we return an "empty" GVariant instead of a NULL GVariant
724 GVariant *untrusted_empty;
725 untrusted_empty = g_variant_new_from_data (type, NULL, 0, FALSE, NULL, NULL);
726 ret = g_variant_ref_sink (g_variant_get_normal_form (untrusted_empty));
727 g_variant_unref (untrusted_empty);
730 g_assert (!g_variant_is_floating (ret));