summaryrefslogtreecommitdiff
path: root/sowatchui/watchscannermodel.cpp
blob: f2c76f795611549efd86d888f93171b8002e8265 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "watchscannermodel.h"

WatchScannerModel::WatchScannerModel(QObject *parent) :
    QAbstractListModel(parent),
    _scanner(new sowatch::AllWatchScanner(this)),
    _timer(new QTimer(this)),
    _enabled(false), _active(false)
{
	QHash<int, QByteArray> roles = roleNames();
	roles[Qt::DisplayRole] = QByteArray("title");
	roles[Qt::DecorationRole] = QByteArray("iconSource");
	roles[Qt::StatusTipRole] = QByteArray("subtitle");
	roles[ObjectRole] = QByteArray("object");
	setRoleNames(roles);

	_timer->setSingleShot(true);
	_timer->setInterval(3000);

	connect(_scanner, SIGNAL(watchFound(QVariantMap)), SLOT(handleWatchFound(QVariantMap)));
	connect(_scanner, SIGNAL(started()), SLOT(handleStarted()));
	connect(_scanner, SIGNAL(finished()), SLOT(handleFinished()));
	connect(_timer, SIGNAL(timeout()), SLOT(handleTimeout()));
}

WatchScannerModel::~WatchScannerModel()
{
}

bool WatchScannerModel::enabled() const
{
	return _enabled;
}

void WatchScannerModel::setEnabled(bool enabled)
{
	_timer->stop();

	_enabled = enabled;

	if (_enabled && !_active) {
		_scanner->start();
	}
}

bool WatchScannerModel::active() const
{
	return _active;
}

int WatchScannerModel::rowCount(const QModelIndex &parent) const
{
	Q_UNUSED(parent);
	return _list.count();
}

QVariant WatchScannerModel::data(const QModelIndex &index, int role) const
{
	const QVariantMap &info = _list.at(index.row());
	switch (role) {
	case Qt::DisplayRole:
		return info["name"];
	case Qt::DecorationRole:
		return QVariant(SOWATCH_RESOURCES_DIR "/sowatch64.png");
	case Qt::StatusTipRole:
		return info["address"];
	case ObjectRole:
		return QVariant::fromValue(info);
	}
	return QVariant();
}

void WatchScannerModel::handleWatchFound(const QVariantMap &info)
{
	qDebug() << "Scan found a watch:" << info << endl;
	if (!_list.contains(info)) {
		int count = _list.count();
		beginInsertRows(QModelIndex(), count, count);
		_list.append(info);
		endInsertRows();
	}
}

void WatchScannerModel::handleStarted()
{
	_active = true;
	emit activeChanged();
}

void WatchScannerModel::handleFinished()
{
	qDebug() << "Scan finished";
	_active = false;
	if (_enabled) {
		qDebug() << "Setting timer for next scan";
		_timer->start();
	}
	emit activeChanged();
}

void WatchScannerModel::handleTimeout()
{
	qDebug() << "Restarting scan now";
	_scanner->start();
}