76078dace89208b22f666793da42c2337bc2a9f5
[apps/homescreen.git] / homescreen / src / shortcutappmodel.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "shortcutappmodel.h"
18 #include "hmi-debug.h"
19 #include <unistd.h>
20 #define SHORTCUTKEY_PATH "/var/local/lib/afm/applications/homescreen/0.1/etc/registeredApp.json"
21
22 class ShortcutAppModel::Private
23 {
24 public:
25     Private();
26
27     QList<RegisterApp> data;
28 };
29
30 ShortcutAppModel::Private::Private()
31 {
32 }
33
34
35 ShortcutAppModel::ShortcutAppModel(QObject *parent)
36     : QAbstractListModel(parent)
37     , d(new Private())
38 {
39     getAppQueue();
40 }
41
42 ShortcutAppModel::~ShortcutAppModel()
43 {
44     delete this->d;
45 }
46
47 int ShortcutAppModel::rowCount(const QModelIndex &parent) const
48 {
49     if (parent.isValid())
50         return 0;
51
52     return this->d->data.count();
53 }
54
55 QVariant ShortcutAppModel::data(const QModelIndex &index, int role) const
56 {
57     QVariant ret;
58     if (!index.isValid())
59         return ret;
60
61     switch (role) {
62     case Qt::DecorationRole:
63         ret = this->d->data[index.row()].icon;
64         break;
65     case Qt::DisplayRole:
66         ret = this->d->data[index.row()].name;
67         break;
68     case Qt::UserRole:
69         ret = this->d->data[index.row()].id;
70         break;
71     default:
72         break;
73     }
74
75     return ret;
76 }
77
78 QHash<int, QByteArray> ShortcutAppModel::roleNames() const
79 {
80     QHash<int, QByteArray> roles;
81     roles[Qt::DecorationRole] = "icon";
82     roles[Qt::DisplayRole] = "name";
83     roles[Qt::UserRole] = "id";
84     return roles;
85 }
86
87 void ShortcutAppModel::changeShortcut(QString id, QString name, QString position)
88 {
89     for(int i = 1; i < d->data.size(); i++) {
90         if(id == d->data.at(i).id) {
91             return;
92         }
93     }
94     d->data.removeAt(position.toInt() + 1);
95
96     RegisterApp temp;
97     temp.id = id;
98     temp.name = name;
99     temp.icon = temp.icon = getIconPath(temp.id);
100     if (temp.icon == "") {
101         temp.isBlank = true;
102     } else {
103         temp.isBlank = false;
104     }
105
106     d->data.insert(position.toInt() + 1, temp);
107     setAppQueue();
108     emit updateShortcut();
109     struct json_object* obj = makeAppListJson();
110     emit shortcutUpdated(QString("launcher"), obj);
111 }
112
113 struct json_object* ShortcutAppModel::makeAppListJson()
114 {
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++)
118     {
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);
123     }
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()));
126     return obj;
127 }
128
129 QString ShortcutAppModel::getId(int index) const
130 {
131     return d->data.at(index).id;
132 }
133
134 QString ShortcutAppModel::getName(int index) const
135 {
136     return d->data.at(index).name;
137 }
138
139 QString ShortcutAppModel::getIcon(int index) const
140 {
141     return d->data.at(index).icon;
142 }
143
144 bool ShortcutAppModel::isBlank(int index) const
145 {
146     return d->data.at(index).isBlank;
147 }
148
149 QString ShortcutAppModel::getIconPath(QString id)
150 {
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);
159     }
160     return "";
161 }
162
163 void ShortcutAppModel::getAppQueue()
164 {
165     QProcess *process = new QProcess(this);
166     if(checkAppFile()) {
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");
168     } else {
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");
170     }
171     QThread::msleep(300);
172
173     QFile file(SHORTCUTKEY_PATH);
174     if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
175         QByteArray allData = file.readAll();
176         QString str(allData);
177         if(str == "") {
178             file.close();
179
180         }
181         QJsonParseError json_error;
182         QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error));
183
184         if(json_error.error != QJsonParseError::NoError)
185         {
186              HMI_ERROR("HomeScreen", "registeredApp.json error");
187              return;
188         }
189
190         QJsonObject rootObj = jsonDoc.object();
191
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());
200     }
201     file.close();
202 }
203
204 void ShortcutAppModel::setAppQueuePoint(QString id, QString name)
205 {
206     app.id = id;
207     app.icon = getIconPath(app.id);
208     if (app.icon == "") {
209         app.isBlank = true;
210     } else {
211         app.isBlank = false;
212     }
213     app.name = name;
214     d->data.append(app);
215 }
216
217 void ShortcutAppModel::screenUpdated()
218 {
219     struct json_object* obj = makeAppListJson();
220     emit shortcutUpdated(QString("launcher"), obj);
221 }
222
223 void ShortcutAppModel::setAppQueue()
224 {
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);
240
241         QJsonDocument jsonDoc;
242         jsonDoc.setObject(rootObj);
243
244         file.write(jsonDoc.toJson());
245     } else {
246         HMI_ERROR("HomeScreen", "write to registeredApp.json file failed");
247     }
248     file.flush();
249     fsync(file.handle());
250     file.close();
251 }
252
253 bool ShortcutAppModel::checkAppFile()
254 {
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()) {
260             fileError = true;
261         }
262     } else {
263         fileError = true;
264         HMI_ERROR("HomeScreen", "registeredApp.json file open failed");
265     }
266     file.close();
267     return fileError;
268 }