First integration of new HMI design
[staging/HomeScreen.git] / HomeScreen / src2 / statusbarmodel.cpp
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "statusbarmodel.h"
18 #include "statusbarserver.h"
19
20 #include <QtDBus/QDBusConnection>
21
22 class StatusBarModel::Private
23 {
24 public:
25     Private(StatusBarModel *parent);
26
27 private:
28     StatusBarModel *q;
29 public:
30     StatusBarServer server;
31     QString iconList[StatusBarServer::SupportedCount];
32 };
33
34 StatusBarModel::Private::Private(StatusBarModel *parent)
35     : q(parent)
36 {
37     QDBusConnection dbus = QDBusConnection::sessionBus();
38     dbus.registerObject("/StatusBar", &server);
39     dbus.registerService("org.agl.homescreen");
40     connect(&server, &StatusBarServer::statusIconChanged, [&](int placeholderIndex, const QString &icon) {
41         if (placeholderIndex < 0 || StatusBarServer::SupportedCount <= placeholderIndex) return;
42         if (iconList[placeholderIndex] == icon) return;
43         iconList[placeholderIndex] = icon;
44         emit q->dataChanged(q->index(placeholderIndex), q->index(placeholderIndex));
45     });
46     for (int i = 0; i < StatusBarServer::SupportedCount; i++) {
47         iconList[i] = server.getStatusIcon(i);
48     }
49 }
50
51 StatusBarModel::StatusBarModel(QObject *parent)
52     : QAbstractListModel(parent)
53     , d(new Private(this))
54 {
55 }
56
57 StatusBarModel::~StatusBarModel()
58 {
59     delete d;
60 }
61
62 int StatusBarModel::rowCount(const QModelIndex &parent) const
63 {
64     if (parent.isValid())
65         return 0;
66
67     return StatusBarServer::SupportedCount;
68 }
69
70 QVariant StatusBarModel::data(const QModelIndex &index, int role) const
71 {
72     QVariant ret;
73     if (!index.isValid())
74         return ret;
75
76     switch (role) {
77     case Qt::DisplayRole:
78         ret = d->iconList[index.row()];
79         break;
80     default:
81         break;
82     }
83
84     return ret;
85 }
86
87 QHash<int, QByteArray> StatusBarModel::roleNames() const
88 {
89     QHash<int, QByteArray> roles;
90     roles[Qt::DisplayRole] = "icon";
91     return roles;
92 }