Fix log characters
[platform/core/uifw/tts.git] / engine-parser / src / tts-engine-parser.c
old mode 100755 (executable)
new mode 100644 (file)
index 7f514bc..97f5ca5
 #include <stdio.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <fcntl.h>
 #include <tzplatform_config.h>
+#include <systemd/sd-login.h>
+
+#include <gum/gum-user.h>
+#include <gum/gum-user-service.h>
+#include <gum/common/gum-user-types.h>
 
 /* Define EXPORT_API */
 #ifndef EXPORT_API
 #define TTS_TAG_ENGINE_VOICE_TYPE              "type"
 #define TTS_TAG_ENGINE_PITCH_SUPPORT           "pitch-support"
 #define TTS_TAG_ENGINE_CREDENTIAL              "credential"
+#define TTS_TAG_ENGINE_TEXT_SIZE               "text-size"
 
 #define TTS_CONFIG_BASE                tzplatform_mkpath(TZ_USER_HOME, "share/.voice")
 #define TTS_HOME               tzplatform_mkpath(TZ_USER_HOME, "share/.voice/tts")
 #define TTS_ENGINE_BASE                tzplatform_mkpath(TZ_USER_HOME, "share/.voice/tts/1.0")
 #define TTS_ENGINE_INFO                tzplatform_mkpath(TZ_USER_SHARE, ".voice/tts/1.0/engine-info")
 
+#define TTS_GLOBAL_CONFIG_BASE         "/etc/skel/share/.voice"
+#define TTS_GLOBAL_HOME                "/etc/skel/share/.voice/tts"
+#define TTS_GLOBAL_ENGINE_BASE         "/etc/skel/share/.voice/tts/1.0"
+#define TTS_GLOBAL_ENGINE_INFO         "/etc/skel/share/.voice/tts/1.0/engine-info"
+
+#define TTS_METADATA_NAME                      "http://tizen.org/metadata/tts-engine/name"
 #define TTS_METADATA_LANGUAGE                  "http://tizen.org/metadata/tts-engine/language"
 #define TTS_METADATA_CREDENTIAL_REQUIRED       "http://tizen.org/metadata/tts-engine/credential-required"
+#define TTS_METADATA_SETTING           "http://tizen.org/metadata/tts-engine/setting"
+#define TTS_METADATA_TEXT_SIZE         "http://tizen.org/metadata/tts-engine/text-size"
 
