Home: Use icon from AFM, display a black area with text if icon unloadable
[apps/homescreen.git] / homescreen / src / applicationmodel.cpp
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  * Copyright (C) 2016, 2017 Mentor Graphics Development (Deutschland) GmbH
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 "applicationmodel.h"
19 #include "appinfo.h"
20
21 #include <QtCore/QDebug>
22
23 #include <QtDBus/QDBusInterface>
24 #include <QtDBus/QDBusReply>
25
26 #include "afm_user_daemon_proxy.h"
27
28 extern org::AGL::afm::user *afm_user_daemon_proxy;
29
30 class ApplicationModel::Private
31 {
32 public:
33     Private();
34
35     QList<AppInfo> data;
36 };
37
38 namespace {
39     QString find_icon_file(QString s)
40     {
41         auto f = QFileInfo(QString(AFM_ICON_DIR), s);
42         if (f.exists()) {
43             return f.absoluteFilePath();
44         }
45         return QString();
46     }
47
48     QString get_icon_name(QJsonObject const &i)
49     {
50         QString icon_file_name = find_icon_file(i["id"].toString());
51
52         if (! icon_file_name.isEmpty()) {
53             return QStringLiteral("file:") + icon_file_name;
54         }
55
56         QString icon = i["id"].toString().split("@").front();
57         if (icon == "hvac" || icon == "poi") {
58             icon = icon.toUpper();
59         } else if (icon == "mediaplayer") {
60             icon = "Multimedia";
61         } else {
62             icon[0] = icon[0].toUpper();
63         }
64
65         return icon;
66     }
67 }
68
69 ApplicationModel::Private::Private()
70 {
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;
80     }
81 }
82
83 ApplicationModel::ApplicationModel(QObject *parent)
84     : QAbstractListModel(parent)
85     , d(new Private())
86 {
87 }
88
89 ApplicationModel::~ApplicationModel()
90 {
91     delete this->d;
92 }
93
94 int ApplicationModel::rowCount(const QModelIndex &parent) const
95 {
96     if (parent.isValid())
97         return 0;
98
99     return this->d->data.count();
100 }
101
102 QVariant ApplicationModel::data(const QModelIndex &index, int role) const
103 {
104     QVariant ret;
105     if (!index.isValid())
106         return ret;
107
108     switch (role) {
109     case Qt::DecorationRole:
110         ret = this->d->data[index.row()].iconPath();
111         break;
112     case Qt::DisplayRole:
113         ret = this->d->data[index.row()].name();
114         break;
115     case Qt::UserRole:
116         ret = this->d->data[index.row()].id();
117         break;
118     default:
119         break;
120     }
121
122     return ret;
123 }
124
125 QHash<int, QByteArray> ApplicationModel::roleNames() const
126 {
127     QHash<int, QByteArray> roles;
128     roles[Qt::DecorationRole] = "icon";
129     roles[Qt::DisplayRole] = "name";
130     roles[Qt::UserRole] = "id";
131     return roles;
132 }