1 /***************************************************************************
2 * Copyright (C) 2005-09 Canonical Ltd *
3 * author: aurelien.gateau@canonical.com *
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. *
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. *
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 ***************************************************************************/
21 #include "indicatornotificationbackend.h"
23 #include <qindicateserver.h>
24 #include <qindicateindicator.h>
27 #include "clientsettings.h"
29 #include "networkmodel.h"
33 #define XSTR(x) STR(x)
35 class Indicator : public QIndicate::Indicator {
37 uint lastNotificationId;
40 IndicatorNotificationBackend::IndicatorNotificationBackend(QObject *parent)
41 : AbstractNotificationBackend(parent)
43 NotificationSettings notificationSettings;
44 _enabled = notificationSettings.value("Indicator/Enabled", false).toBool();
46 notificationSettings.notify("Indicator/Enabled", this, SLOT(enabledChanged(const QVariant &)));
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()), QtUi::mainWindow(), SLOT(forceActivated()));
61 IndicatorNotificationBackend::~IndicatorNotificationBackend() {
62 qDeleteAll(_indicatorHash);
65 void IndicatorNotificationBackend::notify(const Notification ¬ification) {
69 if (notification.type != Highlight && notification.type != PrivMsg) {
72 BufferId bufferId = notification.bufferId;
73 Indicator *indicator = _indicatorHash.value(bufferId);
75 indicator = new Indicator;
76 _indicatorHash.insert(bufferId, indicator);
77 connect(indicator, SIGNAL(display(QIndicate::Indicator*)),
78 SLOT(indicatorDisplayed(QIndicate::Indicator*)));
80 indicator->lastNotificationId = notification.notificationId;
82 BufferInfo::Type type = Client::networkModel()->bufferType(bufferId);
84 if (type == BufferInfo::QueryBuffer) {
85 name = notification.sender;
87 name = QString("%1 (%2)")
88 .arg(Client::networkModel()->bufferName(bufferId))
89 .arg(notification.sender);
91 indicator->setNameProperty(name);
93 indicator->setTimeProperty(QDateTime::currentDateTime());
95 QModelIndex index = Client::networkModel()->bufferIndex(bufferId);
96 QVariant icon = QtUi::style()->bufferViewItemData(index, Qt::DecorationRole);
97 if (icon.canConvert<QPixmap>()) {
98 QImage image = icon.value<QPixmap>().toImage();
99 indicator->setIconProperty(image);
102 indicator->setDrawAttentionProperty(true);
106 void IndicatorNotificationBackend::close(uint notificationId) {
107 // If we find an indicator whose lastNotificationId is notificationId, we
110 IndicatorHash::Iterator
111 it = _indicatorHash.begin(),
112 end = _indicatorHash.end();
113 for (; it != end; ++it) {
114 if (it.value()->lastNotificationId == notificationId) {
119 // Not found, maybe there was no indicator for this notification or another
120 // notification happened after
123 _indicatorHash.erase(it);
127 void IndicatorNotificationBackend::enabledChanged(const QVariant &v) {
128 _enabled = v.toBool();
131 // TODO: Create indicators for existing highlighted buffers?
134 qDeleteAll(_indicatorHash);
138 void IndicatorNotificationBackend::indicatorDisplayed(QIndicate::Indicator *_indicator) {
139 Indicator *indicator = static_cast<Indicator *>(_indicator);
140 emit activated(indicator->lastNotificationId);
143 SettingsPage *IndicatorNotificationBackend::createConfigWidget() const {
144 return new ConfigWidget();
147 /***************************************************************************/
149 IndicatorNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
150 : SettingsPage("Internal", "IndicatorNotification", parent)
154 connect(ui.enabled, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
157 IndicatorNotificationBackend::ConfigWidget::~ConfigWidget() {
160 void IndicatorNotificationBackend::ConfigWidget::widgetChanged() {
161 bool changed = enabled != ui.enabled->isChecked();
163 if(changed != hasChanged()) setChangedState(changed);
166 bool IndicatorNotificationBackend::ConfigWidget::hasDefaults() const {
170 void IndicatorNotificationBackend::ConfigWidget::defaults() {
171 ui.enabled->setChecked(false);
175 void IndicatorNotificationBackend::ConfigWidget::load() {
176 NotificationSettings s;
177 enabled = s.value("Indicator/Enabled", false).toBool();
179 ui.enabled->setChecked(enabled);
180 setChangedState(false);
183 void IndicatorNotificationBackend::ConfigWidget::save() {
184 NotificationSettings s;
185 s.setValue("Indicator/Enabled", ui.enabled->isChecked());