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