Commit 11f09d77 authored by Zeeshan Ali's avatar Zeeshan Ali

location-source: Turn interface into an abstract class

This means less code in subclasses.
parent abc5fe08
......@@ -40,19 +40,11 @@
* Contains functions to get the geolocation corresponding to IP addresses from a server.
**/
enum {
PROP_0,
PROP_LOCATION,
N_PROPERTIES
};
struct _GClueIpclientPrivate {
SoupSession *soup_session;
char *ip;
GeocodeLocation *location;
SoupMessage *query;
gulong network_changed_id;
......@@ -62,59 +54,31 @@ static void
gclue_ipclient_start (GClueLocationSource *source);
static void
gclue_ipclient_stop (GClueLocationSource *source);
static GeocodeLocation *
gclue_ipclient_get_location (GClueLocationSource *source);
static void
gclue_location_source_interface_init (GClueLocationSourceInterface *iface);
G_DEFINE_TYPE_WITH_CODE (GClueIpclient, gclue_ipclient, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (GCLUE_TYPE_LOCATION_SOURCE,
gclue_location_source_interface_init))
G_DEFINE_TYPE (GClueIpclient, gclue_ipclient, GCLUE_TYPE_LOCATION_SOURCE)
static void
gclue_ipclient_finalize (GObject *gipclient)
{
GClueIpclient *ipclient = (GClueIpclient *) gipclient;
gclue_ipclient_stop (GCLUE_LOCATION_SOURCE (ipclient));
g_clear_object (&ipclient->priv->soup_session);
g_free (ipclient->priv->ip);
G_OBJECT_CLASS (gclue_ipclient_parent_class)->finalize (gipclient);
}
static void
gclue_ipclient_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GClueIpclient *ipclient = GCLUE_IPCLIENT (object);
switch (prop_id) {
case PROP_LOCATION:
g_value_set_object (value, ipclient->priv->location);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gclue_ipclient_class_init (GClueIpclientClass *klass)
{
GClueLocationSourceClass *source_class = GCLUE_LOCATION_SOURCE_CLASS (klass);
GObjectClass *gipclient_class = G_OBJECT_CLASS (klass);
source_class->start = gclue_ipclient_start;
source_class->stop = gclue_ipclient_stop;
gipclient_class->finalize = gclue_ipclient_finalize;
gipclient_class->get_property = gclue_ipclient_get_property;
g_type_class_add_private (klass, sizeof (GClueIpclientPrivate));
g_object_class_override_property (gipclient_class,
PROP_LOCATION,
"location");
}
static void
......@@ -128,14 +92,6 @@ gclue_ipclient_init (GClueIpclient *ipclient)
NULL);
}
static void
gclue_location_source_interface_init (GClueLocationSourceInterface *iface)
{
iface->start = gclue_ipclient_start;
iface->stop = gclue_ipclient_stop;
iface->get_location = gclue_ipclient_get_location;
}
/**
* gclue_ipclient_new_for_ip:
* @str: The IP address
......@@ -326,25 +282,6 @@ _gclue_ip_json_to_location (const char *json,
return location;
}
static void
gclue_ipclient_update_location (GClueIpclient *ipclient,
GeocodeLocation *location)
{
GClueIpclientPrivate *priv = ipclient->priv;
if (priv->location == NULL)
priv->location = g_object_new (GEOCODE_TYPE_LOCATION, NULL);
g_object_set (priv->location,
"latitude", geocode_location_get_latitude (location),
"longitude", geocode_location_get_longitude (location),
"accuracy", geocode_location_get_accuracy (location),
"description", geocode_location_get_description (location),
NULL);
g_object_notify (G_OBJECT (ipclient), "location");
}
static void
query_callback (SoupSession *session,
SoupMessage *query,
......@@ -370,7 +307,8 @@ query_callback (SoupSession *session,
return;
}
gclue_ipclient_update_location (ipclient, location);
gclue_location_source_set_location (GCLUE_LOCATION_SOURCE (ipclient),
location);
}
static void
......@@ -439,11 +377,3 @@ gclue_ipclient_stop (GClueLocationSource *source)
SOUP_STATUS_CANCELLED);
priv->query = NULL;
}
static GeocodeLocation *
gclue_ipclient_get_location (GClueLocationSource *source)
{
g_return_val_if_fail (GCLUE_IS_IPCLIENT (source), NULL);
return GCLUE_IPCLIENT (source)->priv->location;
}
......@@ -50,7 +50,7 @@ typedef struct _GClueIpclientPrivate GClueIpclientPrivate;
struct _GClueIpclient {
/* <private> */
GObject parent_instance;
GClueLocationSource parent_instance;
GClueIpclientPrivate *priv;
};
......@@ -61,7 +61,7 @@ struct _GClueIpclient {
**/
struct _GClueIpclientClass {
/* <private> */
GObjectClass parent_class;
GClueLocationSourceClass parent_class;
};
GClueIpclient *gclue_ipclient_new (void);
......
......@@ -30,19 +30,98 @@
* The interface all geolocation sources must implement.
**/
G_DEFINE_INTERFACE (GClueLocationSource, gclue_location_source, 0);
G_DEFINE_ABSTRACT_TYPE (GClueLocationSource, gclue_location_source, G_TYPE_OBJECT)
struct _GClueLocationSourcePrivate
{
GeocodeLocation *location;
};
enum
{
PROP_0,
PROP_LOCATION,
LAST_PROP
};
static GParamSpec *gParamSpecs[LAST_PROP];
static void
gclue_location_source_finalize (GObject *object)
{
gclue_location_source_stop (GCLUE_LOCATION_SOURCE (object));
G_OBJECT_CLASS (gclue_location_source_parent_class)->finalize (object);
}
static void
gclue_location_source_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GClueLocationSource *source = GCLUE_LOCATION_SOURCE (object);
switch (prop_id) {
case PROP_LOCATION:
g_value_set_object (value, source->priv->location);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gclue_location_source_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GClueLocationSource *source = GCLUE_LOCATION_SOURCE (object);
switch (prop_id) {
case PROP_LOCATION:
{
GeocodeLocation *location = g_value_get_object (value);
gclue_location_source_set_location (source, location);
break;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gclue_location_source_class_init (GClueLocationSourceClass *klass)
{
GObjectClass *object_class;
object_class = G_OBJECT_CLASS (klass);
object_class->finalize = gclue_location_source_finalize;
object_class->get_property = gclue_location_source_get_property;
object_class->set_property = gclue_location_source_set_property;
g_type_class_add_private (object_class, sizeof (GClueLocationSourcePrivate));
gParamSpecs[PROP_LOCATION] = g_param_spec_object ("location",
"Location",
"Location",
GEOCODE_TYPE_LOCATION,
G_PARAM_READWRITE);
g_object_class_install_property (object_class,
PROP_LOCATION,
gParamSpecs[PROP_LOCATION]);
}
static void
gclue_location_source_default_init (GClueLocationSourceInterface *iface)
gclue_location_source_init (GClueLocationSource *source)
{
GParamSpec *pspec;
pspec = g_param_spec_object ("location",
"Location",
"Location",
GEOCODE_TYPE_LOCATION,
G_PARAM_READABLE);
g_object_interface_install_property (iface, pspec);
source->priv =
G_TYPE_INSTANCE_GET_PRIVATE (source,
GCLUE_TYPE_LOCATION_SOURCE,
GClueLocationSourcePrivate);
}
/**
......@@ -56,7 +135,7 @@ gclue_location_source_start (GClueLocationSource *source)
{
g_return_if_fail (GCLUE_IS_LOCATION_SOURCE (source));
GCLUE_LOCATION_SOURCE_GET_INTERFACE (source)->start (source);
GCLUE_LOCATION_SOURCE_GET_CLASS (source)->start (source);
}
/**
......@@ -71,7 +150,7 @@ gclue_location_source_stop (GClueLocationSource *source)
{
g_return_if_fail (GCLUE_IS_LOCATION_SOURCE (source));
GCLUE_LOCATION_SOURCE_GET_INTERFACE (source)->stop (source);
GCLUE_LOCATION_SOURCE_GET_CLASS (source)->stop (source);
}
/**
......@@ -85,5 +164,31 @@ gclue_location_source_get_location (GClueLocationSource *source)
{
g_return_val_if_fail (GCLUE_IS_LOCATION_SOURCE (source), NULL);
return GCLUE_LOCATION_SOURCE_GET_INTERFACE (source)->get_location (source);
return source->priv->location;
}
/**
* gclue_location_source_set_location:
* @source: a #GClueLocationSource
*
* Set the current location to @location. Its meant to be only used by
* subclasses.
**/
void
gclue_location_source_set_location (GClueLocationSource *source,
GeocodeLocation *location)
{
GClueLocationSourcePrivate *priv = source->priv;
if (priv->location == NULL)
priv->location = g_object_new (GEOCODE_TYPE_LOCATION, NULL);
g_object_set (priv->location,
"latitude", geocode_location_get_latitude (location),
"longitude", geocode_location_get_longitude (location),
"accuracy", geocode_location_get_accuracy (location),
"description", geocode_location_get_description (location),
NULL);
g_object_notify (G_OBJECT (source), "location");
}
......@@ -28,29 +28,43 @@
G_BEGIN_DECLS
GType gclue_location_source_get_type (void) G_GNUC_CONST;
#define GCLUE_TYPE_LOCATION_SOURCE (gclue_location_source_get_type())
#define GCLUE_LOCATION_SOURCE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GCLUE_TYPE_LOCATION_SOURCE, GClueLocationSource))
#define GCLUE_LOCATION_SOURCE_CONST(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GCLUE_TYPE_LOCATION_SOURCE, GClueLocationSource const))
#define GCLUE_LOCATION_SOURCE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GCLUE_TYPE_LOCATION_SOURCE, GClueLocationSourceClass))
#define GCLUE_IS_LOCATION_SOURCE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GCLUE_TYPE_LOCATION_SOURCE))
#define GCLUE_IS_LOCATION_SOURCE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GCLUE_TYPE_LOCATION_SOURCE))
#define GCLUE_LOCATION_SOURCE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GCLUE_TYPE_LOCATION_SOURCE, GClueLocationSourceClass))
typedef struct _GClueLocationSource GClueLocationSource;
typedef struct _GClueLocationSourceClass GClueLocationSourceClass;
typedef struct _GClueLocationSourcePrivate GClueLocationSourcePrivate;
#define GCLUE_TYPE_LOCATION_SOURCE (gclue_location_source_get_type ())
#define GCLUE_LOCATION_SOURCE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GCLUE_TYPE_LOCATION_SOURCE, GClueLocationSource))
#define GCLUE_IS_LOCATION_SOURCE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GCLUE_TYPE_LOCATION_SOURCE))
#define GCLUE_LOCATION_SOURCE_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), GCLUE_TYPE_LOCATION_SOURCE, GClueLocationSourceInterface))
struct _GClueLocationSource
{
GObject parent;
typedef struct _GClueLocationSource GClueLocationSource; /* dummy object */
typedef struct _GClueLocationSourceInterface GClueLocationSourceInterface;
/*< private >*/
GClueLocationSourcePrivate *priv;
};
struct _GClueLocationSourceInterface
struct _GClueLocationSourceClass
{
GTypeInterface parent_iface;
GObjectClass parent_class;
void (*start) (GClueLocationSource *source);
void (*stop) (GClueLocationSource *source);
GeocodeLocation * (*get_location) (GClueLocationSource *source);
};
GType gclue_location_source_get_type (void) G_GNUC_CONST;
void gclue_location_source_start (GClueLocationSource *source);
void gclue_location_source_stop (GClueLocationSource *source);
GeocodeLocation * gclue_location_source_get_location
(GClueLocationSource *source);
void gclue_location_source_set_location
(GClueLocationSource *source,
GeocodeLocation *location);
G_END_DECLS
......
......@@ -46,17 +46,9 @@
* Location Service</ulink> to achieve that.
**/
enum {
PROP_0,
PROP_LOCATION,
N_PROPERTIES
};
struct _GClueWifiPrivate {
SoupSession *soup_session;
GeocodeLocation *location;
SoupMessage *query;
gulong network_changed_id;
......@@ -66,58 +58,30 @@ static void
gclue_wifi_start (GClueLocationSource *source);
static void
gclue_wifi_stop (GClueLocationSource *source);
static GeocodeLocation *
gclue_wifi_get_location (GClueLocationSource *source);
static void
gclue_location_source_interface_init (GClueLocationSourceInterface *iface);
G_DEFINE_TYPE_WITH_CODE (GClueWifi, gclue_wifi, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (GCLUE_TYPE_LOCATION_SOURCE,
gclue_location_source_interface_init))
G_DEFINE_TYPE (GClueWifi, gclue_wifi, GCLUE_TYPE_LOCATION_SOURCE)
static void
gclue_wifi_finalize (GObject *gwifi)
{
GClueWifi *wifi = (GClueWifi *) gwifi;
gclue_wifi_stop (GCLUE_LOCATION_SOURCE (wifi));
g_clear_object (&wifi->priv->soup_session);
G_OBJECT_CLASS (gclue_wifi_parent_class)->finalize (gwifi);
}
static void
gclue_wifi_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GClueWifi *wifi = GCLUE_WIFI (object);
switch (prop_id) {
case PROP_LOCATION:
g_value_set_object (value, wifi->priv->location);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gclue_wifi_class_init (GClueWifiClass *klass)
{
GClueLocationSourceClass *source_class = GCLUE_LOCATION_SOURCE_CLASS (klass);
GObjectClass *gwifi_class = G_OBJECT_CLASS (klass);
source_class->start = gclue_wifi_start;
source_class->stop = gclue_wifi_stop;
gwifi_class->finalize = gclue_wifi_finalize;
gwifi_class->get_property = gclue_wifi_get_property;
g_type_class_add_private (klass, sizeof (GClueWifiPrivate));
g_object_class_override_property (gwifi_class,
PROP_LOCATION,
"location");
}
static void
......@@ -131,14 +95,6 @@ gclue_wifi_init (GClueWifi *wifi)
NULL);
}
static void
gclue_location_source_interface_init (GClueLocationSourceInterface *iface)
{
iface->start = gclue_wifi_start;
iface->stop = gclue_wifi_stop;
iface->get_location = gclue_wifi_get_location;
}
/**
* gclue_wifi_new:
*
......@@ -294,25 +250,6 @@ _gclue_wifi_json_to_location (const char *json,
return location;
}
static void
gclue_wifi_update_location (GClueWifi *wifi,
GeocodeLocation *location)
{
GClueWifiPrivate *priv = wifi->priv;
if (priv->location == NULL)
priv->location = g_object_new (GEOCODE_TYPE_LOCATION, NULL);
g_object_set (priv->location,
"latitude", geocode_location_get_latitude (location),
"longitude", geocode_location_get_longitude (location),
"accuracy", geocode_location_get_accuracy (location),
"description", geocode_location_get_description (location),
NULL);
g_object_notify (G_OBJECT (wifi), "location");
}
static void
query_callback (SoupSession *session,
SoupMessage *query,
......@@ -338,7 +275,8 @@ query_callback (SoupSession *session,
return;
}
gclue_wifi_update_location (wifi, location);
gclue_location_source_set_location (GCLUE_LOCATION_SOURCE (wifi),
location);
}
static void
......@@ -418,11 +356,3 @@ gclue_wifi_stop (GClueLocationSource *source)
SOUP_STATUS_CANCELLED);
priv->query = NULL;
}
static GeocodeLocation *
gclue_wifi_get_location (GClueLocationSource *source)
{
g_return_val_if_fail (GCLUE_IS_WIFI (source), NULL);
return GCLUE_WIFI (source)->priv->location;
}
......@@ -48,7 +48,7 @@ typedef struct _GClueWifiPrivate GClueWifiPrivate;
struct _GClueWifi {
/* <private> */
GObject parent_instance;
GClueLocationSource parent_instance;
GClueWifiPrivate *priv;
};
......@@ -59,7 +59,7 @@ struct _GClueWifi {
**/
struct _GClueWifiClass {
/* <private> */
GObjectClass parent_class;
GClueLocationSourceClass parent_class;
};
GClueWifi * gclue_wifi_new (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