on e804c19 help sign in
unity8/tests/mocks/Unity/Notifications/MockNotificationModel.cpp
171 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 QQmlEngine::setObjectOwnership(n, QQmlEngine::CppOwnership);
94 int location = m_queue.size();
95 QModelIndex insertionPoint = QModelIndex();
96 beginInsertRows(insertionPoint, location, location);
97 m_queue.insert(location, n);
98 endInsertRows();
101MockNotification* MockNotificationModel::getNotification(int id) const {
102 for(int i = 0; i < m_queue.size(); i++) {
103 MockNotification* n = m_queue.at(i);
104 if(n && n->getID() == id) {
105 return n;
106 }
107 }
109 return nullptr;
112void MockNotificationModel::remove(const int id) {
113 for(int i = 0; i < m_queue.size(); i++) {
114 if(m_queue[i]->getID() == id) {
115 removeInternal(i);
116 return;
117 }
118 }
121void MockNotificationModel::removeSecond() {
122 if(m_queue.size() < 2)
123 return;
124 removeInternal(1);
127void MockNotificationModel::removeInternal(int loc) {
128 QModelIndex deletePoint = QModelIndex();
129 beginRemoveRows(deletePoint, loc, loc);
130 m_queue.erase(m_queue.begin() + loc);
131 endRemoveRows();
134MockNotification* MockNotificationModel::getRaw(const int notificationId) const {
135 return getNotification(notificationId);
138int MockNotificationModel::queued() const {
139 return m_queue.size();
142QHash<int, QByteArray> MockNotificationModel::roleNames() const {
143 QHash<int, QByteArray> roles;
145 roles.insert(ModelInterface::RoleType, "type");
146 roles.insert(ModelInterface::RoleUrgency, "urgency");
147 roles.insert(ModelInterface::RoleId, "id");
148 roles.insert(ModelInterface::RoleSummary, "summary");
149 roles.insert(ModelInterface::RoleBody, "body");
150 roles.insert(ModelInterface::RoleValue, "value");
151 roles.insert(ModelInterface::RoleIcon, "icon");
152 roles.insert(ModelInterface::RoleSecondaryIcon, "secondaryIcon");
153 roles.insert(ModelInterface::RoleActions, "actions");
154 roles.insert(ModelInterface::RoleHints, "hints");
155 roles.insert(ModelInterface::RoleNotification, "notification");
157 return roles;
160void MockNotificationModel::onCompleted(int id) {
161 remove(id);
164void MockNotificationModel::onDataChanged(int id) {
165 for(int i = 0; i < m_queue.size(); i++) {
166 if(m_queue[i]->getID() == id) {
167 Q_EMIT dataChanged(index(i, 0), index(i, 0));
168 break;
169 }
170 }