meson.build: Use 0.0.24 as the agl compositor version
[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 #include <network.h>
21 #include <wifiadapter.h>
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     Network *network;
34     WifiAdapter *wifi_a;
35 };
36
37 StatusBarModel::Private::Private(StatusBarModel *parent)
38     : q(parent)
39 {
40     connect(&server, &StatusBarServer::statusIconChanged, [&](int placeholderIndex, const QString &icon) {
41         if (placeholderIndex < 0 || StatusBarServer::SupportedCount <= placeholderIndex) return;
42         if (iconList[placeholderIndex] == icon) return;
43         iconList[placeholderIndex] = icon;
44         emit q->dataChanged(q->index(placeholderIndex), q->index(placeholderIndex));
45     });
46     for (int i = 0; i < StatusBarServer::SupportedCount; i++) {
47         iconList[i] = server.getStatusIcon(i);
48     }
49 }
50
51 StatusBarModel::StatusBarModel(QObject *parent)
52     : QAbstractListModel(parent)
53     , d(new Private(this))
54 {
55 }
56
57 StatusBarModel::~StatusBarModel()
58 {
59     delete d;
60 }
61
62 void StatusBarModel::init(QQmlContext *context)
63 {
64     d->network = new Network(false, context);
65     context->setContextProperty("network", d->network);
66     d->wifi_a = static_cast<WifiAdapter*>(d->network->findAdapter("wifi"));
67     Q_CHECK_PTR(d->wifi_a);
68
69     QObject::connect(d->wifi_a, &WifiAdapter::wifiConnectedChanged,
70                      this, &StatusBarModel::onWifiConnectedChanged);
71     QObject::connect(d->wifi_a, &WifiAdapter::wifiEnabledChanged,
72                      this, &StatusBarModel::onWifiEnabledChanged);
73     QObject::connect(d->wifi_a, &WifiAdapter::wifiStrengthChanged,
74                      this, &StatusBarModel::onWifiStrengthChanged);
75
76     setWifiStatus(d->wifi_a->wifiConnected(), d->wifi_a->wifiEnabled(), d->wifi_a->wifiStrength());
77 }
78
79 void StatusBarModel::setWifiStatus(bool connected, bool enabled, int strength)
80 {
81     if (enabled && connected)
82         if (strength < 30)
83             d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_1Bar-01.png"));
84         else if (strength < 50)
85             d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_2Bars-01.png"));
86         else if (strength < 70)
87             d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_3Bars-01.png"));
88         else
89             d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_Full-01.png"));
90     else
91         d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_NoBars-01.png"));
92 }
93
94 void StatusBarModel::onWifiConnectedChanged(bool connected)
95 {
96     setWifiStatus(connected, d->wifi_a->wifiEnabled(), d->wifi_a->wifiStrength());
97 }
98
99 void StatusBarModel::onWifiEnabledChanged(bool enabled)
100 {
101     setWifiStatus(d->wifi_a->wifiConnected(), enabled, d->wifi_a->wifiStrength());
102 }
103
104 void StatusBarModel::onWifiStrengthChanged(int strength)
105 {
106     qInfo() << "Strength changed: " << strength;
107     setWifiStatus(d->wifi_a->wifiConnected(), d->wifi_a->wifiEnabled(), strength);
108 }
109
110 int StatusBarModel::rowCount(const QModelIndex &parent) const
111 {
112     if (parent.isValid())
113         return 0;
114
115     // Delete bluetooth because use agl-service-bluetooth.
116     return StatusBarServer::SupportedCount - 1;
117 }
118
119 QVariant StatusBarModel::data(const QModelIndex &index, int role) const
120 {
121     QVariant ret;
122     if (!index.isValid())
123         return ret;
124
125     switch (role) {
126     case Qt::DisplayRole:
127         if (index.row() == 0){
128             ret = d->iconList[StatusBarServer::StatusWifi];
129         }else if (index.row() == 1){
130             ret = d->iconList[StatusBarServer::StatusCellular];
131         }
132         break;
133     default:
134         break;
135     }
136
137     return ret;
138 }
139
140 QHash<int, QByteArray> StatusBarModel::roleNames() const
141 {
142     QHash<int, QByteArray> roles;
143     roles[Qt::DisplayRole] = "icon";
144     return roles;
145 }