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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "indicatornotificationbackend.h"
26 #include <qindicateserver.h>
27 #include <qindicateindicator.h>
30 #include "clientsettings.h"
33 #include "networkmodel.h"
37 #define XSTR(x) STR(x)
39 class Indicator : public QIndicate::Indicator
42 uint lastNotificationId;
46 IndicatorNotificationBackend::IndicatorNotificationBackend(QObject *parent)
47 : AbstractNotificationBackend(parent)
49 NotificationSettings notificationSettings;
50 _enabled = notificationSettings.value("Indicator/Enabled", false).toBool();
52 notificationSettings.notify("Indicator/Enabled", this, SLOT(enabledChanged(const QVariant &)));
54 _server = QIndicate::Server::defaultInstance();
55 _server->setType("message.irc");
56 QString desktopFile = QString("%1/%2.desktop")
57 .arg(XSTR(XDG_APPS_INSTALL_DIR))
58 .arg(QCoreApplication::applicationFilePath().section('/', -1));
59 _server->setDesktopFile(desktopFile);
60 connect(_server, SIGNAL(serverDisplay()), SLOT(activateMainWidget()));
68 IndicatorNotificationBackend::~IndicatorNotificationBackend()
70 qDeleteAll(_indicatorHash);
74 void IndicatorNotificationBackend::activateMainWidget()
76 GraphicalUi::activateMainWidget();
80 void IndicatorNotificationBackend::notify(const Notification ¬ification)
85 if (notification.type != Highlight && notification.type != PrivMsg) {
88 BufferId bufferId = notification.bufferId;
89 Indicator *indicator = _indicatorHash.value(bufferId);
91 indicator = new Indicator;
92 _indicatorHash.insert(bufferId, indicator);
93 connect(indicator, SIGNAL(display(QIndicate::Indicator *)),
94 SLOT(indicatorDisplayed(QIndicate::Indicator *)));
96 indicator->lastNotificationId = notification.notificationId;
98 BufferInfo::Type type = Client::networkModel()->bufferType(bufferId);
100 if (type == BufferInfo::QueryBuffer) {
101 name = notification.sender;
104 name = QString("%1 (%2)")
105 .arg(Client::networkModel()->bufferName(bufferId))
106 .arg(notification.sender);
108 indicator->setNameProperty(name);
110 indicator->setTimeProperty(QDateTime::currentDateTime());
112 QModelIndex index = Client::networkModel()->bufferIndex(bufferId);
113 QVariant icon = QtUi::style()->bufferViewItemData(index, Qt::DecorationRole);
114 if (icon.canConvert<QIcon>()) {
115 QImage image = icon.value<QIcon>().pixmap(16).toImage();
116 indicator->setIconProperty(image);
119 indicator->setDrawAttentionProperty(true);
124 void IndicatorNotificationBackend::close(uint notificationId)
126 // If we find an indicator whose lastNotificationId is notificationId, we
129 IndicatorHash::Iterator
130 it = _indicatorHash.begin(),
131 end = _indicatorHash.end();
132 for (; it != end; ++it) {
133 if (it.value()->lastNotificationId == notificationId) {
138 // Not found, maybe there was no indicator for this notification or another
139 // notification happened after
142 _indicatorHash.erase(it);
147 void IndicatorNotificationBackend::enabledChanged(const QVariant &v)
149 _enabled = v.toBool();
152 // TODO: Create indicators for existing highlighted buffers?
156 qDeleteAll(_indicatorHash);
161 void IndicatorNotificationBackend::indicatorDisplayed(QIndicate::Indicator *_indicator)
163 Indicator *indicator = static_cast<Indicator *>(_indicator);
164 emit activated(indicator->lastNotificationId);
168 SettingsPage *IndicatorNotificationBackend::createConfigWidget() const
170 return new ConfigWidget();
174 /***************************************************************************/
176 IndicatorNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
177 : SettingsPage("Internal", "IndicatorNotification", parent)
180 // FIXME find proper icon (this one is used by the plasmoid as well)
181 ui.enabled->setIcon(icon::get("mail-message-new"));
183 connect(ui.enabled, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
187 IndicatorNotificationBackend::ConfigWidget::~ConfigWidget()
192 void IndicatorNotificationBackend::ConfigWidget::widgetChanged()
194 bool changed = enabled != ui.enabled->isChecked();
196 if (changed != hasChanged()) setChangedState(changed);
200 bool IndicatorNotificationBackend::ConfigWidget::hasDefaults() const
206 void IndicatorNotificationBackend::ConfigWidget::defaults()
208 ui.enabled->setChecked(false);
213 void IndicatorNotificationBackend::ConfigWidget::load()
215 NotificationSettings s;
216 enabled = s.value("Indicator/Enabled", false).toBool();
218 ui.enabled->setChecked(enabled);
219 setChangedState(false);
223 void IndicatorNotificationBackend::ConfigWidget::save()
225 NotificationSettings s;
226 s.setValue("Indicator/Enabled", ui.enabled->isChecked());