Basic implementation of indicators.
[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 "clientsettings.h"
27 #include "mainwin.h"
28 #include "qtui.h"
29
30 class Indicator : public QIndicate::IndicatorMessage {
31 public:
32   uint lastNotificationId;
33 };
34
35 IndicatorNotificationBackend::IndicatorNotificationBackend(QObject *parent)
36   : AbstractNotificationBackend(parent)
37 {
38   NotificationSettings notificationSettings;
39   _enabled = notificationSettings.value("Indicator/Enabled", true).toBool();
40
41   notificationSettings.notify("Indicator/Enabled", this, SLOT(enabledChanged(const QVariant &)));
42
43   _server = QIndicate::Server::defaultInstance();
44   _server->setType("message.im");
45   _server->setDesktopFile(DESKTOP_FILE);
46   if (_enabled) {
47     _server->show();
48   }
49 }
50
51 IndicatorNotificationBackend::~IndicatorNotificationBackend() {
52   qDeleteAll(_indicatorHash);
53 }
54
55 void IndicatorNotificationBackend::notify(const Notification &notification) {
56   if(!_enabled) {
57     return;
58   }
59   if (notification.type != Highlight && notification.type != PrivMsg) {
60     return;
61   }
62   Indicator *indicator = _indicatorHash.value(notification.bufferId);
63   if(!indicator) {
64     indicator = new Indicator;
65     _indicatorHash.insert(notification.bufferId, indicator);
66   }
67   indicator->lastNotificationId = notification.notificationId;
68   indicator->setProperty("sender", notification.sender);
69   indicator->setProperty("time", QTime::currentTime());
70   qDebug() << "FIXME icon";
71   indicator->show();
72 }
73
74 void IndicatorNotificationBackend::close(uint notificationId) {
75   // If we find an indicator whose lastNotificationId is notificationId, we
76   // delete it
77
78   IndicatorHash::Iterator
79     it = _indicatorHash.begin(),
80     end = _indicatorHash.end();
81   for (; it != end; ++it) {
82     if (it.value()->lastNotificationId == notificationId) {
83       break;
84     }
85   }
86   if (it == end) {
87     // Not found, maybe there was no indicator for this notification or another
88     // notification happened after
89     return;
90   }
91   _indicatorHash.erase(it);
92   delete it.value();
93 }
94
95 void IndicatorNotificationBackend::enabledChanged(const QVariant &v) {
96   _enabled = v.toBool();
97   if (_enabled) {
98     _server->show();
99     // TODO: Create indicators for existing highlighted buffers?
100   } else {
101     _server->hide();
102     qDeleteAll(_indicatorHash);
103   }
104 }
105
106 SettingsPage *IndicatorNotificationBackend::createConfigWidget() const {
107   return new ConfigWidget();
108 }
109
110 /***************************************************************************/
111
112 IndicatorNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
113 : SettingsPage("Internal", "IndicatorNotification", parent)
114 {
115   ui.setupUi(this);
116
117   connect(ui.enabled, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
118 }
119
120 IndicatorNotificationBackend::ConfigWidget::~ConfigWidget() {
121 }
122
123 void IndicatorNotificationBackend::ConfigWidget::widgetChanged() {
124   bool changed = enabled != ui.enabled->isChecked();
125
126   if(changed != hasChanged()) setChangedState(changed);
127 }
128
129 bool IndicatorNotificationBackend::ConfigWidget::hasDefaults() const {
130   return true;
131 }
132
133 void IndicatorNotificationBackend::ConfigWidget::defaults() {
134   ui.enabled->setChecked(false);
135   widgetChanged();
136 }
137
138 void IndicatorNotificationBackend::ConfigWidget::load() {
139   NotificationSettings s;
140   enabled = s.value("Indicator/Enabled", false).toBool();
141
142   ui.enabled->setChecked(enabled);
143   setChangedState(false);
144 }
145
146 void IndicatorNotificationBackend::ConfigWidget::save() {
147   NotificationSettings s;
148   s.setValue("Indicator/Enabled", ui.enabled->isChecked());
149   load();
150 }