PoC: Qt Compositor-ized homescreen
[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     // disable apps which don't work with the compositor right now
58     QStringList notShow = {
59         "navigation@0.1"
60         , "phone@0.1"
61         , "controls@0.1"
62         , "poi@0.1"
63         , "mixer@0.1"
64     };
65     for (auto const &app : japps.array()) {
66         QJsonObject const &jso = app.toObject();
67         auto const name = jso["name"].toString();
68         auto const id = jso["id"].toString();
69         if (notShow.contains(id)) continue;
70         auto const icon = get_icon_name(jso);
71         this->data.append(AppInfo(icon, name, id));
72         qDebug() << "name:" << name << "icon:" << icon << "id:" << id;
73     }
74 }
75
76 ApplicationModel::ApplicationModel(QObject *parent)
77     : QAbstractListModel(parent)
78     , d(new Private())
79 {
80 }
81
82 ApplicationModel::~ApplicationModel()
83 {
84     delete this->d;
85 }
86
87 int ApplicationModel::rowCount(const QModelIndex &parent) const
88 {
89     if (parent.isValid())
90         return 0;
91
92     return this->d->data.count();
93 }
94
95 QVariant ApplicationModel::data(const QModelIndex &index, int role) const
96 {
97     QVariant ret;
98     if (!index.isValid())
99         return ret;
100
101     switch (role) {
102     case Qt::DecorationRole:
103         ret = this->d->data[index.row()].iconPath();
104         break;
105     case Qt::DisplayRole:
106         ret = this->d->data[index.row()].name();
107         break;
108     case Qt::UserRole:
109         ret = this->d->data[index.row()].id();
110         break;
111     default:
112         break;
113     }
114
115     return ret;
116 }
117
118 QHash<int, QByteArray> ApplicationModel::roleNames() const
119 {
120     QHash<int, QByteArray> roles;
121     roles[Qt::DecorationRole] = "icon";
122     roles[Qt::DisplayRole] = "name";
123     roles[Qt::UserRole] = "id";
124     return roles;
125 }
126
127 QString ApplicationModel::id(int i) const
128 {
129     return data(index(i), Qt::UserRole).toString();
130 }
131
132 void ApplicationModel::move(int from, int to)
133 {
134     QModelIndex parent;
135     if (to < 0 || to > rowCount()) return;
136     if (from < to) {
137         if (!beginMoveRows(parent, from, from, parent, to + 1)) {
138             qDebug() << from << to << false;
139             return;
140         }
141         d->data.move(from, to);
142         endMoveRows();
143     } else if (from > to) {
144         if (!beginMoveRows(parent, from, from, parent, to)) {
145             qDebug() << from << to << false;
146             return;
147         }
148         d->data.move(from, to);
149         endMoveRows();
150     } else {
151         qDebug() << from << to << false;
152     }
153 }