Apply force option to trigger the updates forcely
[platform/framework/web/livebox-service.git] / src / livebox-service.c
index 0170aa3..279b874 100644 (file)
@@ -35,6 +35,7 @@
 #include <vconf.h>
 #include <vconf-keys.h>
 #include <ail.h>
+#include <unicode/uloc.h>
 
 #include "dlist.h"
 #include "util.h"
@@ -81,12 +82,22 @@ static struct info {
        const char *conf_file;
        int init_count;
        int res_resolved;
+
+       const char *iso3lang;
+       char country[ULOC_COUNTRY_CAPACITY];
+       char *syslang;
+       int country_len;
 } s_info = {
        .handle = NULL,
        .dbfile = "/opt/dbspace/.livebox.db", 
        .conf_file = "/usr/share/data-provider-master/resolution.ini",
        .init_count = 0,
        .res_resolved = 0,
+
+       .iso3lang = NULL,
+       .country = { 0, },
+       .syslang = NULL,
+       .country_len = 0,
 };
 
 static inline int update_info(int width_type, int height_type, int width, int height)
@@ -170,8 +181,9 @@ static inline int update_from_file(void)
 
                switch (status) {
                case START:
-                       if (isspace(ch) || ch == EOF)
+                       if (isspace(ch) || ch == EOF) {
                                continue;
+                       }
 
                        if (ch == '#') {
                                status = COMMENT;
@@ -206,12 +218,14 @@ static inline int update_from_file(void)
                        buffer[idx++] = ch;
                        break;
                case TYPE_END:
-                       if (ch == '=')
+                       if (ch == '=') {
                                status = SIZE_START;
+                       }
                        break;
                case SIZE_START:
-                       if (isspace(ch) || ch == EOF)
+                       if (isspace(ch) || ch == EOF) {
                                continue;
+                       }
 
                        status = SIZE;
                        idx = 0;
@@ -238,19 +252,24 @@ static inline int update_from_file(void)
                        ungetc(ch, fp);
                        break;
                case ERROR:
-                       if (ch == '\n' || ch == '\r' || ch == '\f')
+                       if (ch == '\n' || ch == '\r' || ch == '\f') {
                                status = START;
+                       }
                        break;
                case COMMENT:
-                       if (ch == '\n' || ch == '\r' || ch == '\f')
+                       if (ch == '\n' || ch == '\r' || ch == '\f') {
                                status = START;
+                       }
                        break;
                default:
                        ErrPrint("Unknown status. couldn't be reach to here\n");
                        break;
                }
        } while (!feof(fp));
-       fclose(fp);
+
+       if (fclose(fp) != 0) {
+               ErrPrint("fclose: %s\n", strerror(errno));
+       }
 
        return NR_OF_SIZE_LIST - updated;
 }
@@ -267,8 +286,9 @@ static int update_resolution(void)
        unsigned int depth;
        register int i;
 
-       if (s_info.res_resolved)
+       if (s_info.res_resolved) {
                return LB_STATUS_SUCCESS;
+       }
 
        disp = XOpenDisplay(NULL);
        if (!disp) {
@@ -282,8 +302,9 @@ static int update_resolution(void)
                return LB_STATUS_ERROR_FAULT;
        }
 
