99d52f83d13562edd71a42569fe1d81288691d2a
[quassel.git] / src / qtui / knotificationbackend.cpp
1 /***************************************************************************
2 *   Copyright (C) 2005-09 by the Quassel Project                          *
3 *   devel@quassel-irc.org                                                 *
4 *                                                                         *
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.                                           *
9 *                                                                         *
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.                          *
14 *                                                                         *
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 ***************************************************************************/
20
21 #include <QVBoxLayout>
22
23 #include <KNotification>
24 #include <KNotifyConfigWidget>
25
26 #include "knotificationbackend.h"
27
28 #include "client.h"
29 #include "icon.h"
30 #include "iconloader.h"
31 #include "networkmodel.h"
32 #include "qtui.h"
33 #include "systemtray.h"
34
35 KNotificationBackend::KNotificationBackend(QObject *parent) : AbstractNotificationBackend(parent) {
36
37 }
38
39 void KNotificationBackend::notify(const Notification &n) {
40   QString type;
41   switch(n.type) {
42     case Highlight:
43       type = "Highlight"; break;
44     case HighlightFocused:
45       type = "HighlightFocused"; break;
46     case PrivMsg:
47       type = "PrivMsg"; break;
48     case PrivMsgFocused:
49       type = "PrivMsgFocused"; break;
50   }
51
52   QString message = QString("<b>&lt;%1&gt;</b> %2").arg(n.sender, n.message);
53   KNotification *notification = KNotification::event(type, message, DesktopIcon("dialog-information"), QtUi::mainWindow(),
54                                 KNotification::Persistent|KNotification::RaiseWidgetOnActivation|KNotification::CloseWhenWidgetActivated);
55   connect(notification, SIGNAL(activated(uint)), SLOT(notificationActivated()));
56   connect(notification, SIGNAL(closed()), SLOT(notificationClosed()));
57   notification->setActions(QStringList("View"));
58   _notificationIds[notification] = n.notificationId;
59
60   QtUi::mainWindow()->systemTray()->setAlert(true);
61 }
62
63 void KNotificationBackend::close(uint notificationId) {
64   Q_UNUSED(notificationId);
65   QtUi::mainWindow()->systemTray()->setAlert(false);
66 }
67
68 void KNotificationBackend::notificationActivated() {
69   uint id = 0;
70   KNotification *n = qobject_cast<KNotification *>(sender());
71   if(n && _notificationIds.contains(n))
72     id = _notificationIds.value(n);
73
74   emit activated(id);
75 }
76
77 void KNotificationBackend::notificationClosed() {
78   KNotification *n = qobject_cast<KNotification *>(sender());
79   if(n && _notificationIds.contains(n))
80     _notificationIds.remove(n);
81 }
82
83 SettingsPage *KNotificationBackend::createConfigWidget() const {
84   return new ConfigWidget();
85 }
86
87 /***************************************************************************/
88
89 KNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "KNotification", parent) {
90   _widget = new KNotifyConfigWidget(this);
91   _widget->setApplication("quassel");
92
93   QVBoxLayout *layout = new QVBoxLayout(this);
94   layout->addWidget(_widget);
95
96   connect(_widget, SIGNAL(changed(bool)), SLOT(widgetChanged(bool)));
97 }
98
99 void KNotificationBackend::ConfigWidget::widgetChanged(bool changed) {
100   setChangedState(changed);
101 }
102
103 void KNotificationBackend::ConfigWidget::load() {
104   setChangedState(false);
105 }
106
107 void KNotificationBackend::ConfigWidget::save() {
108   _widget->save();
109   load();
110 }