Add autobuild
[apps/homescreen.git] / homescreen / src / statusbarmodel.cpp
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "statusbarmodel.h"
18 #include "statusbarserver.h"
19 #include <QDebug>
20 #include "hmi-debug.h"
21
22 #include <QtDBus/QDBusConnection>
23
24 #include "network.h"
25
26
27 class StatusBarModel::Private
28 {
29 public:
30     Private(StatusBarModel *parent);
31
32 private:
33     StatusBarModel *q;
34 public:
35     StatusBarServer server;
36     QString iconList[StatusBarServer::SupportedCount];
37     Network *network;
38     WifiAdapter *wifi_a;
39 };
40
41 StatusBarModel::Private::Private(StatusBarModel *parent)
42     : q(parent)
43 {
44     QDBusConnection dbus = QDBusConnection::sessionBus();
45     dbus.registerObject("/StatusBar", &server);
46     dbus.registerService("org.agl.homescreen");
47     connect(&server, &StatusBarServer::statusIconChanged, [&](int placeholderIndex, const QString &icon) {
48         if (placeholderIndex < 0 || StatusBarServer::SupportedCount <= placeholderIndex) return;
49         if (iconList[placeholderIndex] == icon) return;
50         iconList[placeholderIndex] = icon;
51         emit q->dataChanged(q->index(placeholderIndex), q->index(placeholderIndex));
52     });
53     for (int i = 0; i < StatusBarServer::SupportedCount; i++) {
54         iconList[i] = server.getStatusIcon(i);
55     }
56 }
57
58 StatusBarModel::StatusBarModel(QObject *parent)
59     : QAbstractListModel(parent)
60     , d(new Private(this))
61 {
62 }
63
64 StatusBarModel::~StatusBarModel()
65 {
66     delete d;
67 }
68
69 void StatusBarModel::init(QUrl &url, QQmlContext *context)
70 {
71     HMI_DEBUG("HomeScreen", "StatusBarModel::init");
72     d->network = new Network(url, context);
73     context->setContextProperty("network", d->network);
74     d->wifi_a = static_cast<WifiAdapter*>(d->network->findAdapter("wifi"));
75     Q_CHECK_PTR(d->wifi_a);
76
77     QObject::connect(d->wifi_a, &WifiAdapter::wifiConnectedChanged,
78                      this, &StatusBarModel::onWifiConnectedChanged);
79     QObject::connect(d->wifi_a, &WifiAdapter::wifiEnabledChanged,
80                      this, &StatusBarModel::onWifiEnabledChanged);
81     QObject::connect(d->wifi_a, &WifiAdapter::wifiStrengthChanged,
82                      this, &StatusBarModel::onWifiStrengthChanged);
83
84     setWifiStatus(d->wifi_a->wifiConnected(), d->wifi_a->wifiEnabled(), d->wifi_a->wifiStrength());
85 }
86
87 void StatusBarModel::setWifiStatus(bool connected, bool enabled, int strength)
88 {
89     HMI_DEBUG("HomeScreen", "StatusBarModel::setWifiStatus");
90     if (enabled && connected)
91         if (strength < 30)
92             d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_1Bar-01.png"));
93         else if (strength < 50)
94             d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_2Bars-01.png"));
95         else if (strength < 70)
96             d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_3Bars-01.png"));
97         else
98             d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_Full-01.png"));
99     else
100         d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_NoBars-01.png"));
101 }
102
103 void StatusBarModel::onWifiConnectedChanged(bool connected)
104 {
105     setWifiStatus(connected, d->wifi_a->wifiEnabled(), d->wifi_a->wifiStrength());
106 }
107
108 void StatusBarModel::onWifiEnabledChanged(bool enabled)
109 {
110     setWifiStatus(d->wifi_a->wifiConnected(), enabled, d->wifi_a->wifiStrength());
111 }
112
113 void StatusBarModel::onWifiStrengthChanged(int strength)
114 {
115     qInfo() << "Strength changed: " << strength;
116     setWifiStatus(d->wifi_a->wifiConnected(), d->wifi_a->wifiEnabled(), strength);
117 }
118
119 int StatusBarModel::rowCount(const QModelIndex &parent) const
120 {
121     if (parent.isValid())
122         return 0;
123
124     return StatusBarServer::SupportedCount - 1;
125 }
126
127 QVariant StatusBarModel::data(const QModelIndex &index, int role) const
128 {
129     QVariant ret;
130     if (!index.isValid())
131         return ret;
132
133     switch (role) {
134     case Qt::DisplayRole:
135         if (index.row() == 0) {
136             ret = d->iconList[StatusBarServer::StatusWifi];
137         } else if (index.row() == 1) {
138             ret = d->iconList[StatusBarServer::StatusCellular];
139         }
140         break;
141     default:
142         break;
143     }
144
145     return ret;
146 }
147
148 QHash<int, QByteArray> StatusBarModel::roleNames() const
149 {
150     QHash<int, QByteArray> roles;
151     roles[Qt::DisplayRole] = "icon";
152     return roles;
153 }