Fix bug in using db_prepare.
[platform/core/appfw/ail.git] / src / ail_desktop.c
index 769bd13..aeb06ee 100755 (executable)
@@ -30,9 +30,9 @@
 #include <time.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <tzplatform_config.h>
 #include <xdgmime.h>
 
-#include <vconf.h>
 #include <glib.h>
 #include <grp.h>
 #include <pwd.h>
 #include "ail_db.h"
 #include "ail_sql.h"
 #include "ail.h"
+#include "ail_vconf.h"
 
 #define BUFSIZE 4096
+#define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
 
 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
 #define argsdelimiter  " \t"
@@ -64,7 +66,7 @@ typedef enum {
 
 struct entry_parser {
        const char *field;
-       ail_error_e (*value_cb)(void *data, char *tag, char *value);
+       ail_error_e (*value_cb)(void *data, char *tag, char *value, uid_t uid);
 };
 
 inline static char *_ltrim(char *str)
@@ -129,7 +131,7 @@ typedef struct {
 
 
 
-static ail_error_e _read_exec(void *data, char *tag, char *value)
+static ail_error_e _read_exec(void *data, char *tag, char *value, uid_t uid)
 {
        desktop_info_s *info = data;
        char *token_exe_path;
@@ -165,7 +167,7 @@ static ail_error_e _read_exec(void *data, char *tag, char *value)
 
 
 
-static ail_error_e _read_name(void *data, char *tag, char *value)
+static ail_error_e _read_name(void *data, char *tag, char *value, uid_t uid)
 {
        desktop_info_s *info = data;
 
@@ -206,7 +208,7 @@ static ail_error_e _read_name(void *data, char *tag, char *value)
 
 
 
-static ail_error_e _read_type(void *data, char *tag, char *value)
+static ail_error_e _read_type(void *data, char *tag, char *value, uid_t uid)
 {
        desktop_info_s *info = data;
 
@@ -251,13 +253,14 @@ _get_icon_with_path(char* icon, uid_t uid)
                char* theme = NULL;
                char* icon_with_path = NULL;
                int len;
+               char *app_path = NULL;
 
                package = _get_package_from_icon(icon);
                retv_if(!package, NULL);
 
 /* "db/setting/theme" is not exist */
 #if 0
-               theme = vconf_get_str("db/setting/theme");
+               theme = ail_vconf_get_str("db/setting/theme");
                if (!theme) {
                        theme = strdup("default");
                        if(!theme) {
@@ -272,44 +275,28 @@ _get_icon_with_path(char* icon, uid_t uid)
                len = (0x01 << 7) + strlen(icon) + strlen(package) + strlen(theme);
                icon_with_path = malloc(len);
                if(icon_with_path == NULL) {
-                       _E("(icon_with_path == NULL) return\n");
+                       _E("icon_with_path == NULL\n");
                        free(package);
                        free(theme);
                        return NULL;
                }
 
                memset(icon_with_path, 0, len);
-
-               sqlite3_snprintf( len, icon_with_path,"%s/%q/small/%q", ail_get_icon_path(uid), theme, icon);
-               do {
-                       if (access(icon_with_path, R_OK) == 0) break;
-                       sqlite3_snprintf( len, icon_with_path,"%s/%q/small/%q", tzplatform_getenv(TZ_SYS_RO_ICONS), theme, icon);
-                       if (access(icon_with_path, R_OK) == 0) break;
-                       _D("cannot find icon %s", icon_with_path);
-                       sqlite3_snprintf( len, icon_with_path, "%s/default/small/%q", ail_get_icon_path(uid), icon);
-                       if (access(icon_with_path, R_OK) == 0) break;
-                       sqlite3_snprintf( len, icon_with_path, "%s/default/small/%q", tzplatform_getenv(TZ_SYS_RO_ICONS), icon);
-                       if (access(icon_with_path, R_OK) == 0) break;
-
-                       #if 1 /* this will be remove when finish the work for moving icon path */
-                       _E("icon file must be moved to %s", icon_with_path);
-                       sqlite3_snprintf( len, icon_with_path,  "%s/%q/res/icons/%q/small/%q", tzplatform_getenv(TZ_SYS_RW_APP), package, theme, icon);
-                       if (access(icon_with_path, R_OK) == 0) break;
-                       sqlite3_snprintf( len, icon_with_path, "%s/%q/res/icons/%q/small/%q", tzplatform_getenv(TZ_SYS_RO_APP), package, theme, icon);
-                       if (access(icon_with_path, R_OK) == 0) break;
-                       _D("cannot find icon %s", icon_with_path);
-                       sqlite3_snprintf( len, icon_with_path, "%s/%q/res/icons/default/small/%q", tzplatform_getenv(TZ_SYS_RW_APP), package, icon);
-                       if (access(icon_with_path, R_OK) == 0) break;
-                       sqlite3_snprintf( len, icon_with_path, "%s/%q/res/icons/default/small/%q", tzplatform_getenv(TZ_SYS_RO_APP), package, icon);
-                       if (access(icon_with_path, R_OK) == 0) break;
-                       #endif
-               } while (0);
-
+               if (uid != GLOBAL_USER)
+                       sqlite3_snprintf( len, icon_with_path, "%s%q", ail_get_icon_path(uid), icon);
+               else
+                       sqlite3_snprintf( len, icon_with_path, "%s/%q/small/%q", ail_get_icon_path(GLOBAL_USER), theme, icon);
+                       
+               if (access (icon_with_path, F_OK)) {
+                       app_path = tzplatform_getenv(TZ_SYS_RW_APP);
+                       if (app_path)
+                               sqlite3_snprintf( len, icon_with_path, "%s/%q/res/icons/%q/small/%q",app_path, package, theme, icon);
+                       if (access (icon_with_path, F_OK))
+                               _E("Cannot find icon path");
+               }
                free(theme);
                free(package);
-
-               _D("Icon path : %s ---> %s", icon, icon_with_path);
-
+               _D("Icon path : %s", icon_with_path);
                return icon_with_path;
        } else {
                char* confirmed_icon = NULL;
@@ -337,7 +324,7 @@ static ail_error_e _read_icon(void *data, char *tag, char *value, uid_t uid)
 
 
 
-static ail_error_e _read_categories(void *data, char *tag, char *value)
+static ail_error_e _read_categories(void *data, char *tag, char *value, uid_t uid)
 {
        desktop_info_s *info = data;
 
@@ -352,7 +339,7 @@ static ail_error_e _read_categories(void *data, char *tag, char *value)
 
 
 
-static ail_error_e _read_version(void *data, char *tag, char *value)
+static ail_error_e _read_version(void *data, char *tag, char *value, uid_t uid)
 {
        desktop_info_s *info = data;
 
@@ -367,7 +354,7 @@ static ail_error_e _read_version(void *data, char *tag, char *value)
 
 
 
-static ail_error_e _read_mimetype(void *data, char *tag, char *value)
+static ail_error_e _read_mimetype(void *data, char *tag, char *value, uid_t uid)
 {
        desktop_info_s *info = data;
        int size, total_len = 0;
@@ -429,7 +416,7 @@ static ail_error_e _read_mimetype(void *data, char *tag, char *value)
 
 
 
-static ail_error_e _read_nodisplay(void *data, char *tag, char *value)
+static ail_error_e _read_nodisplay(void *data, char *tag, char *value, uid_t uid)
 {
        desktop_info_s* info = data;
 
@@ -443,7 +430,7 @@ static ail_error_e _read_nodisplay(void *data, char *tag, char *value)
 
 
 
-static ail_error_e _read_x_slp_service(void *data, char *tag, char *value)
+static ail_error_e _read_x_slp_service(void *data, char *tag, char *value, uid_t uid)
 {
        desktop_info_s *info = data;
 
@@ -458,7 +445,7 @@ static ail_error_e _read_x_slp_service(void *data, char *tag, char *value)
 
 
 
-static ail_error_e _read_x_slp_packagetype(void *data, char *tag, char *value)
+static ail_error_e _read_x_slp_packagetype(void *data, char *tag, char *value, uid_t uid)
 {
        desktop_info_s *info = data;
 
@@ -473,7 +460,7 @@ static ail_error_e _read_x_slp_packagetype(void *data, char *tag, char *value)
 
 
 
-static ail_error_e _read_x_slp_packagecategories(void *data, char *tag, char *value)
+static ail_error_e _read_x_slp_packagecategories(void *data, char *tag, char *value, uid_t uid)
 {
        desktop_info_s *info = data;
 
@@ -488,7 +475,7 @@ static ail_error_e _read_x_slp_packagecategories(void *data, char *tag, char *va
 
 
 
-static ail_error_e _read_x_slp_packageid(void *data, char *tag, char *value)
+static ail_error_e _read_x_slp_packageid(void *data, char *tag, char *value, uid_t uid)
 {
        desktop_info_s *info = data;
 
@@ -501,7 +488,7 @@ static ail_error_e _read_x_slp_packageid(void *data, char *tag, char *value)
        return AIL_ERROR_OK;
 }
 
-static ail_error_e _read_x_slp_submodemainid(void *data, char *tag, char *value)
+static ail_error_e _read_x_slp_submodemainid(void *data, char *tag, char *value, uid_t uid)
 {
        desktop_info_s *info = data;
 
@@ -514,7 +501,7 @@ static ail_error_e _read_x_slp_submodemainid(void *data, char *tag, char *value)
        return AIL_ERROR_OK;
 }
 
-static ail_error_e _read_x_slp_installedstorage(void *data, char *tag, char *value)
+static ail_error_e _read_x_slp_installedstorage(void *data, char *tag, char *value, uid_t uid)
 {
        desktop_info_s *info = data;
 
@@ -527,7 +514,7 @@ static ail_error_e _read_x_slp_installedstorage(void *data, char *tag, char *val
        return AIL_ERROR_OK;
 }
 
-static ail_error_e _read_x_slp_uri(void *data, char *tag, char *value)
+static ail_error_e _read_x_slp_uri(void *data, char *tag, char *value, uid_t uid)
 {
        desktop_info_s *info = data;
 
@@ -542,7 +529,7 @@ static ail_error_e _read_x_slp_uri(void *data, char *tag, char *value)
 
 
 
-static ail_error_e _read_x_slp_svc(void *data, char *tag, char *value)
+static ail_error_e _read_x_slp_svc(void *data, char *tag, char *value, uid_t uid)
 {
        desktop_info_s *info = data;
 
@@ -557,7 +544,7 @@ static ail_error_e _read_x_slp_svc(void *data, char *tag, char *value)
 
 
 
-static ail_error_e _read_x_slp_taskmanage(void *data, char *tag, char *value)
+static ail_error_e _read_x_slp_taskmanage(void *data, char *tag, char *value, uid_t uid)
 {
        desktop_info_s *info = data;
 
@@ -571,7 +558,7 @@ static ail_error_e _read_x_slp_taskmanage(void *data, char *tag, char *value)
 
 
 
-static ail_error_e _read_x_slp_multiple(void *data, char *tag, char *value)
+static ail_error_e _read_x_slp_multiple(void *data, char *tag, char *value, uid_t uid)
 {
        desktop_info_s *info = data;
 
@@ -585,7 +572,7 @@ static ail_error_e _read_x_slp_multiple(void *data, char *tag, char *value)
 
 
 
-static ail_error_e _read_x_slp_removable(void *data, char *tag, char *value)
+static ail_error_e _read_x_slp_removable(void *data, char *tag, char *value, uid_t uid)
 {
        desktop_info_s *info = data;
 
@@ -598,7 +585,7 @@ static ail_error_e _read_x_slp_removable(void *data, char *tag, char *value)
 }
 
 
-static ail_error_e _read_x_slp_submode(void *data, char *tag, char *value)
+static ail_error_e _read_x_slp_submode(void *data, char *tag, char *value, uid_t uid)
 {
        desktop_info_s *info = data;
 
@@ -610,7 +597,7 @@ static ail_error_e _read_x_slp_submode(void *data, char *tag, char *value)
        return AIL_ERROR_OK;
 }
 
-static ail_error_e _read_x_slp_appid(void *data, char *tag, char *value)
+static ail_error_e _read_x_slp_appid(void *data, char *tag, char *value, uid_t uid)
 {
        desktop_info_s *info = data;
 
@@ -624,7 +611,7 @@ static ail_error_e _read_x_slp_appid(void *data, char *tag, char *value)
 }
 
 
-static ail_error_e _read_x_slp_pkgid(void *data, char *tag, char *value)
+static ail_error_e _read_x_slp_pkgid(void *data, char *tag, char *value, uid_t uid)
 {
        desktop_info_s *info = data;
 
@@ -638,7 +625,7 @@ static ail_error_e _read_x_slp_pkgid(void *data, char *tag, char *value)
 }
 
 
-static ail_error_e _read_x_slp_domain(void *data, char *tag, char *value)
+static ail_error_e _read_x_slp_domain(void *data, char *tag, char *value, uid_t uid)
 {
        desktop_info_s *info = data;
 
@@ -652,7 +639,7 @@ static ail_error_e _read_x_slp_domain(void *data, char *tag, char *value)
 }
 
 
-static ail_error_e _read_x_slp_enabled(void *data, char *tag, char *value)
+static ail_error_e _read_x_slp_enabled(void *data, char *tag, char *value, uid_t uid)
 {
        desktop_info_s *info = data;
 
@@ -802,46 +789,7 @@ char *_pkgname_to_desktop(const char *package, uid_t uid)
 
        retv_if(!package, NULL);
 
-       if(uid != GLOBAL_USER)
-    {
-    desktop_path = tzplatform_mkpath(TZ_USER_HOME, ".applications/desktop");
-    if(access(desktop_path, F_OK)) {
-               struct group *grpinfo = NULL;
-               const char *name = "users"; //to change
-               int ret;
-               char buf[BUFSIZE];
-               mkdir(desktop_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
-               grpinfo = getgrnam(name);
-               if(grpinfo == NULL)
-                       _E("getgrnam(users) returns NULL !");
-
-               ret = chown(desktop_path, uid, grpinfo->gr_gid);
-               if (ret == -1) {
-                       strerror_r(errno, buf, sizeof(buf));
-                       _E("FAIL : chown %s %d.%d, because %s", desktop_path, uid, grpinfo->gr_gid, buf);
-               }       
-       }
-  }
-  else
-  {
-    desktop_path = tzplatform_getenv(TZ_SYS_RW_DESKTOP_APP);
-    if(access(desktop_path, F_OK)) {
-               struct group *grpinfo = NULL;
-               const char *name = "root"; //to change
-               int ret;
-               char buf[BUFSIZE];
-               mkdir(desktop_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH);
-               grpinfo = getgrnam(name);
-               if(grpinfo == NULL)
-                       _E("getgrnam(users) returns NULL !");
-
-               ret = chown(desktop_path, uid, grpinfo->gr_gid);
-               if (ret == -1) {
-                       strerror_r(errno, buf, sizeof(buf));
-                       _E("FAIL : chown %s %d.%d, because %s", desktop_path, uid, grpinfo->gr_gid, buf);
-               }       
-       }
-  }
+  desktop_path = ail_get_desktop_path(uid);
 
        size = strlen(desktop_path) + strlen(package) + 10;
        desktop = malloc(size);
@@ -1036,7 +984,7 @@ static ail_error_e _init_desktop_info(desktop_info_s *info, const char *package,
 
 
 
-static ail_error_e _read_desktop_info(desktop_info_s* info)
+static ail_error_e _read_desktop_info(desktop_info_s* info,uid_t uid)
 {
        char *line = NULL;
        FILE *fp;
@@ -1079,7 +1027,7 @@ static ail_error_e _read_desktop_info(desktop_info_s* info)
 
                for (idx = 0; entry_parsers[idx].field; idx ++) {
                        if (!g_ascii_strcasecmp(entry_parsers[idx].field, field_name) && entry_parsers[idx].value_cb) {
-                               if (entry_parsers[idx].value_cb(info, tag, tmp) != AIL_ERROR_OK) {
+                               if (entry_parsers[idx].value_cb(info, tag, tmp,uid) != AIL_ERROR_OK) {
                                        _E("field - [%s] is wrong.", field_name);
                                }
                                break;
@@ -1186,9 +1134,13 @@ static ail_error_e _load_desktop_info(desktop_info_s* info, uid_t uid)
        do {
                ret = db_open(DB_OPEN_RO, uid);
                if (ret < 0) break;
-//is_admin
-               ret = db_prepare(query, &stmt);
-               //ret = db_prepare_globalro(query, &stmt);
+
+               if (uid != GLOBAL_USER) {
+                       ret = db_prepare(query, &stmt);
+               } else {
+                       ret = db_prepare_globalro(query, &stmt);
+               }
+
                if (ret < 0) break;
 
                ret = db_step(stmt);
@@ -1633,8 +1585,8 @@ static ail_error_e _send_db_done_noti(noti_type type, const char *package)
        retv_if(!noti_string, AIL_ERROR_OUT_OF_MEMORY);
 
        snprintf(noti_string, size + 1, "%s:%s:%u", type_string, package, getuid());
-       vconf_set_str(VCONFKEY_AIL_INFO_STATE, noti_string);
-       vconf_set_str(VCONFKEY_MENUSCREEN_DESKTOP, noti_string); // duplicate, will be removed
+       ail_vconf_set_str(VCONFKEY_AIL_INFO_STATE, noti_string);
+       ail_vconf_set_str(VCONFKEY_MENUSCREEN_DESKTOP, noti_string); // duplicate, will be removed
        _D("Noti : %s", noti_string);
 
        free(noti_string);
@@ -1688,7 +1640,7 @@ static void _fini_desktop_info(desktop_info_s *info)
 static int __is_authorized()
 {
        uid_t uid = getuid();
-       if ((uid_t) 0 == uid )
+       if ((uid_t) GLOBAL_USER == uid )
                return 1;
        else
                return 0;
@@ -1706,7 +1658,7 @@ EXPORT_API ail_error_e ail_usr_desktop_add(const char *appid, uid_t uid)
        ret = _init_desktop_info(&info, appid, uid);
        retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
 
-       ret = _read_desktop_info(&info);
+       ret = _read_desktop_info(&info,uid);
        retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
 
        ret = _insert_desktop_info(&info, uid);
@@ -1735,7 +1687,7 @@ EXPORT_API ail_error_e ail_usr_desktop_update(const char *appid, uid_t uid)
        ret = _init_desktop_info(&info, appid, uid);
        retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
 
-       ret = _read_desktop_info(&info);
+       ret = _read_desktop_info(&info,uid);
        retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
 
        ret = _update_desktop_info(&info, uid);
@@ -1805,7 +1757,7 @@ EXPORT_API ail_error_e ail_usr_desktop_fota(const char *appid, uid_t uid)
        ret = _init_desktop_info(&info, appid, uid);
        retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
 
-       ret = _read_desktop_info(&info);
+       ret = _read_desktop_info(&info,uid);
        retv_if(ret != AIL_ERROR_OK, AIL_ERROR_FAIL);
 
        ret = _insert_desktop_info(&info, uid);