Debian Codegraph @

tests/mocks/Lomiri/Notifications/MockNotificationModel.cpp

raw
1 /*
2 * Copyright 2015-2016 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 */
17
18 #include "MockNotificationModel.h"
19 #include "MockNotification.h"
20
21 #include <lomiri/shell/notifications/ModelInterface.h>
22
23 #include <QTimer>
24 #include <QList>
25 #include <QVector>
26 #include <QMap>
27 #include <QStringListModel>
28 #include <QQmlEngine>
29
30 using namespace lomiri::shell::notifications;
31
32 MockNotificationModel::MockNotificationModel(QObject *parent) : QAbstractListModel(parent) {
33 }
34
35 MockNotificationModel::~MockNotificationModel() {
36 Q_FOREACH(MockNotification *n, m_queue) {
37 n->deleteLater();
38 }
39 m_queue.clear();
40 }
41
42 int MockNotificationModel::rowCount(const QModelIndex &) const {
43 return m_queue.size();
44 }
45
46 int MockNotificationModel::getCount() const {
47 return m_queue.size();
48 }
49
50 QVariant MockNotificationModel::data(const QModelIndex &index, int role) const {
51 if (!index.isValid())
52 return QVariant();
53
54 switch(role) {
55 case ModelInterface::RoleType:
56 return QVariant(m_queue[index.row()]->getType());
57
58 case ModelInterface::RoleId:
59 return QVariant(m_queue[index.row()]->getID());
60
61 case ModelInterface::RoleSummary:
62 return QVariant(m_queue[index.row()]->getSummary());
63
64 case ModelInterface::RoleBody:
65 return QVariant(m_queue[index.row()]->getBody());
66
67 case ModelInterface::RoleValue:
68 return QVariant(m_queue[index.row()]->getValue());
69
70 case ModelInterface::RoleIcon:
71 return QVariant(m_queue[index.row()]->getIcon());
72
73 case ModelInterface::RoleSecondaryIcon:
74 return QVariant(m_queue[index.row()]->getSecondaryIcon());
75
76 case ModelInterface::RoleActions:
77 return QVariant::fromValue(m_queue[index.row()]->getActions());
78
79 case ModelInterface::RoleHints:
80 return QVariant(m_queue[index.row()]->getHints());
81
82 case ModelInterface::RoleNotification:
83 return QVariant::fromValue(m_queue[index.row()]);
84
85 default:
86 return QVariant();
87 }
88 }
89
90 void MockNotificationModel::append(MockNotification* n) {
91 QQmlEngine::setObjectOwnership(n, QQmlEngine::CppOwnership);
92 int location = m_queue.size();
93 QModelIndex insertionPoint = QModelIndex();
94 beginInsertRows(insertionPoint, location, location);
95 m_queue.insert(location, n);
96 endInsertRows();
97 }
98
99 MockNotification* MockNotificationModel::getNotification(int id) const {
100 for(int i = 0; i < m_queue.size(); i++) {
101 MockNotification* n = m_queue.at(i);
102 if(n && n->getID() == id) {
103 return n;
104 }
105 }
106
107 return nullptr;
108 }
109
110 void MockNotificationModel::remove(const int id) {
111 for(int i = 0; i < m_queue.size(); i++) {
112 if(m_queue[i]->getID() == id) {
113 removeInternal(i);
114 return;
115 }
116 }
117 }
118
119 void MockNotificationModel::removeFirst() {
120 if(m_queue.isEmpty())
121 return;
122 removeInternal(0);
123 }
124
125 void MockNotificationModel::removeInternal(int loc) {
126 QModelIndex deletePoint = QModelIndex();
127 beginRemoveRows(deletePoint, loc, loc);
128 m_queue.erase(m_queue.begin() + loc);
129 endRemoveRows();
130 }
131
132 MockNotification* MockNotificationModel::getRaw(const int notificationId) const {
133 return getNotification(notificationId);
134 }
135
136 int MockNotificationModel::queued() const {
137 return m_queue.size();
138 }
139
140 QHash<int, QByteArray> MockNotificationModel::roleNames() const {
141 QHash<int, QByteArray> roles;
142
143 roles.insert(ModelInterface::RoleType, "type");
144 roles.insert(ModelInterface::RoleUrgency, "urgency");
145 roles.insert(ModelInterface::RoleId, "id");
146 roles.insert(ModelInterface::RoleSummary, "summary");
147 roles.insert(ModelInterface::RoleBody, "body");
148 roles.insert(ModelInterface::RoleValue, "value");
149 roles.insert(ModelInterface::RoleIcon, "icon");
150 roles.insert(ModelInterface::RoleSecondaryIcon, "secondaryIcon");
151 roles.insert(ModelInterface::RoleActions, "actions");
152 roles.insert(ModelInterface::RoleHints, "hints");
153 roles.insert(ModelInterface::RoleNotification, "notification");
154
155 return roles;
156 }
157
158 void MockNotificationModel::onCompleted(int id) {
159 remove(id);
160 }
161
162 void MockNotificationModel::onDataChanged(int id) {
163 for(int i = 0; i < m_queue.size(); i++) {
164 if(m_queue[i]->getID() == id) {
165 Q_EMIT dataChanged(index(i, 0), index(i, 0));
166 break;
167 }
168 }
169 }