2 * Copyright (C) 2016 The Qt Company Ltd.
3 * Copyright (C) 2017, 2018 TOYOTA MOTOR CORPORATION
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "statusbarmodel.h"
19 #include "statusbarserver.h"
21 #include "hmi-debug.h"
23 #include <QtDBus/QDBusConnection>
28 class StatusBarModel::Private
31 Private(StatusBarModel *parent);
36 StatusBarServer server;
37 QString iconList[StatusBarServer::SupportedCount];
41 StatusBarModel::Private::Private(StatusBarModel *parent)
44 QDBusConnection dbus = QDBusConnection::sessionBus();
45 dbus.registerObject("/StatusBar", &server);
46 dbus.registerService("org.agl.homescreen");
47 connect(&server, &StatusBarServer::statusIconChanged, [&](int placeholderIndex, const QString &icon) {
48 if (placeholderIndex < 0 || StatusBarServer::SupportedCount <= placeholderIndex) return;
49 if (iconList[placeholderIndex] == icon) return;
50 iconList[placeholderIndex] = icon;
51 emit q->dataChanged(q->index(placeholderIndex), q->index(placeholderIndex));
53 for (int i = 0; i < StatusBarServer::SupportedCount; i++) {
54 iconList[i] = server.getStatusIcon(i);
58 StatusBarModel::StatusBarModel(QObject *parent)
59 : QAbstractListModel(parent)
60 , d(new Private(this))
64 StatusBarModel::~StatusBarModel()
69 void StatusBarModel::init(QUrl &url, QQmlContext *context)
71 HMI_DEBUG("HomeScreen", "StatusBarModel::init");
72 d->network = new Network(url, context);
73 context->setContextProperty("network", d->network);
75 QObject::connect(d->network, &Network::wifiConnectedChanged, this, &StatusBarModel::onWifiConnectedChanged);
76 QObject::connect(d->network, &Network::wifiEnabledChanged, this, &StatusBarModel::onWifiEnabledChanged);
77 QObject::connect(d->network, &Network::wifiStrengthChanged, this, &StatusBarModel::onWifiStrengthChanged);
79 setWifiStatus(d->network->wifiConnected(), d->network->wifiEnabled(), d->network->wifiStrength());
82 void StatusBarModel::setWifiStatus(bool connected, bool enabled, int strength)
84 HMI_DEBUG("HomeScreen", "StatusBarModel::setWifiStatus");
85 if (enabled && connected)
87 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_1Bar-01.png"));
88 else if (strength < 50)
89 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_2Bars-01.png"));
90 else if (strength < 70)
91 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_3Bars-01.png"));
93 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_Full-01.png"));
95 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_NoBars-01.png"));
98 void StatusBarModel::onWifiConnectedChanged(bool connected)
100 setWifiStatus(connected, d->network->wifiEnabled(), d->network->wifiStrength());
103 void StatusBarModel::onWifiEnabledChanged(bool enabled)
105 setWifiStatus(d->network->wifiConnected(), enabled, d->network->wifiStrength());
108 void StatusBarModel::onWifiStrengthChanged(int strength)
110 qInfo() << "Strength changed: " << strength;
111 setWifiStatus(d->network->wifiConnected(), d->network->wifiEnabled(), strength);
114 int StatusBarModel::rowCount(const QModelIndex &parent) const
116 if (parent.isValid())
119 return StatusBarServer::SupportedCount - 1;
122 QVariant StatusBarModel::data(const QModelIndex &index, int role) const
125 if (!index.isValid())
129 case Qt::DisplayRole:
130 if (index.row() == 0) {
131 ret = d->iconList[StatusBarServer::StatusWifi];
132 } else if (index.row() == 1) {
133 ret = d->iconList[StatusBarServer::StatusCellular];
143 QHash<int, QByteArray> StatusBarModel::roleNames() const
145 QHash<int, QByteArray> roles;
146 roles[Qt::DisplayRole] = "icon";