improvement
[apps/agl-service-homescreen.git] / src / hs-config.cpp
1 /*
2  * Copyright (c) 2019 TOYOTA MOTOR CORPORATION
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include "hs-config.h"
19
20 const std::array<std::string, 3> HS_Config::keys_recover_type = {   // based on hs-conf.json
21     "hs-apps",
22     "default-lastmode",
23     "normal-apps"
24 };
25 const std::string HS_Config::lastmode_json = "lastmode.json";
26 const std::string HS_Config::key_appid = "appid";
27 const std::string HS_Config::key_visibility = "visibility";
28 const std::string HS_Config::key_after = "after";
29 std::string HS_Config::root_dir = "";
30
31 /**
32  * read configuration file to memory
33  *
34  * #### Parameters
35  *  - Nothing
36  *
37  * #### Return
38  * None
39  *
40  */
41 int HS_Config::readConfig(void)
42 {
43     root_dir = std::string(getenv("AFM_APP_INSTALL_DIR"));
44     auto path = root_dir + "/etc/" + hs_conf_json;
45     if(readJsonFile(path.c_str(), &m_hs_conf) < 0) {
46         AFB_ERROR("read %s failed.", hs_conf_json.c_str());
47         return -1;
48     }
49
50     path = root_dir + "/etc/" + lastmode_json;
51     if(readJsonFile(path.c_str(), &m_lastmode) < 0) {
52         AFB_WARNING("read %s failed.", lastmode_json.c_str());
53         m_lastmode = nullptr;
54     }
55
56     return parseConfig();
57 }
58
59 /**
60  * parse configuration file contents
61  *
62  * #### Parameters
63  *  - Nothing
64  *
65  * #### Return
66  * 0 : success
67  * 1 : fail
68  *
69  */
70 int HS_Config::parseConfig(void)
71 {
72     struct json_object *handshake_obj, *times_obj, *sleep_obj;
73     if(json_object_object_get_ex(m_hs_conf, key_handshake.c_str(), &handshake_obj)
74     && json_object_object_get_ex(handshake_obj, key_times.c_str(), &times_obj)
75     && json_object_object_get_ex(handshake_obj, key_sleep.c_str(), &sleep_obj)) {
76         m_handshake_info.times = json_object_get_int(times_obj);
77         m_handshake_info.sleep = json_object_get_int(sleep_obj);
78     }
79     else {
80         AFB_WARNING("get handshake info error, use default value.");
81     }
82
83     struct json_object *recover_obj;
84     if(json_object_object_get_ex(m_hs_conf, key_recover.c_str(), &recover_obj)) {
85         for(auto &m : keys_recover_type) {
86             struct json_object *obj;
87             if(json_object_object_get_ex(recover_obj, m.c_str(), &obj)) {
88                 if(json_object_get_type(obj) != json_type_array ) {
89                     continue;
90                 }
91                 m_recover_map[m] = std::move(getRecoverAppInfo(obj));
92             }
93         }
94
95         if(m_recover_map.empty()) {
96             AFB_ERROR("get homescreen recover list failed.");
97             return -1;
98         }
99     }
100     else {
101         AFB_ERROR("get homescreen recover object failed.");
102         return -1;
103     }
104
105     if(json_object_get_type(m_lastmode) == json_type_array ) {
106         struct std::vector<recover_app_info> v_lastmode = std::move(getRecoverAppInfo(m_lastmode));
107         if(!v_lastmode.empty()) {   // got saving lastmode isn't null, instead of default lastmode
108             for(auto &it : v_lastmode) {
109                 if(it.after.empty() && !m_recover_map[keys_recover_type[1]].empty()) {
110                     it.after = m_recover_map[keys_recover_type[1]][0].after;
111                 }
112             }
113             m_recover_map[keys_recover_type[1]] = std::move(v_lastmode);
114         }
115     }
116
117     return 0;
118 }
119
120 /**
121  * get recover application information
122  * appid, visibility
123  *
124  * #### Parameters
125  *  - obj : application information
126  *
127  * #### Return
128  * recover_app_info vector
129  *
130  */
131 std::vector<struct recover_app_info> HS_Config::getRecoverAppInfo(struct json_object *obj)
132 {
133     int array_len = json_object_array_length(obj);
134     std::vector<struct recover_app_info> v_app_info;
135     for (int i = 0; i < array_len; ++i) {
136         struct json_object *info_obj = json_object_array_get_idx(obj, i);
137         struct recover_app_info info;
138         struct json_object *value_obj;
139         if(json_object_object_get_ex(info_obj, key_appid.c_str(), &value_obj)) {
140             info.appid = json_object_get_string(value_obj);
141         }
142         else {
143             AFB_ERROR("recover infomation doesn't include appid.");
144             v_app_info.clear();
145             return v_app_info;
146         }
147         if(json_object_object_get_ex(info_obj, key_visibility.c_str(), &value_obj)) {
148             std::string visibility = json_object_get_string(value_obj);
149             info.visibility = (visibility == "visible") ? true:false;
150         }
151         else {
152             info.visibility = false;
153         }
154         if(json_object_object_get_ex(info_obj, key_after.c_str(), &value_obj)) {
155             info.after = json_object_get_string(value_obj);
156         }
157         else {
158             info.after.clear();
159         }
160         v_app_info.push_back(info);
161     }
162
163     return v_app_info;
164 }