registerShortcut
[apps/launcher.git] / launcher / 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 <QJsonDocument>
20 #include <QJsonObject>
21 #include <QJsonParseError>
22
23 #define SHORTCUTKEY_PATH "/var/local/lib/afm/applications/homescreen/0.1/etc/registeredApp.json"
24
25 class ShortcutAppModel::Private
26 {
27 public:
28     Private();
29
30     QList<RegisterApp> data;
31 };
32
33 ShortcutAppModel::Private::Private()
34 {
35 }
36
37
38 ShortcutAppModel::ShortcutAppModel(QObject *parent)
39     : QAbstractListModel(parent)
40     , d(new Private())
41 {
42     init();
43 //    getAppQueue();
44 }
45
46 ShortcutAppModel::~ShortcutAppModel()
47 {
48     delete this->d;
49 }
50 void ShortcutAppModel::init()
51 {
52     RegisterApp temp;
53     for(int i = 0; i < 3; i++) {
54         temp.id = "video@0.1";
55         temp.name = "video";
56         temp.icon = getIconPath(temp.id);
57         if (temp.icon == "") {
58             temp.isBlank = true;
59         } else {
60             temp.isBlank = false;
61         }
62         d->data.append(temp);
63     }
64 }
65
66 int ShortcutAppModel::rowCount(const QModelIndex &parent) const
67 {
68     if (parent.isValid())
69         return 0;
70
71     return this->d->data.count();
72 }
73
74 QVariant ShortcutAppModel::data(const QModelIndex &index, int role) const
75 {
76     QVariant ret;
77     if (!index.isValid())
78         return ret;
79
80     switch (role) {
81     case Qt::DecorationRole:
82         ret = this->d->data[index.row()].icon;
83         break;
84     case Qt::DisplayRole:
85         ret = this->d->data[index.row()].name;
86         break;
87     case Qt::UserRole:
88         ret = this->d->data[index.row()].id;
89         break;
90     default:
91         break;
92     }
93
94     return ret;
95 }
96
97 QHash<int, QByteArray> ShortcutAppModel::roleNames() const
98 {
99     QHash<int, QByteArray> roles;
100     roles[Qt::DecorationRole] = "icon";
101     roles[Qt::DisplayRole] = "name";
102     roles[Qt::UserRole] = "id";
103     return roles;
104 }
105
106 void ShortcutAppModel::shortcutUpdate(QStringList shortcut_list)
107 {
108     HMI_DEBUG("Launcher", "ShortcutAppModel::shortcutUpdate id1=%s", shortcut_list.at(1).toStdString().c_str());
109     RegisterApp temp;
110     for(int i = 0; i < d->data.size(); i++) {
111         temp.id = shortcut_list.at(2 * i);
112         temp.name = shortcut_list.at(2 * i + 1);
113         temp.icon = getIconPath(temp.id);
114         if (temp.icon == "") {
115             temp.isBlank = true;
116         } else {
117             temp.isBlank = false;
118         }
119         d->data.replace(i, temp);
120     }
121     emit updateShortcut();
122 }
123
124 QString ShortcutAppModel::getId(int index) const
125 {
126     return d->data.at(index).id;
127 }
128
129 QString ShortcutAppModel::getName(int index) const
130 {
131     return d->data.at(index).name;
132 }
133
134 QString ShortcutAppModel::getIcon(int index) const
135 {
136     return d->data.at(index).icon;
137 }
138
139 bool ShortcutAppModel::isBlank(int index) const
140 {
141     return d->data.at(index).isBlank;
142 }
143
144 QString ShortcutAppModel::getIconPath(QString id)
145 {
146     QString name = id.section('@', 0, 0);
147     QString version = id.section('@', 1, 1);
148     QString boardIconPath = "/var/local/lib/afm/applications/" + name + "/" + version + "/icon.svg";
149     QString appIconPath = ":/images/TopShortcut/" + name + ".svg";
150     if (QFile::exists(boardIconPath)) {
151         return "file://" + boardIconPath;
152     } else if (QFile::exists(appIconPath)) {
153         return appIconPath.section('/', 1, -1);
154     }
155     return "";
156 }
157
158 void ShortcutAppModel::setAppQueuePoint(QString id, QString name)
159 {
160     app.id = id;
161     app.icon = getIconPath(app.id);
162     if (app.icon == "") {
163         app.isBlank = true;
164     } else {
165         app.isBlank = false;
166     }
167     app.name = name;
168     d->data.append(app);
169 }