Commit 110abaa6 authored by Jussi Kukkonen's avatar Jussi Kukkonen

GcWebProvider -> GcWebService

GcWebService is no longer an abstract class to derive from,
just a helper class (because providers may need more than one web service)
parent 7e6df114
......@@ -15,7 +15,7 @@ libgeoclue_provider_la_SOURCES = \
geoclue-accuracy.c \
geoclue-error.c \
gc-provider.c \
gc-web-provider.c \
gc-web-service.c \
gc-iface-address.c \
gc-iface-geoclue.c \
gc-iface-geocode.c \
......
#ifndef GC_WEB_PROVIDER_H
#define GC_WEB_PROVIDER_H
#include <glib-object.h>
#include <libxml/xpath.h> /* TODO: should move prvates to .c-file and get rid of this*/
G_BEGIN_DECLS
#define GC_TYPE_WEB_PROVIDER (gc_web_provider_get_type ())
#define GC_WEB_PROVIDER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GC_TYPE_WEB_PROVIDER, GcWebProvider))
#define GC_WEB_PROVIDER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GC_TYPE_WEB_PROVIDER, GcWebProviderClass))
#define GC_IS_WEB_PROVIDER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GC_TYPE_WEB_PROVIDER))
#define GC_IS_WEB_PROVIDER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GC_TYPE_WEB_PROVIDER))
#define GC_WEB_PROVIDER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GC_TYPE_WEB_PROVIDER, GcWebProviderClass))
typedef struct _GcWebProvider {
GcProvider parent;
/* private */
gchar *base_url;
guchar *response;
gint response_length;
GList *namespaces;
xmlXPathContext *xpath_ctx;
} GcWebProvider;
typedef struct _GcWebProviderClass {
GcProviderClass parent_class;
} GcWebProviderClass;
GType gc_web_provider_get_type (void);
gboolean gc_web_provider_set_base_url (GcWebProvider *self, gchar *url);
gboolean gc_web_provider_add_namespace (GcWebProvider *self, gchar *namespace, gchar *uri);
gboolean gc_web_provider_query (GcWebProvider *self, ...);
gboolean gc_web_provider_get_string (GcWebProvider *self, gchar **OUT_value, gchar *xpath);
gboolean gc_web_provider_get_double (GcWebProvider *self, gdouble *OUT_value, gchar *xpath);
gboolean gc_web_provider_get_response (GcWebProvider *self, guchar **response, gint *response_length);
G_END_DECLS
#endif /* GC_WEB_PROVIDER_H */
......@@ -5,10 +5,9 @@
#include <libxml/xpathInternals.h>
#include <libxml/uri.h> /* for xmlURIEscapeStr */
#include <geoclue/gc-provider.h>
#include "gc-web-provider.h"
#include "gc-web-service.h"
G_DEFINE_ABSTRACT_TYPE (GcWebProvider, gc_web_provider, GC_TYPE_PROVIDER)
G_DEFINE_TYPE (GcWebService, gc_web_service, G_TYPE_OBJECT)
typedef struct _XmlNamespace {
gchar *name;
......@@ -17,9 +16,9 @@ typedef struct _XmlNamespace {
/* GFunc, use with g_list_foreach */
static void
gc_web_provider_register_ns (gpointer data, gpointer user_data)
gc_web_service_register_ns (gpointer data, gpointer user_data)
{
GcWebProvider *self = (GcWebProvider *)user_data;
GcWebService *self = (GcWebService *)user_data;
XmlNamespace *ns = (XmlNamespace *)data;
xmlXPathRegisterNs (self->xpath_ctx,
......@@ -28,7 +27,7 @@ gc_web_provider_register_ns (gpointer data, gpointer user_data)
/* GFunc, use with g_list_foreach */
static void
gc_web_provider_free_ns (gpointer data, gpointer user_data)
gc_web_service_free_ns (gpointer data, gpointer user_data)
{
XmlNamespace *ns = (XmlNamespace *)data;
......@@ -40,16 +39,16 @@ gc_web_provider_free_ns (gpointer data, gpointer user_data)
/* Register namespaces listed in self->namespaces */
static void
gc_web_provider_register_namespaces (GcWebProvider *self)
gc_web_service_register_namespaces (GcWebService *self)
{
g_assert (self->xpath_ctx);
g_list_foreach (self->namespaces, (GFunc)gc_web_provider_register_ns, self);
g_list_foreach (self->namespaces, (GFunc)gc_web_service_register_ns, self);
}
/* Parse data (self->response), build xpath context and register
* namespaces. Nothing will be done if xpath context exists already. */
static gboolean
gc_web_provider_build_xpath_context (GcWebProvider *self)
gc_web_service_build_xpath_context (GcWebService *self)
{
xmlDocPtr doc;
xmlChar *tmp;
......@@ -76,13 +75,13 @@ gc_web_provider_build_xpath_context (GcWebProvider *self)
/* TODO: error handling */
return FALSE;
}
gc_web_provider_register_namespaces (self);
gc_web_service_register_namespaces (self);
return TRUE;
}
/* fetch data from url, save into self->response */
static gboolean
gc_web_provider_fetch (GcWebProvider *self, gchar *url)
gc_web_service_fetch (GcWebService *self, gchar *url)
{
void* ctxt = NULL;
gint len;
......@@ -119,7 +118,7 @@ gc_web_provider_fetch (GcWebProvider *self, gchar *url)
}
static void
gc_web_provider_init (GcWebProvider *self)
gc_web_service_init (GcWebService *self)
{
self->response = NULL;
self->response_length = 0;
......@@ -130,31 +129,31 @@ gc_web_provider_init (GcWebProvider *self)
static void
gc_web_provider_finalize (GObject *obj)
gc_web_service_finalize (GObject *obj)
{
GcWebProvider *self = (GcWebProvider *) obj;
GcWebService *self = (GcWebService *) obj;
g_free (self->base_url);
g_free (self->response);
g_list_foreach (self->namespaces, (GFunc)gc_web_provider_free_ns, NULL);
g_list_foreach (self->namespaces, (GFunc)gc_web_service_free_ns, NULL);
g_list_free (self->namespaces);
xmlXPathFreeContext (self->xpath_ctx);
((GObjectClass *) gc_web_provider_parent_class)->finalize (obj);
((GObjectClass *) gc_web_service_parent_class)->finalize (obj);
}
static void
gc_web_provider_class_init (GcWebProviderClass *klass)
gc_web_service_class_init (GcWebServiceClass *klass)
{
GObjectClass *o_class = (GObjectClass *) klass;
o_class->finalize = gc_web_provider_finalize;
o_class->finalize = gc_web_service_finalize;
}
gboolean
gc_web_provider_set_base_url (GcWebProvider *self, gchar *url)
gc_web_service_set_base_url (GcWebService *self, gchar *url)
{
g_return_val_if_fail (url, FALSE);
......@@ -163,7 +162,7 @@ gc_web_provider_set_base_url (GcWebProvider *self, gchar *url)
self->response = NULL;
self->response_length = 0;
g_list_foreach (self->namespaces, (GFunc)gc_web_provider_free_ns, NULL);
g_list_foreach (self->namespaces, (GFunc)gc_web_service_free_ns, NULL);
g_list_free (self->namespaces);
self->namespaces = NULL;
......@@ -176,7 +175,7 @@ gc_web_provider_set_base_url (GcWebProvider *self, gchar *url)
}
gboolean
gc_web_provider_query (GcWebProvider *self, ...)
gc_web_service_query (GcWebService *self, ...)
{
va_list list;
gchar *key, *value, *esc_value, *tmp, *url;
......@@ -208,7 +207,7 @@ gc_web_provider_query (GcWebProvider *self, ...)
}
va_end (list);
if (!gc_web_provider_fetch (self, url)) {
if (!gc_web_service_fetch (self, url)) {
g_free (url);
return FALSE;
}
......@@ -220,7 +219,7 @@ gc_web_provider_query (GcWebProvider *self, ...)
gboolean
gc_web_provider_add_namespace (GcWebProvider *self, gchar *namespace, gchar *uri)
gc_web_service_add_namespace (GcWebService *self, gchar *namespace, gchar *uri)
{
XmlNamespace *ns;
......@@ -235,7 +234,7 @@ gc_web_provider_add_namespace (GcWebProvider *self, gchar *namespace, gchar *uri
gboolean
gc_web_provider_get_double (GcWebProvider *self, gdouble *OUT_value, gchar *xpath)
gc_web_service_get_double (GcWebService *self, gdouble *OUT_value, gchar *xpath)
{
gboolean retval = FALSE;
......@@ -244,7 +243,7 @@ gc_web_provider_get_double (GcWebProvider *self, gdouble *OUT_value, gchar *xpat
g_return_val_if_fail (xpath, FALSE);
/* parse the doc if not parsed yet and register namespaces */
if (!gc_web_provider_build_xpath_context (self)) {
if (!gc_web_service_build_xpath_context (self)) {
return FALSE;
}
g_assert (self->xpath_ctx);
......@@ -261,7 +260,7 @@ gc_web_provider_get_double (GcWebProvider *self, gdouble *OUT_value, gchar *xpat
}
gboolean
gc_web_provider_get_string (GcWebProvider *self, gchar **OUT_value, gchar* xpath)
gc_web_service_get_string (GcWebService *self, gchar **OUT_value, gchar* xpath)
{
gboolean retval= FALSE;
......@@ -270,7 +269,7 @@ gc_web_provider_get_string (GcWebProvider *self, gchar **OUT_value, gchar* xpath
g_return_val_if_fail (xpath, FALSE);
/* parse the doc if not parsed yet and register namespaces */
if (!gc_web_provider_build_xpath_context (self)) {
if (!gc_web_service_build_xpath_context (self)) {
return FALSE;
}
g_assert (self->xpath_ctx);
......@@ -287,7 +286,7 @@ gc_web_provider_get_string (GcWebProvider *self, gchar **OUT_value, gchar* xpath
}
gboolean
gc_web_provider_get_response (GcWebProvider *self, guchar **response, gint *response_length)
gc_web_service_get_response (GcWebService *self, guchar **response, gint *response_length)
{
g_return_val_if_fail (self->response, FALSE);
......
#ifndef GC_WEB_SERVICE_H
#define GC_WEB_SERVICE_H
#include <glib-object.h>
#include <libxml/xpath.h> /* TODO: should move prvates to .c-file and get rid of this*/
G_BEGIN_DECLS
#define GC_TYPE_WEB_SERVICE (gc_web_service_get_type ())
#define GC_WEB_SERVICE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GC_TYPE_WEB_SERVICE, GcWebService))
#define GC_WEB_SERVICE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GC_TYPE_WEB_SERVICE, GcWebServiceClass))
#define GC_IS_WEB_SERVICE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GC_TYPE_WEB_SERVICE))
#define GC_IS_WEB_SERVICE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GC_TYPE_WEB_SERVICE))
#define GC_WEB_SERVICE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GC_TYPE_WEB_SERVICE, GcWebServiceClass))
typedef struct _GcWebService {
GObject parent;
/* private */
gchar *base_url;
guchar *response;
gint response_length;
GList *namespaces;
xmlXPathContext *xpath_ctx;
} GcWebService;
typedef struct _GcWebServiceClass {
GObjectClass parent_class;
} GcWebServiceClass;
GType gc_web_service_get_type (void);
gboolean gc_web_service_set_base_url (GcWebService *self, gchar *url);
gboolean gc_web_service_add_namespace (GcWebService *self, gchar *namespace, gchar *uri);
gboolean gc_web_service_query (GcWebService *self, ...);
gboolean gc_web_service_get_string (GcWebService *self, gchar **OUT_value, gchar *xpath);
gboolean gc_web_service_get_double (GcWebService *self, gdouble *OUT_value, gchar *xpath);
gboolean gc_web_service_get_response (GcWebService *self, guchar **response, gint *response_length);
G_END_DECLS
#endif /* GC_WEB_SERVICE_H */
......@@ -2,6 +2,8 @@
* Geoclue
* gc-provider-hostip.c - A hostip.info-based Address/Position provider
*
* TODO finalize: unref the web service
*
*
* Author: Jussi Kukkonen <jku@o-hand.com>
*/
......@@ -12,7 +14,6 @@
#include <dbus/dbus-glib-bindings.h>
#include <geoclue/gc-provider.h>
#include <geoclue/gc-web-provider.h>
#include <geoclue/geoclue-error.h>
#include <geoclue/gc-iface-position.h>
......@@ -39,7 +40,7 @@ static void gc_provider_hostip_init (GcProviderHostip *obj);
static void gc_provider_hostip_position_init (GcIfacePositionClass *iface);
static void gc_provider_hostip_address_init (GcIfaceAddressClass *iface);
G_DEFINE_TYPE_WITH_CODE (GcProviderHostip, gc_provider_hostip, GC_TYPE_WEB_PROVIDER,
G_DEFINE_TYPE_WITH_CODE (GcProviderHostip, gc_provider_hostip, GC_TYPE_PROVIDER,
G_IMPLEMENT_INTERFACE (GC_TYPE_IFACE_POSITION,
gc_provider_hostip_position_init)
G_IMPLEMENT_INTERFACE (GC_TYPE_IFACE_ADDRESS,
......@@ -93,19 +94,20 @@ gc_provider_hostip_get_position (GcIfacePosition *iface,
GeoclueAccuracy **accuracy,
GError **error)
{
GcWebProvider *obj = GC_WEB_PROVIDER (iface);
GcProviderHostip *obj = (GC_PROVIDER_HOSTIP (iface));
gchar *coord_str = NULL;
*fields = GEOCLUE_POSITION_FIELDS_NONE;
if (!gc_web_provider_query (obj, NULL)) {
if (!gc_web_service_query (obj->web_service, NULL)) {
g_set_error (error, GEOCLUE_ERROR,
GEOCLUE_ERROR_NOT_AVAILABLE, "Web service query failed");
return FALSE;
}
if (gc_web_provider_get_string (obj, &coord_str, HOSTIP_LATLON_XPATH)) {
if (gc_web_service_get_string (obj->web_service,
&coord_str, HOSTIP_LATLON_XPATH)) {
if (sscanf (coord_str, "%lf,%lf", longitude , latitude) == 2) {
*fields |= GEOCLUE_POSITION_FIELDS_LONGITUDE;
*fields |= GEOCLUE_POSITION_FIELDS_LATITUDE;
......@@ -134,11 +136,11 @@ gc_provider_hostip_get_address (GcIfaceAddress *iface,
GeoclueAccuracy **accuracy,
GError **error)
{
GcWebProvider *obj = GC_WEB_PROVIDER (iface);
GcProviderHostip *obj = GC_PROVIDER_HOSTIP (iface);
gchar *locality = NULL;
gchar *country = NULL;
if (!gc_web_provider_query (obj, NULL)) {
if (!gc_web_service_query (obj->web_service, NULL)) {
g_set_error (error, GEOCLUE_ERROR,
GEOCLUE_ERROR_NOT_AVAILABLE, "Web service query failed");
return FALSE;
......@@ -146,7 +148,8 @@ gc_provider_hostip_get_address (GcIfaceAddress *iface,
*address = g_hash_table_new(g_str_hash, g_str_equal);
if (gc_web_provider_get_string (obj, &locality, HOSTIP_LOCALITY_XPATH)) {
if (gc_web_service_get_string (obj->web_service,
&locality, HOSTIP_LOCALITY_XPATH)) {
/* hostip "sctructured data" for the win... */
if (g_ascii_strcasecmp (locality, "(Unknown city)") == 0) {
g_free (locality);
......@@ -158,13 +161,15 @@ gc_provider_hostip_get_address (GcIfaceAddress *iface,
}
}
if (gc_web_provider_get_string (obj, &country, HOSTIP_COUNTRYCODE_XPATH)) {
if (gc_web_service_get_string (obj->web_service,
&country, HOSTIP_COUNTRYCODE_XPATH)) {
/* TODO: get the keys from geoclue-types.h */
g_hash_table_insert(*address, "countrycode", country);
}
if (gc_web_provider_get_string (obj, &country, HOSTIP_COUNTRY_XPATH)) {
if (gc_web_service_get_string (obj->web_service,
&country, HOSTIP_COUNTRY_XPATH)) {
/* TODO: get the keys from geoclue-types.h */
g_hash_table_insert(*address, "country", country);
......@@ -208,11 +213,12 @@ gc_provider_hostip_init (GcProviderHostip *obj)
GC_DBUS_SERVICE_HOSTIP,
GC_DBUS_PATH_HOSTIP);
gc_web_provider_set_base_url (GC_WEB_PROVIDER (obj), HOSTIP_URL);
gc_web_provider_add_namespace (GC_WEB_PROVIDER (obj),
HOSTIP_NS_GML_NAME, HOSTIP_NS_GML_URI);
gc_web_provider_add_namespace (GC_WEB_PROVIDER (obj),
HOSTIP_NS_HOSTIP_NAME, HOSTIP_NS_HOSTIP_URI);
obj->web_service = g_object_new (GC_TYPE_WEB_SERVICE, NULL);
gc_web_service_set_base_url (obj->web_service, HOSTIP_URL);
gc_web_service_add_namespace (obj->web_service,
HOSTIP_NS_GML_NAME, HOSTIP_NS_GML_URI);
gc_web_service_add_namespace (obj->web_service,
HOSTIP_NS_HOSTIP_NAME, HOSTIP_NS_HOSTIP_URI);
}
static void
......
......@@ -9,6 +9,7 @@
#define _GC_PROVIDER_HOSTIP
#include <glib-object.h>
#include <geoclue/gc-web-service.h>
G_BEGIN_DECLS
......@@ -21,12 +22,13 @@ G_BEGIN_DECLS
#define GC_IS_PROVIDER_HOSTIP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GC_TYPE_PROVIDER_HOSTIP))
typedef struct _GcProviderHostip {
GcWebProvider parent;
GcProvider parent;
GMainLoop *loop;
GcWebService *web_service;
} GcProviderHostip;
typedef struct _GcProviderHostipClass {
GcWebProviderClass parent_class;
GcProviderClass parent_class;
} GcProviderHostipClass;
GType gc_provider_hostip_get_type (void);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment