Add gitlab issue/merge request templates
[apps/launcher.git] / launcher / src / applicationmodel.cpp
1 // SPDX-License-Identifier: Apache-2.0
2 /*
3  * Copyright (C) 2016 The Qt Company Ltd.
4  * Copyright (C) 2016, 2017 Mentor Graphics Development (Deutschland) GmbH
5  * Copyright (c) 2018,2019 TOYOTA MOTOR CORPORATION
6  * Copyright (C) 2022 Konsulko Group
7  */
8
9 #include "applicationmodel.h"
10 #include "appinfo.h"
11
12 #include <QtCore/QFile>
13 #include <QtCore/QJsonObject>
14 #include <QtCore/QJsonDocument>
15 #include <QtCore/QJsonArray>
16
17 #include "hmi-debug.h"
18
19 class ApplicationModel::Private
20 {
21 public:
22     Private();
23
24     void addApp(QString icon, QString name, QString id);
25     void removeApp(QString id);
26
27     QList<AppInfo> data;
28 };
29
30 namespace {
31     QString get_icon_name(QJsonObject const &i)
32     {
33         QString icon = i["icon"].toString();
34         fprintf(stderr, "Looking for icon %s\n", icon.toLocal8Bit().data());
35         if ( !QFile::exists(icon) )
36             icon = "blank";
37         return icon;
38     }
39 }
40
41 ApplicationModel::Private::Private()
42 {
43 }
44
45 void ApplicationModel::Private::addApp(QString icon, QString name, QString id)
46 {
47     HMI_DEBUG("addApp","name: %s icon: %s id: %s.", name.toStdString().c_str(), icon.toStdString().c_str(), id.toStdString().c_str());
48     for(int i = 0; i < this->data.size(); ++i) {
49         if(this->data[i].id() == id)
50             return;
51     }
52
53     QString _icon;
54     if ( QFile::exists(icon) )
55     {
56         _icon = QString("file:%1").arg(icon);
57         fprintf(stderr, "using icon '%s'\n", _icon.toLocal8Bit().data());
58     }
59     else
60     {
61         _icon = "blank";
62     }
63
64     int pos = 0;
65     for (pos = 0; pos < this->data.size(); ++pos) {
66         if (QString::compare(this->data.at(pos).name(), name, Qt::CaseInsensitive) > 0)
67             break;
68     }
69     this->data.insert(pos, AppInfo(_icon, name, id));
70 }
71
72 void ApplicationModel::Private::removeApp(QString id)
73 {
74     HMI_DEBUG("removeApp","id: %s.",id.toStdString().c_str());
75     for (int i = 0; i < this->data.size(); ++i) {
76           if (this->data.at(i).id() == id) {
77               this->data.removeAt(i);
78               break;
79           }
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 }
133
134 QString ApplicationModel::id(int i) const
135 {
136     return data(index(i), Qt::UserRole).toString();
137 }
138
139 QString ApplicationModel::appid(int i) const
140 {
141     QString id = data(index(i), Qt::UserRole).toString();
142     return id.split("@")[0];
143 }
144
145 QString ApplicationModel::name(int i) const
146 {
147     return data(index(i), Qt::DisplayRole).toString();
148 }
149
150 void ApplicationModel::move(int from, int to)
151 {
152     QModelIndex parent;
153     if (to < 0 || to > rowCount()) return;
154     if (from < to) {
155         if (!beginMoveRows(parent, from, from, parent, to + 1)) {
156             HMI_NOTICE("launcher","from : %d, to : %d. false.", from, to);
157             return;
158         }
159         d->data.move(from, to);
160         endMoveRows();
161     } else if (from > to) {
162         if (!beginMoveRows(parent, from, from, parent, to)) {
163             HMI_NOTICE("launcher","from : %d, to : %d. false.", from, to);
164             return;
165         }
166         d->data.move(from, to);
167         endMoveRows();
168     } else {
169         HMI_NOTICE("launcher","from : %d, to : %d. false.", from, to);
170     }
171 }
172
173 void ApplicationModel::updateApplist(QStringList info)
174 {
175     QString icon = info.at(0);
176     QString name = info.at(1);
177     QString id = info.at(2);
178
179     beginResetModel();
180     if(icon == "") { // uninstall
181         d->removeApp(id);
182     }
183     else {
184         // new app
185         d->addApp(icon, name, id);
186     }
187     endResetModel();
188 }
189
190 void ApplicationModel::initAppList(QList<QMap<QString, QString>> &apps)
191 {
192     HMI_DEBUG("launcher","init application list.");
193     beginResetModel();
194     qDebug() << "ApplicationModel::initAppList: got " << apps.size() << " apps";
195     for (int i = 0; i < apps.size(); i++) {
196         d->addApp(apps[i]["icon_path"], apps[i]["name"], apps[i]["id"]);
197     }
198     endResetModel();
199 }