Add a tooltip to the tray icon
[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   updateToolTip();
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   updateToolTip();
68   QtUi::mainWindow()->systemTray()->setAlert(true);
69 }
70
71 void KNotificationBackend::removeNotificationById(uint notificationId) {
72   QList<QPair<uint, QPointer<KNotification> > >::iterator i = _notifications.begin();
73   while(i != _notifications.end()) {
74     if(i->first == notificationId) {
75       if(i->second)
76         i->second->close();
77       i = _notifications.erase(i);
78     } else
79       ++i;
80   }
81   updateToolTip();
82 }
83
84 void KNotificationBackend::close(uint notificationId) {
85   removeNotificationById(notificationId);
86   if(!_notifications.count())
87     QtUi::mainWindow()->systemTray()->setAlert(false);
88 }
89
90 void KNotificationBackend::notificationActivated() {
91   uint id = 0;
92   KNotification *n = qobject_cast<KNotification *>(sender());
93   if(n)
94     id = n->property("notificationId").toUInt();
95
96   notificationActivated(id);
97 }
98
99 void KNotificationBackend::notificationActivated(SystemTray::ActivationReason reason) {
100   if(reason == SystemTray::Trigger) {
101     if( _notifications.count())
102       notificationActivated(_notifications.first().first); // oldest one
103     else
104       GraphicalUi::toggleMainWidget();
105   }
106 }
107
108 void KNotificationBackend::notificationActivated(uint notificationId) {
109   emit activated(notificationId);
110 }
111
112 void KNotificationBackend::updateToolTip() {
113   QtUi::mainWindow()->systemTray()->setToolTip("Quassel IRC",
114                                                _notifications.count()? tr("%n pending highlights", "", _notifications.count()) : QString());
115 }
116
117 SettingsPage *KNotificationBackend::createConfigWidget() const {
118   return new ConfigWidget();
119 }
120
121 /***************************************************************************/
122
123 KNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "KNotification", parent) {
124   _widget = new KNotifyConfigWidget(this);
125   _widget->setApplication("quassel");
126
127   QVBoxLayout *layout = new QVBoxLayout(this);
128   layout->addWidget(_widget);
129
130   connect(_widget, SIGNAL(changed(bool)), SLOT(widgetChanged(bool)));
131 }
132
133 void KNotificationBackend::ConfigWidget::widgetChanged(bool changed) {
134   setChangedState(changed);
135 }
136
137 void KNotificationBackend::ConfigWidget::load() {
138   setChangedState(false);
139 }
140
141 void KNotificationBackend::ConfigWidget::save() {
142   _widget->save();
143   load();
144 }