コード例 #1
0
static void
ide_preferences_spin_button_value_changed (IdePreferencesSpinButton *self,
                                           GParamSpec               *pspec,
                                           GtkSpinButton            *spin_button)
{
  GVariant *variant = NULL;
  gdouble value;

  g_assert (IDE_IS_PREFERENCES_SPIN_BUTTON (self));
  g_assert (pspec != NULL);
  g_assert (GTK_IS_SPIN_BUTTON (spin_button));

  value = gtk_spin_button_get_value (spin_button);

  if (g_variant_type_equal (self->type, G_VARIANT_TYPE_DOUBLE))
    variant = g_variant_new_double (value);
  else if (g_variant_type_equal (self->type, G_VARIANT_TYPE_INT16))
    variant = g_variant_new_int16 (value);
  else if (g_variant_type_equal (self->type, G_VARIANT_TYPE_UINT16))
    variant = g_variant_new_uint16 (value);
  else if (g_variant_type_equal (self->type, G_VARIANT_TYPE_INT32))
    variant = g_variant_new_int32 (value);
  else if (g_variant_type_equal (self->type, G_VARIANT_TYPE_UINT32))
    variant = g_variant_new_uint32 (value);
  else if (g_variant_type_equal (self->type, G_VARIANT_TYPE_INT64))
    variant = g_variant_new_int64 (value);
  else if (g_variant_type_equal (self->type, G_VARIANT_TYPE_UINT64))
    variant = g_variant_new_uint64 (value);
  else
    g_return_if_reached ();

  g_variant_ref_sink (variant);
  g_settings_set_value (self->settings, self->key, variant);
  g_clear_pointer (&variant, g_variant_unref);
}
コード例 #2
0
ファイル: gsettings-mapping.c プロジェクト: Andais/glib
static GVariant *
g_settings_set_mapping_float (const GValue       *value,
                              const GVariantType *expected_type)
{
  GVariant *variant = NULL;
  gdouble d;
  gint64 l;

  if (G_VALUE_HOLDS_DOUBLE (value))
    d = g_value_get_double (value);
  else
    return NULL;

  l = (gint64) d;
  if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_INT16))
    {
      if (G_MININT16 <= l && l <= G_MAXINT16)
        variant = g_variant_new_int16 ((gint16) l);
    }
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_UINT16))
    {
      if (0 <= l && l <= G_MAXUINT16)
        variant = g_variant_new_uint16 ((guint16) l);
    }
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_INT32))
    {
      if (G_MININT32 <= l && l <= G_MAXINT32)
        variant = g_variant_new_int32 ((gint) l);
    }
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_UINT32))
    {
      if (0 <= l && l <= G_MAXUINT32)
        variant = g_variant_new_uint32 ((guint) l);
    }
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_INT64))
    {
      if (G_MININT64 <= l && l <= G_MAXINT64)
        variant = g_variant_new_int64 ((gint64) l);
    }
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_UINT64))
    {
      if (0 <= l && l <= G_MAXUINT64)
        variant = g_variant_new_uint64 ((guint64) l);
    }
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_HANDLE))
    {
      if (0 <= l && l <= G_MAXUINT32)
        variant = g_variant_new_handle ((guint) l);
    }
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_DOUBLE))
    variant = g_variant_new_double ((gdouble) d);

  return variant;
}
コード例 #3
0
ファイル: gsettings-mapping.c プロジェクト: Andais/glib
static GVariant *
g_settings_set_mapping_unsigned_int (const GValue       *value,
                                     const GVariantType *expected_type)
{
  GVariant *variant = NULL;
  guint64 u;

  if (G_VALUE_HOLDS_UINT (value))
    u = g_value_get_uint (value);
  else if (G_VALUE_HOLDS_UINT64 (value))
    u = g_value_get_uint64 (value);
  else
    return NULL;

  if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_INT16))
    {
      if (u <= G_MAXINT16)
        variant = g_variant_new_int16 ((gint16) u);
    }
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_UINT16))
    {
      if (u <= G_MAXUINT16)
        variant = g_variant_new_uint16 ((guint16) u);
    }
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_INT32))
    {
      if (u <= G_MAXINT32)
        variant = g_variant_new_int32 ((gint) u);
    }
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_UINT32))
    {
      if (u <= G_MAXUINT32)
        variant = g_variant_new_uint32 ((guint) u);
    }
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_INT64))
    {
      if (u <= G_MAXINT64)
        variant = g_variant_new_int64 ((gint64) u);
    }
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_UINT64))
    {
      if (u <= G_MAXUINT64)
        variant = g_variant_new_uint64 ((guint64) u);
    }
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_HANDLE))
    {
      if (u <= G_MAXUINT32)
        variant = g_variant_new_handle ((guint) u);
    }
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_DOUBLE))
    variant = g_variant_new_double ((gdouble) u);

  return variant;
}
コード例 #4
0
ファイル: dbus_js_convert.c プロジェクト: CannedFish/dde
GVariant* js_to_dbus(JSContextRef ctx, const JSValueRef jsvalue, const GVariantType* sig, JSValueRef *exception)
{
    if (g_variant_type_is_array(sig)) {
        GVariantBuilder builder;
        g_variant_builder_init(&builder, sig);
        JSPropertyNameArrayRef array = JSObjectCopyPropertyNames(ctx, (JSObjectRef)jsvalue);

        const GVariantType* child_sig = g_variant_type_element(sig);

        if (g_variant_type_is_dict_entry(child_sig)) {

            const GVariantType* key_sig = g_variant_type_first(child_sig);
            const GVariantType* value_sig = g_variant_type_next(key_sig);
            for (size_t i=0; i < JSPropertyNameArrayGetCount(array); i++) {
                if (filter_function_child(ctx, jsvalue, i)) continue;

                g_variant_builder_open(&builder, child_sig);
                JSValueRef key = JSValueMakeString(ctx, JSPropertyNameArrayGetNameAtIndex(array, i));
                JSValueRef value = JSObjectGetPropertyAtIndex(ctx, (JSObjectRef)jsvalue, i, NULL);
                g_variant_builder_add_value(&builder, js_to_dbus(ctx, key, key_sig, exception));
                g_variant_builder_add_value(&builder, js_to_dbus(ctx, value, value_sig, exception));
                g_variant_builder_close(&builder);
            }
            return g_variant_builder_end(&builder);

	} else {
	    GVariantBuilder builder;
	    g_variant_builder_init(&builder, sig);
	    JSPropertyNameArrayRef array = JSObjectCopyPropertyNames(ctx, (JSObjectRef)jsvalue);
	    const GVariantType* child_sig = g_variant_type_element(sig);
	    for (size_t i=0; i < JSPropertyNameArrayGetCount(array); i++) {
		if (filter_array_child(ctx, array, i)) continue;
		g_variant_builder_add_value(&builder, js_to_dbus(ctx, JSObjectGetPropertyAtIndex(ctx, (JSObjectRef)jsvalue, i, NULL), child_sig, exception));
	    }
	    JSPropertyNameArrayRelease(array);
	    return g_variant_builder_end(&builder);
	}
    } else if (g_variant_type_is_tuple(sig)) {
	    GVariantBuilder builder;
	    g_variant_builder_init(&builder, sig);
	    JSPropertyNameArrayRef array = JSObjectCopyPropertyNames(ctx, (JSObjectRef)jsvalue);
	    const GVariantType* current_sig = g_variant_type_first(sig);
            for (size_t i=0; i < JSPropertyNameArrayGetCount(array); i++) {
                if (filter_array_child(ctx, array, i)) continue;
                g_variant_builder_add_value(&builder, js_to_dbus(ctx, JSObjectGetPropertyAtIndex(ctx, (JSObjectRef)jsvalue, i, NULL), current_sig, exception));
		current_sig = g_variant_type_next(current_sig);
            }
            JSPropertyNameArrayRelease(array);
            return g_variant_builder_end(&builder);
    } else {
        switch (g_variant_type_peek_string(sig)[0]) {
            case 'y':
                return g_variant_new_byte(JSValueToNumber(ctx, jsvalue, exception));
            case 'n':
                return g_variant_new_int16(JSValueToNumber(ctx, jsvalue, exception));
            case 'q':
                return g_variant_new_uint16(JSValueToNumber(ctx, jsvalue, exception));
            case 'i':
                return g_variant_new_int32(JSValueToNumber(ctx, jsvalue, exception));
            case 'u':
                return g_variant_new_uint32(JSValueToNumber(ctx, jsvalue, exception));
            case 'x':
                return g_variant_new_int64(JSValueToNumber(ctx, jsvalue, exception));
            case 't':
                return g_variant_new_uint64(JSValueToNumber(ctx, jsvalue, exception));
            case 'd':
                return g_variant_new_double(JSValueToNumber(ctx, jsvalue, exception));
            case 'h':
                return g_variant_new_handle(JSValueToNumber(ctx, jsvalue, exception));
            case 'b':
                return g_variant_new_boolean(JSValueToBoolean(ctx, jsvalue));
            case 's':
                {
                    char* v = jsvalue_to_cstr(ctx, jsvalue);
                    GVariant* r = g_variant_new_string(v);
                    g_free(v);
                    return r;
                }
            case 'v':
                {
                    //TODO:
                    /*g_variant_new_variant()*/
                    g_assert_not_reached();
                }
        }
    }
    g_assert_not_reached();
}
コード例 #5
0
ファイル: gconfsettingsbackend.c プロジェクト: BARGAN/gconf
static GVariant *
gconf_settings_backend_simple_gconf_value_type_to_gvariant (GConfValue         *gconf_value,
                                                            const GVariantType *expected_type)
{
  /* Note: it's guaranteed that the types are compatible */
  GVariant *variant = NULL;

  if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_BOOLEAN))
    variant = g_variant_new_boolean (gconf_value_get_bool (gconf_value));
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_BYTE))
    {
      int value = gconf_value_get_int (gconf_value);
      if (value < 0 || value > 255)
        return NULL;
      variant = g_variant_new_byte (value);
    }
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_INT16))
    {
      int value = gconf_value_get_int (gconf_value);
      if (value < G_MINSHORT || value > G_MAXSHORT)
        return NULL;
      variant = g_variant_new_int16 (value);
    }
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_UINT16))
    {
      int value = gconf_value_get_int (gconf_value);
      if (value < 0 || value > G_MAXUSHORT)
        return NULL;
      variant = g_variant_new_uint16 (value);
    }
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_INT32))
    variant = g_variant_new_int32 (gconf_value_get_int (gconf_value));
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_UINT32))
    {
      int value = gconf_value_get_int (gconf_value);
      if (value < 0)
        return NULL;
      variant = g_variant_new_uint32 (value);
    }
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_INT64))
    variant = g_variant_new_int64 ((gint64) gconf_value_get_int (gconf_value));
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_UINT64))
    {
      int value = gconf_value_get_int (gconf_value);
      if (value < 0)
        return NULL;
      variant = g_variant_new_uint64 (value);
    }
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_HANDLE))
    {
      int value = gconf_value_get_int (gconf_value);
      if (value < 0)
        return NULL;
      variant = g_variant_new_handle (value);
    }
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_DOUBLE))
    variant = g_variant_new_double (gconf_value_get_float (gconf_value));
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_STRING))
    variant = g_variant_new_string (gconf_value_get_string (gconf_value));
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_OBJECT_PATH))
    variant = g_variant_new_object_path (gconf_value_get_string (gconf_value));
  else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_SIGNATURE))
    variant = g_variant_new_signature (gconf_value_get_string (gconf_value));

  return variant;
}
コード例 #6
0
END_TEST