-       if (update_from_file() == 0)
+       if (update_from_file() == 0) {
                DbgPrint("Resolution info is all updated by file\n");
+       }
 
        for (i = 0; i < NR_OF_SIZE_LIST; i++) {
                SIZE_LIST[i].w = (unsigned int)((double)SIZE_LIST[i].w * (double)width / 720.0f);
@@ -302,7 +323,7 @@ static sqlite3 *open_db(void)
        if (!s_info.handle) {
                int ret;
 
-               ret = db_util_open(s_info.dbfile, &handle, DB_UTIL_REGISTER_HOOK_METHOD);
+               ret = db_util_open_with_options(s_info.dbfile, &handle, SQLITE_OPEN_READONLY, NULL);
                if (ret != SQLITE_OK) {
                        ErrPrint("Failed to open a DB\n");
                        return NULL;
@@ -316,8 +337,9 @@ static sqlite3 *open_db(void)
 
 static inline __attribute__((always_inline)) void close_db(sqlite3 *handle)
 {
-       if (!s_info.handle)
+       if (!s_info.handle) {
                db_util_close(handle);
+       }
 }
 
 static inline int convert_size_from_type(enum livebox_size_type type, int *width, int *height)
@@ -368,8 +390,9 @@ static inline int convert_size_from_type(enum livebox_size_type type, int *width
                return LB_STATUS_ERROR_INVALID;
        }
 
-       if (update_resolution() < 0)
+       if (update_resolution() < 0) {
                ErrPrint("Failed to update resolution\n");
+       }
 
        *width = SIZE_LIST[idx].w;
        *height = SIZE_LIST[idx].h;
@@ -389,8 +412,9 @@ EAPI int livebox_service_change_period(const char *pkgname, const char *id, doub
        }
 
        uri = util_id_to_uri(id);
-       if (!uri)
+       if (!uri) {
                return LB_STATUS_ERROR_MEMORY;
+       }
 
        packet = packet_create("service_change_period", "ssd", pkgname, uri, period);
        free(uri);
@@ -416,7 +440,7 @@ EAPI int livebox_service_change_period(const char *pkgname, const char *id, doub
        return ret;
 }
 
-EAPI int livebox_service_trigger_update(const char *pkgname, const char *id, const char *cluster, const char *category, int force)
+EAPI int livebox_service_trigger_update_with_content(const char *pkgname, const char *id, const char *cluster, const char *category, const char *content, int force)
 {
        struct packet *packet;
        struct packet *result;
@@ -433,17 +457,28 @@ EAPI int livebox_service_trigger_update(const char *pkgname, const char *id, con
                return LB_STATUS_ERROR_CANCEL;
        }
 
-       uri = util_id_to_uri(id);
-       if (!uri)
-               return LB_STATUS_ERROR_MEMORY;
+       if (id) {
+               uri = util_id_to_uri(id);
+               if (!uri) {
+                       return LB_STATUS_ERROR_MEMORY;
+               }
+       } else {
+               uri = NULL;
+       }
 
-       if (!cluster)
+       if (!cluster) {
                cluster = "user,created";
+       }
 
-       if (!category)
+       if (!category) {
                category = "default";
+       }
 
-       packet = packet_create("service_update", "ssss", pkgname, uri, cluster, category);
+       packet = packet_create("service_update", "sssssi", pkgname, uri, cluster, category, content, force);
+       /*!
+        * \note
+        * "free" function accepts NULL
+        */
        free(uri);
        if (!packet) {
                ErrPrint("Failed to create a packet for service_update\n");
@@ -468,6 +503,11 @@ EAPI int livebox_service_trigger_update(const char *pkgname, const char *id, con
        return ret;
 }
 
+EAPI int livebox_service_trigger_update(const char *pkgname, const char *id, const char *cluster, const char *category, int force)
+{
+       return livebox_service_trigger_update_with_content(pkgname, id, cluster, category, NULL, force);
+}
+
 /*!
  * pkgid == Package Id (not the livebox id)
  */
@@ -540,11 +580,13 @@ EAPI int livebox_service_get_pkglist_item(struct pkglist_handle *handle, char **
        char *_appid = NULL;
        char *_pkgname = NULL;
 
-       if (!handle || handle->type != PKGLIST_TYPE_LB_LIST)
+       if (!handle || handle->type != PKGLIST_TYPE_LB_LIST) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
-       if (sqlite3_step(handle->stmt) != SQLITE_ROW)
+       if (sqlite3_step(handle->stmt) != SQLITE_ROW) {
                return LB_STATUS_ERROR_NOT_EXIST;
+       }
 
        if (appid) {
                tmp = (const char *)sqlite3_column_text(handle->stmt, 0);
@@ -569,22 +611,26 @@ EAPI int livebox_service_get_pkglist_item(struct pkglist_handle *handle, char **
                }
        }
 
-       if (is_prime)
+       if (is_prime) {
                *is_prime = sqlite3_column_int(handle->stmt, 2);
+       }
 
-       if (appid)
+       if (appid) {
                *appid = _appid;
+       }
 
-       if (pkgname)
+       if (pkgname) {
                *pkgname = _pkgname;
+       }
 
        return LB_STATUS_SUCCESS;
 }
 
 EAPI int livebox_service_pkglist_destroy(struct pkglist_handle *handle)
 {
-       if (!handle || handle->type != PKGLIST_TYPE_LB_LIST)
+       if (!handle || handle->type != PKGLIST_TYPE_LB_LIST) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
        handle->type = PKGLIST_TYPE_UNKNOWN;
        sqlite3_reset(handle->stmt);
@@ -603,12 +649,14 @@ EAPI int livebox_service_get_pkglist(int (*cb)(const char *appid, const char *pk
        int is_prime;
        sqlite3 *handle;
 
-       if (!cb)
+       if (!cb) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
        handle = open_db();
-       if (!handle)
+       if (!handle) {
                return LB_STATUS_ERROR_IO;
+       }
 
        ret = sqlite3_prepare_v2(handle, "SELECT appid, pkgid, prime FROM pkgmap", -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
@@ -657,12 +705,14 @@ EAPI int livebox_service_get_pkglist_by_pkgid(const char *pkgid, int (*cb)(const
        int is_prime;
        sqlite3 *handle;
 
-       if (!cb)
+       if (!cb) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
        handle = open_db();
-       if (!handle)
+       if (!handle) {
                return LB_STATUS_ERROR_IO;
+       }
 
        ret = sqlite3_prepare_v2(handle, "SELECT pkgid, prime FROM pkgmap WHERE appid = ?", -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
@@ -719,10 +769,11 @@ static int pkgmgr_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
        int ret;
 
        ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
-       if (ret < 0)
+       if (ret < 0) {
                ErrPrint("Unable to get appid\n");
-       else
+       } else {
                cbdata->cb(cbdata->lbid, appid, cbdata->cbdata);
+       }
 
        return 0;
 }
@@ -766,8 +817,9 @@ static inline int pkgmgr_get_applist(const char *pkgid, const char *lbid, void (
        cbdata.cbdata = data;
 
        ret = pkgmgrinfo_appinfo_get_list(handle, PM_UI_APP, pkgmgr_cb, &cbdata);
-       if (ret < 0)
+       if (ret < 0) {
                ErrPrint("Failed to get applist\n");
+       }
 
        pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
        return ret;
@@ -781,12 +833,14 @@ EAPI int livebox_service_get_applist(const char *lbid, void (*cb)(const char *lb
        sqlite3 *handle;
        int ret;
 
-       if (!lbid || !cb)
+       if (!lbid || !cb) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
        handle = open_db();
-       if (!handle)
+       if (!handle) {
                return LB_STATUS_ERROR_IO;
+       }
 
        ret = sqlite3_prepare_v2(handle, "SELECT appid FROM pkgmap WHERE (pkgid = ?) or (appid = ?)", -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
@@ -840,6 +894,19 @@ EAPI int livebox_service_get_applist(const char *lbid, void (*cb)(const char *lb
        ret = pkgmgr_get_applist(pkgid, lbid, cb, data);
        free(pkgid);
 
+       switch (ret) {
+       case PMINFO_R_EINVAL:
+               ret = LB_STATUS_ERROR_INVALID;
+               break;
+       case PMINFO_R_OK:
+               ret = LB_STATUS_SUCCESS;
+               break;
+       case PMINFO_R_ERROR:
+       default:
+               ret = LB_STATUS_ERROR_FAULT;
+               break;
+       }
+
 out:
        close_db(handle);
        return ret;
@@ -853,12 +920,14 @@ EAPI char *livebox_service_mainappid(const char *lbid)
        sqlite3 *handle;
        char *ret = NULL;
 
-       if (!lbid)
+       if (!lbid) {
                return NULL;
+       }
 
        handle = open_db();
-       if (!handle)
+       if (!handle) {
                return NULL;
+       }
 
        if (sqlite3_prepare_v2(handle, "SELECT appid, uiapp FROM pkgmap WHERE (pkgid = ?) or (appid = ? and prime = 1)", -1, &stmt, NULL) != SQLITE_OK) {
                ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
@@ -898,8 +967,9 @@ EAPI char *livebox_service_mainappid(const char *lbid)
                ret = pkgmgr_get_mainapp(tmp);
        } else {
                ret = strdup(pkgid);
-               if (!ret)
+               if (!ret) {
                        ErrPrint("Error: %s\n", strerror(errno));
+               }
        }
 
        sqlite3_reset(stmt);
@@ -917,12 +987,14 @@ EAPI int livebox_service_get_supported_size_types(const char *pkgid, int *cnt, i
        int size;
        int ret;
 
-       if (!types || !cnt || !pkgid)
+       if (!types || !cnt || !pkgid) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
        handle = open_db();
-       if (!handle)
+       if (!handle) {
                return LB_STATUS_ERROR_IO;
+       }
 
        ret = sqlite3_prepare_v2(handle, "SELECT size_type FROM box_size WHERE pkgid = ? ORDER BY size_type ASC", -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
@@ -940,8 +1012,9 @@ EAPI int livebox_service_get_supported_size_types(const char *pkgid, int *cnt, i
                goto out;
        }
 
-       if (*cnt > NR_OF_SIZE_LIST)
+       if (*cnt > NR_OF_SIZE_LIST) {
                *cnt = NR_OF_SIZE_LIST;
+       }
 
        ret = 0;
        while (sqlite3_step(stmt) == SQLITE_ROW && ret < *cnt) {
@@ -953,7 +1026,7 @@ EAPI int livebox_service_get_supported_size_types(const char *pkgid, int *cnt, i
        *cnt = ret;
        sqlite3_reset(stmt);
        sqlite3_finalize(stmt);
-       ret = 0;
+       ret = LB_STATUS_SUCCESS;
 out:
        close_db(handle);
        return ret;
@@ -973,15 +1046,17 @@ static inline char *cur_locale(void)
                                break;
                        }
 
-                       if (*ptr == '_')
+                       if (*ptr == '_') {
                                *ptr = '-';
+                       }
 
                        ptr++;
                }
        } else {
                language = strdup("en-us");
-               if (!language)
+               if (!language) {
                        ErrPrint("Heap: %s\n", strerror(errno));
+               }
        }
 
        return language;
@@ -995,8 +1070,9 @@ static inline char *get_default_name(const char *pkgid)
        int ret;
 
        handle = open_db();
-       if (!handle)
+       if (!handle) {
                return NULL;
+       }
 
        ret = sqlite3_prepare_v2(handle, "SELECT name FROM client WHERE pkgid = ?", -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
@@ -1018,8 +1094,9 @@ static inline char *get_default_name(const char *pkgid)
                tmp = (const char *)sqlite3_column_text(stmt, 0);
                if (tmp && strlen(tmp)) {
                        name = strdup(tmp);
-                       if (!name)
+                       if (!name) {
                                ErrPrint("Heap: %s\n", strerror(errno));
+                       }
                }
        }
 
@@ -1038,8 +1115,9 @@ static inline char *get_default_icon(const char *pkgid)
        int ret;
 
        handle = open_db();
-       if (!handle)
+       if (!handle) {
                return NULL;
+       }
 
        ret = sqlite3_prepare_v2(handle, "SELECT icon FROM client WHERE pkgid = ?", -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
@@ -1061,8 +1139,9 @@ static inline char *get_default_icon(const char *pkgid)
                tmp = (const char *)sqlite3_column_text(stmt, 0);
                if (tmp && strlen(tmp)) {
                        icon = strdup(tmp);
-                       if (!icon)
+                       if (!icon) {
                                ErrPrint("Heap: %s\n", strerror(errno));
+                       }
                }
        }
 
@@ -1081,8 +1160,9 @@ EAPI char *livebox_service_content(const char *pkgid)
        int ret;
 
        handle = open_db();
-       if (!handle)
+       if (!handle) {
                return NULL;
+       }
 
        ret = sqlite3_prepare_v2(handle, "SELECT content FROM client WHERE pkgid = ?", -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
@@ -1104,8 +1184,9 @@ EAPI char *livebox_service_content(const char *pkgid)
                tmp = (const char *)sqlite3_column_text(stmt, 0);
                if (tmp && strlen(tmp)) {
                        content = strdup(tmp);
-                       if (!content)
+                       if (!content) {
                                ErrPrint("Heap: %s\n", strerror(errno));
+                       }
                }
        }
 
@@ -1124,8 +1205,9 @@ EAPI char *livebox_service_setup_appid(const char *lbid)
        char *appid;
 
        handle = open_db();
-       if (!handle)
+       if (!handle) {
                return NULL;
+       }
 
        ret = sqlite3_prepare_v2(handle, "SELECT setup FROM client WHERE pkgid = ?", -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
@@ -1146,12 +1228,14 @@ EAPI char *livebox_service_setup_appid(const char *lbid)
                const char *tmp;
 
                tmp = (const char *)sqlite3_column_text(stmt, 0);
-               if (!tmp || !strlen(tmp))
+               if (!tmp || !strlen(tmp)) {
                        goto out;
+               }
 
                appid = strdup(tmp);
-               if (!appid)
+               if (!appid) {
                        ErrPrint("Error: %s\n", strerror(errno));
+               }
        }
 
 out:
@@ -1168,8 +1252,9 @@ EAPI int livebox_service_nodisplay(const char *pkgid)
        int ret;
 
        handle = open_db();
-       if (!handle)
+       if (!handle) {
                return 0;
+       }
 
        ret = sqlite3_prepare_v2(handle, "SELECT nodisplay FROM client WHERE pkgid = ?", -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
@@ -1186,10 +1271,11 @@ EAPI int livebox_service_nodisplay(const char *pkgid)
        }
 
        ret = sqlite3_step(stmt);
-       if (ret == SQLITE_ROW)
+       if (ret == SQLITE_ROW) {
                ret = !!sqlite3_column_int(stmt, 0);
-       else
+       } else {
                ret = 0;
+       }
 
 out:
        sqlite3_reset(stmt);
@@ -1206,13 +1292,15 @@ static inline char *get_lb_pkgname_by_appid(const char *appid)
        sqlite3 *handle;
        int ret;
 
-       if (!appid)
+       if (!appid) {
                return NULL;
+       }
 
        pkgid = NULL;
        handle = open_db();
-       if (!handle)
+       if (!handle) {
                return NULL;
+       }
 
        ret = sqlite3_prepare_v2(handle, "SELECT pkgid FROM pkgmap WHERE (appid = ? AND prime = 1) OR pkgid = ?", -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
@@ -1241,8 +1329,9 @@ static inline char *get_lb_pkgname_by_appid(const char *appid)
        tmp = (char *)sqlite3_column_text(stmt, 0);
        if (tmp && strlen(tmp)) {
                pkgid = strdup(tmp);
-               if (!pkgid)
+               if (!pkgid) {
                        ErrPrint("Heap: %s\n", strerror(errno));
+               }
        }
 
 out:
@@ -1381,8 +1470,9 @@ EAPI int livebox_service_mouse_event(const char *pkgid)
        int ret;
 
        handle = open_db();
-       if (!handle)
+       if (!handle) {
                return 0;
+       }
 
        ret = sqlite3_prepare_v2(handle, "SELECT mouse_event FROM client WHERE pkgid = ?", -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
@@ -1421,44 +1511,135 @@ out:
        return ret;
 }
 
+static inline int update_lang_info(void)
+{
+       char *syslang;
+       UErrorCode err;
+
+       syslang = vconf_get_str(VCONFKEY_LANGSET);
+       if (!syslang) {
+               ErrPrint("Failed to get vconf-lang\n");
+               return -EFAULT;
+       }
+
+       if (s_info.syslang && !strcmp(s_info.syslang, syslang)) {
+               DbgPrint("Syslang is not changed: %s\n", syslang);
+               free(syslang);
+               return 0;
+       }
+
+       free(s_info.syslang);
+       s_info.syslang = syslang;
+
+       err = U_ZERO_ERROR;
+       uloc_setDefault((const char *)s_info.syslang, &err);
+       if (!U_SUCCESS(err)) {
+               ErrPrint("Failed to set default lang: %s\n", u_errorName(err));
+               free(s_info.syslang);
+               s_info.syslang = NULL;
+               return -EFAULT;
+       }
+
+       s_info.iso3lang = uloc_getISO3Language(uloc_getDefault());
+       if (!s_info.iso3lang || !strlen(s_info.iso3lang)) {
+               ErrPrint("Failed to get iso3lang\n");
+               free(s_info.syslang);
+               s_info.syslang = NULL;
+               return -EFAULT;
+       }
+
+       err = U_ZERO_ERROR;
+       s_info.country_len = uloc_getCountry(uloc_getDefault(), s_info.country, ULOC_COUNTRY_CAPACITY, &err);
+       if (!U_SUCCESS(err) || s_info.country_len <= 0) {
+               ErrPrint("Failed to get locale: %s, %s, %d (%s)\n", u_errorName(err), s_info.iso3lang, s_info.country_len, s_info.country);
+               free(s_info.syslang);
+               s_info.syslang = NULL;
+               return -EFAULT;
+       }
+
+       return 0;
+}
+
 EAPI char *livebox_service_preview(const char *pkgid, int size_type)
 {
        sqlite3_stmt *stmt;
        sqlite3 *handle;
        int ret;
        char *preview = NULL;
+       const char *tmp;
+       int tmp_len;
+       int buf_len;
+       register int i;
+       int printed;
 
        handle = open_db();
-       if (!handle)
+       if (!handle) {
                return NULL;
+       }
 
        ret = sqlite3_prepare_v2(handle, "SELECT preview FROM box_size WHERE pkgid = ? AND size_type = ?", -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
-               ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
+               ErrPrint("Error: %s, %s\n", sqlite3_errmsg(handle), pkgid);
                close_db(handle);
                return NULL;
        }
 
        ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
        if (ret != SQLITE_OK) {
-               ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
+               ErrPrint("Error: %s, %s\n", sqlite3_errmsg(handle), pkgid);
                goto out;
        }
 
        ret = sqlite3_bind_int(stmt, 2, size_type);
        if (ret != SQLITE_OK) {
-               ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
+               ErrPrint("Error: %s, %s\n", sqlite3_errmsg(handle), pkgid);
                goto out;
        }
 
        ret = sqlite3_step(stmt);
-       if (ret == SQLITE_ROW) {
-               const char *tmp;
-               tmp = (const char *)sqlite3_column_text(stmt, 0);
-               if (tmp && strlen(tmp)) {
-                       preview = strdup(tmp);
-                       if (!preview)
-                               ErrPrint("Heap: %s\n", strerror(errno));
+       if (ret != SQLITE_ROW) {
+               ErrPrint("Error: %s, %s\n", sqlite3_errmsg(handle), pkgid);
+               goto out;
+       }
+
+       tmp = (const char *)sqlite3_column_text(stmt, 0);
+       if (!tmp || !(tmp_len = strlen(tmp))) {
+               ErrPrint("Failed to get data (%s)\n", pkgid);
+               goto out;
+       }
+
+       if (update_lang_info() != 0) {
+               preview = strdup(tmp);
+               if (!preview) {
+                       ErrPrint("Heap: %s\n", strerror(errno));
+               }
+               goto out;
+       }
+
+       buf_len = tmp_len + strlen(s_info.iso3lang) + s_info.country_len + 3; /* '/' '-' '/' */
+       preview = malloc(buf_len + 1);
+       if (!preview) {
+               ErrPrint("Heap: %s\n", strerror(errno));
+               goto out;
+       }
+
+       for (i = tmp_len; i >= 0 && tmp[i] != '/'; i--);
+       i++; /* Skip '/' */
+
+       strncpy(preview, tmp, i);
+       printed = snprintf(preview + i, buf_len - i, "%s-%s/%s", s_info.iso3lang, s_info.country, tmp + i);
+       if (preview[i + printed] != '\0') {
+               ErrPrint("Path is truncated\n");
+               preview[i + printed] = '\0';
+       }
+
+       if (access(preview, R_OK) != 0) {
+               DbgPrint("Access failed: %s, %s\n", preview, strerror(errno));
+               free(preview);
+
+               preview = strdup(tmp);
+               if (!preview) {
+                       ErrPrint("Heap: %s\n", strerror(errno));
                }
        }
 
@@ -1485,8 +1666,9 @@ EAPI char *livebox_service_i18n_icon(const char *pkgid, const char *lang)
                }
        } else {
                language = cur_locale();
-               if (!language)
+               if (!language) {
                        return NULL;
+               }
        }
 
        handle = open_db();
@@ -1523,8 +1705,9 @@ EAPI char *livebox_service_i18n_icon(const char *pkgid, const char *lang)
                        icon = get_default_icon(pkgid);
                } else {
                        icon = strdup(tmp);
-                       if (!icon)
+                       if (!icon) {
                                ErrPrint("Heap: %s\n", strerror(errno));
+                       }
                }
        } else {
                icon = get_default_icon(pkgid);
@@ -1554,8 +1737,9 @@ EAPI char *livebox_service_i18n_name(const char *pkgid, const char *lang)
                }
        } else {
                language = cur_locale();
-               if (!language)
+               if (!language) {
                        return NULL;
+               }
        }
 
        handle = open_db();
@@ -1592,8 +1776,9 @@ EAPI char *livebox_service_i18n_name(const char *pkgid, const char *lang)
                        name = get_default_name(pkgid);
                } else {
                        name = strdup(tmp);
-                       if (!name)
+                       if (!name) {
                                ErrPrint("Heap: %s\n", strerror(errno));
+                       }
                }
        } else {
                name = get_default_name(pkgid);
@@ -1614,12 +1799,14 @@ EAPI int livebox_service_get_supported_sizes(const char *pkgid, int *cnt, int *w
        int size;
        int ret;
 
-       if (!w || !h || !cnt || !pkgid)
+       if (!w || !h || !cnt || !pkgid) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
        handle = open_db();
-       if (!handle)
+       if (!handle) {
                return LB_STATUS_ERROR_IO;
+       }
 
        ret = sqlite3_prepare_v2(handle, "SELECT size_type FROM box_size WHERE pkgid = ? ORDER BY size_type ASC", -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
@@ -1637,8 +1824,9 @@ EAPI int livebox_service_get_supported_sizes(const char *pkgid, int *cnt, int *w
                goto out;
        }
 
-       if (*cnt > NR_OF_SIZE_LIST)
+       if (*cnt > NR_OF_SIZE_LIST) {
                *cnt = NR_OF_SIZE_LIST;
+       }
 
        ret = 0;
        while (sqlite3_step(stmt) == SQLITE_ROW && ret < *cnt) {
@@ -1664,13 +1852,15 @@ EAPI char *livebox_service_libexec(const char *pkgid)
        char *appid;
        char *path;
 
-       if (!pkgid)
+       if (!pkgid) {
                return NULL;
+       }
 
        libexec = NULL;
        handle = open_db();
-       if (!handle)
+       if (!handle) {
                return NULL;
+       }
 
        ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.libexec FROM pkgmap, provider WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
@@ -1742,12 +1932,14 @@ EAPI char *livebox_service_pkgname(const char *appid)
        int ret;
        char *new_appid;
 
-       if (!appid)
+       if (!appid) {
                return NULL;
+       }
 
        lb_pkgname = get_lb_pkgname_by_appid(appid);
-       if (lb_pkgname)
+       if (lb_pkgname) {
                return lb_pkgname;
+       }
 
        /*!
         * \note
@@ -1769,8 +1961,9 @@ EAPI char *livebox_service_pkgname(const char *appid)
        lb_pkgname = get_lb_pkgname_by_appid(new_appid);
        pkgmgr_appinfo_destroy_appinfo(handle);
 
-       if (!lb_pkgname && util_validate_livebox_package(appid) == 0)
+       if (!lb_pkgname && util_validate_livebox_package(appid) == 0) {
                return strdup(appid);
+       }
 
        return lb_pkgname;
 }
@@ -1783,8 +1976,9 @@ EAPI char *livebox_service_provider_name(const char *lbid)
        int idx = 0;
        char *str = SAMSUNG_PREFIX;
 
-       if (!lbid)
+       if (!lbid) {
                return NULL;
+       }
 
        while (str[idx] && lbid[idx] && lbid[idx] == str[idx]) {
                idx++;
@@ -1857,12 +2051,14 @@ EAPI int livebox_service_is_primary(const char *lbid)
        sqlite3 *handle;
        int ret = 0;
 
-       if (!lbid)
+       if (!lbid) {
                return 0;
+       }
 
        handle = open_db();
-       if (!handle)
+       if (!handle) {
                return 0;
+       }
 
        ret = sqlite3_prepare_v2(handle, "SELECT prime FROM pkgmap WHERE pkgid = ?", -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
@@ -1892,10 +2088,6 @@ out:
        return ret;
 }
 
-/*!
- * appid == Package ID
- * pkgid == Livebox ID
- */
 EAPI char *livebox_service_appid(const char *pkgname)
 {
        sqlite3_stmt *stmt;
@@ -1905,13 +2097,15 @@ EAPI char *livebox_service_appid(const char *pkgname)
        int is_prime __attribute__((__unused__));
        int ret;
 
-       if (!pkgname)
+       if (!pkgname) {
                return NULL;
+       }
 
        appid = NULL;
        handle = open_db();
-       if (!handle)
+       if (!handle) {
                return NULL;
+       }
 
        ret = sqlite3_prepare_v2(handle, "SELECT appid, prime FROM pkgmap WHERE pkgid = ? OR appid = ?", -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
@@ -1958,8 +2152,9 @@ EAPI char *livebox_service_appid(const char *pkgname)
                }
 
                appid = strdup(new_appid);
-               if (!appid)
+               if (!appid) {
                        ErrPrint("Heap: %s\n", strerror(errno));
+               }
 
                pkgmgr_appinfo_destroy_appinfo(pkg_handle);
                goto out;
@@ -1999,37 +2194,39 @@ EAPI char *livebox_service_lb_script_path(const char *pkgid)
        char *appid;
        char *lb_src;
 
-       if (!pkgid)
+       if (!pkgid) {
                return NULL;
+       }
 
        path = NULL;
        handle = open_db();
-       if (!handle)
+       if (!handle) {
                return NULL;
+       }
 
        ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.box_src FROM provider, pkgmap WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
-               ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
+               ErrPrint("Error: %s, pkgid(%s), ret(%d)\n", sqlite3_errmsg(handle), pkgid, ret);
                goto out;
        }
 
        ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
        if (ret != SQLITE_OK) {
-               ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
+               ErrPrint("Error: %s, pkgid(%s), ret(%d)\n", sqlite3_errmsg(handle), pkgid, ret);
                sqlite3_finalize(stmt);
                goto out;
        }
 
        ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
        if (ret != SQLITE_OK) {
-               ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
+               ErrPrint("Error: %s, pkgid(%s), ret(%d)\n", sqlite3_errmsg(handle), pkgid, ret);
                sqlite3_finalize(stmt);
                goto out;
        }
 
        ret = sqlite3_step(stmt);
        if (ret != SQLITE_ROW) {
-               ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
+               ErrPrint("Error: %s, pkgid(%s), ret(%d)\n", sqlite3_errmsg(handle), pkgid, ret);
                sqlite3_reset(stmt);
                sqlite3_finalize(stmt);
                goto out;
@@ -2037,7 +2234,7 @@ EAPI char *livebox_service_lb_script_path(const char *pkgid)
 
        appid = (char *)sqlite3_column_text(stmt, 0);
        if (!appid || !strlen(appid)) {
-               ErrPrint("Invalid appid : %s\n", sqlite3_errmsg(handle));
+               ErrPrint("Invalid appid : %s, pkgid(%s)\n", sqlite3_errmsg(handle), pkgid);
                sqlite3_reset(stmt);
                sqlite3_finalize(stmt);
                goto out;
@@ -2045,7 +2242,7 @@ EAPI char *livebox_service_lb_script_path(const char *pkgid)
 
        lb_src = (char *)sqlite3_column_text(stmt, 1);
        if (!lb_src || !strlen(lb_src)) {
-               ErrPrint("No records for lb src : %s\n", sqlite3_errmsg(handle));
+               ErrPrint("No records for lb src : %s, pkgid(%s), appid(%s)\n", sqlite3_errmsg(handle), pkgid, appid);
                sqlite3_reset(stmt);
                sqlite3_finalize(stmt);
                goto out;
@@ -2075,13 +2272,15 @@ EAPI char *livebox_service_lb_script_group(const char *pkgid)
        char *group;
        char *tmp;
 
-       if (!pkgid)
+       if (!pkgid) {
                return NULL;
+       }
 
        group = NULL;
        handle = open_db();
-       if (!handle)
+       if (!handle) {
                return NULL;
+       }
 
        ret = sqlite3_prepare_v2(handle, "SELECT box_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
@@ -2107,8 +2306,9 @@ EAPI char *livebox_service_lb_script_group(const char *pkgid)
        tmp = (char *)sqlite3_column_text(stmt, 0);
        if (tmp && strlen(tmp)) {
                group = strdup(tmp);
-               if (!group)
+               if (!group) {
                        ErrPrint("Heap: %s\n", strerror(errno));
+               }
        }
 
        sqlite3_reset(stmt);
@@ -2127,37 +2327,39 @@ EAPI char *livebox_service_pd_script_path(const char *pkgid)
        char *pd_src;
        const char *appid;
 
-       if (!pkgid)
+       if (!pkgid) {
                return NULL;
+       }
 
        path = NULL;
        handle = open_db();
-       if (!handle)
+       if (!handle) {
                return NULL;
+       }
 
        ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.pd_src FROM provider, pkgmap WHERE provider.pkgid = ? AND pkgmap.pkgid = ?", -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
-               ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
+               ErrPrint("Error: %s pkgid(%s) ret(%d)\n", sqlite3_errmsg(handle), pkgid, ret);
                goto out;
        }
 
        ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
        if (ret != SQLITE_OK) {
-               ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
+               ErrPrint("Error: %s pkgid(%s) ret(%d)\n", sqlite3_errmsg(handle), pkgid, ret);
                sqlite3_finalize(stmt);
                goto out;
        }
 
        ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
        if (ret != SQLITE_OK) {
-               ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
+               ErrPrint("Error: %s pkgid(%s) ret(%d)\n", sqlite3_errmsg(handle), pkgid, ret);
                sqlite3_finalize(stmt);
                goto out;
        }
 
        ret = sqlite3_step(stmt);
        if (ret != SQLITE_ROW) {
-               ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
+               ErrPrint("Error: %s pkgid(%s) ret(%d)\n", sqlite3_errmsg(handle), pkgid, ret);
                sqlite3_reset(stmt);
                sqlite3_finalize(stmt);
                goto out;
@@ -2165,7 +2367,7 @@ EAPI char *livebox_service_pd_script_path(const char *pkgid)
 
        appid = (char *)sqlite3_column_text(stmt, 0);
        if (!appid || !strlen(appid)) {
-               ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
+               ErrPrint("Error: %s pkgid(%s)\n", sqlite3_errmsg(handle), pkgid);
                sqlite3_reset(stmt);
                sqlite3_finalize(stmt);
                goto out;
@@ -2173,7 +2375,7 @@ EAPI char *livebox_service_pd_script_path(const char *pkgid)
 
        pd_src = (char *)sqlite3_column_text(stmt, 1);
        if (!pd_src || !strlen(pd_src)) {
-               ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
+               ErrPrint("Error: %s pkgid(%s) appid(%s)\n", sqlite3_errmsg(handle), pkgid, appid);
                sqlite3_reset(stmt);
                sqlite3_finalize(stmt);
                goto out;
@@ -2203,13 +2405,15 @@ EAPI char *livebox_service_pd_script_group(const char *pkgid)
        char *group;
        char *tmp;
 
-       if (!pkgid)
+       if (!pkgid) {
                return NULL;
+       }
 
        group = NULL;
        handle = open_db();
-       if (!handle)
+       if (!handle) {
                return NULL;
+       }
 
        ret = sqlite3_prepare_v2(handle, "SELECT pd_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
@@ -2235,8 +2439,9 @@ EAPI char *livebox_service_pd_script_group(const char *pkgid)
        tmp = (char *)sqlite3_column_text(stmt, 0);
        if (tmp && strlen(tmp)) {
                group = strdup(tmp);
-               if (!group)
+               if (!group) {
                        ErrPrint("Heap: %s\n", strerror(errno));
+               }
        }
        sqlite3_reset(stmt);
        sqlite3_finalize(stmt);
@@ -2253,12 +2458,14 @@ EAPI int livebox_service_enumerate_cluster_list(int (*cb)(const char *cluster, v
        int cnt;
        int ret;
 
-       if (!cb)
+       if (!cb) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
        handle = open_db();
-       if (!handle)
+       if (!handle) {
                return LB_STATUS_ERROR_IO;
+       }
 
        cnt = 0;
        ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT cluster FROM groupinfo", -1, &stmt, NULL);
@@ -2270,11 +2477,13 @@ EAPI int livebox_service_enumerate_cluster_list(int (*cb)(const char *cluster, v
 
        while (sqlite3_step(stmt) == SQLITE_ROW) {
                cluster = (const char *)sqlite3_column_text(stmt, 0);
-               if (!cluster || !strlen(cluster))
+               if (!cluster || !strlen(cluster)) {
                        continue;
+               }
 
-               if (cb(cluster, data) < 0)
+               if (cb(cluster, data) < 0) {
                        break;
+               }
 
                cnt++;
        }
@@ -2294,12 +2503,14 @@ EAPI int livebox_service_enumerate_category_list(const char *cluster, int (*cb)(
        int cnt;
        int ret;
 
-       if (!cluster || !cb)
+       if (!cluster || !cb) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
        handle = open_db();
-       if (!handle)
+       if (!handle) {
                return LB_STATUS_ERROR_IO;
+       }
 
        ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT category FROM groupinfo WHERE cluster = ?", -1, &stmt, NULL);
        if (ret != SQLITE_OK) {
@@ -2311,11 +2522,13 @@ EAPI int livebox_service_enumerate_category_list(const char *cluster, int (*cb)(
        cnt = 0;
        while (sqlite3_step(stmt) == SQLITE_ROW) {
                category = (const char *)sqlite3_column_text(stmt, 0);
-               if (!category || !strlen(category))
+               if (!category || !strlen(category)) {
                        continue;
+               }
 
-               if (cb(cluster, category, data) < 0)
+               if (cb(cluster, category, data) < 0) {
                        break;
+               }
 
                cnt++;
        }
@@ -2367,11 +2580,13 @@ EAPI int livebox_service_get_size(int type, int *width, int *height)
        int _width;
        int _height;
 
-       if (!width)
+       if (!width) {
                width = &_width;
+       }
 
-       if (!height)
+       if (!height) {
                height = &_height;
+       }
 
        return convert_size_from_type(type, width, height);
 }
@@ -2380,12 +2595,14 @@ EAPI int livebox_service_size_type(int width, int height)
 {
        int idx;
 
-       if (update_resolution() < 0)
+       if (update_resolution() < 0) {
                ErrPrint("Failed to update the size list\n");
+       }
 
        for (idx = 0; idx < NR_OF_SIZE_LIST; idx++) {
-               if (SIZE_LIST[idx].w == width && SIZE_LIST[idx].h == height)
+               if (SIZE_LIST[idx].w == width && SIZE_LIST[idx].h == height) {
                        break;
+               }
        }
 
        switch (idx) {