tizen beta release
[framework/location/libslp-location.git] / location / location-wps.c
index 5b69e7d..8d11352 100644 (file)
@@ -40,6 +40,7 @@
 typedef struct _LocationWpsPrivate {
        LocationWpsMod* mod;
        gboolean is_started;
+       gboolean set_noti;
        gboolean enabled;
        guint    interval;
        LocationPosition *pos;
@@ -118,14 +119,23 @@ location_setting_wps_cb(keynode_t *key,
        LocationWpsPrivate* priv = GET_PRIVATE(self);
        g_return_if_fail (priv->mod);
        g_return_if_fail (priv->mod->handler);
-       if (0 == location_setting_get_key_val(key) && priv->mod->ops.stop) {
-               LOCATION_LOGD("location stopped by setting");
-               priv->mod->ops.stop(priv->mod->handler);
+
+       int ret = LOCATION_ERROR_NONE;
+
+       if (location_setting_get_key_val(key) == 0) {
+               if (priv->mod->ops.stop && priv->is_started) {
+                       ret = priv->mod->ops.stop(priv->mod->handler);
+                       if (ret == LOCATION_ERROR_NONE) priv->is_started = FALSE;
+               }
        }
-       else if (1 == location_setting_get_key_val(key) && priv->mod->ops.start) {
-               LOCATION_LOGD("location resumed by setting");
-               priv->mod->ops.start (priv->mod->handler, wps_status_cb, wps_position_cb, wps_velocity_cb, self);
+       else {
+               if (1 == location_setting_get_int(NETWORK_ENABLED) && priv->mod->ops.start && !priv->is_started) {
+                       LOCATION_LOGD("location resumed by setting");
+                       ret = priv->mod->ops.start (priv->mod->handler, wps_status_cb, wps_position_cb, wps_velocity_cb, self);
+                       if (ret == LOCATION_ERROR_NONE) priv->is_started = TRUE;
+               }
        }
+
 }
 
 static int
@@ -136,16 +146,37 @@ location_wps_start (LocationWps *self)
        g_return_val_if_fail (priv->mod, LOCATION_ERROR_NOT_AVAILABLE);
        g_return_val_if_fail (priv->mod->handler, LOCATION_ERROR_NOT_AVAILABLE);
        g_return_val_if_fail (priv->mod->ops.start, LOCATION_ERROR_NOT_AVAILABLE);
-       setting_retval_if_fail (GPS_ENABLED);
-       setting_retval_if_fail (NETWORK_ENABLED);
+
        if (priv->is_started == TRUE) return LOCATION_ERROR_NONE;
 
-       int ret = priv->mod->ops.start (priv->mod->handler, wps_status_cb, wps_position_cb, wps_velocity_cb, self);
-       if (ret == LOCATION_ERROR_NONE) {
-               priv->is_started = TRUE;
-               location_setting_add_notify (GPS_ENABLED, location_setting_wps_cb, self);
-               location_setting_add_notify (NETWORK_ENABLED, location_setting_wps_cb, self);
+       int ret = LOCATION_ERROR_NONE;
+       int noti_err = 0;
+
+       if (!location_setting_get_int(GPS_ENABLED) || !location_setting_get_int(NETWORK_ENABLED)) {
+               ret = LOCATION_ERROR_NOT_ALLOWED;
+       }
+       else {
+               ret = priv->mod->ops.start (priv->mod->handler, wps_status_cb, wps_position_cb, wps_velocity_cb, self);
+               if (ret == LOCATION_ERROR_NONE) {
+                       priv->is_started = TRUE;
+               }
+               else {
+                       return ret;
+               }
        }
+
+       if (priv->set_noti == FALSE) {
+               noti_err = location_setting_add_notify (GPS_ENABLED, location_setting_wps_cb, self);
+               if (noti_err != 0) {
+                       return LOCATION_ERROR_UNKNOWN;
+               }
+               noti_err = location_setting_add_notify (NETWORK_ENABLED, location_setting_wps_cb, self);
+               if (noti_err != 0) {
+                       return LOCATION_ERROR_UNKNOWN;
+               }
+               priv->set_noti = TRUE;
+       }
+
        return ret;
 }
 
@@ -157,14 +188,32 @@ location_wps_stop (LocationWps *self)
        g_return_val_if_fail (priv->mod, LOCATION_ERROR_NOT_AVAILABLE);
        g_return_val_if_fail (priv->mod->handler, LOCATION_ERROR_NOT_AVAILABLE);
        g_return_val_if_fail (priv->mod->ops.stop, LOCATION_ERROR_NOT_AVAILABLE);
-       if( priv->is_started == FALSE) return LOCATION_ERROR_NONE;
 
-       int ret = priv->mod->ops.stop (priv->mod->handler);
-       if (ret == LOCATION_ERROR_NONE) {
-               priv->is_started = FALSE;
-               location_setting_ignore_notify (GPS_ENABLED, location_setting_wps_cb);
-               location_setting_ignore_notify (NETWORK_ENABLED, location_setting_wps_cb);
+       int ret = LOCATION_ERROR_NONE;
+       int noti_err = 0;
+
+       if (priv->is_started == TRUE) {
+               ret = priv->mod->ops.stop (priv->mod->handler);
+               if (ret == LOCATION_ERROR_NONE) {
+                       priv->is_started = FALSE;
+               }
+               else {
+                       return ret;
+               }
        }
+
+       if (priv->set_noti == TRUE) {
+               noti_err = location_setting_ignore_notify (GPS_ENABLED, location_setting_wps_cb);
+               if (noti_err != 0) {
+                       return LOCATION_ERROR_UNKNOWN;
+               }
+               noti_err = location_setting_ignore_notify (NETWORK_ENABLED, location_setting_wps_cb);
+               if (noti_err != 0) {
+                       return LOCATION_ERROR_UNKNOWN;
+               }
+               priv->set_noti = FALSE;
+       }
+
        return ret;
 }
 
@@ -306,6 +355,7 @@ location_wps_init (LocationWps *self)
        if(!priv->mod) LOCATION_LOGW("module loading failed");
 
        priv->is_started = FALSE;
+       priv->set_noti = FALSE;
        priv->enabled= FALSE;
        priv->interval = LOCATION_UPDATE_INTERVAL_DEFAULT;