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