@@ -0,0 +1,185 @@
1+/*
2+ * Copyright 2015 Canonical Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU Lesser General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU Lesser General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU Lesser General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ *
16+ * Authors:
17+ * Mirco Mueller <mirco.mueller@canonical.com>
18+ */
19+
20+#include "MockNotificationModel.h"
21+#include "MockNotification.h"
22+
23+#include <unity/shell/notifications/ModelInterface.h>
24+
25+#include <QTimer>
26+#include <QList>
27+#include <QVector>
28+#include <QMap>
29+#include <QStringListModel>
30+#include <QQmlEngine>
31+#include <QDebug>
32+
33+using namespace unity::shell::notifications;
34+
35+struct MockNotificationModelPrivate {
36+ QList<QSharedPointer<MockNotification> > queue;
37+};
38+
39+MockNotificationModel::MockNotificationModel(QObject *parent) : QAbstractListModel(parent), p(new MockNotificationModelPrivate) {
40+}
41+
42+MockNotificationModel::~MockNotificationModel() {
43+}
44+
45+int MockNotificationModel::rowCount(const QModelIndex &parent) const {
46+ return p->queue.size();
47+}
48+
49+QVariant MockNotificationModel::data(const QModelIndex &index, int role) const {
50+ //printf("Data %d.\n", index.row());
51+ if (!index.isValid())
52+ return QVariant();
53+
54+ switch(role) {
55+ case ModelInterface::RoleType:
56+ return QVariant(p->queue[index.row()]->getType());
57+
58+ case ModelInterface::RoleId:
59+ return QVariant(p->queue[index.row()]->getID());
60+
61+ case ModelInterface::RoleSummary:
62+ return QVariant(p->queue[index.row()]->getSummary());
63+
64+ case ModelInterface::RoleBody:
65+ return QVariant(p->queue[index.row()]->getBody());
66+
67+ case ModelInterface::RoleValue:
68+ return QVariant(p->queue[index.row()]->getValue());
69+
70+ case ModelInterface::RoleIcon:
71+ return QVariant(p->queue[index.row()]->getIcon());
72+
73+ case ModelInterface::RoleSecondaryIcon:
74+ return QVariant(p->queue[index.row()]->getSecondaryIcon());
75+
76+ case ModelInterface::RoleActions:
77+ return QVariant::fromValue(p->queue[index.row()]->getActions());
78+
79+ case ModelInterface::RoleHints:
80+ return QVariant(p->queue[index.row()]->getHints());
81+
82+ case ModelInterface::RoleNotification:
83+ return QVariant(p->queue[index.row()]);
84+
85+ default:
86+ return QVariant();
87+ }
88+}
89+
90+void MockNotificationModel::append(const QSharedPointer<MockNotification> &n) {
91+ p->queue.append(n);
92+ //qDebug() << "append() called, count:" << p->queue.size() << ", summary: " << QString(p->queue[0]->getSummary());
93+}
94+
95+QSharedPointer<MockNotification> MockNotificationModel::getNotification(unsigned int id) const {
96+ for(int i=0; i<p->queue.size(); i++) {
97+ if(p->queue[i]->getID() == id) {
98+ return p->queue[i];
99+ }
100+ }
101+
102+ QSharedPointer<MockNotification> empty;
103+ return empty;
104+}
105+
106+QSharedPointer<MockNotification> MockNotificationModel::getNotification(const QString &summary) const {
107+ for(int i=0; i<p->queue.size(); i++) {
108+ if(p->queue[i]->getSummary() == summary) {
109+ return p->queue[i];
110+ }
111+ }
112+
113+ QSharedPointer<MockNotification> empty;
114+ return empty;
115+}
116+
117+bool MockNotificationModel::hasNotification(unsigned int id) const {
118+ return !(getNotification(id) != nullptr);
119+}
120+
121+void MockNotificationModel::removeNotification(const unsigned int id) {
122+ for(int i=0; i<p->queue.size(); i++) {
123+ if(p->queue[i]->getID() == id) {
124+ deleteFromVisible(i);
125+ return;
126+ }
127+ }
128+ // The ID was not found in any queue. Should it be an error case or not?
129+}
130+
131+void MockNotificationModel::deleteFirst() {
132+ if(p->queue.empty())
133+ return;
134+ deleteFromVisible(0);
135+}
136+
137+void MockNotificationModel::deleteFromVisible(int loc) {
138+ QModelIndex deletePoint = QModelIndex();
139+ beginRemoveRows(deletePoint, loc, loc);
140+ p->queue.erase(p->queue.begin() + loc);
141+ endRemoveRows();
142+}
143+
144+MockNotification* MockNotificationModel::getRaw(const unsigned int notificationId) const {
145+ for(int i=0; i<p->queue.size(); i++) {
146+ if(p->queue[i]->getID() == notificationId) {
147+ MockNotification* n = p->queue[i].data();
148+ QQmlEngine::setObjectOwnership(n, QQmlEngine::CppOwnership);
149+ return n;
150+ }
151+ }
152+
153+ return nullptr;
154+}
155+
156+int MockNotificationModel::queued() const {
157+ return p->queue.size();
158+}
159+
160+QHash<int, QByteArray> MockNotificationModel::roleNames() const {
161+ QHash<int, QByteArray> roles;
162+
163+ roles.insert(ModelInterface::RoleType, "type");
164+ roles.insert(ModelInterface::RoleUrgency, "urgency");
165+ roles.insert(ModelInterface::RoleId, "nid");
166+ roles.insert(ModelInterface::RoleSummary, "summary");
167+ roles.insert(ModelInterface::RoleBody, "body");
168+ roles.insert(ModelInterface::RoleValue, "value");
169+ roles.insert(ModelInterface::RoleIcon, "icon");
170+ roles.insert(ModelInterface::RoleSecondaryIcon, "secondaryIcon");
171+ roles.insert(ModelInterface::RoleActions, "actions");
172+ roles.insert(ModelInterface::RoleHints, "hints");
173+ roles.insert(ModelInterface::RoleNotification, "notification");
174+
175+ return roles;
176+}
177+
178+void MockNotificationModel::onDataChanged(unsigned int id) {
179+ for(int i=0; i<p->queue.size(); i++) {
180+ if(p->queue[i]->getID() == id) {
181+ Q_EMIT dataChanged(index(i, 0), index(i, 0));
182+ break;
183+ }
184+ }
185+}