Start app and get runnables list by homescreen
[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,2019 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 }
59
60 void ApplicationModel::Private::addApp(QString icon, QString name, QString id)
61 {
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)
65             return;
66     }
67
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)) )
71     {
72         _icon = "blank";
73     }
74
75     int pos = 0;
76     for (pos = 0; pos < this->data.size(); ++pos) {
77         if (QString::compare(this->data.at(pos).name(), name, Qt::CaseInsensitive) > 0)
78             break;
79     }
80     this->data.insert(pos, AppInfo(_icon, name, id));
81 }
82
83 void ApplicationModel::Private::removeApp(QString id)
84 {
85     HMI_DEBUG("removeApp","id: %s.",id.toStdString().c_str());
86     for (int i = 0; i < this->data.size(); ++i) {
87           if (this->data.at(i).id() == id) {
88               this->data.removeAt(i);
89               break;
90           }
91     }
92 }
93
94 ApplicationModel::ApplicationModel(QObject *parent)
95     : QAbstractListModel(parent)
96     , d(new Private())
97 {
98 }
99
100 ApplicationModel::~ApplicationModel()
101 {
102     delete this->d;
103 }
104
105 int ApplicationModel::rowCount(const QModelIndex &parent) const
106 {
107     if (parent.isValid())
108         return 0;
109
110     return this->d->data.count();
111 }
112
113 QVariant ApplicationModel::data(const QModelIndex &index, int role) const
114 {
115     QVariant ret;
116     if (!index.isValid())
117         return ret;
118
119     switch (role) {
120     case Qt::DecorationRole:
121         ret = this->d->data[index.row()].iconPath();
122         break;
123     case Qt::DisplayRole:
124         ret = this->d->data[index.row()].name();
125         break;
126     case Qt::UserRole:
127         ret = this->d->data[index.row()].id();
128         break;
129     default:
130         break;
131     }
132
133     return ret;
134 }
135
136 QHash<int, QByteArray> ApplicationModel::roleNames() const
137 {
138     QHash<int, QByteArray> roles;
139     roles[Qt::DecorationRole] = "icon";
140     roles[Qt::DisplayRole] = "name";
141     roles[Qt::UserRole] = "id";
142     return roles;
143 }
144
145 QString ApplicationModel::id(int i) const
146 {
147     return data(index(i), Qt::UserRole).toString();
148 }
149
150 QString ApplicationModel::appid(int i) const
151 {
152     QString id = data(index(i), Qt::UserRole).toString();
153     return id.split("@")[0];
154 }
155
156 QString ApplicationModel::name(int i) const
157 {
158     return data(index(i), Qt::DisplayRole).toString();
159 }
160
161 void ApplicationModel::move(int from, int to)
162 {
163     QModelIndex parent;
164     if (to < 0 || to > rowCount()) return;
165     if (from < to) {
166         if (!beginMoveRows(parent, from, from, parent, to + 1)) {
167             HMI_NOTICE("launcher","from : %d, to : %d. false.", from, to);
168             return;
169         }
170         d->data.move(from, to);
171         endMoveRows();
172     } else if (from > to) {
173         if (!beginMoveRows(parent, from, from, parent, to)) {
174             HMI_NOTICE("launcher","from : %d, to : %d. false.", from, to);
175             return;
176         }
177         d->data.move(from, to);
178         endMoveRows();
179     } else {
180         HMI_NOTICE("launcher","from : %d, to : %d. false.", from, to);
181     }
182 }
183
184 void ApplicationModel::updateApplist(QStringList info)
185 {
186     QString icon = info.at(0);
187     QString name = info.at(1);
188     QString id = info.at(2);
189
190     beginResetModel();
191     if(icon == "") { // uninstall
192         d->removeApp(id);
193     }
194     else {
195         // new app
196         d->addApp(icon, name, id);
197     }
198     endResetModel();
199 }
200
201 void ApplicationModel::initAppList(QString data)
202 {
203     HMI_DEBUG("launcher","init application list.");
204     beginResetModel();
205     QJsonDocument japps = QJsonDocument::fromJson(data.toUtf8());
206     for (auto const &app : japps.array()) {
207         QJsonObject const &jso = app.toObject();
208         auto const name = jso["name"].toString();
209         auto const id = jso["id"].toString();
210         auto const icon = get_icon_name(jso);
211
212         d->addApp(icon, name, id);
213     }
214     endResetModel();
215 }