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"
32 #include "networkmodel.h"
36 #define XSTR(x) STR(x)
38 class Indicator : public QIndicate::Indicator
41 uint lastNotificationId;
45 IndicatorNotificationBackend::IndicatorNotificationBackend(QObject *parent)
46 : AbstractNotificationBackend(parent)
48 NotificationSettings notificationSettings;
49 _enabled = notificationSettings.value("Indicator/Enabled", false).toBool();
51 notificationSettings.notify("Indicator/Enabled", this, SLOT(enabledChanged(const QVariant &)));
53 _server = QIndicate::Server::defaultInstance();
54 _server->setType("message.irc");
55 QString desktopFile = QString("%1/%2.desktop")
56 .arg(XSTR(XDG_APPS_INSTALL_DIR))
57 .arg(QCoreApplication::applicationFilePath().section('/', -1));
58 _server->setDesktopFile(desktopFile);
59 connect(_server, SIGNAL(serverDisplay()), SLOT(activateMainWidget()));
67 IndicatorNotificationBackend::~IndicatorNotificationBackend()
69 qDeleteAll(_indicatorHash);
73 void IndicatorNotificationBackend::activateMainWidget()
75 GraphicalUi::activateMainWidget();
79 void IndicatorNotificationBackend::notify(const Notification ¬ification)
84 if (notification.type != Highlight && notification.type != PrivMsg) {
87 BufferId bufferId = notification.bufferId;
88 Indicator *indicator = _indicatorHash.value(bufferId);
90 indicator = new Indicator;
91 _indicatorHash.insert(bufferId, indicator);
92 connect(indicator, SIGNAL(display(QIndicate::Indicator *)),
93 SLOT(indicatorDisplayed(QIndicate::Indicator *)));
95 indicator->lastNotificationId = notification.notificationId;
97 BufferInfo::Type type = Client::networkModel()->bufferType(bufferId);
99 if (type == BufferInfo::QueryBuffer) {
100 name = notification.sender;
103 name = QString("%1 (%2)")
104 .arg(Client::networkModel()->bufferName(bufferId))
105 .arg(notification.sender);
107 indicator->setNameProperty(name);
109 indicator->setTimeProperty(QDateTime::currentDateTime());
111 QModelIndex index = Client::networkModel()->bufferIndex(bufferId);
112 QVariant icon = QtUi::style()->bufferViewItemData(index, Qt::DecorationRole);
113 if (icon.canConvert<QIcon>()) {
114 QImage image = icon.value<QIcon>().pixmap(16).toImage();
115 indicator->setIconProperty(image);
118 indicator->setDrawAttentionProperty(true);
123 void IndicatorNotificationBackend::close(uint notificationId)
125 // If we find an indicator whose lastNotificationId is notificationId, we
128 IndicatorHash::Iterator
129 it = _indicatorHash.begin(),
130 end = _indicatorHash.end();
131 for (; it != end; ++it) {
132 if (it.value()->lastNotificationId == notificationId) {
137 // Not found, maybe there was no indicator for this notification or another
138 // notification happened after
141 _indicatorHash.erase(it);
146 void IndicatorNotificationBackend::enabledChanged(const QVariant &v)
148 _enabled = v.toBool();
151 // TODO: Create indicators for existing highlighted buffers?
155 qDeleteAll(_indicatorHash);
160 void IndicatorNotificationBackend::indicatorDisplayed(QIndicate::Indicator *_indicator)
162 Indicator *indicator = static_cast<Indicator *>(_indicator);
163 emit activated(indicator->lastNotificationId);
167 SettingsPage *IndicatorNotificationBackend::createConfigWidget() const
169 return new ConfigWidget();
173 /***************************************************************************/
175 IndicatorNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
176 : SettingsPage("Internal", "IndicatorNotification", parent)
179 // FIXME find proper icon (this one is used by the plasmoid as well)
180 ui.enabled->setIcon(QIcon::fromTheme("mail-message-new"));
182 connect(ui.enabled, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
186 IndicatorNotificationBackend::ConfigWidget::~ConfigWidget()
191 void IndicatorNotificationBackend::ConfigWidget::widgetChanged()
193 bool changed = enabled != ui.enabled->isChecked();
195 if (changed != hasChanged()) setChangedState(changed);
199 bool IndicatorNotificationBackend::ConfigWidget::hasDefaults() const
205 void IndicatorNotificationBackend::ConfigWidget::defaults()
207 ui.enabled->setChecked(false);
212 void IndicatorNotificationBackend::ConfigWidget::load()
214 NotificationSettings s;
215 enabled = s.value("Indicator/Enabled", false).toBool();
217 ui.enabled->setChecked(enabled);
218 setChangedState(false);
222 void IndicatorNotificationBackend::ConfigWidget::save()
224 NotificationSettings s;
225 s.setValue("Indicator/Enabled", ui.enabled->isChecked());