Initial source commit
[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 <QtDBus/QDBusInterface>
22 #include <QtDBus/QDBusReply>
23
24 class ApplicationModel::Private
25 {
26 public:
27     Private(ApplicationModel *parent);
28
29 private:
30     ApplicationModel *q;
31 public:
32     QDBusInterface proxy;
33     QList<AppInfo> data;
34 };
35
36 ApplicationModel::Private::Private(ApplicationModel *parent)
37     : q(parent)
38     , proxy(QStringLiteral("org.agl.homescreenappframeworkbinder"), QStringLiteral("/AppFramework"), QStringLiteral("org.agl.appframework"), QDBusConnection::sessionBus())
39 {
40     QDBusReply<QList<AppInfo>> reply = proxy.call("getAvailableApps");
41     if (false)/*reply.isValid()) TODO: test for CES!  */ {
42         data = reply.value();
43     } else {
44         data.append(AppInfo(QStringLiteral("HVAC"), QStringLiteral("HVAC"), QStringLiteral("hvac@0.1")));
45         data.append(AppInfo(QStringLiteral("Navigation"), QStringLiteral("NAVIGATION"), QStringLiteral("navigation@0.1")));
46         data.append(AppInfo(QStringLiteral("Phone"), QStringLiteral("PHONE"), QStringLiteral("phone@0.1")));
47         data.append(AppInfo(QStringLiteral("Radio"), QStringLiteral("RADIO"), QStringLiteral("radio@0.1")));
48         data.append(AppInfo(QStringLiteral("Multimedia"), QStringLiteral("MULTIMEDIA"), QStringLiteral("mediaplayer@0.1")));
49         data.append(AppInfo(QStringLiteral("Mixer"), QStringLiteral("MIXER"), QStringLiteral("mixer@0.1")));
50         data.append(AppInfo(QStringLiteral("Dashboard"), QStringLiteral("DASHBOARD"), QStringLiteral("dashboard@0.1")));
51         data.append(AppInfo(QStringLiteral("Settings"), QStringLiteral("SETTINGS"), QStringLiteral("settings@0.1")));
52         data.append(AppInfo(QStringLiteral("POI"), QStringLiteral("POINT OF\nINTEREST"), QStringLiteral("poi@0.1")));
53     }
54 }
55
56 ApplicationModel::ApplicationModel(QObject *parent)
57     : QAbstractListModel(parent)
58     , d(new Private(this))
59 {
60 }
61
62 ApplicationModel::~ApplicationModel()
63 {
64     delete d;
65 }
66
67 int ApplicationModel::rowCount(const QModelIndex &parent) const
68 {
69     if (parent.isValid())
70         return 0;
71
72     return d->data.count();
73 }
74
75 QVariant ApplicationModel::data(const QModelIndex &index, int role) const
76 {
77     QVariant ret;
78     if (!index.isValid())
79         return ret;
80
81     switch (role) {
82     case Qt::DecorationRole:
83         ret = d->data[index.row()].iconPath();
84         break;
85     case Qt::DisplayRole:
86         ret = d->data[index.row()].name();
87         break;
88     case Qt::UserRole:
89         ret = d->data[index.row()].id();
90         break;
91     default:
92         break;
93     }
94
95     return ret;
96 }
97
98 QHash<int, QByteArray> ApplicationModel::roleNames() const
99 {
100     QHash<int, QByteArray> roles;
101     roles[Qt::DecorationRole] = "icon";
102     roles[Qt::DisplayRole] = "name";
103     roles[Qt::UserRole] = "id";
104     return roles;
105 }