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