aboutsummaryrefslogtreecommitdiffstats
path: root/homescreen/src/shortcutappmodel.cpp
blob: 7776bd10ef1abb0113dbdacb69c7faa3cb19c5a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "shortcutappmodel.h"
#include "hmi-debug.h"
#include <unistd.h>
#define SHORTCUTKEY_PATH "/var/local/lib/afm/applications/homescreen/0.1/etc/registeredApp.json"

class ShortcutAppModel::Private
{
public:
    Private();

    QList<RegisterApp> data;
};

ShortcutAppModel::Private::Private()
{
}


ShortcutAppModel::ShortcutAppModel(QObject *parent)
    : QAbstractListModel(parent)
    , d(new Private())
{
    getAppQueue();
}

ShortcutAppModel::~ShortcutAppModel()
{
    delete this->d;
}

int ShortcutAppModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;

    return this->d->data.count();
}

QVariant ShortcutAppModel::data(const QModelIndex &index, int role) const
{
    QVariant ret;
    if (!index.isValid())
        return ret;

    switch (role) {
    case Qt::DecorationRole:
        ret = this->d->data[index.row()].icon;
        break;
    case Qt::DisplayRole:
        ret = this->d->data[index.row()].name;
        break;
    case Qt::UserRole:
        ret = this->d->data[index.row()].id;
        break;
    default:
        break;
    }

    return ret;
}

QHash<int, QByteArray> ShortcutAppModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[Qt::DecorationRole] = "icon";
    roles[Qt::DisplayRole] = "name";
    roles[Qt::UserRole] = "id";
    return roles;
}

void ShortcutAppModel::changeShortcut(QString id, QString name, QString position)
{
    for(int i = 1; i < d->data.size(); i++) {
        if(id == d->data.at(i).id) {
            return;
        }
    }
    d->data.removeAt(position.toInt() + 1);

    RegisterApp temp;
    temp.id = id;
    temp.name = name;
    temp.icon = temp.icon = getIconPath(temp.id);
    if (temp.icon == "") {
        temp.isBlank = true;
    } else {
        temp.isBlank = false;
    }

    d->data.insert(position.toInt() + 1, temp);
    setAppQueue();
    emit updateShortcut();
    struct json_object* obj = makeAppListJson();
    emit shortcutUpdated(QString("launcher"), obj);
}

struct json_object* ShortcutAppModel::makeAppListJson()
{
    struct json_object* obj = json_object_new_object();
    struct json_object* obj_array = json_object_new_array();
    for(int i = 1; i < d->data.size(); i++)
    {
        struct json_object* obj_shortcut = json_object_new_object();
        json_object_object_add(obj_shortcut, "shortcut_id", json_object_new_string(d->data.at(i).id.toStdString().c_str()));
        json_object_object_add(obj_shortcut, "shortcut_name", json_object_new_string(d->data.at(i).name.toStdString().c_str()));
        json_object_array_add(obj_array, obj_shortcut);
    }
    json_object_object_add(obj, "shortcut", obj_array);
    return obj;
}

QString ShortcutAppModel::getId(int index) const
{
    return d->data.at(index).id;
}

QString ShortcutAppModel::getName(int index) const
{
    return d->data.at(index).name;
}

QString ShortcutAppModel::getIcon(int index) const
{
    return d->data.at(index).icon;
}

bool ShortcutAppModel::isBlank(int index) const
{
    return d->data.at(index).isBlank;
}

QString ShortcutAppModel::getIconPath(QString id)
{
    QString name = id.section('@', 0, 0);
    QString version = id.section('@', 1, 1);
    QString boardIconPath = "/var/local/lib/afm/applications/" + name + "/" + version + "/icon.svg";
    QString appIconPath = ":/images/Shortcut/" + name + ".svg";
    if (QFile::exists(boardIconPath)) {
        return "file://" + boardIconPath;
    } else if (QFile::exists(appIconPath)) {
        return appIconPath.section('/', 1, -1);
    }
    return "";
}

void ShortcutAppModel::getAppQueue()
{
    QProcess *process = new QProcess(this);
    if(checkAppFile()) {
        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");
    } else {
        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");
    }
    QThread::msleep(300);

    QFile file(SHORTCUTKEY_PATH);
    if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QByteArray allData = file.readAll();
        QString str(allData);
        if(str == "") {
            file.close();

        }
        QJsonParseError json_error;
        QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error));

        if(json_error.error != QJsonParseError::NoError)
        {
             HMI_ERROR("HomeScreen", "registeredApp.json error");
             return;
        }

        QJsonObject rootObj = jsonDoc.object();

        QJsonObject subObj = rootObj.value("1st shortcut key").toObject();
        setAppQueuePoint(subObj["id"].toString(), subObj["name"].toString());
        subObj = rootObj.value("2nd shortcut key").toObject();
        setAppQueuePoint(subObj["id"].toString(), subObj["name"].toString());
        subObj = rootObj.value("3rd shortcut key").toObject();
        setAppQueuePoint(subObj["id"].toString(), subObj["name"].toString());
        subObj = rootObj.value("4th shortcut key").toObject();
        setAppQueuePoint(subObj["id"].toString(), subObj["name"].toString());
    }
    file.close();
}

void ShortcutAppModel::setAppQueuePoint(QString id, QString name)
{
    app.id = id;
    app.icon = getIconPath(app.id);
    if (app.icon == "") {
        app.isBlank = true;
    } else {
        app.isBlank = false;
    }
    app.name = name;
    d->data.append(app);
}

void ShortcutAppModel::screenUpdated()
{
    struct json_object* obj = makeAppListJson();
    emit shortcutUpdated(QString("launcher"), obj);
}

void ShortcutAppModel::setAppQueue()
{
    QFile file(SHORTCUTKEY_PATH);
    if(file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        QJsonObject rootObj, subObj1, subObj2, subObj3, subObj4;
        subObj1.insert("id", d->data.at(0).id);
        subObj1.insert("name", d->data.at(0).name);
        subObj2.insert("id", d->data.at(1).id);
        subObj2.insert("name", d->data.at(1).name);
        subObj3.insert("id", d->data.at(2).id);
        subObj3.insert("name", d->data.at(2).name);
        subObj4.insert("id", d->data.at(3).id);
        subObj4.insert("name", d->data.at(3).name);
        rootObj.insert("1st shortcut key", subObj1);
        rootObj.insert("2nd shortcut key", subObj2);
        rootObj.insert("3rd shortcut key", subObj3);
        rootObj.insert("4th shortcut key", subObj4);

        QJsonDocument jsonDoc;
        jsonDoc.setObject(rootObj);

        file.write(jsonDoc.toJson());
    } else {
        HMI_ERROR("HomeScreen", "write to registeredApp.json file failed");
    }
    file.flush();
    fsync(file.handle());
    file.close();
}

bool ShortcutAppModel::checkAppFile()
{
    bool fileError = false;
    QFile file(SHORTCUTKEY_PATH);
    if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QByteArray line = file.readLine();
        if(line == "\n" || line.isEmpty()) {
            fileError = true;
        }
    } else {
        fileError = true;
        HMI_ERROR("HomeScreen", "registeredApp.json file open failed");
    }
    file.close();
    return fileError;
}