[libgda/LIBGDA_5.0] Added gda_ldap_entry_get_attributes_list() and gda_ldap_attributes_list_free()



commit 2aad7b2e3f9de134b43650a4b5c8d97f39724e93
Author: Vivien Malerba <malerba gnome-db org>
Date:   Sun Feb 5 22:23:52 2012 +0100

    Added gda_ldap_entry_get_attributes_list() and gda_ldap_attributes_list_free()

 doc/C/libgda-sections.txt                   |    3 +
 libgda/gda-data-model-ldap.c                |   30 +++++++++++
 libgda/libgda.symbols                       |    2 +
 libgda/sqlite/virtual/gda-ldap-connection.c |   45 ++++++++++++++++
 libgda/sqlite/virtual/gda-ldap-connection.h |    8 +++
 providers/ldap/gda-ldap-util.c              |   76 +++++++++++++++++++++++++++
 6 files changed, 164 insertions(+), 0 deletions(-)
---
diff --git a/doc/C/libgda-sections.txt b/doc/C/libgda-sections.txt
index 7fa8c40..e44f533 100644
--- a/doc/C/libgda-sections.txt
+++ b/doc/C/libgda-sections.txt
@@ -256,6 +256,9 @@ gda_ldap_dn_split
 gda_ldap_describe_entry
 gda_ldap_entry_free
 gda_ldap_get_entry_children
+GdaLdapAttributeDefinition
+gda_ldap_entry_get_attributes_list
+gda_ldap_attributes_list_free
 <SUBSECTION>
 GdaLdapClassKind
 GdaLdapClass
diff --git a/libgda/gda-data-model-ldap.c b/libgda/gda-data-model-ldap.c
index b117553..3c5ec4b 100644
--- a/libgda/gda-data-model-ldap.c
+++ b/libgda/gda-data-model-ldap.c
@@ -403,3 +403,33 @@ _gda_ldap_get_top_classes (GdaLdapConnection *cnc)
 	
 	return func (cnc);
 }
+
+/*
+ * _gda_ldap_entry_get_attributes_list:
+ * proxy for gda_ldap_entry_get_attributes_list()
+ */
+GSList *
+_gda_ldap_entry_get_attributes_list (GdaLdapConnection *cnc, GdaLdapEntry *entry, GdaLdapAttribute *object_class_attr)
+{
+	g_return_val_if_fail (GDA_IS_LDAP_CONNECTION (cnc), NULL);
+	g_return_val_if_fail (entry || object_class_attr, NULL);
+	if (!object_class_attr) {
+		g_return_val_if_fail (entry->attributes_hash, NULL);
+		object_class_attr = g_hash_table_lookup (entry->attributes_hash, "objectClass");
+		g_return_val_if_fail (object_class_attr, NULL);
+	}
+
+	typedef GSList *(*Func) (GdaLdapConnection *, GdaLdapAttribute *);
+	static Func func = NULL;
+
+	if (!func) {
+		load_ldap_module ();
+		if (!ldap_prov_module)
+			return NULL;
+
+		if (!g_module_symbol (ldap_prov_module, "gdaprov_ldap_get_attributes_list", (void **) &func))
+			return NULL;
+	}
+
+	return func (cnc, object_class_attr);
+}
diff --git a/libgda/libgda.symbols b/libgda/libgda.symbols
index ef8a696..9e8e931 100644
--- a/libgda/libgda.symbols
+++ b/libgda/libgda.symbols
@@ -413,6 +413,7 @@
 	gda_init
 	gda_lang_locale
 #ifdef HAVE_LDAP
+	gda_ldap_attributes_list_free
 	gda_ldap_connection_get_type
 	gda_ldap_get_class_info
 	gda_ldap_get_top_classes
@@ -423,6 +424,7 @@
 	gda_ldap_describe_entry
 	gda_ldap_dn_split
 	gda_ldap_entry_free
+	gda_ldap_entry_get_attributes_list
 	gda_ldap_get_entry_children
 	gda_ldap_is_dn
 #endif
diff --git a/libgda/sqlite/virtual/gda-ldap-connection.c b/libgda/sqlite/virtual/gda-ldap-connection.c
index 855c061..314b98e 100644
--- a/libgda/sqlite/virtual/gda-ldap-connection.c
+++ b/libgda/sqlite/virtual/gda-ldap-connection.c
@@ -1023,3 +1023,48 @@ gda_ldap_get_top_classes (GdaLdapConnection *cnc)
 
 	return _gda_ldap_get_top_classes (cnc);
 }
