Merge lastest commit from homescreen-2017
[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
21 #include <QtDBus/QDBusConnection>
22
23 #include "network.h"
24
25 class StatusBarModel::Private
26 {
27 public:
28     Private(StatusBarModel *parent);
29
30 private:
31     StatusBarModel *q;
32 public:
33     StatusBarServer server;
34     QString iconList[StatusBarServer::SupportedCount];
35     Network *network;
36 };
37
38 StatusBarModel::Private::Private(StatusBarModel *parent)
39     : q(parent)
40 {
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));
49     });
50     for (int i = 0; i < StatusBarServer::SupportedCount; i++) {
51         iconList[i] = server.getStatusIcon(i);
52     }
53 }
54
55 StatusBarModel::StatusBarModel(QObject *parent)
56     : QAbstractListModel(parent)
57     , d(new Private(this))
58 {
59 }
60
61 StatusBarModel::~StatusBarModel()
62 {
63     delete d;
64 }
65
66 void StatusBarModel::init(QUrl &url, QQmlContext *context)
67 {
68     d->network = new Network(url, context);
69     context->setContextProperty("network", d->network);
70
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);
74
75     setWifiStatus(d->network->wifiConnected(), d->network->wifiEnabled(), d->network->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->network->wifiEnabled(), d->network->wifiStrength());
96 }
97
98 void StatusBarModel::onWifiEnabledChanged(bool enabled)
99 {
100     setWifiStatus(d->network->wifiConnected(), enabled, d->network->wifiStrength());
101 }
102
103 void StatusBarModel::onWifiStrengthChanged(int strength)
104 {
105     qInfo() << "Strength changed: " << strength;
106     setWifiStatus(d->network->wifiConnected(), d->network->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 }