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 <QtDBus/QDBusConnection>
25 class StatusBarModel::Private
28 Private(StatusBarModel *parent);
33 StatusBarServer server;
34 QString iconList[StatusBarServer::SupportedCount];
38 StatusBarModel::Private::Private(StatusBarModel *parent)
41 QDBusConnection dbus = QDBusConnection::sessionBus();
42 dbus.registerObject("/StatusBar", &server);
43 dbus.registerService("org.agl.homescreen");
44 connect(&server, &StatusBarServer::statusIconChanged, [&](int placeholderIndex, const QString &icon) {
45 if (placeholderIndex < 0 || StatusBarServer::SupportedCount <= placeholderIndex) return;
46 if (iconList[placeholderIndex] == icon) return;
47 iconList[placeholderIndex] = icon;
48 emit q->dataChanged(q->index(placeholderIndex), q->index(placeholderIndex));
50 for (int i = 0; i < StatusBarServer::SupportedCount; i++) {
51 iconList[i] = server.getStatusIcon(i);
55 StatusBarModel::StatusBarModel(QObject *parent)
56 : QAbstractListModel(parent)
57 , d(new Private(this))
61 StatusBarModel::~StatusBarModel()
66 void StatusBarModel::init(QUrl &url, QQmlContext *context)
68 d->network = new Network(url, context);
69 context->setContextProperty("network", d->network);
71 QObject::connect(d->network, &Network::wifiConnectedChanged, this, &StatusBarModel::onWifiConnectedChanged);
72 QObject::connect(d->network, &Network::wifiEnabledChanged, this, &StatusBarModel::onWifiEnabledChanged);
73 QObject::connect(d->network, &Network::wifiStrengthChanged, this, &StatusBarModel::onWifiStrengthChanged);
75 setWifiStatus(d->network->wifiConnected(), d->network->wifiEnabled(), d->network->wifiStrength());
78 void StatusBarModel::setWifiStatus(bool connected, bool enabled, int strength)
80 if (enabled && connected)
82 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_1Bar-01.png"));
83 else if (strength < 50)
84 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_2Bars-01.png"));
85 else if (strength < 70)
86 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_3Bars-01.png"));
88 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_Full-01.png"));
90 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_NoBars-01.png"));
93 void StatusBarModel::onWifiConnectedChanged(bool connected)
95 setWifiStatus(connected, d->network->wifiEnabled(), d->network->wifiStrength());
98 void StatusBarModel::onWifiEnabledChanged(bool enabled)
100 setWifiStatus(d->network->wifiConnected(), enabled, d->network->wifiStrength());
103 void StatusBarModel::onWifiStrengthChanged(int strength)
105 qInfo() << "Strength changed: " << strength;
106 setWifiStatus(d->network->wifiConnected(), d->network->wifiEnabled(), strength);
109 int StatusBarModel::rowCount(const QModelIndex &parent) const
111 if (parent.isValid())
114 // Delete bluetooth because use agl-service-bluetooth.
115 return StatusBarServer::SupportedCount - 1;
118 QVariant StatusBarModel::data(const QModelIndex &index, int role) const
121 if (!index.isValid())
125 case Qt::DisplayRole:
126 if (index.row() == 0){
127 ret = d->iconList[StatusBarServer::StatusWifi];
128 }else if (index.row() == 1){
129 ret = d->iconList[StatusBarServer::StatusCellular];
139 QHash<int, QByteArray> StatusBarModel::roleNames() const
141 QHash<int, QByteArray> roles;
142 roles[Qt::DisplayRole] = "icon";