launcher: Enable scrolling for the GridView to display more apps
[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 int HomescreenHandler::getRunnablesCount(void)
51 {
52     int apps = 0;
53
54     QDBusPendingReply<QVariantList> reply = applaunch_iface->listApplications(true);
55     reply.waitForFinished();
56
57     if (reply.isError()) {
58         HMI_ERROR("Launcher","Unable to retrieve application list: %s",
59                   reply.error().message().toStdString().c_str());
60         return apps;
61     } else {
62         QVariantList applist_variant = reply.value();
63         for (auto &v: applist_variant)
64             apps++;
65     }
66
67     return apps;
68 }
69
70 void HomescreenHandler::getRunnables(void)
71 {
72     struct json_object *json_applist;
73     QString applist;
74     QStringList apps;
75
76     QDBusPendingReply<QVariantList> reply = applaunch_iface->listApplications(true);
77     reply.waitForFinished();
78     if (reply.isError()) {
79         HMI_ERROR("Launcher","Unable to retrieve application list: %s",
80                   reply.error().message().toStdString().c_str());
81         return;
82     } else {
83         QVariantList applist_variant = reply.value();
84         for (auto &v: applist_variant) {
85             QString app_id;
86             QString name;
87             QString icon_path;
88             const QDBusArgument &dbus_arg = v.value<QDBusArgument>();
89
90             dbus_arg.beginStructure();
91             dbus_arg >> app_id >> name >> icon_path;
92
93             apps << QString("{ \"name\":\"%1\", \"id\":\"%2\", \"icon\":\"%3\" }")
94                         .arg(name)
95                         .arg(app_id)
96                         .arg(icon_path);
97             dbus_arg.endStructure();
98         }
99     }
100
101     applist = QString("[ %1 ]").arg(apps.join(", "));
102     json_applist = json_tokener_parse(applist.toLocal8Bit().data());
103     if (json_applist) {
104         QString data = json_object_to_json_string(json_applist);
105         HMI_DEBUG("Launcher", "doing an emit initAppList()");
106         emit initAppList(data);
107     } else {
108         HMI_DEBUG("Launcher", "app list is invalid!");
109     }
110 }