summaryrefslogtreecommitdiff
path: root/saltoqd/notificationmanager.cpp
blob: 35b5c00d8470a9ced9328c3450481e42a22aeb27 (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
#include <QtCore/QFileInfo>
#include "notificationmanager.h"
#include "notificationmonitor.h"

NotificationManager::NotificationManager(CardManager *card, ToqManager *toq) :
	QObject(toq), _toq(toq), _monitor(NotificationMonitor::instance()),
	_card(card),
	_deck(new CardDeck("com.qualcomm.qce.androidnotifications", "Notifications", this))
{
	_card->installDeck(_deck);

	connect(_monitor, &NotificationMonitor::notification,
			this, &NotificationManager::handleNotification);
}

void NotificationManager::handleNotification(MonitoredNotification *n)
{
	uint notificationId = n->id();

	if (n->sender().isEmpty() || (n->body().isEmpty() && n->summary().isEmpty())) {
		// Never create a card for an empty notification
		return;
	}

	Card *card = new Card(QString::number(qint64(notificationId)));

	card->setHeader(n->sender());
	card->setTitle(n->summary());
	card->setText(n->body());
	card->setDateTime(n->timestamp());
	card->setVibrate(true);

	if (!n->icon().isEmpty()) {
		QFileInfo imgFile(n->icon());
		QImage img(imgFile.absoluteFilePath());
		if (!img.isNull()) {
			card->setIcon(_card->sendImage(_deck, imgFile.completeBaseName(), img.scaled(48, 48, Qt::KeepAspectRatio)));
		} else {
			qDebug() << "Could not read icon from notification: " << imgFile.absoluteFilePath();
		}
	}

	connect(n, &MonitoredNotification::bodyChanged,
			this, &NotificationManager::handleNotificationBodyChanged);
	connect(n, &MonitoredNotification::closed,
			this, &NotificationManager::handleClosedNotification);

	_cards.insert(notificationId, card);
	_deck->insertCard(0, card);
}

void NotificationManager::handleNotificationBodyChanged()
{
	MonitoredNotification *n = static_cast<MonitoredNotification*>(sender());
	uint notificationId = n->id();
	Card *card = _cards.take(notificationId);
	if (card) {
		card->setText(n->body());
	} else {
		qDebug() << "Notification" << notificationId << "does not have an attached card";
	}
}

void NotificationManager::handleClosedNotification()
{
	MonitoredNotification *n = static_cast<MonitoredNotification*>(sender());
	uint notificationId = n->id();
	Card *card = _cards.take(notificationId);
	if (card) {
		_deck->removeCard(card);
		card->deleteLater();
	} else {
		qDebug() << "Notification" << notificationId << "does not have an attached card";
	}
	disconnect(n, 0, this, 0);
}