connection: Notify network config changed from libnet-client's event
[platform/core/api/connection.git] / src / connection.c
index 0bf2f23..76147ab 100755 (executable)
@@ -86,33 +86,13 @@ static void *__connection_get_type_changed_userdata(
        return local_handle->type_changed_user_data;
 }
 
-static gboolean __connection_cb_type_changed_cb_idle(gpointer user_data)
-{
-       int state, status;
-       void *data;
-       connection_type_changed_cb callback;
-       connection_handle_s *local_handle = (connection_handle_s *)user_data;
-
-       if (__connection_check_handle_validity((connection_h)local_handle) != true)
-               return FALSE;
-
-       if (vconf_get_int(VCONFKEY_NETWORK_STATUS, &status) != 0)
-               return FALSE;
-
-       state = __connection_convert_net_state(status);
-
-       callback = __connection_get_type_changed_callback(local_handle);
-       data = __connection_get_type_changed_userdata(local_handle);
-       if (callback)
-               callback(state, data);
-
-       return FALSE;
-}
-
-static void __connection_cb_type_change_cb(keynode_t *node, void *user_data)
+static void __connection_cb_type_change_cb(int type)
 {
        GSList *list;
        connection_h handle;
+       void *data;
+       connection_type_changed_cb callback;
+       int state;
 
        if (_connection_is_created() != true) {
                CONNECTION_LOG(CONNECTION_ERROR, "Application is not registered"
@@ -120,9 +100,15 @@ static void __connection_cb_type_change_cb(keynode_t *node, void *user_data)
                return;
        }
 
+       state = __connection_convert_net_state(type);
+
        for (list = conn_handle_list; list; list = list->next) {
                handle = (connection_h)list->data;
-               _connection_callback_add(__connection_cb_type_changed_cb_idle, (gpointer)handle);
+
+               callback = __connection_get_type_changed_callback(handle);
+               data = __connection_get_type_changed_userdata(handle);
+               if (callback)
+                       callback(state, data);
        }
 }
 
@@ -163,23 +149,19 @@ static int __connection_set_type_changed_callback(connection_h connection,
 
        if (callback) {
                if (refcount == 0)
-                       vconf_notify_key_changed(VCONFKEY_NETWORK_STATUS,
-                                       __connection_cb_type_change_cb, NULL);
+                       _connection_libnet_set_type_changed_cb(
+                                                          __connection_cb_type_change_cb);
 
                refcount++;
-               CONNECTION_LOG(CONNECTION_INFO, "Successfully registered(%d)", refcount);
+               CONNECTION_LOG(CONNECTION_INFO, "Successfully registered(%d)",
+                                          refcount);
        } else {
                if (refcount > 0 &&
                                __connection_get_type_changed_callback(local_handle) != NULL) {
                        if (--refcount == 0) {
-                               if (vconf_ignore_key_changed(VCONFKEY_NETWORK_STATUS,
-                                               __connection_cb_type_change_cb) < 0) {
-                                       CONNECTION_LOG(CONNECTION_ERROR, //LCOV_EXCL_LINE
-                                                       "Error to de-register vconf callback(%d)", refcount);
-                               } else {
-                                       CONNECTION_LOG(CONNECTION_INFO,
-                                                       "Successfully de-registered(%d)", refcount);
-                               }
+                               _connection_libnet_set_type_changed_cb(NULL);
+                               CONNECTION_LOG(CONNECTION_INFO,
+                                               "Successfully de-registered(%d)", refcount);
                        }
                }
        }
@@ -202,43 +184,15 @@ static void *__connection_get_ip_changed_userdata(
        return local_handle->ip_changed_user_data;
 }
 
-static gboolean __connection_cb_ip_changed_cb_idle(gpointer user_data)
-{
-       char *ip_addr;
-       char *ip6_addr;
-       void *data;
-       connection_address_changed_cb callback;
-       connection_handle_s *local_handle = (connection_handle_s *)user_data;
-
-       if (__connection_check_handle_validity((connection_h)local_handle) != true)
-               return FALSE;
-
-       ip_addr = vconf_get_str(VCONFKEY_NETWORK_IP);
-       if (ip_addr == NULL)
-               CONNECTION_LOG(CONNECTION_ERROR, //LCOV_EXCL_LINE
-                       "vconf_get_str(VCONFKEY_NETWORK_IP) is Failed");
-
-       ip6_addr = vconf_get_str(VCONFKEY_NETWORK_IP6);
-       if (ip6_addr == NULL)
-               CONNECTION_LOG(CONNECTION_ERROR, //LCOV_EXCL_LINE
-                       "vconf_get_str(VCONFKEY_NETWORK_IP6) is Failed");
-
-       callback = __connection_get_ip_changed_callback(local_handle);
-       data = __connection_get_ip_changed_userdata(local_handle);
-
-       if (callback)
-               callback(ip_addr, ip6_addr, data);
-
-       free(ip_addr);
-       free(ip6_addr);
-
-       return FALSE;
-}
-
-static void __connection_cb_ip_change_cb(keynode_t *node, void *user_data)
+static void __connection_cb_ip_change_cb(
+                        connection_address_family_e addr_family, char *ip_addr)
 {
        GSList *list;
        connection_h handle;
+       char *ip4_addr = NULL;
+       char *ip6_addr = NULL;
+       void *data;
+       connection_address_changed_cb callback;
 
        if (_connection_is_created() != true) {
                CONNECTION_LOG(CONNECTION_ERROR, "Application is not registered"
@@ -246,10 +200,40 @@ static void __connection_cb_ip_change_cb(keynode_t *node, void *user_data)
                return;
        }
 
+       switch (addr_family) {
+       case CONNECTION_ADDRESS_FAMILY_IPV4:
+               ip4_addr = g_strdup(ip_addr);
+
+               ip6_addr = vconf_get_str(VCONFKEY_NETWORK_IP6);
+               if (ip6_addr == NULL)
+                       CONNECTION_LOG(CONNECTION_ERROR, //LCOV_EXCL_LINE
+                                                  "vconf_get_str(VCONFKEY_NETWORK_IP6) failed");
+               break;
+       case CONNECTION_ADDRESS_FAMILY_IPV6:
+               ip6_addr = g_strdup(ip_addr);
+
+               ip4_addr = vconf_get_str(VCONFKEY_NETWORK_IP);
+               if (ip4_addr == NULL)
+                       CONNECTION_LOG(CONNECTION_ERROR, //LCOV_EXCL_LINE
+                                                  "vconf_get_str(VCONFKEY_NETWORK_IP) failed");
+               break;
+       default:
+               CONNECTION_LOG(CONNECTION_ERROR, "Invalid Address Type");
+               return;
+       }
+
        for (list = conn_handle_list; list; list = list->next) {
                handle = (connection_h)list->data;
-               _connection_callback_add(__connection_cb_ip_changed_cb_idle, (gpointer)handle);
+
+               callback = __connection_get_ip_changed_callback(handle);
+               data = __connection_get_ip_changed_userdata(handle);
+
+               if (callback)
+                       callback(ip4_addr, ip6_addr, data);
        }
+
+       g_free(ip4_addr);
+       g_free(ip6_addr);
 }
 
 static int __connection_set_ip_changed_callback(connection_h connection,
@@ -261,35 +245,20 @@ static int __connection_set_ip_changed_callback(connection_h connection,
        local_handle = (connection_handle_s *)connection;
 
        if (callback) {
-               if (refcount == 0) {
-                       vconf_notify_key_changed(VCONFKEY_NETWORK_IP,
-                                       __connection_cb_ip_change_cb, NULL);
-                       vconf_notify_key_changed(VCONFKEY_NETWORK_IP6,
-                                       __connection_cb_ip_change_cb, NULL);
-               }
+               if (refcount == 0)
+                       _connection_libnet_set_ip_changed_cb(
+                                                          __connection_cb_ip_change_cb);
 
                refcount++;
-               CONNECTION_LOG(CONNECTION_INFO, "Successfully registered(%d)", refcount);
+               CONNECTION_LOG(CONNECTION_INFO, "Successfully registered(%d)",
+                                          refcount);
        } else {
                if (refcount > 0 &&
                                __connection_get_ip_changed_callback(local_handle) != NULL) {
                        if (--refcount == 0) {
-                               if (vconf_ignore_key_changed(VCONFKEY_NETWORK_IP,
-                                               __connection_cb_ip_change_cb) < 0) {
-                                       CONNECTION_LOG(CONNECTION_ERROR, //LCOV_EXCL_LINE
-                                                       "Error to de-register vconf callback(%d)", refcount);
-                               } else {
-                                       CONNECTION_LOG(CONNECTION_INFO,
-                                                       "Successfully de-registered(%d)", refcount);
-                               }
-                               if (vconf_ignore_key_changed(VCONFKEY_NETWORK_IP6,
-                                               __connection_cb_ip_change_cb) < 0) {
-                                       CONNECTION_LOG(CONNECTION_ERROR, //LCOV_EXCL_LINE
-                                                       "Error to de-register vconf callback(%d)", refcount);
-                               } else {
-                                       CONNECTION_LOG(CONNECTION_INFO,
-                                                       "Successfully de-registered(%d)", refcount);
-                               }
+                               _connection_libnet_set_ip_changed_cb(NULL);
+                               CONNECTION_LOG(CONNECTION_INFO,
+                                                          "Successfully de-registered(%d)", refcount);
                        }
                }
        }
@@ -312,36 +281,12 @@ static void *__connection_get_proxy_changed_userdata(
        return local_handle->proxy_changed_user_data;
 }
 
-static gboolean __connection_cb_proxy_changed_cb_idle(gpointer user_data)
-{
-       char *proxy;
-       void *data;
-       connection_address_changed_cb callback;
-       connection_handle_s *local_handle = (connection_handle_s *)user_data;
-
-       if (__connection_check_handle_validity((connection_h)local_handle) != true)
-               return FALSE;
-
-       proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
-       if (proxy == NULL)
-               CONNECTION_LOG(CONNECTION_ERROR, //LCOV_EXCL_LINE
-                       "vconf_get_str(VCONFKEY_NETWORK_PROXY) is Failed");
-
-       callback = __connection_get_proxy_changed_callback(local_handle);
-       data = __connection_get_proxy_changed_userdata(local_handle);
-       /* TODO: IPv6 should be supported */
-       if (callback)
-               callback(proxy, NULL, data);
-
-       free(proxy);
-
-       return FALSE;
-}
-
-static void __connection_cb_proxy_change_cb(keynode_t *node, void *user_data)
+static void __connection_cb_proxy_change_cb(char *proxy_addr)
 {
        GSList *list;
        connection_h handle;
+       void *data;
+       connection_address_changed_cb callback;
 
        if (_connection_is_created() != true) {
                CONNECTION_LOG(CONNECTION_ERROR, "Application is not registered"
@@ -351,7 +296,12 @@ static void __connection_cb_proxy_change_cb(keynode_t *node, void *user_data)
 
        for (list = conn_handle_list; list; list = list->next) {
                handle = (connection_h)list->data;
-               _connection_callback_add(__connection_cb_proxy_changed_cb_idle, (gpointer)handle);
+
+               callback = __connection_get_proxy_changed_callback(handle);
+               data = __connection_get_proxy_changed_userdata(handle);
+               /* TODO: IPv6 should be supported */
+               if (callback)
+                       callback(proxy_addr, NULL, data);
        }
 }
 
@@ -365,23 +315,19 @@ static int __connection_set_proxy_changed_callback(connection_h connection,
 
        if (callback) {
                if (refcount == 0)
-                       vconf_notify_key_changed(VCONFKEY_NETWORK_PROXY,
-                                       __connection_cb_proxy_change_cb, NULL);
+                       _connection_libnet_set_proxy_changed_cb(
+                               __connection_cb_proxy_change_cb);
 
                refcount++;
-               CONNECTION_LOG(CONNECTION_INFO, "Successfully registered(%d)", refcount);
+               CONNECTION_LOG(CONNECTION_INFO, "Successfully registered(%d)",
+                                          refcount);
        } else {
                if (refcount > 0 &&
                                __connection_get_proxy_changed_callback(local_handle) != NULL) {
                        if (--refcount == 0) {
-                               if (vconf_ignore_key_changed(VCONFKEY_NETWORK_PROXY,
-                                               __connection_cb_proxy_change_cb) < 0) {
-                                       CONNECTION_LOG(CONNECTION_ERROR, //LCOV_EXCL_LINE
-                                                       "Error to de-register vconf callback(%d)", refcount);
-                               } else {
-                                       CONNECTION_LOG(CONNECTION_INFO,
-                                                       "Successfully de-registered(%d)", refcount);
-                               }
+                               _connection_libnet_set_proxy_changed_cb(NULL);
+                               CONNECTION_LOG(CONNECTION_INFO,
+                                               "Successfully de-registered(%d)", refcount);
                        }
                }
        }