Merge lastest commit from homescreen-2017
[apps/homescreen.git] / homescreen / src / statusbarmodel.cpp
index 5438e89..5e63b7d 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2016 The Qt Company Ltd.
+ * Copyright (C) 2017, 2018 TOYOTA MOTOR CORPORATION
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -19,6 +20,8 @@
 
 #include <QtDBus/QDBusConnection>
 
+#include "network.h"
+
 class StatusBarModel::Private
 {
 public:
@@ -29,6 +32,7 @@ private:
 public:
     StatusBarServer server;
     QString iconList[StatusBarServer::SupportedCount];
+    Network *network;
 };
 
 StatusBarModel::Private::Private(StatusBarModel *parent)
@@ -59,12 +63,56 @@ StatusBarModel::~StatusBarModel()
     delete d;
 }
 
+void StatusBarModel::init(QUrl &url, QQmlContext *context)
+{
+    d->network = new Network(url, context);
+    context->setContextProperty("network", d->network);
+
+    QObject::connect(d->network, &Network::wifiConnectedChanged, this, &StatusBarModel::onWifiConnectedChanged);
+    QObject::connect(d->network, &Network::wifiEnabledChanged, this, &StatusBarModel::onWifiEnabledChanged);
+    QObject::connect(d->network, &Network::wifiStrengthChanged, this, &StatusBarModel::onWifiStrengthChanged);
+
+    setWifiStatus(d->network->wifiConnected(), d->network->wifiEnabled(), d->network->wifiStrength());
+}
+
+void StatusBarModel::setWifiStatus(bool connected, bool enabled, int strength)
+{
+    if (enabled && connected)
+        if (strength < 30)
+            d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_1Bar-01.png"));
+        else if (strength < 50)
+            d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_2Bars-01.png"));
+        else if (strength < 70)
+            d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_3Bars-01.png"));
+        else
+            d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_Full-01.png"));
+    else
+        d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_NoBars-01.png"));
+}
+
+void StatusBarModel::onWifiConnectedChanged(bool connected)
+{
+    setWifiStatus(connected, d->network->wifiEnabled(), d->network->wifiStrength());
+}
+
+void StatusBarModel::onWifiEnabledChanged(bool enabled)
+{
+    setWifiStatus(d->network->wifiConnected(), enabled, d->network->wifiStrength());
+}
+
+void StatusBarModel::onWifiStrengthChanged(int strength)
+{
+    qInfo() << "Strength changed: " << strength;
+    setWifiStatus(d->network->wifiConnected(), d->network->wifiEnabled(), strength);
+}
+
 int StatusBarModel::rowCount(const QModelIndex &parent) const
 {
     if (parent.isValid())
         return 0;
 
-    return StatusBarServer::SupportedCount;
+    // Delete bluetooth because use agl-service-bluetooth.
+    return StatusBarServer::SupportedCount - 1;
 }
 
 QVariant StatusBarModel::data(const QModelIndex &index, int role) const
@@ -75,7 +123,11 @@ QVariant StatusBarModel::data(const QModelIndex &index, int role) const
 
     switch (role) {
     case Qt::DisplayRole:
-        ret = d->iconList[index.row()];
+        if (index.row() == 0){
+            ret = d->iconList[StatusBarServer::StatusWifi];
+        }else if (index.row() == 1){
+            ret = d->iconList[StatusBarServer::StatusCellular];
+        }
         break;
     default:
         break;