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 <qindicateindicatormessage.h>
27 #include "clientsettings.h"
29 #include "networkmodel.h"
32 class Indicator : public QIndicate::IndicatorMessage {
34 uint lastNotificationId;
37 IndicatorNotificationBackend::IndicatorNotificationBackend(QObject *parent)
38 : AbstractNotificationBackend(parent)
40 NotificationSettings notificationSettings;
41 _enabled = notificationSettings.value("Indicator/Enabled", true).toBool();
43 notificationSettings.notify("Indicator/Enabled", this, SLOT(enabledChanged(const QVariant &)));
45 _server = QIndicate::Server::defaultInstance();
46 _server->setType("message.im");
47 _server->setDesktopFile(DESKTOP_FILE);
48 connect(_server, SIGNAL(serverDisplay()), QtUi::mainWindow(), SLOT(forceActivated()));
55 IndicatorNotificationBackend::~IndicatorNotificationBackend() {
56 qDeleteAll(_indicatorHash);
59 void IndicatorNotificationBackend::notify(const Notification ¬ification) {
63 if (notification.type != Highlight && notification.type != PrivMsg) {
66 BufferId bufferId = notification.bufferId;
67 Indicator *indicator = _indicatorHash.value(bufferId);
69 indicator = new Indicator;
70 _indicatorHash.insert(bufferId, indicator);
71 connect(indicator, SIGNAL(display()),
72 SLOT(indicatorDisplayed()));
74 indicator->lastNotificationId = notification.notificationId;
76 BufferInfo::Type type = Client::networkModel()->bufferType(bufferId);
78 if (type == BufferInfo::QueryBuffer) {
79 sender = notification.sender;
81 sender = QString("%1 (%2)")
82 .arg(Client::networkModel()->bufferName(bufferId))
83 .arg(notification.sender);
85 indicator->setProperty("sender", sender);
87 indicator->setProperty("time", QTime::currentTime());
89 QModelIndex index = Client::networkModel()->bufferIndex(bufferId);
90 QVariant icon = QtUi::style()->bufferViewItemData(index, Qt::DecorationRole);
91 if (icon.canConvert<QPixmap>()) {
92 QImage image = icon.value<QPixmap>().toImage();
93 indicator->setProperty("icon", image);
99 void IndicatorNotificationBackend::close(uint notificationId) {
100 // If we find an indicator whose lastNotificationId is notificationId, we
103 IndicatorHash::Iterator
104 it = _indicatorHash.begin(),
105 end = _indicatorHash.end();
106 for (; it != end; ++it) {
107 if (it.value()->lastNotificationId == notificationId) {
112 // Not found, maybe there was no indicator for this notification or another
113 // notification happened after
116 _indicatorHash.erase(it);
120 void IndicatorNotificationBackend::enabledChanged(const QVariant &v) {
121 _enabled = v.toBool();
124 // TODO: Create indicators for existing highlighted buffers?
127 qDeleteAll(_indicatorHash);
131 void IndicatorNotificationBackend::indicatorDisplayed() {
132 Indicator *indicator = static_cast<Indicator*>(sender());
133 emit activated(indicator->lastNotificationId);
136 SettingsPage *IndicatorNotificationBackend::createConfigWidget() const {
137 return new ConfigWidget();
140 /***************************************************************************/
142 IndicatorNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent)
143 : SettingsPage("Internal", "IndicatorNotification", parent)
147 connect(ui.enabled, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
150 IndicatorNotificationBackend::ConfigWidget::~ConfigWidget() {
153 void IndicatorNotificationBackend::ConfigWidget::widgetChanged() {
154 bool changed = enabled != ui.enabled->isChecked();
156 if(changed != hasChanged()) setChangedState(changed);
159 bool IndicatorNotificationBackend::ConfigWidget::hasDefaults() const {
163 void IndicatorNotificationBackend::ConfigWidget::defaults() {
164 ui.enabled->setChecked(false);
168 void IndicatorNotificationBackend::ConfigWidget::load() {
169 NotificationSettings s;
170 enabled = s.value("Indicator/Enabled", false).toBool();
172 ui.enabled->setChecked(enabled);
173 setChangedState(false);
176 void IndicatorNotificationBackend::ConfigWidget::save() {
177 NotificationSettings s;
178 s.setValue("Indicator/Enabled", ui.enabled->isChecked());