START_TEST (test_credentials_database)
{
    GSignondConfig *config = NULL;
    guint32 identity_id = 5;
    GSignondIdentityInfo *identity = NULL, *identity2= NULL;
    GSignondIdentityInfoList *identities = NULL;
    GSignondSecurityContext *ctx1 = NULL;
    GList *methods = NULL, *reflist = NULL;
    GSignondSecurityContextList *acl = NULL ;
    GSignondSecurityContext *owner = NULL;
    GSignondDbCredentialsDatabase *credentials_db = NULL;
    GSignondSecretStorage *storage =NULL;
    GHashTable *data = NULL;
    GHashTable *data2 = NULL;
    Data input;
    GSignondDictionary *cap_filter = NULL;
    GSignondDictionary *type_filter = NULL;
    GSignondDictionary *cap_type_filter = NULL;
    GSignondDictionary *no_cap_filter = NULL;

    config = gsignond_config_new ();
    gsignond_config_set_string (config, GSIGNOND_CONFIG_GENERAL_SECURE_DIR, "/tmp/gsignond");
    storage = g_object_new (GSIGNOND_TYPE_SECRET_STORAGE,
            "config", config, NULL);
    g_object_unref(config);
    credentials_db = gsignond_db_credentials_database_new (
            config, storage);
    g_object_unref (storage);
    fail_if (credentials_db == NULL);

    fail_unless (gsignond_db_credentials_database_open_secret_storage (
            credentials_db) == TRUE);

    fail_unless (gsignond_db_credentials_database_clear (
            credentials_db) == TRUE);

    identity = _get_filled_identity_info ();

    /*identity load/update*/
    identity_id = gsignond_db_credentials_database_update_identity (
            credentials_db, identity);
    fail_unless (identity_id != 0);
    gsignond_identity_info_set_id (identity, identity_id);

    fail_unless (gsignond_db_credentials_database_load_identity (
                credentials_db, 555, FALSE) == NULL);
    identity2 = gsignond_db_credentials_database_load_identity (
            credentials_db, identity_id, FALSE);
    fail_if (identity2 == NULL);
    gsignond_identity_info_unref (identity2);

    identity2 = gsignond_db_credentials_database_load_identity (
            credentials_db, identity_id, TRUE);
    fail_if (identity2 == NULL);

    fail_unless (g_strcmp0 (gsignond_identity_info_get_username (
            identity2), "username1") == 0);
    fail_unless (g_strcmp0 (gsignond_identity_info_get_secret (
            identity2), "secret1") == 0);
    gsignond_identity_info_unref (identity2);

    fail_unless (gsignond_db_credentials_database_check_secret (
            credentials_db, identity_id, "username2", "secret1") == FALSE);

    fail_unless (gsignond_db_credentials_database_check_secret (
            credentials_db, identity_id, "username1", "secret2") == FALSE);

    fail_unless (gsignond_db_credentials_database_check_secret (
            credentials_db, 0, "username1", "secret2") == FALSE);

    fail_unless (gsignond_db_credentials_database_check_secret (
            credentials_db, identity_id, "username1", "secret1") == TRUE);

    ctx1 = gsignond_security_context_new_from_values ("sysctx1", "appctx1");
    methods = gsignond_db_credentials_database_get_methods (credentials_db,
                identity_id, ctx1);
    fail_if (methods == NULL);
    g_list_free_full (methods, g_free);

    /* add data to store */
    data = g_hash_table_new_full ((GHashFunc)g_str_hash,
            (GEqualFunc)g_str_equal,
            (GDestroyNotify)NULL,
            (GDestroyNotify)g_variant_unref);
    g_hash_table_insert (data,"key1",g_variant_new_string ("string_value"));
    g_hash_table_insert (data,"key2",g_variant_new_double (12223.4223));
    g_hash_table_insert (data,"key3",g_variant_new_uint16(20));
    g_hash_table_insert (data,"key4",g_variant_new("^ay", "byte_value"));

    fail_unless (gsignond_db_credentials_database_update_data (
            credentials_db, 0, "method1", data) == FALSE);

    fail_unless (gsignond_db_credentials_database_update_data (
            credentials_db, identity_id, "method1", data) == TRUE);

    fail_unless (gsignond_db_credentials_database_update_data (
            credentials_db, identity_id, "method1", data) == TRUE);

    fail_unless (gsignond_db_credentials_database_load_data (
            credentials_db, 0, "method1") == NULL);
    fail_unless (gsignond_db_credentials_database_load_data (
            credentials_db, identity_id, "method2") == NULL);

    data2 = gsignond_db_credentials_database_load_data (credentials_db,
            identity_id, "method1");
    fail_if (data2 == NULL);
    input.table = data;
    input.status = 1;
    g_hash_table_foreach (data2, (GHFunc)_compare_key_value, &input);
    fail_if (input.status != 1);
    gsignond_dictionary_unref(data2);
    g_hash_table_unref(data);

    fail_unless (gsignond_db_credentials_database_remove_data (
            credentials_db, 0, "method1") == FALSE);

    fail_unless (gsignond_db_credentials_database_remove_data (
            credentials_db, identity_id, "method1") == TRUE);

    /*references*/
    fail_unless (gsignond_db_credentials_database_insert_reference (
            credentials_db, identity_id, ctx1, "reference1") == TRUE);

    reflist = gsignond_db_credentials_database_get_references (credentials_db,
                identity_id, ctx1);
    fail_if (reflist == NULL);
    fail_unless (g_list_length (reflist) == 1);
    g_list_free_full (reflist, g_free);

    fail_unless (gsignond_db_credentials_database_remove_reference (
            credentials_db, identity_id, ctx1, "reference2") == FALSE);

    fail_unless (gsignond_db_credentials_database_remove_reference (
            credentials_db, identity_id, ctx1, "reference1") == TRUE);
    gsignond_security_context_free (ctx1);

    acl = gsignond_db_credentials_database_get_accesscontrol_list (
            credentials_db, identity_id);
    fail_if (acl == NULL);
    gsignond_security_context_list_free (acl);

    owner = gsignond_db_credentials_database_get_owner (
            credentials_db, identity_id);
    fail_if (owner == NULL);
    gsignond_security_context_free (owner);

    owner = gsignond_db_credentials_database_get_identity_owner (
            credentials_db, identity_id);
    fail_if (owner == NULL);
    gsignond_security_context_free (owner);

    /* load_identities : matched with caption and security context */
    cap_filter = gsignond_dictionary_new ();
    GSignondSecurityContext *ctx =
    		gsignond_security_context_new_from_values("sysctx1", "appctx1");
    gsignond_dictionary_set_string (cap_filter, "Caption", "cap");
    gsignond_dictionary_set(cap_filter, "Owner",
    		 gsignond_security_context_to_variant(ctx));
    gsignond_security_context_free (ctx);
    identities = gsignond_db_credentials_database_load_identities (
            credentials_db, cap_filter);
    gsignond_dictionary_unref (cap_filter);

    fail_if (identities == NULL);
    fail_unless (g_list_length (identities) == 1);
    gsignond_identity_info_list_free (identities);

    /* load_identities: matched with type */
    type_filter = gsignond_dictionary_new();
    gsignond_dictionary_set_int32 (type_filter, "Type", 456);
    identities = gsignond_db_credentials_database_load_identities (
            credentials_db, type_filter);
    gsignond_dictionary_unref (type_filter);

    fail_if (identities == NULL);
    fail_unless (g_list_length (identities) == 1);
    gsignond_identity_info_list_free (identities);

    /* load_identities: matched with type and caption */
    cap_type_filter = gsignond_dictionary_new();
    gsignond_dictionary_set_int32 (cap_type_filter, "Type", 456);
    gsignond_dictionary_set_string (cap_type_filter, "Caption", "CAP");
    identities = gsignond_db_credentials_database_load_identities (
            credentials_db, cap_type_filter);
    gsignond_dictionary_unref (cap_type_filter);

    fail_if (identities == NULL);
    fail_unless (g_list_length (identities) == 1);
    gsignond_identity_info_list_free (identities);

    /* Negative load_identities query */
    no_cap_filter = gsignond_dictionary_new();
    gsignond_dictionary_set_string (no_cap_filter, "Caption", "non_existing");

    identities = gsignond_db_credentials_database_load_identities (
            credentials_db, no_cap_filter);
    gsignond_dictionary_unref (no_cap_filter);
    fail_unless (identities == NULL);

    fail_unless (gsignond_db_credentials_database_remove_identity (
            credentials_db, identity_id) == TRUE);
    gsignond_identity_info_unref (identity);

    g_object_unref(credentials_db);
}
コード例 #7
0
END_TEST

