6479cd9d5958b8663acc2c811414dafcd17dbdaa
[apps/homescreen.git] / homescreen / src / main.cpp
1 /*
2  * Copyright (C) 2016, 2017 Mentor Graphics Development (Deutschland) GmbH
3  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <QGuiApplication>
19 #include <QCommandLineParser>
20 #include <QtGui/QGuiApplication>
21 #include <QtQml/QQmlApplicationEngine>
22 #include <QtQml/QQmlContext>
23 #include <QtQml/qqml.h>
24 #include <QQuickWindow>
25 #include <QThread>
26
27 #include <weather.h>
28 #include <bluetooth.h>
29 #include "applicationlauncher.h"
30 #include "statusbarmodel.h"
31 #include "afm_user_daemon_proxy.h"
32 #include "homescreenhandler.h"
33 #include "toucharea.h"
34 #include "hmi-debug.h"
35 #include <QBitmap>
36
37 // XXX: We want this DBus connection to be shared across the different
38 // QML objects, is there another way to do this, a nice way, perhaps?
39 org::AGL::afm::user *afm_user_daemon_proxy;
40
41 namespace {
42
43 struct Cleanup {
44     static inline void cleanup(org::AGL::afm::user *p) {
45         delete p;
46         afm_user_daemon_proxy = Q_NULLPTR;
47     }
48 };
49
50 void noOutput(QtMsgType, const QMessageLogContext &, const QString &)
51 {
52 }
53
54 }
55
56 int main(int argc, char *argv[])
57 {
58     QGuiApplication a(argc, argv);
59     const char* graphic_role = "homescreen";
60
61     // use launch process
62     QScopedPointer<org::AGL::afm::user, Cleanup> afm_user_daemon_proxy(new org::AGL::afm::user("org.AGL.afm.user",
63                                                                                                "/org/AGL/afm/user",
64                                                                                                QDBusConnection::sessionBus(),
65                                                                                                0));
66     ::afm_user_daemon_proxy = afm_user_daemon_proxy.data();
67
68     QCoreApplication::setOrganizationDomain("LinuxFoundation");
69     QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
70     QCoreApplication::setApplicationName("HomeScreen");
71     QCoreApplication::setApplicationVersion("0.7.0");
72
73     QCommandLineParser parser;
74     parser.addPositionalArgument("port", a.translate("main", "port for binding"));
75     parser.addPositionalArgument("secret", a.translate("main", "secret for binding"));
76     parser.addHelpOption();
77     parser.addVersionOption();
78     parser.process(a);
79     QStringList positionalArguments = parser.positionalArguments();
80
81     int port = 1700;
82     QString token = "wm";
83
84     if (positionalArguments.length() == 2) {
85         port = positionalArguments.takeFirst().toInt();
86         token = positionalArguments.takeFirst();
87     }
88
89     HMI_DEBUG("HomeScreen","port = %d, token = %s", port, token.toStdString().c_str());
90
91     // import C++ class to QML
92     // qmlRegisterType<ApplicationLauncher>("HomeScreen", 1, 0, "ApplicationLauncher");
93     qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
94
95     ApplicationLauncher *launcher = new ApplicationLauncher();
96     HomescreenHandler* homescreenHandler = new HomescreenHandler();
97     homescreenHandler->init(graphic_role, port, token.toStdString().c_str());
98
99     QUrl bindingAddress;
100     bindingAddress.setScheme(QStringLiteral("ws"));
101     bindingAddress.setHost(QStringLiteral("localhost"));
102     bindingAddress.setPort(port);
103     bindingAddress.setPath(QStringLiteral("/api"));
104
105     QUrlQuery query;
106     query.addQueryItem(QStringLiteral("token"), token);
107     bindingAddress.setQuery(query);
108
109     TouchArea* touchArea = new TouchArea();
110
111     // mail.qml loading
112     QQmlApplicationEngine engine;
113     engine.rootContext()->setContextProperty("homescreenHandler", homescreenHandler);
114     engine.rootContext()->setContextProperty("touchArea", touchArea);
115     engine.rootContext()->setContextProperty("launcher", launcher);
116     engine.rootContext()->setContextProperty("weather", new Weather(bindingAddress));
117     engine.rootContext()->setContextProperty("bluetooth", new Bluetooth(bindingAddress));
118     engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
119
120     QObject *root = engine.rootObjects().first();
121
122     WMHandler wmh;
123     wmh.on_screen_updated = [launcher, root](std::vector<std::string> list) {
124         for(const auto& i : list) {
125             HMI_DEBUG("HomeScreen", "ids=%s", i.c_str());
126         }
127         int arrLen = list.size();
128         QString label = QString("");
129         for( int idx = 0; idx < arrLen; idx++)
130         {
131             label = list[idx].c_str();
132             HMI_DEBUG("HomeScreen","Event_ScreenUpdated application: %s.", label.toStdString().c_str());
133             QMetaObject::invokeMethod(launcher, "setCurrent", Qt::QueuedConnection, Q_ARG(QString, label));
134             if(label == "launcher") {
135                 QMetaObject::invokeMethod(root, "turnToNormal");
136             } else {
137                 QMetaObject::invokeMethod(root, "turnToFullscreen");
138             }
139             if((arrLen == 1) && (QString("restriction") != label)) {
140                 QMetaObject::invokeMethod(root, "disableSplitSwitchBtn");
141             } else {
142                 QMetaObject::invokeMethod(root, "enableSplitSwitchBtn");
143             }
144         }
145     };
146     homescreenHandler->setWMHandler(wmh);
147     homescreenHandler->attach(&engine);
148
149     QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
150
151     touchArea->setWindow(window);
152     QThread* thread = new QThread;
153     touchArea->moveToThread(thread);
154     QObject::connect(thread, &QThread::started, touchArea, &TouchArea::init);
155
156     thread->start();
157
158     QList<QObject *> sobjs = engine.rootObjects();
159     StatusBarModel *statusBar = sobjs.first()->findChild<StatusBarModel *>("statusBar");
160     statusBar->init(bindingAddress, engine.rootContext());
161
162     return a.exec();
163 }