aboutsummaryrefslogtreecommitdiffstats
path: root/core/connectionlistmodel.cpp
blob: 6966802e34158153ff951bf0bde82f33955c124c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// SPDX-License-Identifier: GPL-2.0

#include "core/connectionlistmodel.h"
#if defined(BT_SUPPORT)
#include "core/btdiscovery.h"
#endif

ConnectionListModel::ConnectionListModel(QObject *parent) :
	QAbstractListModel(parent)
{
}

QHash <int, QByteArray> ConnectionListModel::roleNames() const
{
	QHash<int, QByteArray> roles;
	roles[Qt::DisplayRole] = "display";
	return roles;
}

QVariant ConnectionListModel::data(const QModelIndex &index, int role) const
{
	if (index.row() < 0 || index.row() >= m_addresses.count())
		return QVariant();
	if (role != Qt::DisplayRole)
		return QVariant();
	return m_addresses[index.row()];
}

int ConnectionListModel::rowCount(const QModelIndex&) const
{
	return m_addresses.count();
}

void ConnectionListModel::addAddress(const QString &address)
{
	if (!m_addresses.contains(address)) {
		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);
		endInsertRows();
	}
}

void ConnectionListModel::removeAllAddresses()
{
	if (rowCount() == 0)
		return;

	beginResetModel();
	m_addresses.clear();
	endResetModel();
}

int ConnectionListModel::indexOf(const QString &address) const
{
	for (int i = 0; i < m_addresses.count(); i++)
		if (m_addresses.at(i).contains(address, Qt::CaseInsensitive))
			return i;
	return -1;
}