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