From 378611f629abc146eaf0b13301f119d826edb86b Mon Sep 17 00:00:00 2001
From: "Javier S. Pedro" <maemo@javispedro.com>
Date: Sat, 15 Jun 2013 20:35:33 +0200
Subject: add some support for notifications in liveview

---
 libsowatch/notificationsmodel.cpp | 90 ++++++++++++++++++++++++++++++---------
 libsowatch/notificationsmodel.h   |  9 +++-
 libsowatch/watchserver.cpp        |  3 +-
 3 files changed, 80 insertions(+), 22 deletions(-)

(limited to 'libsowatch')

diff --git a/libsowatch/notificationsmodel.cpp b/libsowatch/notificationsmodel.cpp
index 53b714e..16f6692 100644
--- a/libsowatch/notificationsmodel.cpp
+++ b/libsowatch/notificationsmodel.cpp
@@ -22,17 +22,13 @@ NotificationsModel::NotificationsModel(QObject *parent) :
 
 int NotificationsModel::rowCount(const QModelIndex &parent) const
 {
-	int count = 0;
 	Q_UNUSED(parent);
-	FOREACH_TYPE(type) {
-		count += _list[type].count();
-	}
-	return count;
+	return size();
 }
 
 QVariant NotificationsModel::data(const QModelIndex &index, int role) const
 {
-	const Notification *n = getNotificationByIndex(index.row());
+	const Notification *n = at(index.row());
 	if (!n) return QVariant();
 	switch (role) {
 	case Qt::DisplayRole:
@@ -47,6 +43,63 @@ QVariant NotificationsModel::data(const QModelIndex &index, int role) const
 	return QVariant();
 }
 
+int NotificationsModel::size() const
+{
+	int count = 0;
+	FOREACH_TYPE(type) {
+		count += _list[type].count();
+	}
+	return count;
+}
+
+int NotificationsModel::size(const QList<Notification::Type>& types) const
+{
+	int count = 0;
+	foreach(Notification::Type type, types) {
+		count += _list[type].count();
+	}
+	return count;
+}
+
+Notification * NotificationsModel::at(int position) const
+{
+	FOREACH_TYPE(type) {
+		const int size = _list[type].size();
+		if (position < size) {
+			return _list[type].at(position);
+		} else {
+			position -= size;
+		}
+	}
+	qWarning() << "Notification with index" << position << "not found";
+	return 0;
+}
+
+Notification * NotificationsModel::at(Notification::Type type, int position) const
+{
+	const int size = _list[type].size();
+	if (position < size) {
+		return _list[type].at(position);
+	} else {
+		qWarning() << "Notification with index" << position << "not found";
+		return 0;
+	}
+}
+
+Notification * NotificationsModel::at(const QList<Notification::Type>& types, int position) const
+{
+	foreach(Notification::Type type, types) {
+		const int size = _list[type].size();
+		if (position < size) {
+			return _list[type].at(position);
+		} else {
+			position -= size;
+		}
+	}
+	qWarning() << "Notification with index" << position << "not found";
+	return 0;
+}
+
 void NotificationsModel::add(Notification *n)
 {
 	const Notification::Type type = n->type();
@@ -83,6 +136,17 @@ void NotificationsModel::remove(Notification::Type type, Notification *n)
 	emit modelChanged();
 }
 
+int NotificationsModel::countByType(Notification::Type type) const
+{
+	int count = 0;
+	Q_FOREACH(const Notification *n, _list[type]) {
+		if (n->priority() != Notification::Silent) {
+			count++;
+		}
+	}
+	return count;
+}
+
 int NotificationsModel::fullCount() const
 {
 	int count = 0;
@@ -160,20 +224,6 @@ int NotificationsModel::getIndexForNotification(Notification *n) const
 	return getOffsetForType(type) + subindex;
 }
 
-const Notification * NotificationsModel::getNotificationByIndex(int index) const
-{
-	FOREACH_TYPE(type) {
-		const int size = _list[type].size();
-		if (index < size) {
-			return _list[type].at(index);
-		} else {
-			index -= size;
-		}
-	}
-	qWarning() << "Notification with index" << index << "not found";
-	return 0;
-}
-
 void NotificationsModel::handleNotificationChanged()
 {
 	QObject *obj = sender();
diff --git a/libsowatch/notificationsmodel.h b/libsowatch/notificationsmodel.h
index ffd8ab7..86ed2af 100644
--- a/libsowatch/notificationsmodel.h
+++ b/libsowatch/notificationsmodel.h
@@ -24,10 +24,18 @@ public:
 	int rowCount(const QModelIndex &parent) const;
 	QVariant data(const QModelIndex &index, int role) const;
 
+	int size() const;
+	int size(const QList<Notification::Type>& types) const;
+	Notification* at(int position) const;
+	Notification* at(Notification::Type type, int position) const;
+	Notification* at(const QList<Notification::Type>& types, int position) const;
+
 	void add(Notification *n);
 	void remove(Notification *n);
 	void remove(Notification::Type type, Notification *n);
 
+	Q_INVOKABLE int countByType(Notification::Type type) const;
+
 	Q_INVOKABLE int fullCount() const;
 	Q_INVOKABLE int fullCountByType(Notification::Type type) const;
 	Q_INVOKABLE int fullCountByType(int type) const; // See QTBUG-26415
@@ -44,7 +52,6 @@ private:
 	int getOffsetForType(Notification::Type type) const;
 	int getAppendOffsetForType(Notification::Type type) const;
 	int getIndexForNotification(Notification *n) const;
-	const Notification* getNotificationByIndex(int index) const;
 
 private slots:
 	void handleNotificationChanged();
diff --git a/libsowatch/watchserver.cpp b/libsowatch/watchserver.cpp
index da93730..373d367 100644
--- a/libsowatch/watchserver.cpp
+++ b/libsowatch/watchserver.cpp
@@ -32,6 +32,7 @@ WatchServer::WatchServer(Watch* watch, QObject* parent) :
 
 	_watchlets->setWatchModel(_watch->model());
 	_watch->setWatchletsModel(_watchlets);
+	_watch->setNotificationsModel(_notifications);
 }
 
 Watch* WatchServer::watch()
@@ -242,7 +243,7 @@ void WatchServer::closeWatchlet()
 			deactivateActiveWatchlet();
 		}
 		_currentWatchlet = 0;
-		if (_watch->isConnected() && _pendingNotifications.empty()) {
+		if (_watch->isConnected()) {
 			goToIdle();
 		}
 	}
-- 
cgit v1.2.3