2 * Copyright (C) 2016 The Qt Company Ltd.
3 * Copyright (C) 2016, 2017 Mentor Graphics Development (Deutschland) GmbH
4 * Copyright (c) 2018 TOYOTA MOTOR CORPORATION
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 #include "applicationmodel.h"
22 #include "hmi-debug.h"
24 #include <QtDBus/QDBusInterface>
25 #include <QtDBus/QDBusReply>
27 #include "afm_user_daemon_proxy.h"
29 extern org::AGL::afm::user *afm_user_daemon_proxy;
31 class ApplicationModel::Private
40 QString get_icon_name(QJsonObject const &i)
42 QString icon = i["name"].toString().toLower();
44 if ( !QFile::exists(QString(":/images/%1_active.svg").arg(icon)) ||
45 !QFile::exists(QString(":/images/%1_inactive.svg").arg(icon)) )
53 ApplicationModel::Private::Private()
55 QString apps = afm_user_daemon_proxy->runnables(QStringLiteral(""));
56 QJsonDocument japps = QJsonDocument::fromJson(apps.toUtf8());
57 for (auto const &app : japps.array()) {
58 QJsonObject const &jso = app.toObject();
59 auto const name = jso["name"].toString();
60 auto const id = jso["id"].toString();
61 auto const icon = get_icon_name(jso);
63 // Hide HomeScreen icon itself
64 if (name != "launcher" && name != "homescreen-2017" && name != "OnScreenApp") {
65 this->data.append(AppInfo(icon, name, id));
68 HMI_DEBUG("launcher","name: %s icon: %s id: %s.", name.toStdString().c_str(), icon.toStdString().c_str(), id.toStdString().c_str());
72 ApplicationModel::ApplicationModel(QObject *parent)
73 : QAbstractListModel(parent)
78 ApplicationModel::~ApplicationModel()
83 int ApplicationModel::rowCount(const QModelIndex &parent) const
88 return this->d->data.count();
91 QVariant ApplicationModel::data(const QModelIndex &index, int role) const
98 case Qt::DecorationRole:
99 ret = this->d->data[index.row()].iconPath();
101 case Qt::DisplayRole:
102 ret = this->d->data[index.row()].name();
105 ret = this->d->data[index.row()].id();
114 QHash<int, QByteArray> ApplicationModel::roleNames() const
116 QHash<int, QByteArray> roles;
117 roles[Qt::DecorationRole] = "icon";
118 roles[Qt::DisplayRole] = "name";
119 roles[Qt::UserRole] = "id";
123 QString ApplicationModel::id(int i) const
125 return data(index(i), Qt::UserRole).toString();
128 QString ApplicationModel::name(int i) const
130 return data(index(i), Qt::DisplayRole).toString();
133 void ApplicationModel::move(int from, int to)
136 if (to < 0 || to > rowCount()) return;
138 if (!beginMoveRows(parent, from, from, parent, to + 1)) {
139 HMI_NOTICE("launcher","from : %d, to : %d. false.", from, to);
142 d->data.move(from, to);
144 } else if (from > to) {
145 if (!beginMoveRows(parent, from, from, parent, to)) {
146 HMI_NOTICE("launcher","from : %d, to : %d. false.", from, to);
149 d->data.move(from, to);
152 HMI_NOTICE("launcher","from : %d, to : %d. false.", from, to);