on daa5708 help sign in
unity8/tests/mocks/Unity/Notifications/MockNotificationModel.cpp
172 lines 5.0 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() {
40int MockNotificationModel::rowCount(const QModelIndex &) const {
41 return m_queue.size();
44int MockNotificationModel::getCount() const {
45 return m_queue.size();
48QVariant MockNotificationModel::data(const QModelIndex &index, int role) const {
49 if (!index.isValid())
50 return QVariant();
52 switch(role) {
53 case ModelInterface::RoleType:
54 return QVariant(m_queue[index.row()]->getType());
56 case ModelInterface::RoleId:
57 return QVariant(m_queue[index.row()]->getID());
59 case ModelInterface::RoleSummary:
60 return QVariant(m_queue[index.row()]->getSummary());
62 case ModelInterface::RoleBody:
63 return QVariant(m_queue[index.row()]->getBody());
65 case ModelInterface::RoleValue:
66 return QVariant(m_queue[index.row()]->getValue());
68 case ModelInterface::RoleIcon:
69 return QVariant(m_queue[index.row()]->getIcon());
71 case ModelInterface::RoleSecondaryIcon:
72 return QVariant(m_queue[index.row()]->getSecondaryIcon());
74 case ModelInterface::RoleActions:
75 return QVariant::fromValue(m_queue[index.row()]->getActions());
77 case ModelInterface::RoleHints:
78 return QVariant(m_queue[index.row()]->getHints());
80 case ModelInterface::RoleNotification:
81 return QVariant::fromValue(m_queue[index.row()]);
83 default:
84 return QVariant();
85 }
88void MockNotificationModel::append(MockNotification* n) {
89 int location = m_queue.size();
90 QModelIndex insertionPoint = QModelIndex();
91 beginInsertRows(insertionPoint, location, location);
92 m_queue.insert(location, n);
93 endInsertRows();
96MockNotification* MockNotificationModel::getNotification(int id) const {
97 for(int i=0; i < m_queue.size(); i++) {
98 if(m_queue[i]->getID() == id) {
99 return m_queue[i];
100 }
101 }
103 return nullptr;
106void MockNotificationModel::remove(const int id) {
107 for(int i = 0; i < m_queue.size(); i++) {
108 if(m_queue[i]->getID() == id) {
109 removeInternal(i);
110 return;
111 }
112 }
115void MockNotificationModel::removeSecond() {
116 if(m_queue.size() < 2)
117 return;
118 removeInternal(1);
121void MockNotificationModel::removeInternal(int loc) {
122 QModelIndex deletePoint = QModelIndex();
123 beginRemoveRows(deletePoint, loc, loc);
124 m_queue.erase(m_queue.begin() + loc);
125 endRemoveRows();
128MockNotification* MockNotificationModel::getRaw(const int notificationId) const {
129 for(int i = 0; i < m_queue.size(); i++) {
130 if(m_queue[i]->getID() == notificationId) {
131 MockNotification* n = m_queue[i];
132 return n;
133 }
134 }
136 return nullptr;
139int MockNotificationModel::queued() const {
140 return m_queue.size();
143QHash<int, QByteArray> MockNotificationModel::roleNames() const {
144 QHash<int, QByteArray> roles;
146 roles.insert(ModelInterface::RoleType, "type");
147 roles.insert(ModelInterface::RoleUrgency, "urgency");
148 roles.insert(ModelInterface::RoleId, "id");
149 roles.insert(ModelInterface::RoleSummary, "summary");
150 roles.insert(ModelInterface::RoleBody, "body");
151 roles.insert(ModelInterface::RoleValue, "value");
152 roles.insert(ModelInterface::RoleIcon, "icon");
153 roles.insert(ModelInterface::RoleSecondaryIcon, "secondaryIcon");
154 roles.insert(ModelInterface::RoleActions, "actions");
155 roles.insert(ModelInterface::RoleHints, "hints");
156 roles.insert(ModelInterface::RoleNotification, "notification");
158 return roles;
161void MockNotificationModel::onCompleted(int id) {
162 remove(id);
165void MockNotificationModel::onDataChanged(int id) {
166 for(int i = 0; i < m_queue.size(); i++) {
167 if(m_queue[i]->getID() == id) {
168 Q_EMIT dataChanged(index(i, 0), index(i, 0));
169 break;
170 }
171 }