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