Ported to libindicate 0.2.1.
[quassel.git] / src / qtui / indicatornotificationbackend.cpp
1 /***************************************************************************
2 *   Copyright (C) 2005-09 Canonical Ltd                                   *
3 *   author: aurelien.gateau@canonical.com                                 *
4 *                                                                         *
5 *   This program is free software; you can redistribute it and/or modify  *
6 *   it under the terms of the GNU General Public License as published by  *
7 *   the Free Software Foundation; either version 2 of the License, or     *
8 *   (at your option) version 3.                                           *
9 *                                                                         *
10 *   This program is distributed in the hope that it will be useful,       *
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13 *   GNU General Public License for more details.                          *
14 *                                                                         *
15 *   You should have received a copy of the GNU General Public License     *
16 *   along with this program; if not, write to the                         *
17 *   Free Software Foundation, Inc.,                                       *
18 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19 ***************************************************************************/
20
21 #include "indicatornotificationbackend.h"
22
23 #include <qindicateserver.h>
24 #include <qindicateindicator.h>
25
26 #include "client.h"
27 #include "clientsettings.h"
28 #include "mainwin.h"
29 #include "networkmodel.h"
30 #include "qtui.h"
31
32 class Indicator : public QIndicate::Indicator {
33 public:
34   uint lastNotificationId;
35 };
36
37 IndicatorNotificationBackend::IndicatorNotificationBackend(QObject *parent)
38   : AbstractNotificationBackend(parent)
39 {
40   NotificationSettings notificationSettings;
41   _enabled = notificationSettings.value("Indicator/Enabled", true).toBool();
42
43   notificationSettings.notify("Indicator/Enabled", this, SLOT(enabledChanged(const QVariant &)));
44
45   _server = QIndicate::Server::defaultInstance();
46   _server->setType("messaging");
47   _server->setDesktopFile(DESKTOP_FILE);
48   connect(_server, SIGNAL(serverDisplay()), QtUi::mainWindow(), SLOT(forceActivated()));
49
50   if (_enabled) {
51     _server->show();
52   }
53 }
54
55 IndicatorNotificationBackend::~IndicatorNotificationBackend() {
56   qDeleteAll(_indicatorHash);
57 }
58
59 void IndicatorNotificationBackend::notify(const Notification &notification) {
60   if(!_enabled) {
61     return;
62   }
63   if (notification.type != Highlight && notification.type != PrivMsg) {
64     return;
65   }
66   BufferId bufferId = notification.bufferId;
67   Indicator *indicator = _indicatorHash.value(bufferId);
68   if(!indicator) {
69     indicator = new Indicator;
70     _indicatorHash.insert(bufferId, indicator);
71     connect(indicator, SIGNAL(display(QIndicate::Indicator*)),
72       SLOT(indicatorDisplayed(QIndicate::Indicator*)));
73   }
74   indicator->lastNotificationId = notification.notificationId;
75
76   BufferInfo::Type type = Client::networkModel()->bufferType(bufferId);
77   QString name;
78   if (type == BufferInfo::QueryBuffer) {
79     name = notification.sender;
80   } else {
81     name = QString("%1 (%2)")
82       .arg(Client::networkModel()->bufferName(bufferId))
83       .arg(notification.sender);
84   }
85   indicator->setNameProperty(name);
86
87   indicator->setTimeProperty(QDateTime::currentDateTime());
88
89   QModelIndex index = Client::networkModel()->bufferIndex(bufferId);
90   QVariant icon = QtUi::style()->bufferViewItemData(index, Qt::DecorationRole);
91   if (icon.canConvert<QPixmap>()) {
92     QImage image = icon.value<QPixmap>().toImage();
93     indicator->setIconProperty(image);
94   }
95
96   indicator->setDrawAttentionProperty(true);
97   indicator->show();
98 }
99
100 void IndicatorNotificationBackend::close(uint notificationId) {
101   // If we find an indicator whose lastNotificationId is notificationId, we
102   // delete it
103
104   IndicatorHash::Iterator
105     it = _indicatorHash.begin(),
106     end = _indicatorHash.end();
107   for (; it != end; ++it) {
108     if (it.value()->lastNotificationId == notificationId) {
109       break;
110     }
111   }
112   if (it == end) {
113     // Not found, maybe there was no indicator for this notification or another
114     // notification happened after
115     return;
116   }
117   _indicatorHash.erase(it);
118   delete it.value();
119 }
120
121 void IndicatorNotificationBackend::enabledChanged(const QVariant &v) {
122   _enabled = v.toBool();
123   if (_enabled) {
124     _server->show();
125     // TODO: Create indicators for existing highlighted buffers?
126   } else {
127     _server->hide();
128     qDeleteAll(_indicatorHash);
129   }
130 }
131
132 void IndicatorNotificationBackend::indicatorDisplayed(QIndicate::Indicator *_indicator) {
133   Indicator *indicator = static_cast<Indicator *>(_indicator);
134   emit activated(indicator->lastNotificationId);
135 }
136
137 SettingsPage *IndicatorNotificationBackend::createConfigWidget() const {
138   return new ConfigWidget();
139 }
140
141 /***************************************************************************/
142
143 IndicatorNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
144 : SettingsPage("Internal", "IndicatorNotification", parent)
145 {
146   ui.setupUi(this);
147
148   connect(ui.enabled, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
149 }
150
151 IndicatorNotificationBackend::ConfigWidget::~ConfigWidget() {
152 }
153
154 void IndicatorNotificationBackend::ConfigWidget::widgetChanged() {
155   bool changed = enabled != ui.enabled->isChecked();
156
157   if(changed != hasChanged()) setChangedState(changed);
158 }
159
160 bool IndicatorNotificationBackend::ConfigWidget::hasDefaults() const {
161   return true;
162 }
163
164 void IndicatorNotificationBackend::ConfigWidget::defaults() {
165   ui.enabled->setChecked(false);
166   widgetChanged();
167 }
168
169 void IndicatorNotificationBackend::ConfigWidget::load() {
170   NotificationSettings s;
171   enabled = s.value("Indicator/Enabled", false).toBool();
172
173   ui.enabled->setChecked(enabled);
174   setChangedState(false);
175 }
176
177 void IndicatorNotificationBackend::ConfigWidget::save() {
178   NotificationSettings s;
179   s.setValue("Indicator/Enabled", ui.enabled->isChecked());
180   load();
181 }