SystemTray refactoring in preparation of supporting StatusNotifier
[quassel.git] / src / qtui / knotificationbackend.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 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 <KNotification>
22 #include <KNotifyConfigWidget>
23 #include <QTextDocument>
24 #include <QVBoxLayout>
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
34 KNotificationBackend::KNotificationBackend(QObject *parent)
35 : AbstractNotificationBackend(parent)
36 {
37   connect(QtUi::mainWindow()->systemTray(), SIGNAL(activated(SystemTray::ActivationReason)),
38                                             SLOT(notificationActivated(SystemTray::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   notification->setActions(QStringList("View"));
61   notification->setProperty("notificationId", n.notificationId);
62
63   _notifications.append(qMakePair(n.notificationId, QPointer<KNotification>(notification)));
64
65   QtUi::mainWindow()->systemTray()->setAlert(true);
66 }
67
68 void KNotificationBackend::removeNotificationById(uint notificationId) {
69   QList<QPair<uint, QPointer<KNotification> > >::iterator i = _notifications.begin();
70   while(i != _notifications.end()) {
71     if(i->first == notificationId) {
72       if(i->second)
73         i->second->close();
74       i = _notifications.erase(i);
75     } else
76       ++i;
77   }
78 }
79
80 void KNotificationBackend::close(uint notificationId) {
81   removeNotificationById(notificationId);
82   if(!_notifications.count())
83     QtUi::mainWindow()->systemTray()->setAlert(false);
84 }
85
86 void KNotificationBackend::notificationActivated() {
87   uint id = 0;
88   KNotification *n = qobject_cast<KNotification *>(sender());
89   if(n)
90     id = n->property("notificationId").toUInt();
91
92   notificationActivated(id);
93 }
94
95 void KNotificationBackend::notificationActivated(SystemTray::ActivationReason reason) {
96   if(reason == SystemTray::Trigger && _notifications.count()) {
97     notificationActivated(_notifications.first().first); // oldest one
98   }
99 }
100
101 void KNotificationBackend::notificationActivated(uint notificationId) {
102   QtUi::mainWindow()->systemTray()->setInhibitActivation();
103   emit activated(notificationId);
104
105 }
106
107 SettingsPage *KNotificationBackend::createConfigWidget() const {
108   return new ConfigWidget();
109 }
110
111 /***************************************************************************/
112
113 KNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "KNotification", parent) {
114   _widget = new KNotifyConfigWidget(this);
115   _widget->setApplication("quassel");
116
117   QVBoxLayout *layout = new QVBoxLayout(this);
118   layout->addWidget(_widget);
119
120   connect(_widget, SIGNAL(changed(bool)), SLOT(widgetChanged(bool)));
121 }
122
123 void KNotificationBackend::ConfigWidget::widgetChanged(bool changed) {
124   setChangedState(changed);
125 }
126
127 void KNotificationBackend::ConfigWidget::load() {
128   setChangedState(false);
129 }
130
131 void KNotificationBackend::ConfigWidget::save() {
132   _widget->save();
133   load();
134 }