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"
23 #include <qindicateserver.h>
24 #include <qindicateindicator.h>
27 #include "clientsettings.h"
28 #include "iconloader.h"
30 #include "networkmodel.h"
34 #define XSTR(x) STR(x)
36 class Indicator : public QIndicate::Indicator
39 uint lastNotificationId;
43 IndicatorNotificationBackend::IndicatorNotificationBackend(QObject *parent)
44 : AbstractNotificationBackend(parent)
46 NotificationSettings notificationSettings;
47 _enabled = notificationSettings.value("Indicator/Enabled", false).toBool();
49 notificationSettings.notify("Indicator/Enabled", this, SLOT(enabledChanged(const QVariant &)));
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()));
65 IndicatorNotificationBackend::~IndicatorNotificationBackend()
67 qDeleteAll(_indicatorHash);
71 void IndicatorNotificationBackend::activateMainWidget()
73 GraphicalUi::activateMainWidget();
77 void IndicatorNotificationBackend::notify(const Notification ¬ification)
82 if (notification.type != Highlight && notification.type != PrivMsg) {
85 BufferId bufferId = notification.bufferId;
86 Indicator *indicator = _indicatorHash.value(bufferId);
88 indicator = new Indicator;
89 _indicatorHash.insert(bufferId, indicator);
90 connect(indicator, SIGNAL(display(QIndicate::Indicator *)),
91 SLOT(indicatorDisplayed(QIndicate::Indicator *)));
93 indicator->lastNotificationId = notification.notificationId;
95 BufferInfo::Type type = Client::networkModel()->bufferType(bufferId);
97 if (type == BufferInfo::QueryBuffer) {
98 name = notification.sender;
101 name = QString("%1 (%2)")
102 .arg(Client::networkModel()->bufferName(bufferId))
103 .arg(notification.sender);
105 indicator->setNameProperty(name);
107 indicator->setTimeProperty(QDateTime::currentDateTime());
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);
116 indicator->setDrawAttentionProperty(true);
121 void IndicatorNotificationBackend::close(uint notificationId)
123 // If we find an indicator whose lastNotificationId is notificationId, we
126 IndicatorHash::Iterator
127 it = _indicatorHash.begin(),
128 end = _indicatorHash.end();
129 for (; it != end; ++it) {
130 if (it.value()->lastNotificationId == notificationId) {
135 // Not found, maybe there was no indicator for this notification or another
136 // notification happened after
139 _indicatorHash.erase(it);
144 void IndicatorNotificationBackend::enabledChanged(const QVariant &v)
146 _enabled = v.toBool();
149 // TODO: Create indicators for existing highlighted buffers?
153 qDeleteAll(_indicatorHash);
158 void IndicatorNotificationBackend::indicatorDisplayed(QIndicate::Indicator *_indicator)
160 Indicator *indicator = static_cast<Indicator *>(_indicator);
161 emit activated(indicator->lastNotificationId);
165 SettingsPage *IndicatorNotificationBackend::createConfigWidget() const
167 return new ConfigWidget();
171 /***************************************************************************/
173 IndicatorNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
174 : SettingsPage("Internal", "IndicatorNotification", parent)
177 // FIXME find proper icon (this one is used by the plasmoid as well)
178 ui.enabled->setIcon(SmallIcon("mail-message-new"));
180 connect(ui.enabled, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
184 IndicatorNotificationBackend::ConfigWidget::~ConfigWidget()
189 void IndicatorNotificationBackend::ConfigWidget::widgetChanged()
191 bool changed = enabled != ui.enabled->isChecked();
193 if (changed != hasChanged()) setChangedState(changed);
197 bool IndicatorNotificationBackend::ConfigWidget::hasDefaults() const
203 void IndicatorNotificationBackend::ConfigWidget::defaults()
205 ui.enabled->setChecked(false);
210 void IndicatorNotificationBackend::ConfigWidget::load()
212 NotificationSettings s;
213 enabled = s.value("Indicator/Enabled", false).toBool();
215 ui.enabled->setChecked(enabled);
216 setChangedState(false);
220 void IndicatorNotificationBackend::ConfigWidget::save()
222 NotificationSettings s;
223 s.setValue("Indicator/Enabled", ui.enabled->isChecked());