START_TEST (test_secret_storage)
{
    GSignondSecretStorage *storage = NULL;
    GSignondConfig *config = NULL;
    GSignondCredentials *creds = NULL;
    guint32 id = 1, method = 2;
    GHashTable *data = NULL;
    GHashTable *data2 = NULL;
    Data input;
    const gchar *dir = NULL;

    config = gsignond_config_new ();
    gsignond_config_set_string (config, GSIGNOND_CONFIG_GENERAL_SECURE_DIR, "/tmp/gsignond");
    
    /* Secret Storage */
    storage = g_object_new (GSIGNOND_TYPE_SECRET_STORAGE,
            "config", config, NULL);
    g_object_unref(config);
    fail_if (storage == NULL);

    dir = gsignond_config_get_string (config,
            GSIGNOND_CONFIG_GENERAL_SECURE_DIR);
    if (!dir) {
        dir = g_get_user_data_dir ();
    }
    g_mkdir_with_parents (dir, S_IRWXU);

    fail_unless (gsignond_secret_storage_get_last_error (storage) == NULL);
    fail_unless (gsignond_secret_storage_clear_db (storage) == FALSE);
    fail_unless (gsignond_secret_storage_is_open_db (storage) == FALSE);
    fail_unless (gsignond_secret_storage_load_credentials (storage, 1) == NULL);
    fail_unless (gsignond_secret_storage_update_credentials (
            storage, NULL) == FALSE);
    fail_unless (gsignond_secret_storage_remove_credentials (
            storage, 1) == FALSE);
    fail_unless (gsignond_secret_storage_load_data (
            storage, 1, 2) == NULL);
    fail_unless (gsignond_secret_storage_update_data (
            storage, 1, 2, NULL) == FALSE);
    fail_unless (gsignond_secret_storage_remove_data (
            storage, 1, 2) == FALSE);

    fail_unless (gsignond_secret_storage_open_db (storage) == TRUE);
    /* don't open the db again if its already open */
    fail_unless (gsignond_secret_storage_open_db (storage) == TRUE);

    creds = gsignond_credentials_new ();
    fail_if (creds == NULL);

    fail_unless (gsignond_credentials_set_data (
            creds, id, "user 1", "pass 1") == TRUE);

    fail_unless (gsignond_secret_storage_update_credentials (
            storage, creds) == TRUE);
    g_object_unref (creds); creds = NULL;

    creds = gsignond_secret_storage_load_credentials (storage, id);
    fail_if (creds == NULL);

    fail_unless (gsignond_secret_storage_check_credentials (
            storage, creds) == TRUE);

    gsignond_credentials_set_id (creds, 3);
    fail_unless (gsignond_secret_storage_check_credentials (
            storage, creds) == FALSE);
    g_object_unref (creds);

    /* remove the added credentials */
    fail_unless (gsignond_secret_storage_remove_credentials (
            storage, id) == TRUE);

    /* add data to store */
    data = g_hash_table_new_full ((GHashFunc)g_str_hash,
            (GEqualFunc)g_str_equal,
            (GDestroyNotify)NULL,
            (GDestroyNotify)g_variant_unref);
    fail_if (data == NULL);

    g_hash_table_insert (data,"key1",g_variant_new_string ("string_value"));
    g_hash_table_insert (data,"key2",g_variant_new_double (12223.4223));
    g_hash_table_insert (data,"key3",g_variant_new_uint16(20));
    g_hash_table_insert (data,"key4",g_variant_new("^ay", "byte_value"));

    fail_unless (gsignond_secret_storage_update_data (
            storage, id, method, data) == TRUE);
    data2 = gsignond_secret_storage_load_data (storage, id, method);
    fail_if (data2 == NULL);
    input.table = data;
    input.status = 1;
    g_hash_table_foreach (data2, (GHFunc)_compare_key_value, &input);
    fail_if (input.status != 1);

    gsignond_dictionary_unref(data2);
    g_hash_table_unref(data);

    fail_unless (gsignond_secret_storage_remove_data (
            storage, id, method) == TRUE);
    fail_unless (gsignond_secret_storage_clear_db (storage) == TRUE);
    fail_unless (gsignond_secret_storage_close_db (storage) == TRUE);
    g_object_unref(storage);
}
コード例 #8
0
Variant::Variant(uint16_t value)
  : Variant(g_variant_new_uint16(value))
{}
コード例 #9
0
static GVariant *
dconf_dbus_to_gv (DBusMessageIter  *iter,
                  GError          **error)
{
  gint arg_type;

  arg_type = dbus_message_iter_get_arg_type (iter);

  switch (dbus_message_iter_get_arg_type (iter))
    {
     case DBUS_TYPE_BOOLEAN:
      {
        dbus_bool_t value;
        dbus_message_iter_get_basic (iter, &value);
        return g_variant_new_boolean (value);
      }

     case DBUS_TYPE_BYTE:
      {
        guchar value;
        dbus_message_iter_get_basic (iter, &value);
        return g_variant_new_byte (value);
      }

     case DBUS_TYPE_INT16:
      {
        gint16 value;
        dbus_message_iter_get_basic (iter, &value);
        return g_variant_new_int16 (value);
      }

     case DBUS_TYPE_UINT16:
      {
        guint16 value;
        dbus_message_iter_get_basic (iter, &value);
        return g_variant_new_uint16 (value);
      }

     case DBUS_TYPE_INT32:
      {
        gint32 value;
        dbus_message_iter_get_basic (iter, &value);
        return g_variant_new_int32 (value);
      }

     case DBUS_TYPE_UINT32:
      {
        guint32 value;
        dbus_message_iter_get_basic (iter, &value);
        return g_variant_new_uint32 (value);
      }

     case DBUS_TYPE_INT64:
      {
        gint64 value;
        dbus_message_iter_get_basic (iter, &value);
        return g_variant_new_int64 (value);
      }

     case DBUS_TYPE_UINT64:
      {
        guint64 value;
        dbus_message_iter_get_basic (iter, &value);
        return g_variant_new_uint64 (value);
      }

     case DBUS_TYPE_DOUBLE:
      {
        gdouble value;
        dbus_message_iter_get_basic (iter, &value);
        return g_variant_new_double (value);
      }

     case DBUS_TYPE_STRING:
      {
       const gchar *value;
       dbus_message_iter_get_basic (iter, &value);
       return g_variant_new_string (value);
      }

     case DBUS_TYPE_OBJECT_PATH:
      {
       const gchar *value;
       dbus_message_iter_get_basic (iter, &value);
       return g_variant_new_object_path (value);
      }

     case DBUS_TYPE_SIGNATURE:
      {
       const gchar *value;
       dbus_message_iter_get_basic (iter, &value);
       return g_variant_new_signature (value);
      }

     case DBUS_TYPE_VARIANT:
       {
        GVariantBuilder *builder;
        GVariantClass class;
        DBusMessageIter sub;
        char *type;
        GVariant *val;

        dbus_message_iter_recurse (iter, &sub);
        class = dbus_message_iter_get_arg_type (iter);
        type = dbus_message_iter_get_signature (&sub);
        builder = g_variant_builder_new (G_VARIANT_TYPE_VARIANT);
        dbus_free (type);

        while (dbus_message_iter_get_arg_type (&sub))
          {
            val = dconf_dbus_to_gv (&sub, error);
            if (val == NULL)
              {
                g_variant_builder_cancel (builder);
                goto fail;
              }
            g_variant_builder_add_value (builder, val);
            dbus_message_iter_next (&sub);
          }

        return g_variant_builder_end (builder);
       }

     case DBUS_TYPE_ARRAY:
     case DBUS_TYPE_STRUCT:
     case DBUS_TYPE_DICT_ENTRY:
      {
        GVariantBuilder *builder;
        GVariantClass class;
        DBusMessageIter sub;
        char *type;
        GVariant *val;

        dbus_message_iter_recurse (iter, &sub);
        class = dbus_message_iter_get_arg_type (iter);
        type = dbus_message_iter_get_signature (iter);
        builder = g_variant_builder_new (G_VARIANT_TYPE (type));
        dbus_free (type);

        while (dbus_message_iter_get_arg_type (&sub))
          {
            val = dconf_dbus_to_gv (&sub, error);
            if (val == NULL)
              {
                g_variant_builder_cancel (builder);
                goto fail;
              }
            g_variant_builder_add_value (builder, val);
            dbus_message_iter_next (&sub);
          }

        return g_variant_builder_end (builder);
      }

     default:
       g_set_error (error,
                    G_DBUS_ERROR,
                    G_DBUS_ERROR_CONVERSION_FAILED,
                    _("Error serializing D-Bus message to GVariant. Unsupported arg type `%c' (%d)"),
                    arg_type,
                    arg_type);
      goto fail;
    }

  g_assert_not_reached ();

 fail:
  return NULL;
}
コード例 #10
0
ファイル: gdbusutils.c プロジェクト: 183amir/glib
/**
 * g_dbus_gvalue_to_gvariant:
 * @gvalue: A #GValue to convert to a #GVariant
 * @type: A #GVariantType
 *
 * Converts a #GValue to a #GVariant of the type indicated by the @type
 * parameter.
 *
 * The conversion is using the following rules:
 *
 * - #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 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 @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 *type)
{
  GVariant *ret;
  const gchar *s;
  const gchar * const *as;
  const gchar *empty_strv[1] = {NULL};

  g_return_val_if_fail (gvalue != NULL, NULL);
  g_return_val_if_fail (type != NULL, NULL);

  ret = NULL;

  /* @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)
    {
      ret = g_value_dup_variant (gvalue);
    }
  else
    {
      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)));
          break;

        case G_VARIANT_CLASS_BYTE:
          ret = g_variant_ref_sink (g_variant_new_byte (g_value_get_uchar (gvalue)));
          break;

        case G_VARIANT_CLASS_INT16:
          ret = g_variant_ref_sink (g_variant_new_int16 (g_value_get_int (gvalue)));
          break;

        case G_VARIANT_CLASS_UINT16:
          ret = g_variant_ref_sink (g_variant_new_uint16 (g_value_get_uint (gvalue)));
          break;

        case G_VARIANT_CLASS_INT32:
          ret = g_variant_ref_sink (g_variant_new_int32 (g_value_get_int (gvalue)));
          break;

        case G_VARIANT_CLASS_UINT32:
          ret = g_variant_ref_sink (g_variant_new_uint32 (g_value_get_uint (gvalue)));
          break;

        case G_VARIANT_CLASS_INT64:
          ret = g_variant_ref_sink (g_variant_new_int64 (g_value_get_int64 (gvalue)));
          break;

        case G_VARIANT_CLASS_UINT64:
          ret = g_variant_ref_sink (g_variant_new_uint64 (g_value_get_uint64 (gvalue)));
          break;

        case G_VARIANT_CLASS_DOUBLE:
          ret = g_variant_ref_sink (g_variant_new_double (g_value_get_double (gvalue)));
          break;

        case G_VARIANT_CLASS_STRING:
          s = g_value_get_string (gvalue);
          if (s == NULL)
            s = "";
          ret = g_variant_ref_sink (g_variant_new_string (s));
          break;

        case G_VARIANT_CLASS_OBJECT_PATH:
          s = g_value_get_string (gvalue);
          if (s == NULL)
            s = "/";
          ret = g_variant_ref_sink (g_variant_new_object_path (s));
          break;

        case G_VARIANT_CLASS_SIGNATURE:
          s = g_value_get_string (gvalue);
          if (s == NULL)
            s = "";
          ret = g_variant_ref_sink (g_variant_new_signature (s));
          break;

        case G_VARIANT_CLASS_ARRAY:
          switch (g_variant_type_peek_string (type)[1])
            {
            case G_VARIANT_CLASS_BYTE:
              s = g_value_get_string (gvalue);
              if (s == NULL)
                s = "";
              ret = g_variant_ref_sink (g_variant_new_bytestring (s));
              break;

            case G_VARIANT_CLASS_STRING:
              as = g_value_get_boxed (gvalue);
              if (as == NULL)
                as = empty_strv;
              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 (type)[2])
                {
                case G_VARIANT_CLASS_BYTE:
                  as = g_value_get_boxed (gvalue);
                  if (as == NULL)
                    as = empty_strv;
                  ret = g_variant_ref_sink (g_variant_new_bytestring_array (as, -1));
                  break;

                default:
                  ret = g_value_dup_variant (gvalue);
                  break;
                }
              break;

            default:
              ret = g_value_dup_variant (gvalue);
              break;
            }
          break;

        case G_VARIANT_CLASS_HANDLE:
        case G_VARIANT_CLASS_VARIANT:
        case G_VARIANT_CLASS_MAYBE:
        case G_VARIANT_CLASS_TUPLE:
        case G_VARIANT_CLASS_DICT_ENTRY:
          ret = g_value_dup_variant (gvalue);
          break;
        }
    }

  /* Could be that the GValue is holding a NULL GVariant - in that case,
   * we return an "empty" GVariant instead of a NULL GVariant
   */
  if (ret == NULL)
    {
      GVariant *untrusted_empty;
      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);
    }

  g_assert (!g_variant_is_floating (ret));

  return ret;
}
コード例 #11
0
ファイル: main.c プロジェクト: contactless/wb-mqtt-co2mon
static void
device_loop(co2mon_device dev)
{
    co2mon_magic_table_t magic_table = {0};
    co2mon_data_t result;

    if (!co2mon_send_magic_table(dev, magic_table))
    {
        fprintf(stderr, "Unable to send magic table to CO2 device\n");
        return;
    }

    printf("Sending values to D-Bus...\n");

    while (1)
    {
        int r = co2mon_read_data(dev, magic_table, result);
        if (r == LIBUSB_ERROR_NO_DEVICE)
        {
            fprintf(stderr, "Device has been disconnected\n");
            break;
        }
        else if (r <= 0)
        {
            continue;
        }

        if (result[4] != 0x0d)
        {
            fprintf(stderr, "Unexpected data from device (data[4] = %02hhx, await 0x0d)\n", result[4]);
            continue;
        }

        unsigned char r0, r1, r2, r3, checksum;
        r0 = result[0];
        r1 = result[1];
        r2 = result[2];
        r3 = result[3];
        checksum = r0 + r1 + r2;
        if (checksum != r3)
        {
            fprintf(stderr, "checksum error (%02hhx, await %02hhx)\n", checksum, r3);
            continue;
        }

        uint16_t w = (result[1] << 8) + result[2];
        g_rw_lock_writer_lock(&co2mon_data_lock);
        co2mon_data[r0] = w;
        g_rw_lock_writer_unlock(&co2mon_data_lock);

        const char *name = "UNKNOWN";
        GVariant *value = NULL;
        switch (r0)
        {
        case CODE_TEMP:
            name = "TEMP";
            value = g_variant_new_double(decode_temperature(w));
            break;
        case CODE_CO2:
            name = "CO2";
            value = g_variant_new_uint16(w);
            break;
        default:
            value = g_variant_new_uint16(w);
        }

        GVariant *params = g_variant_new("(yqsv)", r0, w, name, value);

        GError *error = NULL;
        gboolean ret = g_dbus_connection_emit_signal(
            connection,
            NULL, /* destination bus name */
            "/io/github/dmage/CO2Mon",
            "io.github.dmage.CO2Mon",
            "NewValue",
            params,
            &error);
        if (ret != TRUE)
        {
            fprintf(stderr, "Unable to emit D-Bus signal: %s\n", error->message);
            g_error_free(error);
        }
    }
}
コード例 #12
0
ファイル: cockpitdbusjson.c プロジェクト: Legun/cockpit
static GVariant *
parse_json (JsonNode *node,
            const GVariantType *type,
            GError **error)
{
  const GVariantType *element_type;
  const gchar *str;

  if (!g_variant_type_is_definite (type))
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
                   "Indefinite type '%.*s' is not supported",
                   (int)g_variant_type_get_string_length (type),
                   g_variant_type_peek_string (type));
      return NULL;
    }

  if (g_variant_type_is_basic (type))
    {
      if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_BOOLEAN, error))
            return g_variant_new_boolean (json_node_get_boolean (node));
        }
      else if (g_variant_type_equal (type, G_VARIANT_TYPE_BYTE))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_INT64, error))
            return g_variant_new_byte (json_node_get_int (node));
        }
      else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT16))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_INT64, error))
            return g_variant_new_int16 (json_node_get_int (node));
        }
      else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT16))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_INT64, error))
            return g_variant_new_uint16 (json_node_get_int (node));
        }
      else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT32))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_INT64, error))
            return g_variant_new_int32 (json_node_get_int (node));
        }
      else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT32))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_INT64, error))
            return g_variant_new_uint32 (json_node_get_int (node));
        }
      else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT64))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_INT64, error))
            return g_variant_new_int64 (json_node_get_int (node));
        }
      else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT64))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_INT64, error))
            return g_variant_new_uint64 (json_node_get_int (node));
        }
      else if (g_variant_type_equal (type, G_VARIANT_TYPE_DOUBLE))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_INT64, NULL))
            return g_variant_new_double (json_node_get_int (node));
          else if (check_type (node, JSON_NODE_VALUE, G_TYPE_DOUBLE, error))
            return g_variant_new_double (json_node_get_double (node));
        }
      else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_STRING, error))
            return g_variant_new_string (json_node_get_string (node));
        }
      else if (g_variant_type_equal (type, G_VARIANT_TYPE_OBJECT_PATH))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_STRING, error))
            {
              str = json_node_get_string (node);
              if (g_variant_is_object_path (str))
                return g_variant_new_object_path (str);
              else
                {
                  g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
                               "Invalid object path '%s'", str);
                  return NULL;
                }
            }
        }
      else if (g_variant_type_equal (type, G_VARIANT_TYPE_SIGNATURE))
        {
          if (check_type (node, JSON_NODE_VALUE, G_TYPE_STRING, error))
            {
              str = json_node_get_string (node);
              if (g_variant_is_signature (str))
                return g_variant_new_signature (str);
              else
                {
                  g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
                               "Invalid signature '%s'", str);
                  return NULL;
                }
            }
        }
      else
        {
          parse_not_supported (type, error);
        }
    }
  else if (g_variant_type_is_variant (type))
    {
      return parse_json_variant (node, error);
    }
  else if (g_variant_type_is_array (type))
    {
      element_type = g_variant_type_element (type);
      if (g_variant_type_is_dict_entry (element_type))
        return parse_json_dictionary (node, element_type, error);
      else
        return parse_json_array (node, element_type, error);
    }
  else if (g_variant_type_is_tuple (type))
    {
      return parse_json_tuple (node, g_variant_type_first (type), error);
    }
  else
    {
      parse_not_supported (type, error);
    }

  return NULL;
}