get runnables list from hss
[apps/launcher.git] / launcher / src / applicationmodel.cpp
1 /*
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
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #include "applicationmodel.h"
20 #include "appinfo.h"
21
22 #include "hmi-debug.h"
23
24 #include <QtDBus/QDBusInterface>
25 #include <QtDBus/QDBusReply>
26
27 #include "afm_user_daemon_proxy.h"
28
29 extern org::AGL::afm::user *afm_user_daemon_proxy;
30
31 class ApplicationModel::Private
32 {
33 public:
34     Private();
35
36     void addApp(QString icon, QString name, QString id);
37     void removeApp(QString id);
38
39     QList<AppInfo> data;
40 };
41
42 namespace {
43     QString get_icon_name(QJsonObject const &i)
44     {
45         QString icon = i["name"].toString().toLower();
46
47         if ( !QFile::exists(QString(":/images/%1_active.svg").arg(icon)) ||
48              !QFile::exists(QString(":/images/%1_inactive.svg").arg(icon)) )
49         {
50             icon = "blank";
51         }
52         return icon;
53     }
54 }
55
56 ApplicationModel::Private::Private()
57 {
58 //    QString apps = afm_user_daemon_proxy->runnables(QStringLiteral(""));
59 //    QJsonDocument japps = QJsonDocument::fromJson(apps.toUtf8());
60 //    for (auto const &app : japps.array()) {
61 //        QJsonObject const &jso = app.toObject();
62 //        auto const name = jso["name"].toString();
63 //        auto const id = jso["id"].toString();
64 //        auto const icon = get_icon_name(jso);
65
66 //        if ( name != "launcher" &&
67 //             name != "homescreen-2017" &&
68 //             name != "homescreen" &&
69 //             name != "OnScreenApp") {
70 //            this->data.append(AppInfo(icon, name, id));
71 //        }
72
73 //        HMI_DEBUG("launcher","name: %s icon: %s id: %s.", name.toStdString().c_str(), icon.toStdString().c_str(), id.toStdString().c_str());
74 //    }
75 }
76
77 void ApplicationModel::Private::addApp(QString icon, QString name, QString id)
78 {
79     HMI_DEBUG("addApp","name: %s icon: %s id: %s.", name.toStdString().c_str(), icon.toStdString().c_str(), id.toStdString().c_str());
80     for(int i = 0; i < this->data.size(); ++i) {
81         if(this->data[i].id() == id)
82             return;
83     }
84
85     QString _icon = name.toLower();
86     if ( !QFile::exists(QString(":/images/%1_active.svg").arg(_icon)) ||
87          !QFile::exists(QString(":/images/%1_inactive.svg").arg(_icon)) )
88     {
89         _icon = "blank";
90     }
91     this->data.append(AppInfo(_icon, name, id));
92 }
93
94 void ApplicationModel::Private::removeApp(QString id)
95 {
96     HMI_DEBUG("removeApp","id: %s.",id.toStdString().c_str());
97     for (int i = 0; i < this->data.size(); ++i) {
98           if (this->data.at(i).id() == id) {
99               this->data.removeAt(i);
100               break;
101           }
102     }
103 }
104
105 ApplicationModel::ApplicationModel(QObject *parent)
106     : QAbstractListModel(parent)
107     , d(new Private())
108 {
109 }
110
111 ApplicationModel::~ApplicationModel()
112 {
113     delete this->d;
114 }
115
116 int ApplicationModel::rowCount(const QModelIndex &parent) const
117 {
118     if (parent.isValid())
119         return 0;
120
121     return this->d->data.count();
122 }
123
124 QVariant ApplicationModel::data(const QModelIndex &index, int role) const
125 {
126     QVariant ret;
127     if (!index.isValid())
128         return ret;
129
130     switch (role) {
131     case Qt::DecorationRole:
132         ret = this->d->data[index.row()].iconPath();
133         break;
134     case Qt::DisplayRole:
135         ret = this->d->data[index.row()].name();
136         break;
137     case Qt::UserRole:
138         ret = this->d->data[index.row()].id();
139         break;
140     default:
141         break;
142     }
143
144     return ret;
145 }
146
147 QHash<int, QByteArray> ApplicationModel::roleNames() const
148 {
149     QHash<int, QByteArray> roles;
150     roles[Qt::DecorationRole] = "icon";
151     roles[Qt::DisplayRole] = "name";
152     roles[Qt::UserRole] = "id";
153     return roles;
154 }
155
156 QString ApplicationModel::id(int i) const
157 {
158     return data(index(i), Qt::UserRole).toString();
159 }
160
161 QString ApplicationModel::appid(int i) const
162 {
163     QString id = data(index(i), Qt::UserRole).toString();
164     return id.split("@")[0];
165 }
166
167 QString ApplicationModel::name(int i) const
168 {
169     return data(index(i), Qt::DisplayRole).toString();
170 }
171
172 void ApplicationModel::move(int from, int to)
173 {
174     QModelIndex parent;
175     if (to < 0 || to > rowCount()) return;
176     if (from < to) {
177         if (!beginMoveRows(parent, from, from, parent, to + 1)) {
178             HMI_NOTICE("launcher","from : %d, to : %d. false.", from, to);
179             return;
180         }
181         d->data.move(from, to);
182         endMoveRows();
183     } else if (from > to) {
184         if (!beginMoveRows(parent, from, from, parent, to)) {
185             HMI_NOTICE("launcher","from : %d, to : %d. false.", from, to);
186             return;
187         }
188         d->data.move(from, to);
189         endMoveRows();
190     } else {
191         HMI_NOTICE("launcher","from : %d, to : %d. false.", from, to);
192     }
193 }
194
195 void ApplicationModel::updateApplist(QStringList info)
196 {
197     QString icon = info.at(0);
198     QString name = info.at(1);
199     QString id = info.at(2);
200     QString appid = id.split('@')[0];
201
202     beginResetModel();
203     if(icon == "") { // uninstall
204         d->removeApp(id);
205     }
206     else {
207         // new app
208         d->addApp(icon, name, id);
209     }
210     endResetModel();
211 }
212
213 void ApplicationModel::initAppList(QString data)
214 {
215     beginResetModel();
216     QJsonDocument japps = QJsonDocument::fromJson(data.toUtf8());
217     for (auto const &app : japps.array()) {
218         QJsonObject const &jso = app.toObject();
219         auto const name = jso["name"].toString();
220         auto const id = jso["id"].toString();
221         auto const icon = get_icon_name(jso);
222
223         d->addApp(icon, name, id);
224         HMI_DEBUG("launcher","name: %s icon: %s id: %s.", name.toStdString().c_str(), icon.toStdString().c_str(), id.toStdString().c_str());
225     }
226     endResetModel();
227 }