app: Add the app state events
[apps/onscreenapp.git] / app / main.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 <QGuiApplication>
18 #include <QCommandLineParser>
19 #include <QtGui/QGuiApplication>
20 #include <QtQml/QQmlApplicationEngine>
21 #include <QtQml/QQmlContext>
22 #include <QtQml/qqml.h>
23 #include <QQuickWindow>
24 #include <QtQuickControls2/QQuickStyle>
25 #include <QVariant>
26 #include <QTimer>
27
28 #include <json-c/json.h>
29
30 #include "eventhandler.h"
31 #include "onscreenmodel.h"
32
33 /*
34 {
35    "application_id": "onscreenapp",
36    "title": "onscreen title",
37    "type": "information",
38    "contents": "message contents",
39    "buttons": ["button_name1"],
40    "replyto":"onstestapp"
41 }
42 */
43 static struct json_object *
44 build_fake_json(void)
45 {
46         struct json_object *tmp;
47         struct json_object *obj = json_object_new_object();
48         struct json_object *array = json_object_new_array();
49
50         tmp = json_object_new_string("Big Title");
51         json_object_object_add(obj, "title", tmp);
52
53         tmp = json_object_new_string("Informatin");
54         json_object_object_add(obj, "type", tmp);
55
56         tmp = json_object_new_string("Our message contents");
57         json_object_object_add(obj, "contents", tmp);
58
59         tmp = json_object_new_string("onstestapp");
60         json_object_object_add(obj, "replyto", tmp);
61
62         /* array */
63         tmp = json_object_new_string("Button");
64         json_object_array_add(array, tmp);
65         json_object_object_add(obj, "buttons", array);
66
67         tmp = json_object_new_string(APP_ID);
68         json_object_object_add(obj, "application_id", tmp);
69
70         /* adds the entire object */
71         return obj;
72 }
73
74 int main(int argc, char *argv[])
75 {
76     QGuiApplication app(argc, argv);
77
78 #if 0
79     QCoreApplication::setOrganizationDomain("LinuxFoundation");
80     QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
81     QCoreApplication::setApplicationName("Onscreenapp");
82     QCoreApplication::setApplicationVersion("0.1.0");
83 #endif
84
85     // this is necessary to identify app, setApplicationName is only for the
86     // title
87     app.setDesktopFileName(APP_ID);
88
89     //QQuickStyle::setStyle("AGL");
90
91     QCommandLineParser parser;
92     parser.addPositionalArgument("port", app.translate("main", "port for binding"));
93     parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
94     parser.addHelpOption();
95     parser.addVersionOption();
96     parser.process(app);
97     QStringList positionalArguments = parser.positionalArguments();
98
99     int port = 1700;
100     QString token = "wm";
101
102     if (positionalArguments.length() == 2) {
103         port = positionalArguments.takeFirst().toInt();
104         token = positionalArguments.takeFirst();
105     }
106
107     HMI_DEBUG(APP_ID, "port = %d, token = %s", port, token.toStdString().c_str());
108
109     QQmlApplicationEngine engine;
110     EventHandler *eventHandler = new EventHandler();
111     eventHandler->init(port, token.toStdString().c_str());
112     engine.rootContext()->setContextProperty("eventHandler", eventHandler);
113     qmlRegisterType<OnScreenModel>("OnScreenModel", 1, 0, "OnScreenModel");
114     engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
115     if (engine.rootObjects().isEmpty()) {
116         HMI_DEBUG(APP_ID, "Fatal Error, rootObject is empty!");
117         return -1;
118     }
119
120     QObject *root = engine.rootObjects().first();
121     QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
122
123     QObject::connect(eventHandler, SIGNAL(updateModel(QVariant)), window, SLOT(setOnScreenModel(QVariant)));
124     QObject::connect(eventHandler, SIGNAL(showOnScreen()), window, SLOT(showOnScreen()));
125     QObject::connect(eventHandler, SIGNAL(hideOnScreen()), window, SLOT(hideOnScreen()));
126
127
128     HMI_DEBUG(APP_ID, "onscreenapp started!");
129
130     /* fake(s) an event */
131 #if 0
132     QTimer::singleShot(1000, [eventHandler](){
133                 struct json_object *json = build_fake_json();
134                 const char *json_str = json_object_to_json_string_ext(json,
135                                         JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY);
136                 emit eventHandler->updateModel(QVariant(json_str));
137                 emit eventHandler->showOnScreen();
138     });
139 #endif
140
141     return app.exec();
142 }
143