app/: Avoid any AGL wrappers and use plain Qt
[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 <QtGui/QGuiApplication>
18 #include <QDebug>
19 #include <QUrlQuery>
20 #include <QCommandLineParser>
21 #include <QtQml/QQmlApplicationEngine>
22 #include <hvac.h>
23 #include <QtQml/QQmlContext>
24
25 #include "translator.h"
26
27 int main(int argc, char *argv[])
28 {
29         setenv("QT_QPA_PLATFORM", "wayland", 1);
30         int port;
31         QString token;
32
33         QCommandLineParser parser;
34         QGuiApplication app(argc, argv);
35
36         parser.addPositionalArgument("port",
37                 app.translate("main", "port for binding"));
38         parser.addPositionalArgument("secret",
39                 app.translate("main", "secret for binding"));
40
41         parser.addHelpOption();
42         parser.addVersionOption();
43         parser.process(app);
44         QStringList positionalArguments = parser.positionalArguments();
45
46         if (positionalArguments.length() == 2) {
47                 port = positionalArguments.takeFirst().toInt();
48                 token = positionalArguments.takeFirst();
49                 qInfo() << "setting port:" << port << ", token:" << token;
50         } else {
51                 qInfo() << "Need to specify port and token";
52                 exit(EXIT_FAILURE);
53         }
54
55         QUrl bindingAddress;
56         bindingAddress.setScheme(QStringLiteral("ws"));
57         bindingAddress.setHost(QStringLiteral("localhost"));
58         bindingAddress.setPort(port);
59         bindingAddress.setPath(QStringLiteral("/api"));
60
61         QUrlQuery query;
62         query.addQueryItem(QStringLiteral("token"), token);
63         bindingAddress.setQuery(query);
64
65         QQmlApplicationEngine engine;
66         engine.rootContext()->setContextProperty("bindingAddress", bindingAddress);
67         engine.rootContext()->setContextProperty("hvac", new HVAC(bindingAddress));
68         qmlRegisterType<Translator>("Translator", 1, 0, "Translator");
69         engine.load(QUrl(QStringLiteral("qrc:/HVAC.qml")));
70
71         return app.exec();
72 }