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