app/: Fixes and some monior tweaks to the qml file
[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     fprintf(stderr, "OnScreenModel::setModel: setModel start!\n");
31
32     clearModel();
33     struct json_object *j_title = nullptr, *j_type = nullptr, *j_contents = nullptr, *j_buttons = nullptr;
34     struct json_object *j_param = json_tokener_parse(data.toString().toStdString().c_str());
35
36     fprintf(stderr, "OnScreenModel::setModel receiving end: %s\n", data.toString().toStdString().c_str());
37
38     if(json_object_object_get_ex(j_param, _title, &j_title)) {
39         m_title = json_object_get_string(j_title);
40     }
41     else {
42         fprintf(stderr, "OnScreenModel::setModel: title input is null\n");
43     }
44
45     if(json_object_object_get_ex(j_param, _type, &j_type)) {
46         m_type = json_object_get_string(j_type);
47     }
48     else {
49         fprintf(stderr, "OnScreenModel::setModel: type input is null\n");
50     }
51
52     if(json_object_object_get_ex(j_param, _contents, &j_contents)) {
53         m_contents = json_object_get_string(j_contents);
54     }
55     else {
56         fprintf(stderr, "OnScreenModel::setModel: contents input is null\n");
57     }
58
59     if(json_object_object_get_ex(j_param, _buttons, &j_buttons)) {
60         if(json_object_get_type(j_buttons) != json_type_array) {
61             fprintf(stderr, "OnScreenModel::setModel buttons josn type isn't array!\n");
62         }
63         else {
64             m_buttons.clear();
65             int len = json_object_array_length(j_buttons);
66             struct json_object *json_tmp = nullptr;
67             for (int i = 0; i < len; i++) {
68                 json_tmp = json_object_array_get_idx(j_buttons, i);
69                 m_buttons << QString(json_object_get_string(json_tmp));
70             }
71         }
72     }
73     else {
74         fprintf(stderr, "OnScreenModel::setModel: buttons input is null\n");
75     }
76
77     fprintf(stderr, "OnScreenModel::setModel setModel end!titile=%s,type=%s,contents=%s,btnNum=%d\n",
78               m_title.toStdString().c_str(), m_type.toStdString().c_str(), m_contents.toStdString().c_str(), m_buttons.size());
79 }
80
81 QString OnScreenModel::buttonName(int index) const
82 {
83     if(index >= m_buttons.size())
84         return QString("");
85     else
86         return m_buttons[index];
87 }
88
89 void OnScreenModel::clearModel(void)
90 {
91     m_title = "";
92     m_type = "";
93     m_contents = "";
94     m_buttons.clear();
95 }