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