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