2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "shortcutappmodel.h"
18 #include "hmi-debug.h"
20 #define SHORTCUTKEY_PATH "/var/local/lib/afm/applications/homescreen/0.1/etc/registeredApp.json"
22 class ShortcutAppModel::Private
27 QList<RegisterApp> data;
30 ShortcutAppModel::Private::Private()
35 ShortcutAppModel::ShortcutAppModel(QObject *parent)
36 : QAbstractListModel(parent)
42 ShortcutAppModel::~ShortcutAppModel()
47 int ShortcutAppModel::rowCount(const QModelIndex &parent) const
52 return this->d->data.count();
55 QVariant ShortcutAppModel::data(const QModelIndex &index, int role) const
62 case Qt::DecorationRole:
63 ret = this->d->data[index.row()].icon;
66 ret = this->d->data[index.row()].name;
69 ret = this->d->data[index.row()].id;
78 QHash<int, QByteArray> ShortcutAppModel::roleNames() const
80 QHash<int, QByteArray> roles;
81 roles[Qt::DecorationRole] = "icon";
82 roles[Qt::DisplayRole] = "name";
83 roles[Qt::UserRole] = "id";
87 void ShortcutAppModel::changeShortcut(QString id, QString name, QString position)
89 for(int i = 1; i < d->data.size(); i++) {
90 if(id == d->data.at(i).id) {
94 d->data.removeAt(position.toInt() + 1);
99 temp.icon = temp.icon = getIconPath(temp.id);
100 if (temp.icon == "") {
103 temp.isBlank = false;
106 d->data.insert(position.toInt() + 1, temp);
108 emit updateShortcut();
109 struct json_object* obj = makeAppListJson();
110 emit shortcutUpdated(QString("launcher"), obj);
113 struct json_object* ShortcutAppModel::makeAppListJson()
115 struct json_object* obj = json_object_new_object();
116 struct json_object* obj_array = json_object_new_array();
117 for(int i = 1; i < d->data.size(); i++)
119 struct json_object* obj_shortcut = json_object_new_object();
120 json_object_object_add(obj_shortcut, "shortcut_id", json_object_new_string(d->data.at(i).id.toStdString().c_str()));
121 json_object_object_add(obj_shortcut, "shortcut_name", json_object_new_string(d->data.at(i).name.toStdString().c_str()));
122 json_object_array_add(obj_array, obj_shortcut);
124 json_object_object_add(obj, "shortcut", obj_array);
128 QString ShortcutAppModel::getId(int index) const
130 return d->data.at(index).id;
133 QString ShortcutAppModel::getName(int index) const
135 return d->data.at(index).name;
138 QString ShortcutAppModel::getIcon(int index) const
140 return d->data.at(index).icon;
143 bool ShortcutAppModel::isBlank(int index) const
145 return d->data.at(index).isBlank;
148 QString ShortcutAppModel::getIconPath(QString id)
150 QString name = id.section('@', 0, 0);
151 QString version = id.section('@', 1, 1);
152 QString boardIconPath = "/var/local/lib/afm/applications/" + name + "/" + version + "/icon.svg";
153 QString appIconPath = ":/images/Shortcut/" + name + ".svg";
154 if (QFile::exists(boardIconPath)) {
155 return "file://" + boardIconPath;
156 } else if (QFile::exists(appIconPath)) {
157 return appIconPath.section('/', 1, -1);
162 void ShortcutAppModel::getAppQueue()
164 QProcess *process = new QProcess(this);
166 process->start("cp /var/local/lib/afm/applications/homescreen/0.1/etc/registeredApp.aaa.json /var/local/lib/afm/applications/homescreen/0.1/etc/registeredApp.json");
168 process->start("cp /var/local/lib/afm/applications/homescreen/0.1/etc/registeredApp.json /var/local/lib/afm/applications/homescreen/0.1/etc/registeredApp.aaa.json");
170 QThread::msleep(300);
172 QFile file(SHORTCUTKEY_PATH);
173 if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
174 QByteArray allData = file.readAll();
175 QString str(allData);
180 QJsonParseError json_error;
181 QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error));
183 if(json_error.error != QJsonParseError::NoError)
185 HMI_ERROR("HomeScreen", "registeredApp.json error");
189 QJsonObject rootObj = jsonDoc.object();
191 QJsonObject subObj = rootObj.value("1st shortcut key").toObject();
192 setAppQueuePoint(subObj["id"].toString(), subObj["name"].toString());
193 subObj = rootObj.value("2nd shortcut key").toObject();
194 setAppQueuePoint(subObj["id"].toString(), subObj["name"].toString());
195 subObj = rootObj.value("3rd shortcut key").toObject();
196 setAppQueuePoint(subObj["id"].toString(), subObj["name"].toString());
197 subObj = rootObj.value("4th shortcut key").toObject();
198 setAppQueuePoint(subObj["id"].toString(), subObj["name"].toString());
203 void ShortcutAppModel::setAppQueuePoint(QString id, QString name)
206 app.icon = getIconPath(app.id);
207 if (app.icon == "") {
216 void ShortcutAppModel::screenUpdated()
218 struct json_object* obj = makeAppListJson();
219 emit shortcutUpdated(QString("launcher"), obj);
222 void ShortcutAppModel::setAppQueue()
224 QFile file(SHORTCUTKEY_PATH);
225 if(file.open(QIODevice::WriteOnly | QIODevice::Text)) {
226 QJsonObject rootObj, subObj1, subObj2, subObj3, subObj4;
227 subObj1.insert("id", d->data.at(0).id);
228 subObj1.insert("name", d->data.at(0).name);
229 subObj2.insert("id", d->data.at(1).id);
230 subObj2.insert("name", d->data.at(1).name);
231 subObj3.insert("id", d->data.at(2).id);
232 subObj3.insert("name", d->data.at(2).name);
233 subObj4.insert("id", d->data.at(3).id);
234 subObj4.insert("name", d->data.at(3).name);
235 rootObj.insert("1st shortcut key", subObj1);
236 rootObj.insert("2nd shortcut key", subObj2);
237 rootObj.insert("3rd shortcut key", subObj3);
238 rootObj.insert("4th shortcut key", subObj4);
240 QJsonDocument jsonDoc;
241 jsonDoc.setObject(rootObj);
243 file.write(jsonDoc.toJson());
245 HMI_ERROR("HomeScreen", "write to registeredApp.json file failed");
248 fsync(file.handle());
252 bool ShortcutAppModel::checkAppFile()
254 bool fileError = false;
255 QFile file(SHORTCUTKEY_PATH);
256 if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
257 QByteArray line = file.readLine();
258 if(line == "\n" || line.isEmpty()) {
263 HMI_ERROR("HomeScreen", "registeredApp.json file open failed");