* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
- * Public License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: David Zeuthen <davidz@redhat.com>
*/
/**
* SECTION:gdbusutils
* @title: D-Bus Utilities
- * @short_description: Various utilities related to D-Bus.
+ * @short_description: Various utilities related to D-Bus
* @include: gio/gio.h
*
* Various utility routines related to D-Bus.
/**
* g_dbus_gvariant_to_gvalue:
* @value: A #GVariant.
- * @out_gvalue: Return location for the #GValue.
+ * @out_gvalue: (out): Return location pointing to a zero-filled (uninitialized) #GValue.
*
- * Convert a #GVariant to a #GValue. If @value is floating, it is consumed.
+ * Converts a #GVariant to a #GValue. If @value is floating, it is consumed.
*
- * The rules specified in g_dbus_gvalue_to_gvariant() are used.
+ * The rules specified in the g_dbus_gvalue_to_gvariant() function are
+ * used - this function is essentially its reverse form.
*
- * Note that the passed @out_gvalue does not have to have a #GType
- * set, as it will be initialized to the #GType corresponding to
- * @value.
- *
- * This conversion can never fail.
+ * The conversion never fails - a valid #GValue is always returned in
+ * @out_gvalue.
*
* Since: 2.30
*/
g_value_set_uint64 (out_gvalue, g_variant_get_uint64 (value));
break;
- case G_VARIANT_CLASS_HANDLE:
- g_value_init (out_gvalue, G_TYPE_INT);
- g_value_set_int (out_gvalue, g_variant_get_int32 (value));
- break;
-
case G_VARIANT_CLASS_DOUBLE:
g_value_init (out_gvalue, G_TYPE_DOUBLE);
g_value_set_double (out_gvalue, g_variant_get_double (value));
g_value_take_boxed (out_gvalue, array);
break;
+ case G_VARIANT_CLASS_OBJECT_PATH:
+ g_value_init (out_gvalue, G_TYPE_STRV);
+ array = g_variant_dup_objv (value, NULL);
+ g_value_take_boxed (out_gvalue, array);
+ break;
+
case G_VARIANT_CLASS_ARRAY:
switch (g_variant_type_peek_string (type)[2])
{
}
break;
+ case G_VARIANT_CLASS_HANDLE:
case G_VARIANT_CLASS_VARIANT:
case G_VARIANT_CLASS_MAYBE:
case G_VARIANT_CLASS_TUPLE:
/**
* g_dbus_gvalue_to_gvariant:
- * @gvalue: A #GValue to convert to a #GVariant.
- * @expected_type: The #GVariantType to create.
+ * @gvalue: A #GValue to convert to a #GVariant
+ * @type: A #GVariantType
*
- * Convert a #GValue to #GVariant.
+ * Converts a #GValue to a #GVariant of the type indicated by the @type
+ * parameter.
*
* The conversion is using the following rules:
- * <table frame='all'>
- * <title>#GValue / #GVariant Conversion rules</title>
- * <tgroup cols='2' align='left' colsep='1' rowsep='1'>
- * <thead>
- * <row>
- * <entry>If the #GType for @gvalue is...</entry>
- * <entry>... then @expected_type must be</entry>
- * </row>
- * </thead>
- * <tbody>
- * <row>
- * <entry>#G_TYPE_STRING</entry>
- * <entry>s, o, g, ay</entry>
- * </row>
- * <row>
- * <entry>#G_TYPE_STRV</entry>
- * <entry>as, ao, ag, aay</entry>
- * </row>
- * <row>
- * <entry>#G_TYPE_BOOLEAN</entry>
- * <entry>b</entry>
- * </row>
- * <row>
- * <entry>#G_TYPE_UCHAR</entry>
- * <entry>y</entry>
- * </row>
- * <row>
- * <entry>#G_TYPE_INT</entry>
- * <entry>n</entry>
- * </row>
- * <row>
- * <entry>#G_TYPE_UINT</entry>
- * <entry>q</entry>
- * </row>
- * <row>
- * <entry>#G_TYPE_INT</entry>
- * <entry>i</entry>
- * </row>
- * <row>
- * <entry>#G_TYPE_UINT</entry>
- * <entry>u</entry>
- * </row>
- * <row>
- * <entry>#G_TYPE_INT64</entry>
- * <entry>x</entry>
- * </row>
- * <row>
- * <entry>#G_TYPE_UINT64</entry>
- * <entry>t</entry>
- * </row>
- * <row>
- * <entry>#G_TYPE_INT</entry>
- * <entry>h</entry>
- * </row>
- * <row>
- * <entry>#G_TYPE_DOUBLE</entry>
- * <entry>d</entry>
- * </row>
- * <row>
- * <entry>#G_TYPE_VARIANT</entry>
- * <entry>Any #GVariantType</entry>
- * </row>
- * </tbody>
- * </tgroup>
- * </table>
- * This can fail if e.g. @gvalue is of type #G_TYPE_STRING and
- * @expected_type is <literal>'i'</literal>. It will also fail for any
- * #GType (including e.g. #G_TYPE_OBJECT and #G_TYPE_BOXED
- * derived-types) not in the table above.
+ *
+ * - #G_TYPE_STRING: 's', 'o', 'g' or 'ay'
+ * - #G_TYPE_STRV: 'as', 'ao' or 'aay'
+ * - #G_TYPE_BOOLEAN: 'b'
+ * - #G_TYPE_UCHAR: 'y'
+ * - #G_TYPE_INT: 'i', 'n'
+ * - #G_TYPE_UINT: 'u', 'q'
+ * - #G_TYPE_INT64 'x'
+ * - #G_TYPE_UINT64: 't'
+ * - #G_TYPE_DOUBLE: 'd'
+ * - #G_TYPE_VARIANT: Any #GVariantType
+ *
+ * This can fail if e.g. @gvalue is of type #G_TYPE_STRING and @type
+ * is ['i'][G-VARIANT-TYPE-INT32:CAPS]. It will also fail for any #GType
+ * (including e.g. #G_TYPE_OBJECT and #G_TYPE_BOXED derived-types) not
+ * in the table above.
*
* Note that if @gvalue is of type #G_TYPE_VARIANT and its value is
- * %NULL, the <emphasis>empty</emphasis> #GVariant instance (never
- * %NULL) for @expected_type is returned (e.g. 0 for scalar types, the
- * empty string for string types and so on).
+ * %NULL, the empty #GVariant instance (never %NULL) for @type is
+ * returned (e.g. 0 for scalar types, the empty string for string types,
+ * '/' for object path types, the empty array for any array type and so on).
+ *
+ * See the g_dbus_gvariant_to_gvalue() function for how to convert a
+ * #GVariant to a #GValue.
*
- * Returns: A #GVariant (never floating) of #GVariantType
- * @expected_type holding the data from @gvalue or %NULL in case of
- * failure. Free with g_variant_unref().
+ * Returns: A #GVariant (never floating) of #GVariantType @type holding
+ * the data from @gvalue or %NULL in case of failure. Free with
+ * g_variant_unref().
*
* Since: 2.30
*/
GVariant *
-g_dbus_gvalue_to_gvariant (const GValue *gvalue,
- const GVariantType *expected_type)
+g_dbus_gvalue_to_gvariant (const GValue *gvalue,
+ const GVariantType *type)
{
GVariant *ret;
const gchar *s;
const gchar *empty_strv[1] = {NULL};
g_return_val_if_fail (gvalue != NULL, NULL);
- g_return_val_if_fail (expected_type != NULL, NULL);
+ g_return_val_if_fail (type != NULL, NULL);
ret = NULL;
- /* The expected type could easily be e.g. "s" with the GValue holding a string.
- * because of the UseGVariant annotation
+ /* @type can easily be e.g. "s" with the GValue holding a GVariant - for example this
+ * can happen when using the org.gtk.GDBus.C.ForceGVariant annotation with the
+ * gdbus-codegen(1) tool.
*/
if (G_VALUE_TYPE (gvalue) == G_TYPE_VARIANT)
{
}
else
{
- switch (g_variant_type_peek_string (expected_type)[0])
+ switch (g_variant_type_peek_string (type)[0])
{
case G_VARIANT_CLASS_BOOLEAN:
ret = g_variant_ref_sink (g_variant_new_boolean (g_value_get_boolean (gvalue)));
ret = g_variant_ref_sink (g_variant_new_uint64 (g_value_get_uint64 (gvalue)));
break;
- case G_VARIANT_CLASS_HANDLE:
- ret = g_variant_ref_sink (g_variant_new_handle (g_value_get_int (gvalue)));
- break;
-
case G_VARIANT_CLASS_DOUBLE:
ret = g_variant_ref_sink (g_variant_new_double (g_value_get_double (gvalue)));
break;
break;
case G_VARIANT_CLASS_ARRAY:
- switch (g_variant_type_peek_string (expected_type)[1])
+ switch (g_variant_type_peek_string (type)[1])
{
case G_VARIANT_CLASS_BYTE:
s = g_value_get_string (gvalue);
ret = g_variant_ref_sink (g_variant_new_strv (as, -1));
break;
+ case G_VARIANT_CLASS_OBJECT_PATH:
+ as = g_value_get_boxed (gvalue);
+ if (as == NULL)
+ as = empty_strv;
+ ret = g_variant_ref_sink (g_variant_new_objv (as, -1));
+ break;
+
case G_VARIANT_CLASS_ARRAY:
- switch (g_variant_type_peek_string (expected_type)[2])
+ switch (g_variant_type_peek_string (type)[2])
{
case G_VARIANT_CLASS_BYTE:
as = g_value_get_boxed (gvalue);
}
break;
- default:
+ case G_VARIANT_CLASS_HANDLE:
case G_VARIANT_CLASS_VARIANT:
case G_VARIANT_CLASS_MAYBE:
case G_VARIANT_CLASS_TUPLE:
if (ret == NULL)
{
GVariant *untrusted_empty;
- untrusted_empty = g_variant_new_from_data (expected_type, NULL, 0, FALSE, NULL, NULL);
+ untrusted_empty = g_variant_new_from_data (type, NULL, 0, FALSE, NULL, NULL);
ret = g_variant_ref_sink (g_variant_get_normal_form (untrusted_empty));
g_variant_unref (untrusted_empty);
}