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