summaryrefslogtreecommitdiff
path: root/sowatchui/scanwatchesmodel.cpp
diff options
context:
space:
mode:
authorJavier S. Pedro <maemo@javispedro.com>2012-08-09 04:03:20 +0200
committerJavier S. Pedro <maemo@javispedro.com>2012-08-09 04:03:20 +0200
commit406332eb6b3199d19388f359d04c9f184e6082b5 (patch)
tree0560e8dabcd715cdf1932b7ac6105df5d20025e5 /sowatchui/scanwatchesmodel.cpp
parent3aa62b0543d978c1a01c5cf05a898fd8d805c44b (diff)
downloadsowatch-406332eb6b3199d19388f359d04c9f184e6082b5.tar.gz
sowatch-406332eb6b3199d19388f359d04c9f184e6082b5.zip
watch status UI
Diffstat (limited to 'sowatchui/scanwatchesmodel.cpp')
-rw-r--r--sowatchui/scanwatchesmodel.cpp102
1 files changed, 102 insertions, 0 deletions
diff --git a/sowatchui/scanwatchesmodel.cpp b/sowatchui/scanwatchesmodel.cpp
new file mode 100644
index 0000000..5b7c331
--- /dev/null
+++ b/sowatchui/scanwatchesmodel.cpp
@@ -0,0 +1,102 @@
+#include <QtDebug>
+
+#include "scanwatchesmodel.h"
+
+ScanWatchesModel::ScanWatchesModel(QObject *parent) :
+ QAbstractListModel(parent),
+ _scanner(new ScannerProxy("com.javispedro.sowatchd", "/com/javispedro/sowatch/allscanner", QDBusConnection::sessionBus())),
+ _timer(new QTimer(this)),
+ _enabled(false), _active(false)
+{
+ QHash<int, QByteArray> roles = roleNames();
+ roles[Qt::DisplayRole] = QByteArray("title");
+ 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()));
+}
+
+ScanWatchesModel::~ScanWatchesModel()
+{
+}
+
+bool ScanWatchesModel::enabled() const
+{
+ return _enabled;
+}
+
+void ScanWatchesModel::setEnabled(bool enabled)
+{
+ _timer->stop();
+
+ _enabled = enabled;
+
+ if (_enabled && !_active) {
+ _scanner->Start();
+ }
+}
+
+bool ScanWatchesModel::active() const
+{
+ return _active;
+}
+
+int ScanWatchesModel::rowCount(const QModelIndex &parent) const
+{
+ return _list.count();
+}
+
+QVariant ScanWatchesModel::data(const QModelIndex &index, int role) const
+{
+ qDebug() << "Asked for data" << index.row() << index.column() << role;
+ const QVariantMap &info = _list.at(index.row());
+ switch (role) {
+ case Qt::DisplayRole:
+ return info["name"];
+ case Qt::StatusTipRole:
+ return info["address"];
+ case ObjectRole:
+ return QVariant::fromValue(info);
+ }
+ return QVariant();
+}
+
+void ScanWatchesModel::handleWatchFound(const QVariantMap &info)
+{
+ qDebug() << "Watch found" << info << endl;
+ if (!_list.contains(info)) {
+ int count = _list.count();
+ beginInsertRows(QModelIndex(), count, count);
+ _list.append(info);
+ endInsertRows();
+ }
+}
+
+void ScanWatchesModel::handleStarted()
+{
+ _active = true;
+ emit activeChanged();
+}
+
+void ScanWatchesModel::handleFinished()
+{
+ qDebug() << "Scan finished";
+ _active = false;
+ if (_enabled) {
+ _timer->start();
+ }
+ emit activeChanged();
+}
+
+void ScanWatchesModel::handleTimeout()
+{
+ qDebug() << "Restarting scan";
+ _scanner->Start();
+}