From: Ji-hoon Lee Date: Wed, 17 Oct 2018 07:56:45 +0000 (+0900) Subject: Implement ma_config_mgr_get_assistant_info() function X-Git-Tag: submit/tizen/20190213.101901~4 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f933c97ff4ea36d314da397566c340f66ae3d0ca;p=platform%2Fcore%2Fuifw%2Fmulti-assistant.git Implement ma_config_mgr_get_assistant_info() function Change-Id: Ib6f29621bacfc97f732538e5060461fbd79207b4 --- diff --git a/assistant-parser/src/ma_assistant_parser.c b/assistant-parser/src/ma_assistant_parser.c index b78890d..36aaa47 100644 --- a/assistant-parser/src/ma_assistant_parser.c +++ b/assistant-parser/src/ma_assistant_parser.c @@ -32,6 +32,8 @@ #include #include +#include "ma_defs.h" + /* Define EXPORT_API */ #ifndef EXPORT_API #define EXPORT_API __attribute__((visibility("default"))) @@ -42,20 +44,6 @@ #endif #define LOG_TAG "ma-assistant-parser" -#define MA_TAG_ASSISTANT_BASE "multi-assistant" -#define MA_TAG_ASSISTANT_NAME "name" -#define MA_TAG_ASSISTANT_ID "id" -#define MA_TAG_ASSISTANT_ICON_PATH "icon-path" -#define MA_TAG_ASSISTANT_LANGUAGE_SET "languages" -#define MA_TAG_ASSISTANT_LANGUAGE "lang" -#define MA_TAG_ASSISTANT_WAKEUP_WORD_SET "wakeup-words" -#define MA_TAG_ASSISTANT_WAKEUP_WORD "wakeup-word" - -#define MA_CONFIG_BASE tzplatform_mkpath(TZ_USER_HOME, "share/.multiassistant") -#define MA_HOME tzplatform_mkpath(TZ_USER_HOME, "share/.multiassistant/ma") -#define MA_ASSISTANT_BASE tzplatform_mkpath(TZ_USER_HOME, "share/.multiassistant/ma/1.0") -#define MA_ASSISTANT_INFO tzplatform_mkpath(TZ_USER_HOME, "share/.multiassistant/ma/1.0/assistant-info") - #define MA_GLOBAL_CONFIG_BASE "/etc/skel/share/.multiassistant" #define MA_GLOBAL_HOME "/etc/skel/share/.multiassistant/ma" #define MA_GLOBAL_ASSISTANT_BASE "/etc/skel/share/.multiassistant/ma/1.0" diff --git a/client/ma_ui.c b/client/ma_ui.c index 12d8ad0..e5d9354 100644 --- a/client/ma_ui.c +++ b/client/ma_ui.c @@ -28,6 +28,7 @@ #include "ma_ui_client.h" #include "ma_main.h" #include "ma_defs.h" +#include "ma_config_mgr.h" @@ -641,11 +642,11 @@ int ma_ui_foreach_assistant_info(ma_ui_assistant_info_cb callback, void* user_da } int ret = -1; -/* ret = ma_config_mgr_get_assistant_info(callback, user_data); + ret = ma_config_mgr_get_assistant_info(callback, user_data); if (0 != ret) { SLOG(LOG_ERROR, TAG_MAUI, "[UI ERROR] Fail to get assistant info"); } -*/ + ret = 0; return ret; } diff --git a/common/ma_config_mgr.c b/common/ma_config_mgr.c index 9330317..b66a110 100644 --- a/common/ma_config_mgr.c +++ b/common/ma_config_mgr.c @@ -14,6 +14,9 @@ * limitations under the License. */ +#include +#include +#include #include "ma_config_mgr.h" #include "ma_main.h" @@ -43,8 +46,174 @@ int ma_config_mgr_get_default_language(char** language) return 0; } +int ma_config_mgr_parse_assistant_info(ma_ui_assistant_info_cb callback, void* user_data, const char *path) +{ + xmlDocPtr doc = NULL; + xmlNodePtr cur = NULL; + xmlChar *key; + + int loop; + int retry_count = 0; + + while (NULL == doc) { + doc = xmlParseFile(path); + if (NULL != doc) { + break; + } + + if (MA_RETRY_COUNT == retry_count++) { + SLOG(LOG_ERROR, TAG_MACONFIG, "[ERROR] Fail to parse file error : %s", path); + xmlCleanupParser(); + return -1; + } + usleep(10000); + } + + cur = xmlDocGetRootElement(doc); + if (cur == NULL) { + SLOG(LOG_ERROR, TAG_MACONFIG, "[ERROR] Empty document"); + xmlFreeDoc(doc); + return -1; + } + + if (xmlStrcmp(cur->name, (const xmlChar *) MA_TAG_ASSISTANT_BASE)) { + SLOG(LOG_ERROR, TAG_MACONFIG, "[ERROR] The wrong type, root node is NOT %s", MA_TAG_ASSISTANT_BASE); + xmlFreeDoc(doc); + return -1; + } + + cur = cur->xmlChildrenNode; + if (cur == NULL) { + SLOG(LOG_ERROR, TAG_MACONFIG, "[ERROR] Empty document"); + xmlFreeDoc(doc); + return -1; + } + + /* alloc assistant info */ + ma_assistant_info_s* temp; + temp = (ma_assistant_info_s*)calloc(1, sizeof(ma_assistant_info_s)); + if (NULL == temp) { + SLOG(LOG_ERROR, TAG_MACONFIG, "[ERROR] Fail to allocate memory"); + xmlFreeDoc(doc); + return MA_ERROR_OUT_OF_MEMORY; + } + + temp->app_id = NULL; + temp->name = NULL; + temp->icon_path = NULL; + memset(temp->wakeup_list, 0x00, sizeof(temp->wakeup_list)); + temp->cnt_wakeup = 0; + memset(temp->supported_lang, 0x00, sizeof(temp->supported_lang)); + temp->cnt_lang = 0; + + while (cur != NULL) { + if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar *)MA_TAG_ASSISTANT_LANGUAGE_SET)) { + xmlNodePtr child_node = cur->xmlChildrenNode; + while (child_node != NULL) { + if (child_node->name && 0 == xmlStrcmp(child_node->name, (const xmlChar*)MA_TAG_ASSISTANT_LANGUAGE)) { + key = xmlNodeGetContent(child_node); + if (key) { + temp->supported_lang[temp->cnt_lang++] = strdup((const char*)key); + SLOG(LOG_DEBUG, TAG_MACONFIG, "Language : %s", key); + xmlFree(key); + } + } + + child_node = child_node->next; + } + } else if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar *)MA_TAG_ASSISTANT_WAKEUP_WORD_SET)) { + xmlNodePtr child_node = cur->xmlChildrenNode; + while (child_node != NULL) { + if (child_node->name && 0 == xmlStrcmp(child_node->name, (const xmlChar*)MA_TAG_ASSISTANT_WAKEUP_WORD)) { + key = xmlNodeGetContent(child_node); + if (key) { + temp->wakeup_list[temp->cnt_wakeup++] = strdup((const char*)key); + SLOG(LOG_DEBUG, TAG_MACONFIG, "Wakeup Word : %s", key); + xmlFree(key); + } + } + + child_node = child_node->next; + } + } else if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar*)MA_TAG_ASSISTANT_NAME)) { + key = xmlNodeGetContent(cur); + if (key) { + temp->name = strdup((const char*)key); + SLOG(LOG_DEBUG, TAG_MACONFIG, "Name : %s", key); + xmlFree(key); + } + } else if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar*)MA_TAG_ASSISTANT_ID)) { + key = xmlNodeGetContent(cur); + if (key) { + temp->app_id = strdup((const char*)key); + SLOG(LOG_DEBUG, TAG_MACONFIG, "ID : %s", key); + xmlFree(key); + } + } else if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar*)MA_TAG_ASSISTANT_ICON_PATH)) { + key = xmlNodeGetContent(cur); + if (key) { + temp->icon_path = strdup((const char*)key); + SLOG(LOG_DEBUG, TAG_MACONFIG, "Icon Path : %s", key); + xmlFree(key); + } + } + + cur = cur->next; + } + + if (callback) { + callback(temp->app_id, temp->name, temp->icon_path, + temp->wakeup_list, temp->cnt_wakeup, temp->supported_lang, temp->cnt_lang, user_data); + } + + if (temp->app_id) { + free((void*)temp->app_id); + } + if (temp->name) { + free((void*)temp->name); + } + if (temp->icon_path) { + free((void*)temp->icon_path); + } + for (loop = 0; loop < temp->cnt_wakeup; loop++) { + if (temp->wakeup_list[loop]) { + free((void*)(temp->wakeup_list[loop])); + } + } + for (loop = 0; loop < temp->cnt_lang; loop++) { + if (temp->supported_lang[loop]) { + free((void*)(temp->supported_lang[loop])); + } + } + + free(temp); + xmlFreeDoc(doc); + + return 0; +} + int ma_config_mgr_get_assistant_info(ma_ui_assistant_info_cb callback, void* user_data) { + const char *suffix = ".xml"; + + DIR *d; + struct dirent *dir; + d = opendir(MA_ASSISTANT_INFO); + + if (d) { + while (NULL != (dir = readdir(d))) { + if (suffix && dir->d_name && strlen(suffix) <= strlen(dir->d_name)) { + if (0 == strcmp(dir->d_name + strlen(dir->d_name) - strlen(suffix), suffix)) { + char fullpath[_POSIX_PATH_MAX]; + snprintf(fullpath, _POSIX_PATH_MAX - 1, "%s/%s", MA_ASSISTANT_INFO, dir->d_name); + SLOG(LOG_DEBUG, TAG_MAUI, "Parsing file : %s\n", fullpath); + ma_config_mgr_parse_assistant_info(callback, user_data, fullpath); + } + } + } + closedir(d); + } + return 0; } diff --git a/common/ma_config_mgr.h b/common/ma_config_mgr.h index b4e458a..de3f742 100644 --- a/common/ma_config_mgr.h +++ b/common/ma_config_mgr.h @@ -27,6 +27,18 @@ extern "C" { #endif +#define MAX_WAKEUP_LIST_NUM 32 +#define MAX_SUPPORTED_LANGUAGE_NUM 128 + +typedef struct { + const char* app_id; + const char* name; + const char* icon_path; + const char* wakeup_list[MAX_WAKEUP_LIST_NUM]; + int cnt_wakeup; + const char* supported_lang[MAX_SUPPORTED_LANGUAGE_NUM]; + int cnt_lang; +} ma_assistant_info_s; typedef void (*ma_config_lang_changed_cb)(const char* previous_lang, const char* current_lang); diff --git a/common/ma_defs.h b/common/ma_defs.h index 295839e..a5da661 100644 --- a/common/ma_defs.h +++ b/common/ma_defs.h @@ -40,6 +40,11 @@ extern "C" #define MA_BASE tzplatform_mkpath(tzplatform_getid("TZ_USER_HOME"), "share/.multiassistant"); #define MA_CONFIG tzplatform_mkpath(tzplatform_getid("TZ_USER_HOME"), "share/.multiassistant/ma-config.xml") +#define MA_CONFIG_BASE tzplatform_mkpath(TZ_USER_HOME, "share/.multiassistant") +#define MA_HOME tzplatform_mkpath(TZ_USER_HOME, "share/.multiassistant/ma") +#define MA_ASSISTANT_BASE tzplatform_mkpath(TZ_USER_HOME, "share/.multiassistant/ma/1.0") +#define MA_ASSISTANT_INFO tzplatform_mkpath(TZ_USER_HOME, "share/.multiassistant/ma/1.0/assistant-info") + /************************************************************************************** *** Definitions for DBus *************************************************************************************/ @@ -80,6 +85,17 @@ extern "C" #define MAS_UI_METHOD_SEND_RESULT "mas_ui_method_send_result" #define MAS_UI_METHOD_ERROR "mas_ui_method_error" +/************************************************************************************** + *** Definitions for xml file + *************************************************************************************/ +#define MA_TAG_ASSISTANT_BASE "multi-assistant" +#define MA_TAG_ASSISTANT_NAME "name" +#define MA_TAG_ASSISTANT_ID "id" +#define MA_TAG_ASSISTANT_ICON_PATH "icon-path" +#define MA_TAG_ASSISTANT_LANGUAGE_SET "languages" +#define MA_TAG_ASSISTANT_LANGUAGE "lang" +#define MA_TAG_ASSISTANT_WAKEUP_WORD_SET "wakeup-words" +#define MA_TAG_ASSISTANT_WAKEUP_WORD "wakeup-word" /************************************************************************************** *** Definitions for ETC diff --git a/common/ma_main.h b/common/ma_main.h index efceee8..f5214da 100644 --- a/common/ma_main.h +++ b/common/ma_main.h @@ -38,8 +38,9 @@ extern "C" /************************************************************************************** *** Definitions for log tags *************************************************************************************/ -#define TAG_MAC "mac" /* Multi-assistant client log tag */ -#define TAG_MAUI "maui" /* Multi-assistant UI client log tag */ +#define TAG_MAC "mac" /* Multi-assistant client log tag */ +#define TAG_MAUI "maui" /* Multi-assistant UI client log tag */ +#define TAG_MACONFIG "mainfo" /* Multi-assistant config lib log tag */ /************************************************************************************** *** Structures for multi-assistant handle