This repository has been archived on 2026-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
Michael Zanetti 22dd3fe27d intermediate commit.
Working pretty well now. No cleanup done. some broken menu entries related to
connect.

TBC
2019-02-06 03:00:43 +01:00

273 lines
7.2 KiB
C++

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2018 Michael Zanetti <michael.zanetti@guh.io> *
* *
* This file is part of nymea:app. *
* *
* nymea:app is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, version 3 of the License. *
* *
* nymea:app is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with nymea:app. If not, see <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "nymeahost.h"
#include <QUrl>
NymeaHost::NymeaHost(QObject *parent):
QObject(parent),
m_connections(new Connections(this))
{
connect(m_connections, &Connections::dataChanged, this, [this](const QModelIndex &, const QModelIndex &, const QVector<int>){
emit connectionChanged();
});
connect(m_connections, &Connections::connectionAdded, this, [this](Connection*){
emit connectionChanged();
});
connect(m_connections, &Connections::connectionRemoved, this, [this](Connection*){
emit connectionChanged();
});
}
QUuid NymeaHost::uuid() const
{
return m_uuid;
}
void NymeaHost::setUuid(const QUuid &uuid)
{
m_uuid = uuid;
}
QString NymeaHost::name() const
{
return m_name;
}
void NymeaHost::setName(const QString &name)
{
if (m_name != name) {
m_name = name;
emit nameChanged();
}
}
QString NymeaHost::version() const
{
return m_version;
}
void NymeaHost::setVersion(const QString &version)
{
if (m_version != version) {
m_version = version;
emit versionChanged();
}
}
Connections* NymeaHost::connections() const
{
return m_connections;
}
Connections::Connections(QObject *parent):
QAbstractListModel(parent)
{
}
int Connections::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_connections.count();
}
QVariant Connections::data(const QModelIndex &index, int role) const
{
switch (role) {
case RoleUrl:
return m_connections.at(index.row())->url();
case RoleName:
return m_connections.at(index.row())->displayName();
case RoleBearerType:
return m_connections.at(index.row())->bearerType();
case RoleSecure:
return m_connections.at(index.row())->secure();
case RoleOnline:
return m_connections.at(index.row())->online();
}
return QVariant();
}
Connection* Connections::find(const QUrl &url) const
{
foreach (Connection *conn, m_connections) {
if (conn->url() == url) {
return conn;
}
}
return nullptr;
}
void Connections::addConnection(Connection *connection)
{
connection->setParent(this);
beginInsertRows(QModelIndex(), m_connections.count(), m_connections.count());
m_connections.append(connection);
connect(connection, &Connection::onlineChanged, this, [this, connection]() {
int idx = m_connections.indexOf(connection);
if (idx < 0) {
return;
}
emit dataChanged(index(idx), index(idx), {RoleOnline});
});
endInsertRows();
emit connectionAdded(connection);
emit countChanged();
}
void Connections::removeConnection(Connection *connection)
{
int idx = m_connections.indexOf(connection);
if (idx == -1) {
qWarning() << "Cannot remove connections as it's not in this model";
return;
}
beginRemoveRows(QModelIndex(), idx, idx);
m_connections.takeAt(idx)->deleteLater();
endRemoveRows();
emit connectionRemoved(connection);
emit countChanged();
}
void Connections::removeConnection(int index)
{
if (index < 0 || index >= m_connections.count()) {
qWarning() << "Index out of range. Not removing any connection";
return;
}
beginRemoveRows(QModelIndex(), index, index);
m_connections.takeAt(index)->deleteLater();
endRemoveRows();
emit countChanged();
}
Connection* Connections::get(int index) const
{
if (index >= 0 && index < m_connections.count()) {
return m_connections.at(index);
}
return nullptr;
}
Connection *Connections::bestMatch(Connection::BearerTypes bearerTypes) const
{
QList<Connection::BearerType> bearerPreference = {Connection::BearerTypeEthernet, Connection::BearerTypeWifi, Connection::BearerTypeCloud, Connection::BearerTypeBluetooth, Connection::BearerTypeNone};
Connection *best = nullptr;
// qDebug() << "Bestmatch" << m_connections.count();
foreach (Connection *c, m_connections) {
// qDebug() << "have connection:" << bearerTypes << c->url() << bearerTypes.testFlag(c->bearerType());
if (!bearerTypes.testFlag(c->bearerType())) {
continue;
}
if (!best) {
best = c;
continue;
}
if (c->priority() > best->priority()) {
best = c;
}
}
return best;
}
QHash<int, QByteArray> Connections::roleNames() const
{
QHash<int, QByteArray> roles;
roles.insert(RoleUrl, "url");
roles.insert(RoleBearerType, "bearerType");
roles.insert(RoleName, "name");
roles.insert(RoleSecure, "secure");
roles.insert(RoleOnline, "online");
return roles;
}
Connection::Connection(const QUrl &url, Connection::BearerType bearerType, bool secure, const QString &displayName, QObject *parent):
QObject(parent),
m_url(url),
m_bearerType(bearerType),
m_secure(secure),
m_displayName(displayName)
{
}
QUrl Connection::url() const
{
return m_url;
}
Connection::BearerType Connection::bearerType() const
{
return m_bearerType;
}
bool Connection::secure() const
{
return m_secure;
}
QString Connection::displayName() const
{
return m_displayName;
}
bool Connection::online() const
{
return m_online;
}
void Connection::setOnline(bool online)
{
if (m_online != online) {
m_online = online;
emit onlineChanged();
}
}
int Connection::priority() const
{
int prio = 0;
switch(m_bearerType) {
case BearerTypeEthernet:
prio += 400;
break;
case BearerTypeWifi:
prio += 300;
break;
case BearerTypeBluetooth:
prio += 200;
break;
case BearerTypeCloud:
prio += 100;
break;
default:
prio += 0;
}
if (m_secure) {
prio += 10;
}
// if (m_url.scheme().startsWith("nymea")) {
// prio += 5;
// }
return prio;
}