2 * Copyright (C) 2016 The Qt Company Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "statusbarmodel.h"
18 #include "statusbarserver.h"
20 #include "hmi-debug.h"
22 #include <QtDBus/QDBusConnection>
27 class StatusBarModel::Private
30 Private(StatusBarModel *parent);
35 StatusBarServer server;
36 QString iconList[StatusBarServer::SupportedCount];
40 StatusBarModel::Private::Private(StatusBarModel *parent)
43 QDBusConnection dbus = QDBusConnection::sessionBus();
44 dbus.registerObject("/StatusBar", &server);
45 dbus.registerService("org.agl.homescreen");
46 connect(&server, &StatusBarServer::statusIconChanged, [&](int placeholderIndex, const QString &icon) {
47 if (placeholderIndex < 0 || StatusBarServer::SupportedCount <= placeholderIndex) return;
48 if (iconList[placeholderIndex] == icon) return;
49 iconList[placeholderIndex] = icon;
50 emit q->dataChanged(q->index(placeholderIndex), q->index(placeholderIndex));
52 for (int i = 0; i < StatusBarServer::SupportedCount; i++) {
53 iconList[i] = server.getStatusIcon(i);
57 StatusBarModel::StatusBarModel(QObject *parent)
58 : QAbstractListModel(parent)
59 , d(new Private(this))
63 StatusBarModel::~StatusBarModel()
68 void StatusBarModel::init(QUrl &url, QQmlContext *context)
70 HMI_DEBUG("HomeScreen", "StatusBarModel::init");
71 d->network = new Network(url, context);
72 context->setContextProperty("network", d->network);
74 QObject::connect(d->network, &Network::wifiConnectedChanged, this, &StatusBarModel::onWifiConnectedChanged);
75 QObject::connect(d->network, &Network::wifiEnabledChanged, this, &StatusBarModel::onWifiEnabledChanged);
76 QObject::connect(d->network, &Network::wifiStrengthChanged, this, &StatusBarModel::onWifiStrengthChanged);
78 setWifiStatus(d->network->wifiConnected(), d->network->wifiEnabled(), d->network->wifiStrength());
81 void StatusBarModel::setWifiStatus(bool connected, bool enabled, int strength)
83 HMI_DEBUG("HomeScreen", "StatusBarModel::setWifiStatus");
84 if (enabled && connected)
86 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_1Bar-01.png"));
87 else if (strength < 50)
88 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_2Bars-01.png"));
89 else if (strength < 70)
90 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_3Bars-01.png"));
92 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_Full-01.png"));
94 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_NoBars-01.png"));
97 void StatusBarModel::onWifiConnectedChanged(bool connected)
99 setWifiStatus(connected, d->network->wifiEnabled(), d->network->wifiStrength());
102 void StatusBarModel::onWifiEnabledChanged(bool enabled)
104 setWifiStatus(d->network->wifiConnected(), enabled, d->network->wifiStrength());
107 void StatusBarModel::onWifiStrengthChanged(int strength)
109 qInfo() << "Strength changed: " << strength;
110 setWifiStatus(d->network->wifiConnected(), d->network->wifiEnabled(), strength);
113 int StatusBarModel::rowCount(const QModelIndex &parent) const
115 if (parent.isValid())
118 return StatusBarServer::SupportedCount - 1;
121 QVariant StatusBarModel::data(const QModelIndex &index, int role) const
124 if (!index.isValid())
128 case Qt::DisplayRole:
129 if (index.row() == 0) {
130 ret = d->iconList[StatusBarServer::StatusWifi];
131 } else if (index.row() == 1) {
132 ret = d->iconList[StatusBarServer::StatusCellular];
142 QHash<int, QByteArray> StatusBarModel::roleNames() const
144 QHash<int, QByteArray> roles;
145 roles[Qt::DisplayRole] = "icon";