summaryrefslogtreecommitdiff
path: root/libsowatch/notificationsmodel.cpp
blob: ce0a2fbb7717eb817a7167a325d8b1ef88ab717c (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "notificationsmodel.h"

using namespace sowatch;

#define FOREACH_TYPE_FROM_TO(x, from, to) \
	for(Notification::Type x = from; x < to; x = static_cast<Notification::Type>(x + 1))

#define FOREACH_TYPE_UNTIL(x, to) FOREACH_TYPE_FROM_TO(x, Notification::OtherNotification, to)

#define FOREACH_TYPE(x) FOREACH_TYPE_FROM_TO(x, Notification::OtherNotification, Notification::TypeCount)

NotificationsModel::NotificationsModel(QObject *parent) :
    QAbstractListModel(parent)
{
	QHash<int, QByteArray> roles = roleNames();
	roles[Qt::DisplayRole] = QByteArray("title");
	roles[ObjectRole] = QByteArray("object");
	roles[BodyRole] = QByteArray("body");
	roles[CountRole] = QByteArray("count");
	setRoleNames(roles);
}

int NotificationsModel::rowCount(const QModelIndex &parent) const
{
	int count = 0;
	Q_UNUSED(parent);
	FOREACH_TYPE(type) {
		count += _list[type].count();
	}
	return count;
}

QVariant NotificationsModel::data(const QModelIndex &index, int role) const
{
	const Notification *n = getNotificationByIndex(index.row());
	if (!n) return QVariant();
	switch (role) {
	case Qt::DisplayRole:
		return QVariant::fromValue(n->title());
	case ObjectRole:
		return QVariant::fromValue(const_cast<sowatch::Notification*>(n));
	case BodyRole:
		return QVariant::fromValue(n->body());
	case CountRole:
		return QVariant::fromValue(n->count());
	}
	return QVariant();
}

void NotificationsModel::add(Notification *n)
{
	const Notification::Type type = n->type();
	const int offset = getAppendOffsetForType(type);

	beginInsertRows(QModelIndex(), offset, offset);
	_list[type].append(n);
	endInsertRows();

	emit modelChanged();

	connect(n, SIGNAL(changed()), SLOT(handleNotificationChanged()));
}

void NotificationsModel::remove(Notification *n)
{
	const Notification::Type type = n->type();
	remove(type, n);
}

void NotificationsModel::remove(Notification::Type type, Notification *n)
{
	const int subindex = _list[type].indexOf(n);
	const int index = getOffsetForType(type) + subindex;

	Q_ASSERT(index >= 0);

	disconnect(n, 0, this, 0);

	beginRemoveRows(QModelIndex(), index, index);
	_list[type].removeAt(subindex);
	endRemoveRows();

	emit modelChanged();
}

int NotificationsModel::fullCount() const
{
	int count = 0;
	FOREACH_TYPE(type) {
		count += fullCountByType(type);
	}
	return count;
}

int NotificationsModel::fullCountByType(Notification::Type type) const
{
	int count = 0;
	Q_FOREACH(const Notification *n, _list[type]) {
		if (n->priority() != Notification::Silent) {
			count += n->count();
		}
	}
	return count;
}

int NotificationsModel::fullCountByType(int type) const
{
	Q_ASSERT(type >= 0 && type < Notification::TypeCount);
	return fullCountByType(static_cast<Notification::Type>(type));
}

Notification* NotificationsModel::getMostRecentByType(Notification::Type type) const
{
	if (!_list[type].empty()) {
		// TODO Actually get the most recent (sort by date)
		return _list[type].first();
	} else {
		return 0;
	}
}

Notification* NotificationsModel::getMostRecentByType(int type) const
{
	Q_ASSERT(type >= 0 && type < Notification::TypeCount);
	return getMostRecentByType(static_cast<Notification::Type>(type));
}

Notification::Type NotificationsModel::getTypeOfDeletedNotification(Notification *n) const
{
	// Can't call any methods of 'n'
	FOREACH_TYPE(type) {
		if (_list[type].contains(n)) {
			return type;
		}
	}
	return Notification::OtherNotification;
}

int NotificationsModel::getOffsetForType(Notification::Type type) const
{
	int count = 0;
	FOREACH_TYPE_UNTIL(t, type) {
		count += _list[t].count();
	}
	return count;
}

int NotificationsModel::getAppendOffsetForType(Notification::Type type) const
{
	return getOffsetForType(type) + _list[type].count();
}

int NotificationsModel::getIndexForNotification(Notification *n) const
{
	Notification::Type type = n->type();
	const int subindex = _list[type].indexOf(n);

	Q_ASSERT(subindex >= 0);

	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();
	if (obj) {
		Notification* n = static_cast<Notification*>(obj);
		const int index = getIndexForNotification(n);

		emit dataChanged(createIndex(index, 0), createIndex(index, 0));
		emit modelChanged();
	}
}