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);
125 HMI_DEBUG("Homescreen", "makeAppListJson id1=%s",json_object_new_string(d->data.at(1).name.toStdString().c_str()));
129 QString ShortcutAppModel::getId(int index) const
131 return d->data.at(index).id;
134 QString ShortcutAppModel::getName(int index) const
136 return d->data.at(index).name;
139 QString ShortcutAppModel::getIcon(int index) const
141 return d->data.at(index).icon;
144 bool ShortcutAppModel::isBlank(int index) const
146 return d->data.at(index).isBlank;
149 QString ShortcutAppModel::getIconPath(QString id)
151 QString name = id.section('@', 0, 0);
152 QString version = id.section('@', 1, 1);
153 QString boardIconPath = "/var/local/lib/afm/applications/" + name + "/" + version + "/icon.svg";
154 QString appIconPath = ":/images/Shortcut/" + name + ".svg";
155 if (QFile::exists(boardIconPath)) {
156 return "file://" + boardIconPath;
157 } else if (QFile::exists(appIconPath)) {
158 return appIconPath.section('/', 1, -1);
163 void ShortcutAppModel::getAppQueue()
165 QProcess *process = new QProcess(this);
167 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");
169 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");
171 QThread::msleep(300);
173 QFile file(SHORTCUTKEY_PATH);
174 if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
175 QByteArray allData = file.readAll();
176 QString str(allData);
181 QJsonParseError json_error;
182 QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error));
184 if(json_error.error != QJsonParseError::NoError)
186 HMI_ERROR("HomeScreen", "registeredApp.json error");
190 QJsonObject rootObj = jsonDoc.object();
192 QJsonObject subObj = rootObj.value("1st shortcut key").toObject();
193 setAppQueuePoint(subObj["id"].toString(), subObj["name"].toString());
194 subObj = rootObj.value("2nd shortcut key").toObject();
195 setAppQueuePoint(subObj["id"].toString(), subObj["name"].toString());
196 subObj = rootObj.value("3rd shortcut key").toObject();
197 setAppQueuePoint(subObj["id"].toString(), subObj["name"].toString());
198 subObj = rootObj.value("4th shortcut key").toObject();
199 setAppQueuePoint(subObj["id"].toString(), subObj["name"].toString());
204 void ShortcutAppModel::setAppQueuePoint(QString id, QString name)
207 app.icon = getIconPath(app.id);
208 if (app.icon == "") {
217 void ShortcutAppModel::screenUpdated()
219 struct json_object* obj = makeAppListJson();
220 emit shortcutUpdated(QString("launcher"), obj);
223 void ShortcutAppModel::setAppQueue()
225 QFile file(SHORTCUTKEY_PATH);
226 if(file.open(QIODevice::WriteOnly | QIODevice::Text)) {
227 QJsonObject rootObj, subObj1, subObj2, subObj3, subObj4;
228 subObj1.insert("id", d->data.at(0).id);
229 subObj1.insert("name", d->data.at(0).name);
230 subObj2.insert("id", d->data.at(1).id);
231 subObj2.insert("name", d->data.at(1).name);
232 subObj3.insert("id", d->data.at(2).id);
233 subObj3.insert("name", d->data.at(2).name);
234 subObj4.insert("id", d->data.at(3).id);
235 subObj4.insert("name", d->data.at(3).name);
236 rootObj.insert("1st shortcut key", subObj1);
237 rootObj.insert("2nd shortcut key", subObj2);
238 rootObj.insert("3rd shortcut key", subObj3);
239 rootObj.insert("4th shortcut key", subObj4);
241 QJsonDocument jsonDoc;
242 jsonDoc.setObject(rootObj);
244 file.write(jsonDoc.toJson());
246 HMI_ERROR("HomeScreen", "write to registeredApp.json file failed");
249 fsync(file.handle());
253 bool ShortcutAppModel::checkAppFile()
255 bool fileError = false;
256 QFile file(SHORTCUTKEY_PATH);
257 if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
258 QByteArray line = file.readLine();
259 if(line == "\n" || line.isEmpty()) {
264 HMI_ERROR("HomeScreen", "registeredApp.json file open failed");