add binding initial code in qml
[apps/hvac.git] / app / main.cpp
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
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 <QtCore/QDebug>
18 #include <QtCore/QCommandLineParser>
19 #include <QtCore/QUrlQuery>
20 #include <QtGui/QGuiApplication>
21 #include <QtQml/QQmlApplicationEngine>
22 #include <QtQml/QQmlContext>
23 #include <QtQuickControls2/QQuickStyle>
24
25 #ifdef HAVE_LIBHOMESCREEN
26 #include <libhomescreen.hpp>
27 #endif
28
29 int main(int argc, char *argv[])
30 {
31 #ifdef HAVE_LIBHOMESCREEN
32     LibHomeScreen libHomeScreen;
33
34     if (!libHomeScreen.renderAppToAreaAllowed(0, 1)) {
35         qWarning() << "renderAppToAreaAllowed is denied";
36         return -1;
37     }
38 #endif
39
40     QGuiApplication app(argc, argv);
41     app.setApplicationName(QStringLiteral("HVAC"));
42     app.setApplicationVersion(QStringLiteral("0.1.0"));
43     app.setOrganizationDomain(QStringLiteral("automotivelinux.org"));
44     app.setOrganizationName(QStringLiteral("AutomotiveGradeLinux"));
45
46     QQuickStyle::setStyle("AGL");
47
48     QCommandLineParser parser;
49     parser.addPositionalArgument("port", app.translate("main", "port for binding"));
50     parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
51     parser.addHelpOption();
52     parser.addVersionOption();
53     parser.process(app);
54     QStringList positionalArguments = parser.positionalArguments();
55
56
57     QQmlApplicationEngine engine;
58     if (positionalArguments.length() == 2) {
59         int port = positionalArguments.takeFirst().toInt();
60         QString secret = positionalArguments.takeFirst();
61         QUrl bindingAddress;
62         bindingAddress.setScheme(QStringLiteral("ws"));
63         bindingAddress.setHost(QStringLiteral("localhost"));
64         bindingAddress.setPort(port);
65         bindingAddress.setPath(QStringLiteral("/api"));
66         QUrlQuery query;
67         query.addQueryItem(QStringLiteral("token"), secret);
68         bindingAddress.setQuery(query);
69         QQmlContext *context = engine.rootContext();
70         context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
71     }
72     engine.load(QUrl(QStringLiteral("qrc:/HVAC.qml")));
73
74     return app.exec();
75 }
76