on 00a2ef4 help sign in
unity8/tests/mocks/Unity/Notifications/MockNotificationModel.cpp
176 lines 5.1 kB history raw
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 */
20#include "MockNotificationModel.h"
21#include "MockNotification.h"
23#include <unity/shell/notifications/ModelInterface.h>
25#include <QTimer>
26#include <QList>
27#include <QVector>
28#include <QMap>
29#include <QStringListModel>
30#include <QQmlEngine>
32using namespace unity::shell::notifications;
34MockNotificationModel::MockNotificationModel(QObject *parent) : QAbstractListModel(parent) {
37MockNotificationModel::~MockNotificationModel() {
38 Q_FOREACH(MockNotification *n, m_queue) {
39 n->deleteLater();
40 }
41 m_queue.clear();
44int MockNotificationModel::rowCount(const QModelIndex &) const {
45 return m_queue.size();
48int MockNotificationModel::getCount() const {
49 return m_queue.size();
52QVariant MockNotificationModel::data(const QModelIndex &index, int role) const {
53 if (!index.isValid())
54 return QVariant();
56 switch(role) {
57 case ModelInterface::RoleType:
58 return QVariant(m_queue[index.row()]->getType());
60 case ModelInterface::RoleId:
61 return QVariant(m_queue[index.row()]->getID());
63 case ModelInterface::RoleSummary:
64 return QVariant(m_queue[index.row()]->getSummary());
66 case ModelInterface::RoleBody:
67 return QVariant(m_queue[index.row()]->getBody());
69 case ModelInterface::RoleValue:
70 return QVariant(m_queue[index.row()]->getValue());
72 case ModelInterface::RoleIcon:
73 return QVariant(m_queue[index.row()]->getIcon());
75 case ModelInterface::RoleSecondaryIcon:
76 return QVariant(m_queue[index.row()]->getSecondaryIcon());
78 case ModelInterface::RoleActions:
79 return QVariant::fromValue(m_queue[index.row()]->getActions());
81 case ModelInterface::RoleHints:
82 return QVariant(m_queue[index.row()]->getHints());
84 case ModelInterface::RoleNotification:
85 return QVariant::fromValue(m_queue[index.row()]);
87 default:
88 return QVariant();
89 }
92void MockNotificationModel::append(MockNotification* n) {
93 int location = m_queue.size();
94 QModelIndex insertionPoint = QModelIndex();
95 beginInsertRows(insertionPoint, location, location);
96 m_queue.insert(location, n);
97 endInsertRows();
100MockNotification* MockNotificationModel::getNotification(int id) const {
101 for(int i=0; i < m_queue.size(); i++) {
102 if(m_queue[i]->getID() == id) {
103 return m_queue[i];
104 }
105 }
107 return nullptr;
110void 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 }
119void MockNotificationModel::removeSecond() {
120 if(m_queue.size() < 2)
121 return;
122 removeInternal(1);
125void MockNotificationModel::removeInternal(int loc) {
126 QModelIndex deletePoint = QModelIndex();
127 beginRemoveRows(deletePoint, loc, loc);
128 m_queue.erase(m_queue.begin() + loc);
129 endRemoveRows();
132MockNotification* MockNotificationModel::getRaw(const int notificationId) const {
133 for(int i = 0; i < m_queue.size(); i++) {
134 if(m_queue[i]->getID() == notificationId) {
135 MockNotification* n = m_queue[i];
136 return n;
137 }
138 }
140 return nullptr;
143int MockNotificationModel::queued() const {
144 return m_queue.size();
147QHash<int, QByteArray> MockNotificationModel::roleNames() const {
148 QHash<int, QByteArray> roles;
150 roles.insert(ModelInterface::RoleType, "type");
151 roles.insert(ModelInterface::RoleUrgency, "urgency");
152 roles.insert(ModelInterface::RoleId, "id");
153 roles.insert(ModelInterface::RoleSummary, "summary");
154 roles.insert(ModelInterface::RoleBody, "body");
155 roles.insert(ModelInterface::RoleValue, "value");
156 roles.insert(ModelInterface::RoleIcon, "icon");
157 roles.insert(ModelInterface::RoleSecondaryIcon, "secondaryIcon");
158 roles.insert(ModelInterface::RoleActions, "actions");
159 roles.insert(ModelInterface::RoleHints, "hints");
160 roles.insert(ModelInterface::RoleNotification, "notification");
162 return roles;
165void MockNotificationModel::onCompleted(int id) {
166 remove(id);
169void MockNotificationModel::onDataChanged(int id) {
170 for(int i = 0; i < m_queue.size(); i++) {
171 if(m_queue[i]->getID() == id) {
172 Q_EMIT dataChanged(index(i, 0), index(i, 0));
173 break;
174 }
175 }