+/* Define Macro */
+#define FREE(x)        { if (NULL != x)        { free(x);      x = NULL; } }
+#define G_FREE(x)      { if (NULL != x) { g_free(x);   x = NULL; } }
 
 typedef struct metadata {
        const char *key;
@@ -62,94 +80,261 @@ typedef struct metadata {
 } metadata;
 
 static xmlDocPtr g_doc;
+GumUser *g_guser = NULL;
+uid_t g_uid = 301;     // app_fw
+gid_t g_gid = 301;     // app_fw
+GumUserType g_ut = GUM_USERTYPE_NONE;
+gchar *g_user_type = NULL;
+
+char *g_dir_config_base = NULL;
+char *g_dir_home = NULL;
+char *g_dir_engine_base = NULL;
+char *g_dir_engine_info = NULL;
 
 static int __create_engine_info_xml(const char *pkgid)
 {
-       LOGD("=== Create engine info doc");
+       LOGD("@@@ Create engine info doc");
        g_doc = xmlNewDoc((xmlChar*)"1.0");
        if (NULL == g_doc) {
                LOGE("[ERROR] Fail to new doc");
                return -1;
        }
-       LOGD("===");
+       LOGD("@@@");
        return 0;
 }
 
-static int __save_engine_info_xml(const char *pkgid)
+static int __save_engine_info_xml(const char *pkgid, gchar *ut, uid_t uid, gid_t gid)
 {
-       LOGD("=== Save engine info doc");
+       LOGD("@@@ Save engine info doc");
+       char *dir_config_base = NULL;
+       char *dir_home = NULL;
+       char *dir_engine_base = NULL;
+       char *dir_engine_info = NULL;
+
+       if (NULL == ut || (NULL != ut && 0 == strcmp(ut, "none"))) {
+               LOGE("[ERROR] Usertype is NONE");
+               return -1;
+       }
+
+       uid_t globalapp_uid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
+       uid_t tmp_uid = 0;
+       gid_t tmp_gid = 0;
+
+       LOGD("uid(%d)", uid);
+
+       if (globalapp_uid == uid) {
+               /* Global app */
+               dir_config_base = strdup(TTS_GLOBAL_CONFIG_BASE);
+               dir_home = strdup(TTS_GLOBAL_HOME);
+               dir_engine_base = strdup(TTS_GLOBAL_ENGINE_BASE);
+               dir_engine_info = strdup(TTS_GLOBAL_ENGINE_INFO);
+               tmp_uid = 301;  // app_fw
+               tmp_gid = 301;  // app_fw
+       } else {
+               /* User app, Guest app, Security app */
+               if (NULL != g_dir_config_base)
+                       dir_config_base = strdup(g_dir_config_base);
+               if (NULL != g_dir_home)
+                       dir_home = strdup(g_dir_home);
+               if (NULL != g_dir_engine_base)
+                       dir_engine_base = strdup(g_dir_engine_base);
+               if (NULL != g_dir_engine_info)
+                       dir_engine_info = strdup(g_dir_engine_info);
+               tmp_uid = uid;
+               tmp_gid = gid;
+       }
+
+       if (NULL == dir_config_base || NULL == dir_home || NULL == dir_engine_base || NULL == dir_engine_info) {
+               LOGE("[ERROR] Fail to allocate memory");
+               FREE(dir_config_base)
+               FREE(dir_home)
+               FREE(dir_engine_base)
+               FREE(dir_engine_info)
+
+               return -1;
+       }
+
+       LOGD("[DEBUG] dir_engine_info(%s)", dir_engine_info);
+
 
        /* Make directories */
-       if (0 != access(TTS_CONFIG_BASE, F_OK)) {
-               if (0 != mkdir(TTS_CONFIG_BASE, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
-                       LOGE("[ERROR] Fail to make directory : %s", TTS_CONFIG_BASE);
+       int fd = -1;
+//     if (0 != access(dir_config_base, F_OK)) {
+       fd = open(dir_config_base, O_DIRECTORY);
+       if (-1 == fd) {
+               LOGE("[INFO] No directory : %s, errno : %d", dir_config_base, errno);
+               if (0 != mkdir(dir_config_base, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
+                       LOGE("[ERROR] Fail to make directory : %s, errno : %d", dir_config_base, errno);
+                       FREE(dir_config_base)
+                       FREE(dir_home)
+                       FREE(dir_engine_base)
+                       FREE(dir_engine_info)
                        return -1;
                } else {
-                       LOGD("Success to make directory : %s", TTS_CONFIG_BASE);
+                       LOGD("Success to make directory : %s", dir_config_base);
+                       if (0 != chown(dir_config_base, tmp_uid, tmp_gid)) {
+                               LOGD("[ERROR] Fail to change user and group, errno : %d", errno);
+                       } else {
+                               LOGD("[DEBUG] Success to change user and group");
+                       }
                }
+       } else {
+               close(fd);
        }
 
-       if (0 != access(TTS_HOME, F_OK)) {
-               if (0 != mkdir(TTS_HOME, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
-                       LOGE("[ERROR] Fail to make directory : %s", TTS_HOME);
+//     if (0 != access(dir_home, F_OK)) {
+       fd = open(dir_home, O_DIRECTORY);
+       if (-1 == fd) {
+               LOGE("[INFO] No directory : %s, errno : %d", dir_home, errno);
+               if (0 != mkdir(dir_home, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
+                       LOGE("[ERROR] Fail to make directory : %s, errno : %d", dir_home, errno);
+                       FREE(dir_config_base)
+                       FREE(dir_home)
+                       FREE(dir_engine_base)
+                       FREE(dir_engine_info)
                        return -1;
                } else {
-                       LOGD("Success to make directory : %s", TTS_HOME);
+                       LOGD("Success to make directory : %s", dir_home);
+                       if (0 != chown(dir_home, tmp_uid, tmp_gid)) {
+                               LOGD("[ERROR] Fail to change user and group, errno : %d", errno);
+                       } else {
+                               LOGD("[DEBUG] Success to change user and group");
+                       }
                }
+       } else {
+               close(fd);
        }
 
-       if (0 != access(TTS_ENGINE_BASE, F_OK)) {
-               if (0 != mkdir(TTS_ENGINE_BASE, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
-                       LOGE("[ERROR] Fail to make directory : %s", TTS_ENGINE_BASE);
+//     if (0 != access(dir_engine_base, F_OK)) {
+       fd = open(dir_engine_base, O_DIRECTORY);
+       if (-1 == fd) {
+               LOGE("[INFO] No directory : %s, errno : %d", dir_engine_base, errno);
+               if (0 != mkdir(dir_engine_base, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
+                       LOGE("[ERROR] Fail to make directory : %s, errno : %d", dir_engine_base, errno);
+                       FREE(dir_config_base)
+                       FREE(dir_home)
+                       FREE(dir_engine_base)
+                       FREE(dir_engine_info)
                        return -1;
                } else {
-                       LOGD("Success to make directory : %s", TTS_ENGINE_BASE);
+                       LOGD("Success to make directory : %s", dir_engine_base);
+                       if (0 != chown(dir_engine_base, tmp_uid, tmp_gid)) {
+                               LOGD("[ERROR] Fail to change user and group, errno : %d", errno);
+                       } else {
+                               LOGD("[DEBUG] Success to change user and group");
+                       }
                }
+       } else {
+               close(fd);
        }
 
-       if (0 != access(TTS_ENGINE_INFO, F_OK)) {
-               if (0 != mkdir(TTS_ENGINE_INFO, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
-                       LOGE("[ERROR] Fail to make directory : %s", TTS_ENGINE_INFO);
+//     if (0 != access(dir_engine_info, F_OK)) {
+       fd = open(dir_engine_info, O_DIRECTORY);
+       if (-1 == fd) {
+               LOGE("[INFO] No directory : %s, errno : %d", dir_engine_info, errno);
+               if (0 != mkdir(dir_engine_info, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
+                       LOGE("[ERROR] Fail to make directory : %s, errno : %d", dir_engine_info, errno);
+                       FREE(dir_config_base)
+                       FREE(dir_home)
+                       FREE(dir_engine_base)
+                       FREE(dir_engine_info)
                        return -1;
                } else {
-                       LOGD("Success to make directory : %s", TTS_ENGINE_INFO);
+                       LOGD("Success to make directory : %s", dir_engine_info);
+                       if (0 != chown(dir_engine_info, tmp_uid, tmp_gid)) {
+                               LOGD("[ERROR] Fail to change user and group, errno : %d", errno);
+                       } else {
+                               LOGD("[DEBUG] Success to change user and group");
+                       }
                }
+       } else {
+               close(fd);
        }
 
+
        char path[256] = {'\0',};
-       snprintf(path, 256, "%s/%s.xml", TTS_ENGINE_INFO, pkgid);
+       snprintf(path, 256, "%s/%s.xml", dir_engine_info, pkgid);
        int ret = xmlSaveFormatFile(path, g_doc, 1);
        LOGD("xmlSaveFile (%d)", ret);
-       LOGD("===");
+       if (0 == ret) {
+               if (0 != chown(path, tmp_uid, tmp_gid)) {
+                       LOGD("[ERROR] Fail to change user and group");
+               } else {
+                       LOGD("[DEBUG] Success to change user and group");
+               }
+       }
+
+       FREE(dir_config_base)
+       FREE(dir_home)
+       FREE(dir_engine_base)
+       FREE(dir_engine_info)
+
+       LOGD("@@@");
        return 0;
 }
 
-static int __remove_engine_info_xml(const char *pkgid)
+static int __remove_engine_info_xml(const char *pkgid, gchar *ut, uid_t uid)
 {
-       LOGD("=== Remove engine info doc");
+       LOGD("@@@ Remove engine info doc");
+
+       char *dir_engine_info = NULL;
+
+       if (NULL == ut || (NULL != ut && 0 == strcmp(ut, "none"))) {
+               LOGE("[ERROR] Usertype is NONE");
+               return -1;
+       }
+
+       uid_t globalapp_uid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
+
+       LOGD("uid(%d)", uid);
+
+       if (globalapp_uid == uid) {
+               /* Global app */
+               dir_engine_info = strdup(TTS_GLOBAL_ENGINE_INFO);
+       } else {
+               /* User app, Guest app, Security app */
+               if (NULL != g_dir_engine_info)
+                       dir_engine_info = strdup(g_dir_engine_info);
+       }
+
+       if (NULL == dir_engine_info) {
+               LOGE("[ERROR] Fail to allocate memory");
+               return -1;
+       }
+
+       LOGD("[DEBUG] dir_engine_info(%s)", dir_engine_info);
+
+
        char path[256] = {'\0',};
-       snprintf(path, 256, "%s/%s.xml", TTS_ENGINE_INFO, pkgid);
+       snprintf(path, 256, "%s/%s.xml", dir_engine_info, pkgid);
        if (0 == access(path, F_OK)) {
                LOGD("Remove engine info xml(%s)", path);
                if (0 != remove(path)) {
                        LOGE("[ERROR] Fail to Remove engine info xml(%s)", path);
                }
        }
-       LOGD("===");
+
+       FREE(dir_engine_info)
+
+       LOGD("@@@");
        return 0;
 }
 
 static void __insert_language_from_metadata(xmlNodePtr root, const char *language)
 {
-       LOGD("==== Insert language");
+       LOGD("@@@ Insert language");
        char* voice = NULL;
        char* lang = NULL;
        char* type = NULL;
+       if (NULL == root || NULL == language) {
+               LOGE("Invalid parameter, root(%p), language(%s)", root, language);
+               return;
+       }
 
        char *tmp_lang = NULL;
        char *tmp_free = NULL;
        tmp_free = tmp_lang = strdup(language);
-       if (NULL != tmp_lang) {
+       if (NULL == tmp_lang) {
                LOGE("Fail to memory allocation");
                return;
        }
@@ -173,39 +358,11 @@ static void __insert_language_from_metadata(xmlNodePtr root, const char *languag
        }
        xmlAddChild(root, voices_node);
 
-       free(tmp_free);
-       tmp_free = NULL;
+       FREE(tmp_free)
 }
 
-EXPORT_API
-int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
+static int __write_metadata_inxml(const char *pkgid, const char *appid, GList *list)
 {
-       LOGD("METADATA INSTALL");
-       LOGD("pkgid(%s) appid(%s) list(%d)", pkgid, appid, g_list_length(list));
-
-       uid_t uid = 0;
-       int ret = -1;
-       ret = pkgmgr_installer_info_get_target_uid(&uid);
-       if (ret < 0) {
-               LOGE("[ERROR] Fail to get target uid");
-               return 0;
-       } else {
-               LOGD("uid(%d)", uid);
-       }
-
-       ret = tzplatform_set_user(uid);
-       if (ret < 0) {
-               LOGE("[ERROR] Invalid uid");
-               return 0;
-       } else {
-               LOGD("TZ_USER_HOME: %s", tzplatform_mkstr(TZ_USER_HOME, "/"));
-       }
-
-       if (0 >= g_list_length(list)) {
-               LOGE("[ERROR] No Engine Metadata");
-               return 0;
-       }
-
        GList *iter = NULL;
        metadata *md = NULL;
 
@@ -217,15 +374,16 @@ int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *
        root = xmlNewNode(NULL, (const xmlChar*)TTS_TAG_ENGINE_BASE);
        if (NULL == root) {
                LOGE("[ERROR] Fail to get new node");
-               xmlFreeDoc(g_doc);
+//             xmlFreeDoc(g_doc);
                return -1;
        }
        xmlDocSetRootElement(g_doc, root);
 
        /* Save name */
-       cur = xmlNewNode(NULL, (const xmlChar*)TTS_TAG_ENGINE_NAME);
-       xmlNodeSetContent(cur, (const xmlChar*)pkgid);
-       xmlAddChild(root, cur);
+//     cur = xmlNewNode(NULL, (const xmlChar*)TTS_TAG_ENGINE_NAME);
+//     xmlNodeSetContent(cur, (const xmlChar*)pkgid);
+//     xmlAddChild(root, cur);
+
 
        iter = g_list_first(list);
        while (NULL != iter) {
@@ -238,6 +396,18 @@ int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *
                                cur = xmlNewNode(NULL, (const xmlChar*)TTS_TAG_ENGINE_CREDENTIAL);
                                xmlNodeSetContent(cur, (const xmlChar*)md->value);
                                xmlAddChild(root, cur);
+                       } else if (!strcmp(md->key, TTS_METADATA_SETTING)) {
+                               cur = xmlNewNode(NULL, (const xmlChar*)TTS_TAG_ENGINE_SETTING);
+                               xmlNodeSetContent(cur, (const xmlChar*)md->value);
+                               xmlAddChild(root, cur);
+                       } else if (!strcmp(md->key, TTS_METADATA_NAME)) {
+                               cur = xmlNewNode(NULL, (const xmlChar*)TTS_TAG_ENGINE_NAME);
+                               xmlNodeSetContent(cur, (const xmlChar*)md->value);
+                               xmlAddChild(root, cur);
+                       } else if (!strcmp(md->key, TTS_METADATA_TEXT_SIZE)) {
+                               cur = xmlNewNode(NULL, (const xmlChar*)TTS_TAG_ENGINE_TEXT_SIZE);
+                               xmlNodeSetContent(cur, (const xmlChar*)md->value);
+                               xmlAddChild(root, cur);
                        } else {
                                LOGW("[WARNING] Unknown metadata type");
                        }
@@ -251,8 +421,276 @@ int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *
 
        LOGD("");
 
-       __save_engine_info_xml(pkgid);
+       return 0;
+
+}
+
+EXPORT_API
+int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
+{
+       LOGD("METADATA INSTALL");
+       LOGD("pkgid(%s) appid(%s) list(%d)", pkgid, appid, g_list_length(list));
+
+       int ret = -1;
+       ret = pkgmgr_installer_info_get_target_uid(&g_uid);
+       if (ret < 0) {
+               LOGE("[ERROR] Fail to get target uid");
+               return 0;
+       } else {
+               LOGD("uid(%d)", g_uid);
+               printf("[Parser Debug][DEBUG] uid(%d)", g_uid);
+       }
+
+       uid_t globalapp_uid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
+       if (globalapp_uid == g_uid) {
+               g_user_type = g_strdup("admin");
+       } else {
+               g_guser = gum_user_get_sync(g_uid, FALSE);
+               if (NULL == g_guser) {
+                       LOGE("[ERROR] g_guser is NULL");
+                       return -1;
+               }
+
+               g_object_get(G_OBJECT(g_guser), "gid", &g_gid, NULL);
+               g_object_get(G_OBJECT(g_guser), "usertype", &g_ut, NULL);
+               g_user_type = g_strdup(gum_user_type_to_string(g_ut));
+       }
+
+       if (NULL == g_user_type) {
+               LOGE("[ERROR] Fail to allocate memory");
+               if (NULL != g_guser) {
+                       g_object_unref(g_guser);
+                       g_guser = NULL;
+               }
+               return -1;
+       }
+
+       if (0 == strcmp(g_user_type, "none")) {
+               /* GUM_USERTYPE_NONE */
+               LOGE("[ERROR] Fail to get target uid");
+               g_object_unref(g_guser);
+               g_guser = NULL;
+               G_FREE(g_user_type)
+               return -1;
+       }
+
+       if (globalapp_uid == g_uid) {
+               /* global directory */
+               LOGD("[DEBUG] usertype: %s", g_user_type);
+               if (0 >= g_list_length(list)) {
+                       LOGE("[ERROR] No Engine Metadata");
+                       G_FREE(g_user_type)
+                       return 0;
+               }
+
+               if (0 != __write_metadata_inxml(pkgid, appid, list)) {
+                       LOGE("[ERROR] Fail to write metadata in the xml");
+                       xmlFreeDoc(g_doc);
+                       G_FREE(g_user_type)
+                       return -1;
+               }
+
+               /* Save in /etc/skel/share/ */
+               g_dir_config_base = strdup(TTS_GLOBAL_CONFIG_BASE);
+               g_dir_home = strdup(TTS_GLOBAL_HOME);
+               g_dir_engine_base = strdup(TTS_GLOBAL_ENGINE_BASE);
+               g_dir_engine_info = strdup(TTS_GLOBAL_ENGINE_INFO);
+
+               if (NULL == g_dir_config_base || NULL == g_dir_home || NULL == g_dir_engine_base || NULL == g_dir_engine_info) {
+                       LOGE("[ERROR] Fail to allocate memory");
+                       FREE(g_dir_config_base)
+                       FREE(g_dir_home)
+                       FREE(g_dir_engine_base)
+                       FREE(g_dir_engine_info)
+
+                       xmlFreeDoc(g_doc);
+                       G_FREE(g_user_type)
+                       return -1;
+               }
+
+               if (0 != __save_engine_info_xml(pkgid, g_user_type, g_uid, g_gid)) {
+                       LOGE("[ERROR] Fail to make engine info file");
+                       xmlFreeDoc(g_doc);
+                       G_FREE(g_user_type)
+                       return -1;
+               }
+
+               /* Get user data by using libgum */
+
+               GumUserService *gus = NULL;
+               GumUserList *users = NULL;
+               GumUserList *iter = NULL;
+               GumUser *user = NULL;
+               gchar **query;
+               GumUserType gumut = GUM_USERTYPE_NONE;
+               gchar *user_type = NULL;
+
+               uid_t uid;
+               gid_t gid;
+               gchar *home_dir = NULL;
+
+               gus = gum_user_service_create_sync(TRUE);
+               if (!gus) {
+                       LOGE("Failed to create gum user service");
+                       G_FREE(g_user_type)
+                       return -1;
+               }
+
+               query = g_strsplit("admin,normal", ",", -1);
+
+               users = gum_user_service_get_user_list_sync(gus, (const gchar *const *)query);
+               g_strfreev(query);
+
+               if (!users) {
+                       LOGD("NO users");
+                       g_object_unref(gus);
+                       gus = NULL;
+                       G_FREE(g_user_type)
+                       return 0;
+               }
+
+               /* Make new user list */
+
+               iter = users;
+               while (iter != NULL) {
+                       user = (GumUser*) iter->data;
+                       g_object_get(G_OBJECT(user), "uid", &uid, NULL);
+                       G_FREE(home_dir)
+
+                       g_object_get(G_OBJECT(user), "gid", &gid, NULL);
+                       g_object_get(G_OBJECT(user), "homedir", &home_dir, NULL);
+                       g_object_get(G_OBJECT(user), "usertype", &gumut, NULL);
+                       user_type = g_strdup(gum_user_type_to_string(gumut));
+                       if (NULL == user_type) {
+                               gum_user_service_list_free(users);
+                               G_FREE(home_dir)
+                               g_object_unref(gus);
+                               gus = NULL;
+                               return -1;
+                       }
+
+                       LOGD("[DEBUG] user info");
+                       if (NULL != home_dir) {
+                               LOGD("[DEBUG] uid(%d), gid(%d),  user_type(%s), home_dir(%s)", uid, gid, user_type, home_dir);
+
+                               g_dir_config_base = (char*)calloc(strlen(home_dir) + 14, sizeof(char));
+                               g_dir_home = (char*)calloc(strlen(home_dir) + 18, sizeof(char));
+                               g_dir_engine_base = (char*)calloc(strlen(home_dir) + 22, sizeof(char));
+                               g_dir_engine_info = (char*)calloc(strlen(home_dir) + 34, sizeof(char));
+
+                               if (NULL == g_dir_config_base || NULL == g_dir_home || NULL == g_dir_engine_base || NULL == g_dir_engine_info) {
+                                       LOGE("[ERROR] Fail to allocate memory");
+                                       FREE(g_dir_config_base)
+                                       FREE(g_dir_home)
+                                       FREE(g_dir_engine_base)
+                                       FREE(g_dir_engine_info)
+                                       gum_user_service_list_free(users);
+                                       g_object_unref(gus);
+                                       gus = NULL;
+                                       G_FREE(user_type)
+                                       G_FREE(home_dir)
+                                       return -1;
+                               }
+                               snprintf(g_dir_config_base, strlen(home_dir) + 14, "%s/share/.voice", home_dir);
+                               snprintf(g_dir_home, strlen(home_dir) + 18, "%s/share/.voice/tts", home_dir);
+                               snprintf(g_dir_engine_base, strlen(home_dir) + 22, "%s/share/.voice/tts/1.0", home_dir);
+                               snprintf(g_dir_engine_info, strlen(home_dir) + 34, "%s/share/.voice/tts/1.0/engine-info", home_dir);
+
+                               LOGD("[DEBUG] g_dir_engine_info(%s)", g_dir_engine_info);
+
+                               if (0 != __save_engine_info_xml(pkgid, user_type, uid, gid)) {
+                                       LOGE("[ERROR] Fail to make engine info file");
+                               }
+
+                               FREE(g_dir_config_base)
+                               FREE(g_dir_home)
+                               FREE(g_dir_engine_base)
+                               FREE(g_dir_engine_info)
+
+                               G_FREE(home_dir)
+                       }
+
+                       G_FREE(user_type)
+                       iter = g_list_next(iter);
+               }
+
+               gum_user_service_list_free(users);
+               g_object_unref(gus);
+               gus = NULL;
+       } else {
+               /* user directory */
+               LOGD("[DEBUG] usertype: %s", g_user_type);
+
+               ret = tzplatform_set_user(g_uid);
+               if (ret < 0) {
+                       LOGE("[ERROR] Invalid uid");
+                       g_object_unref(g_guser);
+                       g_guser = NULL;
+                       G_FREE(g_user_type)
+                       return 0;
+               } else {
+                       LOGD("TZ_USER_HOME: %s", tzplatform_mkstr(TZ_USER_HOME, "/"));
+                       printf("[Parser Debug][DEBUG] TZ_USER_HOME: %s", tzplatform_mkstr(TZ_USER_HOME, "/"));
+               }
+
+               if (0 >= g_list_length(list)) {
+                       LOGE("[ERROR] No Engine Metadata");
+                       g_object_unref(g_guser);
+                       g_guser = NULL;
+                       G_FREE(g_user_type)
+                       return 0;
+               }
+
+               if (0 != __write_metadata_inxml(pkgid, appid, list)) {
+                       LOGE("[ERROR] Fail to write metadata in the xml");
+                       xmlFreeDoc(g_doc);
+                       g_object_unref(g_guser);
+                       g_guser = NULL;
+                       G_FREE(g_user_type)
+                       return -1;
+               }
+
+               g_dir_config_base = strdup(TTS_CONFIG_BASE);
+               g_dir_home = strdup(TTS_HOME);
+               g_dir_engine_base = strdup(TTS_ENGINE_BASE);
+               g_dir_engine_info = strdup(TTS_ENGINE_INFO);
+
+               if (NULL == g_dir_config_base || NULL == g_dir_home || NULL == g_dir_engine_base || NULL == g_dir_engine_info) {
+                       LOGE("[ERROR] Fail to allocate memory");
+                       FREE(g_dir_config_base)
+                       FREE(g_dir_home)
+                       FREE(g_dir_engine_base)
+                       FREE(g_dir_engine_info)
+                       xmlFreeDoc(g_doc);
+                       g_object_unref(g_guser);
+                       g_guser = NULL;
+                       G_FREE(g_user_type)
+                       return -1;
+               }
+
+               if (0 != __save_engine_info_xml(pkgid, g_user_type, g_uid, g_gid)) {
+                       LOGE("[ERROR] Fail to make engine info file");
+                       xmlFreeDoc(g_doc);
+                       if (NULL != g_guser) {
+                               g_object_unref(g_guser);
+                               g_guser = NULL;
+                       }
+                       G_FREE(g_user_type)
+                       return -1;
+               }
+       }
+
        xmlFreeDoc(g_doc);
+       if (NULL != g_guser) {
+               g_object_unref(g_guser);
+               g_guser = NULL;
+       }
+       G_FREE(g_user_type)
+
+       FREE(g_dir_config_base)
+       FREE(g_dir_home)
+       FREE(g_dir_engine_base)
+       FREE(g_dir_engine_info)
 
        return 0;
 }
@@ -263,19 +701,193 @@ int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList
        LOGD("METADATA UNINSTALL");
        LOGD("pkgid(%s) appid(%s) list(%d)", pkgid, appid, g_list_length(list));
 
-       GList *iter = NULL;
-       metadata *md = NULL;
+       int ret = -1;
+       ret = pkgmgr_installer_info_get_target_uid(&g_uid);
+       if (ret < 0) {
+               LOGE("[ERROR] Fail to get target uid");
+               return 0;
+       } else {
+               LOGD("uid(%d)", g_uid);
+               printf("[Parser Debug][DEBUG] uid(%d)", g_uid);
+       }
 
-       iter = g_list_first(list);
-       while (NULL != iter) {
-               md = (metadata *)iter->data;
-               if (NULL != md && NULL != md->key) {
-                       LOGD(" - key(%s) value(%s)", md->key, md->value);
+       uid_t globalapp_uid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
+       if (globalapp_uid == g_uid) {
+               g_user_type = g_strdup("admin");
+       } else {
+               g_guser = gum_user_get_sync(g_uid, FALSE);
+               if (NULL == g_guser) {
+                       LOGE("[ERROR] g_guser is NULL");
+                       return -1;
                }
-               iter = g_list_next(iter);
+
+               g_object_get(G_OBJECT(g_guser), "usertype", &g_ut, NULL);
+               g_user_type = g_strdup(gum_user_type_to_string(g_ut));
+       }
+
+       if (NULL == g_user_type) {
+               LOGE("[ERROR] Fail to allocate memory");
+               if (NULL != g_guser) {
+                       g_object_unref(g_guser);
+                       g_guser = NULL;
+               }
+               return -1;
+       }
+
+       if (0 == strcmp(g_user_type, "none")) {
+               /* GUM_USERTYPE_NONE */
+               LOGE("[ERROR] Fail to get target uid");
+               g_object_unref(g_guser);
+               g_guser = NULL;
+               G_FREE(g_user_type)
+               return -1;
+       }
+
+       if (globalapp_uid == g_uid) {
+               /* global directory */
+               LOGD("[DEBUG] usertype: %s", g_user_type);
+
+               /* Remove files in /etc/skel/share/ */
+               g_dir_engine_info = strdup(TTS_GLOBAL_ENGINE_INFO);
+               if (NULL == g_dir_engine_info) {
+                       LOGE("[ERROR] Fail to allocate memory");
+                       G_FREE(g_user_type)
+                       return -1;
+               }
+
+               if (0 != __remove_engine_info_xml(pkgid, g_user_type, g_uid)) {
+                       LOGE("[ERROR] Fail to remove engine info file");
+               }
+
+               /* Get user data by using libgum */
+
+               GumUserService *gus = NULL;
+               GumUserList *users = NULL;
+               GumUserList *iter = NULL;
+               GumUser *user = NULL;
+               gchar **query;
+               GumUserType gumut = GUM_USERTYPE_NONE;
+               gchar *user_type = NULL;
+
+               uid_t uid;
+               gchar *home_dir = NULL;
+
+               GList *md_iter = NULL;
+               metadata *md = NULL;
+
+               gus = gum_user_service_create_sync(TRUE);
+               if (!gus) {
+                       LOGE("Failed to create gum user service");
+                       G_FREE(g_user_type)
+                       return -1;
+               }
+
+               query = g_strsplit("admin,normal", ",", -1);
+
+               users = gum_user_service_get_user_list_sync(gus, (const gchar *const *)query);
+               g_strfreev(query);
+
+               if (!users) {
+                       LOGD("NO users");
+                       g_object_unref(gus);
+                       gus = NULL;
+                       G_FREE(g_user_type)
+                       return 0;
+               }
+
+               /* Make new user list */
+
+               iter = users;
+               while (iter != NULL) {
+                       user = (GumUser*) iter->data;
+                       g_object_get(G_OBJECT(user), "uid", &uid, NULL);
+                       G_FREE(home_dir)
+                       g_object_get(G_OBJECT(user), "homedir", &home_dir, NULL);
+                       g_object_get(G_OBJECT(user), "usertype", &gumut, NULL);
+                       user_type = g_strdup(gum_user_type_to_string(gumut));
+                       if (NULL == user_type) {
+                               gum_user_service_list_free(users);
+                               G_FREE(home_dir)
+                               g_object_unref(gus);
+                               gus = NULL;
+                               return -1;
+                       }
+
+                       if (NULL != home_dir) {
+                               g_dir_engine_info = (char*)calloc(strlen(home_dir) + 34, sizeof(char));
+                               if (NULL == g_dir_engine_info) {
+                                       gum_user_service_list_free(users);
+                                       G_FREE(home_dir)
+                                       g_object_unref(gus);
+                                       gus = NULL;
+                                       G_FREE(user_type)
+                                       return -1;
+                               }
+
+                               snprintf(g_dir_engine_info, strlen(home_dir) + 34, "%s/share/.voice/tts/1.0/engine-info", home_dir);
+
+                               md_iter = g_list_first(list);
+                               while (NULL != md_iter) {
+                                       md = (metadata *)md_iter->data;
+                                       LOGD(" - key(%s) value(%s)", md->key, md->value);
+                                       md_iter = g_list_next(md_iter);
+                               }
+
+                               if (0 != __remove_engine_info_xml(pkgid, user_type, uid)) {
+                                       LOGE("[ERROR] Fail to remove engine info file");
+                               }
+
+                               G_FREE(home_dir)
+                               FREE(g_dir_engine_info)
+                       }
+                       G_FREE(user_type)
+
+                       LOGD("Finish release memory");
+                       iter = g_list_next(iter);
+                       LOGD("Finish next iter");
+               }
+
+               gum_user_service_list_free(users);
+               g_object_unref(gus);
+               gus = NULL;
+       } else {
+               /* user directory */
+               LOGD("[DEBUG] usertype: %s", g_user_type);
+
+               ret = tzplatform_set_user(g_uid);
+               if (ret < 0) {
+                       LOGE("[ERROR] Invalid uid");
+                       g_object_unref(g_guser);
+                       g_guser = NULL;
+                       G_FREE(g_user_type)
+                       return -1;
+               } else {
+                       LOGD("TZ_USER_HOME: %s", tzplatform_mkstr(TZ_USER_HOME, "/"));
+                       printf("[Parser Debug][DEBUG] TZ_USER_HOME: %s", tzplatform_mkstr(TZ_USER_HOME, "/"));
+               }
+
+               g_dir_engine_info = strdup(TTS_ENGINE_INFO);
+               if (NULL == g_dir_engine_info) {
+                       LOGE("[ERROR] Fail to allocate memory");
+                       g_object_unref(g_guser);
+                       g_guser = NULL;
+                       G_FREE(g_user_type)
+                       return -1;
+               }
+
+               if (0 != __remove_engine_info_xml(pkgid, g_user_type, g_uid)) {
+                       LOGE("[ERROR] Fail to remove engine info file");
+               }
+
+       }
+
+       if (NULL != g_guser) {
+               g_object_unref(g_guser);
+               g_guser = NULL;
        }
+       G_FREE(g_user_type)
 
-       __remove_engine_info_xml(pkgid);
+       FREE(g_dir_engine_info)
 
        LOGD("");
        return 0;