diff options
author | Marcus Lundblad <ml@dfupdate.se> | 2022-04-24 23:26:08 +0200 |
---|---|---|
committer | Marcus Lundblad <ml@dfupdate.se> | 2022-04-27 22:41:58 +0200 |
commit | e97a3314d245c990d34069b3d89f4e49829d4f71 (patch) | |
tree | 59b907ebe8d4edb47a1aded0e72e1a6e5e6b5732 /lib | |
parent | b1bb0267830e591d5a7564143a4ac5388ca8606b (diff) | |
download | gnome-maps-e97a3314d245c990d34069b3d89f4e49829d4f71.tar.gz |
maps-osm: Add function to parse OSM user details
Adds a function to parse the logged in user name
from the user_details OSM REST API call result.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/maps-osm.c | 47 | ||||
-rw-r--r-- | lib/maps-osm.h | 2 |
2 files changed, 48 insertions, 1 deletions
diff --git a/lib/maps-osm.c b/lib/maps-osm.c index 307a9361..1164001c 100644 --- a/lib/maps-osm.c +++ b/lib/maps-osm.c @@ -169,7 +169,8 @@ get_sub_node (xmlDoc *doc) xmlXPathObject * xpath_obj; xpath_ctx = xmlXPathNewContext (doc); - xpath_obj = xmlXPathEvalExpression ((xmlChar *) "/osm/node|/osm/way|/osm/relation", + xpath_obj = xmlXPathEvalExpression ((xmlChar *) + "/osm/node|/osm/way|/osm/relation|/osm/user", xpath_ctx); if (xpath_obj && xpath_obj->nodesetval && xpath_obj->nodesetval->nodeNr > 0) @@ -469,3 +470,47 @@ maps_osm_parse (const char *content, guint length, GError **error) return object; } + +/** + * maps_osm_parse_user_details: + * @content: XML data + * @error: Error handle + * Returns: (transfer full) Logged in user name + */ +char * +maps_osm_parse_user_details (const char *content, GError **error) +{ + xmlDocPtr doc; + xmlNodePtr sub_node; + char *ret; + + doc = read_xml_doc (content, strlen (content), error); + sub_node = get_sub_node (doc); + + if (!sub_node) + { + xmlFreeDoc (doc); + *error = g_error_new_literal (MAPS_OSM_ERROR, 0, + _("Could not find OSM element")); + return NULL; + } + + if (g_str_equal (sub_node->name, "user")) + { + g_autoptr (GHashTable) attributes; + + attributes = parse_attributes (sub_node); + ret = g_strdup (g_hash_table_lookup (attributes, "display_name")); + } + else + { + *error = g_error_new_literal (MAPS_OSM_ERROR, 0, + _("Could not find user element")); + ret = NULL; + } + + xmlFreeDoc (doc); + xmlFreeNode (sub_node); + + return ret; +} diff --git a/lib/maps-osm.h b/lib/maps-osm.h index faca7404..b3202479 100644 --- a/lib/maps-osm.h +++ b/lib/maps-osm.h @@ -32,4 +32,6 @@ void maps_osm_finalize (void); MapsOSMObject *maps_osm_parse (const char *content, guint length, GError **error); + +char *maps_osm_parse_user_details(const char *content, GError **error); #endif |