Add onscreenapp
[apps/onscreenapp.git] / app / onscreenmodel.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 #include "onscreenmodel.h"
18 #include "hmi-debug.h"
19 #include <json-c/json.h>
20
21 const char _modelName[] = "OnScreenModel";
22 const char _title[] = "title";
23 const char _type[] = "type";
24 const char _contents[] = "contents";
25 const char _buttons[] = "buttons";
26
27
28 void OnScreenModel::setModel(QVariant data)
29 {
30     HMI_DEBUG(_modelName, "setModel start!");
31     clearModel();
32     struct json_object *j_title = nullptr, *j_type = nullptr, *j_contents = nullptr, *j_buttons = nullptr;
33     struct json_object *j_param = json_tokener_parse(data.toString().toStdString().c_str());
34     if(json_object_object_get_ex(j_param, _title, &j_title)) {
35         m_title = json_object_get_string(j_title);
36     }
37     else {
38         HMI_DEBUG(_modelName, "title input is null");
39     }
40
41     if(json_object_object_get_ex(j_param, _type, &j_type)) {
42         m_type = json_object_get_string(j_type);
43     }
44     else {
45         HMI_DEBUG(_modelName, "type input is null");
46     }
47
48     if(json_object_object_get_ex(j_param, _contents, &j_contents)) {
49         m_contents = json_object_get_string(j_contents);
50     }
51     else {
52         HMI_DEBUG(_modelName, "contents input is null");
53     }
54
55     if(json_object_object_get_ex(j_param, _buttons, &j_buttons)) {
56         if(json_object_get_type(j_buttons) != json_type_array) {
57             HMI_DEBUG(_modelName, "buttons josn type isn't array!");
58         }
59         else {
60             m_buttons.clear();
61             int len = json_object_array_length(j_buttons);
62             struct json_object *json_tmp = nullptr;
63             for (int i = 0; i < len; i++) {
64                 json_tmp = json_object_array_get_idx(j_buttons, i);
65                 m_buttons << QString(json_object_get_string(json_tmp));
66             }
67         }
68     }
69     else {
70         HMI_DEBUG("OnScreenModel", "buttons input is null");
71     }
72     HMI_DEBUG(_modelName, "setModel end!titile=%s,type=%s,contents=%s,btnNum=%d",
73               m_title.toStdString().c_str(), m_type.toStdString().c_str(), m_contents.toStdString().c_str(), m_buttons.size());
74 }
75
76 QString OnScreenModel::buttonName(int index) const
77 {
78     if(index >= m_buttons.size())
79         return QString("");
80     else
81         return m_buttons[index];
82 }
83
84 void OnScreenModel::clearModel(void)
85 {
86     m_title = "";
87     m_type = "";
88     m_contents = "";
89     m_buttons.clear();
90 }