417bc4ce6eb945538d8b6d31620cf8821587a3a7
[apps/homescreen.git] / homescreen / src / applicationmodel.cpp
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  * Copyright (C) 2016, 2017 Mentor Graphics Development (Deutschland) GmbH
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "applicationmodel.h"
19 #include "appinfo.h"
20
21 #include <QtCore/QDebug>
22
23 #include <QtDBus/QDBusInterface>
24 #include <QtDBus/QDBusReply>
25
26 #include "afm_user_daemon_proxy.h"
27
28 extern org::AGL::afm::user *afm_user_daemon_proxy;
29
30 class ApplicationModel::Private
31 {
32 public:
33     Private();
34
35     QList<AppInfo> data;
36 };
37
38 namespace {
39     QString get_icon_name(QJsonObject const &i)
40     {
41         QString icon = i["id"].toString().split("@").front();
42         if (icon == "hvac" || icon == "poi") {
43             icon = icon.toUpper();
44         } else if (icon == "mediaplayer") {
45             icon = "Multimedia";
46         } else {
47             icon[0] = icon[0].toUpper();
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         this->data.append(AppInfo(icon, name, id));
63         qDebug() << "name:" << name << "icon:" << icon << "id:" << id;
64     }
65 }
66
67 ApplicationModel::ApplicationModel(QObject *parent)
68     : QAbstractListModel(parent)
69     , d(new Private())
70 {
71 }
72
73 ApplicationModel::~ApplicationModel()
74 {
75     delete this->d;
76 }
77
78 int ApplicationModel::rowCount(const QModelIndex &parent) const
79 {
80     if (parent.isValid())
81         return 0;
82
83     return this->d->data.count();
84 }
85
86 QVariant ApplicationModel::data(const QModelIndex &index, int role) const
87 {
88     QVariant ret;
89     if (!index.isValid())
90         return ret;
91
92     switch (role) {
93     case Qt::DecorationRole:
94         ret = this->d->data[index.row()].iconPath();
95         break;
96     case Qt::DisplayRole:
97         ret = this->d->data[index.row()].name();
98         break;
99     case Qt::UserRole:
100         ret = this->d->data[index.row()].id();
101         break;
102     default:
103         break;
104     }
105
106     return ret;
107 }
108
109 QHash<int, QByteArray> ApplicationModel::roleNames() const
110 {
111     QHash<int, QByteArray> roles;
112     roles[Qt::DecorationRole] = "icon";
113     roles[Qt::DisplayRole] = "name";
114     roles[Qt::UserRole] = "id";
115     return roles;
116 }
117
118 QString ApplicationModel::id(int i) const
119 {
120     return data(index(i), Qt::UserRole).toString();
121 }
122
123 void ApplicationModel::move(int from, int to)
124 {
125     QModelIndex parent;
126     if (to < 0 || to > rowCount()) return;
127     if (from < to) {
128         if (!beginMoveRows(parent, from, from, parent, to + 1)) {
129             qDebug() << from << to << false;
130             return;
131         }
132         d->data.move(from, to);
133         endMoveRows();
134     } else if (from > to) {
135         if (!beginMoveRows(parent, from, from, parent, to)) {
136             qDebug() << from << to << false;
137             return;
138         }
139         d->data.move(from, to);
140         endMoveRows();
141     } else {
142         qDebug() << from << to << false;
143     }
144 }