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