9cf7e612fbe300f052f41449bec0f56bb42b4718
[apps/launcher.git] / launcher / src / homescreenhandler.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  * Copyright (c) 2018,2019 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 <QDBusMessage>
19 #include <QDBusConnection>
20 #include "homescreenhandler.h"
21 #include "hmi-debug.h"
22
23 #include <json.h>
24
25 #define APPLAUNCH_DBUS_IFACE     "org.automotivelinux.AppLaunch"
26 #define APPLAUNCH_DBUS_OBJECT    "/org/automotivelinux/AppLaunch"
27
28 HomescreenHandler::HomescreenHandler(QObject *parent) : QObject(parent)
29 {
30     applaunch_iface = new org::automotivelinux::AppLaunch(APPLAUNCH_DBUS_IFACE, APPLAUNCH_DBUS_OBJECT, QDBusConnection::sessionBus(), this);
31 }
32
33 HomescreenHandler::~HomescreenHandler()
34 {
35 }
36
37 void HomescreenHandler::tapShortcut(QString application_id)
38 {
39     HMI_DEBUG("Launcher","tapShortcut %s", application_id.toStdString().c_str());
40
41     QDBusPendingReply<> reply = applaunch_iface->start(application_id);
42     reply.waitForFinished();
43     if (reply.isError()) {
44         HMI_ERROR("Launcher","Unable to start application '%s': %s",
45                   application_id.toStdString().c_str(),
46                   reply.error().message().toStdString().c_str());
47     }
48 }
49
50 void HomescreenHandler::getRunnables(void)
51 {
52         struct json_object *json_applist;
53         QString applist;
54     QStringList apps;
55
56     QDBusPendingReply<QVariantList> reply = applaunch_iface->listApplications(true);
57     reply.waitForFinished();
58     if (reply.isError()) {
59         HMI_ERROR("Launcher","Unable to retrieve application list: %s",
60                   reply.error().message().toStdString().c_str());
61         return;
62     } else {
63         QVariantList applist_variant = reply.value();
64         for (auto &v: applist_variant) {
65             QString app_id;
66             QString name;
67             QString icon_path;
68             const QDBusArgument &dbus_arg = v.value<QDBusArgument>();
69
70             dbus_arg.beginStructure();
71             dbus_arg >> app_id >> name >> icon_path;
72
73             apps << QString("{ \"name\":\"%1\", \"id\":\"%2\", \"icon\":\"%3\" }")
74                         .arg(name)
75                         .arg(app_id)
76                         .arg(icon_path);
77             dbus_arg.endStructure();
78         }
79     }
80
81     applist = QString("[ %1 ]").arg(apps.join(", "));
82     json_applist = json_tokener_parse(applist.toLocal8Bit().data());
83     if (json_applist) {
84         QString data = json_object_to_json_string(json_applist);
85         HMI_DEBUG("Launcher", "doing an emit initAppList()");
86         emit initAppList(data);
87     } else {
88         HMI_DEBUG("Launcher", "app list is invalid!");
89     }
90 }