Systray icon improvements
[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) : AbstractNotificationBackend(parent) {
37   connect(QtUi::mainWindow()->systemTray(), SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
38                                             SLOT(notificationActivated(QSystemTrayIcon::ActivationReason)));
39 }
40
41 void KNotificationBackend::notify(const Notification &n) {
42   QString type;
43   switch(n.type) {
44     case Highlight:
45       type = "Highlight"; break;
46     case HighlightFocused:
47       type = "HighlightFocused"; break;
48     case PrivMsg:
49       type = "PrivMsg"; break;
50     case PrivMsgFocused:
51       type = "PrivMsgFocused"; break;
52   }
53
54   QString message = QString("<b>&lt;%1&gt;</b> %2").arg(n.sender, Qt::escape(n.message));
55   KNotification *notification = KNotification::event(type, message, DesktopIcon("dialog-information"), QtUi::mainWindow(),
56                                 KNotification::RaiseWidgetOnActivation
57                                |KNotification::CloseWhenWidgetActivated
58                                |KNotification::CloseOnTimeout);
59   connect(notification, SIGNAL(activated(uint)), SLOT(notificationActivated()));
60   connect(notification, SIGNAL(closed()), SLOT(notificationClosed()));
61   notification->setActions(QStringList("View"));
62   _notificationIds[notification] = n.notificationId;
63
64   QtUi::mainWindow()->systemTray()->setAlert(true);
65 }
66
67 void KNotificationBackend::close(uint notificationId) {
68   Q_UNUSED(notificationId);
69   QtUi::mainWindow()->systemTray()->setAlert(false);
70 }
71
72 void KNotificationBackend::notificationActivated() {
73   uint id = 0;
74   KNotification *n = qobject_cast<KNotification *>(sender());
75   if(n && _notificationIds.contains(n))
76     id = _notificationIds.value(n);
77
78   notificationActivated(id);
79 }
80
81 void KNotificationBackend::notificationActivated(QSystemTrayIcon::ActivationReason reason) {
82   if(reason == QSystemTrayIcon::Trigger && _notificationIds.count() > 0) {
83     notificationActivated(_notificationIds.values().at(0)); // we choose a random one for now
84   }
85 }
86
87 void KNotificationBackend::notificationActivated(uint notificationId) {
88   QHash<KNotification *, uint>::iterator i = _notificationIds.begin();
89   while(i != _notificationIds.end()) {
90     if(i.value() == notificationId)
91       i = _notificationIds.erase(i);
92     else
93       ++i;
94   }
95
96   QtUi::mainWindow()->systemTray()->setInhibitActivation();
97   emit activated(notificationId);
98
99   if(!_notificationIds.count())
100     QtUi::mainWindow()->systemTray()->setAlert(false);
101
102 }
103
104 void KNotificationBackend::notificationClosed() {
105   KNotification *n = qobject_cast<KNotification *>(sender());
106   if(n && _notificationIds.contains(n))
107     _notificationIds.remove(n);
108 }
109
110 SettingsPage *KNotificationBackend::createConfigWidget() const {
111   return new ConfigWidget();
112 }
113
114 /***************************************************************************/
115
116 KNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "KNotification", parent) {
117   _widget = new KNotifyConfigWidget(this);
118   _widget->setApplication("quassel");
119
120   QVBoxLayout *layout = new QVBoxLayout(this);
121   layout->addWidget(_widget);
122
123   connect(_widget, SIGNAL(changed(bool)), SLOT(widgetChanged(bool)));
124 }
125
126 void KNotificationBackend::ConfigWidget::widgetChanged(bool changed) {
127   setChangedState(changed);
128 }
129
130 void KNotificationBackend::ConfigWidget::load() {
131   setChangedState(false);
132 }
133
134 void KNotificationBackend::ConfigWidget::save() {
135   _widget->save();
136   load();
137 }