warehouse for ces2019
[apps/onscreenapp.git] / app / src / nativeappmodel.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 "nativeappmodel.h"
20 #include <QtDBus/QDBusInterface>
21 #include <QtDBus/QDBusReply>
22 #include "afm_user_daemon_proxy.h"
23 #include "httpclient.h"
24
25 #include "hmi-debug.h"
26
27 extern org::AGL::afm::user* afm_user_daemon_proxy;
28
29 class NativeAppModel::Private {
30  public:
31   Private();
32
33   void getApps();
34
35   QList<AppInfo> data;
36 };
37
38 NativeAppModel::Private::Private() {
39   // this->getApps();
40 }
41
42 void NativeAppModel::Private::getApps() {
43   QString apps = afm_user_daemon_proxy->runnables(QStringLiteral(""));
44   QJsonDocument japps = QJsonDocument::fromJson(apps.toUtf8());
45   for (auto const& app : japps.array()) {
46     QJsonObject const& jso = app.toObject();
47
48     AppInfo appinfo;
49     appinfo.read(jso);
50
51     this->data.append(appinfo);
52   }
53 }
54
55 NativeAppModel::NativeAppModel(QObject* parent)
56     : QAbstractListModel(parent), d(new Private()) {
57   connect(afm_user_daemon_proxy, &org::AGL::afm::user::changed, this,
58           &NativeAppModel::appChanged);
59 }
60
61 NativeAppModel::~NativeAppModel() {
62   delete this->d;
63 }
64
65 int NativeAppModel::rowCount(const QModelIndex& parent) const {
66   if (parent.isValid())
67     return 0;
68
69   return this->d->data.count();
70 }
71
72 QVariant NativeAppModel::data(const QModelIndex& index, int role) const {
73   QVariant ret;
74   if (!index.isValid())
75     return ret;
76
77   switch (role) {
78     case IconRole:
79       ret = this->d->data[index.row()].iconPath();
80       break;
81     case NameRole:
82       ret = this->d->data[index.row()].name();
83       break;
84     case IdRole:
85       ret = this->d->data[index.row()].id();
86       break;
87     case VersionRole:
88       ret = this->d->data[index.row()].version();
89       break;
90     case DescriptionRole:
91       ret = this->d->data[index.row()].description();
92       break;
93     case ShortNameRole:
94       ret = this->d->data[index.row()].shortname();
95       break;
96     case AuthorRole:
97       ret = this->d->data[index.row()].author();
98       break;
99     default:
100       break;
101   }
102
103   return ret;
104 }
105
106 QHash<int, QByteArray> NativeAppModel::roleNames() const {
107   QHash<int, QByteArray> roles;
108   roles[IconRole] = "icon";
109   roles[NameRole] = "name";
110   roles[IdRole] = "id";
111   roles[VersionRole] = "version";
112   roles[DescriptionRole] = "description";
113   roles[ShortNameRole] = "shortname";
114   roles[AuthorRole] = "author";
115   return roles;
116 }
117
118 QString NativeAppModel::id(int i) const {
119   return data(index(i), IdRole).toString();
120 }
121
122 QString NativeAppModel::name(int i) const {
123   return data(index(i), NameRole).toString();
124 }
125
126 void NativeAppModel::appChanged(const QString& info) {
127   this->refresh();
128 }
129
130 int NativeAppModel::launch(const QString& application) {
131   int result = -1;
132   HMI_DEBUG("launch", "ApplicationLauncher launch %s.",
133             application.toStdString().c_str());
134
135   result = afm_user_daemon_proxy->start(application).value().toInt();
136   HMI_DEBUG("launch", "ApplicationLauncher pid: %d.", result);
137
138   return result;
139 }
140
141 void NativeAppModel::uninstall(int index) {
142   const QString& id = this->d->data[index].id();
143   QString result = afm_user_daemon_proxy->uninstall(id);
144   if (result == "null") {
145     beginRemoveRows(QModelIndex(), index, index);
146     this->d->data.removeAt(index);
147     endRemoveRows();
148   }
149 }
150
151 void NativeAppModel::refresh() {
152   beginResetModel();
153   this->d->data.clear();
154   this->d->getApps();
155   endResetModel();
156   emit applistChanged(this->d->data);
157 }