use two sdl app, change default sdl to sdl_usb
[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 <QDebug>
21 #include "hmi-debug.h"
22
23 #include <QtDBus/QDBusConnection>
24
25 #include "network.h"
26
27
28 class StatusBarModel::Private
29 {
30 public:
31     Private(StatusBarModel *parent);
32
33 private:
34     StatusBarModel *q;
35 public:
36     StatusBarServer server;
37     QString iconList[StatusBarServer::SupportedCount];
38     Network *network;
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
75     QObject::connect(d->network, &Network::wifiConnectedChanged, this, &StatusBarModel::onWifiConnectedChanged);
76     QObject::connect(d->network, &Network::wifiEnabledChanged, this, &StatusBarModel::onWifiEnabledChanged);
77     QObject::connect(d->network, &Network::wifiStrengthChanged, this, &StatusBarModel::onWifiStrengthChanged);
78
79     setWifiStatus(d->network->wifiConnected(), d->network->wifiEnabled(), d->network->wifiStrength());
80 }
81
82 void StatusBarModel::setWifiStatus(bool connected, bool enabled, int strength)
83 {
84     HMI_DEBUG("HomeScreen", "StatusBarModel::setWifiStatus");
85     if (enabled && connected)
86         if (strength < 30)
87             d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_1Bar-01.png"));
88         else if (strength < 50)
89             d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_2Bars-01.png"));
90         else if (strength < 70)
91             d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_3Bars-01.png"));
92         else
93             d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_Full-01.png"));
94     else
95         d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_NoBars-01.png"));
96 }
97
98 void StatusBarModel::onWifiConnectedChanged(bool connected)
99 {
100     setWifiStatus(connected, d->network->wifiEnabled(), d->network->wifiStrength());
101 }
102
103 void StatusBarModel::onWifiEnabledChanged(bool enabled)
104 {
105     setWifiStatus(d->network->wifiConnected(), enabled, d->network->wifiStrength());
106 }
107
108 void StatusBarModel::onWifiStrengthChanged(int strength)
109 {
110     qInfo() << "Strength changed: " << strength;
111     setWifiStatus(d->network->wifiConnected(), d->network->wifiEnabled(), strength);
112 }
113
114 int StatusBarModel::rowCount(const QModelIndex &parent) const
115 {
116     if (parent.isValid())
117         return 0;
118
119     return StatusBarServer::SupportedCount - 1;
120 }
121
122 QVariant StatusBarModel::data(const QModelIndex &index, int role) const
123 {
124     QVariant ret;
125     if (!index.isValid())
126         return ret;
127
128     switch (role) {
129     case Qt::DisplayRole:
130         if (index.row() == 0) {
131             ret = d->iconList[StatusBarServer::StatusWifi];
132         } else if (index.row() == 1) {
133             ret = d->iconList[StatusBarServer::StatusCellular];
134         }
135         break;
136     default:
137         break;
138     }
139
140     return ret;
141 }
142
143 QHash<int, QByteArray> StatusBarModel::roleNames() const
144 {
145     QHash<int, QByteArray> roles;
146     roles[Qt::DisplayRole] = "icon";
147     return roles;
148 }