2 * Copyright (C) 2016 The Qt Company Ltd.
3 * Copyright (C) 2016, 2017 Mentor Graphics Development (Deutschland) GmbH
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "applicationmodel.h"
21 #include <QtCore/QDebug>
23 #include <QtDBus/QDBusInterface>
24 #include <QtDBus/QDBusReply>
26 #include "afm_user_daemon_proxy.h"
28 extern org::AGL::afm::user *afm_user_daemon_proxy;
30 class ApplicationModel::Private
39 QString find_icon_file(QString s)
41 auto f = QFileInfo(QString(AFM_ICON_DIR), s);
43 return f.absoluteFilePath();
48 QString get_icon_name(QJsonObject const &i)
50 QString icon_file_name = find_icon_file(i["id"].toString());
52 if (! icon_file_name.isEmpty()) {
53 return QStringLiteral("file:") + icon_file_name;
56 QString icon = i["id"].toString().split("@").front();
57 if (icon == "hvac" || icon == "poi") {
58 icon = icon.toUpper();
59 } else if (icon == "mediaplayer") {
62 icon[0] = icon[0].toUpper();
69 ApplicationModel::Private::Private()
71 QString apps = afm_user_daemon_proxy->runnables(QStringLiteral(""));
72 QJsonDocument japps = QJsonDocument::fromJson(apps.toUtf8());
73 for (auto const &app : japps.array()) {
74 auto const &jso = app.toObject();
75 auto const name = jso["name"].toString();
76 auto const id = jso["id"].toString();
77 auto const icon = get_icon_name(jso);
78 this->data.append(AppInfo(icon, name, id));
79 qDebug() << "name:" << name << "icon:" << icon << "id:" << id;
83 ApplicationModel::ApplicationModel(QObject *parent)
84 : QAbstractListModel(parent)
89 ApplicationModel::~ApplicationModel()
94 int ApplicationModel::rowCount(const QModelIndex &parent) const
99 return this->d->data.count();
102 QVariant ApplicationModel::data(const QModelIndex &index, int role) const
105 if (!index.isValid())
109 case Qt::DecorationRole:
110 ret = this->d->data[index.row()].iconPath();
112 case Qt::DisplayRole:
113 ret = this->d->data[index.row()].name();
116 ret = this->d->data[index.row()].id();
125 QHash<int, QByteArray> ApplicationModel::roleNames() const
127 QHash<int, QByteArray> roles;
128 roles[Qt::DecorationRole] = "icon";
129 roles[Qt::DisplayRole] = "name";
130 roles[Qt::UserRole] = "id";