use appid instead of appname in "tap_shortcut"
[apps/launcher.git] / launcher / src / applicationmodel.cpp
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  * Copyright (C) 2016, 2017 Mentor Graphics Development (Deutschland) GmbH
4  * Copyright (c) 2018 TOYOTA MOTOR CORPORATION
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include "applicationmodel.h"
20 #include "appinfo.h"
21
22 #include "hmi-debug.h"
23
24 #include <QtDBus/QDBusInterface>
25 #include <QtDBus/QDBusReply>
26
27 #include "afm_user_daemon_proxy.h"
28
29 extern org::AGL::afm::user *afm_user_daemon_proxy;
30
31 class ApplicationModel::Private
32 {
33 public:
34     Private();
35
36     QList<AppInfo> data;
37 };
38
39 namespace {
40     QString get_icon_name(QJsonObject const &i)
41     {
42         QString icon = i["name"].toString().toLower();
43
44         if ( !QFile::exists(QString(":/images/%1_active.svg").arg(icon)) ||
45              !QFile::exists(QString(":/images/%1_inactive.svg").arg(icon)) )
46         {
47             icon = "blank";
48         }
49         return icon;
50     }
51 }
52
53 ApplicationModel::Private::Private()
54 {
55     QString apps = afm_user_daemon_proxy->runnables(QStringLiteral(""));
56     QJsonDocument japps = QJsonDocument::fromJson(apps.toUtf8());
57     for (auto const &app : japps.array()) {
58         QJsonObject const &jso = app.toObject();
59         auto const name = jso["name"].toString();
60         auto const id = jso["id"].toString();
61         auto const icon = get_icon_name(jso);
62
63         if ( name != "launcher" &&
64              name != "homescreen-2017" &&
65              name != "homescreen" &&
66              name != "OnScreenApp") {
67             this->data.append(AppInfo(icon, name, id));
68         }
69
70         HMI_DEBUG("launcher","name: %s icon: %s id: %s.", name.toStdString().c_str(), icon.toStdString().c_str(), id.toStdString().c_str());
71     }
72 }
73
74 ApplicationModel::ApplicationModel(QObject *parent)
75     : QAbstractListModel(parent)
76     , d(new Private())
77 {
78 }
79
80 ApplicationModel::~ApplicationModel()
81 {
82     delete this->d;
83 }
84
85 int ApplicationModel::rowCount(const QModelIndex &parent) const
86 {
87     if (parent.isValid())
88         return 0;
89
90     return this->d->data.count();
91 }
92
93 QVariant ApplicationModel::data(const QModelIndex &index, int role) const
94 {
95     QVariant ret;
96     if (!index.isValid())
97         return ret;
98
99     switch (role) {
100     case Qt::DecorationRole:
101         ret = this->d->data[index.row()].iconPath();
102         break;
103     case Qt::DisplayRole:
104         ret = this->d->data[index.row()].name();
105         break;
106     case Qt::UserRole:
107         ret = this->d->data[index.row()].id();
108         break;
109     default:
110         break;
111     }
112
113     return ret;
114 }
115
116 QHash<int, QByteArray> ApplicationModel::roleNames() const
117 {
118     QHash<int, QByteArray> roles;
119     roles[Qt::DecorationRole] = "icon";
120     roles[Qt::DisplayRole] = "name";
121     roles[Qt::UserRole] = "id";
122     return roles;
123 }
124
125 QString ApplicationModel::id(int i) const
126 {
127     return data(index(i), Qt::UserRole).toString();
128 }
129
130 QString ApplicationModel::appid(int i) const
131 {
132     QString id = data(index(i), Qt::UserRole).toString();
133     return id.split("@")[0];
134 }
135
136 QString ApplicationModel::name(int i) const
137 {
138     return data(index(i), Qt::DisplayRole).toString();
139 }
140
141 void ApplicationModel::move(int from, int to)
142 {
143     QModelIndex parent;
144     if (to < 0 || to > rowCount()) return;
145     if (from < to) {
146         if (!beginMoveRows(parent, from, from, parent, to + 1)) {
147             HMI_NOTICE("launcher","from : %d, to : %d. false.", from, to);
148             return;
149         }
150         d->data.move(from, to);
151         endMoveRows();
152     } else if (from > to) {
153         if (!beginMoveRows(parent, from, from, parent, to)) {
154             HMI_NOTICE("launcher","from : %d, to : %d. false.", from, to);
155             return;
156         }
157         d->data.move(from, to);
158         endMoveRows();
159     } else {
160         HMI_NOTICE("launcher","from : %d, to : %d. false.", from, to);
161     }
162 }