add some comments
[makneto-zunavac1.git] / src / ui-mobile / notifications-model.cpp
blob7b7c6d4767e735ed1d5dba9096461b1617865df5
1 /*
2 * Copyright (C) 2011 Lukáš Karas <lukas.karas@centrum.cz>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include "../libnotify-qt/Notification.h"
22 #include "notifications-model.h"
23 #include "session-model.h"
25 NotificationsModel::NotificationsModel(MaknetoBackend::TelepathyClient* client, MainWindow *window, QObject *parent) :
26 QAbstractListModel(parent),
27 _client(client),
28 lastId(0),
29 _window(window) {
31 if (!Notification::isInitted()) {
32 if (Notification::init("makneto-mobile")) {
33 qDebug() << "NotificationsModel: init notifications";
34 QStringList caps = Notification::getServerCaps();
35 if (!caps.contains("actions")) {
36 qWarning() << "NotificationsModel: notification server doesn't support actions";
38 } else {
39 qWarning() << "NotificationsModel: could not init Freedesktop Notification API";
43 QHash<int, QByteArray> roles;
45 // general roles
46 roles[IdRole] = "id";
47 roles[TitleRole] = "title";
48 roles[SessionIdRole] = "sessionId";
49 roles[SessionNameRole] = "sessionName";
50 roles[TypeRole] = "type";
51 roles[IconRole] = "icon";
52 roles[MessageRole] = "message";
54 setRoleNames(roles);
57 void NotificationsModel::addNotification(MaknetoBackend::Session *session, NotificationItem::NotificationType type, QString msg) {
59 emit beginInsertRows(QModelIndex(), _items.size(), _items.size());
60 NotificationItem *item = new NotificationItem(lastId++, session, type, msg);
62 connect(item, SIGNAL(acceptNotification(int)), this, SLOT(acceptNotification(int)));
63 connect(item, SIGNAL(declineNotification(int)), this, SLOT(declineNotification(int)));
64 connect(item, SIGNAL(notificationExpired(int)), this, SLOT(onNotificationExpired(int)));
66 _items.push_back(item);
67 _itemsMap.insert(item->getId(), item);
68 emit endInsertRows();
70 //emit sessionCreated(session->getUniqueName(), extendsExisting, requested);
73 NotificationsModel::~NotificationsModel() {
74 if (Notification::isInitted()) {
75 qDebug() << "NotificationsModel: de-init notifications";
76 Notification::uninit();
80 bool NotificationsModel::containsNotification(const QString &sessionId, NotificationItem::NotificationType type) {
81 QList<NotificationItem *>::iterator i = _items.begin();
82 while (i != _items.end()) {
83 if ((*i)->getSession()->getUniqueName() == sessionId && (*i)->getType() == type)
84 return true;
85 ++i;
87 return false;
90 int NotificationsModel::rowCount(const QModelIndex& index) const {
91 return _items.size();
94 QVariant NotificationsModel::data(int row, int role) {
95 return data(index(row, 0, QModelIndex()), role);
98 QVariant NotificationsModel::data(const QModelIndex& index, int role) const {
99 if (index.column() != 0)
100 return QVariant();
102 NotificationItem *item = _items.at(index.row());
103 if (!item)
104 return QVariant();
106 return item->data(role);
109 void NotificationsModel::acceptNotification(int id) {
110 NotificationItem *item = _itemsMap[id];
111 if (!item)
112 return;
114 if (_window)
115 _window->setVisibleAndActivate();
117 if (item->getType() == NotificationItem::ChatType) {
118 emit acceptChat(item->getSession()->getUniqueName());
120 if (item->getType() == NotificationItem::AudioCallType || item->getType() == NotificationItem::VideoCallType) {
121 emit acceptCall(item->getSession()->getUniqueName());
123 removeNotification(item, id);
126 void NotificationsModel::declineNotification(int id) {
127 NotificationItem *item = _itemsMap[id];
128 if (!item)
129 return;
131 if (item->getType() == NotificationItem::ChatType) {
132 emit rejectChat(item->getSession()->getUniqueName());
134 if (item->getType() == NotificationItem::AudioCallType || item->getType() == NotificationItem::VideoCallType) {
135 emit rejectCall(item->getSession()->getUniqueName());
137 removeNotification(item, id);
140 void NotificationsModel::onNotificationExpired(int id) {
141 declineNotification(id);
144 void NotificationsModel::removeNotification(NotificationItem *item, int id) {
145 int row = 0;
146 for (; row < _items.size(); row++) {
147 if (_items.at(row) == item)
148 break;
150 emit beginRemoveRows(QModelIndex(), row, row);
151 _items.removeOne(item);
152 _itemsMap.remove(id);
153 emit endRemoveRows();
154 emit notificationRemoved(item->getSession()->getUniqueName(), item->getType());
155 item->deleteLater();
158 #include "notifications-model.moc"