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
36 void addApp(QString icon, QString name, QString id);
37 void removeApp(QString id);
43 QString get_icon_name(QJsonObject const &i)
45 QString icon = i["name"].toString().toLower();
47 if ( !QFile::exists(QString(":/images/%1_active.svg").arg(icon)) ||
48 !QFile::exists(QString(":/images/%1_inactive.svg").arg(icon)) )
56 ApplicationModel::Private::Private()
60 void ApplicationModel::Private::addApp(QString icon, QString name, QString id)
62 HMI_DEBUG("addApp","name: %s icon: %s id: %s.", name.toStdString().c_str(), icon.toStdString().c_str(), id.toStdString().c_str());
63 for(int i = 0; i < this->data.size(); ++i) {
64 if(this->data[i].id() == id)
68 QString _icon = name.toLower();
69 if ( !QFile::exists(QString(":/images/%1_active.svg").arg(_icon)) ||
70 !QFile::exists(QString(":/images/%1_inactive.svg").arg(_icon)) )
74 this->data.append(AppInfo(_icon, name, id));
77 void ApplicationModel::Private::removeApp(QString id)
79 HMI_DEBUG("removeApp","id: %s.",id.toStdString().c_str());
80 for (int i = 0; i < this->data.size(); ++i) {
81 if (this->data.at(i).id() == id) {
82 this->data.removeAt(i);
88 ApplicationModel::ApplicationModel(QObject *parent)
89 : QAbstractListModel(parent)
94 ApplicationModel::~ApplicationModel()
99 int ApplicationModel::rowCount(const QModelIndex &parent) const
101 if (parent.isValid())
104 return this->d->data.count();
107 QVariant ApplicationModel::data(const QModelIndex &index, int role) const
110 if (!index.isValid())
114 case Qt::DecorationRole:
115 ret = this->d->data[index.row()].iconPath();
117 case Qt::DisplayRole:
118 ret = this->d->data[index.row()].name();
121 ret = this->d->data[index.row()].id();
130 QHash<int, QByteArray> ApplicationModel::roleNames() const
132 QHash<int, QByteArray> roles;
133 roles[Qt::DecorationRole] = "icon";
134 roles[Qt::DisplayRole] = "name";
135 roles[Qt::UserRole] = "id";
139 QString ApplicationModel::id(int i) const
141 return data(index(i), Qt::UserRole).toString();
144 QString ApplicationModel::appid(int i) const
146 QString id = data(index(i), Qt::UserRole).toString();
147 return id.split("@")[0];
150 QString ApplicationModel::name(int i) const
152 return data(index(i), Qt::DisplayRole).toString();
155 void ApplicationModel::move(int from, int to)
158 if (to < 0 || to > rowCount()) return;
160 if (!beginMoveRows(parent, from, from, parent, to + 1)) {
161 HMI_NOTICE("launcher","from : %d, to : %d. false.", from, to);
164 d->data.move(from, to);
166 } else if (from > to) {
167 if (!beginMoveRows(parent, from, from, parent, to)) {
168 HMI_NOTICE("launcher","from : %d, to : %d. false.", from, to);
171 d->data.move(from, to);
174 HMI_NOTICE("launcher","from : %d, to : %d. false.", from, to);
178 void ApplicationModel::updateApplist(QStringList info)
180 QString icon = info.at(0);
181 QString name = info.at(1);
182 QString id = info.at(2);
185 if(icon == "") { // uninstall
190 d->addApp(icon, name, id);
195 void ApplicationModel::initAppList(QString data)
197 HMI_DEBUG("launcher","init application list.");
199 QJsonDocument japps = QJsonDocument::fromJson(data.toUtf8());
200 for (auto const &app : japps.array()) {
201 QJsonObject const &jso = app.toObject();
202 auto const name = jso["name"].toString();
203 auto const id = jso["id"].toString();
204 auto const icon = get_icon_name(jso);
206 d->addApp(icon, name, id);