Fix tray icon behavior for non-KDE StatusNotifier
[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 "iconloader.h"
29 #include "mainwin.h"
30 #include "networkmodel.h"
31 #include "qtui.h"
32
33 #define STR(x) #x
34 #define XSTR(x) STR(x)
35
36 class Indicator : public QIndicate::Indicator {
37 public:
38   uint lastNotificationId;
39 };
40
41 IndicatorNotificationBackend::IndicatorNotificationBackend(QObject *parent)
42   : AbstractNotificationBackend(parent)
43 {
44   NotificationSettings notificationSettings;
45   _enabled = notificationSettings.value("Indicator/Enabled", false).toBool();
46
47   notificationSettings.notify("Indicator/Enabled", this, SLOT(enabledChanged(const QVariant &)));
48
49   _server = QIndicate::Server::defaultInstance();
50   _server->setType("message.irc");
51   QString desktopFile = QString("%1/%2.desktop")
52     .arg(XSTR(XDG_APPS_INSTALL_DIR))
53     .arg(QCoreApplication::applicationFilePath().section('/', -1));
54   _server->setDesktopFile(desktopFile);
55   connect(_server, SIGNAL(serverDisplay()), SLOT(activateMainWidget()));
56
57   if (_enabled) {
58     _server->show();
59   }
60 }
61
62 IndicatorNotificationBackend::~IndicatorNotificationBackend() {
63   qDeleteAll(_indicatorHash);
64 }
65
66 void IndicatorNotificationBackend::activateMainWidget() {
67   GraphicalUi::activateMainWidget();
68 }
69
70 void IndicatorNotificationBackend::notify(const Notification &notification) {
71   if(!_enabled) {
72     return;
73   }
74   if (notification.type != Highlight && notification.type != PrivMsg) {
75     return;
76   }
77   BufferId bufferId = notification.bufferId;
78   Indicator *indicator = _indicatorHash.value(bufferId);
79   if(!indicator) {
80     indicator = new Indicator;
81     _indicatorHash.insert(bufferId, indicator);
82     connect(indicator, SIGNAL(display(QIndicate::Indicator*)),
83       SLOT(indicatorDisplayed(QIndicate::Indicator*)));
84   }
85   indicator->lastNotificationId = notification.notificationId;
86
87   BufferInfo::Type type = Client::networkModel()->bufferType(bufferId);
88   QString name;
89   if (type == BufferInfo::QueryBuffer) {
90     name = notification.sender;
91   } else {
92     name = QString("%1 (%2)")
93       .arg(Client::networkModel()->bufferName(bufferId))
94       .arg(notification.sender);
95   }
96   indicator->setNameProperty(name);
97
98   indicator->setTimeProperty(QDateTime::currentDateTime());
99
100   QModelIndex index = Client::networkModel()->bufferIndex(bufferId);
101   QVariant icon = QtUi::style()->bufferViewItemData(index, Qt::DecorationRole);
102   if (icon.canConvert<QPixmap>()) {
103     QImage image = icon.value<QPixmap>().toImage();
104     indicator->setIconProperty(image);
105   }
106
107   indicator->setDrawAttentionProperty(true);
108   indicator->show();
109 }
110
111 void IndicatorNotificationBackend::close(uint notificationId) {
112   // If we find an indicator whose lastNotificationId is notificationId, we
113   // delete it
114
115   IndicatorHash::Iterator
116     it = _indicatorHash.begin(),
117     end = _indicatorHash.end();
118   for (; it != end; ++it) {
119     if (it.value()->lastNotificationId == notificationId) {
120       break;
121     }
122   }
123   if (it == end) {
124     // Not found, maybe there was no indicator for this notification or another
125     // notification happened after
126     return;
127   }
128   _indicatorHash.erase(it);
129   delete it.value();
130 }
131
132 void IndicatorNotificationBackend::enabledChanged(const QVariant &v) {
133   _enabled = v.toBool();
134   if (_enabled) {
135     _server->show();
136     // TODO: Create indicators for existing highlighted buffers?
137   } else {
138     _server->hide();
139     qDeleteAll(_indicatorHash);
140   }
141 }
142
143 void IndicatorNotificationBackend::indicatorDisplayed(QIndicate::Indicator *_indicator) {
144   Indicator *indicator = static_cast<Indicator *>(_indicator);
145   emit activated(indicator->lastNotificationId);
146 }
147
148 SettingsPage *IndicatorNotificationBackend::createConfigWidget() const {
149   return new ConfigWidget();
150 }
151
152 /***************************************************************************/
153
154 IndicatorNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
155 : SettingsPage("Internal", "IndicatorNotification", parent)
156 {
157   ui.setupUi(this);
158    // FIXME find proper icon (this one is used by the plasmoid as well)
159   ui.enabled->setIcon(SmallIcon("mail-message-new"));
160
161   connect(ui.enabled, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
162 }
163
164 IndicatorNotificationBackend::ConfigWidget::~ConfigWidget() {
165 }
166
167 void IndicatorNotificationBackend::ConfigWidget::widgetChanged() {
168   bool changed = enabled != ui.enabled->isChecked();
169
170   if(changed != hasChanged()) setChangedState(changed);
171 }
172
173 bool IndicatorNotificationBackend::ConfigWidget::hasDefaults() const {
174   return true;
175 }
176
177 void IndicatorNotificationBackend::ConfigWidget::defaults() {
178   ui.enabled->setChecked(false);
179   widgetChanged();
180 }
181
182 void IndicatorNotificationBackend::ConfigWidget::load() {
183   NotificationSettings s;
184   enabled = s.value("Indicator/Enabled", false).toBool();
185
186   ui.enabled->setChecked(enabled);
187   setChangedState(false);
188 }
189
190 void IndicatorNotificationBackend::ConfigWidget::save() {
191   NotificationSettings s;
192   s.setValue("Indicator/Enabled", ui.enabled->isChecked());
193   load();
194 }