bb4417141901b49258cf8e611c25aae6137b198c
[apps/homescreen.git] / homescreen / src / statusbarmodel.cpp
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  * Copyright (C) 2017, 2018 TOYOTA MOTOR CORPORATION
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 "statusbarmodel.h"
19 #include "statusbarserver.h"
20
21 #include <QtDBus/QDBusConnection>
22
23 class StatusBarModel::Private
24 {
25 public:
26     Private(StatusBarModel *parent);
27
28 private:
29     StatusBarModel *q;
30 public:
31     StatusBarServer server;
32     QString iconList[StatusBarServer::SupportedCount];
33 };
34
35 StatusBarModel::Private::Private(StatusBarModel *parent)
36     : q(parent)
37 {
38     QDBusConnection dbus = QDBusConnection::sessionBus();
39     dbus.registerObject("/StatusBar", &server);
40     dbus.registerService("org.agl.homescreen");
41     connect(&server, &StatusBarServer::statusIconChanged, [&](int placeholderIndex, const QString &icon) {
42         if (placeholderIndex < 0 || StatusBarServer::SupportedCount <= placeholderIndex) return;
43         if (iconList[placeholderIndex] == icon) return;
44         iconList[placeholderIndex] = icon;
45         emit q->dataChanged(q->index(placeholderIndex), q->index(placeholderIndex));
46     });
47     for (int i = 0; i < StatusBarServer::SupportedCount; i++) {
48         iconList[i] = server.getStatusIcon(i);
49     }
50 }
51
52 StatusBarModel::StatusBarModel(QObject *parent)
53     : QAbstractListModel(parent)
54     , d(new Private(this))
55 {
56 }
57
58 StatusBarModel::~StatusBarModel()
59 {
60     delete d;
61 }
62
63 int StatusBarModel::rowCount(const QModelIndex &parent) const
64 {
65     if (parent.isValid())
66         return 0;
67
68     // Delete bluetooth because use agl-service-bluetooth.
69     return StatusBarServer::SupportedCount - 1;
70 }
71
72 QVariant StatusBarModel::data(const QModelIndex &index, int role) const
73 {
74     QVariant ret;
75     if (!index.isValid())
76         return ret;
77
78     switch (role) {
79     case Qt::DisplayRole:
80         if (index.row() == 0){
81             ret = d->iconList[StatusBarServer::StatusWifi];
82         }else if (index.row() == 1){
83             ret = d->iconList[StatusBarServer::StatusCellular];
84         }
85         break;
86     default:
87         break;
88     }
89
90     return ret;
91 }
92
93 QHash<int, QByteArray> StatusBarModel::roleNames() const
94 {
95     QHash<int, QByteArray> roles;
96     roles[Qt::DisplayRole] = "icon";
97     return roles;
98 }