2017-10-06 14:51:30 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
#include "core/connectionlistmodel.h"
|
2020-05-15 00:37:45 +00:00
|
|
|
#if defined(BT_SUPPORT)
|
|
|
|
#include "core/btdiscovery.h"
|
|
|
|
#endif
|
2017-10-06 14:51:30 +00:00
|
|
|
|
|
|
|
ConnectionListModel::ConnectionListModel(QObject *parent) :
|
|
|
|
QAbstractListModel(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-16 07:47:28 +00:00
|
|
|
QHash <int, QByteArray> ConnectionListModel::roleNames() const
|
|
|
|
{
|
|
|
|
QHash<int, QByteArray> roles;
|
|
|
|
roles[Qt::DisplayRole] = "display";
|
|
|
|
return roles;
|
|
|
|
}
|
|
|
|
|
2017-10-06 14:51:30 +00:00
|
|
|
QVariant ConnectionListModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (index.row() < 0 || index.row() >= m_addresses.count())
|
|
|
|
return QVariant();
|
2019-03-20 14:31:24 +00:00
|
|
|
if (role != Qt::DisplayRole)
|
2017-10-06 14:51:30 +00:00
|
|
|
return QVariant();
|
|
|
|
return m_addresses[index.row()];
|
|
|
|
}
|
|
|
|
|
2018-05-21 15:36:04 +00:00
|
|
|
int ConnectionListModel::rowCount(const QModelIndex&) const
|
2017-10-06 14:51:30 +00:00
|
|
|
{
|
|
|
|
return m_addresses.count();
|
|
|
|
}
|
|
|
|
|
2019-03-22 20:25:59 +00:00
|
|
|
void ConnectionListModel::addAddress(const QString &address)
|
2017-10-06 14:51:30 +00:00
|
|
|
{
|
2018-01-23 20:50:11 +00:00
|
|
|
if (!m_addresses.contains(address)) {
|
2020-05-15 00:37:45 +00:00
|
|
|
int idx = rowCount();
|
|
|
|
#if defined(BT_SUPPORT)
|
|
|
|
// make sure that addresses that are just a BT/BLE address without name stay at the end of the list
|
|
|
|
if (address != extractBluetoothAddress(address)) {
|
|
|
|
for (idx = 0; idx < rowCount(); idx++)
|
|
|
|
if (m_addresses[idx] == extractBluetoothAddress(m_addresses[idx]))
|
|
|
|
// found the first name-less BT/BLE address, insert before that
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
beginInsertRows(QModelIndex(), idx, idx);
|
|
|
|
m_addresses.insert(idx, address);
|
2018-01-23 20:50:11 +00:00
|
|
|
endInsertRows();
|
|
|
|
}
|
2017-10-06 14:51:30 +00:00
|
|
|
}
|
2017-10-11 17:31:45 +00:00
|
|
|
|
|
|
|
void ConnectionListModel::removeAllAddresses()
|
|
|
|
{
|
2020-04-10 21:31:34 +00:00
|
|
|
if (rowCount() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
beginResetModel();
|
2017-10-11 17:31:45 +00:00
|
|
|
m_addresses.clear();
|
2020-04-10 21:31:34 +00:00
|
|
|
endResetModel();
|
2017-10-11 17:31:45 +00:00
|
|
|
}
|
2018-08-08 12:54:10 +00:00
|
|
|
|
2019-03-22 20:25:59 +00:00
|
|
|
int ConnectionListModel::indexOf(const QString &address) const
|
2018-08-08 12:54:10 +00:00
|
|
|
{
|
2019-03-22 20:25:59 +00:00
|
|
|
for (int i = 0; i < m_addresses.count(); i++)
|
|
|
|
if (m_addresses.at(i).contains(address, Qt::CaseInsensitive))
|
|
|
|
return i;
|
|
|
|
return -1;
|
2018-08-08 12:54:10 +00:00
|
|
|
}
|