c5f0fc005fde4c4d174b3eed6d278dc6ca0c721b
[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 class ApplicationModel::Private
27 {
28 public:
29     Private(ApplicationModel *parent);
30
31 private:
32     ApplicationModel *q;
33 public:
34     QDBusInterface proxy;
35     QList<AppInfo> data;
36 };
37
38 namespace {
39     // This is a disgrace, shouldn't we be having a defined way to know which icon (if it is not given by the getAvailableApps() reply)?
40     QString get_icon_name(AppInfo const &i)
41     {
42         QString icon = i.iconPath().isEmpty() ? i.id().split("@").front() : i.iconPath();
43         if (icon == "hvac" || icon == "poi") {
44             icon = icon.toUpper();
45         } else if (icon == "mediaplayer") {
46             icon = "Multimedia";
47         } else {
48             icon[0] = icon[0].toUpper();
49         }
50         return icon;
51     }
52 }
53
54 ApplicationModel::Private::Private(ApplicationModel *parent)
55     : q(parent)
56     , proxy(QStringLiteral("org.agl.homescreenappframeworkbinder"), QStringLiteral("/AppFramework"), QStringLiteral("org.agl.appframework"), QDBusConnection::sessionBus())
57 {
58     QDBusReply<QList<AppInfo>> reply = proxy.call("getAvailableApps");
59     if (reply.isValid()) {
60         // FIXME: Is the order from dbus the one we want to use?!
61         for (auto const &i: reply.value()) {
62             auto const name = i.name().split(" ").front().toUpper();
63             auto const icon = get_icon_name(i);
64             data.append(AppInfo(icon, name, i.id()));
65         }
66     } else {
67         qDebug() << "getAvailableApps() reply is INVALID!";
68     }
69 }
70
71 ApplicationModel::ApplicationModel(QObject *parent)
72     : QAbstractListModel(parent)
73     , d(new Private(this))
74 {
75 }
76
77 ApplicationModel::~ApplicationModel()
78 {
79     delete d;
80 }
81
82 int ApplicationModel::rowCount(const QModelIndex &parent) const
83 {
84     if (parent.isValid())
85         return 0;
86
87     return d->data.count();
88 }
89
90 QVariant ApplicationModel::data(const QModelIndex &index, int role) const
91 {
92     QVariant ret;
93     if (!index.isValid())
94         return ret;
95
96     switch (role) {
97     case Qt::DecorationRole:
98         ret = d->data[index.row()].iconPath();
99         break;
100     case Qt::DisplayRole:
101         ret = d->data[index.row()].name();
102         break;
103     case Qt::UserRole:
104         ret = d->data[index.row()].id();
105         break;
106     default:
107         break;
108     }
109
110     return ret;
111 }
112
113 QHash<int, QByteArray> ApplicationModel::roleNames() const
114 {
115     QHash<int, QByteArray> roles;
116     roles[Qt::DecorationRole] = "icon";
117     roles[Qt::DisplayRole] = "name";
118     roles[Qt::UserRole] = "id";
119     return roles;
120 }