Merge "Add metadata for setting app" into tizen
[platform/core/uifw/tts.git] / engine-parser / src / tts-engine-parser.c
index 1a3e8dc..68786dc 100644 (file)
 #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"
 
 typedef struct metadata {
        const char *key;
@@ -73,7 +74,8 @@ typedef struct metadata {
 
 static xmlDocPtr g_doc;
 GumUser *g_guser = NULL;
-uid_t g_uid = 5001;
+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;
 
@@ -94,7 +96,7 @@ static int __create_engine_info_xml(const char *pkgid)
        return 0;
 }
 
-static int __save_engine_info_xml(const char *pkgid, gchar *ut, uid_t uid)
+static int __save_engine_info_xml(const char *pkgid, gchar *ut, uid_t uid, gid_t gid)
 {
        LOGD("=== Save engine info doc");
        char *dir_config_base = NULL;
@@ -108,6 +110,8 @@ static int __save_engine_info_xml(const char *pkgid, gchar *ut, uid_t uid)
        }
 
        uid_t globalapp_uid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
+       uid_t tmp_uid = 0;
+       gid_t tmp_gid = 0;
 
        LOGD("uid(%d)", uid);
 
@@ -117,6 +121,8 @@ static int __save_engine_info_xml(const char *pkgid, gchar *ut, uid_t uid)
                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)
@@ -127,6 +133,8 @@ static int __save_engine_info_xml(const char *pkgid, gchar *ut, uid_t uid)
                        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) {
@@ -168,6 +176,11 @@ static int __save_engine_info_xml(const char *pkgid, gchar *ut, uid_t uid)
                        return -1;
                } else {
                        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");
+                       } else {
+                               LOGD("[DEBUG] Success to change user and group");
+                       }
                }
        }
 
@@ -185,6 +198,11 @@ static int __save_engine_info_xml(const char *pkgid, gchar *ut, uid_t uid)
                        return -1;
                } else {
                        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");
+                       } else {
+                               LOGD("[DEBUG] Success to change user and group");
+                       }
                }
        }
 
@@ -202,6 +220,11 @@ static int __save_engine_info_xml(const char *pkgid, gchar *ut, uid_t uid)
                        return -1;
                } else {
                        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");
+                       } else {
+                               LOGD("[DEBUG] Success to change user and group");
+                       }
                }
        }
 
@@ -219,6 +242,11 @@ static int __save_engine_info_xml(const char *pkgid, gchar *ut, uid_t uid)
                        return -1;
                } else {
                        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");
+                       } else {
+                               LOGD("[DEBUG] Success to change user and group");
+                       }
                }
        }
 
@@ -227,6 +255,13 @@ static int __save_engine_info_xml(const char *pkgid, gchar *ut, uid_t uid)
        snprintf(path, 256, "%s/%s.xml", dir_engine_info, pkgid);
        int ret = xmlSaveFormatFile(path, g_doc, 1);
        LOGD("xmlSaveFile (%d)", ret);
+       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);
        dir_config_base = NULL;
@@ -346,9 +381,9 @@ static int __write_metadata_inxml(const char *pkgid, const char *appid, GList *l
        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);
@@ -362,6 +397,14 @@ static int __write_metadata_inxml(const char *pkgid, const char *appid, GList *l
                                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 {
                                LOGW("[WARNING] Unknown metadata type");
                        }
@@ -405,6 +448,7 @@ int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *
                        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));
        }
@@ -443,6 +487,42 @@ int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *
                        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");
+                       if (NULL != g_dir_config_base) {
+                               free(g_dir_config_base);
+                               g_dir_config_base = NULL;
+                       }
+                       if (NULL != g_dir_home) {
+                               free(g_dir_home);
+                               g_dir_home = NULL;
+                       }
+                       if (NULL != g_dir_engine_base) {
+                               free(g_dir_engine_base);
+                               g_dir_engine_base = NULL;
+                       }
+                       if (NULL != g_dir_engine_info) {
+                               free(g_dir_engine_info);
+                               g_dir_engine_info = NULL;
+                       }
+                       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;
@@ -454,6 +534,7 @@ int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *
                gchar *user_type = NULL;
 
                uid_t uid;
+               gid_t gid;
                gchar *home_dir = NULL;
 
                gus = gum_user_service_create_sync(TRUE);
@@ -486,6 +567,7 @@ int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *
                                free(home_dir);
                                home_dir = NULL;
                        }
+                       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));
@@ -498,7 +580,7 @@ int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *
 
                        LOGD("[DEBUG] user info");
                        if (NULL != home_dir) {
-                               LOGD("[DEBUG] uid(%d), user_type(%s), home_dir(%s)", uid, user_type, 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));
@@ -535,7 +617,7 @@ int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *
 
                                LOGD("[DEBUG] g_dir_engine_info(%s)", g_dir_engine_info);
 
-                               if (0 != __save_engine_info_xml(pkgid, user_type, uid)) {
+                               if (0 != __save_engine_info_xml(pkgid, user_type, uid, gid)) {
                                        LOGE("[ERROR] Fail to make engine info file");
                                }
 
@@ -630,7 +712,7 @@ int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *
                        return -1;
                }
 
-               if (0 != __save_engine_info_xml(pkgid, g_user_type, g_uid)) {
+               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) {
@@ -721,6 +803,18 @@ int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList
                /* 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;