correct tab order for the settingspages
[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 #include <QTextDocument>
26
27 #include "knotificationbackend.h"
28
29 #include "client.h"
30 #include "icon.h"
31 #include "iconloader.h"
32 #include "networkmodel.h"
33 #include "qtui.h"
34 #include "systemtray.h"
35
36 KNotificationBackend::KNotificationBackend(QObject *parent)
37 : AbstractNotificationBackend(parent)
38 {
39   connect(QtUi::mainWindow()->systemTray(), SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
40                                             SLOT(notificationActivated(QSystemTrayIcon::ActivationReason)));
41 }
42
43 void KNotificationBackend::notify(const Notification &n) {
44   QString type;
45   switch(n.type) {
46     case Highlight:
47       type = "Highlight"; break;
48     case HighlightFocused:
49       type = "HighlightFocused"; break;
50     case PrivMsg:
51       type = "PrivMsg"; break;
52     case PrivMsgFocused:
53       type = "PrivMsgFocused"; break;
54   }
55
56   QString message = QString("<b>&lt;%1&gt;</b> %2").arg(n.sender, Qt::escape(n.message));
57   KNotification *notification = KNotification::event(type, message, DesktopIcon("dialog-information"), QtUi::mainWindow(),
58                                 KNotification::RaiseWidgetOnActivation
59                                |KNotification::CloseWhenWidgetActivated
60                                |KNotification::CloseOnTimeout);
61   connect(notification, SIGNAL(activated(uint)), SLOT(notificationActivated()));
62   notification->setActions(QStringList("View"));
63   notification->setProperty("notificationId", n.notificationId);
64
65   _notifications.append(qMakePair(n.notificationId, QPointer<KNotification>(notification)));
66
67   QtUi::mainWindow()->systemTray()->setAlert(true);
68 }
69
70 void KNotificationBackend::removeNotificationById(uint notificationId) {
71   QList<QPair<uint, QPointer<KNotification> > >::iterator i = _notifications.begin();
72   while(i != _notifications.end()) {
73     if(i->first == notificationId) {
74       if(i->second)
75         i->second->close();
76       i = _notifications.erase(i);
77     } else
78       ++i;
79   }
80 }
81
82 void KNotificationBackend::close(uint notificationId) {
83   removeNotificationById(notificationId);
84   if(!_notifications.count())
85     QtUi::mainWindow()->systemTray()->setAlert(false);
86 }
87
88 void KNotificationBackend::notificationActivated() {
89   uint id = 0;
90   KNotification *n = qobject_cast<KNotification *>(sender());
91   if(n)
92     id = n->property("notificationId").toUInt();
93
94   notificationActivated(id);
95 }
96
97 void KNotificationBackend::notificationActivated(QSystemTrayIcon::ActivationReason reason) {
98   if(reason == QSystemTrayIcon::Trigger && _notifications.count()) {
99     notificationActivated(_notifications.first().first); // oldest one
100   }
101 }
102
103 void KNotificationBackend::notificationActivated(uint notificationId) {
104   QtUi::mainWindow()->systemTray()->setInhibitActivation();
105   emit activated(notificationId);
106
107 }
108
109 SettingsPage *KNotificationBackend::createConfigWidget() const {
110   return new ConfigWidget();
111 }
112
113 /***************************************************************************/
114
115 KNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "KNotification", parent) {
116   _widget = new KNotifyConfigWidget(this);
117   _widget->setApplication("quassel");
118
119   QVBoxLayout *layout = new QVBoxLayout(this);
120   layout->addWidget(_widget);
121
122   connect(_widget, SIGNAL(changed(bool)), SLOT(widgetChanged(bool)));
123 }
124
125 void KNotificationBackend::ConfigWidget::widgetChanged(bool changed) {
126   setChangedState(changed);
127 }
128
129 void KNotificationBackend::ConfigWidget::load() {
130   setChangedState(false);
131 }
132
133 void KNotificationBackend::ConfigWidget::save() {
134   _widget->save();
135   load();
136 }