Support for icon.
[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 <qindicateindicatormessage.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::IndicatorMessage {
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("message.im");
47   _server->setDesktopFile(DESKTOP_FILE);
48   if (_enabled) {
49     _server->show();
50   }
51 }
52
53 IndicatorNotificationBackend::~IndicatorNotificationBackend() {
54   qDeleteAll(_indicatorHash);
55 }
56
57 void IndicatorNotificationBackend::notify(const Notification &notification) {
58   if(!_enabled) {
59     return;
60   }
61   if (notification.type != Highlight && notification.type != PrivMsg) {
62     return;
63   }
64   BufferId bufferId = notification.bufferId;
65   Indicator *indicator = _indicatorHash.value(bufferId);
66   if(!indicator) {
67     indicator = new Indicator;
68     _indicatorHash.insert(bufferId, indicator);
69   }
70   indicator->lastNotificationId = notification.notificationId;
71
72   BufferInfo::Type type = Client::networkModel()->bufferType(bufferId);
73   QString sender;
74   if (type == BufferInfo::QueryBuffer) {
75     sender = notification.sender;
76   } else {
77     sender = QString("%1 (%2)")
78       .arg(Client::networkModel()->bufferName(bufferId))
79       .arg(notification.sender);
80   }
81   indicator->setProperty("sender", sender);
82
83   indicator->setProperty("time", QTime::currentTime());
84
85   QModelIndex index = Client::networkModel()->bufferIndex(bufferId);
86   QVariant icon = QtUi::style()->bufferViewItemData(index, Qt::DecorationRole);
87   if (icon.canConvert<QPixmap>()) {
88     QImage image = icon.value<QPixmap>().toImage();
89     indicator->setProperty("icon", image);
90   }
91
92   indicator->show();
93 }
94
95 void IndicatorNotificationBackend::close(uint notificationId) {
96   // If we find an indicator whose lastNotificationId is notificationId, we
97   // delete it
98
99   IndicatorHash::Iterator
100     it = _indicatorHash.begin(),
101     end = _indicatorHash.end();
102   for (; it != end; ++it) {
103     if (it.value()->lastNotificationId == notificationId) {
104       break;
105     }
106   }
107   if (it == end) {
108     // Not found, maybe there was no indicator for this notification or another
109     // notification happened after
110     return;
111   }
112   _indicatorHash.erase(it);
113   delete it.value();
114 }
115
116 void IndicatorNotificationBackend::enabledChanged(const QVariant &v) {
117   _enabled = v.toBool();
118   if (_enabled) {
119     _server->show();
120     // TODO: Create indicators for existing highlighted buffers?
121   } else {
122     _server->hide();
123     qDeleteAll(_indicatorHash);
124   }
125 }
126
127 SettingsPage *IndicatorNotificationBackend::createConfigWidget() const {
128   return new ConfigWidget();
129 }
130
131 /***************************************************************************/
132
133 IndicatorNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
134 : SettingsPage("Internal", "IndicatorNotification", parent)
135 {
136   ui.setupUi(this);
137
138   connect(ui.enabled, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
139 }
140
141 IndicatorNotificationBackend::ConfigWidget::~ConfigWidget() {
142 }
143
144 void IndicatorNotificationBackend::ConfigWidget::widgetChanged() {
145   bool changed = enabled != ui.enabled->isChecked();
146
147   if(changed != hasChanged()) setChangedState(changed);
148 }
149
150 bool IndicatorNotificationBackend::ConfigWidget::hasDefaults() const {
151   return true;
152 }
153
154 void IndicatorNotificationBackend::ConfigWidget::defaults() {
155   ui.enabled->setChecked(false);
156   widgetChanged();
157 }
158
159 void IndicatorNotificationBackend::ConfigWidget::load() {
160   NotificationSettings s;
161   enabled = s.value("Indicator/Enabled", false).toBool();
162
163   ui.enabled->setChecked(enabled);
164   setChangedState(false);
165 }
166
167 void IndicatorNotificationBackend::ConfigWidget::save() {
168   NotificationSettings s;
169   s.setValue("Indicator/Enabled", ui.enabled->isChecked());
170   load();
171 }