+
+GSList *_gda_ldap_entry_get_attributes_list (GdaLdapConnection *cnc, GdaLdapEntry *entry, GdaLdapAttribute *object_class_attr);
+/**
+ * gda_ldap_entry_get_attributes_list:
+ * @entry: a #GdaLdapEntry
+ *
+ * Get a list of all the possible attributes which @entry can have. Each possible attribute is represented
+ * by a #GdaLdapAttributeDefinition strunture.
+ *
+ * Returns: (transfer full) (element-type GdaLdapAttributeDefinition): a #GSList of #GdaLdapAttributeDefinition pointers, free the list using gda_ldap_attributes_list_free()
+ *
+ * Since: 5.2.0
+ */
+GSList *
+gda_ldap_entry_get_attributes_list (GdaLdapConnection *cnc, GdaLdapEntry *entry)
+{
+	g_return_val_if_fail (entry, NULL);
+
+	return _gda_ldap_entry_get_attributes_list (cnc, entry, NULL);
+}
+
+/**
+ * gda_ldap_attributes_list_free:
+ * @list: (allow-none): a #GSList of #GdaLdapAttributeDefinition pointers, or %NULL
+ *
+ * Frees the list returned by gda_ldap_entry_get_attributes_list().
+ *
+ * Since: 5.2.0
+ */
+void
+gda_ldap_attributes_list_free (GSList *list)
+{
+	GSList *l;
+	if (!list)
+		return;
+	for (l = list; l; l = l->next) {
+		GdaLdapAttributeDefinition *def;
+		def = (GdaLdapAttributeDefinition*) l->data;
+		if (def) {
+			g_free (def->name);
+			g_free (def);
+		}
+	}
+	g_slist_free (list);
+}
diff --git a/libgda/sqlite/virtual/gda-ldap-connection.h b/libgda/sqlite/virtual/gda-ldap-connection.h
index 202c577..4269163 100644
--- a/libgda/sqlite/virtual/gda-ldap-connection.h
+++ b/libgda/sqlite/virtual/gda-ldap-connection.h
@@ -170,6 +170,14 @@ GdaLdapEntry **gda_ldap_get_entry_children         (GdaLdapConnection *cnc, cons
 gchar        **gda_ldap_dn_split                   (const gchar *dn, gboolean all);
 gboolean       gda_ldap_is_dn                      (const gchar *dn);
 
+typedef struct {
+	gchar   *name;
+	GType    g_type;
+	gboolean required;
+} GdaLdapAttributeDefinition;
+void           gda_ldap_attributes_list_free      (GSList *list);
+GSList        *gda_ldap_entry_get_attributes_list (GdaLdapConnection *cnc, GdaLdapEntry *entry);
+
 
 /**
  * GdaLdapClassKind:
diff --git a/providers/ldap/gda-ldap-util.c b/providers/ldap/gda-ldap-util.c
index 0cdd924..7c66a99 100644
--- a/providers/ldap/gda-ldap-util.c
+++ b/providers/ldap/gda-ldap-util.c
@@ -1445,3 +1445,79 @@ gdaprov_ldap_get_base_dn (GdaLdapConnection *cnc)
 	else
 		return cdata->base_dn;
 }
+
+static gint
+def_cmp_func (GdaLdapAttributeDefinition *def1, GdaLdapAttributeDefinition *def2)
+{
+	return strcmp (def1->name, def2->name);
+}
+
+GSList *
+gdaprov_ldap_get_attributes_list (GdaLdapConnection *cnc, GdaLdapAttribute *object_class_attr)
+{
+	LdapConnectionData *cdata;
+	g_return_val_if_fail (GDA_IS_LDAP_CONNECTION (cnc), NULL);
+	g_return_val_if_fail (object_class_attr, NULL);
+
+	cdata = (LdapConnectionData*) gda_virtual_connection_internal_get_provider_data (GDA_VIRTUAL_CONNECTION (cnc));
+        if (!cdata)
+                return NULL;
+
+	guint i;
+	const gchar *tmp;
+	GHashTable *hash;
+	GSList *retlist = NULL;
+
+	hash = g_hash_table_new (g_str_hash, g_str_equal);
+	for (i = 0; i < object_class_attr->nb_values; i++) {
+		if (G_VALUE_TYPE (object_class_attr->values [i]) != G_TYPE_STRING) {
+			g_warning (_("Unexpected data type '%s' for objectClass attribute!"),
+				   gda_g_type_to_string (G_VALUE_TYPE (object_class_attr->values [i])));
+			continue;
+		}
+		tmp = g_value_get_string (object_class_attr->values [i]);
+		/*g_print ("Class [%s]\n", tmp);*/
+		GdaLdapClass *kl;
+		kl = gdaprov_ldap_get_class_info (cnc, tmp);
+		if (!kl) {
+			g_warning (_("Can't get information about '%s' class"), tmp);
+			continue;
+		}
+		guint j;
+		for (j = 0; j < kl->nb_req_attributes; j++) {
+			/*g_print ("  attr [%s] REQ\n", kl->req_attributes [j]);*/
+			GdaLdapAttributeDefinition *def;
+			LdapAttribute *latt;
+			latt = gda_ldap_get_attr_info (cdata, kl->req_attributes [j]);
+			def = g_hash_table_lookup (hash, kl->req_attributes [j]);
+			if (def)
+				def->required = TRUE;
+			else {
+				def = g_new0 (GdaLdapAttributeDefinition, 1);
+				def->name = g_strdup (kl->req_attributes [j]);
+				def->required = TRUE;
+				def->g_type = latt ? latt->type->gtype : G_TYPE_STRING;
+				g_hash_table_insert (hash, def->name, def);
+				retlist = g_slist_insert_sorted (retlist, def, (GCompareFunc) def_cmp_func);
+			}
+		}
+		for (j = 0; j < kl->nb_opt_attributes; j++) {
+			/*g_print ("  attr [%s] OPT\n", kl->opt_attributes [j]);*/
+			GdaLdapAttributeDefinition *def;
+			LdapAttribute *latt;
+			latt = gda_ldap_get_attr_info (cdata, kl->opt_attributes [j]);
+			def = g_hash_table_lookup (hash, kl->opt_attributes [j]);
+			if (!def) {
+				def = g_new0 (GdaLdapAttributeDefinition, 1);
+				def->name = g_strdup (kl->opt_attributes [j]);
+				def->required = FALSE;
+				def->g_type = latt ? latt->type->gtype : G_TYPE_STRING;
+				g_hash_table_insert (hash, def->name, def);
+				retlist = g_slist_insert_sorted (retlist, def, (GCompareFunc) def_cmp_func);
+			}
+		}
+	}
+	g_hash_table_destroy (hash);
+
+	return retlist;
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]